From 00a3a0ba3d0b93d96873239d70d4ba6e4d3eec98 Mon Sep 17 00:00:00 2001 From: tommywatson Date: Tue, 18 Jun 2024 01:18:51 -0500 Subject: [PATCH] Code clean resulting from looking into #218 (#220) * Code clean resulting from looking into #218 * Fixed asic count Set canary value for invalid device's asic_count --------- Co-authored-by: tommy --- components/connect/connect.c | 8 +++++--- main/global_state.h | 10 ++++++---- main/http_server/http_server.c | 6 +++++- main/main.c | 9 ++++++++- main/oled.c | 4 ++-- main/oled.h | 2 +- main/system.c | 9 +++------ 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/components/connect/connect.c b/components/connect/connect.c index 6ac4e969..a8f3d70d 100644 --- a/components/connect/connect.c +++ b/components/connect/connect.c @@ -150,8 +150,10 @@ esp_netif_t * wifi_init_sta(const char * wifi_ssid, const char * wifi_pass) // .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER, }, }; - strncpy((char *) wifi_sta_config.sta.ssid, wifi_ssid, 31); - wifi_sta_config.sta.ssid[31] = '\0'; + strncpy((char *) wifi_sta_config.sta.ssid, + wifi_ssid, + sizeof(wifi_sta_config.sta.ssid)); + wifi_sta_config.sta.ssid[sizeof(wifi_sta_config.sta.ssid) - 1] = '\0'; strncpy((char *) wifi_sta_config.sta.password, wifi_pass, 63); wifi_sta_config.sta.password[63] = '\0'; @@ -214,4 +216,4 @@ EventBits_t wifi_connect(void) * happened. */ return bits; -} \ No newline at end of file +} diff --git a/main/global_state.h b/main/global_state.h index b8aa95f3..65bd314c 100644 --- a/main/global_state.h +++ b/main/global_state.h @@ -20,14 +20,16 @@ typedef enum { - DEVICE_MAX = 0, + DEVICE_UNKNOWN = -1, + DEVICE_MAX, DEVICE_ULTRA, DEVICE_SUPRA, } DeviceModel; typedef enum { - ASIC_BM1397 = 0, + ASIC_UNKNOWN = -1, + ASIC_BM1397, ASIC_BM1366, ASIC_BM1368, } AsicModel; @@ -60,7 +62,7 @@ typedef struct char best_session_diff_string[DIFF_STRING_SIZE]; bool FOUND_BLOCK; bool startup_done; - char ssid[20]; + char ssid[32]; char wifi_status[20]; char * pool_url; uint16_t pool_port; @@ -103,4 +105,4 @@ typedef struct } GlobalState; -#endif /* GLOBAL_STATE_H_ */ \ No newline at end of file +#endif /* GLOBAL_STATE_H_ */ diff --git a/main/http_server/http_server.c b/main/http_server/http_server.c index 560c7485..56aee3ee 100644 --- a/main/http_server/http_server.c +++ b/main/http_server/http_server.c @@ -354,7 +354,7 @@ static esp_err_t GET_system_info(httpd_req_t * req) char * hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, CONFIG_LWIP_LOCAL_HOSTNAME); char * stratumURL = nvs_config_get_string(NVS_CONFIG_STRATUM_URL, CONFIG_STRATUM_URL); char * stratumUser = nvs_config_get_string(NVS_CONFIG_STRATUM_USER, CONFIG_STRATUM_USER); - char * board_version = nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, 'unknown'); + char * board_version = nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "unknown"); cJSON * root = cJSON_CreateObject(); cJSON_AddNumberToObject(root, "power", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power); @@ -389,6 +389,10 @@ static esp_err_t GET_system_info(httpd_req_t * req) case ASIC_BM1368: core_count = BM1368_CORE_COUNT; break; + case ASIC_UNKNOWN: + default: + core_count = -1; + break; } cJSON_AddNumberToObject(root, "coreCount", core_count); cJSON_AddStringToObject(root, "ASICModel", GLOBAL_STATE->asic_model_str); diff --git a/main/main.c b/main/main.c index 21906c85..b2ed1dc2 100644 --- a/main/main.c +++ b/main/main.c @@ -47,6 +47,10 @@ void app_main(void) } else { ESP_LOGE(TAG, "Invalid DEVICE model"); // maybe should return here to now execute anything with a faulty device parameter ! + // this stops crashes/reboots and allows dev testing without an asic + GLOBAL_STATE.device_model = DEVICE_UNKNOWN; + GLOBAL_STATE.asic_count = -1; + GLOBAL_STATE.voltage_domain = 1; } GLOBAL_STATE.board_version = atoi(nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "000")); ESP_LOGI(TAG, "Found Device Model: %s", GLOBAL_STATE.device_model_str); @@ -122,7 +126,10 @@ void app_main(void) char * hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, HOSTNAME); // copy the wifi ssid to the global state - strncpy(GLOBAL_STATE.SYSTEM_MODULE.ssid, wifi_ssid, 20); + strncpy(GLOBAL_STATE.SYSTEM_MODULE.ssid, + wifi_ssid, + sizeof(GLOBAL_STATE.SYSTEM_MODULE.ssid)); + GLOBAL_STATE.SYSTEM_MODULE.ssid[sizeof(GLOBAL_STATE.SYSTEM_MODULE.ssid)-1] = 0; // init and connect to wifi wifi_init(wifi_ssid, wifi_pass, hostname); diff --git a/main/oled.c b/main/oled.c index a917ba64..1f40c3fa 100644 --- a/main/oled.c +++ b/main/oled.c @@ -213,7 +213,7 @@ int OLED_setPixel(int x, int y, uint8_t ucColor) // The X position is in character widths (8 or 16) // The Y position is in memory pages (8 lines each) // -int OLED_writeString(int x, int y, char * szMsg) +int OLED_writeString(int x, int y, const char * szMsg) { int i, iLen; uint8_t * s; @@ -286,4 +286,4 @@ static esp_err_t write(uint8_t * data, uint8_t len) ret = i2c_master_write_to_device(I2C_MASTER_NUM, 0x3C, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); return ret; -} \ No newline at end of file +} diff --git a/main/oled.h b/main/oled.h index c0ce30a2..e3593fd2 100644 --- a/main/oled.h +++ b/main/oled.h @@ -37,7 +37,7 @@ int OLED_fill(uint8_t ucPattern); // Write a text string to the display at x (column 0-127) and y (row 0-7) // bLarge = 0 - 8x8 font, bLarge = 1 - 16x24 font -int OLED_writeString(int x, int y, char *szText); +int OLED_writeString(int x, int y, const char *szText); // Sets a pixel to On (1) or Off (0) // Coordinate system is pixels, not text rows (0-127, 0-63) diff --git a/main/system.c b/main/system.c index 149a0630..0c87b1bc 100644 --- a/main/system.c +++ b/main/system.c @@ -70,7 +70,7 @@ static void _init_system(GlobalState * GLOBAL_STATE) _suffix_string(module->best_session_nonce_diff, module->best_session_diff_string, DIFF_STRING_SIZE, 0); // set the ssid string to blank - memset(module->ssid, 0, 20); + memset(module->ssid, 0, sizeof(module->ssid)); // set the wifi_status to blank memset(module->wifi_status, 0, 20); @@ -293,8 +293,8 @@ static void _update_connection(GlobalState * GLOBAL_STATE) case DEVICE_SUPRA: if (OLED_status()) { OLED_clearLine(2); - memset(module->oled_buf, 0, 20); - snprintf(module->oled_buf, 20, "%s", module->ssid); + strncpy(module->oled_buf, module->ssid, sizeof(module->oled_buf)); + module->oled_buf[sizeof(module->oled_buf) - 1] = 0; OLED_writeString(0, 1, module->oled_buf); memset(module->oled_buf, 0, 20); @@ -470,9 +470,6 @@ void SYSTEM_task(void * pvParameters) _clear_display(GLOBAL_STATE); _init_connection(GLOBAL_STATE); - wifi_mode_t wifi_mode; - esp_err_t result; - char input_event[10]; ESP_LOGI(TAG, "SYSTEM_task started");