8000 Clean up NVS Wi-Fi credentials code by mutatrum · Pull Request #859 · bitaxeorg/ESP-Miner · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Clean up NVS Wi-Fi credentials code #859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions components/connect/connect.c
ESP_ERROR_CHECK(esp_wifi_start());
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "connect.h"
#include "global_state.h"
#include "nvs_config.h"

// Maximum number of access points to scan
#define MAX_AP_COUNT 20
Expand Down Expand Up @@ -316,10 +317,17 @@ esp_netif_t * wifi_init_sta(const char * wifi_ssid, const char * wifi_pass)
return esp_netif_sta;
}

void wifi_init(void * pvParameters, const char * wifi_ssid, const char * wifi_pass, const char * hostname)
void wifi_init(void * pvParameters)
{
GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;

char * wifi_ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, CONFIG_ESP_WIFI_SSID);
// copy the wifi ssid to the global state
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;

free(wifi_ssid);

ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());

Expand All @@ -338,7 +346,7 @@ void wifi_init(void * pvParameters, const char * wifi_ssid, const char * wifi_pa
wifi_init_softap(GLOBAL_STATE->SYSTEM_MODULE.ap_ssid);

/* Skip connection if SSID is null */
if (strlen(wifi_ssid) == 0) {
if (strlen(GLOBAL_STATE->SYSTEM_MODULE.ssid) == 0) {
ESP_LOGI(TAG, "No WiFi SSID provided, skipping connection");

/* Start WiFi */
Expand All @@ -349,16 +357,23 @@ void wifi_init(void * pvParameters, const char * wifi_ssid, const char * wifi_pa

return;
} else {

char * wifi_pass = nvs_config_get_string(NVS_CONFIG_WIFI_PASS, CONFIG_ESP_WIFI_PASSWORD);

/* Initialize STA */
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
esp_netif_t * esp_netif_sta = wifi_init_sta(wifi_ssid, wifi_pass);
esp_netif_t * esp_netif_sta = wifi_init_sta(GLOBAL_STATE->SYSTEM_MODULE.ssid, wifi_pass);

free(wifi_pass);

/* Start Wi-Fi */

/* Disable power savings for best performance */
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));

char * hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, CONFIG_LWIP_LOCAL_HOSTNAME);

/* Set Hostname */
esp_err_t err = esp_netif_set_hostname(esp_netif_sta, hostname);
if (err != ERR_OK) {
Expand All @@ -367,6 +382,8 @@ void wifi_init(void * pvParameters, const char * wifi_ssid, const char * wifi_pa
ESP_LOGI(TAG, "ESP_WIFI setting hostname to: %s", hostname);
}

free(hostname);

ESP_LOGI(TAG, "wifi_init_sta finished.");

return;
Expand Down
2 changes: 1 addition & 1 deletion components/connect/include/connect.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct {
void toggle_wifi_softap(void);
void wifi_softap_on(void);
void wifi_softap_off(void);
void wifi_init(void * GLOBAL_STATE, const char * wifi_ssid, const char * wifi_pass, const char * hostname);
void wifi_init(void * GLOBAL_STATE);
esp_err_t wifi_scan(wifi_ap_record_simple_t *ap_records, uint16_t *ap_count);
esp_err_t get_wifi_current_rssi(int8_t *rssi);

Expand Down
18 changes: 6 additions & 12 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,15 @@ void app_main(void)

SYSTEM_init_system(&GLOBAL_STATE);

char * wifi_ssid;
char * wifi_pass;
char * hostname;

NVSDevice_get_wifi_creds(&GLOBAL_STATE, &wifi_ssid, &wifi_pass, &hostname);

// init AP and connect to wifi
wifi_init(&GLOBAL_STATE, wifi_ssid, wifi_pass, hostname);
wifi_init(&GLOBAL_STATE);

SYSTEM_init_peripherals(&GLOBAL_STATE);

// This needs to be done before the power management task starts
GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY);
ESP_LOGI(TAG, "NVS_CONFIG_ASIC_FREQ %f", (float)GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value);

xTaskCreate(POWER_MANAGEMENT_task, "power management", 8192, (void *) &GLOBAL_STATE, 10, NULL);

//start the API for AxeOS
Expand All @@ -98,11 +96,7 @@ void app_main(void)
vTaskDelay(100 / portTICK_PERIOD_MS);
}

ESP_LOGI(TAG, "Connected to SSID: %s", wifi_ssid);

free(wifi_ssid);
free(wifi_pass);
free(hostname);
ESP_LOGI(TAG, "Connected to SSID: %s", GLOBAL_STATE.SYSTEM_MODULE.ssid);

GLOBAL_STATE.new_stratum_version_rolling_msg = false;

Expand Down
3 changes: 0 additions & 3 deletions main/nvs_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ char * nvs_config_get_string(const char * key, const char * default_value)

void nvs_config_set_string(const char * key, const char * value)
{

nvs_handle handle;
esp_err_t err;
err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READWRITE, &handle);
Expand Down Expand Up @@ -77,7 +76,6 @@ uint16_t nvs_config_get_u16(const char * key, const uint16_t default_value)

void nvs_config_set_u16(const char * key, const uint16_t value)
{

nvs_handle handle;
esp_err_t err;
err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READWRITE, &handle);
Expand Down Expand Up @@ -154,7 +152,6 @@ uint64_t nvs_config_get_u64(const char * key, const uint64_t default_value)

void nvs_config_set_u64(const char * key, const uint64_t value)
{

nvs_handle handle;
esp_err_t err;
err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READWRITE, &handle);
Expand Down
20 changes: 2 additions & 18 deletions main/nvs_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,8 @@ esp_err_t NVSDevice_init(void) {
return err;
}

esp_err_t NVSDevice_get_wifi_creds(GlobalState * GLOBAL_STATE, char ** wifi_ssid, char ** wifi_pass, char ** hostname) {
// pull the wifi credentials and hostname out of NVS
*wifi_ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, CONFIG_ESP_WIFI_SSID);
*wifi_pass = nvs_config_get_string(NVS_CONFIG_WIFI_PASS, CONFIG_ESP_WIFI_PASSWORD);
*hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, CONFIG_LWIP_LOCAL_HOSTNAME);

// copy the wifi ssid to the global state
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;

return ESP_OK;
}

esp_err_t NVSDevice_parse_config(GlobalState * GLOBAL_STATE) {

GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY);
ESP_LOGI(TAG, "NVS_CONFIG_ASIC_FREQ %f", (float)GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value);

esp_err_t NVSDevice_parse_config(GlobalState * GLOBAL_STATE)
{
GLOBAL_STATE->asic_model_str = nvs_config_get_string(NVS_CONFIG_ASIC_MODEL, "");
GLOBAL_STATE->device_model_str = nvs_config_get_string(NVS_CONFIG_DEVICE_MODEL, "invalid");
char * board_version = nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "000");
Expand Down
1 change: 0 additions & 1 deletion main/nvs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

esp_err_t NVSDevice_init(void);
esp_err_t NVSDevice_parse_config(GlobalState *);
esp_err_t NVSDevice_get_wifi_creds(GlobalState *, char **, char **, char **);

#endif // MAIN_NVS_DEVICE_H
Loading
0