#include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" #include "esp_console.h" #include "linenoise/linenoise.h" #include "nvs_flash.h" #include "esp_netif.h" #include "lwip/inet.h" // Components #include "iperf.h" #include "wifi_cfg.h" #include "csi_log.h" #include "gps_sync.h" #include "status_led.h" #include "board_config.h" #include "csi_manager.h" #include "wifi_controller.h" // <--- New Component static const char *TAG = "MAIN"; // --- Console Commands ---------------------------------------------- static int cmd_mode_monitor(int argc, char **argv) { int channel = wifi_ctl_get_monitor_channel(); if (argc > 1) channel = atoi(argv[1]); if (wifi_ctl_switch_to_monitor(channel, WIFI_BW_HT20) != ESP_OK) { printf("Failed to switch to monitor mode\n"); return 1; } return 0; } static int cmd_mode_sta(int argc, char **argv) { if (wifi_ctl_switch_to_sta(WIFI_BAND_MODE_AUTO) != ESP_OK) { printf("Failed to switch to STA mode\n"); return 1; } printf("Switching to STA mode...\n"); return 0; } static int cmd_mode_status(int argc, char **argv) { printf("\n=== WiFi Mode Status ===\n"); printf("Current mode: %s\n", wifi_ctl_get_mode() == WIFI_CTL_MODE_STA ? "STA" : "MONITOR"); printf("LED state: %d\n", status_led_get_state()); printf("GPS synced: %s\n", gps_is_synced() ? "Yes" : "No"); if (wifi_ctl_get_mode() == WIFI_CTL_MODE_STA) { printf("CSI Enabled: %s\n", csi_mgr_is_enabled() ? "Yes" : "No"); printf("CSI Packets: %lu\n", (unsigned long)csi_mgr_get_packet_count()); } else { printf("Monitor Channel: %d\n", wifi_ctl_get_monitor_channel()); printf("Frames Captured: %lu\n", (unsigned long)wifi_ctl_get_monitor_frame_count()); } return 0; } static int cmd_csi_dump(int argc, char **argv) { if (wifi_ctl_get_mode() != WIFI_CTL_MODE_STA) { printf("Error: CSI only available in STA mode\n"); return 1; } csi_mgr_schedule_dump(); return 0; } static void register_mode_commands(void) { const esp_console_cmd_t cmds[] = { { .command = "mode_monitor", .help = "Switch to monitor mode", .func = &cmd_mode_monitor }, { .command = "mode_sta", .help = "Switch to STA mode", .func = &cmd_mode_sta }, { .command = "mode_status", .help = "Show status", .func = &cmd_mode_status }, { .command = "csi_dump", .help = "Dump CSI data", .func = &cmd_csi_dump }, }; for(int i=0; i<4; i++) ESP_ERROR_CHECK(esp_console_cmd_register(&cmds[i])); } // --- Event Handler ------------------------------------------------- static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT) { if (event_id == WIFI_EVENT_STA_START) { if (wifi_ctl_get_mode() == WIFI_CTL_MODE_STA) { status_led_set_state(LED_STATE_WAITING); } } else if (event_id == WIFI_EVENT_STA_DISCONNECTED) { if (wifi_ctl_get_mode() == WIFI_CTL_MODE_STA) { status_led_set_state(LED_STATE_FAILED); } } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { if (wifi_ctl_get_mode() != WIFI_CTL_MODE_STA) return; ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); status_led_set_state(LED_STATE_CONNECTED); csi_mgr_enable_async(); iperf_cfg_t cfg = { .flag = IPERF_FLAG_SERVER | IPERF_FLAG_TCP, .sport = 5001 }; iperf_start(&cfg); csi_mgr_schedule_dump(); } } // --- Main ---------------------------------------------------------- void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); // Init Subsystems ESP_ERROR_CHECK(csi_log_init()); status_led_init(RGB_LED_GPIO, HAS_RGB_LED); csi_mgr_init(); wifi_ctl_init(); // Init GPS const gps_sync_config_t gps_cfg = { .uart_port = UART_NUM_1, .tx_pin = GPS_TX_PIN, .rx_pin = GPS_RX_PIN, .pps_pin = GPS_PPS_PIN, }; gps_sync_init(&gps_cfg, true); // Register Events ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL)); // Init Config & Console wifi_cfg_init(); setvbuf(stdin, NULL, _IONBF, 0); esp_console_config_t console_config = { .max_cmdline_args = 8, .max_cmdline_length = 256, }; ESP_ERROR_CHECK(esp_console_init(&console_config)); linenoiseSetMultiLine(1); esp_console_register_help_command(); register_mode_commands(); // Apply Config if (wifi_cfg_apply_from_nvs()) { status_led_set_state(LED_STATE_WAITING); char mode[16] = {0}; uint8_t mon_ch = 36; if (wifi_cfg_get_mode(mode, &mon_ch) && strcmp(mode, "MONITOR") == 0) { wifi_ctl_auto_monitor_start(mon_ch); } } else { status_led_set_state(LED_STATE_NO_CONFIG); } }