@@ -133,39 +133,73 @@ bool Context::setConnectionConfig(MO_ConnectionConfig connectionConfig) {
133133}
134134#endif // MO_WS_USE != MO_WS_CUSTOM
135135
136- void Context::setConnection (Connection *connection) {
136+ void Context::setConnection (MO_Connection *connection) {
137+ if (this ->connection != nullptr && connection != nullptr ) {
138+ MO_DBG_ERR (" switching connection during runtime not supported" );
139+ return ;
140+ }
141+
137142 if (this ->connection ) {
138- this ->connection ->setContext (nullptr );
143+ this ->connection ->ctx = nullptr ;
144+ }
145+ #if MO_ENABLE_MOCK_SERVER
146+ if (this ->connection && useMockConnection) {
147+ mo_loopback_free (this ->connection );
148+ this ->connection = nullptr ;
149+ useMockConnection = false ;
139150 }
151+ #endif // MO_ENABLE_MOCK_SERVER
140152#if MO_WS_USE != MO_WS_CUSTOM
141- if (this ->connection && isConnectionOwner ) {
142- freeDefaultConnection (this ->connection );
153+ if (this ->connection && useDefaultConnection ) {
154+ mo_freeDefaultConnection (this ->connection );
143155 this ->connection = nullptr ;
144- isConnectionOwner = false ;
156+ useDefaultConnection = false ;
145157 }
146158#endif // MO_WS_USE != MO_WS_CUSTOM
147159 this ->connection = connection;
148160 if (this ->connection ) {
149- this ->connection ->setContext (this );
161+ this ->connection ->ctx = reinterpret_cast <MO_Context*> (this );
150162 }
151163}
152164
153- Connection *Context::getConnection () {
165+ MO_Connection *Context::getConnection () {
166+ #if MO_ENABLE_MOCK_SERVER
167+ if (!connection && useMockConnection) {
168+ auto loopback = mo_loopback_make ();
169+ if (!loopback) {
170+ MO_DBG_ERR (" OOM" );
171+ return nullptr ;;
172+ }
173+ setConnection (loopback);
174+ }
175+ #endif // MO_ENABLE_MOCK_SERVER
154176 #if MO_WS_USE != MO_WS_CUSTOM
155177 if (!connection && connectionConfigDefined) {
156178 // init default Connection implementation
157- connection = static_cast <Connection*>( makeDefaultConnection ( connectionConfig, ocppVersion) );
158- if (!connection ) {
179+ auto defaultConnection = mo_makeDefaultConnection ( connectionConfig, ocppVersion);
180+ if (!defaultConnection ) {
159181 MO_DBG_ERR (" OOM" );
160182 return nullptr ;
161183 }
162- isConnectionOwner = true ;
184+ setConnection (defaultConnection);
185+ useDefaultConnection = true ;
163186 mo_connectionConfig_deinit (&connectionConfig);
187+ connectionConfigDefined = false ;
164188 }
165189 #endif // MO_WS_USE != MO_WS_CUSTOM
166190 return connection;
167191}
168192
193+ #if MO_ENABLE_MOCK_SERVER
194+ void Context::useMockServer () {
195+ if (connection) {
196+ MO_DBG_ERR (" switching to mock server mode after setting connection not supported" );
197+ return ;
198+ }
199+ useMockConnection = true ;
200+ }
201+ #endif // MO_ENABLE_MOCK_SERVER
202+
169203#if MO_ENABLE_MBEDTLS
170204void Context::setFtpConfig (MO_FTPConfig ftpConfig) {
171205 this ->ftpConfig = ftpConfig;
@@ -381,8 +415,8 @@ bool Context::setup() {
381415}
382416
383417void Context::loop () {
384- if (connection) {
385- connection->loop ();
418+ if (connection && connection-> loop ) {
419+ connection->loop (connection );
386420 }
387421 clock.loop ();
388422 msgService.loop ();
0 commit comments