diff --git a/components/config/src/board_config.c b/components/config/src/board_config.c index b5e1cda..1116980 100644 --- a/components/config/src/board_config.c +++ b/components/config/src/board_config.c @@ -1,160 +1,24 @@ #include "board_config.h" -#include #include #include -#include -#include -static const char* TAG = "config"; +#include "board_config_parser.h" -board_cfg_t board_config; - -#define SET_CONFIG_VALUE(name, prop, convert_fn) \ - if (!strcmp(key, name)) { \ - config.prop = convert_fn(value); \ - continue; \ - } +// static const char* TAG = "board_config"; -#define SET_CONFIG_VALUE_STR(name, prop) \ - if (!strcmp(key, name)) { \ - strcpy(config.prop, value); \ - continue; \ - } +board_cfg_t board_config; void board_config_load(void) { - memset(&board_config, 0, sizeof(board_cfg_t)); - - FILE* file = fopen("/cfg/board.cfg", "r"); + FILE* file = fopen("/cfg/board.yaml", "r"); if (file == NULL) { ESP_ERROR_CHECK(ESP_ERR_NOT_FOUND); } - char buffer[256]; - - while (fgets(buffer, 256, file)) { - int buf_length = strlen(buffer); - int buf_start = 0; - while (buf_start < buf_length && isspace((unsigned char)buffer[buf_start])) { - buf_start++; - } - int buf_end = buf_length; - while (buf_end > 0 && !isgraph((unsigned char)buffer[buf_end - 1])) { - buf_end--; - } - - buffer[buf_end] = '\0'; - char* line = &buffer[buf_start]; - - if (line[0] != '#') { - char* saveptr; - char* key = strtok_r(line, "=", &saveptr); - if (key != NULL) { - char* value = strtok_r(NULL, "=", &saveptr); - if (value != NULL) { - // SET_CONFIG_VALUE_STR("DEVICE_NAME", device_name); - // // SET_CONFIG_VALUE("LED_CHARGING", led_charging, atob); - // SET_CONFIG_VALUE("LED_CHARGING_GPIO", led_charging_gpio, atoi); - // // SET_CONFIG_VALUE("LED_ERROR", led_error, atob); - // SET_CONFIG_VALUE("LED_ERROR_GPIO", led_error_gpio, atoi); - // // SET_CONFIG_VALUE("LED_WIFI", led_wifi, atob); - // SET_CONFIG_VALUE("LED_WIFI_GPIO", led_wifi_gpio, atoi); - // SET_CONFIG_VALUE("BUTTON_WIFI_GPIO", button_gpio, atoi); - // SET_CONFIG_VALUE("PILOT_PWM_GPIO", pilot_gpio, atoi); - // SET_CONFIG_VALUE("PILOT_ADC_CHANNEL", pilot_adc_channel, atoi); - // SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_12", pilot_down_threshold_12, atoi); - // SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_9", pilot_down_threshold_9, atoi); - // SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_6", pilot_down_threshold_6, atoi); - // SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_3", pilot_down_threshold_3, atoi); - // SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_N12", pilot_down_threshold_n12, atoi); - // SET_CONFIG_VALUE("PROXIMITY", proximity, atob); - // SET_CONFIG_VALUE("PROXIMITY_ADC_CHANNEL", proximity_adc_channel, atoi); - // SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_13", proximity_down_threshold_13, atoi); - // SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_20", proximity_down_threshold_20, atoi); - // SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_32", proximity_down_threshold_32, atoi); - // SET_CONFIG_VALUE("AC_RELAY_GPIO", ac_relay_gpio, atoi); - // SET_CONFIG_VALUE("SOCKET_LOCK", socket_lock, atob); - // SET_CONFIG_VALUE("SOCKET_LOCK_A_GPIO", socket_lock_a_gpio, atoi); - // SET_CONFIG_VALUE("SOCKET_LOCK_B_GPIO", socket_lock_b_gpio, atoi); - // SET_CONFIG_VALUE("SOCKET_LOCK_DETECTION_GPIO", socket_lock_detection_gpio, atoi); - // SET_CONFIG_VALUE("SOCKET_LOCK_DETECTION_DELAY", socket_lock_detection_delay, atoi); - // SET_CONFIG_VALUE("SOCKET_LOCK_MIN_BREAK_TIME", socket_lock_min_break_time, atoi); - // SET_CONFIG_VALUE("RCM", rcm, atob); - // SET_CONFIG_VALUE("RCM_GPIO", rcm_gpio, atoi); - // SET_CONFIG_VALUE("RCM_TEST_GPIO", rcm_test_gpio, atoi); - // SET_CONFIG_VALUE("ENERGY_METER", energy_meter, atoem); - // SET_CONFIG_VALUE("ENERGY_METER_THREE_PHASES", energy_meter_three_phases, atob); - // SET_CONFIG_VALUE("ENERGY_METER_L1_CUR_ADC_CHANNEL", energy_meter_l1_cur_adc_channel, atoi); - // SET_CONFIG_VALUE("ENERGY_METER_L2_CUR_ADC_CHANNEL", energy_meter_l2_cur_adc_channel, atoi); - // SET_CONFIG_VALUE("ENERGY_METER_L3_CUR_ADC_CHANNEL", energy_meter_l3_cur_adc_channel, atoi); - // SET_CONFIG_VALUE("ENERGY_METER_CUR_SCALE", energy_meter_cur_scale, atoff); - // SET_CONFIG_VALUE("ENERGY_METER_L1_VLT_ADC_CHANNEL", energy_meter_l1_vlt_adc_channel, atoi); - // SET_CONFIG_VALUE("ENERGY_METER_L2_VLT_ADC_CHANNEL", energy_meter_l2_vlt_adc_channel, atoi); - // SET_CONFIG_VALUE("ENERGY_METER_L3_VLT_ADC_CHANNEL", energy_meter_l3_vlt_adc_channel, atoi); - // SET_CONFIG_VALUE("ENERGY_METER_VLT_SCALE", energy_meter_vlt_scale, atoff); - // SET_CONFIG_VALUE("AUX_IN_1", aux_in_1, atob); - // SET_CONFIG_VALUE_STR("AUX_IN_1_NAME", aux_in_1_name); - // SET_CONFIG_VALUE("AUX_IN_1_GPIO", aux_in_1_gpio, atoi); - // SET_CONFIG_VALUE("AUX_IN_2", aux_in_2, atob); - // SET_CONFIG_VALUE_STR("AUX_IN_2_NAME", aux_in_2_name); - // SET_CONFIG_VALUE("AUX_IN_2_GPIO", aux_in_2_gpio, atoi); - // SET_CONFIG_VALUE("AUX_IN_3", aux_in_3, atob); - // SET_CONFIG_VALUE_STR("AUX_IN_3_NAME", aux_in_3_name); - // SET_CONFIG_VALUE("AUX_IN_3_GPIO", aux_in_3_gpio, atoi); - // SET_CONFIG_VALUE("AUX_IN_4", aux_in_4, atob); - // SET_CONFIG_VALUE_STR("AUX_IN_4_NAME", aux_in_4_name); - // SET_CONFIG_VALUE("AUX_IN_4_GPIO", aux_in_4_gpio, atoi); - // SET_CONFIG_VALUE("AUX_OUT_1", aux_out_1, atob); - // SET_CONFIG_VALUE_STR("AUX_OUT_1_NAME", aux_out_1_name); - // SET_CONFIG_VALUE("AUX_OUT_1_GPIO", aux_out_1_gpio, atoi); - // SET_CONFIG_VALUE("AUX_OUT_2", aux_out_2, atob); - // SET_CONFIG_VALUE_STR("AUX_OUT_2_NAME", aux_out_2_name); - // SET_CONFIG_VALUE("AUX_OUT_2_GPIO", aux_out_2_gpio, atoi); - // SET_CONFIG_VALUE("AUX_OUT_3", aux_out_3, atob); - // SET_CONFIG_VALUE_STR("AUX_OUT_3_NAME", aux_out_3_name); - // SET_CONFIG_VALUE("AUX_OUT_3_GPIO", aux_out_3_gpio, atoi); - // SET_CONFIG_VALUE("AUX_OUT_4", aux_out_4, atob); - // SET_CONFIG_VALUE_STR("AUX_OUT_4_NAME", aux_out_4_name); - // SET_CONFIG_VALUE("AUX_OUT_4_GPIO", aux_out_4_gpio, atoi); - // SET_CONFIG_VALUE("AUX_AIN_1", aux_ain_1, atob); - // SET_CONFIG_VALUE_STR("AUX_AIN_1_NAME", aux_ain_1_name); - // SET_CONFIG_VALUE("AUX_AIN_1_ADC_CHANNEL", aux_ain_1_adc_channel, atoi); - // SET_CONFIG_VALUE("AUX_AIN_2", aux_ain_2, atob); - // SET_CONFIG_VALUE_STR("AUX_AIN_2_NAME", aux_ain_2_name); - // SET_CONFIG_VALUE("AUX_AIN_2_ADC_CHANNEL", aux_ain_2_adc_channel, atoi); - // #if CONFIG_ESP_CONSOLE_NONE || CONFIG_ESP_CONSOLE_UART_NUM != 0 - // SET_CONFIG_VALUE("SERIAL_1", serial_1, atoser); - // SET_CONFIG_VALUE_STR("SERIAL_1_NAME", serial_1_name); - // SET_CONFIG_VALUE("SERIAL_1_RXD_GPIO", serial_1_rxd_gpio, atoi); - // SET_CONFIG_VALUE("SERIAL_1_TXD_GPIO", serial_1_txd_gpio, atoi); - // SET_CONFIG_VALUE("SERIAL_1_RTS_GPIO", serial_1_rts_gpio, atoi); - // #endif /* CONFIG_ESP_CONSOLE_NONE || CONFIG_ESP_CONSOLE_UART_NUM != 0 */ - // #if CONFIG_ESP_CONSOLE_NONE || CONFIG_ESP_CONSOLE_UART_NUM != 1 - // SET_CONFIG_VALUE("SERIAL_2", serial_2, atoser); - // SET_CONFIG_VALUE_STR("SERIAL_2_NAME", serial_2_name); - // SET_CONFIG_VALUE("SERIAL_2_RXD_GPIO", serial_2_rxd_gpio, atoi); - // SET_CONFIG_VALUE("SERIAL_2_TXD_GPIO", serial_2_txd_gpio, atoi); - // SET_CONFIG_VALUE("SERIAL_2_RTS_GPIO", serial_2_rts_gpio, atoi); - // #endif /* CONFIG_ESP_CONSOLE_NONE || CONFIG_ESP_CONSOLE_UART_NUM != 1 */ - // #if SOC_UART_NUM > 2 - // #if CONFIG_ESP_CONSOLE_NONE || CONFIG_ESP_CONSOLE_UART_NUM != 2 - // SET_CONFIG_VALUE("SERIAL_3", serial_3, atoser); - // SET_CONFIG_VALUE_STR("SERIAL_3_NAME", serial_3_name); - // SET_CONFIG_VALUE("SERIAL_3_RXD_GPIO", serial_3_rxd_gpio, atoi); - // SET_CONFIG_VALUE("SERIAL_3_TXD_GPIO", serial_3_txd_gpio, atoi); - // SET_CONFIG_VALUE("SERIAL_3_RTS_GPIO", serial_3_rts_gpio, atoi); - // #endif /* CONFIG_ESP_CONSOLE_NONE || CONFIG_ESP_CONSOLE_UART_NUM != 2 */ - // #endif /* SOC_UART_NUM > 2 */ - // SET_CONFIG_VALUE("ONEWIRE", onewire, atob); - // SET_CONFIG_VALUE("ONEWIRE_GPIO", onewire_gpio, atoi); - // SET_CONFIG_VALUE("ONEWIRE_TEMP_SENSOR", onewire_temp_sensor, atob); - - ESP_LOGE(TAG, "Unknown config value %s=%s", key, value); - } - } - } - } + esp_err_t ret = board_config_parse_file(file, &board_config); fclose(file); + + ESP_ERROR_CHECK(ret); } diff --git a/components/config/src/board_config_parser.c b/components/config/src/board_config_parser.c index 6917aab..7c72116 100644 --- a/components/config/src/board_config_parser.c +++ b/components/config/src/board_config_parser.c @@ -9,25 +9,25 @@ #define CASE_SET_VALUE(level, prop, convert_fn) \ case level: \ - config->prop = convert_fn(value); \ + config->prop = convert_fn(value); \ break; -#define CASE_SET_VALUE_STR(level, prop, len) \ - case level: \ - strncpy(config->prop, value, len); \ +#define CASE_SET_VALUE_STR(level, prop, len) \ + case level: \ + strncpy(config->prop, value, len); \ break; #define CASE_SET_SEQ_VALUE(level, prop, convert_fn, condition) \ case level: \ - if (condition) config->prop = convert_fn(value); \ + if (condition) config->prop = convert_fn(value); \ break; -#define CASE_SET_SEQ_VALUE_STR(level, prop, len, condition) \ - case level: \ - if (condition) strncpy(config->prop, value, len); \ +#define CASE_SET_SEQ_VALUE_STR(level, prop, len, condition) \ + case level: \ + if (condition) strncpy(config->prop, value, len); \ break; -static const char* TAG = "config_parser"; +static const char* TAG = "board_config_parser"; static uint8_t str_to_gpio(const char* value) { @@ -278,7 +278,7 @@ static void empty_config(board_cfg_t* config) } } -bool board_cfg_parse_file(FILE* src, board_cfg_t* config) +esp_err_t board_config_parse_file(FILE* src, board_cfg_t* board_cfg) { yaml_parser_t parser; yaml_event_t event; @@ -286,12 +286,12 @@ bool board_cfg_parse_file(FILE* src, board_cfg_t* config) if (!yaml_parser_initialize(&parser)) { ESP_LOGE(TAG, "Cant initialize yaml parser"); - return false; + return ESP_ERR_INVALID_STATE; } yaml_parser_set_input_file(&parser, src); - empty_config(config); + empty_config(board_cfg); int level = -1; uint8_t seq_level = 0; @@ -321,7 +321,7 @@ bool board_cfg_parse_file(FILE* src, board_cfg_t* config) } } else { if (key[level] != KEY_INVALID) { - if (!set_key_value(config, key, seq_idx, value)) { + if (!set_key_value(board_cfg, key, seq_idx, value)) { ESP_LOGW(TAG, "Unknow property: %s (line: %zu column: %zu)", keys[key[level]], key_mark.line, key_mark.column); } } @@ -369,12 +369,12 @@ bool board_cfg_parse_file(FILE* src, board_cfg_t* config) yaml_parser_delete(&parser); - return true; + return ESP_OK; error: - ESP_LOGE(TAG, "Error read"); + ESP_LOGE(TAG, "Parsing error"); yaml_event_delete(&event); yaml_parser_delete(&parser); - return false; + return ESP_FAIL; } diff --git a/components/config/src/board_config_parser.h b/components/config/src/board_config_parser.h index c975e8a..a33a2c8 100644 --- a/components/config/src/board_config_parser.h +++ b/components/config/src/board_config_parser.h @@ -1,10 +1,11 @@ #ifndef BOARD_CONFIG_PARSER_H_ #define BOARD_CONFIG_PARSER_H_ +#include #include #include "board_config.h" -bool board_cfg_parse_file(FILE* src, board_cfg_t* board_cfg); +esp_err_t board_config_parse_file(FILE* src, board_cfg_t* board_cfg); #endif /* BOARD_CONFIG_PARSER_H_ */ diff --git a/components/config/src/config_parser.c b/components/config/src/config_parser.c deleted file mode 100644 index 6917aab..0000000 --- a/components/config/src/config_parser.c +++ /dev/null @@ -1,380 +0,0 @@ - -#include "board_config_parser.h" - -#include - -#include "yaml.h" - -#define YAML_MAX_LEVEL 2 - -#define CASE_SET_VALUE(level, prop, convert_fn) \ - case level: \ - config->prop = convert_fn(value); \ - break; - -#define CASE_SET_VALUE_STR(level, prop, len) \ - case level: \ - strncpy(config->prop, value, len); \ - break; - -#define CASE_SET_SEQ_VALUE(level, prop, convert_fn, condition) \ - case level: \ - if (condition) config->prop = convert_fn(value); \ - break; - -#define CASE_SET_SEQ_VALUE_STR(level, prop, len, condition) \ - case level: \ - if (condition) strncpy(config->prop, value, len); \ - break; - -static const char* TAG = "config_parser"; - -static uint8_t str_to_gpio(const char* value) -{ - if (strlen(value)) { - return atoi(value); - } else { - return -1; - } -} - -static bool str_to_bool(const char* value) -{ - return !strcmp(value, "true"); -} - -static board_cfg_serial_type_t str_to_serial_type(const char* value) -{ - if (!strcmp(value, "uart")) { - return BOARD_CFG_SERIAL_TYPE_UART; - } - if (!strcmp(value, "rs485")) { - return BOARD_CFG_SERIAL_TYPE_RS485; - } - return BOARD_CFG_SERIAL_TYPE_NONE; -} - -typedef enum { - KEY_INVALID = -2, - KEY_NONE = -1, - // - KEY_DEVICE_NAME, - KEY_LED_CHARGING_GPIO, - KEY_LED_ERROR_GPIO, - KEY_LED_WIFI_GPIO, - KEY_BUTTON_GPIO, - KEY_AC_RELAY_GPIO, - KEY_PILOT, - KEY_PROXIMITY, - KEY_SOCKET_LOCK, - KEY_RCM, - KEY_AUX_IN, - KEY_AUX_OUT, - KEY_AUX_ANALOG_IN, - KEY_GPIO, - KEY_NAME, - KEY_ADC_CHANNEL, - KEY_LEVELS, - KEY_A_GPIO, - KEY_B_GPIO, - KEY_DETECTION_GPIO, - KEY_DETECTION_DELAY, - KEY_MIN_BREAK_TIME, - KEY_TEST_GPIO, - KEY_ENERGY_METER, - KEY_CURRENT_ADC_CHANNELS, - KEY_VOLTAGE_ADC_CHANNELS, - KEY_CURRENT_SCALE, - KEY_VOLTAGE_SCALE, - KEY_SERIAL, - KEY_TYPE, - KEY_RXD_GPIO, - KEY_TXD_GPIO, - KEY_RTS_GPIO, - KEY_ONEWIRE, - KEY_TEMPERATURE_SENSOR, - // - KEY_MAX, -} key_t; - -// string values of key_t, must match key_t order -static const char* keys[] = { - "deviceName", - "ledChargingGpio", - "ledErrorGpio", - "ledWifiGpio", - "buttonGpio", - "acRelayGpio", - "pilot", - "proximity", - "socketLock", - "rcm", - "auxIn", - "auxOut", - "auxAnalogIn", - "gpio", - "name", - "adcChannel", - "levels", - "aGpio", - "bGpio", - "detectionGpio", - "detectionDelay", - "minBreakTime", - "testGpio", - "energyMeter", - "currentAdcChannels", - "voltageAdcChannels", - "currentScale", - "voltageScale", - "serial", - "type", - "rxdGpio", - "txdGpio", - "rtsGpio", - "onewire", - "temperatureSensor", -}; - -static key_t get_key(const char* key) -{ - for (key_t i = KEY_NONE + 1; i < KEY_MAX; i++) { - if (!strcmp(key, keys[i])) return i; - } - return KEY_NONE; -} - -static bool set_key_value(board_cfg_t* config, const key_t* key, const int* seq_idx, const char* value) -{ - switch (key[0]) { - CASE_SET_VALUE_STR(KEY_DEVICE_NAME, device_name, BOARD_CFG_DEVICE_NAME_LEN); - CASE_SET_VALUE(KEY_LED_CHARGING_GPIO, led_charging_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_LED_ERROR_GPIO, led_error_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_LED_WIFI_GPIO, led_wifi_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_BUTTON_GPIO, button_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_AC_RELAY_GPIO, ac_relay_gpio, str_to_gpio); - case KEY_PILOT: - switch (key[1]) { - CASE_SET_VALUE(KEY_GPIO, pilot_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_ADC_CHANNEL, pilot_adc_channel, str_to_gpio); - CASE_SET_SEQ_VALUE(KEY_LEVELS, pilot_levels[seq_idx[1]], atoi, seq_idx[1] < BOARD_CFG_PILOT_LEVEL_MAX); - default: - return false; - } - break; - case KEY_PROXIMITY: - switch (key[1]) { - CASE_SET_VALUE(KEY_ADC_CHANNEL, proximity_adc_channel, str_to_gpio); - CASE_SET_SEQ_VALUE(KEY_LEVELS, proximity_levels[seq_idx[1]], atoi, seq_idx[1] < BOARD_CFG_PROXIMITY_LEVEL_MAX); - default: - return false; - } - break; - case KEY_SOCKET_LOCK: - switch (key[1]) { - CASE_SET_VALUE(KEY_A_GPIO, socket_lock_a_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_B_GPIO, socket_lock_b_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_DETECTION_GPIO, socket_lock_detection_gpio, atoi); - CASE_SET_VALUE(KEY_DETECTION_DELAY, socket_lock_detection_delay, atoi); - CASE_SET_VALUE(KEY_MIN_BREAK_TIME, socket_lock_min_break_time, atoi); - default: - return false; - } - break; - case KEY_ENERGY_METER: - switch (key[1]) { - CASE_SET_SEQ_VALUE(KEY_CURRENT_ADC_CHANNELS, energy_meter_cur_adc_channel[seq_idx[1]], str_to_gpio, seq_idx[1] < BOARD_CFG_ENERGY_METER_ADC_CHANNEL_MAX); - CASE_SET_SEQ_VALUE(KEY_VOLTAGE_ADC_CHANNELS, energy_meter_vlt_adc_channel[seq_idx[1]], str_to_gpio, seq_idx[1] < BOARD_CFG_ENERGY_METER_ADC_CHANNEL_MAX); - CASE_SET_VALUE(KEY_CURRENT_SCALE, energy_meter_cur_scale, atof); - CASE_SET_VALUE(KEY_VOLTAGE_SCALE, energy_meter_vlt_scale, atof); - default: - return false; - } - break; - case KEY_RCM: - switch (key[1]) { - CASE_SET_VALUE(KEY_GPIO, rcm_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_TEST_GPIO, rcm_test_gpio, str_to_gpio); - default: - return false; - } - break; - case KEY_AUX_IN: - switch (key[1]) { - CASE_SET_SEQ_VALUE_STR(KEY_NAME, aux_in[seq_idx[0]].name, BOARD_CFG_AUX_NAME_LEN, seq_idx[0] < BOARD_CFG_AUX_IN_COUNT); - CASE_SET_SEQ_VALUE(KEY_GPIO, aux_in[seq_idx[0]].gpio, str_to_gpio, seq_idx[0] < BOARD_CFG_AUX_IN_COUNT); - default: - return false; - } - break; - case KEY_AUX_OUT: - switch (key[1]) { - CASE_SET_SEQ_VALUE_STR(KEY_NAME, aux_out[seq_idx[0]].name, BOARD_CFG_AUX_NAME_LEN, seq_idx[0] < BOARD_CFG_AUX_OUT_COUNT); - CASE_SET_SEQ_VALUE(KEY_GPIO, aux_out[seq_idx[0]].gpio, str_to_gpio, seq_idx[0] < BOARD_CFG_AUX_OUT_COUNT); - default: - return false; - } - break; - case KEY_AUX_ANALOG_IN: - switch (key[1]) { - CASE_SET_SEQ_VALUE_STR(KEY_NAME, aux_analog_in[seq_idx[0]].name, BOARD_CFG_AUX_NAME_LEN, seq_idx[0] < BOARD_CFG_AUX_ANALOG_IN_COUNT); - CASE_SET_SEQ_VALUE(KEY_ADC_CHANNEL, aux_analog_in[seq_idx[0]].adc_channel, str_to_gpio, seq_idx[0] < BOARD_CFG_AUX_ANALOG_IN_COUNT); - default: - return false; - } - break; - case KEY_SERIAL: - switch (key[1]) { - CASE_SET_SEQ_VALUE(KEY_TYPE, serial[seq_idx[0]].type, str_to_serial_type, seq_idx[0] < BOARD_CFG_SERIAL_COUNT); - CASE_SET_SEQ_VALUE_STR(KEY_NAME, serial[seq_idx[0]].name, BOARD_CFG_SERIAL_NAME_LEN, seq_idx[0] < BOARD_CFG_SERIAL_COUNT); - CASE_SET_SEQ_VALUE(KEY_RXD_GPIO, serial[seq_idx[0]].rxd_gpio, str_to_gpio, seq_idx[0] < BOARD_CFG_SERIAL_COUNT); - CASE_SET_SEQ_VALUE(KEY_TXD_GPIO, serial[seq_idx[0]].txd_gpio, str_to_gpio, seq_idx[0] < BOARD_CFG_SERIAL_COUNT); - CASE_SET_SEQ_VALUE(KEY_RTS_GPIO, serial[seq_idx[0]].rts_gpio, str_to_gpio, seq_idx[0] < BOARD_CFG_SERIAL_COUNT); - default: - return false; - } - break; - case KEY_ONEWIRE: - switch (key[1]) { - CASE_SET_VALUE(KEY_GPIO, onewire_gpio, str_to_gpio); - CASE_SET_VALUE(KEY_TEMPERATURE_SENSOR, onewire_temp_sensor, str_to_bool); - default: - return false; - } - break; - default: - return false; - } - return true; -} - -static void empty_config(board_cfg_t* config) -{ - memset(config, 0, sizeof(board_cfg_t)); - config->led_charging_gpio = -1; - config->led_error_gpio = -1; - config->led_wifi_gpio = -1; - config->button_gpio = -1; - config->pilot_gpio = -1; - config->pilot_adc_channel = -1; - config->proximity_adc_channel = -1; - for (uint8_t i = 0; i < BOARD_CFG_ENERGY_METER_ADC_CHANNEL_MAX; i++) { - config->energy_meter_cur_adc_channel[i] = -1; - config->energy_meter_vlt_adc_channel[i] = -1; - } - for (uint8_t i = 0; i < BOARD_CFG_AUX_IN_COUNT; i++) { - config->aux_in[i].gpio = -1; - } - for (uint8_t i = 0; i < BOARD_CFG_AUX_OUT_COUNT; i++) { - config->aux_out[i].gpio = -1; - } - for (uint8_t i = 0; i < BOARD_CFG_AUX_ANALOG_IN_COUNT; i++) { - config->aux_analog_in[i].adc_channel = -1; - } - for (uint8_t i = 0; i < BOARD_CFG_SERIAL_COUNT; i++) { - config->serial[i].rxd_gpio = -1; - config->serial[i].txd_gpio = -1; - config->serial[i].rts_gpio = -1; - } -} - -bool board_cfg_parse_file(FILE* src, board_cfg_t* config) -{ - yaml_parser_t parser; - yaml_event_t event; - yaml_mark_t key_mark; - - if (!yaml_parser_initialize(&parser)) { - ESP_LOGE(TAG, "Cant initialize yaml parser"); - return false; - } - - yaml_parser_set_input_file(&parser, src); - - empty_config(config); - - int level = -1; - uint8_t seq_level = 0; - key_t key[YAML_MAX_LEVEL]; - int seq_idx[YAML_MAX_LEVEL] = { 0 }; - for (int i = 0; i < YAML_MAX_LEVEL; i++) { - key[i] = KEY_NONE; - } - bool done = false; - - while (!done) { - if (!yaml_parser_parse(&parser, &event)) { - ESP_LOGE(TAG, "Parsing error: %s (line: %zu column: %zu)", parser.problem, parser.problem_mark.line, parser.problem_mark.column); - goto error; - } - - switch (event.type) { - case YAML_SCALAR_EVENT: - if (level < YAML_MAX_LEVEL) { - const char* value = (char*)event.data.scalar.value; - if (key[level] == KEY_NONE) { - key[level] = get_key(value); - key_mark = parser.mark; - if (key[level] == KEY_NONE) { - key[level] = KEY_INVALID; - ESP_LOGW(TAG, "Unknow property: %s (line: %zu column: %zu)", value, event.start_mark.line, event.start_mark.column); - } - } else { - if (key[level] != KEY_INVALID) { - if (!set_key_value(config, key, seq_idx, value)) { - ESP_LOGW(TAG, "Unknow property: %s (line: %zu column: %zu)", keys[key[level]], key_mark.line, key_mark.column); - } - } - if (seq_level & (1 << level)) { - seq_idx[level]++; // for array of primitives - } else { - key[level] = KEY_NONE; - } - } - } - break; - case YAML_SEQUENCE_START_EVENT: - if (level < YAML_MAX_LEVEL) { - seq_level |= (1 << level); - } - break; - case YAML_SEQUENCE_END_EVENT: - if (level < YAML_MAX_LEVEL) { - seq_level &= ~(1 << level); - key[level] = KEY_NONE; // for array of primitives - seq_idx[level] = 0; - } - break; - case YAML_MAPPING_START_EVENT: - level++; - break; - case YAML_MAPPING_END_EVENT: - if (level < YAML_MAX_LEVEL) key[level] = KEY_NONE; - level--; - if (level < YAML_MAX_LEVEL) { - if (seq_level & (1 << level)) - seq_idx[level]++; // for array of objects, key[level] clear YAML_SEQUENCE_END_EVENT - else - key[level] = KEY_NONE; - } - break; - case YAML_STREAM_END_EVENT: - done = true; - break; - default: - break; - } - yaml_event_delete(&event); - } - - yaml_parser_delete(&parser); - - return true; - -error: - ESP_LOGE(TAG, "Error read"); - yaml_event_delete(&event); - yaml_parser_delete(&parser); - - return false; -} diff --git a/components/protocols/src/http_json.c b/components/protocols/src/http_json.c index b4ff1da..e82b054 100644 --- a/components/protocols/src/http_json.c +++ b/components/protocols/src/http_json.c @@ -630,7 +630,7 @@ cJSON* http_json_get_board_config(void) cJSON_AddNumberToObject(json, "socketLockMinBreakTime", board_config.socket_lock_min_break_time); cJSON_AddBoolToObject(json, "rcm", board_cfg_is_rcm(board_config)); cJSON_AddBoolToObject(json, "temperatureSensor", board_cfg_is_onewire(board_config) && board_config.onewire_temp_sensor); - + const char* energy_meter = "none"; bool energy_meter_three_phases = false; if (board_cfg_is_energy_meter_cur(board_config)) { @@ -678,7 +678,7 @@ cJSON* http_json_get_board_config(void) cJSON_AddItemToArray(aux_json, cJSON_CreateString(board_config.aux_analog_in[i].name)); } } - cJSON_AddItemToObject(json, "auxAin", aux_json); + cJSON_AddItemToObject(json, "auxAnalogIn", aux_json); return json; } diff --git a/components/script/src/l_board_config_lib.c b/components/script/src/l_board_config_lib.c index d643da1..ff51b5c 100644 --- a/components/script/src/l_board_config_lib.c +++ b/components/script/src/l_board_config_lib.c @@ -57,7 +57,7 @@ int luaopen_board_config(lua_State* L) energy_meter = ENERGY_METER_CUR; energy_meter_three_phases = board_cfg_is_energy_meter_vlt_3p(board_config); } - } + } lua_pushinteger(L, energy_meter); lua_setfield(L, -2, "energymeter"); @@ -100,7 +100,7 @@ int luaopen_board_config(lua_State* L) lua_rawseti(L, -2, i + 1); } } - lua_setfield(L, -2, "auxain"); + lua_setfield(L, -2, "auxanalogin"); return 1; } \ No newline at end of file diff --git a/test_app/main/test_config.c b/test_app/main/test_config.c index 37d9f57..c590fae 100644 --- a/test_app/main/test_config.c +++ b/test_app/main/test_config.c @@ -24,10 +24,10 @@ TEST(config, board) config_file = fopen(BOARD_YAML, "r"); board_cfg_t config; - bool ret = board_cfg_parse_file(config_file, &config); + esp_err_t ret = board_config_parse_file(config_file, &config); fclose(config_file); - TEST_ASSERT_TRUE(ret); + TEST_ASSERT_EQUAL(ret, ESP_OK); TEST_ASSERT_EQUAL_STRING("test", config.device_name); TEST_ASSERT_EQUAL(1, config.led_charging_gpio);