Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
dzurikmiroslav committed Nov 22, 2024
1 parent d88dee5 commit 5d072c4
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 546 deletions.
150 changes: 7 additions & 143 deletions components/config/src/board_config.c
Original file line number Diff line number Diff line change
@@ -1,160 +1,24 @@
#include "board_config.h"

#include <ctype.h>
#include <esp_err.h>
#include <esp_log.h>
#include <esp_system.h>
#include <string.h>

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);
}
32 changes: 16 additions & 16 deletions components/config/src/board_config_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -278,20 +278,20 @@ 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;
yaml_mark_t key_mark;

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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion components/config/src/board_config_parser.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef BOARD_CONFIG_PARSER_H_
#define BOARD_CONFIG_PARSER_H_

#include <esp_err.h>
#include <stdio.h>

#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_ */
Loading

0 comments on commit 5d072c4

Please sign in to comment.