Skip to content

Commit be4f092

Browse files
committed
v201 compat for ocpp_tx C-API
1 parent bcfe1ba commit be4f092

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed

src/MicroOcpp.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,12 @@ std::shared_ptr<Transaction>& getTransaction(unsigned int connectorId) {
704704
MO_DBG_WARN("OCPP uninitialized");
705705
return mocpp_undefinedTx;
706706
}
707+
#if MO_ENABLE_V201
708+
if (context->getVersion().major == 2) {
709+
MO_DBG_ERR("only supported in v16");
710+
return mocpp_undefinedTx;
711+
}
712+
#endif
707713
auto connector = context->getModel().getConnector(connectorId);
708714
if (!connector) {
709715
MO_DBG_ERR("could not find connector");
@@ -720,7 +726,7 @@ Ocpp201::Transaction *getTransactionV201(unsigned int evseId) {
720726
}
721727

722728
if (context->getVersion().major != 2) {
723-
MO_DBG_ERR("only supported in v201"); //need to call mocpp_initialize before
729+
MO_DBG_ERR("only supported in v201");
724730
return nullptr;
725731
}
726732

src/MicroOcpp/Model/Transactions/Transaction.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,61 +355,170 @@ bool deserializeTransactionEventChargingState(const char *chargingStateCstr, Tra
355355

356356
#endif //MO_ENABLE_V201
357357

358+
#if MO_ENABLE_V201
359+
bool g_ocpp_tx_compat_v201;
360+
361+
void ocpp_tx_compat_setV201(bool isV201) {
362+
g_ocpp_tx_compat_v201 = isV201;
363+
}
364+
#endif
365+
358366
int ocpp_tx_getTransactionId(OCPP_Transaction *tx) {
367+
#if MO_ENABLE_V201
368+
if (g_ocpp_tx_compat_v201) {
369+
MO_DBG_ERR("only supported in v16");
370+
return -1;
371+
}
372+
#endif //MO_ENABLE_V201
359373
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getTransactionId();
360374
}
375+
#if MO_ENABLE_V201
376+
const char *ocpp_tx_getTransactionIdV201(OCPP_Transaction *tx) {
377+
if (!g_ocpp_tx_compat_v201) {
378+
MO_DBG_ERR("only supported in v201");
379+
return nullptr;
380+
}
381+
return reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx)->transactionId;
382+
}
383+
#endif //MO_ENABLE_V201
361384
bool ocpp_tx_isAuthorized(OCPP_Transaction *tx) {
385+
#if MO_ENABLE_V201
386+
if (g_ocpp_tx_compat_v201) {
387+
return reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx)->isAuthorized;
388+
}
389+
#endif //MO_ENABLE_V201
362390
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->isAuthorized();
363391
}
364392
bool ocpp_tx_isIdTagDeauthorized(OCPP_Transaction *tx) {
393+
#if MO_ENABLE_V201
394+
if (g_ocpp_tx_compat_v201) {
395+
return reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx)->isDeauthorized;
396+
}
397+
#endif //MO_ENABLE_V201
365398
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->isIdTagDeauthorized();
366399
}
367400

368401
bool ocpp_tx_isRunning(OCPP_Transaction *tx) {
402+
#if MO_ENABLE_V201
403+
if (g_ocpp_tx_compat_v201) {
404+
auto transaction = reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx);
405+
return transaction->started && !transaction->stopped;
406+
}
407+
#endif //MO_ENABLE_V201
369408
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->isRunning();
370409
}
371410
bool ocpp_tx_isActive(OCPP_Transaction *tx) {
411+
#if MO_ENABLE_V201
412+
if (g_ocpp_tx_compat_v201) {
413+
return reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx)->active;
414+
}
415+
#endif //MO_ENABLE_V201
372416
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->isActive();
373417
}
374418
bool ocpp_tx_isAborted(OCPP_Transaction *tx) {
419+
#if MO_ENABLE_V201
420+
if (g_ocpp_tx_compat_v201) {
421+
auto transaction = reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx);
422+
return !transaction->active && !transaction->started;
423+
}
424+
#endif //MO_ENABLE_V201
375425
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->isAborted();
376426
}
377427
bool ocpp_tx_isCompleted(OCPP_Transaction *tx) {
428+
#if MO_ENABLE_V201
429+
if (g_ocpp_tx_compat_v201) {
430+
auto transaction = reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx);
431+
return transaction->stopped && transaction->seqNos.empty();
432+
}
433+
#endif //MO_ENABLE_V201
378434
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->isCompleted();
379435
}
380436

381437
const char *ocpp_tx_getIdTag(OCPP_Transaction *tx) {
438+
#if MO_ENABLE_V201
439+
if (g_ocpp_tx_compat_v201) {
440+
auto transaction = reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx);
441+
return transaction->idToken.get();
442+
}
443+
#endif //MO_ENABLE_V201
382444
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getIdTag();
383445
}
384446

385447
bool ocpp_tx_getBeginTimestamp(OCPP_Transaction *tx, char *buf, size_t len) {
448+
#if MO_ENABLE_V201
449+
if (g_ocpp_tx_compat_v201) {
450+
return reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx)->beginTimestamp.toJsonString(buf, len);
451+
}
452+
#endif //MO_ENABLE_V201
386453
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getBeginTimestamp().toJsonString(buf, len);
387454
}
388455

389456
int32_t ocpp_tx_getMeterStart(OCPP_Transaction *tx) {
457+
#if MO_ENABLE_V201
458+
if (g_ocpp_tx_compat_v201) {
459+
MO_DBG_ERR("only supported in v16");
460+
return -1;
461+
}
462+
#endif //MO_ENABLE_V201
390463
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getMeterStart();
391464
}
392465

