ESP32/main/main.c

154 lines
4.7 KiB
C

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_console.h"
// REMOVED: #include "linenoise/linenoise.h" <-- CAUSE OF CONFLICT
#include "nvs_flash.h"
#include "esp_netif.h"
#include "lwip/inet.h"
// Components
#include "board_config.h"
#include "status_led.h"
#include "gps_sync.h"
#include "wifi_cfg.h"
#include "wifi_controller.h"
#include "app_console.h"
#include "iperf.h"
// GUARDED INCLUDE
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_log.h"
#include "csi_manager.h"
#endif
static const char *TAG = "MAIN";
// --- 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);
}
}
}
// In event_handler function inside main.c
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);
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
if (csi_mgr_should_enable()) {
ESP_LOGI(TAG, "CSI enabled in config - starting capture");
csi_mgr_enable_async();
csi_mgr_schedule_dump();
} else {
ESP_LOGI(TAG, "CSI disabled in config - skipping capture");
}
#endif
// iperf_start() will fill this from NVS (including Dest IP and Role)
iperf_cfg_t cfg = { 0 };
iperf_start(&cfg);
}
}
// --- Main ----------------------------------------------------------
void app_main(void) {
// 1. System Init
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
// 2. Hardware/Driver Init
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
ESP_ERROR_CHECK(csi_log_init());
#endif
status_led_init(RGB_LED_GPIO, HAS_RGB_LED);
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);
// 3. Subsystem Init
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
csi_mgr_init();
#endif
wifi_ctl_init();
// THIS call starts the cmd_transport (UART listener task)
// which effectively replaces the manual console loop below.
wifi_cfg_init();
// 4. Console Registry Init (Still needed for registering commands)
esp_console_config_t console_config = {
.max_cmdline_args = 8,
.max_cmdline_length = 256,
};
ESP_ERROR_CHECK(esp_console_init(&console_config));
esp_console_register_help_command();
// Register App Commands
app_console_register_commands();
// 5. 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));
// 6. Application Start
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
bool csi_enabled = csi_mgr_should_enable();
ESP_LOGI(TAG, "CSI Capture: %s", csi_enabled ? "ENABLED" : "DISABLED");
#endif
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);
ESP_LOGW(TAG, "No Config Found. Waiting for setup...");
}
// 7. Keep Main Task Alive
// We removed linenoise because cmd_transport.c is already reading UART.
ESP_LOGI(TAG, "Initialization complete. Entering idle loop.");
while (true) {
// Just sleep forever. cmd_transport task handles input.
// main event loop task handles wifi events.
vTaskDelay(pdMS_TO_TICKS(1000));
}
}