Skip to content

Commit 7e13005

Browse files
authored
Merge pull request #2 from matth-x/feature/explicit-apply
Add function to reload WS creds from configs
2 parents e3bf1e0 + ca56c0d commit 7e13005

File tree

4 files changed

+82
-82
lines changed

4 files changed

+82
-82
lines changed

src/MicroOcppMongooseClient.cpp

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,
4747
#if !MOCPP_CA_CERT_LOCAL
4848
setting_ca_cert_str = declareConfiguration<const char*>(
4949
MOCPP_CONFIG_EXT_PREFIX "CaCert", CA_cert_factory, MOCPP_WSCONN_FN, readonly, true);
50+
#else
51+
ca_cert = CA_cert_factory ? CA_cert_factory : "";
5052
#endif
5153

5254
ws_ping_interval_int = declareConfiguration<int>(
@@ -56,17 +58,9 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,
5658
stale_timeout_int = declareConfiguration<int>(
5759
MOCPP_CONFIG_EXT_PREFIX "StaleTimeout", 300, MOCPP_WSCONN_FN);
5860

59-
configuration_load(MOCPP_WSCONN_FN);
61+
configuration_load(MOCPP_WSCONN_FN); //load configs with values stored on flash
6062

61-
backend_url = setting_backend_url_str ? setting_backend_url_str->getString() : "";
62-
cb_id = setting_cb_id_str ? setting_cb_id_str->getString() : "";
63-
auth_key = setting_auth_key_str ? setting_auth_key_str->getString() : "";
64-
65-
#if !MOCPP_CA_CERT_LOCAL
66-
ca_cert = setting_ca_cert_str ? setting_ca_cert_str->getString() : "";
67-
#else
68-
ca_cert = CA_cert_factory ? CA_cert_factory : "";
69-
#endif
63+
reloadConfigs(); //load WS creds with configs values
7064

7165
#if defined(MOCPP_MG_VERSION_614)
7266
MOCPP_DBG_DEBUG("use MG version %s (tested with 6.14)", MG_VERSION);
@@ -153,11 +147,6 @@ void MOcppMongooseClient::maintainWsConn() {
153147
return;
154148
}
155149

156-
if (credentials_changed) {
157-
reload_credentials();
158-
credentials_changed = false;
159-
}
160-
161150
if (url.empty()) {
162151
//cannot open OCPP connection: credentials missing
163152
return;
@@ -227,126 +216,136 @@ void MOcppMongooseClient::maintainWsConn() {
227216

228217
}
229218

230-
void MOcppMongooseClient::reload_credentials() {
231-
url.clear();
232-
basic_auth64.clear();
233-
234-
if (backend_url.empty()) {
235-
MOCPP_DBG_DEBUG("empty URL closes connection");
219+
void MOcppMongooseClient::reconnect() {
220+
if (!websocket) {
236221
return;
237-
} else {
238-
url = backend_url;
239-
240-
if (url.back() != '/' && !cb_id.empty()) {
241-
url.append("/");
242-
}
243-
244-
url.append(cb_id);
245222
}
246-
247-
if (!auth_key.empty()) {
248-
std::string token = cb_id + ":" + auth_key;
249-
250-
MOCPP_DBG_DEBUG("auth Token=%s", token.c_str());
251-
252-
unsigned int base64_length = encode_base64_length(token.length());
253-
std::vector<unsigned char> base64 (base64_length + 1);
254-
255-
// encode_base64() places a null terminator automatically, because the output is a string
256-
base64_length = encode_base64((const unsigned char*) token.c_str(), token.length(), &base64[0]);
257-
258-
MOCPP_DBG_DEBUG("auth64 len=%u, auth64 Token=%s", base64_length, &base64[0]);
259-
260-
basic_auth64 = (const char*) &base64[0];
261-
} else {
262-
MOCPP_DBG_DEBUG("no authentication");
263-
(void) 0;
223+
#if defined(MOCPP_MG_VERSION_614)
224+
if (!connection_closing) {
225+
const char *msg = "socket closed by client";
226+
mg_send_websocket_frame(websocket, WEBSOCKET_OP_CLOSE, msg, strlen(msg));
264227
}
228+
#else
229+
websocket->is_closing = 1; //Mongoose will close the socket and the following maintainWsConn() call will open it again
230+
#endif
231+
setConnectionOpen(false);
265232
}
266233

267234
void MOcppMongooseClient::setBackendUrl(const char *backend_url_cstr) {
268235
if (!backend_url_cstr) {
269236
MOCPP_DBG_ERR("invalid argument");
270237
return;
271238
}
272-
backend_url = backend_url_cstr;
273239

274240
if (setting_backend_url_str) {
275241
setting_backend_url_str->setString(backend_url_cstr);
276242
configuration_save();
277243
}
278-
279-
credentials_changed = true; //reload composed credentials when reconnecting the next time
280-
281-
reconnect();
282244
}
283245

284246
void MOcppMongooseClient::setChargeBoxId(const char *cb_id_cstr) {
285247
if (!cb_id_cstr) {
286248
MOCPP_DBG_ERR("invalid argument");
287249
return;
288250
}
289-
cb_id = cb_id_cstr;
290251

291252
if (setting_cb_id_str) {
292253
setting_cb_id_str->setString(cb_id_cstr);
293254
configuration_save();
294255
}
295-
296-
credentials_changed = true; //reload composed credentials when reconnecting the next time
297-
298-
reconnect();
299256
}
300257

301258
void MOcppMongooseClient::setAuthKey(const char *auth_key_cstr) {
302259
if (!auth_key_cstr) {
303260
MOCPP_DBG_ERR("invalid argument");
304261
return;
305262
}
306-
auth_key = auth_key_cstr;
307263

308264
if (setting_auth_key_str) {
309265
setting_auth_key_str->setString(auth_key_cstr);
310266
configuration_save();
311267
}
312-
313-
credentials_changed = true; //reload composed credentials when reconnecting the next time
314-
315-
reconnect();
316268
}
317269

318270
void MOcppMongooseClient::setCaCert(const char *ca_cert_cstr) {
319271
if (!ca_cert_cstr) {
320272
MOCPP_DBG_ERR("invalid argument");
321273
return;
322274
}
323-
ca_cert = ca_cert_cstr;
324275

325276
#if !MOCPP_CA_CERT_LOCAL
326277
if (setting_ca_cert_str) {
327278
setting_ca_cert_str->setString(ca_cert_cstr);
328279
configuration_save();
329280
}
281+
#else
282+
ca_cert = ca_cert_cstr; //updated ca_cert takes immediate effect
330283
#endif
284+
}
331285

332-
credentials_changed = true; //reload composed credentials when reconnecting the next time
286+
void MOcppMongooseClient::reloadConfigs() {
333287

334-
reconnect();
335-
}
288+
reconnect(); //closes WS connection; will be reopened in next maintainWsConn execution
336289

337-
void MOcppMongooseClient::reconnect() {
338-
if (!websocket) {
339-
return;
290+
/*
291+
* reload WS credentials from configs
292+
*/
293+
if (setting_backend_url_str) {
294+
backend_url = setting_backend_url_str->getString();
340295
}
341-
#if defined(MOCPP_MG_VERSION_614)
342-
if (!connection_closing) {
343-
const char *msg = "socket closed by client";
344-
mg_send_websocket_frame(websocket, WEBSOCKET_OP_CLOSE, msg, strlen(msg));
296+
297+
if (setting_cb_id_str) {
298+
cb_id = setting_cb_id_str->getString();
299+
}
300+
301+
if (setting_auth_key_str) {
302+
auth_key = setting_auth_key_str->getString();
303+
}
304+
305+
#if !MOCPP_CA_CERT_LOCAL
306+
if (setting_ca_cert_str) {
307+
ca_cert = setting_ca_cert_str->getString();
345308
}
346-
#else
347-
websocket->is_closing = 1; //Mongoose will close the socket and the following maintainWsConn() call will open it again
348309
#endif
349-
setConnectionOpen(false);
310+
311+
/*
312+
* determine new URL and auth token with updated WS credentials
313+
*/
314+
315+
url.clear();
316+
basic_auth64.clear();
317+
318+
if (backend_url.empty()) {
319+
MOCPP_DBG_DEBUG("empty URL closes connection");
320+
return;
321+
} else {
322+
url = backend_url;
323+
324+
if (url.back() != '/' && !cb_id.empty()) {
325+
url.append("/");
326+
}
327+
328+
url.append(cb_id);
329+
}
330+
331+
if (!auth_key.empty()) {
332+
std::string token = cb_id + ":" + auth_key;
333+
334+
MOCPP_DBG_DEBUG("auth Token=%s", token.c_str());
335+
336+
unsigned int base64_length = encode_base64_length(token.length());
337+
std::vector<unsigned char> base64 (base64_length + 1);
338+
339+
// encode_base64() places a null terminator automatically, because the output is a string
340+
base64_length = encode_base64((const unsigned char*) token.c_str(), token.length(), &base64[0]);
341+
342+
MOCPP_DBG_DEBUG("auth64 len=%u, auth64 Token=%s", base64_length, &base64[0]);
343+
344+
basic_auth64 = (const char*) &base64[0];
345+
} else {
346+
MOCPP_DBG_DEBUG("no authentication");
347+
(void) 0;
348+
}
350349
}
351350

352351
void MOcppMongooseClient::setConnectionOpen(bool open) {

src/MicroOcppMongooseClient.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ class MOcppMongooseClient : public MicroOcpp::Connection {
6161
bool connection_closing {false};
6262
ReceiveTXTcallback receiveTXTcallback = [] (const char *, size_t) {return false;};
6363

64-
bool credentials_changed {true}; //set credentials to be reloaded
65-
void reload_credentials();
64+
void reconnect();
6665

6766
void maintainWsConn();
6867

@@ -88,12 +87,13 @@ class MOcppMongooseClient : public MicroOcpp::Connection {
8887
return receiveTXTcallback;
8988
}
9089

90+
//update WS configs. To apply the updates, call `reloadConfigs()` afterwards
9191
void setBackendUrl(const char *backend_url);
9292
void setChargeBoxId(const char *cb_id);
9393
void setAuthKey(const char *auth_key);
9494
void setCaCert(const char *ca_cert); //forwards this string to Mongoose as ssl_ca_cert (see https://github.com/cesanta/mongoose/blob/ab650ec5c99ceb52bb9dc59e8e8ec92a2724932b/mongoose.h#L4192)
9595

96-
void reconnect(); //after updating all credentials, reconnect to apply them
96+
void reloadConfigs();
9797

9898
const char *getBackendUrl() {return backend_url.c_str();}
9999
const char *getChargeBoxId() {return cb_id.c_str();}

src/MicroOcppMongooseClient_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ void ocpp_setCaCert(OCPP_Connection *sock, const char *ca_cert) {
7474
mgsock->setCaCert(ca_cert);
7575
}
7676

77-
void ocpp_reconnect(OCPP_Connection *sock) {
77+
void ocpp_reloadConfigs(OCPP_Connection *sock) {
7878
if (!sock) {
7979
MOCPP_DBG_ERR("invalid argument");
8080
return;
8181
}
8282
auto mgsock = reinterpret_cast<MOcppMongooseClient*>(sock);
83-
mgsock->reconnect();
83+
mgsock->reloadConfigs();
8484
}
8585

8686
const char *ocpp_getBackendUrl(OCPP_Connection *sock) {

src/MicroOcppMongooseClient_c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ OCPP_Connection *ocpp_makeConnection(struct mg_mgr *mgr,
2929

3030
void ocpp_deinitConnection(OCPP_Connection *sock);
3131

32+
//update WS configs. To apply the updates, call `ocpp_reloadConfigs()` afterwards
3233
void ocpp_setBackendUrl(OCPP_Connection *sock, const char *backend_url);
3334
void ocpp_setChargeBoxId(OCPP_Connection *sock, const char *cb_id);
3435
void ocpp_setAuthKey(OCPP_Connection *sock, const char *auth_key);
3536
void ocpp_setCaCert(OCPP_Connection *sock, const char *ca_cert);
3637

37-
void ocpp_reconnect(OCPP_Connection *sock); //after updating all credentials, reconnect to apply them
38+
void ocpp_reloadConfigs(OCPP_Connection *sock);
3839

3940
const char *ocpp_getBackendUrl(OCPP_Connection *sock);
4041
const char *ocpp_getChargeBoxId(OCPP_Connection *sock);

0 commit comments

Comments
 (0)