Skip to content

Commit 14e9f80

Browse files
committed
API cleanup
1 parent 74c96f3 commit 14e9f80

17 files changed

Lines changed: 101 additions & 64 deletions

src/MicroOcpp.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void mo_setFilesystemConfig2(MO_Context *ctx, MO_FilesystemOpt opt, const char *
103103
auto context = mo_getContext2(ctx);
104104

105105
MO_FilesystemConfig filesystemConfig;
106-
memset(&filesystemConfig, 0, sizeof(filesystemConfig));
106+
mo_filesystemConfig_init(&filesystemConfig);
107107
filesystemConfig.opt = opt;
108108
filesystemConfig.path_prefix = pathPrefix;
109109

@@ -171,7 +171,7 @@ void mo_setOcppVersion2(MO_Context *ctx, int ocppVersion) {
171171
//Set BootNotification fields
172172
bool mo_setBootNotificationData(const char *chargePointModel, const char *chargePointVendor) {
173173
MO_BootNotificationData bnData;
174-
mo_BootNotificationData_init(&bnData);
174+
mo_bootNotificationData_init(&bnData);
175175
bnData.chargePointModel = chargePointModel;
176176
bnData.chargePointVendor = chargePointVendor;
177177

@@ -264,7 +264,7 @@ void mo_setConnectorPluggedInput2(MO_Context *ctx, unsigned int evseId, bool (*c
264264
//Input of the electricity meter register in Wh
265265
bool mo_setEnergyMeterInput(int32_t (*energyInput)()) {
266266
MO_MeterInput mInput;
267-
mo_MeterInput_init(&mInput, MO_MeterInputType_Int);
267+
mo_meterInput_init(&mInput, MO_MeterInputType_Int);
268268
mInput.getInt = energyInput;
269269
mInput.measurand = "Energy.Active.Import.Register";
270270
mInput.unit = "Wh";
@@ -273,7 +273,7 @@ bool mo_setEnergyMeterInput(int32_t (*energyInput)()) {
273273

274274
bool mo_setEnergyMeterInput2(MO_Context *ctx, unsigned int evseId, int32_t (*energyInput2)(MO_ReadingContext, unsigned int, void*), void *userData) {
275275
MO_MeterInput mInput;
276-
mo_MeterInput_init(&mInput, MO_MeterInputType_IntWithArgs);
276+
mo_meterInput_init(&mInput, MO_MeterInputType_IntWithArgs);
277277
mInput.getInt2 = energyInput2;
278278
mInput.measurand = "Energy.Active.Import.Register";
279279
mInput.unit = "Wh";
@@ -284,7 +284,7 @@ bool mo_setEnergyMeterInput2(MO_Context *ctx, unsigned int evseId, int32_t (*ene
284284
//Input of the power meter reading in W
285285
bool mo_setPowerMeterInput(float (*powerInput)()) {
286286
MO_MeterInput mInput;
287-
mo_MeterInput_init(&mInput, MO_MeterInputType_Float);
287+
mo_meterInput_init(&mInput, MO_MeterInputType_Float);
288288
mInput.getFloat = powerInput;
289289
mInput.measurand = "Power.Active.Import";
290290
mInput.unit = "W";
@@ -293,7 +293,7 @@ bool mo_setPowerMeterInput(float (*powerInput)()) {
293293

294294
bool mo_setPowerMeterInput2(MO_Context *ctx, unsigned int evseId, float (*powerInput2)(MO_ReadingContext, unsigned int, void*), void *userData) {
295295
MO_MeterInput mInput;
296-
mo_MeterInput_init(&mInput, MO_MeterInputType_Float);
296+
mo_meterInput_init(&mInput, MO_MeterInputType_Float);
297297
mInput.getFloat2 = powerInput2;
298298
mInput.measurand = "Power.Active.Import";
299299
mInput.unit = "W";
@@ -1095,14 +1095,14 @@ bool mo_addErrorCodeInput(const char* (*errorCodeInput)()) {
10951095
}
10961096

10971097
MO_ErrorDataInput errorDataInput;
1098-
mo_ErrorDataInput_init(&errorDataInput);
1098+
mo_errorDataInput_init(&errorDataInput);
10991099
errorDataInput.userData = reinterpret_cast<void*>(errorCodeInput);
11001100
errorDataInput.getErrorData = [] (unsigned int, void *userData) {
11011101
auto errorCodeInput = reinterpret_cast<const char* (*)()>(userData);
11021102

11031103
MO_ErrorData errorData;
1104-
mo_ErrorData_init(&errorData);
1105-
mo_ErrorData_setErrorCode(&errorData, errorCodeInput());
1104+
mo_errorData_init(&errorData);
1105+
mo_errorData_setErrorCode(&errorData, errorCodeInput());
11061106
return errorData;
11071107
};
11081108

@@ -1155,7 +1155,7 @@ bool mo_v16_addErrorDataInput(MO_Context *ctx, unsigned int evseId, MO_ErrorData
11551155
}
11561156

11571157
MO_ErrorDataInput errorDataInput;
1158-
mo_ErrorDataInput_init(&errorDataInput);
1158+
mo_errorDataInput_init(&errorDataInput);
11591159
errorDataInput.getErrorData = errorData;
11601160
errorDataInput.userData = userData;
11611161

@@ -1212,7 +1212,7 @@ bool mo_v201_addFaultedInput(MO_Context *ctx, unsigned int evseId, bool (*faulte
12121212

12131213
bool mo_addMeterValueInputInt(int32_t (*meterInput)(), const char *measurand, const char *unit, const char *location, const char *phase) {
12141214
MO_MeterInput mInput;
1215-
mo_MeterInput_init(&mInput, MO_MeterInputType_Int);
1215+
mo_meterInput_init(&mInput, MO_MeterInputType_Int);
12161216
mInput.getInt = meterInput;
12171217
mInput.measurand = measurand;
12181218
mInput.unit = unit;
@@ -1223,7 +1223,7 @@ bool mo_addMeterValueInputInt(int32_t (*meterInput)(), const char *measurand, co
12231223

12241224
bool mo_addMeterValueInputInt2(MO_Context *ctx, unsigned int evseId, int32_t (*meterInput)(MO_ReadingContext, unsigned int, void*), const char *measurand, const char *unit, const char *location, const char *phase, void *userData) {
12251225
MO_MeterInput mInput;
1226-
mo_MeterInput_init(&mInput, MO_MeterInputType_IntWithArgs);
1226+
mo_meterInput_init(&mInput, MO_MeterInputType_IntWithArgs);
12271227
mInput.getInt2 = meterInput;
12281228
mInput.measurand = measurand;
12291229
mInput.unit = unit;
@@ -1235,7 +1235,7 @@ bool mo_addMeterValueInputInt2(MO_Context *ctx, unsigned int evseId, int32_t (*m
12351235

12361236
bool mo_addMeterValueInputFloat(float (*meterInput)(), const char *measurand, const char *unit, const char *location, const char *phase) {
12371237
MO_MeterInput mInput;
1238-
mo_MeterInput_init(&mInput, MO_MeterInputType_Float);
1238+
mo_meterInput_init(&mInput, MO_MeterInputType_Float);
12391239
mInput.getFloat = meterInput;
12401240
mInput.measurand = measurand;
12411241
mInput.unit = unit;
@@ -1246,7 +1246,7 @@ bool mo_addMeterValueInputFloat(float (*meterInput)(), const char *measurand, co
12461246

12471247
bool mo_addMeterValueInputFloat2(MO_Context *ctx, unsigned int evseId, float (*meterInput)(MO_ReadingContext, unsigned int, void*), const char *measurand, const char *unit, const char *location, const char *phase, void *userData) {
12481248
MO_MeterInput mInput;
1249-
mo_MeterInput_init(&mInput, MO_MeterInputType_FloatWithArgs);
1249+
mo_meterInput_init(&mInput, MO_MeterInputType_FloatWithArgs);
12501250
mInput.getFloat2 = meterInput;
12511251
mInput.measurand = measurand;
12521252
mInput.unit = unit;
@@ -1258,7 +1258,7 @@ bool mo_addMeterValueInputFloat2(MO_Context *ctx, unsigned int evseId, float (*m
12581258

12591259
bool mo_addMeterValueInputString(int (*meterInput)(char *buf, size_t size), const char *measurand, const char *unit, const char *location, const char *phase) {
12601260
MO_MeterInput mInput;
1261-
mo_MeterInput_init(&mInput, MO_MeterInputType_String);
1261+
mo_meterInput_init(&mInput, MO_MeterInputType_String);
12621262
mInput.getString = meterInput;
12631263
mInput.measurand = measurand;
12641264
mInput.unit = unit;
@@ -1268,7 +1268,7 @@ bool mo_addMeterValueInputString(int (*meterInput)(char *buf, size_t size), cons
12681268
}
12691269
bool mo_addMeterValueInputString2(MO_Context *ctx, unsigned int evseId, int (*meterInput)(char *buf, size_t size, MO_ReadingContext, unsigned int, void*), const char *measurand, const char *unit, const char *location, const char *phase, void *userData) {
12701270
MO_MeterInput mInput;
1271-
mo_MeterInput_init(&mInput, MO_MeterInputType_StringWithArgs);
1271+
mo_meterInput_init(&mInput, MO_MeterInputType_StringWithArgs);
12721272
mInput.getString2 = meterInput;
12731273
mInput.measurand = measurand;
12741274
mInput.unit = unit;

src/MicroOcpp.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ void mo_setFilesystemConfig2(MO_Context *ctx, MO_FilesystemOpt opt, const char *
4545
#if MO_WS_USE == MO_WS_ARDUINO
4646
/*
4747
* Setup MO with links2004/WebSockets library. Only available on Arduino, for other platforms set custom
48-
* WebSockets adapter (see examples folder). `backendUrl`, `chargeBoxId`, `authorizationKey` and
49-
* `CA_cert` are zero-copy and must remain valid until `mo_setup()`. `CA_cert` is zero-copy and must
50-
* outlive the MO lifecycle.
48+
* WebSockets adapter (see examples folder). `CA_cert` is zero-copy and must outlive the MO lifecycle.
5149
*
5250
* If the connections fails, please refer to
5351
* https://github.com/matth-x/MicroOcpp/issues/36#issuecomment-989716573 for recommendations on
@@ -57,7 +55,7 @@ bool mo_setWebsocketUrl(
5755
const char *backendUrl, //e.g. "wss://example.com:8443/steve/websocket/CentralSystemService". Must be defined
5856
const char *chargeBoxId, //e.g. "charger001". Can be NULL
5957
const char *authorizationKey, //authorizationKey present in the websocket message header. Can be NULL. Set this to enable OCPP Security Profile 2
60-
const char *CA_cert); //TLS certificate. Can be NULL. Set this to enable OCPP Security Profile 2
58+
const char *CA_cert); //TLS certificate. Can be NULL. Zero-copy, must outlive MO. Set this to enable OCPP Security Profile 2
6159
#endif
6260

6361
#if __cplusplus
@@ -101,7 +99,7 @@ bool mo_setBootNotificationData2(MO_Context *ctx, MO_BootNotificationData bnData
10199
* the energy meter stores the energy register in the global variable `e_reg`, then you can allow
102100
* this library to read it by defining the following Input and passing it to the library.
103101
* ```
104-
* setEnergyMeterInput([] () {
102+
* mo_setEnergyMeterInput([] () {
105103
* return e_reg;
106104
* });
107105
* ```
@@ -110,7 +108,7 @@ bool mo_setBootNotificationData2(MO_Context *ctx, MO_BootNotificationData bnData
110108
* For example, to let Smart Charging control the PWM signal of the Control Pilot, define the
111109
* following Output and pass it to the library.
112110
* ```
113-
* setSmartChargingPowerOutput([] (float p_max) {
111+
* mo_setSmartChargingPowerOutput([] (float p_max) {
114112
* pwm = p_max / PWM_FACTOR; //(simplified example)
115113
* });
116114
* ```

src/MicroOcpp/Context.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ Context::~Context() {
3737
setFtpClient(nullptr);
3838
setCertificateStore(nullptr);
3939

40+
#if MO_USE_FILEAPI != MO_CUSTOM_FS
41+
mo_filesystemConfig_deinit(&filesystemConfig);
42+
filesystemConfigDefined = false;
43+
#endif //MO_USE_FILEAPI != MO_CUSTOM_FS
44+
4045
#if MO_WS_USE != MO_WS_CUSTOM
4146
mo_connectionConfig_deinit(&connectionConfig);
4247
#endif //MO_WS_USE != MO_WS_CUSTOM
@@ -77,9 +82,13 @@ uint32_t (*Context::getRngCb())() {
7782
}
7883

7984
#if MO_USE_FILEAPI != MO_CUSTOM_FS
80-
void Context::setFilesystemConfig(MO_FilesystemConfig filesystemConfig) {
81-
this->filesystemConfig = filesystemConfig;
85+
bool Context::setFilesystemConfig(MO_FilesystemConfig filesystemConfig) {
86+
if (!mo_filesystemConfig_copy(&this->filesystemConfig, &filesystemConfig)) {
87+
MO_DBG_ERR("OOM");
88+
return false;
89+
}
8290
filesystemConfigDefined = true;
91+
return true;
8392
}
8493
#endif //MO_USE_FILEAPI != MO_CUSTOM_FS
8594

@@ -104,6 +113,8 @@ MO_FilesystemAdapter *Context::getFilesystem() {
104113
return nullptr;
105114
}
106115
isFilesystemOwner = true;
116+
mo_filesystemConfig_deinit(&filesystemConfig);
117+
filesystemConfigDefined = false;
107118
}
108119
#endif //MO_USE_FILEAPI != MO_CUSTOM_FS
109120

@@ -303,6 +314,7 @@ bool Context::setup() {
303314
#if MO_USE_FILEAPI != MO_CUSTOM_FS
304315
if (!filesystemConfigDefined) {
305316
//set defaults
317+
mo_filesystemConfig_init(&filesystemConfig);
306318
filesystemConfig.opt = MO_FS_OPT_USE_MOUNT;
307319
filesystemConfig.path_prefix = MO_FILENAME_PREFIX;
308320
filesystemConfigDefined = true;

src/MicroOcpp/Context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Context : public MemoryManaged {
8585
uint32_t (*getRngCb())();
8686

8787
#if MO_USE_FILEAPI != MO_CUSTOM_FS
88-
void setFilesystemConfig(MO_FilesystemConfig filesystemConfig);
88+
bool setFilesystemConfig(MO_FilesystemConfig filesystemConfig);
8989
#endif //MO_USE_FILEAPI != MO_CUSTOM_FS
9090
void setFilesystem(MO_FilesystemAdapter *filesystem);
9191
MO_FilesystemAdapter *getFilesystem();

src/MicroOcpp/Core/FilesystemAdapter.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,41 @@ MO_FilesystemAdapter *resetIndex(MO_FilesystemAdapter *decorator) {
398398

399399
#endif //MO_ENABLE_FILE_INDEX
400400

401+
#if MO_USE_FILEAPI != MO_CUSTOM_FS
402+
403+
void mo_filesystemConfig_init(MO_FilesystemConfig *config) {
404+
memset(config, 0, sizeof(*config));
405+
}
406+
407+
bool mo_filesystemConfig_copy(MO_FilesystemConfig *dst, MO_FilesystemConfig *src) {
408+
409+
char *prefix_copy = nullptr;
410+
411+
const char *prefix = src->path_prefix;
412+
size_t prefix_len = prefix ? strlen(prefix) : 0;
413+
if (prefix_len > 0) {
414+
size_t prefix_size = prefix_len + 1;
415+
prefix_copy = static_cast<char*>(MO_MALLOC("Filesystem", prefix_size));
416+
if (!prefix_copy) {
417+
MO_DBG_ERR("OOM");
418+
return false;
419+
}
420+
(void)snprintf(prefix_copy, prefix_size, "%s", prefix);
421+
}
422+
423+
dst->internalBuf = prefix_copy;
424+
dst->path_prefix = dst->internalBuf;
425+
dst->opt = src->opt;
426+
return true;
427+
}
428+
429+
void mo_filesystemConfig_deinit(MO_FilesystemConfig *config) {
430+
MO_FREE(config->internalBuf);
431+
memset(config, 0, sizeof(*config));
432+
}
433+
434+
#endif //MO_USE_FILEAPI != MO_CUSTOM_FS
435+
401436
#if MO_USE_FILEAPI == MO_ARDUINO_LITTLEFS || MO_USE_FILEAPI == MO_ARDUINO_SPIFFS
402437

403438
#if MO_USE_FILEAPI == MO_ARDUINO_LITTLEFS

src/MicroOcpp/Core/FilesystemAdapter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,14 @@ typedef struct {
152152
* a filename to the end of path_prefix. Can be empty if the filesystem implementation accepts the MO filenames as
153153
* path directly. MO doesn't use sub-directories. */
154154
const char *path_prefix;
155+
156+
char *internalBuf; //used by MO internally
155157
} MO_FilesystemConfig;
158+
159+
void mo_filesystemConfig_init(MO_FilesystemConfig *config);
160+
bool mo_filesystemConfig_copy(MO_FilesystemConfig *dst, MO_FilesystemConfig *src);
161+
void mo_filesystemConfig_deinit(MO_FilesystemConfig *config);
162+
156163
MO_FilesystemAdapter *mo_makeDefaultFilesystemAdapter(MO_FilesystemConfig config);
157164
void mo_freeDefaultFilesystemAdapter(MO_FilesystemAdapter *filesystem);
158165

src/MicroOcpp/Model/Authorization/AuthorizationService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ void AuthorizationService::notifyAuthorization(const char *idTag, JsonObject idT
201201
auto cpStatus = availSvcCp ? availSvcCp->getStatus() : MO_ChargePointStatus_UNDEFINED;
202202

203203
MO_ErrorData errorCode;
204-
mo_ErrorData_init(&errorCode);
205-
mo_ErrorData_setErrorCode(&errorCode, "LocalListConflict");
204+
mo_errorData_init(&errorCode);
205+
mo_errorData_setErrorCode(&errorCode, "LocalListConflict");
206206

207207
auto statusNotification = makeRequest(context, new StatusNotification(
208208
context,

src/MicroOcpp/Model/Availability/AvailabilityDefs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88

99
#if MO_ENABLE_V16
1010

11-
void mo_ErrorData_init(MO_ErrorData *errorData) {
11+
void mo_errorData_init(MO_ErrorData *errorData) {
1212
memset(errorData, 0, sizeof(*errorData));
1313
errorData->severity = 1;
1414
}
1515

16-
void mo_ErrorData_setErrorCode(MO_ErrorData *errorData, const char *errorCode) {
16+
void mo_errorData_setErrorCode(MO_ErrorData *errorData, const char *errorCode) {
1717
errorData->errorCode = errorCode;
1818
if (errorCode) {
1919
errorData->isError = true;
2020
errorData->isFaulted = true;
2121
}
2222
}
2323

24-
void mo_ErrorDataInput_init(MO_ErrorDataInput *errorDataInput) {
24+
void mo_errorDataInput_init(MO_ErrorDataInput *errorDataInput) {
2525
memset(errorDataInput, 0, sizeof(*errorDataInput));
2626
}
2727

src/MicroOcpp/Model/Availability/AvailabilityDefs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ typedef struct {
2525
const char *vendorErrorCode; //vendor-specific error code
2626
} MO_ErrorData;
2727

28-
void mo_ErrorData_init(MO_ErrorData *errorData);
29-
void mo_ErrorData_setErrorCode(MO_ErrorData *errorData, const char *errorCode);
28+
void mo_errorData_init(MO_ErrorData *errorData);
29+
void mo_errorData_setErrorCode(MO_ErrorData *errorData, const char *errorCode);
3030

3131
typedef struct {
3232
MO_ErrorData (*getErrorData)(unsigned int evseId, void *userData);
3333
void *userData;
3434
} MO_ErrorDataInput;
3535

36-
void mo_ErrorDataInput_init(MO_ErrorDataInput *errorDataInput);
36+
void mo_errorDataInput_init(MO_ErrorDataInput *errorDataInput);
3737

3838
#ifdef __cplusplus
3939
} //extern "C"

src/MicroOcpp/Model/Availability/AvailabilityService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void v16::AvailabilityServiceEvse::loop() {
7474
}
7575

7676
MO_ErrorData errorData;
77-
mo_ErrorData_init(&errorData);
77+
mo_errorData_init(&errorData);
7878
errorData.severity = 0;
7979
int errorDataIndex = -1;
8080

@@ -326,7 +326,7 @@ bool v16::AvailabilityServiceEvse::isOperative() {
326326
Operation *v16::AvailabilityServiceEvse::createTriggeredStatusNotification() {
327327

328328
MO_ErrorData errorData;
329-
mo_ErrorData_init(&errorData);
329+
mo_errorData_init(&errorData);
330330
errorData.severity = 0;
331331

332332
if (reportedErrorIndex >= 0) {

0 commit comments

Comments
 (0)