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 <tommy@tommywatson.com>
This commit is contained in:
tommywatson 2024-06-18 01:18:51 -05:00 committed by GitHub
parent 344c5d8d22
commit 00a3a0ba3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 18 deletions

View File

@ -150,8 +150,10 @@ esp_netif_t * wifi_init_sta(const char * wifi_ssid, const char * wifi_pass)
// .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER, // .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER,
}, },
}; };
strncpy((char *) wifi_sta_config.sta.ssid, wifi_ssid, 31); strncpy((char *) wifi_sta_config.sta.ssid,
wifi_sta_config.sta.ssid[31] = '\0'; 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); strncpy((char *) wifi_sta_config.sta.password, wifi_pass, 63);
wifi_sta_config.sta.password[63] = '\0'; wifi_sta_config.sta.password[63] = '\0';
@ -214,4 +216,4 @@ EventBits_t wifi_connect(void)
* happened. */ * happened. */
return bits; return bits;
} }

View File

@ -20,14 +20,16 @@
typedef enum typedef enum
{ {
DEVICE_MAX = 0, DEVICE_UNKNOWN = -1,
DEVICE_MAX,
DEVICE_ULTRA, DEVICE_ULTRA,
DEVICE_SUPRA, DEVICE_SUPRA,
} DeviceModel; } DeviceModel;
typedef enum typedef enum
{ {
ASIC_BM1397 = 0, ASIC_UNKNOWN = -1,
ASIC_BM1397,
ASIC_BM1366, ASIC_BM1366,
ASIC_BM1368, ASIC_BM1368,
} AsicModel; } AsicModel;
@ -60,7 +62,7 @@ typedef struct
char best_session_diff_string[DIFF_STRING_SIZE]; char best_session_diff_string[DIFF_STRING_SIZE];
bool FOUND_BLOCK; bool FOUND_BLOCK;
bool startup_done; bool startup_done;
char ssid[20]; char ssid[32];
char wifi_status[20]; char wifi_status[20];
char * pool_url; char * pool_url;
uint16_t pool_port; uint16_t pool_port;
@ -103,4 +105,4 @@ typedef struct
} GlobalState; } GlobalState;
#endif /* GLOBAL_STATE_H_ */ #endif /* GLOBAL_STATE_H_ */

View File

@ -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 * 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 * 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 * 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 * root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "power", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power); 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: case ASIC_BM1368:
core_count = BM1368_CORE_COUNT; core_count = BM1368_CORE_COUNT;
break; break;
case ASIC_UNKNOWN:
default:
core_count = -1;
break;
} }
cJSON_AddNumberToObject(root, "coreCount", core_count); cJSON_AddNumberToObject(root, "coreCount", core_count);
cJSON_AddStringToObject(root, "ASICModel", GLOBAL_STATE->asic_model_str); cJSON_AddStringToObject(root, "ASICModel", GLOBAL_STATE->asic_model_str);

View File

@ -47,6 +47,10 @@ void app_main(void)
} else { } else {
ESP_LOGE(TAG, "Invalid DEVICE model"); ESP_LOGE(TAG, "Invalid DEVICE model");
// maybe should return here to now execute anything with a faulty device parameter ! // 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")); 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); 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); char * hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, HOSTNAME);
// copy the wifi ssid to the global state // 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 // init and connect to wifi
wifi_init(wifi_ssid, wifi_pass, hostname); wifi_init(wifi_ssid, wifi_pass, hostname);

View File

@ -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 X position is in character widths (8 or 16)
// The Y position is in memory pages (8 lines each) // 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; int i, iLen;
uint8_t * s; 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); ret = i2c_master_write_to_device(I2C_MASTER_NUM, 0x3C, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
return ret; return ret;
} }

View File

@ -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) // 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 // 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) // Sets a pixel to On (1) or Off (0)
// Coordinate system is pixels, not text rows (0-127, 0-63) // Coordinate system is pixels, not text rows (0-127, 0-63)

View File

@ -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); _suffix_string(module->best_session_nonce_diff, module->best_session_diff_string, DIFF_STRING_SIZE, 0);
// set the ssid string to blank // set the ssid string to blank
memset(module->ssid, 0, 20); memset(module->ssid, 0, sizeof(module->ssid));
// set the wifi_status to blank // set the wifi_status to blank
memset(module->wifi_status, 0, 20); memset(module->wifi_status, 0, 20);
@ -293,8 +293,8 @@ static void _update_connection(GlobalState * GLOBAL_STATE)
case DEVICE_SUPRA: case DEVICE_SUPRA:
if (OLED_status()) { if (OLED_status()) {
OLED_clearLine(2); OLED_clearLine(2);
memset(module->oled_buf, 0, 20); strncpy(module->oled_buf, module->ssid, sizeof(module->oled_buf));
snprintf(module->oled_buf, 20, "%s", module->ssid); module->oled_buf[sizeof(module->oled_buf) - 1] = 0;
OLED_writeString(0, 1, module->oled_buf); OLED_writeString(0, 1, module->oled_buf);
memset(module->oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
@ -470,9 +470,6 @@ void SYSTEM_task(void * pvParameters)
_clear_display(GLOBAL_STATE); _clear_display(GLOBAL_STATE);
_init_connection(GLOBAL_STATE); _init_connection(GLOBAL_STATE);
wifi_mode_t wifi_mode;
esp_err_t result;
char input_event[10]; char input_event[10];
ESP_LOGI(TAG, "SYSTEM_task started"); ESP_LOGI(TAG, "SYSTEM_task started");