Add native Wi-Fi management and persistence
This commit is contained in:
+53
-1
@@ -13,6 +13,9 @@
|
||||
#include "status_led.h"
|
||||
#include "usb_cdc_transport.h"
|
||||
#include "usb_console.h"
|
||||
#include "wifi_config.h"
|
||||
#include "wifi_console.h"
|
||||
#include "wifi_manager.h"
|
||||
|
||||
#define CONSOLE_BAUD_RATE 115200
|
||||
#define CONSOLE_TX_GPIO 43
|
||||
@@ -22,7 +25,7 @@ static const char *TAG = "firmware";
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "ESP32-S3 Serial Swiss Army Knife native USB CDC phase started");
|
||||
ESP_LOGI(TAG, "ESP32-S3 Serial Swiss Army Knife Wi-Fi foundation phase started");
|
||||
|
||||
if (esp_psram_is_initialized()) {
|
||||
ESP_LOGI(TAG, "PSRAM initialized: %u bytes", (unsigned int)esp_psram_get_size());
|
||||
@@ -30,6 +33,9 @@ void app_main(void)
|
||||
ESP_LOGW(TAG, "PSRAM is not initialized");
|
||||
}
|
||||
|
||||
/* Seed credential generation before any future RF or ADC initialization. */
|
||||
esp_err_t wifi_entropy_error = wifi_config_entropy_init();
|
||||
|
||||
/* Blue means the firmware is initialized and waiting for a console command. */
|
||||
ESP_ERROR_CHECK(status_led_init());
|
||||
ESP_ERROR_CHECK(rs232_port_owner_init());
|
||||
@@ -49,6 +55,51 @@ void app_main(void)
|
||||
ESP_ERROR_CHECK(session_broker_init());
|
||||
/* Native USB owns GPIO19/20; UART0 logging stays on the USB-to-UART bridge. */
|
||||
ESP_ERROR_CHECK(usb_cdc_transport_init());
|
||||
|
||||
wifi_app_config_t wifi_config;
|
||||
wifi_config_load_source_t wifi_config_source;
|
||||
esp_err_t wifi_config_error = wifi_entropy_error;
|
||||
if (wifi_config_error == ESP_OK) {
|
||||
wifi_config_error = wifi_config_load(&wifi_config, &wifi_config_source);
|
||||
}
|
||||
if (wifi_config_error != ESP_OK) {
|
||||
ESP_LOGW(TAG,
|
||||
"NVS Wi-Fi configuration unavailable (%s); trying generated RAM defaults",
|
||||
esp_err_to_name(wifi_config_error));
|
||||
wifi_config_error = wifi_config_defaults(&wifi_config);
|
||||
} else if (wifi_config_source == WIFI_CONFIG_LOAD_GENERATED_MISSING) {
|
||||
/* Persist the random per-device fallback-AP credential on true first boot. */
|
||||
esp_err_t save_error = wifi_config_save(&wifi_config);
|
||||
if (save_error != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Could not persist initial Wi-Fi defaults: %s",
|
||||
esp_err_to_name(save_error));
|
||||
}
|
||||
} else if (wifi_config_source == WIFI_CONFIG_LOAD_GENERATED_INVALID) {
|
||||
ESP_LOGW(TAG,
|
||||
"Stored Wi-Fi configuration is incompatible; using RAM defaults without overwriting it");
|
||||
}
|
||||
|
||||
if (wifi_config_error == ESP_OK) {
|
||||
esp_err_t wifi_error = wifi_manager_init(&wifi_config);
|
||||
if (wifi_error == ESP_OK && wifi_config.enabled_at_boot != 0U) {
|
||||
wifi_error = wifi_manager_start();
|
||||
}
|
||||
if (wifi_error != ESP_OK) {
|
||||
/* UART0 and native USB remain recovery paths if networking is unavailable. */
|
||||
ESP_LOGE(TAG, "Wi-Fi manager unavailable: %s", esp_err_to_name(wifi_error));
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Using %s Wi-Fi configuration; AP policy=%s",
|
||||
wifi_config_source == WIFI_CONFIG_LOAD_STORED
|
||||
? "stored"
|
||||
: "generated default",
|
||||
wifi_config_ap_policy_to_string(wifi_config.ap_policy));
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Could not create a valid Wi-Fi configuration: %s",
|
||||
esp_err_to_name(wifi_config_error));
|
||||
}
|
||||
wifi_config_secure_wipe(&wifi_config, sizeof(wifi_config));
|
||||
|
||||
ESP_LOGI(
|
||||
TAG,
|
||||
"Using %s serial configuration; UART service starts on 'serial start' or native USB open",
|
||||
@@ -77,6 +128,7 @@ void app_main(void)
|
||||
ESP_ERROR_CHECK(serial_console_register_commands());
|
||||
ESP_ERROR_CHECK(session_console_register_commands());
|
||||
ESP_ERROR_CHECK(usb_console_register_commands());
|
||||
ESP_ERROR_CHECK(wifi_console_register_commands());
|
||||
ESP_ERROR_CHECK(esp_console_start_repl(repl));
|
||||
|
||||
ESP_LOGI(TAG, "Interactive test console ready at %d baud", CONSOLE_BAUD_RATE);
|
||||
|
||||
Reference in New Issue
Block a user