From c76a9669d819e1aaae63cca1c61dc97297e4dc1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0=20=D0=93=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D1=83=D1=88=D0=BA=D0=BE?= Date: Sat, 16 Dec 2023 12:51:13 +0300 Subject: [PATCH] refactoring --- main/can.c | 23 +++++++-------- main/can.h | 4 +-- main/cmd_can.c | 74 ++++++++++++++++++++++------------------------- main/cmd_system.c | 7 ++--- main/cmd_utils.c | 4 +-- main/console.c | 16 ++++------ main/fs.c | 2 +- main/xvprintf.c | 3 +- 8 files changed, 57 insertions(+), 76 deletions(-) diff --git a/main/can.c b/main/can.c index c3579aa..ca2d81e 100644 --- a/main/can.c +++ b/main/can.c @@ -5,7 +5,6 @@ #include "freertos/projdefs.h" #include "hal/twai_types.h" #include "sdkconfig.h" -#include #include #include #include "freertos/ringbuf.h" @@ -23,9 +22,9 @@ SemaphoreHandle_t can_mutex; volatile can_status_t curr_can_state = { 0 }; static can_status_t get_can_state() { - can_status_t result; - twai_status_info_t status = { 0 }; - esp_err_t res = twai_get_status_info(&status); + can_status_t result = { 0 }; + twai_status_info_t status = {0}; + const esp_err_t res = twai_get_status_info(&status); if (res != ESP_OK) { result.state = CAN_NOT_INSTALLED; return result; @@ -57,8 +56,7 @@ static can_status_t get_can_state() { return result; } -void can_msg_to_str(twai_message_t *can_msg, char *start_str, char *out_str) { - char byte_str[3]; +void can_msg_to_str(const twai_message_t *can_msg, char *start_str, char *out_str) { out_str[0] = '\0'; sprintf(out_str, "%scan frame: ID: %08X dlc: %d ", start_str, (int) can_msg->identifier, can_msg->data_length_code); if (can_msg->data_length_code == 0) { @@ -66,17 +64,17 @@ void can_msg_to_str(twai_message_t *can_msg, char *start_str, char *out_str) { } else { strcat(out_str, "data: "); for (int i = 0; i < can_msg->data_length_code; i++) { + char byte_str[3]; sprintf(byte_str, "%02X", can_msg->data[i]); strcat(out_str, byte_str); } } } -bool matches_filters(twai_message_t *msg) { - List *tmp_cursor = adv_filters.filters; +bool matches_filters(const twai_message_t *msg) { + const List *tmp_cursor = adv_filters.filters; while (tmp_cursor != NULL) { - smart_filt_element_t *curr_filter; - curr_filter = (smart_filt_element_t *) tmp_cursor->data; + const smart_filt_element_t* curr_filter = tmp_cursor->data; if ((msg->identifier & curr_filter->mask) == curr_filter->filt) { return true; } @@ -89,10 +87,8 @@ void can_task(void* arg) { static const TickType_t can_task_timeout = pdMS_TO_TICKS(200); uint32_t alerts = 0; esp_err_t ret = ESP_OK; - BaseType_t sem_res; can_mutex = xSemaphoreCreateMutex(); twai_message_t rx_msg; - char data_bytes_str[70]; for (;;) { // A Task shall never return or exit. if (twai_read_alerts(&alerts, 0) == ESP_OK) { if (alerts & TWAI_ALERT_ERR_ACTIVE) { @@ -121,9 +117,10 @@ void can_task(void* arg) { } } curr_can_state = get_can_state(); - sem_res = xSemaphoreTake(can_mutex, 0); + const BaseType_t sem_res = xSemaphoreTake(can_mutex, 0); if (sem_res == pdTRUE) { while ((ret = twai_receive(&rx_msg, can_task_timeout)) == ESP_OK) { + char data_bytes_str[70]; if (adv_filters.sw_filtering) { if (!matches_filters(&rx_msg)) continue; } diff --git a/main/can.h b/main/can.h index 3dd20b2..e55403a 100644 --- a/main/can.h +++ b/main/can.h @@ -3,9 +3,7 @@ #include "driver/twai.h" #include "hal/twai_types.h" -#include "sdkconfig.h" #include "freertos/semphr.h" -#include #include typedef enum { @@ -50,6 +48,6 @@ extern adv_filt_t adv_filters; // functions void can_task(void* arg); -void can_msg_to_str(twai_message_t *can_msg, char *start_str, char *out_str); +void can_msg_to_str(const twai_message_t *can_msg, char *start_str, char *out_str); #endif // MAIN_CAN_H diff --git a/main/cmd_can.c b/main/cmd_can.c index dc60b20..906bfca 100644 --- a/main/cmd_can.c +++ b/main/cmd_can.c @@ -45,28 +45,29 @@ static struct { static int send_can_frame(int argc, char **argv) { twai_message_t msg = { 0 }; char printf_str[70]; - int nerrors = arg_parse(argc, argv, (void **) &cansend_args); + const int nerrors = arg_parse(argc, argv, (void **) &cansend_args); if (nerrors != 0) { arg_print_errors(stderr, cansend_args.end, argv[0]); return 1; } const char *can_msg_ptr = cansend_args.message->sval[0]; - char *can_msg_str_buf = strdup(can_msg_ptr); - char *id_substr = strtok(can_msg_str_buf, "#"); - char *data_substr = strtok(NULL, "#"); + char* can_msg_str_buf = strdup(can_msg_ptr); + const char* id_substr = strtok(can_msg_str_buf, "#"); + const char *data_substr = strtok(NULL, "#"); if ((id_substr == NULL) || (strtok(NULL, "#") != NULL)) goto invalid_args; - int id_l = strlen(id_substr); - int dt_l = data_substr == NULL ? 0 : strlen(data_substr); + const int id_l = strlen(id_substr); + const int dt_l = data_substr == NULL ? 0 : strlen(data_substr); if ((id_l > 8) || (dt_l > 16) || (dt_l % 2)) goto invalid_args; for (int i = 0; i < id_l; i++) if(!isxdigit((int) id_substr[i])) goto invalid_args; for (int i = 0; i < dt_l; i++) if(!isxdigit((int) data_substr[i])) goto invalid_args; int msg_id; if (sscanf(id_substr, "%X", &msg_id) < 1) goto invalid_args; for (int i = 0; i < (dt_l / 2); i++) { - char *byte_to_parse = malloc(3); - strncpy(byte_to_parse, data_substr + i * 2, 2); + char* byte_to_parse = malloc(3); + // ReSharper disable once CppDFANullDereference (if dt_l == 0 we skip this loop) + strncpy(byte_to_parse, i * 2 + data_substr, 2); int num; - int res = sscanf(byte_to_parse, "%X", &num); + const int res = sscanf(byte_to_parse, "%X", &num); free(byte_to_parse); if (res < 1) goto invalid_args; msg.data[i] = num; @@ -74,7 +75,7 @@ static int send_can_frame(int argc, char **argv) { msg.data_length_code = dt_l / 2; msg.identifier = msg_id; msg.extd = (id_l > 3); - esp_err_t res = twai_transmit(&msg, pdMS_TO_TICKS(1000)); + const esp_err_t res = twai_transmit(&msg, pdMS_TO_TICKS(1000)); switch(res) { case ESP_OK: can_msg_to_str(&msg, "sent ", printf_str); @@ -99,22 +100,16 @@ invalid_args: } static int canrecover(int argc, char **argv) { - esp_err_t res = twai_initiate_recovery(); + const esp_err_t res = twai_initiate_recovery(); if (res == ESP_OK) print_w_clr_time("Started CAN recovery.", LOG_COLOR_GREEN, true); else if (curr_can_state.state == CAN_NOT_INSTALLED) print_w_clr_time("CAN driver is not installed!", LOG_COLOR_RED, true); else print_w_clr_time("Can't start recovery - not in bus-off state!", LOG_COLOR_RED, true); return 0; } -static const char* can_states_str[] = { - "not installed", - "stopped", - "error active", - "error passive", - "bus off", - "recovering" -}; +static const char* can_states_str[] = {"not installed", "stopped", "error active", "error passive", "bus off", "recovering"}; +// ReSharper disable once CppDFAConstantFunctionResult static int canstats(int argc, char **argv) { if (curr_can_state.state == CAN_NOT_INSTALLED) { print_w_clr_time("CAN driver is not installed!", LOG_COLOR_RED, true); @@ -146,11 +141,10 @@ static struct { } canup_args; static int canup(int argc, char **argv) { - esp_err_t res; static twai_timing_config_t t_config; twai_general_config_t gen_cfg = default_g_config; twai_filter_config_t f_config; - int nerrors = arg_parse(argc, argv, (void **) &canup_args); + const int nerrors = arg_parse(argc, argv, (void **) &canup_args); if (nerrors != 0) { arg_print_errors(stderr, canup_args.end, argv[0]); return 1; @@ -164,7 +158,7 @@ static int canup(int argc, char **argv) { f_config = (twai_filter_config_t) TWAI_FILTER_CONFIG_ACCEPT_ALL(); printf("Using accept all filters.\n"); } - esp_log_level_t prev_gpio_lvl = esp_log_level_get("gpio"); + const esp_log_level_t prev_gpio_lvl = esp_log_level_get("gpio"); int mode = 0; if (canup_args.mode->count) { const char* mode_str = canup_args.mode->sval[0]; @@ -239,7 +233,7 @@ static int canup(int argc, char **argv) { } xSemaphoreTake(can_mutex, portMAX_DELAY); esp_log_level_set("gpio", ESP_LOG_ERROR); - res = twai_driver_install(&gen_cfg, &t_config, &f_config); + const esp_err_t res = twai_driver_install(&gen_cfg, &t_config, &f_config); if (res == ESP_OK) { print_w_clr_time("CAN driver installed", LOG_COLOR_BLUE, true); if (canup_args.autorecover->count) { @@ -264,7 +258,7 @@ free_exit: static int canstart(int argc, char **argv) { xSemaphoreTake(can_mutex, portMAX_DELAY); - esp_err_t res = twai_start(); + const esp_err_t res = twai_start(); if (res == ESP_OK) { print_w_clr_time("CAN driver started", LOG_COLOR_GREEN, true); is_error_passive = false; @@ -276,7 +270,7 @@ static int canstart(int argc, char **argv) { static int candown(int argc, char **argv) { xSemaphoreTake(can_mutex, portMAX_DELAY); if (curr_can_state.state != CAN_BUF_OFF) { - esp_err_t res = twai_stop(); + const esp_err_t res = twai_stop(); if (res == ESP_OK) print_w_clr_time("CAN was stopped.", LOG_COLOR_GREEN, true); else { print_w_clr_time("Driver is not in running state, or is not installed.", LOG_COLOR_RED, true); @@ -370,15 +364,15 @@ static struct { } canfilter_args; static int canfilter(int argc, char **argv) { - int nerrors = arg_parse(argc, argv, (void **) &canfilter_args); + const int nerrors = arg_parse(argc, argv, (void **) &canfilter_args); if (nerrors != 0) { arg_print_errors(stderr, canfilter_args.end, argv[0]); return 1; } const char* mask_s = canfilter_args.mask_arg->sval[0]; const char* code_s = canfilter_args.code_arg->sval[0]; - int m_l = strlen(mask_s); - int c_l = strlen(code_s); + const int m_l = strlen(mask_s); + const int c_l = strlen(code_s); if (m_l != 8 || c_l != 8) goto invalid_args; for (int i = 0; i < m_l; i++) if(!isxdigit((int) mask_s[i])) goto invalid_args; for (int i = 0; i < c_l; i++) if(!isxdigit((int) code_s[i])) goto invalid_args; @@ -439,8 +433,8 @@ void smartfilters_destroy(List** head) { static int cansmartfilter(int argc, char **argv) { char *filter_str_buf = NULL; - smart_filt_element_t *filt_element = NULL; - int nerrors = arg_parse(argc, argv, (void **) &cansmart_args); + smart_filt_element_t* filt_element = NULL; + const int nerrors = arg_parse(argc, argv, (void **) &cansmart_args); if (nerrors != 0) { arg_print_errors(stderr, cansmart_args.end, argv[0]); return 1; @@ -455,14 +449,14 @@ static int cansmartfilter(int argc, char **argv) { filt_element = malloc(sizeof(smart_filt_element_t)); const char *filter_str_ptr = cansmart_args.filters->sval[i]; filter_str_buf = strdup(filter_str_ptr); - char *code_substr = strtok(filter_str_buf, "#"); - char *mask_substr = strtok(NULL, "#"); + const char* code_substr = strtok(filter_str_buf, "#"); + const char *mask_substr = strtok(NULL, "#"); if (code_substr == NULL || mask_substr == NULL || strtok(NULL, "#") != NULL) goto invalid_args; - int m_l = strlen(mask_substr); - int c_l = strlen(code_substr); + const int m_l = strlen(mask_substr); + const int c_l = strlen(code_substr); if (m_l > 8 || c_l > 8) goto invalid_args; - for (int i = 0; i < m_l; i++) if (!isxdigit((int) mask_substr[i])) goto invalid_args; - for (int i = 0; i < c_l; i++) if (!isxdigit((int) code_substr[i])) goto invalid_args; + for (int j = 0; j < m_l; j++) if (!isxdigit((int) mask_substr[j])) goto invalid_args; + for (int j = 0; j < c_l; j++) if (!isxdigit((int) code_substr[j])) goto invalid_args; if (sscanf(code_substr, "%" PRIX32, &filt_element->filt) < 1) goto invalid_args; if (sscanf(mask_substr, "%" PRIX32, &filt_element->mask) < 1) goto invalid_args; free(filter_str_buf); @@ -471,11 +465,11 @@ static int cansmartfilter(int argc, char **argv) { hwfilt_mask = filt_element->mask; hwfilt_code = filt_element->filt; } else { - uint32_t common_bits = filt_element->mask & hwfilt_mask; - uint32_t new_bits = filt_element->mask - common_bits; - uint32_t missing_bits = hwfilt_mask - common_bits; + const uint32_t common_bits = filt_element->mask & hwfilt_mask; + const uint32_t new_bits = filt_element->mask - common_bits; + const uint32_t missing_bits = hwfilt_mask - common_bits; hwfilt_mask &= filt_element->mask; - uint32_t bit_to_delete = (hwfilt_code ^ filt_element->filt) & hwfilt_mask; + const uint32_t bit_to_delete = (hwfilt_code ^ filt_element->filt) & hwfilt_mask; hwfilt_mask -= bit_to_delete; if (new_bits || missing_bits || bit_to_delete) tmp_sw = true; } diff --git a/main/cmd_system.c b/main/cmd_system.c index 04ece50..8eac959 100644 --- a/main/cmd_system.c +++ b/main/cmd_system.c @@ -8,7 +8,6 @@ #include "esp_chip_info.h" #include "esp_sleep.h" #include "esp_flash.h" -#include "driver/rtc_io.h" #include "driver/uart.h" #include "argtable3/argtable3.h" #include "freertos/FreeRTOS.h" @@ -145,7 +144,7 @@ static void register_free(void) /* 'heap' command prints minumum heap size */ static int heap_size(int argc, char **argv) { - uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT); + const uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT); printf("min heap size: %"PRIu32"\n", heap_size); return 0; } @@ -215,7 +214,7 @@ static const char* s_log_level_names[] = { static int log_level(int argc, char **argv) { - int nerrors = arg_parse(argc, argv, (void **) &log_level_args); + const int nerrors = arg_parse(argc, argv, (void **) &log_level_args); if (nerrors != 0) { arg_print_errors(stderr, log_level_args.end, argv[0]); return 1; @@ -225,7 +224,7 @@ static int log_level(int argc, char **argv) const char* tag = log_level_args.tag->sval[0]; const char* level_str = log_level_args.level->sval[0]; esp_log_level_t level; - size_t level_len = strlen(level_str); + const size_t level_len = strlen(level_str); for (level = ESP_LOG_NONE; level <= ESP_LOG_VERBOSE; level++) { if (memcmp(level_str, s_log_level_names[level], level_len) == 0) { break; diff --git a/main/cmd_utils.c b/main/cmd_utils.c index f73ab9c..1aa0634 100644 --- a/main/cmd_utils.c +++ b/main/cmd_utils.c @@ -1,12 +1,10 @@ #include "cmd_utils.h" #include "esp_log.h" #include "inttypes.h" -#include "freertos/projdefs.h" #include "string.h" #include "esp_console.h" #include "argtable3/argtable3.h" #include "xvprintf.h" -#include #include #include @@ -22,7 +20,7 @@ static struct { } timestamp_args; static int timestamp(int argc, char **argv) { - int nerrors = arg_parse(argc, argv, (void **) ×tamp_args); + const int nerrors = arg_parse(argc, argv, (void **) ×tamp_args); if (nerrors != 0) { arg_print_errors(stderr, timestamp_args.end, argv[0]); return 1; diff --git a/main/console.c b/main/console.c index 7afe547..6ee8f68 100644 --- a/main/console.c +++ b/main/console.c @@ -1,3 +1,4 @@ +// ReSharper disable CppDFAUnreachableCode #include "console.h" #include "sdkconfig.h" #include @@ -8,7 +9,6 @@ #include "esp_vfs_dev.h" #include #include -#include "esp_system.h" #include "freertos/portmacro.h" #include "freertos/projdefs.h" #include "linenoise/linenoise.h" @@ -39,7 +39,6 @@ SemaphoreHandle_t console_taken_sem; static void update_prompt() { char text[45]; - int text_len; static char* prompt_color; text[0] = '\0'; switch (curr_can_state.state) { @@ -82,19 +81,17 @@ static void update_prompt() { } else { strcat(prompt_buf, text); } - text_len = strlen(text); + const int text_len = strlen(text); ls.prompt = prompt_buf; ls.plen = text_len; } void console_task_tx(void* arg) { static const TickType_t prompt_timeout = pdMS_TO_TICKS(200); - // static const TickType_t prompt_timeout = portMAX_DELAY; const int fd = fileno(stdout); - char *msg_to_print; size_t msg_to_print_size; while(1) { - msg_to_print = (char *)xRingbufferReceive(can_messages, &msg_to_print_size, prompt_timeout); + char* msg_to_print = xRingbufferReceive(can_messages, &msg_to_print_size, prompt_timeout); update_prompt(); xSemaphoreTake(console_taken_sem, portMAX_DELAY); xSemaphoreTake(stdout_taken_sem, portMAX_DELAY); @@ -117,10 +114,9 @@ void console_task_interactive(void* arg) { console_taken_sem = xSemaphoreCreateMutex(); stdout_taken_sem = xSemaphoreCreateMutex(); char *buf = calloc(1, console_config.max_cmdline_length); - char *line; /* Figure out if the terminal supports escape sequences */ printf("Testing your console...\n"); - int probe_status = linenoiseProbe(); + const int probe_status = linenoiseProbe(); // int probe_status = 1; if (probe_status) { /* zero indicates success */ printf("\n" @@ -142,7 +138,7 @@ void console_task_interactive(void* arg) { xTaskCreate(console_task_tx, "console tsk tx", 5000, NULL, CONFIG_CONSOLE_TX_PRIORITY, NULL); esp_log_set_vprintf(&vxprintf); while (true) { - line = linenoiseEditFeed(&ls); + char* line = linenoiseEditFeed(&ls); if (line == linenoiseEditMore) continue; xSemaphoreTake(console_taken_sem, portMAX_DELAY); linenoiseEditStop(&ls); @@ -159,7 +155,7 @@ void console_task_interactive(void* arg) { } int ret; - esp_err_t err = esp_console_run(line, &ret); + const esp_err_t err = esp_console_run(line, &ret); if (err == ESP_ERR_NOT_FOUND) { printf("Unrecognized command\n"); } else if (err == ESP_ERR_INVALID_ARG) { diff --git a/main/fs.c b/main/fs.c index 3121d29..56e0c57 100644 --- a/main/fs.c +++ b/main/fs.c @@ -11,7 +11,7 @@ static const char* TAG = "fs"; void initialize_filesystem(void) { static wl_handle_t wl_handle; const esp_vfs_fat_mount_config_t mount_config = {.max_files = 4, .format_if_mount_failed = true}; - esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle); + const esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err)); return; diff --git a/main/xvprintf.c b/main/xvprintf.c index 1c45976..653f9d4 100644 --- a/main/xvprintf.c +++ b/main/xvprintf.c @@ -15,8 +15,7 @@ void init_tx_ringbuf() { // @important Do NOT use the ESP_LOG* macro's in this function ELSE recursive loop and stack overflow! So use printf() instead for debug messages. int vxprintf(const char *fmt, va_list args) { char msg_to_send[300]; - size_t str_len; - str_len = vsnprintf(msg_to_send, 299, fmt, args); + const size_t str_len = vsnprintf(msg_to_send, 299, fmt, args); xRingbufferSend(can_messages, msg_to_send, str_len + 1, pdMS_TO_TICKS(200)); return str_len; }