@@ -466,7 +466,6 @@ uint32_t ws_sdcard::HexStrToInt(const char *hex_str) {
466
466
return std::stoi (hex_str, nullptr , 16 );
467
467
}
468
468
469
-
470
469
/* !
471
470
@brief Parses a Uartadd message from the JSON configuration file.
472
471
@param component
@@ -475,56 +474,143 @@ uint32_t ws_sdcard::HexStrToInt(const char *hex_str) {
475
474
The UartAdd message to populate.
476
475
@returns True if the message was successfully populated, False otherwise.
477
476
*/
478
- bool ws_sdcard::ParseUartAdd (JsonObject &component, wippersnapper_uart_UartAdd &msg_uart_add) {
477
+ bool ws_sdcard::ParseUartAdd (JsonObject &component,
478
+ wippersnapper_uart_UartAdd &msg_uart_add) {
479
479
// Configure the Serial
480
480
msg_uart_add.has_cfg_serial = true ;
481
- strncpy (msg_uart_add.cfg_serial .pin_rx , component[" pinRx" ] | UNKNOWN_VALUE, sizeof (msg_uart_add.cfg_serial .pin_rx ) - 1 );
482
- strncpy (msg_uart_add.cfg_serial .pin_tx , component[" pinTx" ] | UNKNOWN_VALUE, sizeof (msg_uart_add.cfg_serial .pin_tx ) - 1 );
481
+ strncpy (msg_uart_add.cfg_serial .pin_rx , component[" pinRx" ] | UNKNOWN_VALUE,
482
+ sizeof (msg_uart_add.cfg_serial .pin_rx ) - 1 );
483
+ strncpy (msg_uart_add.cfg_serial .pin_tx , component[" pinTx" ] | UNKNOWN_VALUE,
484
+ sizeof (msg_uart_add.cfg_serial .pin_tx ) - 1 );
483
485
msg_uart_add.cfg_serial .uart_nbr = component[" uartNbr" ] | 0 ;
484
486
msg_uart_add.cfg_serial .baud_rate = component[" baudRate" ] | 9600 ;
485
- msg_uart_add.cfg_serial .format = wippersnapper_uart_UartPacketFormat_UART_PACKET_FORMAT_8N1; // Stick to default 8N1 for now
486
- msg_uart_add.cfg_serial .timeout = component[" timeout" ] | 1000 ; // Use a default UART timeout of 1000ms
487
+ msg_uart_add.cfg_serial .format =
488
+ wippersnapper_uart_UartPacketFormat_UART_PACKET_FORMAT_8N1; // Stick to
489
+ // default 8N1
490
+ // for now
491
+ msg_uart_add.cfg_serial .timeout =
492
+ component[" timeout" ] | 1000 ; // Use a default UART timeout of 1000ms
487
493
msg_uart_add.cfg_serial .use_sw_serial = component[" useSwSerial" ] | false ;
488
- msg_uart_add.cfg_serial .sw_serial_invert = component[" swSerialInvert" ] | false ;
489
- // Configure the UART device
494
+ msg_uart_add.cfg_serial .sw_serial_invert =
495
+ component[" swSerialInvert" ] | false ;
496
+ // Configure the Device
490
497
msg_uart_add.has_cfg_device = true ;
491
- strncpy (msg_uart_add.cfg_device .device_id , component[" deviceId" ] | UNKNOWN_VALUE, sizeof (msg_uart_add.cfg_device .device_id ) - 1 );
492
- // set UartDeviceType
498
+ strncpy (msg_uart_add.cfg_device .device_id ,
499
+ component[" deviceId" ] | UNKNOWN_VALUE,
500
+ sizeof (msg_uart_add.cfg_device .device_id ) - 1 );
493
501
const char *device_type = component[" deviceType" ] | " UNKNOWN" ;
494
502
if (strcmp (device_type, " GPS" ) == 0 ) {
495
- msg_uart_add.cfg_device .device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS;
496
- // Fill the config field
503
+ msg_uart_add.cfg_device .device_type =
504
+ wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS;
505
+ msg_uart_add.cfg_device .which_config =
506
+ wippersnapper_uart_UartDeviceConfig_gps_tag;
507
+ if (!ParseGPSConfig (component, &msg_uart_add.cfg_device .config .gps )) {
508
+ WS_DEBUG_PRINTLN (
509
+ " [SD] Parsing Error: Failed to parse GPS configuration!" );
510
+ return false ;
511
+ }
497
512
} else if (strcmp (device_type, " PM25AQI" ) == 0 ) {
498
- msg_uart_add.cfg_device .device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI;
499
- msg_uart_add.cfg_device .which_config = wippersnapper_uart_UartDeviceConfig_pm25aqi_tag;
513
+ WS_DEBUG_PRINTLN (" [SD] Parsing PM25AQI device type" );
514
+ msg_uart_add.cfg_device .device_type =
515
+ wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI;
516
+ msg_uart_add.cfg_device .which_config =
517
+ wippersnapper_uart_UartDeviceConfig_pm25aqi_tag;
500
518
msg_uart_add.cfg_device .config .pm25aqi .period = component[" period" ] | 0.0 ;
501
- msg_uart_add.cfg_device .config .pm25aqi .is_pm1006 = component[" isPm1006" ] | false ;
519
+ msg_uart_add.cfg_device .config .pm25aqi .is_pm1006 =
520
+ component[" isPm1006" ] | false ;
502
521
// Fill sensor types
503
522
pb_size_t sensor_type_count = 0 ;
504
523
for (JsonObject sensor_type : component[" SensorTypes" ].as <JsonArray>()) {
505
- msg_uart_add.cfg_device .config .pm25aqi .sensor_types [sensor_type_count] = ParseSensorType (sensor_type[" type" ]);
506
- sensor_type_count++;
524
+ msg_uart_add.cfg_device .config .pm25aqi .sensor_types [sensor_type_count] =
525
+ ParseSensorType (sensor_type[" type" ]);
526
+ sensor_type_count++;
507
527
}
508
- msg_uart_add.cfg_device .config .pm25aqi .sensor_types_count = sensor_type_count;
528
+ msg_uart_add.cfg_device .config .pm25aqi .sensor_types_count =
529
+ sensor_type_count;
509
530
} else if (strcmp (device_type, " GENERIC-INPUT" ) == 0 ) {
510
- // TODO: Fill device name (requires an update to uart.pb.h so it's not a pb_callback field)
511
- msg_uart_add.cfg_device .device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GENERIC_INPUT;
512
- msg_uart_add.cfg_device .which_config = wippersnapper_uart_UartDeviceConfig_generic_uart_input_tag;
513
- msg_uart_add.cfg_device .config .generic_uart_input .line_ending = ParseUartLineEnding (component[" lineEnding" ] | " LF" );
514
- msg_uart_add.cfg_device .config .generic_uart_input .period = component[" period" ] | 0.0 ;
531
+ WS_DEBUG_PRINTLN (" [SD] Parsing GENERIC-INPUT device type" );
532
+ // TODO: Fill device name (requires an update to uart.pb.h so it's not a
533
+ // pb_callback field)
534
+ msg_uart_add.cfg_device .device_type =
535
+ wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GENERIC_INPUT;
536
+ msg_uart_add.cfg_device .which_config =
537
+ wippersnapper_uart_UartDeviceConfig_generic_uart_input_tag;
538
+ msg_uart_add.cfg_device .config .generic_uart_input .line_ending =
539
+ ParseUartLineEnding (component[" lineEnding" ] | " LF" );
540
+ msg_uart_add.cfg_device .config .generic_uart_input .period =
541
+ component[" period" ] | 0.0 ;
515
542
// Fill sensor types
516
543
pb_size_t sensor_type_count = 0 ;
517
544
for (JsonObject sensor_type : component[" SensorTypes" ].as <JsonArray>()) {
518
- msg_uart_add.cfg_device .config .generic_uart_input .sensor_types [sensor_type_count] = ParseSensorType (sensor_type[" type" ]);
519
- sensor_type_count++;
545
+ msg_uart_add.cfg_device .config .generic_uart_input
546
+ .sensor_types [sensor_type_count] =
547
+ ParseSensorType (sensor_type[" type" ]);
548
+ sensor_type_count++;
520
549
}
521
- msg_uart_add.cfg_device .config .generic_uart_input .sensor_types_count = sensor_type_count;
550
+ msg_uart_add.cfg_device .config .generic_uart_input .sensor_types_count =
551
+ sensor_type_count;
522
552
} else {
523
- WS_DEBUG_PRINTLN (" [SD] Parsing Error: Unknown UART device type found: " + String (device_type));
553
+ WS_DEBUG_PRINTLN (" [SD] Parsing Error: Unknown UART device type found: " +
554
+ String (device_type));
524
555
return false ;
525
556
}
557
+ return true ;
558
+ }
526
559
527
-
560
+ /* !
561
+ @brief Parses a GPS configuration from the JSON configuration file.
562
+ @param gps_config
563
+ The JSON object containing GPS configuration.
564
+ @param cfg_gps
565
+ The GPSConfig structure to populate.
566
+ @returns True if the GPS configuration was successfully parsed, False
567
+ otherwise.
568
+ */
569
+ bool ws_sdcard::ParseGPSConfig (JsonObject &gps_config,
570
+ wippersnapper_gps_GPSConfig *cfg_gps) {
571
+ cfg_gps->period = gps_config[" period" ] | 15000 ; // Default period 15 seconds
572
+ // Parse PMTK or UBX commands if they exist
573
+ if (gps_config[" commands_pmtks" ].is <JsonArray>() &&
574
+ sizeof (cfg_gps->commands_pmtks ) > 0 ) {
575
+ WS_DEBUG_PRINTLN (" [SD] Parsing PMTK commands..." );
576
+ pb_size_t pmtk_cmd_count = 0 ;
577
+ JsonArray pmtk_array = gps_config[" commands_pmtks" ];
578
+ for (JsonVariant pmtk_cmd : pmtk_array) {
579
+ if (pmtk_cmd_count >= 8 ) {
580
+ WS_DEBUG_PRINTLN (" [SD] Warning: Too many PMTK commands, skipping..." );
581
+ break ;
582
+ }
583
+ strlcpy (cfg_gps->commands_pmtks [pmtk_cmd_count],
584
+ pmtk_cmd.as <const char *>(), 90 );
585
+ pmtk_cmd_count++;
586
+ }
587
+ cfg_gps->commands_pmtks_count = pmtk_cmd_count;
588
+ } else if (gps_config[" commands_ubxes" ].is <JsonArray>() &&
589
+ sizeof (cfg_gps->commands_ubxes ) > 0 ) {
590
+ WS_DEBUG_PRINTLN (" [SD] Parsing UBX commands..." );
591
+ pb_size_t ubx_cmd_count = 0 ;
592
+ JsonArray ubx_array = gps_config[" commands_ubxes" ];
593
+ for (JsonVariant ubx_cmd : ubx_array) {
594
+ if (ubx_cmd_count >= 8 ) {
595
+ WS_DEBUG_PRINTLN (" [SD] Warning: Too many UBX commands, skipping..." );
596
+ break ;
597
+ }
598
+ JsonArray ubx_bytes = ubx_cmd.as <JsonArray>();
599
+ for (JsonVariant byte_val : ubx_bytes) {
600
+ if (cfg_gps->commands_ubxes [ubx_cmd_count].size >= 128 ) {
601
+ WS_DEBUG_PRINTLN (" [SD] Warning: UBX command too long, skipping..." );
602
+ break ;
603
+ }
604
+ cfg_gps->commands_ubxes [ubx_cmd_count]
605
+ .bytes [cfg_gps->commands_ubxes [ubx_cmd_count].size ] =
606
+ byte_val.as <uint8_t >();
607
+ cfg_gps->commands_ubxes [ubx_cmd_count].size ++;
608
+ }
609
+ }
610
+ cfg_gps->commands_ubxes_count = ubx_cmd_count;
611
+ } else {
612
+ WS_DEBUG_PRINTLN (" [SD] No PMTK or UBX commands found for GPS!" );
613
+ }
528
614
return true ;
529
615
}
530
616
@@ -924,15 +1010,18 @@ bool ws_sdcard::ParseComponents(JsonArray &components) {
924
1010
_cfg_i2c_addresses.push_back (
925
1011
msg_add.i2c_device_description .i2c_device_address );
926
1012
}
927
- } else if (strcmp (component_api_type, " uart" ) ==0 ) {
928
- WS_DEBUG_PRINTLN (" [SD] UART component found in cfg" );
929
- wippersnapper_uart_UartAdd msg_uart_add = wippersnapper_uart_UartAdd_init_default;
930
- success = ParseUartAdd (component, msg_uart_add);
931
- if (success) {
932
- WS_DEBUG_PRINTLN (" [SD] UART component parsed successfully, adding to shared buffer.." );
933
- msg_signal_b2d.which_payload = wippersnapper_signal_BrokerToDevice_uart_add_tag;
934
- msg_signal_b2d.payload .uart_add = msg_uart_add;
935
- }
1013
+ } else if (strcmp (component_api_type, " uart" ) == 0 ) {
1014
+ WS_DEBUG_PRINTLN (" [SD] UART component found in cfg" );
1015
+ wippersnapper_uart_UartAdd msg_uart_add =
1016
+ wippersnapper_uart_UartAdd_init_default;
1017
+ success = ParseUartAdd (component, msg_uart_add);
1018
+ if (success) {
1019
+ WS_DEBUG_PRINTLN (" [SD] UART component parsed successfully, adding to "
1020
+ " shared buffer.." );
1021
+ msg_signal_b2d.which_payload =
1022
+ wippersnapper_signal_BrokerToDevice_uart_add_tag;
1023
+ msg_signal_b2d.payload .uart_add = msg_uart_add;
1024
+ }
936
1025
} else {
937
1026
WS_DEBUG_PRINTLN (" [SD] Error: Unknown Component API: " +
938
1027
String (component_api_type));
0 commit comments