if def work

This commit is contained in:
Bob 2025-12-11 09:35:32 -08:00
parent 2a6a2bf425
commit 960bfc49c1
7 changed files with 75 additions and 23 deletions

View File

@ -8,10 +8,14 @@
// Dependencies // Dependencies
#include "wifi_controller.h" #include "wifi_controller.h"
#include "csi_manager.h"
#include "status_led.h" #include "status_led.h"
#include "gps_sync.h" #include "gps_sync.h"
// 1. GUARDED INCLUDE
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_manager.h"
#endif
// --- Command Handlers --- // --- Command Handlers ---
static int cmd_mode_monitor(int argc, char **argv) { static int cmd_mode_monitor(int argc, char **argv) {
@ -53,8 +57,13 @@ static int cmd_mode_status(int argc, char **argv) {
printf("GPS synced: %s\n", gps_is_synced() ? "Yes" : "No"); printf("GPS synced: %s\n", gps_is_synced() ? "Yes" : "No");
if (mode == WIFI_CTL_MODE_STA) { if (mode == WIFI_CTL_MODE_STA) {
// 2. GUARDED STATUS PRINT
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
printf("CSI Enabled: %s\n", csi_mgr_is_enabled() ? "Yes" : "No"); printf("CSI Enabled: %s\n", csi_mgr_is_enabled() ? "Yes" : "No");
printf("CSI Packets: %lu\n", (unsigned long)csi_mgr_get_packet_count()); printf("CSI Packets: %lu\n", (unsigned long)csi_mgr_get_packet_count());
#else
printf("CSI Support: Disabled in build\n");
#endif
} else { } else {
printf("Monitor Ch: %d\n", wifi_ctl_get_monitor_channel()); printf("Monitor Ch: %d\n", wifi_ctl_get_monitor_channel());
printf("Captured: %lu frames\n", (unsigned long)wifi_ctl_get_monitor_frame_count()); printf("Captured: %lu frames\n", (unsigned long)wifi_ctl_get_monitor_frame_count());
@ -67,8 +76,15 @@ static int cmd_csi_dump(int argc, char **argv) {
printf("Error: CSI only available in STA mode\n"); printf("Error: CSI only available in STA mode\n");
return 1; return 1;
} }
// 3. GUARDED DUMP ACTION
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
printf("Scheduling CSI dump...\n"); printf("Scheduling CSI dump...\n");
csi_mgr_schedule_dump(); csi_mgr_schedule_dump();
#else
printf("Error: CSI feature is disabled in this firmware build.\n");
#endif
return 0; return 0;
} }

View File

@ -1,5 +1,11 @@
set(srcs "")
if(CONFIG_ESP_WIFI_CSI_ENABLED)
list(APPEND srcs "csi_log.c")
endif()
idf_component_register( idf_component_register(
SRCS "csi_log.c" SRCS ${srcs}
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES esp_wifi esp_partition REQUIRES esp_wifi esp_partition
) )

View File

@ -1,4 +1,12 @@
idf_component_register(SRCS "csi_manager.c" # Define sources list
set(srcs "")
# Only add the source file if the feature is enabled in menuconfig
if(CONFIG_ESP_WIFI_CSI_ENABLED)
list(APPEND srcs "csi_manager.c")
endif()
idf_component_register(SRCS ${srcs}
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES esp_wifi freertos REQUIRES esp_wifi freertos
PRIV_REQUIRES csi_log log nvs_flash) PRIV_REQUIRES csi_log log nvs_flash)

View File

@ -209,7 +209,8 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) {
nvs_close(my_handle); nvs_close(my_handle);
} }
#if 0 #if defined(CONFIG_FREERTOS_USE_TRACE_FACILITY) && defined(CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS)
// Note: You must ensure CONFIG_FREERTOS_USE_TRACE_FACILITY and CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS are enabled in your menuconfig for vTaskList to work. If they aren't, this function will be empty or not compile. If you can't change menuconfig, let me know, and I can give you a simpler way to just check the current task's priority.
static void print_all_task_priorities(void) { static void print_all_task_priorities(void) {
char *task_list_buffer = malloc(1024); // Allocate buffer for list char *task_list_buffer = malloc(1024); // Allocate buffer for list
if (task_list_buffer) { if (task_list_buffer) {
@ -221,8 +222,6 @@ static void print_all_task_priorities(void) {
ESP_LOGE(TAG, "Failed to allocate buffer for task list"); ESP_LOGE(TAG, "Failed to allocate buffer for task list");
} }
} }
// Note: You must ensure CONFIG_FREERTOS_USE_TRACE_FACILITY and CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS are enabled in your menuconfig for vTaskList to work. If they aren't, this function will be empty or not compile. If you can't change menuconfig, let me know, and I can give you a simpler way to just check the current task's priority.
#endif #endif
static void __attribute__((unused)) socket_send(int sockfd, const uint8_t *buffer, int len) {} static void __attribute__((unused)) socket_send(int sockfd, const uint8_t *buffer, int len) {}
@ -261,7 +260,7 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl)
double total_mbps = (double)((uint64_t)burst_count * payload_len * 8 * (1000000.0 / pacing_period_us)) / 1000000.0; double total_mbps = (double)((uint64_t)burst_count * payload_len * 8 * (1000000.0 / pacing_period_us)) / 1000000.0;
ESP_LOGI(TAG, "Pacing: %" PRIu32 " pkts every %" PRIu32 " us (Approx %.2f Mbps)", burst_count, pacing_period_us, total_mbps); ESP_LOGI(TAG, "Pacing: %" PRIu32 " pkts every %" PRIu32 " us (Approx %.2f Mbps)", burst_count, pacing_period_us, total_mbps);
#if 0 #if defined(CONFIG_FREERTOS_USE_TRACE_FACILITY) && defined(CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS)
print_all_task_priorities(); print_all_task_priorities();
#endif #endif
// Force LED to Purple immediately // Force LED to Purple immediately

