Skip to content

Commit c9c904c

Browse files
committed
[SD] Fully refactor ParseConfigFile
1 parent 4794bcd commit c9c904c

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ bool ws_sdcard::ParseAnalogIOAdd(
231231
bool ws_sdcard::ParseDS18X20Add(
232232
wippersnapper_ds18x20_Ds18x20Add &msg_DS18X20Add, const char *pin,
233233
int resolution, float period, int num_sensors, const char *sensor_type_1,
234-
char *sensor_type_2) {
234+
const char *sensor_type_2) {
235235
strcpy(msg_DS18X20Add.onewire_pin, pin);
236236
msg_DS18X20Add.sensor_resolution = resolution;
237237
msg_DS18X20Add.period = period;
@@ -249,6 +249,35 @@ bool ws_sdcard::ParseDS18X20Add(
249249
return true;
250250
}
251251

252+
bool ws_sdcard::PushSignalToSharedBuffer(
253+
wippersnapper_signal_BrokerToDevice &msg_signal) {
254+
// Create a temporary buffer to hold the encoded signal message
255+
std::vector<uint8_t> tempBuf(128);
256+
size_t tempBufSz;
257+
258+
// Get the encoded size of the signal message first so we can resize the
259+
// buffer prior to encoding
260+
WS_DEBUG_PRINTLN("Encoding D2b signal message...");
261+
if (!pb_get_encoded_size(&tempBufSz,
262+
wippersnapper_signal_BrokerToDevice_fields,
263+
&msg_signal)) {
264+
WS_DEBUG_PRINTLN("[SD] ERROR: Unable to get signal message size!");
265+
return false;
266+
}
267+
268+
// Encode and push the signal message to the shared config buffer
269+
tempBuf.resize(tempBufSz);
270+
pb_ostream_t ostream = pb_ostream_from_buffer(tempBuf.data(), tempBuf.size());
271+
if (!ws_pb_encode(&ostream, wippersnapper_signal_BrokerToDevice_fields,
272+
&msg_signal)) {
273+
WS_DEBUG_PRINTLN("[SD] ERROR: Unable to encode D2B signal message!");
274+
return false;
275+
}
276+
WsV2._sharedConfigBuffers.push_back(std::move(tempBuf));
277+
WS_DEBUG_PRINTLN("Encoded the D2b signal message");
278+
return true;
279+
}
280+
252281
/**************************************************************************/
253282
/*!
254283
@brief Searches for and parses the JSON configuration file and sets up
@@ -340,19 +369,20 @@ bool ws_sdcard::parseConfigFile() {
340369
// Parse the component API type
341370
const char *component_api_type = component["componentAPI"];
342371
if (component_api_type == nullptr) {
343-
WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - No component API type found "
344-
"in JSON string!");
372+
WS_DEBUG_PRINTLN(
373+
"[SD] FATAL Parsing error - Missing component API type!");
345374
return false;
346375
}
347376

348377
// Determine the component type and parse it into a PB message
349378
if (strcmp(component_api_type, "digitalio") == 0) {
350379
WS_DEBUG_PRINTLN(
351380
"[SD] DigitalIO component found, decoding JSON to PB...");
352-
353-
// Parse: JSON->DigitalIOAdd
354381
wippersnapper_digitalio_DigitalIOAdd msg_DigitalIOAdd =
355382
wippersnapper_digitalio_DigitalIOAdd_init_default;
383+
msg_signal_b2d.which_payload =
384+
wippersnapper_signal_BrokerToDevice_digitalio_add_tag;
385+
msg_signal_b2d.payload.digitalio_add = msg_DigitalIOAdd;
356386
if (!ParseDigitalIOAdd(msg_DigitalIOAdd, component["pinName"],
357387
component["period"], component["value"],
358388
component["sampleMode"], component["direction"],
@@ -361,33 +391,27 @@ bool ws_sdcard::parseConfigFile() {
361391
"[SD] FATAL Parsing error - Unable to parse DigitalIO component!");
362392
return false;
363393
}
364-
365-
// Configure the signal message for the digitalio payload
366-
msg_signal_b2d.which_payload =
367-
wippersnapper_signal_BrokerToDevice_digitalio_add_tag;
368-
msg_signal_b2d.payload.digitalio_add = msg_DigitalIOAdd;
369394
} else if (strcmp(component_api_type, "analogio") == 0) {
370395
WS_DEBUG_PRINTLN("[SD] AnalogIO component found, decoding JSON to PB...");
371396
wippersnapper_analogio_AnalogIOAdd msg_AnalogIOAdd =
372397
wippersnapper_analogio_AnalogIOAdd_init_default;
373-
398+
msg_signal_b2d.which_payload =
399+
wippersnapper_signal_BrokerToDevice_analogio_add_tag;
400+
msg_signal_b2d.payload.analogio_add = msg_AnalogIOAdd;
374401
// Parse: JSON->AnalogIOAdd
375402
if (!ParseAnalogIOAdd(msg_AnalogIOAdd, component["pinName"],
376403
component["period"], component["analogReadMode"])) {
377404
WS_DEBUG_PRINTLN(
378405
"[SD] FATAL Parsing error - Unable to parse AnalogIO component!");
379406
return false;
380407
}
381-
382-
msg_signal_b2d.which_payload =
383-
wippersnapper_signal_BrokerToDevice_analogio_add_tag;
384-
msg_signal_b2d.payload.analogio_add = msg_AnalogIOAdd;
385408
} else if (strcmp(component_api_type, "ds18x20") == 0) {
386-
WS_DEBUG_PRINTLN("[SD] ds18x20 component found, decoding JSON to PB...");
387-
// Create new DS18X20Add message
409+
WS_DEBUG_PRINTLN("[SD] Ds18x20 component found, decoding JSON to PB...");
388410
wippersnapper_ds18x20_Ds18x20Add msg_DS18X20Add =
389411
wippersnapper_ds18x20_Ds18x20Add_init_default;
390-
412+
msg_signal_b2d.which_payload =
413+
wippersnapper_signal_BrokerToDevice_ds18x20_add_tag;
414+
msg_signal_b2d.payload.ds18x20_add = msg_DS18X20Add;
391415
// Parse: JSON->DS18X20Add
392416
if (!ParseDS18X20Add(msg_DS18X20Add, component["pinName"],
393417
component["sensorResolution"], component["period"],
@@ -398,47 +422,23 @@ bool ws_sdcard::parseConfigFile() {
398422
"[SD] FATAL Parsing error - Unable to parse DS18X20 component!");
399423
return false;
400424
}
401-
402-
// Configure the signal message for the ds18x20 payload
403-
msg_signal_b2d.which_payload =
404-
wippersnapper_signal_BrokerToDevice_ds18x20_add_tag;
405-
msg_signal_b2d.payload.ds18x20_add = msg_DS18X20Add;
406425
} else {
407426
// Unknown component API type
408427
WS_DEBUG_PRINTLN("[SD] Unknown component API type found: " +
409428
String(component_api_type));
410429
return false;
411430
}
412431

413-
// Create a temporary buffer to hold the encoded signal message
414-
std::vector<uint8_t> tempBuf(128);
415-
size_t tempBufSz;
416-
417-
// Get the encoded size of the signal message first so we can resize the
418-
// buffer prior to encoding
419-
WS_DEBUG_PRINTLN("Encoding D2b signal message...");
420-
if (!pb_get_encoded_size(&tempBufSz,
421-
wippersnapper_signal_BrokerToDevice_fields,
422-
&msg_signal_b2d)) {
423-
WS_DEBUG_PRINTLN("[SD] ERROR: Unable to get signal message size!");
432+
if (!PushSignalToSharedBuffer(msg_signal_b2d)) {
433+
WS_DEBUG_PRINTLN("[SD] FATAL Error - Unable to push signal message to "
434+
"shared buffer!");
424435
return false;
425436
}
426-
WS_DEBUG_PRINTLN("Signal message size: " + String(tempBufSz));
427-
// Encode and push the signal message to the shared config buffer
428-
tempBuf.resize(tempBufSz);
429-
pb_ostream_t ostream =
430-
pb_ostream_from_buffer(tempBuf.data(), tempBuf.size());
431-
if (!ws_pb_encode(&ostream, wippersnapper_signal_BrokerToDevice_fields,
432-
&msg_signal_b2d)) {
433-
WS_DEBUG_PRINTLN("[SD] ERROR: Unable to encode D2B signal message!");
434-
return false;
435-
}
436-
WsV2._sharedConfigBuffers.push_back(std::move(tempBuf));
437-
WS_DEBUG_PRINTLN("Encoded the D2b signal message");
438437
}
439438
return true;
440439
}
441440

441+
#ifdef OFFLINE_MODE_DEBUG
442442
/**************************************************************************/
443443
/*!
444444
@brief Validates a JSON string.
@@ -593,6 +593,7 @@ bool ws_sdcard::waitForSerialConfig() {
593593
WS_DEBUG_PRINTLN("[SD] Valid JSON string received!");
594594
return true;
595595
}
596+
#endif
596597

597598
/**************************************************************************/
598599
/*!

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class ws_sdcard {
3535
~ws_sdcard();
3636
bool InitSDCard();
3737
bool ConfigureRTC(const char *rtc_type);
38-
bool waitForSerialConfig();
39-
bool validateJson(const char *input);
40-
bool mode_offline; // TODO: Refactor to getter/setter
4138
uint32_t GetTimestamp();
39+
bool mode_offline; // TODO: Refactor to getter/setter
40+
bool
41+
PushSignalToSharedBuffer(wippersnapper_signal_BrokerToDevice &msg_signal);
4242

4343
bool parseConfigFile();
4444
wippersnapper_sensor_SensorType ParseSensorType(const char *sensor_type);
@@ -50,8 +50,8 @@ class ws_sdcard {
5050
const char *pin, float period, const char *mode);
5151
bool ParseDS18X20Add(wippersnapper_ds18x20_Ds18x20Add &msg_DS18X20Add,
5252
const char *pin, int resolution, float period,
53-
int num_sensors, char *sensor_type_1,
54-
char *sensor_type_2);
53+
int num_sensors, const char *sensor_type_1,
54+
const char *sensor_type_2);
5555

5656
bool LogGPIOSensorEventToSD(uint8_t pin, float value,
5757
wippersnapper_sensor_SensorType read_type);
@@ -61,6 +61,10 @@ class ws_sdcard {
6161
wippersnapper_sensor_SensorType read_type);
6262
bool LogDS18xSensorEventToSD(wippersnapper_ds18x20_Ds18x20Event *event_msg);
6363

64+
#ifdef OFFLINE_MODE_DEBUG
65+
bool waitForSerialConfig();
66+
bool validateJson(const char *input);
67+
#endif
6468
private:
6569
bool InitDS1307();
6670
bool InitDS3231();

0 commit comments

Comments
 (0)