393466
bool ocpp_tx_getStartTimestamp(OCPP_Transaction *tx, char *buf, size_t len) {
467+
#if MO_ENABLE_V201
468+
if (g_ocpp_tx_compat_v201) {
469+
MO_DBG_ERR("only supported in v16");
470+
return -1;
471+
}
472+
#endif //MO_ENABLE_V201
394473
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getStartTimestamp().toJsonString(buf, len);
395474
}
396475

397476
const char *ocpp_tx_getStopIdTag(OCPP_Transaction *tx) {
477+
#if MO_ENABLE_V201
478+
if (g_ocpp_tx_compat_v201) {
479+
auto transaction = reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx);
480+
return transaction->stopIdToken ? transaction->stopIdToken->get() : "";
481+
}
482+
#endif //MO_ENABLE_V201
398483
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getStopIdTag();
399484
}
400485

401486
int32_t ocpp_tx_getMeterStop(OCPP_Transaction *tx) {
487+
#if MO_ENABLE_V201
488+
if (g_ocpp_tx_compat_v201) {
489+
MO_DBG_ERR("only supported in v16");
490+
return -1;
491+
}
492+
#endif //MO_ENABLE_V201
402493
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getMeterStop();
403494
}
404495

405496
void ocpp_tx_setMeterStop(OCPP_Transaction* tx, int32_t meter) {
497+
#if MO_ENABLE_V201
498+
if (g_ocpp_tx_compat_v201) {
499+
MO_DBG_ERR("only supported in v16");
500+
return;
501+
}
502+
#endif //MO_ENABLE_V201
406503
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->setMeterStop(meter);
407504
}
408505

409506
bool ocpp_tx_getStopTimestamp(OCPP_Transaction *tx, char *buf, size_t len) {
507+
#if MO_ENABLE_V201
508+
if (g_ocpp_tx_compat_v201) {
509+
MO_DBG_ERR("only supported in v16");
510+
return -1;
511+
}
512+
#endif //MO_ENABLE_V201
410513
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getStopTimestamp().toJsonString(buf, len);
411514
}
412515

413516
const char *ocpp_tx_getStopReason(OCPP_Transaction *tx) {
517+
#if MO_ENABLE_V201
518+
if (g_ocpp_tx_compat_v201) {
519+
auto transaction = reinterpret_cast<MicroOcpp::Ocpp201::Transaction*>(tx);
520+
return serializeTransactionStoppedReason(transaction->stoppedReason);
521+
}
522+
#endif //MO_ENABLE_V201
414523
return reinterpret_cast<MicroOcpp::Transaction*>(tx)->getStopReason();
415524
}

src/MicroOcpp/Model/Transactions/Transaction.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef TRANSACTION_H
66
#define TRANSACTION_H
77

8+
#include <MicroOcpp/Version.h>
9+
810
/* General Tx defs */
911
#ifdef __cplusplus
1012
extern "C" {
@@ -215,8 +217,6 @@ class Transaction : public MemoryManaged {
215217

216218
} // namespace MicroOcpp
217219

218-
#include <MicroOcpp/Version.h>
219-
220220
#if MO_ENABLE_V201
221221

222222
#include <memory>
@@ -451,7 +451,18 @@ extern "C" {
451451
struct OCPP_Transaction;
452452
typedef struct OCPP_Transaction OCPP_Transaction;
453453

454+
/*
455+
* Compat mode for transactions. This means that all following C-wrapper functions will interprete the handle as v201 transactions
456+
*/
457+
#if MO_ENABLE_V201
458+
void ocpp_tx_compat_setV201(bool isV201); //if set, all OCPP_Transaction* handles are treated as v201 transactions
459+
#endif
460+
454461
int ocpp_tx_getTransactionId(OCPP_Transaction *tx);
462+
#if MO_ENABLE_V201
463+
const char *ocpp_tx_getTransactionIdV201(OCPP_Transaction *tx);
464+
#endif
465+
455466
bool ocpp_tx_isAuthorized(OCPP_Transaction *tx);
456467
bool ocpp_tx_isIdTagDeauthorized(OCPP_Transaction *tx);
457468

src/MicroOcpp_c.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "MicroOcpp.h"
77

88
#include <MicroOcpp/Model/Certificates/Certificate_c.h>
9+
#include <MicroOcpp/Core/Context.h>
10+
#include <MicroOcpp/Model/Model.h>
911
#include <MicroOcpp/Core/Memory.h>
1012

1113
#include <MicroOcpp/Platform.h>
@@ -184,10 +186,29 @@ OCPP_Transaction *ocpp_getTransaction() {
184186
return ocpp_getTransaction_m(1);
185187
}
186188
OCPP_Transaction *ocpp_getTransaction_m(unsigned int connectorId) {
189+
#if MO_ENABLE_V201
190+
{
191+
if (!getOcppContext()) {
192+
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
193+
return nullptr;
194+
}
195+
if (getOcppContext()->getModel().getVersion().major == 2) {
196+
ocpp_tx_compat_setV201(true); //set the ocpp_tx C-API into v201 mode globally
197+
if (getTransactionV201(connectorId)) {
198+
return reinterpret_cast<OCPP_Transaction*>(getTransactionV201(connectorId));
199+
} else {
200+
return nullptr;
201+
}
202+
} else {
203+
ocpp_tx_compat_setV201(false); //set the ocpp_tx C-API into v16 mode globally
204+
//continue with V16 implementation
205+
}
206+
}
207+
#endif //MO_ENABLE_V201
187208
if (getTransaction(connectorId)) {
188209
return reinterpret_cast<OCPP_Transaction*>(getTransaction(connectorId).get());
189210
} else {
190-
return NULL;
211+
return nullptr;
191212
}
192213
}
193214

0 commit comments

Comments
 (0)