View File

@ -15,7 +15,11 @@
#include "wifi_cfg.h" #include "wifi_cfg.h"
#include "cmd_transport.h" // Now uses the transport component #include "cmd_transport.h" // Now uses the transport component
// GUARDED INCLUDE
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_manager.h" // For CSI enable/disable #include "csi_manager.h" // For CSI enable/disable
#endif
static const char *TAG = "wifi_cfg"; static const char *TAG = "wifi_cfg";
static esp_netif_t *sta_netif = NULL; static esp_netif_t *sta_netif = NULL;
@ -148,7 +152,6 @@ bool wifi_cfg_apply_from_nvs(void) {
} }
wifi_config_t wcfg = {0}; wifi_config_t wcfg = {0};
// Note: strlcpy takes the FULL buffer size and handles the -1 internally
strlcpy((char*)wcfg.sta.ssid, ssid, sizeof(wcfg.sta.ssid)); strlcpy((char*)wcfg.sta.ssid, ssid, sizeof(wcfg.sta.ssid));
strlcpy((char*)wcfg.sta.password, pass, sizeof(wcfg.sta.password)); strlcpy((char*)wcfg.sta.password, pass, sizeof(wcfg.sta.password));
wcfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; wcfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
@ -160,7 +163,7 @@ bool wifi_cfg_apply_from_nvs(void) {
esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_mode(WIFI_MODE_STA);
// Protocol selection based on target // Protocol selection
#if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 #if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6
wifi_protocols_t protocols = { wifi_protocols_t protocols = {
.ghz_2g = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N, .ghz_2g = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N,
@ -214,7 +217,11 @@ static void on_cfg_line(const char *line, char *ssid, char *pass, char *ip, char
if (strncmp(line, "MODE:",5)==0){ strncpy(mode, line+5, 15); mode[15]=0; return; } if (strncmp(line, "MODE:",5)==0){ strncpy(mode, line+5, 15); mode[15]=0; return; }
if (strncmp(line, "MON_CH:",7)==0){ *mon_ch = atoi(line+7); return; } if (strncmp(line, "MON_CH:",7)==0){ *mon_ch = atoi(line+7); return; }
if (strncmp(line, "DHCP:",5)==0){ *dhcp = atoi(line+5) ? true:false; return; } if (strncmp(line, "DHCP:",5)==0){ *dhcp = atoi(line+5) ? true:false; return; }
// GUARDED CSI PARSING
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
if (strncmp(line, "CSI:",4)==0){ *csi_enable = atoi(line+4) ? true:false; return; } if (strncmp(line, "CSI:",4)==0){ *csi_enable = atoi(line+4) ? true:false; return; }
#endif
} }
static bool wifi_cfg_cmd_handler(const char *line, cmd_reply_func_t reply_func, void *reply_ctx) { static bool wifi_cfg_cmd_handler(const char *line, cmd_reply_func_t reply_func, void *reply_ctx) {
@ -247,6 +254,8 @@ static bool wifi_cfg_cmd_handler(const char *line, cmd_reply_func_t reply_func,
save_cfg(ssid, pass, ip, mask, gw, dhcp, band, bw, powersave, mode, mon_ch); save_cfg(ssid, pass, ip, mask, gw, dhcp, band, bw, powersave, mode, mon_ch);
// GUARDED SAVE LOGIC
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
// Save CSI enable state // Save CSI enable state
esp_err_t err = csi_mgr_save_enable_state(csi_enable); esp_err_t err = csi_mgr_save_enable_state(csi_enable);
if (err == ESP_OK) { if (err == ESP_OK) {
@ -254,6 +263,7 @@ static bool wifi_cfg_cmd_handler(const char *line, cmd_reply_func_t reply_func,
} else { } else {
printf("Failed to save CSI state: %s\n", esp_err_to_name(err)); printf("Failed to save CSI state: %s\n", esp_err_to_name(err));
} }
#endif
if (reply_func) reply_func("OK\n", reply_ctx); if (reply_func) reply_func("OK\n", reply_ctx);
wifi_cfg_apply_from_nvs(); wifi_cfg_apply_from_nvs();

View File

@ -7,11 +7,15 @@
// Dependencies // Dependencies
#include "iperf.h" #include "iperf.h"
#include "csi_manager.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"
// 1. GUARDED INCLUDE
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_manager.h"
#endif
static const char *TAG = "WIFI_CTL"; 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;
@ -107,7 +111,10 @@ esp_err_t wifi_ctl_switch_to_monitor(uint8_t channel, wifi_bandwidth_t bandwidth
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
// 2. Disable CSI (hardware conflict) // 2. Disable CSI (hardware conflict)
// 2. GUARDED CALL
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
csi_mgr_disable(); csi_mgr_disable();
#endif
// 3. Teardown Station // 3. Teardown Station
esp_wifi_disconnect(); esp_wifi_disconnect();
@ -172,8 +179,6 @@ esp_err_t wifi_ctl_switch_to_sta(wifi_band_mode_t band_mode) {
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; // Auto channel scan
// Note: band_mode preference logic would go here if using scan filters
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));
@ -183,13 +188,10 @@ esp_err_t wifi_ctl_switch_to_sta(wifi_band_mode_t band_mode) {
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);
// Note: csi_mgr_enable is handled by GOT_IP event in main.c
return ESP_OK; return ESP_OK;
} }
void wifi_ctl_auto_monitor_start(uint8_t channel) { void wifi_ctl_auto_monitor_start(uint8_t channel) {
// Pass channel as value-in-pointer (it fits in void*)
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

@ -17,12 +17,16 @@
#include "status_led.h" #include "status_led.h"
#include "gps_sync.h" #include "gps_sync.h"
#include "wifi_cfg.h" #include "wifi_cfg.h"
#include "csi_log.h"
#include "csi_manager.h"
#include "wifi_controller.h" #include "wifi_controller.h"
#include "app_console.h" #include "app_console.h"
#include "iperf.h" #include "iperf.h"
// GUARDED INCLUDE: Prevents issues if csi_manager types aren't available
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_log.h"
#include "csi_manager.h"
#endif
static const char *TAG = "MAIN"; static const char *TAG = "MAIN";
// --- Event Handler ------------------------------------------------- // --- Event Handler -------------------------------------------------
@ -49,6 +53,8 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
status_led_set_state(LED_STATE_CONNECTED); status_led_set_state(LED_STATE_CONNECTED);
// GUARDED CSI STARTUP LOGIC
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
// Start App Services - Only enable CSI if configured in NVS // Start App Services - Only enable CSI if configured in NVS
if (csi_mgr_should_enable()) { if (csi_mgr_should_enable()) {
ESP_LOGI(TAG, "CSI enabled in config - starting capture"); ESP_LOGI(TAG, "CSI enabled in config - starting capture");
@ -57,6 +63,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
} else { } else {
ESP_LOGI(TAG, "CSI disabled in config - skipping capture"); ESP_LOGI(TAG, "CSI disabled in config - skipping capture");
} }
#endif
// Always start iperf server // Always start iperf server
iperf_cfg_t cfg = { .flag = IPERF_FLAG_SERVER | IPERF_FLAG_TCP, .sport = 5001 }; iperf_cfg_t cfg = { .flag = IPERF_FLAG_SERVER | IPERF_FLAG_TCP, .sport = 5001 };
@ -72,7 +79,10 @@ void app_main(void) {
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
// 2. Hardware/Driver Init // 2. Hardware/Driver Init
// GUARDED LOG INIT
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
ESP_ERROR_CHECK(csi_log_init()); ESP_ERROR_CHECK(csi_log_init());
#endif
status_led_init(RGB_LED_GPIO, HAS_RGB_LED); status_led_init(RGB_LED_GPIO, HAS_RGB_LED);
const gps_sync_config_t gps_cfg = { const gps_sync_config_t gps_cfg = {
@ -84,7 +94,10 @@ void app_main(void) {
gps_sync_init(&gps_cfg, true); gps_sync_init(&gps_cfg, true);
// 3. Subsystem Init // 3. Subsystem Init
// GUARDED MANAGER INIT
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
csi_mgr_init(); csi_mgr_init();
#endif
wifi_ctl_init(); wifi_ctl_init();
wifi_cfg_init(); // Starts cmd_transport (UART/USB) wifi_cfg_init(); // Starts cmd_transport (UART/USB)
@ -107,8 +120,11 @@ void app_main(void) {
// 6. Application Start // 6. Application Start
// Display CSI config status // Display CSI config status
// GUARDED PRINT
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
bool csi_enabled = csi_mgr_should_enable(); bool csi_enabled = csi_mgr_should_enable();
ESP_LOGI(TAG, "CSI Capture: %s", csi_enabled ? "ENABLED" : "DISABLED"); ESP_LOGI(TAG, "CSI Capture: %s", csi_enabled ? "ENABLED" : "DISABLED");
#endif
if (wifi_cfg_apply_from_nvs()) { if (wifi_cfg_apply_from_nvs()) {
status_led_set_state(LED_STATE_WAITING); status_led_set_state(LED_STATE_WAITING);
@ -123,8 +139,7 @@ void app_main(void) {
ESP_LOGW(TAG, "No Config Found. Waiting for setup..."); ESP_LOGW(TAG, "No Config Found. Waiting for setup...");
} }
// 7. Enter Console Loop (CRITICAL FIX) // 7. Enter Console Loop
// This keeps the main task alive to process UART commands from the Python script
ESP_LOGI(TAG, "Initialization complete. Entering console loop."); ESP_LOGI(TAG, "Initialization complete. Entering console loop.");
const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR; const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
@ -139,7 +154,6 @@ void app_main(void) {
} }
while (true) { while (true) {
// This blocks until a line is received from UART
char* line = linenoise(prompt); char* line = linenoise(prompt);
if (line == NULL) { /* Break on EOF or error */ if (line == NULL) { /* Break on EOF or error */
break; break;
@ -147,8 +161,6 @@ void app_main(void) {
if (strlen(line) > 0) { if (strlen(line) > 0) {
linenoiseHistoryAdd(line); linenoiseHistoryAdd(line);
// Try to run the command
int ret; int ret;
esp_err_t err = esp_console_run(line, &ret); esp_err_t err = esp_console_run(line, &ret);
if (err == ESP_ERR_NOT_FOUND) { if (err == ESP_ERR_NOT_FOUND) {
@ -159,7 +171,6 @@ void app_main(void) {
printf("Internal error: %s\n", esp_err_to_name(err)); printf("Internal error: %s\n", esp_err_to_name(err));
} }
} }
linenoiseFree(line); linenoiseFree(line);
} }
} }