Skip to content

Commit 7b68bfd

Browse files
Matthias Akstallermatth-x
authored andcommitted
bug fixes after testing
1 parent 11aae0d commit 7b68bfd

8 files changed

Lines changed: 74 additions & 22 deletions

File tree

src/MicroOcpp.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ void mo_setFilesystemConfig2(MO_Context *ctx, MO_FilesystemOpt opt, const char *
111111
}
112112
#endif // MO_USE_FILEAPI != MO_CUSTOM_FS
113113

114+
void mo_setFilesystem(MO_FilesystemAdapter *filesystem) {
115+
return mo_setFilesystem2(mo_getApiContext(), filesystem);
116+
}
117+
118+
void mo_setFilesystem2(MO_Context *ctx, MO_FilesystemAdapter *filesystem) {
119+
if (!ctx) {
120+
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
121+
return;
122+
}
123+
auto context = mo_getContext2(ctx);
124+
125+
context->setFilesystem(filesystem);
126+
}
127+
114128
#if MO_WS_USE == MO_WS_ARDUINO
115129
//Setup MO with links2004/WebSockets library
116130
bool mo_setWebsocketUrl(const char *backendUrl, const char *chargeBoxId, const char *authorizationKey, const char *CA_cert) {

src/MicroOcpp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void mo_setFilesystemConfig(MO_FilesystemOpt opt);
4747
void mo_setFilesystemConfig2(MO_Context *ctx, MO_FilesystemOpt opt, const char *pathPrefix);
4848
#endif //#if MO_USE_FILEAPI != MO_CUSTOM_FS
4949

50+
void mo_setFilesystem(MO_FilesystemAdapter *filesystem);
51+
void mo_setFilesystem2(MO_Context *ctx, MO_FilesystemAdapter *filesystem);
52+
5053
#if MO_WS_USE == MO_WS_ARDUINO
5154
/*
5255
* Setup MO with links2004/WebSockets library. Only available on Arduino, for other platforms set custom
@@ -98,9 +101,13 @@ void mo_setConnection(MO_Connection *connection);
98101
void mo_setConnection2(MO_Context *ctx, MO_Connection *connection);
99102

100103
#ifdef __cplusplus
104+
} // extern "C"
105+
101106
/* Same as `mo_setConnection()`, but accepts the C++ `Connection` class from MO v1. Does not
102107
* transfer ownership of `cppConnection`. */
103108
bool mo_setCppConnection(MicroOcpp::Connection *cppConnection);
109+
110+
extern "C" {
104111
#endif
105112

106113
#if MO_ENABLE_MOCK_SERVER
@@ -649,6 +656,8 @@ bool mo_getVarConfigString(MO_Context *ctx, const char *component201, const char
649656
*/
650657

651658
#ifdef __cplusplus
659+
} // extern "C"
660+
652661
namespace MicroOcpp {
653662
class Context;
654663
}
@@ -658,6 +667,8 @@ class Context;
658667
//To use, add `#include <MicroOcpp/Context.h>`
659668
MicroOcpp::Context *mo_getContext();
660669
MicroOcpp::Context *mo_getContext2(MO_Context *ctx);
670+
671+
extern "C" {
661672
#endif //__cplusplus
662673

663674
/*

src/MicroOcpp/Core/Connection.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ MO_Connection *mo_loopback_make() {
167167
}
168168

169169
void mo_loopback_free(MO_Connection *connection) {
170-
auto data = reinterpret_cast<MicroOcpp::LoopbackConnection::LoopbackData*>(connection->userData);
171-
delete data;
172-
MO_FREE(connection);
170+
if (connection) {
171+
auto data = reinterpret_cast<MicroOcpp::LoopbackConnection::LoopbackData*>(connection->userData);
172+
delete data;
173+
MO_FREE(connection);
174+
}
173175
}
174176

175177
void mo_loopback_setConnected(MO_Connection *connection, bool connected) {

src/MicroOcpp/Model/Diagnostics/DiagnosticsService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,6 @@ void DiagnosticsService::setFtpServerCert(const char *cert) {
10061006

10071007
} //namespace MicroOcpp
10081008

1009-
#endif //(MO_ENABLE_V16 || MO_ENABLE_V201) && MO_ENABLE_DIAGNOSTICS
1010-
10111009
#if MO_USE_DIAGNOSTICS == MO_DIAGNOSTICS_BUILTIN_MBEDTLS_ESP32
10121010

10131011
#include "esp_heap_caps.h"
@@ -1049,3 +1047,5 @@ void defaultDiagnosticsOnClose(void*) {
10491047
} //namespace MicroOcpp
10501048

10511049
#endif //MO_USE_DIAGNOSTICS
1050+
1051+
#endif //(MO_ENABLE_V16 || MO_ENABLE_V201) && MO_ENABLE_DIAGNOSTICS

src/MicroOcpp/Model/Metering/MeterValue.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Copyright Matthias Akstaller 2019 - 2024
33
// MIT License
44

5+
#include <inttypes.h>
6+
57
#include <MicroOcpp/Model/Metering/MeterValue.h>
68
#include <MicroOcpp/Model/Metering/MeteringService.h>
79
#include <MicroOcpp/Debug.h>
@@ -347,16 +349,25 @@ int MeterValue::getJsonCapacity(int ocppVersion, bool internalFormat) {
347349
int valueLen = -1;
348350
switch (sv->getType()) {
349351
case SampledValue::Type::Int:
350-
valueLen = snprintf(nullptr, 0, "%i", sv->valueInt);
352+
if (internalFormat || ocppVersion == MO_OCPP_V201) {
353+
valueLen = 0;
354+
}
355+
valueLen = snprintf(nullptr, 0, PRId32, sv->valueInt);
351356
break;
352357
case SampledValue::Type::Float:
358+
if (internalFormat || ocppVersion == MO_OCPP_V201) {
359+
valueLen = 0;
360+
}
353361
valueLen = snprintf(nullptr, 0, MO_SAMPLEDVALUE_FLOAT_FORMAT, sv->valueFloat);
354362
break;
355363
case SampledValue::Type::String:
356364
valueLen = (int)strlen(sv->valueString ? sv->valueString : "");
357365
break;
358366
#if MO_ENABLE_V201
359367
case SampledValue::Type::SignedValue:
368+
if (internalFormat || ocppVersion == MO_OCPP_V201) {
369+
valueLen = 0;
370+
}
360371
valueLen = snprintf(nullptr, 0, MO_SAMPLEDVALUE_FLOAT_FORMAT, sv->valueSigned->value);
361372
break;
362373
#endif //MO_ENABLE_V201
@@ -471,10 +482,10 @@ bool MeterValue::toJson(Clock& clock, int ocppVersion, bool internalFormat, Json
471482

472483
switch (sv->getType()) {
473484
case SampledValue::Type::Int:
474-
if (internalFormat) {
485+
if (internalFormat || ocppVersion == MO_OCPP_V201) {
475486
svJson["value"] = sv->valueInt;
476487
} else {
477-
int ret = snprintf(valueBuf, sizeof(valueBuf), "%i", sv->valueInt);
488+
int ret = snprintf(valueBuf, sizeof(valueBuf), PRId32, sv->valueInt);
478489
if (ret < 0 || (size_t)ret >= sizeof(valueBuf)) {
479490
MO_DBG_ERR("serialization error");
480491
return false;
@@ -483,7 +494,7 @@ bool MeterValue::toJson(Clock& clock, int ocppVersion, bool internalFormat, Json
483494
}
484495
break;
485496
case SampledValue::Type::Float:
486-
if (internalFormat) {
497+
if (internalFormat || ocppVersion == MO_OCPP_V201) {
487498
svJson["value"] = sv->valueFloat;
488499
} else {
489500
int ret = snprintf(valueBuf, sizeof(valueBuf), MO_SAMPLEDVALUE_FLOAT_FORMAT, sv->valueFloat);
@@ -500,11 +511,11 @@ bool MeterValue::toJson(Clock& clock, int ocppVersion, bool internalFormat, Json
500511
#if MO_ENABLE_V201
501512
case SampledValue::Type::SignedValue: {
502513
JsonObject signedMeterValue;
503-
if (internalFormat) {
514+
if (internalFormat || ocppVersion == MO_OCPP_V201) {
504515
svJson["value"] = sv->valueSigned->value;
505516
signedMeterValue = svJson; //write signature data into same sv JSON object
506517
} else {
507-
int ret = snprintf(valueBuf, sizeof(valueBuf), MO_SAMPLEDVALUE_FLOAT_FORMAT, sv->valueFloat);
518+
int ret = snprintf(valueBuf, sizeof(valueBuf), MO_SAMPLEDVALUE_FLOAT_FORMAT, sv->valueSigned->value);
508519
if (ret < 0 || (size_t)ret >= sizeof(valueBuf)) {
509520
MO_DBG_ERR("serialization error");
510521
return false;

src/MicroOcpp/Model/Model.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,15 @@ void v16::Model::updateSupportedStandardProfiles() {
277277
}
278278
}
279279

280+
#if MO_ENABLE_FIRMWAREMANAGEMENT && MO_ENABLE_DIAGNOSTICS
280281
if (firmwareService ||
281282
getDiagnosticsService()) {
282283
if (!strstr(supportedFeatureProfilesString->getString(), "FirmwareManagement")) {
283284
if (!buf.empty()) buf += ',';
284285
buf += "FirmwareManagement";
285286
}
286287
}
288+
#endif //MO_ENABLE_FIRMWAREMANAGEMENT && MO_ENABLE_DIAGNOSTICS
287289

288290
#if MO_ENABLE_LOCAL_AUTH
289291
if (authorizationService) {

src/MicroOcpp/Model/SmartCharging/SmartChargingModel.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,22 @@ bool ChargingProfile::parseJson(Clock& clock, int ocppVersion, JsonObject json)
590590
}
591591
}
592592

593-
auto success = chargingSchedule.parseJson(clock, ocppVersion, json["chargingSchedule"]);
594-
if (!success) {
595-
return false;
593+
#if MO_ENABLE_V16
594+
if (ocppVersion == MO_OCPP_V16) {
595+
auto success = chargingSchedule.parseJson(clock, ocppVersion, json["chargingSchedule"]);
596+
if (!success) {
597+
return false;
598+
}
599+
}
600+
#endif //MO_ENABLE_V16
601+
#if MO_ENABLE_V201
602+
if (ocppVersion == MO_OCPP_V201) {
603+
auto success = chargingSchedule.parseJson(clock, ocppVersion, json["chargingSchedule"][0]);
604+
if (!success) {
605+
return false;
606+
}
596607
}
608+
#endif //MO_ENABLE_V201
597609

598610
//duplicate some fields to chargingSchedule to simplify the max charge rate calculation
599611
chargingSchedule.chargingProfileKind = chargingProfileKind;

src/MicroOcpp/Model/Transactions/TransactionStore.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ bool v16::TransactionStore::serializeSendStatus(Clock& clock, SendStatus& status
121121
}
122122

123123
if (status.getAttemptTime().isDefined()) {
124-
char attemptTime [MO_JSONDATE_SIZE];
124+
char attemptTime [MO_INTERNALTIME_SIZE];
125125
if (!clock.toInternalString(status.getAttemptTime(), attemptTime, sizeof(attemptTime))) {
126126
return false;
127127
}
@@ -174,7 +174,7 @@ bool v16::TransactionStore::serializeTransaction(Clock& clock, Transaction& tx,
174174
sessionState["deauthorized"] = true;
175175
}
176176
if (tx.getBeginTimestamp().isDefined()) {
177-
char timeStr [MO_JSONDATE_SIZE] = {'\0'};
177+
char timeStr [MO_INTERNALTIME_SIZE] = {'\0'};
178178
if (!clock.toInternalString(tx.getBeginTimestamp(), timeStr, sizeof(timeStr))) {
179179
return false;
180180
}
@@ -198,7 +198,7 @@ bool v16::TransactionStore::serializeTransaction(Clock& clock, Transaction& tx,
198198
}
199199

200200
if (tx.getStartTimestamp().isDefined()) {
201-
char startTimeStr [MO_JSONDATE_SIZE] = {'\0'};
201+
char startTimeStr [MO_INTERNALTIME_SIZE] = {'\0'};
202202
if (!clock.toInternalString(tx.getStartTimestamp(), startTimeStr, sizeof(startTimeStr))) {
203203
return false;
204204
}
@@ -224,7 +224,7 @@ bool v16::TransactionStore::serializeTransaction(Clock& clock, Transaction& tx,
224224
}
225225

226226
if (tx.getStopTimestamp().isDefined()) {
227-
char stopTimeStr [MO_JSONDATE_SIZE] = {'\0'};
227+
char stopTimeStr [MO_INTERNALTIME_SIZE] = {'\0'};
228228
if (!clock.toInternalString(tx.getStopTimestamp(), stopTimeStr, sizeof(stopTimeStr))) {
229229
return false;
230230
}
@@ -419,7 +419,7 @@ bool v201::TransactionStoreEvse::serializeTransaction(Transaction& tx, JsonObjec
419419
}
420420

421421
if (tx.beginTimestamp.isDefined()) {
422-
char timeStr [MO_JSONDATE_SIZE] = {'\0'};
422+
char timeStr [MO_INTERNALTIME_SIZE] = {'\0'};
423423
if (!context.getClock().toInternalString(tx.beginTimestamp, timeStr, sizeof(timeStr))) {
424424
MO_DBG_ERR("serialization error");
425425
return false;
@@ -428,7 +428,7 @@ bool v201::TransactionStoreEvse::serializeTransaction(Transaction& tx, JsonObjec
428428
}
429429

430430
if (tx.startTimestamp.isDefined()) {
431-
char timeStr [MO_JSONDATE_SIZE] = {'\0'};
431+
char timeStr [MO_INTERNALTIME_SIZE] = {'\0'};
432432
if (!context.getClock().toInternalString(tx.startTimestamp, timeStr, sizeof(timeStr))) {
433433
MO_DBG_ERR("serialization error");
434434
return false;
@@ -623,7 +623,7 @@ bool v201::TransactionStoreEvse::serializeTransactionEvent(TransactionEventData&
623623
}
624624

625625
if (txEvent.timestamp.isDefined()) {
626-
char timeStr [MO_JSONDATE_SIZE] = {'\0'};
626+
char timeStr [MO_INTERNALTIME_SIZE] = {'\0'};
627627
if (!context.getClock().toInternalString(txEvent.timestamp, timeStr, sizeof(timeStr))) {
628628
return false;
629629
}
@@ -678,7 +678,7 @@ bool v201::TransactionStoreEvse::serializeTransactionEvent(TransactionEventData&
678678
txEventJson["attemptNr"] = txEvent.attemptNr;
679679

680680
if (txEvent.attemptTime.isDefined()) {
681-
char timeStr [MO_JSONDATE_SIZE] = {'\0'};
681+
char timeStr [MO_INTERNALTIME_SIZE] = {'\0'};
682682
if (!context.getClock().toInternalString(txEvent.attemptTime, timeStr, sizeof(timeStr))) {
683683
return false;
684684
}

0 commit comments

Comments
 (0)