From 3276da4af4b95c2e13cd59d1874af0a5fe7af5f3 Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sat, 2 Nov 2024 21:39:27 +0100 Subject: [PATCH 1/4] update MO Variable API --- src/MicroOcppMongooseClient.cpp | 13 ++++++++----- src/MicroOcppMongooseClient.h | 5 +++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/MicroOcppMongooseClient.cpp b/src/MicroOcppMongooseClient.cpp index 09b61dd..3911b8b 100644 --- a/src/MicroOcppMongooseClient.cpp +++ b/src/MicroOcppMongooseClient.cpp @@ -76,15 +76,18 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr, #if MO_ENABLE_V201 if (protocolVersion.major == 2) { - websocketSettings = makeVariableContainerVolatile(MO_WSCONN_FN_V201, true); - auto variable = websocketSettings->createVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual); + websocketSettings = std::unique_ptr(new VariableContainerOwning()); + if (filesystem) { + websocketSettings->enablePersistency(filesystem, MO_WSCONN_FN_V201); + } + auto variable = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual); variable->setComponentId("SecurityCtrlr"); variable->setName("BasicAuthPassword"); char basicAuthPassword [MO_AUTHKEY_LEN_MAX + 1]; snprintf(basicAuthPassword, sizeof(basicAuthPassword), "%.*s", (int)auth_key_factory_len, auth_key_factory ? (const char*)auth_key_factory : ""); variable->setString(basicAuthPassword); + basicAuthPasswordString = variable.get(); websocketSettings->add(std::move(variable)); - basicAuthPasswordString = websocketSettings->getVariable("SecurityCtrlr", "BasicAuthPassword"); } else #endif { @@ -531,8 +534,8 @@ unsigned long MOcppMongooseClient::getLastConnected() { } #if MO_ENABLE_V201 -std::shared_ptr MOcppMongooseClient::getVariableContainer() { - return websocketSettings; +VariableContainer *MOcppMongooseClient::getVariableContainer() { + return websocketSettings.get(); } #endif diff --git a/src/MicroOcppMongooseClient.h b/src/MicroOcppMongooseClient.h index 149b3d3..78d6df0 100644 --- a/src/MicroOcppMongooseClient.h +++ b/src/MicroOcppMongooseClient.h @@ -36,6 +36,7 @@ class Configuration; #if MO_ENABLE_V201 class Variable; class VariableContainer; +class VariableContainerOwning; #endif class MOcppMongooseClient : public MicroOcpp::Connection { @@ -58,7 +59,7 @@ class MOcppMongooseClient : public MicroOcpp::Connection { std::shared_ptr ws_ping_interval_int; //heartbeat intervall in s. 0 sets hb off unsigned long last_hb {0}; #if MO_ENABLE_V201 - std::shared_ptr websocketSettings; + std::unique_ptr websocketSettings; Variable *basicAuthPasswordString = nullptr; #endif bool connection_established {false}; @@ -133,7 +134,7 @@ class MOcppMongooseClient : public MicroOcpp::Connection { #if MO_ENABLE_V201 //WS client creates and manages its own Variables. This getter function is a temporary solution, in future //the WS client will be initialized with a Context reference for registering the Variables directly - std::shared_ptr getVariableContainer(); + VariableContainer *getVariableContainer(); #endif }; From ec0a5b97455787a8ee1cc334c5beefc700e6443f Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:18:09 +0100 Subject: [PATCH 2/4] add Variables for WS URL --- src/MicroOcppMongooseClient.cpp | 111 +++++++++++++++++++++++--------- src/MicroOcppMongooseClient.h | 4 +- 2 files changed, 84 insertions(+), 31 deletions(-) diff --git a/src/MicroOcppMongooseClient.cpp b/src/MicroOcppMongooseClient.cpp index 3911b8b..2567cb8 100644 --- a/src/MicroOcppMongooseClient.cpp +++ b/src/MicroOcppMongooseClient.cpp @@ -64,11 +64,6 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr, readonly = true; } - setting_backend_url_str = declareConfiguration( - MO_CONFIG_EXT_PREFIX "BackendUrl", backend_url_factory, MO_WSCONN_FN, readonly, true); - setting_cb_id_str = declareConfiguration( - MO_CONFIG_EXT_PREFIX "ChargeBoxId", charge_box_id_factory, MO_WSCONN_FN, readonly, true); - if (auth_key_factory_len > MO_AUTHKEY_LEN_MAX) { MO_DBG_WARN("auth_key_factory too long - will be cropped"); auth_key_factory_len = MO_AUTHKEY_LEN_MAX; @@ -80,17 +75,42 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr, if (filesystem) { websocketSettings->enablePersistency(filesystem, MO_WSCONN_FN_V201); } - auto variable = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual); - variable->setComponentId("SecurityCtrlr"); - variable->setName("BasicAuthPassword"); - char basicAuthPassword [MO_AUTHKEY_LEN_MAX + 1]; - snprintf(basicAuthPassword, sizeof(basicAuthPassword), "%.*s", (int)auth_key_factory_len, auth_key_factory ? (const char*)auth_key_factory : ""); - variable->setString(basicAuthPassword); - basicAuthPasswordString = variable.get(); - websocketSettings->add(std::move(variable)); + + auto csmsUrl = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual); + csmsUrl->setComponentId("SecurityCtrlr"); + csmsUrl->setName("CsmsUrl"); + csmsUrl->setString(backend_url_factory ? backend_url_factory : ""); + csmsUrl->setPersistent(); + v201csmsUrlString = csmsUrl.get(); + websocketSettings->add(std::move(csmsUrl)); + + auto identity = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual); + identity->setComponentId("SecurityCtrlr"); + identity->setName("Identity"); + identity->setString(charge_box_id_factory ? charge_box_id_factory : ""); + identity->setPersistent(); + v201identityString = identity.get(); + websocketSettings->add(std::move(identity)); + + auto basicAuthPassword = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual); + basicAuthPassword->setComponentId("SecurityCtrlr"); + basicAuthPassword->setName("BasicAuthPassword"); + char basicAuthPasswordVal [MO_AUTHKEY_LEN_MAX + 1]; + snprintf(basicAuthPasswordVal, sizeof(basicAuthPasswordVal), "%.*s", (int)auth_key_factory_len, auth_key_factory ? (const char*)auth_key_factory : ""); + basicAuthPassword->setString(basicAuthPasswordVal); + basicAuthPassword->setPersistent(); + v201basicAuthPasswordString = basicAuthPassword.get(); + websocketSettings->add(std::move(basicAuthPassword)); + + websocketSettings->load(); //if settings on flash already exist, this overwrites factory defaults } else #endif { + setting_backend_url_str = declareConfiguration( + MO_CONFIG_EXT_PREFIX "BackendUrl", backend_url_factory, MO_WSCONN_FN, readonly, true); + setting_cb_id_str = declareConfiguration( + MO_CONFIG_EXT_PREFIX "ChargeBoxId", charge_box_id_factory, MO_WSCONN_FN, readonly, true); + char auth_key_hex [2 * MO_AUTHKEY_LEN_MAX + 1]; auth_key_hex[0] = '\0'; if (auth_key_factory) { @@ -369,10 +389,21 @@ void MOcppMongooseClient::setBackendUrl(const char *backend_url_cstr) { return; } - if (setting_backend_url_str) { - setting_backend_url_str->setString(backend_url_cstr); - configuration_save(); +#if MO_ENABLE_V201 + if (protocolVersion.major == 2) { + if (v201csmsUrlString) { + v201csmsUrlString->setString(backend_url_cstr); + websocketSettings->commit(); + } + } else +#endif + { + if (setting_backend_url_str) { + setting_backend_url_str->setString(backend_url_cstr); + configuration_save(); + } } + } void MOcppMongooseClient::setChargeBoxId(const char *cb_id_cstr) { @@ -381,10 +412,21 @@ void MOcppMongooseClient::setChargeBoxId(const char *cb_id_cstr) { return; } - if (setting_cb_id_str) { - setting_cb_id_str->setString(cb_id_cstr); - configuration_save(); +#if MO_ENABLE_V201 + if (protocolVersion.major == 2) { + if (v201identityString) { + v201identityString->setString(cb_id_cstr); + websocketSettings->commit(); + } + } else +#endif + { + if (setting_cb_id_str) { + setting_cb_id_str->setString(cb_id_cstr); + configuration_save(); + } } + } void MOcppMongooseClient::setAuthKey(const char *auth_key_cstr) { @@ -407,8 +449,8 @@ void MOcppMongooseClient::setAuthKey(const unsigned char *auth_key, size_t len) if (protocolVersion.major == 2) { char basicAuthPassword [MO_AUTHKEY_LEN_MAX + 1]; snprintf(basicAuthPassword, sizeof(basicAuthPassword), "%.*s", (int)len, auth_key ? (const char*)auth_key : ""); - if (basicAuthPasswordString) { - basicAuthPasswordString->setString(basicAuthPassword); + if (v201basicAuthPasswordString) { + v201basicAuthPasswordString->setString(basicAuthPassword); } } else #endif @@ -436,23 +478,32 @@ void MOcppMongooseClient::reloadConfigs() { /* * reload WS credentials from configs */ - if (setting_backend_url_str) { - backend_url = setting_backend_url_str->getString(); - } - - if (setting_cb_id_str) { - cb_id = setting_cb_id_str->getString(); - } #if MO_ENABLE_V201 if (protocolVersion.major == 2) { - if (basicAuthPasswordString) { - snprintf((char*)auth_key, sizeof(auth_key), "%s", basicAuthPasswordString->getString()); + if (v201csmsUrlString) { + backend_url = v201csmsUrlString->getString(); + } + + if (v201identityString) { + cb_id = v201identityString->getString(); + } + + if (v201basicAuthPasswordString) { + snprintf((char*)auth_key, sizeof(auth_key), "%s", v201basicAuthPasswordString->getString()); auth_key_len = strlen((char*)auth_key); } } else #endif { + if (setting_backend_url_str) { + backend_url = setting_backend_url_str->getString(); + } + + if (setting_cb_id_str) { + cb_id = setting_cb_id_str->getString(); + } + if (setting_auth_key_hex_str) { auto auth_key_hex = setting_auth_key_hex_str->getString(); auto auth_key_hex_len = strlen(setting_auth_key_hex_str->getString()); diff --git a/src/MicroOcppMongooseClient.h b/src/MicroOcppMongooseClient.h index 78d6df0..759d040 100644 --- a/src/MicroOcppMongooseClient.h +++ b/src/MicroOcppMongooseClient.h @@ -60,7 +60,9 @@ class MOcppMongooseClient : public MicroOcpp::Connection { unsigned long last_hb {0}; #if MO_ENABLE_V201 std::unique_ptr websocketSettings; - Variable *basicAuthPasswordString = nullptr; + Variable *v201csmsUrlString = nullptr; + Variable *v201identityString = nullptr; + Variable *v201basicAuthPasswordString = nullptr; #endif bool connection_established {false}; unsigned long last_connection_established {-1UL / 2UL}; From c00c63e13ec1c3de262b8f26349ac5c5cdc997c9 Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:33:12 +0100 Subject: [PATCH 3/4] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98fe6a5..c980a71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Added - Mongoose v7.13 - v7.15 support ([#11](https://github.com/matth-x/MicroOcppMongoose/pull/11), [#14](https://github.com/matth-x/MicroOcppMongoose/pull/14)) -- OCPP 2.0.1 BasicAuthPassword integration ([#13](https://github.com/matth-x/MicroOcppMongoose/pull/13)) +- OCPP 2.0.1 Variables integration ([#13](https://github.com/matth-x/MicroOcppMongoose/pull/13)), ([#16](https://github.com/matth-x/MicroOcppMongoose/pull/16)) ### Fixed - AuthorizationKey hex conversion ([#12](https://github.com/matth-x/MicroOcppMongoose/pull/12), [#15](https://github.com/matth-x/MicroOcppMongoose/pull/15)) From 3ff76fd42e2ad937d0c49f42cf96e7abf158cf50 Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:34:15 +0100 Subject: [PATCH 4/4] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c980a71..5e184f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Added - Mongoose v7.13 - v7.15 support ([#11](https://github.com/matth-x/MicroOcppMongoose/pull/11), [#14](https://github.com/matth-x/MicroOcppMongoose/pull/14)) -- OCPP 2.0.1 Variables integration ([#13](https://github.com/matth-x/MicroOcppMongoose/pull/13)), ([#16](https://github.com/matth-x/MicroOcppMongoose/pull/16)) +- OCPP 2.0.1 Variables integration ([#13](https://github.com/matth-x/MicroOcppMongoose/pull/13), [#16](https://github.com/matth-x/MicroOcppMongoose/pull/16)) ### Fixed - AuthorizationKey hex conversion ([#12](https://github.com/matth-x/MicroOcppMongoose/pull/12), [#15](https://github.com/matth-x/MicroOcppMongoose/pull/15))