Skip to content

Commit 11aae0d

Browse files
Matthias Akstallermatth-x
authored andcommitted
fix C compatibility
1 parent 59d731d commit 11aae0d

20 files changed

Lines changed: 640 additions & 147 deletions

src/MicroOcpp.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,12 @@ bool mo_setWebsocketUrl(const char *backendUrl, const char *chargeBoxId, const c
136136
}
137137
#endif
138138

139-
#ifdef __cplusplus
140139
// Set a WebSocket Client
141-
void mo_setConnection(MicroOcpp::Connection *connection) {
140+
void mo_setConnection(MO_Connection *connection) {
142141
mo_setConnection2(mo_getApiContext(), connection);
143142
}
144143

145-
void mo_setConnection2(MO_Context *ctx, MicroOcpp::Connection *connection) {
144+
void mo_setConnection2(MO_Context *ctx, MO_Connection *connection) {
146145
if (!ctx) {
147146
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
148147
return;
@@ -151,7 +150,17 @@ void mo_setConnection2(MO_Context *ctx, MicroOcpp::Connection *connection) {
151150

152151
context->setConnection(connection);
153152
}
154-
#endif
153+
154+
#if MO_ENABLE_MOCK_SERVER
155+
void mo_useMockServer() {
156+
if (!g_context) {
157+
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
158+
return;
159+
}
160+
auto context = g_context;
161+
context->useMockServer();
162+
}
163+
#endif //MO_ENABLE_MOCK_SERVER
155164

156165
//Set the OCPP version
157166
void mo_setOcppVersion(int ocppVersion) {
@@ -1539,7 +1548,12 @@ bool mo_isConnected2(MO_Context *ctx) {
15391548
auto context = mo_getContext2(ctx);
15401549

15411550
if (auto connection = context->getConnection()) {
1542-
return connection->isConnected();
1551+
if (connection->isConnected) {
1552+
return connection->isConnected(connection);
1553+
} else {
1554+
// MO_Connection driver does not implement `isConnected()`. Default to true
1555+
return true;
1556+
}
15431557
} else {
15441558
MO_DBG_ERR("WebSocket not set up");
15451559
return false;

src/MicroOcpp.h

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,59 @@ bool mo_setWebsocketUrl(
6363
const char *CA_cert); //TLS certificate. Can be NULL. Zero-copy, must outlive MO. Set this to enable OCPP Security Profile 2
6464
#endif
6565

66-
#ifdef __cplusplus
6766
/* Set a WebSocket Client. Required if ArduinoWebsockets is not supported. This library requires
6867
* that you handle establishing the connection and keeping it alive. MO does not take ownership
6968
* of the passed `connection` object, i.e. it must be destroyed after `mo_deinitialize()` Please
7069
* refer to https://github.com/matth-x/MicroOcpp/tree/main/examples/ESP-TLS for an example how to
7170
* use it.
7271
*
73-
* This GitHub project also delivers an Connection implementation based on links2004/WebSockets. If
74-
* you need another WebSockets implementation, you can subclass the Connection class and pass it to
75-
* this initialize() function. Please refer to
72+
* This GitHub project also delivers an MO_Connection implementation based on links2004/WebSockets.
73+
* If you need another WebSockets implementation, you can implement the MO_Connection driver and
74+
* pass it to`mo_setConnection()` function. Please refer to
7675
* https://github.com/OpenEVSE/ESP32_WiFi_V4.x/blob/master/src/MongooseConnectionClient.cpp for
77-
* an example.
76+
* an example. Need to set MO_Connection after `mo_initialize()` and before `mo_setup()`.
77+
*
78+
* If migrating from MO v1, use `makeCppConnectionAdapter()` to adapt the C++ `Connection`
79+
* class to the C-style `MO_Connection` handle:
80+
* ```
81+
* mo_initialize();
82+
* MyConnectionClass *cppConnection = <legacy MO v1 WebSocket implementation>;
83+
* MO_Connection *connection = MicroOcpp::makeCppConnectionAdapter(cppConnection); //Does not take ownership of `cppConnection`
84+
* mo_setConnection(connection);
85+
* mo_setup();
86+
*
87+
* // ...
88+
*
89+
* mo_deinitialize();
90+
* MicroOcpp::freeCppConnectionAdapter(connection);
91+
* delete cppConnection;
92+
* ```
93+
*
94+
* Note: MO v2 does not reset the `Context` pointer in the C++ `Connection` anymore during
95+
* `mo_deinitialize()`. It's recommended to check `ctx` in `MO_Connection`, or `mo_getContext()`.
7896
*/
79-
void mo_setConnection(MicroOcpp::Connection *connection);
80-
void mo_setConnection2(MO_Context *ctx, MicroOcpp::Connection *connection);
97+
void mo_setConnection(MO_Connection *connection);
98+
void mo_setConnection2(MO_Context *ctx, MO_Connection *connection);
99+
100+
#ifdef __cplusplus
101+
/* Same as `mo_setConnection()`, but accepts the C++ `Connection` class from MO v1. Does not
102+
* transfer ownership of `cppConnection`. */
103+
bool mo_setCppConnection(MicroOcpp::Connection *cppConnection);
81104
#endif
82105

106+
#if MO_ENABLE_MOCK_SERVER
107+
/* Setup MicroOCPP with a mock OCPP server and don't connect to a real server. The mock server is
108+
* a minimal server-side implementation of the OCPP operations. If enabled, MO will reply to its
109+
* own OCPP messages. It's an alternative to providing MO with an OCPP URL or setting a Connection
110+
* handle.
111+
*
112+
* Tip: when integrating MO into a new platform, it is more convenient to first use the mock server,
113+
* and then switch to a real WebSocket connection. Also, it can be useful to narrow down connection
114+
* issues, or to develop offline, with no OCPP server in reach.
115+
*/
116+
void mo_useMockServer();
117+
#endif //MO_ENABLE_MOCK_SERVER
118+
83119
//Set the OCPP version `MO_OCPP_V16` or `MO_OCPP_V201`. Works only if build flags `MO_ENABLE_V16` or
84120
//`MO_ENABLE_V201` are set to 1
85121
void mo_setOcppVersion(int ocppVersion);

src/MicroOcpp/Context.cpp

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
170204
void Context::setFtpConfig(MO_FTPConfig ftpConfig) {
171205
this->ftpConfig = ftpConfig;
@@ -381,8 +415,8 @@ bool Context::setup() {
381415
}
382416

383417
void Context::loop() {
384-
if (connection) {
385-
connection->loop();
418+
if (connection && connection->loop) {
419+
connection->loop(connection);
386420
}
387421
clock.loop();
388422
msgService.loop();

src/MicroOcpp/Context.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ class Context : public MemoryManaged {
4242
bool isFilesystemOwner = false;
4343
#endif //MO_USE_FILEAPI != MO_CUSTOM_FS
4444
MO_FilesystemAdapter *filesystem = nullptr;
45-
45+
46+
#if MO_ENABLE_MOCK_SERVER
47+
bool useMockConnection = false;
48+
#endif //MO_ENABLE_MOCK_SERVER
4649
#if MO_WS_USE != MO_WS_CUSTOM
4750
MO_ConnectionConfig connectionConfig;
4851
bool connectionConfigDefined = false;
49-
bool isConnectionOwner = false;
52+
bool useDefaultConnection = false;
5053
#endif //MO_WS_USE != MO_WS_CUSTOM
51-
Connection *connection = nullptr;
54+
MO_Connection *connection = nullptr;
5255

5356
#if MO_ENABLE_MBEDTLS
5457
MO_FTPConfig ftpConfig;
@@ -93,8 +96,12 @@ class Context : public MemoryManaged {
9396
#if MO_WS_USE != MO_WS_CUSTOM
9497
bool setConnectionConfig(MO_ConnectionConfig connectionConfig);
9598
#endif //MO_WS_USE != MO_WS_CUSTOM
96-
void setConnection(Connection *connection);
97-
Connection *getConnection();
99+
void setConnection(MO_Connection *connection);
100+
MO_Connection *getConnection();
101+
102+
#if MO_ENABLE_MOCK_SERVER
103+
void useMockServer();
104+
#endif //MO_ENABLE_MOCK_SERVER
98105

99106
#if MO_ENABLE_MBEDTLS
100107
void setFtpConfig(MO_FTPConfig ftpConfig);

0 commit comments

Comments
 (0)