Skip to content

Commit 7207778

Browse files
committed
[SD] All tests green again
1 parent c2ea367 commit 7207778

File tree

11 files changed

+302
-228
lines changed

11 files changed

+302
-228
lines changed

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ lib_compat_mode = soft ; can be strict once pio detects SleepyDog on RP2040
131131
extends = common:esp32
132132
board = esp32dev
133133
board_build.partitions = default_8MB.csv
134-
build_flags = -DARDUINO_ESP32_DEV -DOFFLINE_MODE_DEBUG
134+
build_flags = -DARDUINO_ESP32_DEV -DOFFLINE_MODE_DEBUG -DOFFLINE_MODE_WOKWI
135135
board_build.filesystem = littlefs
136136
upload_speed = 921600
137137

src/Wippersnapper_V2.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,9 +1095,11 @@ void Wippersnapper_V2::pingBrokerV2() {
10951095
*/
10961096
/*******************************************************/
10971097
void Wippersnapper_V2::feedWDTV2() {
1098+
#ifndef OFFLINE_MODE_WOKWI
10981099
// TODO: This is a temporary fix for watchdog.reset() not firing
10991100
// Watchdog.reset();
11001101
esp_task_wdt_reset();
1102+
#endif
11011103
}
11021104

11031105
/********************************************************/

src/components/analogIO/controller.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ bool AnalogIOController::EncodePublishPinEvent(
224224
*/
225225
/***************************************************************************/
226226
bool AnalogIOController::EncodePublishPinValue(uint8_t pin, uint16_t value) {
227-
228227
if (WsV2._sdCardV2->isModeOffline()) {
228+
WS_DEBUG_PRINTLN("Logging offline analogio event...");
229229
return WsV2._sdCardV2->LogGPIOSensorEventToSD(
230230
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW);
231231
} else {
@@ -262,8 +262,9 @@ bool AnalogIOController::EncodePublishPinVoltage(uint8_t pin, float value) {
262262
/***************************************************************************/
263263
void AnalogIOController::update() {
264264
// Bail-out if the vector is empty
265-
if (_analogio_pins.empty())
265+
if (_analogio_pins.empty()) {
266266
return;
267+
}
267268

268269
// Process analog input pins
269270
for (int i = 0; i < _analogio_pins.size(); i++) {

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 126 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
/**************************************************************************/
2222
ws_sdcard::ws_sdcard() {
2323
is_mode_offline = false;
24+
#ifdef OFFLINE_MODE_WOKWI
25+
_is_using_wokwi = true;
26+
#else
2427
_is_using_wokwi = false;
28+
#endif
2529
_use_test_data = false;
2630
_sz_log_file = 0;
2731
}
@@ -167,6 +171,41 @@ void ws_sdcard::CheckIn(uint8_t max_digital_pins, uint8_t max_analog_pins,
167171
WsV2.analogio_controller->SetRefVoltage(ref_voltage);
168172
}
169173

174+
/**************************************************************************/
175+
/*!
176+
@brief Parses a sensor type from the JSON configuration file.
177+
@param sensor_type
178+
The sensor type to parse.
179+
@returns The parsed sensor type.
180+
*/
181+
/**************************************************************************/
182+
wippersnapper_sensor_SensorType
183+
ws_sdcard::ParseSensorType(const char *sensor_type) {
184+
if (strcmp(sensor_type, "PIN_VALUE") == 0) {
185+
return wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW;
186+
} else if (strcmp(sensor_type, "VOLTAGE") == 0) {
187+
return wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE;
188+
} else if (strcmp(sensor_type, "object-temp-fahrenheit") == 0) {
189+
WS_DEBUG_PRINTLN("Found object-temp-fahrenheit");
190+
return wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT;
191+
} else if (strcmp(sensor_type, "object-temp") == 0) {
192+
WS_DEBUG_PRINTLN("Found object-temp");
193+
return wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE;
194+
} else {
195+
WS_DEBUG_PRINT("Found unspecified sensortype - ");
196+
WS_DEBUG_PRINTLN(sensor_type);
197+
return wippersnapper_sensor_SensorType_SENSOR_TYPE_UNSPECIFIED;
198+
}
199+
}
200+
201+
bool ws_sdcard::ValidateJSONKey(const char *key, const char *error_msg) {
202+
if (strcmp(key, UNKNOWN_VALUE) == 0) {
203+
WS_DEBUG_PRINTLN(error_msg);
204+
return false;
205+
}
206+
return true;
207+
}
208+
170209
/**************************************************************************/
171210
/*!
172211
@brief Parses a DigitalIOAdd message from the JSON configuration file.
@@ -193,10 +232,8 @@ bool ws_sdcard::ParseDigitalIOAdd(
193232
float period, bool value, const char *sample_mode, const char *direction,
194233
const char *pull) {
195234
bool rc = true;
196-
if (!strcmp(pin, UNKNOWN_VALUE) == 0) {
197-
WS_DEBUG_PRINTLN("[SD] Parsing Error: Digital pin name not found!");
235+
if (!ValidateJSONKey(pin, "[SD] Parsing Error: Digital pin name not found!"))
198236
return false;
199-
}
200237
strcpy(msg_DigitalIOAdd.pin_name, pin);
201238

202239
if (period == 0.0) {
@@ -207,9 +244,9 @@ bool ws_sdcard::ParseDigitalIOAdd(
207244
msg_DigitalIOAdd.value = value;
208245

209246
// Determine the sample mode
210-
if (strcmp(sample_mode, UNKNOWN_VALUE) == 0) {
211-
WS_DEBUG_PRINTLN(
212-
"[SD] Parsing Error: Digital pin's sample mode not found!");
247+
if (!ValidateJSONKey(
248+
sample_mode,
249+
"[SD] Parsing Error: Digital pin's sample mode not found!")) {
213250
return false;
214251
} else if (strcmp(sample_mode, "TIMER") == 0) {
215252
msg_DigitalIOAdd.sample_mode =
@@ -223,8 +260,9 @@ bool ws_sdcard::ParseDigitalIOAdd(
223260
}
224261

225262
// Determine the pin direction and pull
226-
if (strcmp(direction, UNKNOWN_VALUE) == 0) {
227-
WS_DEBUG_PRINTLN("[SD] Parsing Error: Digital pin's direction not found!");
263+
if (!ValidateJSONKey(
264+
direction,
265+
"[SD] Parsing Error: Digital pin's direction not found!")) {
228266
return false;
229267
} else if (strcmp(direction, "INPUT") == 0) {
230268
if (pull != nullptr) {
@@ -246,33 +284,6 @@ bool ws_sdcard::ParseDigitalIOAdd(
246284
return rc;
247285
}
248286

249-
/**************************************************************************/
250-
/*!
251-
@brief Parses a sensor type from the JSON configuration file.
252-
@param sensor_type
253-
The sensor type to parse.
254-
@returns The parsed sensor type.
255-
*/
256-
/**************************************************************************/
257-
wippersnapper_sensor_SensorType
258-
ws_sdcard::ParseSensorType(const char *sensor_type) {
259-
if (strcmp(sensor_type, "PIN_VALUE") == 0) {
260-
return wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW;
261-
} else if (strcmp(sensor_type, "VOLTAGE") == 0) {
262-
return wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE;
263-
} else if (strcmp(sensor_type, "object-temp-fahrenheit") == 0) {
264-
WS_DEBUG_PRINTLN("Found object-temp-fahrenheit");
265-
return wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT;
266-
} else if (strcmp(sensor_type, "object-temp") == 0) {
267-
WS_DEBUG_PRINTLN("Found object-temp");
268-
return wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE;
269-
} else {
270-
WS_DEBUG_PRINT("Found unspecified sensortype - ");
271-
WS_DEBUG_PRINTLN(sensor_type);
272-
return wippersnapper_sensor_SensorType_SENSOR_TYPE_UNSPECIFIED;
273-
}
274-
}
275-
276287
/**************************************************************************/
277288
/*!
278289
@brief Parses an AnalogIOAdd message from the JSON configuration file.
@@ -291,8 +302,21 @@ ws_sdcard::ParseSensorType(const char *sensor_type) {
291302
bool ws_sdcard::ParseAnalogIOAdd(
292303
wippersnapper_analogio_AnalogIOAdd &msg_AnalogIOAdd, const char *pin,
293304
float period, const char *mode) {
305+
306+
if (!ValidateJSONKey(pin, "[SD] Parsing Error: Analog pin name not found!"))
307+
return false;
294308
strcpy(msg_AnalogIOAdd.pin_name, pin);
309+
310+
if (period == 0.0) {
311+
WS_DEBUG_PRINTLN("[SD] Parsing Error: Analog pin period less than 1.0 "
312+
"seconds or not found!");
313+
return false;
314+
}
295315
msg_AnalogIOAdd.period = period;
316+
317+
if (!ValidateJSONKey(mode,
318+
"[SD] Parsing Error: Analog pin read mode not found!"))
319+
return false;
296320
msg_AnalogIOAdd.read_mode = ParseSensorType(mode);
297321
if (msg_AnalogIOAdd.read_mode ==
298322
wippersnapper_sensor_SensorType_SENSOR_TYPE_UNSPECIFIED) {
@@ -307,18 +331,47 @@ bool ws_sdcard::ParseDS18X20Add(
307331
wippersnapper_ds18x20_Ds18x20Add &msg_DS18X20Add, const char *pin,
308332
int resolution, float period, int num_sensors, const char *sensor_type_1,
309333
const char *sensor_type_2) {
334+
335+
if (strcmp(pin, UNKNOWN_VALUE) == 0) {
336+
WS_DEBUG_PRINTLN("[SD] Parsing Error: DS18X20 pin name not found!");
337+
return false;
338+
}
310339
strcpy(msg_DS18X20Add.onewire_pin, pin);
340+
341+
if (resolution == 0) {
342+
WS_DEBUG_PRINTLN(
343+
"[SD] Parsing Error: DS18X20 sensor resolution not found!");
344+
return false;
345+
}
311346
msg_DS18X20Add.sensor_resolution = resolution;
347+
348+
if (period == 0.0) {
349+
WS_DEBUG_PRINTLN("[SD] Parsing Error: DS18X20 sensor period not found!");
350+
return false;
351+
}
312352
msg_DS18X20Add.period = period;
353+
354+
if (num_sensors == 0) {
355+
WS_DEBUG_PRINTLN("[SD] Parsing Error: DS18X20 sensor count not found!");
356+
return false;
357+
}
313358
msg_DS18X20Add.sensor_types_count = num_sensors;
314359

315360
WS_DEBUG_PRINT("[SD] msg_DS18X20Add.sensor_types_count: ");
316361
WS_DEBUG_PRINTLN(msg_DS18X20Add.sensor_types_count);
317362

318363
// Parse the first sensor type
364+
if (strcmp(sensor_type_1, UNKNOWN_VALUE) == 0) {
365+
WS_DEBUG_PRINTLN("[SD] Parsing Error: DS18X20 sensor type 1 not found!");
366+
return false;
367+
}
319368
msg_DS18X20Add.sensor_types[0] = ParseSensorType(sensor_type_1);
320369
// Parse the second sensor type, if it exists
321370
if (num_sensors == 2) {
371+
if (strcmp(sensor_type_2, UNKNOWN_VALUE) == 0) {
372+
WS_DEBUG_PRINTLN("[SD] Parsing Error: DS18X20 sensor type 2 not found!");
373+
return false;
374+
}
322375
msg_DS18X20Add.sensor_types[1] = ParseSensorType(sensor_type_2);
323376
}
324377
return true;
@@ -414,8 +467,9 @@ bool ws_sdcard::parseConfigFile() {
414467
error = deserializeJson(doc, file_config);
415468
#else
416469
// Test Mode - do not use the SD card, use test data instead!
417-
if (_use_test_data == true) {
470+
if (!_use_test_data) {
418471
WS_DEBUG_PRINTLN("[SD] Parsing Serial Input...");
472+
WS_DEBUG_PRINT(_serialInput);
419473
error = deserializeJson(doc, _serialInput.c_str(), max_json_len);
420474
} else {
421475
WS_DEBUG_PRINTLN("[SD] Parsing Test Data...");
@@ -432,6 +486,8 @@ bool ws_sdcard::parseConfigFile() {
432486
return false;
433487
}
434488

489+
WS_DEBUG_PRINTLN("[SD] Successfully deserialized JSON config file!");
490+
435491
// NOTE: This is only used by the CI runner, production builds do not run
436492
// this!
437493
const char *exportedBy = doc["exportedBy"];
@@ -448,25 +504,28 @@ bool ws_sdcard::parseConfigFile() {
448504
}
449505

450506
// Mock the check-in process using the JSON's values
507+
WS_DEBUG_PRINTLN("[SD] Mocking check-in process...");
451508
CheckIn(exportedFromDevice["totalGPIOPins"] | 0,
452509
exportedFromDevice["totalAnalogPins"] | 0,
453510
exportedFromDevice["referenceVoltage"] | 0.0);
454511

512+
WS_DEBUG_PRINTLN("[SD] Configuring status LED...");
455513
setStatusLEDBrightness(exportedFromDevice["statusLEDBrightness"] | 0.3);
456514

457-
// Initialize and configure RTC
515+
// Initialize and configure RTC
516+
#ifndef OFFLINE_MODE_WOKWI
517+
WS_DEBUG_PRINTLN("[SD] Configuring RTC...");
458518
const char *json_rtc = exportedFromDevice["rtc"] | "SOFT_RTC";
459-
if (json_rtc == nullptr) {
460-
WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - rtc field not found in "
461-
"configuration JSON, unable to configure RTC!");
462-
return false;
463-
}
464519
if (!ConfigureRTC(json_rtc)) {
465520
WS_DEBUG_PRINTLN("[SD] Failed to to configure RTC!");
466521
return false;
467522
}
523+
#else
524+
WS_DEBUG_PRINTLN("[SD] Skipping RTC configuration for Wokwi Simulator...");
525+
#endif
468526

469527
// Parse the "components" array into a JsonObject
528+
WS_DEBUG_PRINTLN("[SD] Parsing out components array...");
470529
JsonArray components_ar = doc["components"].as<JsonArray>();
471530
if (components_ar.isNull()) {
472531
WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - No components array found in "
@@ -512,8 +571,10 @@ bool ws_sdcard::parseConfigFile() {
512571
wippersnapper_analogio_AnalogIOAdd msg_AnalogIOAdd =
513572
wippersnapper_analogio_AnalogIOAdd_init_default;
514573
// Parse: JSON->AnalogIOAdd
515-
if (!ParseAnalogIOAdd(msg_AnalogIOAdd, component["pinName"],
516-
component["period"], component["analogReadMode"])) {
574+
if (!ParseAnalogIOAdd(msg_AnalogIOAdd,
575+
component["pinName"] | UNKNOWN_VALUE,
576+
component["period"] | 0.0,
577+
component["analogReadMode"] | UNKNOWN_VALUE)) {
517578
WS_DEBUG_PRINTLN(
518579
"[SD] FATAL Parsing error - Unable to parse AnalogIO component!");
519580
return false;
@@ -526,11 +587,12 @@ bool ws_sdcard::parseConfigFile() {
526587
wippersnapper_ds18x20_Ds18x20Add msg_DS18X20Add =
527588
wippersnapper_ds18x20_Ds18x20Add_init_default;
528589
// Parse: JSON->DS18X20Add
529-
if (!ParseDS18X20Add(msg_DS18X20Add, component["pinName"],
530-
component["sensorResolution"], component["period"],
531-
component["sensorTypeCount"],
532-
component["sensorType1"],
533-
component["sensorType2"])) {
590+
if (!ParseDS18X20Add(msg_DS18X20Add, component["pinName"] | UNKNOWN_VALUE,
591+
component["sensorResolution"] | 0,
592+
component["period"] | 0.0,
593+
component["sensorTypeCount"] | 0,
594+
component["sensorType1"] | UNKNOWN_VALUE,
595+
component["sensorType2"] | UNKNOWN_VALUE)) {
534596
WS_DEBUG_PRINTLN(
535597
"[SD] FATAL Parsing error - Unable to parse DS18X20 component!");
536598
return false;
@@ -569,12 +631,11 @@ uint32_t ws_sdcard::GetTimestamp() {
569631
now = _rtc_ds1307->now();
570632
else if (_rtc_pcf8523 != nullptr)
571633
now = _rtc_pcf8523->now();
572-
else {
634+
else if (_rtc_soft != nullptr) {
573635
now = _rtc_soft->now();
574-
}
575-
576-
if (_is_using_wokwi)
636+
} else { // we're either using a simulator or have undefined behavior
577637
return 0;
638+
}
578639

579640
return now.unixtime();
580641
}
@@ -683,6 +744,7 @@ void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, uint16_t value,
683744
char pin_name[12];
684745
sprintf(pin_name, "A%d", pin);
685746
doc["timestamp"] = GetTimestamp();
747+
;
686748
doc["pin"] = pin_name;
687749
doc["value"] = value;
688750
doc["si_unit"] = SensorTypeToString(read_type);
@@ -847,7 +909,13 @@ bool ws_sdcard::waitForSerialConfig() {
847909
// 2. Provide a JSON string via the hardware's serial input
848910
// 3. Use a test JSON string - for debugging purposes ONLY
849911

850-
_use_test_data = true;
912+
// TODO: Redundant conditional - should this just be enabled within the class
913+
// ctor?
914+
if (_is_using_wokwi)
915+
_use_test_data = false;
916+
else
917+
_use_test_data = true;
918+
851919
json_test_data = "{"
852920
"\"exportVersion\": \"1.0.0\","
853921
"\"exportedBy\": \"tester\","
@@ -900,8 +968,8 @@ bool ws_sdcard::waitForSerialConfig() {
900968
"\"componentAPI\": \"ds18x20\","
901969
"\"name\": \"DS18B20: Temperature Sensor (°F)\","
902970
"\"sensorTypeCount\": 2,"
903-
"\"sensorType1\": \"ambient-temp-fahrenheit\","
904-
"\"sensorType2\": \"ambient-temp\","
971+
"\"sensorType1\": \"object-temp-fahrenheit\","
972+
"\"sensorType2\": \"object-temp\","
905973
"\"pinName\": \"D12\","
906974
"\"sensorResolution\": 12,"
907975
"\"period\": 5"
@@ -910,8 +978,8 @@ bool ws_sdcard::waitForSerialConfig() {
910978
"\"componentAPI\": \"ds18x20\","
911979
"\"name\": \"DS18B20: Temperature Sensor (°F)\","
912980
"\"sensorTypeCount\": 2,"
913-
"\"sensorType1\": \"ambient-temp-fahrenheit\","
914-
"\"sensorType2\": \"ambient-temp\","
981+
"\"sensorType1\": \"object-temp-fahrenheit\","
982+
"\"sensorType2\": \"object-temp\","
915983
"\"pinName\": \"D25\","
916984
"\"sensorResolution\": 12,"
917985
"\"period\": 5"

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ws_sdcard {
5353

5454
private:
5555
bool ValidateJSON(const char *input);
56+
bool ValidateJSONKey(const char *key, const char *error_msg);
5657
void CheckIn(uint8_t max_digital_pins, uint8_t max_analog_pins,
5758
float ref_voltage);
5859

tests/bin/offline/firmware.elf

131 KB
Binary file not shown.

0 commit comments

Comments
 (0)