Compare commits

..

3 Commits

Author SHA1 Message Date
Robert McMahon 3969c5780d nvs saves 2025-12-19 10:36:10 -08:00
Robert McMahon ca8b382a40 more on console 2025-12-19 10:21:08 -08:00
Robert McMahon 0f1c5b3079 more on console 2025-12-19 10:12:32 -08:00
9 changed files with 348 additions and 76 deletions

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS "app_console.c" idf_component_register(SRCS "app_console.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
PRIV_REQUIRES console wifi_cfg wifi_controller iperf) PRIV_REQUIRES console wifi_cfg wifi_controller iperf nvs_flash)

View File

@ -1,5 +1,3 @@
#include <string.h>
#include <arpa/inet.h>
#include "app_console.h" #include "app_console.h"
#include "esp_console.h" #include "esp_console.h"
#include "esp_log.h" #include "esp_log.h"
@ -7,17 +5,133 @@
#include "wifi_cfg.h" #include "wifi_cfg.h"
#include "iperf.h" #include "iperf.h"
#include "wifi_controller.h" #include "wifi_controller.h"
#include "esp_wifi.h"
#include "nvs.h"
#include "nvs_flash.h"
#include <string.h>
#include <arpa/inet.h>
#include <inttypes.h>
// Helper to refresh prompt at end of commands // --- Helper: Prompt Update ---
// Updates the "esp32>" vs "esp32*>" prompt based on dirty state
static void end_cmd(void) { static void end_cmd(void) {
app_console_update_prompt(); app_console_update_prompt();
} }
// ============================================================================
// COMMAND: nvs (Storage Management)
// ============================================================================
static struct {
struct arg_lit *dump;
struct arg_lit *clear_all;
struct arg_lit *help;
struct arg_end *end;
} nvs_args;
static void print_nvs_key_str(nvs_handle_t h, const char *key, const char *label) {
char buf[64] = {0};
size_t len = sizeof(buf);
if (nvs_get_str(h, key, buf, &len) == ESP_OK) {
printf(" %-12s : %s\n", label, buf);
} else {
printf(" %-12s : <empty>\n", label);
}
}
static void print_nvs_key_u32(nvs_handle_t h, const char *key, const char *label) {
uint32_t val = 0;
if (nvs_get_u32(h, key, &val) == ESP_OK) {
printf(" %-12s : %" PRIu32 "\n", label, val);
} else {
printf(" %-12s : <empty>\n", label);
}
}
static void print_nvs_key_u8(nvs_handle_t h, const char *key, const char *label) {
uint8_t val = 0;
if (nvs_get_u8(h, key, &val) == ESP_OK) {
printf(" %-12s : %u\n", label, val);
} else {
printf(" %-12s : <empty>\n", label);
}
}
static int cmd_nvs(int argc, char **argv) {
int nerrors = arg_parse(argc, argv, (void **)&nvs_args);
if (nerrors > 0) {
arg_print_errors(stderr, nvs_args.end, argv[0]);
return 1;
}
if (nvs_args.help->count > 0) {
printf("Usage: nvs [--dump] [--clear-all]\n");
return 0;
}
// --- CLEAR ALL ---
if (nvs_args.clear_all->count > 0) {
printf("Erasing ALL settings from NVS...\n");
wifi_cfg_clear_credentials();
wifi_cfg_clear_monitor_channel();
iperf_param_clear();
printf("Done. Please reboot.\n");
end_cmd();
return 0;
}
// --- DUMP ---
printf("\n--- [WiFi Config (netcfg)] ---\n");
nvs_handle_t h;
if (nvs_open("netcfg", NVS_READONLY, &h) == ESP_OK) {
print_nvs_key_str(h, "ssid", "SSID");
print_nvs_key_str(h, "pass", "Password");
print_nvs_key_str(h, "ip", "Static IP");
print_nvs_key_str(h, "mask", "Netmask");
print_nvs_key_str(h, "gw", "Gateway");
print_nvs_key_u8 (h, "dhcp", "DHCP");
print_nvs_key_u8 (h, "mon_ch", "Monitor Ch");
nvs_close(h);
} else {
printf("Failed to open 'netcfg' namespace.\n");
}
printf("\n--- [iPerf Config (storage)] ---\n");
if (nvs_open("storage", NVS_READONLY, &h) == ESP_OK) {
print_nvs_key_str(h, "iperf_dst_ip", "Dest IP");
print_nvs_key_u32(h, "iperf_port", "Port");
print_nvs_key_u32(h, "iperf_pps", "Target PPS");
print_nvs_key_u32(h, "iperf_len", "Packet Len");
print_nvs_key_u32(h, "iperf_burst", "Burst");
nvs_close(h);
} else {
printf("Failed to open 'storage' namespace.\n");
}
printf("\n");
end_cmd();
return 0;
}
static void register_nvs_cmd(void) {
nvs_args.dump = arg_lit0(NULL, "dump", "Show all");
nvs_args.clear_all = arg_lit0(NULL, "clear-all", "Factory Reset");
nvs_args.help = arg_lit0("h", "help", "Help");
nvs_args.end = arg_end(1);
const esp_console_cmd_t cmd = {
.command = "nvs",
.help = "Storage Management",
.func = &cmd_nvs,
.argtable = &nvs_args
};
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
// ============================================================================ // ============================================================================
// COMMAND: iperf // COMMAND: iperf
// ============================================================================ // ============================================================================
static struct { static struct {
struct arg_lit *start, *stop, *status, *save, *reload; struct arg_lit *start, *stop, *status, *save, *reload;
struct arg_lit *clear_nvs;
struct arg_str *ip; struct arg_str *ip;
struct arg_int *port, *pps, *len, *burst; struct arg_int *port, *pps, *len, *burst;
struct arg_lit *help; struct arg_lit *help;
@ -32,7 +146,20 @@ static int cmd_iperf(int argc, char **argv) {
} }
if (iperf_args.help->count > 0) { if (iperf_args.help->count > 0) {
printf("Usage: iperf [options]\n"); // ... (Shortened for brevity) printf("Usage: iperf [options]\n");
printf(" --start Start traffic\n");
printf(" --stop Stop traffic\n");
printf(" --save Save config to NVS\n");
printf(" --clear-nvs Reset to defaults\n");
printf(" -c <ip> Set Dest IP\n");
printf(" --pps <n> Set Packets Per Sec\n");
return 0;
}
if (iperf_args.clear_nvs->count > 0) {
iperf_param_clear();
printf("iPerf Configuration cleared (Reset to defaults).\n");
end_cmd();
return 0; return 0;
} }
@ -50,9 +177,8 @@ static int cmd_iperf(int argc, char **argv) {
if (iperf_args.len->count > 0) { cfg.send_len = (uint32_t)iperf_args.len->ival[0]; config_changed = true; } if (iperf_args.len->count > 0) { cfg.send_len = (uint32_t)iperf_args.len->ival[0]; config_changed = true; }
if (iperf_args.burst->count > 0) { cfg.burst_count = (uint32_t)iperf_args.burst->ival[0]; config_changed = true; } if (iperf_args.burst->count > 0) { cfg.burst_count = (uint32_t)iperf_args.burst->ival[0]; config_changed = true; }
if (iperf_args.pps->count > 0) { if (iperf_args.pps->count > 0) {
int pps = iperf_args.pps->ival[0]; if (iperf_args.pps->ival[0] > 0) {
if (pps > 0) { cfg.target_pps = (uint32_t)iperf_args.pps->ival[0];
cfg.target_pps = (uint32_t)pps; // Update directly
config_changed = true; config_changed = true;
} }
} }
@ -75,15 +201,35 @@ static int cmd_iperf(int argc, char **argv) {
if (iperf_args.start->count > 0) iperf_start(); if (iperf_args.start->count > 0) iperf_start();
if (iperf_args.status->count > 0) iperf_print_status(); if (iperf_args.status->count > 0) iperf_print_status();
end_cmd(); // Update Prompt end_cmd();
return 0; return 0;
} }
static void register_iperf_cmd(void) {
iperf_args.start = arg_lit0(NULL, "start", "Start");
iperf_args.stop = arg_lit0(NULL, "stop", "Stop");
iperf_args.status = arg_lit0(NULL, "status", "Status");
iperf_args.save = arg_lit0(NULL, "save", "Save");
iperf_args.reload = arg_lit0(NULL, "reload", "Reload");
iperf_args.clear_nvs = arg_lit0(NULL, "clear-nvs", "Clear NVS");
iperf_args.ip = arg_str0("c", "client", "<ip>", "IP");
iperf_args.port = arg_int0("p", "port", "<port>", "Port");
iperf_args.pps = arg_int0(NULL, "pps", "<n>", "PPS");
iperf_args.len = arg_int0(NULL, "len", "<bytes>", "Len");
iperf_args.burst = arg_int0(NULL, "burst", "<count>", "Burst");
iperf_args.help = arg_lit0("h", "help", "Help");
iperf_args.end = arg_end(20);
const esp_console_cmd_t cmd = { .command = "iperf", .help = "Traffic Gen", .func = &cmd_iperf, .argtable = &iperf_args };
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
// ============================================================================ // ============================================================================
// COMMAND: monitor // COMMAND: monitor
// ============================================================================ // ============================================================================
static struct { static struct {
struct arg_lit *start, *stop, *status, *save, *reload; struct arg_lit *start, *stop, *status, *save, *reload;
struct arg_lit *clear_nvs;
struct arg_int *channel; struct arg_int *channel;
struct arg_lit *help; struct arg_lit *help;
struct arg_end *end; struct arg_end *end;
@ -101,6 +247,13 @@ static int cmd_monitor(int argc, char **argv) {
return 0; return 0;
} }
if (mon_args.clear_nvs->count > 0) {
wifi_ctl_param_clear();
printf("Monitor config cleared (Defaulting to Ch 6).\n");
end_cmd();
return 0;
}
if (mon_args.reload->count > 0) { if (mon_args.reload->count > 0) {
wifi_ctl_param_reload(); wifi_ctl_param_reload();
printf("Config reloaded from NVS.\n"); printf("Config reloaded from NVS.\n");
@ -132,12 +285,28 @@ static int cmd_monitor(int argc, char **argv) {
printf(" Mode: %s\n", (mode == WIFI_CTL_MODE_MONITOR) ? "MONITOR" : "STATION"); printf(" Mode: %s\n", (mode == WIFI_CTL_MODE_MONITOR) ? "MONITOR" : "STATION");
printf(" Active: Ch %d\n", (mode == WIFI_CTL_MODE_MONITOR) ? wifi_ctl_get_monitor_channel() : 0); printf(" Active: Ch %d\n", (mode == WIFI_CTL_MODE_MONITOR) ? wifi_ctl_get_monitor_channel() : 0);
printf(" Staged: Ch %d\n", wifi_ctl_param_get_monitor_channel()); printf(" Staged: Ch %d\n", wifi_ctl_param_get_monitor_channel());
printf(" Frames: %" PRIu32 "\n", wifi_ctl_get_monitor_frame_count());
} }
end_cmd(); // Update Prompt end_cmd();
return 0; return 0;
} }
static void register_monitor_cmd(void) {
mon_args.start = arg_lit0(NULL, "start", "Start");
mon_args.stop = arg_lit0(NULL, "stop", "Stop");
mon_args.status = arg_lit0(NULL, "status", "Status");
mon_args.save = arg_lit0(NULL, "save", "Save");
mon_args.reload = arg_lit0(NULL, "reload", "Reload");
mon_args.clear_nvs = arg_lit0(NULL, "clear-nvs", "Clear NVS");
mon_args.channel = arg_int0("c", "channel", "<n>", "Chan");
mon_args.help = arg_lit0("h", "help", "Help");
mon_args.end = arg_end(20);
const esp_console_cmd_t cmd = { .command = "monitor", .help = "Monitor Mode", .func = &cmd_monitor, .argtable = &mon_args };
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
// ============================================================================ // ============================================================================
// COMMAND: scan // COMMAND: scan
// ============================================================================ // ============================================================================
@ -159,6 +328,10 @@ static int cmd_scan(int argc, char **argv) {
} }
printf("Starting WiFi Scan...\n"); printf("Starting WiFi Scan...\n");
// Force STA mode to allow scanning
wifi_ctl_switch_to_sta(WIFI_BW_HT20);
wifi_scan_config_t scan_config = { .show_hidden = true }; wifi_scan_config_t scan_config = { .show_hidden = true };
esp_err_t err = esp_wifi_scan_start(&scan_config, true); esp_err_t err = esp_wifi_scan_start(&scan_config, true);
if (err != ESP_OK) { if (err != ESP_OK) {
@ -185,39 +358,6 @@ static int cmd_scan(int argc, char **argv) {
return 0; return 0;
} }
// Registration
static void register_iperf_cmd(void) {
iperf_args.start = arg_lit0(NULL, "start", "Start");
iperf_args.stop = arg_lit0(NULL, "stop", "Stop");
iperf_args.status = arg_lit0(NULL, "status", "Status");
iperf_args.save = arg_lit0(NULL, "save", "Save");
iperf_args.reload = arg_lit0(NULL, "reload", "Reload");
iperf_args.ip = arg_str0("c", "client", "<ip>", "IP");
iperf_args.port = arg_int0("p", "port", "<port>", "Port");
iperf_args.pps = arg_int0(NULL, "pps", "<n>", "PPS");
iperf_args.len = arg_int0(NULL, "len", "<bytes>", "Len");
iperf_args.burst = arg_int0(NULL, "burst", "<count>", "Burst");
iperf_args.help = arg_lit0("h", "help", "Help");
iperf_args.end = arg_end(20);
const esp_console_cmd_t cmd = { .command = "iperf", .help = "Traffic Gen", .func = &cmd_iperf, .argtable = &iperf_args };
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
static void register_monitor_cmd(void) {
mon_args.start = arg_lit0(NULL, "start", "Start");
mon_args.stop = arg_lit0(NULL, "stop", "Stop");
mon_args.status = arg_lit0(NULL, "status", "Status");
mon_args.save = arg_lit0(NULL, "save", "Save");
mon_args.reload = arg_lit0(NULL, "reload", "Reload");
mon_args.channel = arg_int0("c", "channel", "<n>", "Chan");
mon_args.help = arg_lit0("h", "help", "Help");
mon_args.end = arg_end(20);
const esp_console_cmd_t cmd = { .command = "monitor", .help = "Monitor Mode", .func = &cmd_monitor, .argtable = &mon_args };
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
static void register_scan_cmd(void) { static void register_scan_cmd(void) {
scan_args.help = arg_lit0("h", "help", "Help"); scan_args.help = arg_lit0("h", "help", "Help");
scan_args.end = arg_end(1); scan_args.end = arg_end(1);
@ -225,11 +365,86 @@ static void register_scan_cmd(void) {
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
} }
// Don't forget to keep your existing register_wifi_cmd ... // ============================================================================
// COMMAND: wifi_config
// ============================================================================
static struct {
struct arg_str *ssid;
struct arg_str *pass;
struct arg_str *ip;
struct arg_lit *dhcp;
struct arg_lit *clear_nvs;
struct arg_lit *help;
struct arg_end *end;
} wifi_args;
static int cmd_wifi_config(int argc, char **argv) {
int nerrors = arg_parse(argc, argv, (void **)&wifi_args);
if (nerrors > 0) {
arg_print_errors(stderr, wifi_args.end, argv[0]);
return 1;
}
if (wifi_args.help->count > 0) {
printf("Usage: wifi_config -s <ssid> [-p <pass>] [--clear-nvs]\n");
return 0;
}
if (wifi_args.clear_nvs->count > 0) {
wifi_cfg_clear_credentials();
printf("WiFi Credentials CLEARED from NVS.\n");
return 0;
}
if (wifi_args.ssid->count == 0) {
printf("Error: SSID is required (-s)\n");
return 1;
}
const char* ssid = wifi_args.ssid->sval[0];
const char* pass = (wifi_args.pass->count > 0) ? wifi_args.pass->sval[0] : "";
const char* ip = (wifi_args.ip->count > 0) ? wifi_args.ip->sval[0] : NULL;
bool dhcp = (wifi_args.dhcp->count > 0);
printf("Saving WiFi Config: SSID='%s' DHCP=%d\n", ssid, dhcp);
wifi_cfg_set_credentials(ssid, pass);
if (ip) {
char mask[] = "255.255.255.0";
char gw[32];
strlcpy(gw, ip, sizeof(gw));
char *last_dot = strrchr(gw, '.');
if (last_dot) strcpy(last_dot, ".1");
wifi_cfg_set_static_ip(ip, mask, gw);
wifi_cfg_set_dhcp(false);
} else {
wifi_cfg_set_dhcp(dhcp);
}
printf("Config saved. Rebooting to apply...\n");
esp_restart();
return 0;
}
static void register_wifi_cmd(void) {
wifi_args.ssid = arg_str0("s", "ssid", "<ssid>", "SSID");
wifi_args.pass = arg_str0("p", "password", "<pass>", "Pass");
wifi_args.ip = arg_str0("i", "ip", "<ip>", "Static IP");
wifi_args.dhcp = arg_lit0("d", "dhcp", "Enable DHCP");
wifi_args.clear_nvs = arg_lit0(NULL, "clear-nvs", "Clear NVS");
wifi_args.help = arg_lit0("h", "help", "Help");
wifi_args.end = arg_end(20);
const esp_console_cmd_t cmd = { .command = "wifi_config", .help = "Configure WiFi", .func = &cmd_wifi_config, .argtable = &wifi_args };
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
void app_console_register_commands(void) { void app_console_register_commands(void) {
register_iperf_cmd(); register_iperf_cmd();
register_monitor_cmd(); register_monitor_cmd();
register_scan_cmd(); register_scan_cmd();
// register_wifi_cmd(); // Ensure this is linked register_wifi_cmd();
register_nvs_cmd();
} }

View File

@ -121,6 +121,25 @@ static void trim_whitespace(char *str) {
*(end+1) = 0; *(end+1) = 0;
} }
// Clear NVS
void iperf_param_clear(void) {
nvs_handle_t h;
if (nvs_open("storage", NVS_READWRITE, &h) == ESP_OK) {
nvs_erase_key(h, NVS_KEY_IPERF_PPS);
nvs_erase_key(h, NVS_KEY_IPERF_BURST);
nvs_erase_key(h, NVS_KEY_IPERF_LEN);
nvs_erase_key(h, NVS_KEY_IPERF_PORT);
nvs_erase_key(h, NVS_KEY_IPERF_DST_IP);
nvs_commit(h);
nvs_close(h);
ESP_LOGI(TAG, "iPerf NVS configuration cleared.");
}
// Reset RAM to defaults to match the "Empty" NVS state
set_defaults(&s_staging_cfg);
}
void iperf_param_init(void) { void iperf_param_init(void) {
if (s_staging_initialized) return; if (s_staging_initialized) return;

View File

@ -63,4 +63,6 @@ void iperf_print_status(void);
// Utils // Utils
void iperf_init_led(led_strip_handle_t handle); void iperf_init_led(led_strip_handle_t handle);
// Erase NVS and reset RAM defaults
void iperf_param_clear(void);
#endif #endif

View File

@ -52,6 +52,30 @@ void wifi_cfg_set_dhcp(bool enable) {
nvs_write_u8("dhcp", enable ? 1 : 0); nvs_write_u8("dhcp", enable ? 1 : 0);
} }
// --- Clearing ---
void wifi_cfg_clear_credentials(void) {
nvs_handle_t h;
if (nvs_open("netcfg", NVS_READWRITE, &h) == ESP_OK) {
nvs_erase_key(h, "ssid");
nvs_erase_key(h, "pass");
nvs_erase_key(h, "ip");
nvs_erase_key(h, "mask");
nvs_erase_key(h, "gw");
nvs_erase_key(h, "dhcp");
nvs_commit(h);
nvs_close(h);
}
}
void wifi_cfg_clear_monitor_channel(void) {
nvs_handle_t h;
if (nvs_open("netcfg", NVS_READWRITE, &h) == ESP_OK) {
nvs_erase_key(h, "mon_ch");
nvs_commit(h);
nvs_close(h);
}
}
// --- Init & Load --- // --- Init & Load ---
void wifi_cfg_init(void) { void wifi_cfg_init(void) {

View File

@ -1,3 +1,4 @@
#ifndef WIFI_CFG_H #ifndef WIFI_CFG_H
#define WIFI_CFG_H #define WIFI_CFG_H
@ -29,6 +30,10 @@ void wifi_cfg_set_dhcp(bool enable);
// Returns true if NVS was actually updated, false if values were identical // Returns true if NVS was actually updated, false if values were identical
bool wifi_cfg_set_monitor_channel(uint8_t channel); bool wifi_cfg_set_monitor_channel(uint8_t channel);
// --- Clearing ---
void wifi_cfg_clear_credentials(void);
void wifi_cfg_clear_monitor_channel(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -4,15 +4,14 @@
#include "esp_log.h" #include "esp_log.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "inttypes.h" #include "inttypes.h"
#include "wifi_cfg.h" // Required for apply_from_nvs
// Dependencies // Dependencies
#include "iperf.h" #include "iperf.h"
#include "status_led.h" #include "status_led.h"
#include "wifi_monitor.h" #include "wifi_monitor.h"
#include "gps_sync.h" #include "gps_sync.h"
#include "wifi_cfg.h"
// 1. GUARDED INCLUDE
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED #ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_manager.h" #include "csi_manager.h"
#endif #endif
@ -21,15 +20,15 @@ static const char *TAG = "WIFI_CTL";
static wifi_ctl_mode_t s_current_mode = WIFI_CTL_MODE_STA; static wifi_ctl_mode_t s_current_mode = WIFI_CTL_MODE_STA;
static uint8_t s_monitor_channel = 6; static uint8_t s_monitor_channel = 6;
static uint8_t s_monitor_channel_staging = 6;
static bool s_monitor_enabled = false; static bool s_monitor_enabled = false;
static uint32_t s_monitor_frame_count = 0; static uint32_t s_monitor_frame_count = 0;
static TaskHandle_t s_monitor_stats_task_handle = NULL; static TaskHandle_t s_monitor_stats_task_handle = NULL;
static uint8_t s_monitor_channel_staging = 6; //RAM Staging Variable
// --- Helper: Log Collapse Events --- // --- Helper: Log Collapse Events ---
static void log_collapse_event(float nav_duration_us, int rssi, int retry) { static void log_collapse_event(float nav_duration_us, int rssi, int retry) {
gps_timestamp_t ts = gps_get_timestamp(); gps_timestamp_t ts = gps_get_timestamp();
// CSV Format: COLLAPSE,MonoMS,GpsMS,Synced,Duration,RSSI,Retry
printf("COLLAPSE,%" PRIi64 ",%" PRIi64 ",%d,%.2f,%d,%d\n", printf("COLLAPSE,%" PRIi64 ",%" PRIi64 ",%d,%.2f,%d,%d\n",
ts.monotonic_ms, ts.monotonic_ms,
ts.gps_ms, ts.gps_ms,
@ -43,12 +42,9 @@ static void log_collapse_event(float nav_duration_us, int rssi, int retry) {
static void monitor_frame_callback(const wifi_frame_info_t *frame, const uint8_t *payload, uint16_t len) { static void monitor_frame_callback(const wifi_frame_info_t *frame, const uint8_t *payload, uint16_t len) {
s_monitor_frame_count++; s_monitor_frame_count++;
// Check for Collapse conditions (High NAV + Retry)
if (frame->retry && frame->duration_id > 5000) { if (frame->retry && frame->duration_id > 5000) {
log_collapse_event((float)frame->duration_id, frame->rssi, frame->retry); log_collapse_event((float)frame->duration_id, frame->rssi, frame->retry);
} }
if (frame->duration_id > 30000) { if (frame->duration_id > 30000) {
ESP_LOGW("MONITOR", "⚠️ VERY HIGH NAV: %u us", frame->duration_id); ESP_LOGW("MONITOR", "⚠️ VERY HIGH NAV: %u us", frame->duration_id);
} }
@ -71,33 +67,42 @@ static void monitor_stats_task(void *arg) {
static void auto_monitor_task_func(void *arg) { static void auto_monitor_task_func(void *arg) {
uint8_t channel = (uint8_t)(uintptr_t)arg; uint8_t channel = (uint8_t)(uintptr_t)arg;
ESP_LOGI(TAG, "Waiting for WiFi connection before switching to monitor mode..."); ESP_LOGI(TAG, "Waiting for WiFi connection before switching to monitor mode...");
// Wait until LED indicates connected
while (status_led_get_state() != LED_STATE_CONNECTED) { while (status_led_get_state() != LED_STATE_CONNECTED) {
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
} }
ESP_LOGI(TAG, "WiFi connected, waiting for GPS sync (2s)..."); ESP_LOGI(TAG, "WiFi connected, waiting for GPS sync (2s)...");
vTaskDelay(pdMS_TO_TICKS(2000)); vTaskDelay(pdMS_TO_TICKS(2000));
ESP_LOGI(TAG, "Auto-switching to MONITOR mode on channel %d...", channel); ESP_LOGI(TAG, "Auto-switching to MONITOR mode on channel %d...", channel);
wifi_ctl_switch_to_monitor(channel, WIFI_BW_HT20); wifi_ctl_switch_to_monitor(channel, WIFI_BW_HT20);
vTaskDelete(NULL); vTaskDelete(NULL);
} }
// --- API Implementation --- // --- API Implementation ---
void wifi_ctl_init(void) { void wifi_ctl_init(void) {
s_current_mode = WIFI_CTL_MODE_STA; s_current_mode = WIFI_CTL_MODE_STA;
s_monitor_enabled = false; s_monitor_enabled = false;
s_monitor_frame_count = 0; s_monitor_frame_count = 0;
// Load Initial Staging from NVS // -------------------------------------------------------------
// CRITICAL FIX: Initialize the WiFi Driver via Config
// This calls esp_wifi_init() and esp_wifi_start()
// -------------------------------------------------------------
if (!wifi_cfg_apply_from_nvs()) {
ESP_LOGW(TAG, "No saved WiFi config found, driver initialized in defaults.");
} else {
ESP_LOGI(TAG, "WiFi driver initialized from NVS.");
}
// Load Staging Params
char mode_ignored[16]; char mode_ignored[16];
wifi_cfg_get_mode(mode_ignored, &s_monitor_channel_staging); wifi_cfg_get_mode(mode_ignored, &s_monitor_channel_staging);
if (s_monitor_channel_staging == 0) s_monitor_channel_staging = 6; if (s_monitor_channel_staging == 0) s_monitor_channel_staging = 6;
} }
// --- Parameter Management ---
void wifi_ctl_param_set_monitor_channel(uint8_t channel) { void wifi_ctl_param_set_monitor_channel(uint8_t channel) {
if (channel >= 1 && channel <= 14) { if (channel >= 1 && channel <= 14) {
s_monitor_channel_staging = channel; s_monitor_channel_staging = channel;
@ -127,8 +132,10 @@ void wifi_ctl_param_reload(void) {
bool wifi_ctl_param_is_unsaved(void) { bool wifi_ctl_param_is_unsaved(void) {
return wifi_cfg_monitor_channel_is_unsaved(s_monitor_channel_staging); return wifi_cfg_monitor_channel_is_unsaved(s_monitor_channel_staging);
} }
// --- Actions ---
esp_err_t wifi_ctl_switch_to_monitor(uint8_t channel_override, wifi_bandwidth_t bandwidth) { esp_err_t wifi_ctl_switch_to_monitor(uint8_t channel_override, wifi_bandwidth_t bandwidth) {
// If override is 0, use Staging
uint8_t channel = (channel_override > 0) ? channel_override : s_monitor_channel_staging; uint8_t channel = (channel_override > 0) ? channel_override : s_monitor_channel_staging;
if (s_current_mode == WIFI_CTL_MODE_MONITOR && s_monitor_channel == channel) { if (s_current_mode == WIFI_CTL_MODE_MONITOR && s_monitor_channel == channel) {
@ -136,7 +143,6 @@ esp_err_t wifi_ctl_switch_to_monitor(uint8_t channel_override, wifi_bandwidth_t
return ESP_OK; return ESP_OK;
} }
// Monitor mode typically requires 20MHz
if (bandwidth != WIFI_BW_HT20) { if (bandwidth != WIFI_BW_HT20) {
ESP_LOGW(TAG, "Forcing bandwidth to 20MHz for monitor mode"); ESP_LOGW(TAG, "Forcing bandwidth to 20MHz for monitor mode");
bandwidth = WIFI_BW_HT20; bandwidth = WIFI_BW_HT20;
@ -144,22 +150,17 @@ esp_err_t wifi_ctl_switch_to_monitor(uint8_t channel_override, wifi_bandwidth_t
ESP_LOGI(TAG, "Switching to MONITOR MODE (Ch %d)", channel); ESP_LOGI(TAG, "Switching to MONITOR MODE (Ch %d)", channel);
// 1. Stop high-level apps
iperf_stop(); iperf_stop();
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
// 2. Disable CSI (hardware conflict)
// 2. GUARDED CALL
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED #ifdef CONFIG_ESP_WIFI_CSI_ENABLED
csi_mgr_disable(); csi_mgr_disable();
#endif #endif
// 3. Teardown Station
esp_wifi_disconnect(); esp_wifi_disconnect();
esp_wifi_stop(); esp_wifi_stop();
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
// 4. Re-init in NULL/Promiscuous Mode
esp_wifi_set_mode(WIFI_MODE_NULL); esp_wifi_set_mode(WIFI_MODE_NULL);
if (wifi_monitor_init(channel, monitor_frame_callback) != ESP_OK) { if (wifi_monitor_init(channel, monitor_frame_callback) != ESP_OK) {
@ -174,7 +175,6 @@ esp_err_t wifi_ctl_switch_to_monitor(uint8_t channel_override, wifi_bandwidth_t
return ESP_FAIL; return ESP_FAIL;
} }
// 5. Update State
s_monitor_enabled = true; s_monitor_enabled = true;
s_current_mode = WIFI_CTL_MODE_MONITOR; s_current_mode = WIFI_CTL_MODE_MONITOR;
s_monitor_channel = channel; s_monitor_channel = channel;
@ -195,40 +195,45 @@ esp_err_t wifi_ctl_switch_to_sta(wifi_band_mode_t band_mode) {
ESP_LOGI(TAG, "Switching to STA MODE"); ESP_LOGI(TAG, "Switching to STA MODE");
// 1. Stop Monitor Tasks
if (s_monitor_stats_task_handle != NULL) { if (s_monitor_stats_task_handle != NULL) {
vTaskDelete(s_monitor_stats_task_handle); vTaskDelete(s_monitor_stats_task_handle);
s_monitor_stats_task_handle = NULL; s_monitor_stats_task_handle = NULL;
} }
// 2. Stop Monitor Driver
if (s_monitor_enabled) { if (s_monitor_enabled) {
wifi_monitor_stop(); wifi_monitor_stop();
s_monitor_enabled = false; s_monitor_enabled = false;
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
} }
// 3. Re-enable Station Mode
esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_mode(WIFI_MODE_STA);
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
// 4. Configure & Connect
wifi_config_t wifi_config; wifi_config_t wifi_config;
esp_wifi_get_config(WIFI_IF_STA, &wifi_config); esp_wifi_get_config(WIFI_IF_STA, &wifi_config);
wifi_config.sta.channel = 0; // Auto channel scan wifi_config.sta.channel = 0;
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
esp_wifi_start(); esp_wifi_start();
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
esp_wifi_connect(); esp_wifi_connect();
// 5. Update State
s_current_mode = WIFI_CTL_MODE_STA; s_current_mode = WIFI_CTL_MODE_STA;
status_led_set_state(LED_STATE_WAITING); status_led_set_state(LED_STATE_WAITING);
return ESP_OK; return ESP_OK;
} }
void wifi_ctl_param_clear(void) {
// 1. Erase NVS
wifi_cfg_clear_monitor_channel();
// 2. Reset RAM Staging to Default (6)
s_monitor_channel_staging = 6;
ESP_LOGI(TAG, "Monitor configuration cleared (Defaulting to Ch 6).");
}
void wifi_ctl_auto_monitor_start(uint8_t channel) { void wifi_ctl_auto_monitor_start(uint8_t channel) {
xTaskCreate(auto_monitor_task_func, "auto_monitor", 4096, (void*)(uintptr_t)channel, 5, NULL); xTaskCreate(auto_monitor_task_func, "auto_monitor", 4096, (void*)(uintptr_t)channel, 5, NULL);
} }

View File

@ -15,6 +15,8 @@ typedef enum {
void wifi_ctl_init(void); void wifi_ctl_init(void);
void wifi_ctl_param_clear(void);
// --- Parameter Management (Set/Get/Save/Read) --- // --- Parameter Management (Set/Get/Save/Read) ---
void wifi_ctl_param_set_monitor_channel(uint8_t channel); void wifi_ctl_param_set_monitor_channel(uint8_t channel);
uint8_t wifi_ctl_param_get_monitor_channel(void); uint8_t wifi_ctl_param_get_monitor_channel(void);

View File

@ -100,7 +100,7 @@ void app_main(void) {
csi_mgr_init(); csi_mgr_init();
#endif #endif
// 4. Initialize WiFi Controller (Loads config from NVS automatically) // 4. Initialize WiFi Controller & iPerf
wifi_ctl_init(); wifi_ctl_init();
iperf_param_init(); iperf_param_init();
@ -108,7 +108,9 @@ void app_main(void) {
esp_console_repl_t *repl = NULL; esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
// CRITICAL: Point the prompt to our mutable buffer // ---------------------------------------------------------
// CRITICAL FIX: Use the mutable buffer, NOT a string literal
// ---------------------------------------------------------
repl_config.prompt = s_cli_prompt; repl_config.prompt = s_cli_prompt;
repl_config.max_cmdline_length = 1024; repl_config.max_cmdline_length = 1024;
@ -121,7 +123,6 @@ void app_main(void) {
app_console_register_commands(); app_console_register_commands();
// 7. Initial Prompt State Check // 7. Initial Prompt State Check
// (Sets it to "esp32*>" immediately if NVS is empty/dirty on boot)
app_console_update_prompt(); app_console_update_prompt();
// 8. Start Shell // 8. Start Shell
@ -130,6 +131,5 @@ void app_main(void) {
printf(" | Type 'help' for commands |\n"); printf(" | Type 'help' for commands |\n");
printf(" ==================================================\n"); printf(" ==================================================\n");
// This function runs the REPL loop and does not return
ESP_ERROR_CHECK(esp_console_start_repl(repl)); ESP_ERROR_CHECK(esp_console_start_repl(repl));
} }