@@ -32,11 +32,9 @@ ws_sdcard::ws_sdcard() {
32
32
*/
33
33
/* *************************************************************************/
34
34
ws_sdcard::~ws_sdcard () {
35
- // TODO: Close any open files
36
- // Then, end the SD card (ends SPI transaction)
37
35
if (is_mode_offline) {
38
- _sd.end ();
39
- is_mode_offline = false ;
36
+ _sd.end (); // Close the SD card
37
+ is_mode_offline = false ; // Disable offline mode
40
38
}
41
39
}
42
40
@@ -195,11 +193,25 @@ bool ws_sdcard::ParseDigitalIOAdd(
195
193
float period, bool value, const char *sample_mode, const char *direction,
196
194
const char *pull) {
197
195
bool rc = true ;
196
+ if (!strcmp (pin, UNKNOWN_VALUE) == 0 ) {
197
+ WS_DEBUG_PRINTLN (" [SD] Parsing Error: Digital pin name not found!" );
198
+ return false ;
199
+ }
198
200
strcpy (msg_DigitalIOAdd.pin_name , pin);
201
+
202
+ if (period == 0.0 ) {
203
+ WS_DEBUG_PRINTLN (" [SD] Parsing Error: Digital pin period not found!" );
204
+ return false ;
205
+ }
199
206
msg_DigitalIOAdd.period = period;
200
207
msg_DigitalIOAdd.value = value;
208
+
201
209
// Determine the sample mode
202
- if (strcmp (sample_mode, " TIMER" ) == 0 ) {
210
+ if (strcmp (sample_mode, UNKNOWN_VALUE) == 0 ) {
211
+ WS_DEBUG_PRINTLN (
212
+ " [SD] Parsing Error: Digital pin's sample mode not found!" );
213
+ return false ;
214
+ } else if (strcmp (sample_mode, " TIMER" ) == 0 ) {
203
215
msg_DigitalIOAdd.sample_mode =
204
216
wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_TIMER;
205
217
} else if (strcmp (sample_mode, " EVENT" ) == 0 ) {
@@ -211,7 +223,10 @@ bool ws_sdcard::ParseDigitalIOAdd(
211
223
}
212
224
213
225
// Determine the pin direction and pull
214
- if (strcmp (direction, " INPUT" ) == 0 ) {
226
+ if (strcmp (direction, UNKNOWN_VALUE) == 0 ) {
227
+ WS_DEBUG_PRINTLN (" [SD] Parsing Error: Digital pin's direction not found!" );
228
+ return false ;
229
+ } else if (strcmp (direction, " INPUT" ) == 0 ) {
215
230
if (pull != nullptr ) {
216
231
msg_DigitalIOAdd.gpio_direction =
217
232
wippersnapper_digitalio_DigitalIODirection_DIGITAL_IO_DIRECTION_INPUT_PULL_UP;
@@ -321,7 +336,7 @@ bool ws_sdcard::ParseDS18X20Add(
321
336
bool ws_sdcard::PushSignalToSharedBuffer (
322
337
wippersnapper_signal_BrokerToDevice &msg_signal) {
323
338
// Create a temporary buffer to hold the encoded signal message
324
- std::vector<uint8_t > tempBuf (128 );
339
+ std::vector<uint8_t > tempBuf (512 );
325
340
size_t tempBufSz;
326
341
327
342
// Get the encoded size of the signal message first so we can resize the
@@ -357,8 +372,15 @@ bool ws_sdcard::PushSignalToSharedBuffer(
357
372
/* *************************************************************************/
358
373
bool ws_sdcard::CreateNewLogFile () {
359
374
File32 file;
375
+ // Generate a name for the new log file using the RTC's timestamp
360
376
String logFilename = " log_" + String (GetTimestamp ()) + " .json" ;
361
- _log_filename = logFilename.c_str ();
377
+ static char log_filename_buffer[256 ];
378
+ strncpy (log_filename_buffer, logFilename.c_str (),
379
+ sizeof (log_filename_buffer) - 1 );
380
+ log_filename_buffer[sizeof (log_filename_buffer) - 1 ] = ' \0 ' ;
381
+ _log_filename = log_filename_buffer;
382
+
383
+ // Attempt to create the new log file
362
384
if (!file.open (_log_filename, FILE_WRITE))
363
385
return false ;
364
386
WS_DEBUG_PRINT (" [SD] Created new log file on SD card: " );
@@ -375,18 +397,17 @@ bool ws_sdcard::CreateNewLogFile() {
375
397
*/
376
398
/* *************************************************************************/
377
399
bool ws_sdcard::parseConfigFile () {
378
- File32 file_config;
379
- JsonDocument doc;
380
- int max_json_len =
381
- 2048 ; // TODO: It's possible this is too small - what's the max size?
400
+ int max_json_len = 4096 ;
382
401
383
402
// Attempt to open and deserialize the JSON config file
403
+ File32 file_config;
384
404
DeserializationError error;
405
+ JsonDocument doc;
385
406
#ifndef OFFLINE_MODE_DEBUG
386
- WS_DEBUG_PRINTLN (" [SD] Parsing config.json from SD card..." );
407
+ WS_DEBUG_PRINTLN (" [SD] Parsing config.txt from SD card..." );
387
408
if (!_sd.exists (" config.txt" )) {
388
409
WS_DEBUG_PRINTLN (
389
- " [SD] FATAL Error - config.json file not found on SD Card!" );
410
+ " [SD] FATAL Error - config.txt file not found on SD Card!" );
390
411
return false ;
391
412
}
392
413
file_config = _sd.open (" config.txt" , O_RDONLY);
@@ -427,14 +448,14 @@ bool ws_sdcard::parseConfigFile() {
427
448
}
428
449
429
450
// Mock the check-in process using the JSON's values
430
- CheckIn (exportedFromDevice[" totalGPIOPins" ],
431
- exportedFromDevice[" totalAnalogPins" ],
432
- exportedFromDevice[" referenceVoltage" ]);
451
+ CheckIn (exportedFromDevice[" totalGPIOPins" ] | 0 ,
452
+ exportedFromDevice[" totalAnalogPins" ] | 0 ,
453
+ exportedFromDevice[" referenceVoltage" ] | 0.0 );
433
454
434
- setStatusLEDBrightness (exportedFromDevice[" statusLEDBrightness" ]);
455
+ setStatusLEDBrightness (exportedFromDevice[" statusLEDBrightness" ] | 0.3 );
435
456
436
457
// Initialize and configure RTC
437
- const char *json_rtc = exportedFromDevice[" rtc" ];
458
+ const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT_RTC " ;
438
459
if (json_rtc == nullptr ) {
439
460
WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - rtc field not found in "
440
461
" configuration JSON, unable to configure RTC!" );
@@ -474,12 +495,13 @@ bool ws_sdcard::parseConfigFile() {
474
495
" [SD] DigitalIO component found, decoding JSON to PB..." );
475
496
wippersnapper_digitalio_DigitalIOAdd msg_DigitalIOAdd =
476
497
wippersnapper_digitalio_DigitalIOAdd_init_default;
477
- if (!ParseDigitalIOAdd (msg_DigitalIOAdd, component[" pinName" ],
478
- component[" period" ], component[" value" ],
479
- component[" sampleMode" ], component[" direction" ],
480
- component[" pull" ])) {
481
- WS_DEBUG_PRINTLN (
482
- " [SD] FATAL Parsing error - Unable to parse DigitalIO component!" );
498
+ if (!ParseDigitalIOAdd (
499
+ msg_DigitalIOAdd, component[" pinName" ] | UNKNOWN_VALUE,
500
+ component[" period" ] | 0.0 , component[" value" ],
501
+ component[" sampleMode" ] | UNKNOWN_VALUE,
502
+ component[" direction" ] | UNKNOWN_VALUE, component[" pull" ])) {
503
+ WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - Unable to parse "
504
+ " DigitalIO component!" );
483
505
return false ;
484
506
}
485
507
msg_signal_b2d.which_payload =
@@ -693,6 +715,14 @@ bool ws_sdcard::LogJSONDoc(JsonDocument &doc) {
693
715
Serial.print (" \n " ); // JSONL format specifier
694
716
#endif
695
717
_sz_log_file = szJson + 2 ; // +2 bytes for "\n"
718
+
719
+ if (_sz_log_file > MAX_LOG_FILE_SZ) {
720
+ WS_DEBUG_PRINTLN (" [SD] Log file size has exceeded maximum size, creating "
721
+ " a new file..." );
722
+ CreateNewLogFile ();
723
+ return false ;
724
+ }
725
+
696
726
return true ;
697
727
}
698
728
@@ -773,19 +803,15 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
773
803
/* *************************************************************************/
774
804
bool ws_sdcard::LogDS18xSensorEventToSD (
775
805
wippersnapper_ds18x20_Ds18x20Event *event_msg) {
776
- // Get the RTC's timestamp
777
- uint32_t timestamp = GetTimestamp ();
778
-
779
- // Create the JSON document
780
806
JsonDocument doc;
781
807
// Iterate over the event message's sensor events
782
808
for (int i = 0 ; i < event_msg->sensor_events_count ; i++) {
809
+ uint32_t timestamp = GetTimestamp ();
783
810
doc[" timestamp" ] = timestamp;
784
811
doc[" pin" ] = event_msg->onewire_pin ;
785
812
doc[" value" ] = event_msg->sensor_events [i].value .float_value ;
786
813
doc[" si_unit" ] = SensorTypeToString (event_msg->sensor_events [i].type );
787
- serializeJson (doc, Serial);
788
- Serial.println (" " );
814
+ LogJSONDoc (doc);
789
815
}
790
816
return true ;
791
817
}
@@ -799,7 +825,7 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
799
825
@returns True if the provided JSON string is valid, False otherwise.
800
826
*/
801
827
/* *************************************************************************/
802
- bool ws_sdcard::validateJson (const char *input) {
828
+ bool ws_sdcard::ValidateJSON (const char *input) {
803
829
JsonDocument doc, filter;
804
830
805
831
DeserializationError error =
@@ -931,12 +957,12 @@ bool ws_sdcard::waitForSerialConfig() {
931
957
932
958
// Attempt to validate the string as JSON
933
959
if (!_use_test_data) {
934
- if (!validateJson (_serialInput.c_str ())) {
960
+ if (!ValidateJSON (_serialInput.c_str ())) {
935
961
WS_DEBUG_PRINTLN (" [SD] Invalid JSON string received!" );
936
962
return false ;
937
963
}
938
964
} else {
939
- if (!validateJson (json_test_data)) {
965
+ if (!ValidateJSON (json_test_data)) {
940
966
WS_DEBUG_PRINTLN (" [SD] Invalid JSON string received!" );
941
967
return false ;
942
968
}
0 commit comments