ESP32/components/app_console/app_console.c

121 lines
3.3 KiB
C

#include "app_console.h"
#include "esp_console.h"
#include "esp_log.h"
#include "argtable3/argtable3.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Dependencies
#include "wifi_controller.h"
#include "status_led.h"
#include "gps_sync.h"
// 1. GUARDED INCLUDE
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_manager.h"
#endif
// --- Command Handlers ---
static int cmd_mode_monitor(int argc, char **argv) {
// Default to current channel if not provided
int channel = wifi_ctl_get_monitor_channel();
if (argc > 1) {
channel = atoi(argv[1]);
if (channel < 1 || channel > 165) {
printf("Error: Invalid channel %d\n", channel);
return 1;
}
}
// Default bandwidth HT20 for monitor mode
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) {
// Simple switch to STA, auto band
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) {
wifi_ctl_mode_t mode = wifi_ctl_get_mode();
printf("\n=== WiFi Mode Status ===\n");
printf("Current mode: %s\n", 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 (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 Packets: %lu\n", (unsigned long)csi_mgr_get_packet_count());
#else
printf("CSI Support: Disabled in build\n");
#endif
} else {
printf("Monitor Ch: %d\n", wifi_ctl_get_monitor_channel());
printf("Captured: %lu frames\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;
}
// 3. GUARDED DUMP ACTION
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
printf("Scheduling CSI dump...\n");
csi_mgr_schedule_dump();
#else
printf("Error: CSI feature is disabled in this firmware build.\n");
#endif
return 0;
}
// --- Registration ---
void app_console_register_commands(void) {
const esp_console_cmd_t cmds[] = {
{
.command = "mode_monitor",
.help = "Switch to monitor mode (Usage: mode_monitor [channel])",
.func = &cmd_mode_monitor
},
{
.command = "mode_sta",
.help = "Switch to STA mode",
.func = &cmd_mode_sta
},
{
.command = "mode_status",
.help = "Show device status",
.func = &cmd_mode_status
},
{
.command = "csi_dump",
.help = "Dump collected CSI data to UART",
.func = &cmd_csi_dump
},
};
for (int i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++) {
ESP_ERROR_CHECK(esp_console_cmd_register(&cmds[i]));
}
}