Skip to content

Commit e228297

Browse files
authored
Add further v2.0.1 Variables (#16)
* update MO Variable API * add Variables for WS URL * update changelog * update changelog
1 parent fb1292e commit e228297

File tree

3 files changed

+94
-37
lines changed

3 files changed

+94
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Added
66
- Mongoose v7.13 - v7.15 support ([#11](https://github.com/matth-x/MicroOcppMongoose/pull/11), [#14](https://github.com/matth-x/MicroOcppMongoose/pull/14))
7-
- OCPP 2.0.1 BasicAuthPassword integration ([#13](https://github.com/matth-x/MicroOcppMongoose/pull/13))
7+
- OCPP 2.0.1 Variables integration ([#13](https://github.com/matth-x/MicroOcppMongoose/pull/13), [#16](https://github.com/matth-x/MicroOcppMongoose/pull/16))
88

99
### Fixed
1010
- AuthorizationKey hex conversion ([#12](https://github.com/matth-x/MicroOcppMongoose/pull/12), [#15](https://github.com/matth-x/MicroOcppMongoose/pull/15))

src/MicroOcppMongooseClient.cpp

Lines changed: 87 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,53 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,
6464
readonly = true;
6565
}
6666

67-
setting_backend_url_str = declareConfiguration<const char*>(
68-
MO_CONFIG_EXT_PREFIX "BackendUrl", backend_url_factory, MO_WSCONN_FN, readonly, true);
69-
setting_cb_id_str = declareConfiguration<const char*>(
70-
MO_CONFIG_EXT_PREFIX "ChargeBoxId", charge_box_id_factory, MO_WSCONN_FN, readonly, true);
71-
7267
if (auth_key_factory_len > MO_AUTHKEY_LEN_MAX) {
7368
MO_DBG_WARN("auth_key_factory too long - will be cropped");
7469
auth_key_factory_len = MO_AUTHKEY_LEN_MAX;
7570
}
7671

7772
#if MO_ENABLE_V201
7873
if (protocolVersion.major == 2) {
79-
websocketSettings = makeVariableContainerVolatile(MO_WSCONN_FN_V201, true);
80-
auto variable = websocketSettings->createVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual);
81-
variable->setComponentId("SecurityCtrlr");
82-
variable->setName("BasicAuthPassword");
83-
char basicAuthPassword [MO_AUTHKEY_LEN_MAX + 1];
84-
snprintf(basicAuthPassword, sizeof(basicAuthPassword), "%.*s", (int)auth_key_factory_len, auth_key_factory ? (const char*)auth_key_factory : "");
85-
variable->setString(basicAuthPassword);
86-
websocketSettings->add(std::move(variable));
87-
basicAuthPasswordString = websocketSettings->getVariable("SecurityCtrlr", "BasicAuthPassword");
74+
websocketSettings = std::unique_ptr<VariableContainerOwning>(new VariableContainerOwning());
75+
if (filesystem) {
76+
websocketSettings->enablePersistency(filesystem, MO_WSCONN_FN_V201);
77+
}
78+
79+
auto csmsUrl = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual);
80+
csmsUrl->setComponentId("SecurityCtrlr");
81+
csmsUrl->setName("CsmsUrl");
82+
csmsUrl->setString(backend_url_factory ? backend_url_factory : "");
83+
csmsUrl->setPersistent();
84+
v201csmsUrlString = csmsUrl.get();
85+
websocketSettings->add(std::move(csmsUrl));
86+
87+
auto identity = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual);
88+
identity->setComponentId("SecurityCtrlr");
89+
identity->setName("Identity");
90+
identity->setString(charge_box_id_factory ? charge_box_id_factory : "");
91+
identity->setPersistent();
92+
v201identityString = identity.get();
93+
websocketSettings->add(std::move(identity));
94+
95+
auto basicAuthPassword = makeVariable(Variable::InternalDataType::String, Variable::AttributeType::Actual);
96+
basicAuthPassword->setComponentId("SecurityCtrlr");
97+
basicAuthPassword->setName("BasicAuthPassword");
98+
char basicAuthPasswordVal [MO_AUTHKEY_LEN_MAX + 1];
99+
snprintf(basicAuthPasswordVal, sizeof(basicAuthPasswordVal), "%.*s", (int)auth_key_factory_len, auth_key_factory ? (const char*)auth_key_factory : "");
100+
basicAuthPassword->setString(basicAuthPasswordVal);
101+
basicAuthPassword->setPersistent();
102+
v201basicAuthPasswordString = basicAuthPassword.get();
103+
websocketSettings->add(std::move(basicAuthPassword));
104+
105+
websocketSettings->load(); //if settings on flash already exist, this overwrites factory defaults
88106
} else
89107
#endif
90108
{
109+
setting_backend_url_str = declareConfiguration<const char*>(
110+
MO_CONFIG_EXT_PREFIX "BackendUrl", backend_url_factory, MO_WSCONN_FN, readonly, true);
111+
setting_cb_id_str = declareConfiguration<const char*>(
112+
MO_CONFIG_EXT_PREFIX "ChargeBoxId", charge_box_id_factory, MO_WSCONN_FN, readonly, true);
113+
91114
char auth_key_hex [2 * MO_AUTHKEY_LEN_MAX + 1];
92115
auth_key_hex[0] = '\0';
93116
if (auth_key_factory) {
@@ -366,10 +389,21 @@ void MOcppMongooseClient::setBackendUrl(const char *backend_url_cstr) {
366389
return;
367390
}
368391

369-
if (setting_backend_url_str) {
370-
setting_backend_url_str->setString(backend_url_cstr);
371-
configuration_save();
392+
#if MO_ENABLE_V201
393+
if (protocolVersion.major == 2) {
394+
if (v201csmsUrlString) {
395+
v201csmsUrlString->setString(backend_url_cstr);
396+
websocketSettings->commit();
397+
}
398+
} else
399+
#endif
400+
{
401+
if (setting_backend_url_str) {
402+
setting_backend_url_str->setString(backend_url_cstr);
403+
configuration_save();
404+
}
372405
}
406+
373407
}
374408

375409
void MOcppMongooseClient::setChargeBoxId(const char *cb_id_cstr) {
@@ -378,10 +412,21 @@ void MOcppMongooseClient::setChargeBoxId(const char *cb_id_cstr) {
378412
return;
379413
}
380414

381-
if (setting_cb_id_str) {
382-
setting_cb_id_str->setString(cb_id_cstr);
383-
configuration_save();
415+
#if MO_ENABLE_V201
416+
if (protocolVersion.major == 2) {
417+
if (v201identityString) {
418+
v201identityString->setString(cb_id_cstr);
419+
websocketSettings->commit();
420+
}
421+
} else
422+
#endif
423+
{
424+
if (setting_cb_id_str) {
425+
setting_cb_id_str->setString(cb_id_cstr);
426+
configuration_save();
427+
}
384428
}
429+
385430
}
386431

387432
void MOcppMongooseClient::setAuthKey(const char *auth_key_cstr) {
@@ -404,8 +449,8 @@ void MOcppMongooseClient::setAuthKey(const unsigned char *auth_key, size_t len)
404449
if (protocolVersion.major == 2) {
405450
char basicAuthPassword [MO_AUTHKEY_LEN_MAX + 1];
406451
snprintf(basicAuthPassword, sizeof(basicAuthPassword), "%.*s", (int)len, auth_key ? (const char*)auth_key : "");
407-
if (basicAuthPasswordString) {
408-
basicAuthPasswordString->setString(basicAuthPassword);
452+
if (v201basicAuthPasswordString) {
453+
v201basicAuthPasswordString->setString(basicAuthPassword);
409454
}
410455
} else
411456
#endif
@@ -433,23 +478,32 @@ void MOcppMongooseClient::reloadConfigs() {
433478
/*
434479
* reload WS credentials from configs
435480
*/
436-
if (setting_backend_url_str) {
437-
backend_url = setting_backend_url_str->getString();
438-
}
439-
440-
if (setting_cb_id_str) {
441-
cb_id = setting_cb_id_str->getString();
442-
}
443481

444482
#if MO_ENABLE_V201
445483
if (protocolVersion.major == 2) {
446-
if (basicAuthPasswordString) {
447-
snprintf((char*)auth_key, sizeof(auth_key), "%s", basicAuthPasswordString->getString());
484+
if (v201csmsUrlString) {
485+
backend_url = v201csmsUrlString->getString();
486+
}
487+
488+
if (v201identityString) {
489+
cb_id = v201identityString->getString();
490+
}
491+
492+
if (v201basicAuthPasswordString) {
493+
snprintf((char*)auth_key, sizeof(auth_key), "%s", v201basicAuthPasswordString->getString());
448494
auth_key_len = strlen((char*)auth_key);
449495
}
450496
} else
451497
#endif
452498
{
499+
if (setting_backend_url_str) {
500+
backend_url = setting_backend_url_str->getString();
501+
}
502+
503+
if (setting_cb_id_str) {
504+
cb_id = setting_cb_id_str->getString();
505+
}
506+
453507
if (setting_auth_key_hex_str) {
454508
auto auth_key_hex = setting_auth_key_hex_str->getString();
455509
auto auth_key_hex_len = strlen(setting_auth_key_hex_str->getString());
@@ -531,8 +585,8 @@ unsigned long MOcppMongooseClient::getLastConnected() {
531585
}
532586

533587
#if MO_ENABLE_V201
534-
std::shared_ptr<VariableContainer> MOcppMongooseClient::getVariableContainer() {
535-
return websocketSettings;
588+
VariableContainer *MOcppMongooseClient::getVariableContainer() {
589+
return websocketSettings.get();
536590
}
537591
#endif
538592

src/MicroOcppMongooseClient.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Configuration;
3636
#if MO_ENABLE_V201
3737
class Variable;
3838
class VariableContainer;
39+
class VariableContainerOwning;
3940
#endif
4041

4142
class MOcppMongooseClient : public MicroOcpp::Connection {
@@ -58,8 +59,10 @@ class MOcppMongooseClient : public MicroOcpp::Connection {
5859
std::shared_ptr<Configuration> ws_ping_interval_int; //heartbeat intervall in s. 0 sets hb off
5960
unsigned long last_hb {0};
6061
#if MO_ENABLE_V201
61-
std::shared_ptr<VariableContainer> websocketSettings;
62-
Variable *basicAuthPasswordString = nullptr;
62+
std::unique_ptr<VariableContainerOwning> websocketSettings;
63+
Variable *v201csmsUrlString = nullptr;
64+
Variable *v201identityString = nullptr;
65+
Variable *v201basicAuthPasswordString = nullptr;
6366
#endif
6467
bool connection_established {false};
6568
unsigned long last_connection_established {-1UL / 2UL};
@@ -133,7 +136,7 @@ class MOcppMongooseClient : public MicroOcpp::Connection {
133136
#if MO_ENABLE_V201
134137
//WS client creates and manages its own Variables. This getter function is a temporary solution, in future
135138
//the WS client will be initialized with a Context reference for registering the Variables directly
136-
std::shared_ptr<VariableContainer> getVariableContainer();
139+
VariableContainer *getVariableContainer();
137140
#endif
138141
};
139142

0 commit comments

Comments
 (0)