Skip to content

Commit e1c93a9

Browse files
committed
upgrade v201 compat in MicroOcpp.h
1 parent 2187f79 commit e1c93a9

File tree

6 files changed

+159
-45
lines changed

6 files changed

+159
-45
lines changed

src/MicroOcpp.cpp

Lines changed: 106 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
293293
new TransactionService(*context, filesystem, MO_NUM_EVSEID)));
294294
model.setRemoteControlService(std::unique_ptr<RemoteControlService>(
295295
new RemoteControlService(*context, MO_NUM_EVSEID)));
296+
model.setResetServiceV201(std::unique_ptr<Ocpp201::ResetService>(
297+
new Ocpp201::ResetService(*context)));
296298
} else
297299
#endif
298300
{
@@ -305,20 +307,24 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
305307
connectors.emplace_back(new Connector(*context, filesystem, connectorId));
306308
}
307309
model.setConnectors(std::move(connectors));
308-
}
309-
model.setHeartbeatService(std::unique_ptr<HeartbeatService>(
310-
new HeartbeatService(*context)));
311310

312311
#if MO_ENABLE_LOCAL_AUTH
313-
model.setAuthorizationService(std::unique_ptr<AuthorizationService>(
314-
new AuthorizationService(*context, filesystem)));
312+
model.setAuthorizationService(std::unique_ptr<AuthorizationService>(
313+
new AuthorizationService(*context, filesystem)));
315314
#endif //MO_ENABLE_LOCAL_AUTH
316315

317316
#if MO_ENABLE_RESERVATION
318-
model.setReservationService(std::unique_ptr<ReservationService>(
319-
new ReservationService(*context, MO_NUMCONNECTORS)));
317+
model.setReservationService(std::unique_ptr<ReservationService>(
318+
new ReservationService(*context, MO_NUMCONNECTORS)));
320319
#endif
321320

321+
model.setResetService(std::unique_ptr<ResetService>(
322+
new ResetService(*context)));
323+
}
324+
325+
model.setHeartbeatService(std::unique_ptr<HeartbeatService>(
326+
new HeartbeatService(*context)));
327+
322328
#if MO_ENABLE_CERT_MGMT && MO_ENABLE_CERT_STORE_MBEDTLS
323329
std::unique_ptr<CertificateStore> certStore = makeCertificateStoreMbedTLS(filesystem);
324330
if (certStore) {
@@ -330,18 +336,6 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
330336
}
331337
#endif
332338

333-
#if MO_ENABLE_V201
334-
if (version.major == 2) {
335-
//depends on VariableService
336-
model.setResetServiceV201(std::unique_ptr<Ocpp201::ResetService>(
337-
new Ocpp201::ResetService(*context)));
338-
} else
339-
#endif
340-
{
341-
model.setResetService(std::unique_ptr<ResetService>(
342-
new ResetService(*context)));
343-
}
344-
345339
#if !defined(MO_CUSTOM_UPDATER)
346340
#if MO_PLATFORM == MO_PLATFORM_ARDUINO && defined(ESP32) && MO_ENABLE_MBEDTLS
347341
model.setFirmwareService(
@@ -376,6 +370,12 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
376370

377371
configuration_load();
378372

373+
#if MO_ENABLE_V201
374+
if (version.major == 2) {
375+
model.getVariableService()->load();
376+
}
377+
#endif //MO_ENABLE_V201
378+
379379
MO_DBG_INFO("initialized MicroOcpp v" MO_VERSION " running OCPP %i.%i.%i", version.major, version.minor, version.patch);
380380
}
381381

@@ -422,48 +422,105 @@ void mocpp_loop() {
422422
context->loop();
423423
}
424424

