@@ -45,15 +45,13 @@ ws_sdcard::~ws_sdcard() {
45
45
*/
46
46
/* *************************************************************************/
47
47
bool ws_sdcard::InitSDCard (uint8_t pin_cs) {
48
- /* if (pin_cs == 255)
49
- return false; */
48
+ /* if (pin_cs == 255)
49
+ return false; */
50
50
if (_sd.begin (pin_cs)) {
51
51
is_mode_offline = true ;
52
52
return is_mode_offline;
53
53
}
54
- return false ;
55
-
56
-
54
+ return false ;
57
55
}
58
56
59
57
/* *************************************************************************/
@@ -390,7 +388,7 @@ bool ws_sdcard::ParseDS18X20Add(
390
388
buffer, False otherwise.
391
389
*/
392
390
/* *************************************************************************/
393
- bool ws_sdcard::PushSignalToSharedBuffer (
391
+ bool ws_sdcard::AddSignalMessageToSharedBuffer (
394
392
wippersnapper_signal_BrokerToDevice &msg_signal) {
395
393
// Create a temporary buffer to hold the encoded signal message
396
394
std::vector<uint8_t > tempBuf (512 );
@@ -472,16 +470,14 @@ bool ws_sdcard::ValidateChecksum(JsonDocument &doc) {
472
470
/* *************************************************************************/
473
471
bool ws_sdcard::parseConfigFile () {
474
472
int max_json_len = 4096 ;
475
-
476
- // Attempt to open and deserialize the JSON config file
477
- File32 file_config;
478
473
DeserializationError error;
479
474
JsonDocument doc;
475
+
480
476
#ifndef OFFLINE_MODE_DEBUG
481
477
WS_DEBUG_PRINTLN (" [SD] Parsing config.json..." );
482
- doc = WsV2._config_doc ;
478
+ doc = WsV2._config_doc ; // Use the config document from the filesystem
483
479
#else
484
- // Test Mode - do not use the SD card, use test data instead!
480
+ // Use test data rather than data from the filesystem
485
481
if (!_use_test_data) {
486
482
WS_DEBUG_PRINTLN (" [SD] Parsing Serial Input..." );
487
483
WS_DEBUG_PRINT (_serialInput);
@@ -491,43 +487,43 @@ bool ws_sdcard::parseConfigFile() {
491
487
error = deserializeJson (doc, json_test_data, max_json_len);
492
488
}
493
489
#endif
494
- // If the JSON document failed to deserialize - halt the running device and
495
- // print the error because it is not possible to continue running in offline
496
- // mode without a valid config file
490
+ // It is not possible to continue running in offline mode without a valid
491
+ // config file
497
492
if (error) {
498
493
WS_DEBUG_PRINTLN (" [SD] Unable to deserialize config JSON, error code: " +
499
494
String (error.c_str ()));
500
495
return false ;
501
496
}
502
497
WS_DEBUG_PRINTLN (" [SD] Successfully deserialized JSON config file!" );
503
498
504
- // Calculate the 8-bit checksum of the JSON file to ensure it is valid data
499
+ // Check the file's integrity
505
500
if (!ValidateChecksum (doc)) {
506
501
WS_DEBUG_PRINTLN (" [SD] Checksum mismatch, file has been modified from its "
507
502
" original state!" );
508
503
}
509
504
WS_DEBUG_PRINTLN (" [SD] JSON checksum OK!" );
510
505
511
- // Parse the exportedFromDevice array
506
+ // Begin parsing the JSON document
512
507
JsonObject exportedFromDevice = doc[" exportedFromDevice" ];
513
508
if (exportedFromDevice.isNull ()) {
514
509
WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - No exportedFromDevice object "
515
510
" found in JSON string! Unable to configure hardware!" );
516
511
return false ;
517
512
}
513
+ JsonArray components_ar = doc[" components" ].as <JsonArray>();
514
+ if (components_ar.isNull ()) {
515
+ WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - No components array found in "
516
+ " JSON string!" );
517
+ return false ;
518
+ }
518
519
519
- // Mock the check-in process using the JSON's values
520
- WS_DEBUG_PRINTLN (" [SD] Mocking check-in process..." );
520
+ // We don't talk to IO here, perform an "offline" device check-in
521
521
CheckIn (exportedFromDevice[" totalGPIOPins" ] | 0 ,
522
522
exportedFromDevice[" totalAnalogPins" ] | 0 ,
523
523
exportedFromDevice[" referenceVoltage" ] | 0.0 );
524
-
525
- WS_DEBUG_PRINTLN (" [SD] Configuring status LED..." );
526
524
setStatusLEDBrightness (exportedFromDevice[" statusLEDBrightness" ] | 0.3 );
527
525
528
- // Initialize and configure RTC
529
526
#ifndef OFFLINE_MODE_WOKWI
530
- WS_DEBUG_PRINTLN (" [SD] Configuring RTC..." );
531
527
const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT_RTC" ;
532
528
if (!ConfigureRTC (json_rtc)) {
533
529
WS_DEBUG_PRINTLN (" [SD] Failed to to configure RTC!" );
@@ -537,17 +533,6 @@ bool ws_sdcard::parseConfigFile() {
537
533
WS_DEBUG_PRINTLN (" [SD] Skipping RTC configuration for Wokwi Simulator..." );
538
534
#endif
539
535
540
- // Parse the "components" array into a JsonObject
541
- WS_DEBUG_PRINTLN (" [SD] Parsing out components array..." );
542
- JsonArray components_ar = doc[" components" ].as <JsonArray>();
543
- if (components_ar.isNull ()) {
544
- WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - No components array found in "
545
- " JSON string!" );
546
- return false ;
547
- }
548
- int count = components_ar.size ();
549
- WS_DEBUG_PRINTLN (" [SD] Found " + String (count) + " components in JSON file!" );
550
-
551
536
// Parse each component from JSON->PB and push into a shared buffer
552
537
for (JsonObject component : doc[" components" ].as <JsonArray>()) {
553
538
wippersnapper_signal_BrokerToDevice msg_signal_b2d =
@@ -576,14 +561,14 @@ bool ws_sdcard::parseConfigFile() {
576
561
" DigitalIO component!" );
577
562
return false ;
578
563
}
564
+
579
565
msg_signal_b2d.which_payload =
580
566
wippersnapper_signal_BrokerToDevice_digitalio_add_tag;
581
567
msg_signal_b2d.payload .digitalio_add = msg_DigitalIOAdd;
582
568
} else if (strcmp (component_api_type, " analogio" ) == 0 ) {
583
569
WS_DEBUG_PRINTLN (" [SD] AnalogIO component found, decoding JSON to PB..." );
584
570
wippersnapper_analogio_AnalogIOAdd msg_AnalogIOAdd =
585
571
wippersnapper_analogio_AnalogIOAdd_init_default;
586
- // Parse: JSON->AnalogIOAdd
587
572
if (!ParseAnalogIOAdd (msg_AnalogIOAdd,
588
573
component[" pinName" ] | UNKNOWN_VALUE,
589
574
component[" period" ] | 0.0 ,
@@ -592,14 +577,14 @@ bool ws_sdcard::parseConfigFile() {
592
577
" [SD] FATAL Parsing error - Unable to parse AnalogIO component!" );
593
578
return false ;
594
579
}
580
+
595
581
msg_signal_b2d.which_payload =
596
582
wippersnapper_signal_BrokerToDevice_analogio_add_tag;
597
583
msg_signal_b2d.payload .analogio_add = msg_AnalogIOAdd;
598
584
} else if (strcmp (component_api_type, " ds18x20" ) == 0 ) {
599
585
WS_DEBUG_PRINTLN (" [SD] Ds18x20 component found, decoding JSON to PB..." );
600
586
wippersnapper_ds18x20_Ds18x20Add msg_DS18X20Add =
601
587
wippersnapper_ds18x20_Ds18x20Add_init_default;
602
- // Parse: JSON->DS18X20Add
603
588
if (!ParseDS18X20Add (msg_DS18X20Add, component[" pinName" ] | UNKNOWN_VALUE,
604
589
component[" sensorResolution" ] | 0 ,
605
590
component[" period" ] | 0.0 ,
@@ -610,6 +595,7 @@ bool ws_sdcard::parseConfigFile() {
610
595
" [SD] FATAL Parsing error - Unable to parse DS18X20 component!" );
611
596
return false ;
612
597
}
598
+
613
599
msg_signal_b2d.which_payload =
614
600
wippersnapper_signal_BrokerToDevice_ds18x20_add_tag;
615
601
msg_signal_b2d.payload .ds18x20_add = msg_DS18X20Add;
@@ -620,10 +606,10 @@ bool ws_sdcard::parseConfigFile() {
620
606
return false ;
621
607
}
622
608
623
- // Push the signal message into the shared buffer
624
- if (!PushSignalToSharedBuffer (msg_signal_b2d)) {
625
- WS_DEBUG_PRINTLN (" [SD] FATAL Error - Unable to push signal message to "
626
- " shared buffer!" );
609
+ // App handles the signal messages, in-order
610
+ if (!AddSignalMessageToSharedBuffer (msg_signal_b2d)) {
611
+ WS_DEBUG_PRINTLN (
612
+ " [SD] FATAL Error - Unable to add signal message to shared buffer!" );
627
613
return false ;
628
614
}
629
615
}
@@ -897,6 +883,8 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
897
883
return true ;
898
884
}
899
885
886
+ // TODO: Do we even need to use this function anymore now that we're running
887
+ // wokwi unit tests?
900
888
#ifdef OFFLINE_MODE_DEBUG
901
889
/* *************************************************************************/
902
890
/* !
0 commit comments