425-
std::shared_ptr<Transaction> beginTransaction(const char *idTag, unsigned int connectorId) {
425+
bool beginTransaction(const char *idTag, unsigned int connectorId) {
426426
if (!context) {
427427
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
428-
return nullptr;
428+
return false;
429+
}
430+
431+
#if MO_ENABLE_V201
432+
if (context->getVersion().major == 2) {
433+
if (!idTag || strnlen(idTag, MO_IDTOKEN_LEN_MAX + 2) > MO_IDTOKEN_LEN_MAX) {
434+
MO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", MO_IDTOKEN_LEN_MAX);
435+
return false;
436+
}
437+
TransactionService::Evse *evse = nullptr;
438+
if (auto txService = context->getModel().getTransactionService()) {
439+
evse = txService->getEvse(connectorId);
440+
}
441+
if (!evse) {
442+
MO_DBG_ERR("could not find EVSE");
443+
return false;
444+
}
445+
return evse->beginAuthorization(idTag, true);
429446
}
447+
#endif
448+
430449
if (!idTag || strnlen(idTag, IDTAG_LEN_MAX + 2) > IDTAG_LEN_MAX) {
431450
MO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", IDTAG_LEN_MAX);
432-
return nullptr;
451+
return false;
433452
}
434453
auto connector = context->getModel().getConnector(connectorId);
435454
if (!connector) {
436455
MO_DBG_ERR("could not find connector");
437-
return nullptr;
456+
return false;
438457
}
439458

440-
return connector->beginTransaction(idTag);
459+
return connector->beginTransaction(idTag) != nullptr;
441460
}
442461

443-
std::shared_ptr<Transaction> beginTransaction_authorized(const char *idTag, const char *parentIdTag, unsigned int connectorId) {
462+
bool beginTransaction_authorized(const char *idTag, const char *parentIdTag, unsigned int connectorId) {
444463
if (!context) {
445464
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
446-
return nullptr;
465+
return false;
466+
}
467+
468+
#if MO_ENABLE_V201
469+
if (context->getVersion().major == 2) {
470+
if (!idTag || strnlen(idTag, MO_IDTOKEN_LEN_MAX + 2) > MO_IDTOKEN_LEN_MAX) {
471+
MO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", MO_IDTOKEN_LEN_MAX);
472+
return false;
473+
}
474+
TransactionService::Evse *evse = nullptr;
475+
if (auto txService = context->getModel().getTransactionService()) {
476+
evse = txService->getEvse(connectorId);
477+
}
478+
if (!evse) {
479+
MO_DBG_ERR("could not find EVSE");
480+
return false;
481+
}
482+
return evse->beginAuthorization(idTag, false);
447483
}
484+
#endif
485+
448486
if (!idTag || strnlen(idTag, IDTAG_LEN_MAX + 2) > IDTAG_LEN_MAX ||
449487
(parentIdTag && strnlen(parentIdTag, IDTAG_LEN_MAX + 2) > IDTAG_LEN_MAX)) {
450488
MO_DBG_ERR("(parent)idTag format violation. Expect c-style string with at most %u characters", IDTAG_LEN_MAX);
451-
return nullptr;
489+
return false;
452490
}
453491
auto connector = context->getModel().getConnector(connectorId);
454492
if (!connector) {
455493
MO_DBG_ERR("could not find connector");
456-
return nullptr;
494+
return false;
457495
}
458496

459-
return connector->beginTransaction_authorized(idTag, parentIdTag);
497+
return connector->beginTransaction_authorized(idTag, parentIdTag) != nullptr;
460498
}
461499

462500
bool endTransaction(const char *idTag, const char *reason, unsigned int connectorId) {
463501
if (!context) {
464502
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
465503
return false;
466504
}
505+
506+
#if MO_ENABLE_V201
507+
if (context->getVersion().major == 2) {
508+
if (!idTag || strnlen(idTag, MO_IDTOKEN_LEN_MAX + 2) > MO_IDTOKEN_LEN_MAX) {
509+
MO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", MO_IDTOKEN_LEN_MAX);
510+
return false;
511+
}
512+
TransactionService::Evse *evse = nullptr;
513+
if (auto txService = context->getModel().getTransactionService()) {
514+
evse = txService->getEvse(connectorId);
515+
}
516+
if (!evse) {
517+
MO_DBG_ERR("could not find EVSE");
518+
return false;
519+
}
520+
return evse->endAuthorization(idTag, true);
521+
}
522+
#endif
523+
467524
bool res = false;
468525
if (isTransactionActive(connectorId) && getTransactionIdTag(connectorId)) {
469526
//end transaction now if either idTag is nullptr (i.e. force stop) or the idTag matches beginTransaction
@@ -524,6 +581,25 @@ bool endTransaction_authorized(const char *idTag, const char *reason, unsigned i
524581
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
525582
return false;
526583
}
584+
585+
#if MO_ENABLE_V201
586+
if (context->getVersion().major == 2) {
587+
if (!idTag || strnlen(idTag, MO_IDTOKEN_LEN_MAX + 2) > MO_IDTOKEN_LEN_MAX) {
588+
MO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", MO_IDTOKEN_LEN_MAX);
589+
return false;
590+
}
591+
TransactionService::Evse *evse = nullptr;
592+
if (auto txService = context->getModel().getTransactionService()) {
593+
evse = txService->getEvse(connectorId);
594+
}
595+
if (!evse) {
596+
MO_DBG_ERR("could not find EVSE");
597+
return false;
598+
}
599+
return evse->endAuthorization(idTag, false);
600+
}
601+
#endif
602+
527603
auto connector = context->getModel().getConnector(connectorId);
528604
if (!connector) {
529605
MO_DBG_ERR("could not find connector");
@@ -1003,6 +1079,7 @@ void setOccupiedInput(std::function<bool()> occupied, unsigned int connectorId)
10031079
evse->setOccupiedInput(occupied);
10041080
}
10051081
}
1082+
return;
10061083
}
10071084
#endif
10081085
auto connector = context->getModel().getConnector(connectorId);

src/MicroOcpp.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void mocpp_loop();
127127
/*
128128
* Transaction management.
129129
*
130+
* OCPP 1.6 (2.0.1 see below):
130131
* Begin the transaction process and prepare it. When all conditions for the transaction are true,
131132
* eventually send a StartTransaction request to the OCPP server.
132133
* Conditions:
@@ -140,18 +141,24 @@ void mocpp_loop();
140141
*
141142
* See beginTransaction_authorized for skipping steps 1) to 3)
142143
*
143-
* Returns the transaction object if it was possible to create the transaction process. Returns
144-
* nullptr if either another transaction process is still active or you need to try it again later.
144+
* Returns true if it was possible to create the transaction process. Returns
145+
* false if either another transaction process is still active or you need to try it again later.
146+
*
147+
* OCPP 2.0.1:
148+
* Authorize a transaction. Like the OCPP 1.6 behavior, this should be called when the user swipes the
149+
* card to start charging, but the semantic is slightly different. This function begins the authorized
150+
* phase, but a transaction may already have started due to an earlier transaction start point.
145151
*/
146-
std::shared_ptr<MicroOcpp::Transaction> beginTransaction(const char *idTag, unsigned int connectorId = 1);
152+
bool beginTransaction(const char *idTag, unsigned int connectorId = 1);
147153

148154
/*
149155
* Begin the transaction process and skip the OCPP-side authorization. See beginTransaction(...) for a
150156
* complete description
151157
*/
152-
std::shared_ptr<MicroOcpp::Transaction> beginTransaction_authorized(const char *idTag, const char *parentIdTag = nullptr, unsigned int connectorId = 1);
158+
bool beginTransaction_authorized(const char *idTag, const char *parentIdTag = nullptr, unsigned int connectorId = 1);
153159

154160
/*
161+
* OCPP 1.6 (2.0.1 see below):
155162
* End the transaction process if idTag is authorized to stop the transaction. The OCPP lib sends
156163
* a StopTransaction request if the following conditions are true:
157164
* Conditions:
@@ -181,6 +188,15 @@ std::shared_ptr<MicroOcpp::Transaction> beginTransaction_authorized(const char *
181188
* `endTransaction_authorized(nullptr, reason);`
182189
*
183190
* Returns true if there is a transaction which could eventually be ended by this action
191+
*
192+
* OCPP 2.0.1:
193+
* End the user authorization. Like when running with OCPP 1.6, this should be called when the user
194+
* swipes the card to stop charging. The difference between the 1.6/2.0.1 behavior is that in 1.6,
195+
* endTransaction always sets the transaction inactive so that it wants to stop. In 2.0.1, this only
196+
* revokes the user authorization which may terminate the transaction but doesn't have to if the
197+
* transaction stop point is set to EvConnected.
198+
*
199+
* Note: the stop reason parameter is ignored when running with OCPP 2.0.1. It's always Local
184200
*/
185201
bool endTransaction(const char *idTag = nullptr, const char *reason = nullptr, unsigned int connectorId = 1);
186202

@@ -238,6 +254,14 @@ const char *getTransactionIdTag(unsigned int connectorId = 1);
238254
*/
239255
std::shared_ptr<MicroOcpp::Transaction>& getTransaction(unsigned int connectorId = 1);
240256

257+
#if MO_ENABLE_V201
258+
/*
259+
* OCPP 2.0.1 version of getTransaction(). Note that the return transaction object is of another type
260+
* and unlike the 1.6 version, this function does not give ownership.
261+
*/
262+
MicroOcpp::Ocpp201::Transaction *getTransactionV201(unsigned int evseId = 1);
263+
#endif //MO_ENABLE_V201
264+
241265
/*
242266
* Returns if the OCPP library allows the EVSE to charge at the moment.
243267
*

src/MicroOcpp/Model/Variables/VariableService.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ bool VariableService::addVariable(std::unique_ptr<Variable> variable) {
248248
return getContainerInternalByVariable(variable->getComponentId(), variable->getName()).add(std::move(variable));
249249
}
250250

251+
bool VariableService::load() {
252+
bool success = true;
253+
254+
for (size_t i = 0; i < MO_VARIABLESTORE_BUCKETS; i++) {
255+
if (!containersInternal[i].load()) {
256+
success = false;
257+
}
258+
}
259+
260+
return success;
261+
}
262+
251263
bool VariableService::commit() {
252264
bool success = true;
253265

src/MicroOcpp/Model/Variables/VariableService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class VariableService : public MemoryManaged {
7777
//Get Variable. If not existent, return nullptr
7878
Variable *getVariable(const ComponentId& component, const char *name);
7979

80+
bool load();
8081
bool commit();
8182

8283
void addContainer(VariableContainer *container);

src/MicroOcpp_c.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ std::function<UnlockConnectorResult()> adaptFn(unsigned int connectorId, PollUnl
131131
}
132132
#endif //MO_ENABLE_CONNECTOR_LOCK
133133

134-
void ocpp_beginTransaction(const char *idTag) {
135-
beginTransaction(idTag);
134+
bool ocpp_beginTransaction(const char *idTag) {
135+
return beginTransaction(idTag);
136136
}
137-
void ocpp_beginTransaction_m(unsigned int connectorId, const char *idTag) {
138-
beginTransaction(idTag, connectorId);
137+
bool ocpp_beginTransaction_m(unsigned int connectorId, const char *idTag) {
138+
return beginTransaction(idTag, connectorId);
139139
}
140140

141-
void ocpp_beginTransaction_authorized(const char *idTag, const char *parentIdTag) {
142-
beginTransaction_authorized(idTag, parentIdTag);
141+
bool ocpp_beginTransaction_authorized(const char *idTag, const char *parentIdTag) {
142+
return beginTransaction_authorized(idTag, parentIdTag);
143143
}
144-
void ocpp_beginTransaction_authorized_m(unsigned int connectorId, const char *idTag, const char *parentIdTag) {
145-
beginTransaction_authorized(idTag, parentIdTag, connectorId);
144+
bool ocpp_beginTransaction_authorized_m(unsigned int connectorId, const char *idTag, const char *parentIdTag) {
145+
return beginTransaction_authorized(idTag, parentIdTag, connectorId);
146146
}
147147

148148
bool ocpp_endTransaction(const char *idTag, const char *reason) {

src/MicroOcpp_c.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ void ocpp_loop();
7878
* Charging session management
7979
*/
8080

81-
void ocpp_beginTransaction(const char *idTag);
82-
void ocpp_beginTransaction_m(unsigned int connectorId, const char *idTag); //multiple connectors version
81+
bool ocpp_beginTransaction(const char *idTag);
82+
bool ocpp_beginTransaction_m(unsigned int connectorId, const char *idTag); //multiple connectors version
8383

84-
void ocpp_beginTransaction_authorized(const char *idTag, const char *parentIdTag);
85-
void ocpp_beginTransaction_authorized_m(unsigned int connectorId, const char *idTag, const char *parentIdTag);
84+
bool ocpp_beginTransaction_authorized(const char *idTag, const char *parentIdTag);
85+
bool ocpp_beginTransaction_authorized_m(unsigned int connectorId, const char *idTag, const char *parentIdTag);
8686

8787
bool ocpp_endTransaction(const char *idTag, const char *reason); //idTag, reason can be NULL
8888
bool ocpp_endTransaction_m(unsigned int connectorId, const char *idTag, const char *reason); //idTag, reason can be NULL

0 commit comments

Comments
 (0)