236 lines
8.1 KiB
C
236 lines
8.1 KiB
C
#include <string.h>
|
|
#include <arpa/inet.h>
|
|
#include "app_console.h"
|
|
#include "esp_console.h"
|
|
#include "esp_log.h"
|
|
#include "argtable3/argtable3.h"
|
|
#include "wifi_cfg.h"
|
|
#include "iperf.h"
|
|
#include "wifi_controller.h"
|
|
|
|
// Helper to refresh prompt at end of commands
|
|
static void end_cmd(void) {
|
|
app_console_update_prompt();
|
|
}
|
|
|
|
// ============================================================================
|
|
// COMMAND: iperf
|
|
// ============================================================================
|
|
static struct {
|
|
struct arg_lit *start, *stop, *status, *save, *reload;
|
|
struct arg_str *ip;
|
|
struct arg_int *port, *pps, *len, *burst;
|
|
struct arg_lit *help;
|
|
struct arg_end *end;
|
|
} iperf_args;
|
|
|
|
static int cmd_iperf(int argc, char **argv) {
|
|
int nerrors = arg_parse(argc, argv, (void **)&iperf_args);
|
|
if (nerrors > 0) {
|
|
arg_print_errors(stderr, iperf_args.end, argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
if (iperf_args.help->count > 0) {
|
|
printf("Usage: iperf [options]\n"); // ... (Shortened for brevity)
|
|
return 0;
|
|
}
|
|
|
|
if (iperf_args.reload->count > 0) {
|
|
iperf_param_init();
|
|
printf("Configuration reloaded from NVS.\n");
|
|
}
|
|
|
|
bool config_changed = false;
|
|
iperf_cfg_t cfg;
|
|
iperf_param_get(&cfg);
|
|
|
|
if (iperf_args.ip->count > 0) { cfg.dip = inet_addr(iperf_args.ip->sval[0]); config_changed = true; }
|
|
if (iperf_args.port->count > 0) { cfg.dport = (uint16_t)iperf_args.port->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.pps->count > 0) {
|
|
int pps = iperf_args.pps->ival[0];
|
|
if (pps > 0) {
|
|
cfg.target_pps = (uint32_t)pps; // Update directly
|
|
config_changed = true;
|
|
}
|
|
}
|
|
|
|
if (config_changed) {
|
|
iperf_param_set(&cfg);
|
|
printf("RAM configuration updated.\n");
|
|
}
|
|
|
|
if (iperf_args.save->count > 0) {
|
|
bool changed = false;
|
|
if (iperf_param_save(&changed) == ESP_OK) {
|
|
printf(changed ? "Configuration saved to NVS.\n" : "No changes to save (NVS matches RAM).\n");
|
|
} else {
|
|
printf("Error saving to NVS.\n");
|
|
}
|
|
}
|
|
|
|
if (iperf_args.stop->count > 0) iperf_stop();
|
|
if (iperf_args.start->count > 0) iperf_start();
|
|
if (iperf_args.status->count > 0) iperf_print_status();
|
|
|
|
end_cmd(); // Update Prompt
|
|
return 0;
|
|
}
|
|
|
|
// ============================================================================
|
|
// COMMAND: monitor
|
|
// ============================================================================
|
|
static struct {
|
|
struct arg_lit *start, *stop, *status, *save, *reload;
|
|
struct arg_int *channel;
|
|
struct arg_lit *help;
|
|
struct arg_end *end;
|
|
} mon_args;
|
|
|
|
static int cmd_monitor(int argc, char **argv) {
|
|
int nerrors = arg_parse(argc, argv, (void **)&mon_args);
|
|
if (nerrors > 0) {
|
|
arg_print_errors(stderr, mon_args.end, argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
if (mon_args.help->count > 0) {
|
|
printf("Usage: monitor [--start|--stop] [-c <ch>] [--save|--reload]\n");
|
|
return 0;
|
|
}
|
|
|
|
if (mon_args.reload->count > 0) {
|
|
wifi_ctl_param_reload();
|
|
printf("Config reloaded from NVS.\n");
|
|
}
|
|
|
|
if (mon_args.channel->count > 0) {
|
|
wifi_ctl_param_set_monitor_channel((uint8_t)mon_args.channel->ival[0]);
|
|
printf("Channel set to %d (RAM).\n", mon_args.channel->ival[0]);
|
|
}
|
|
|
|
if (mon_args.save->count > 0) {
|
|
if (wifi_ctl_param_save()) printf("Configuration saved to NVS.\n");
|
|
else printf("No changes to save (NVS matches RAM).\n");
|
|
}
|
|
|
|
if (mon_args.stop->count > 0) {
|
|
wifi_ctl_switch_to_sta(WIFI_BW_HT20);
|
|
printf("Switched to Station Mode.\n");
|
|
}
|
|
|
|
if (mon_args.start->count > 0) {
|
|
if (wifi_ctl_switch_to_monitor(0, WIFI_BW_HT20) == ESP_OK) printf("Monitor Mode Started.\n");
|
|
else printf("Failed to start Monitor Mode.\n");
|
|
}
|
|
|
|
if (mon_args.status->count > 0) {
|
|
wifi_ctl_mode_t mode = wifi_ctl_get_mode();
|
|
printf("MONITOR STATUS:\n");
|
|
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(" Staged: Ch %d\n", wifi_ctl_param_get_monitor_channel());
|
|
}
|
|
|
|
end_cmd(); // Update Prompt
|
|
return 0;
|
|
}
|
|
|
|
// ============================================================================
|
|
// COMMAND: scan
|
|
// ============================================================================
|
|
static struct {
|
|
struct arg_lit *help;
|
|
struct arg_end *end;
|
|
} scan_args;
|
|
|
|
static int cmd_scan(int argc, char **argv) {
|
|
int nerrors = arg_parse(argc, argv, (void **)&scan_args);
|
|
if (nerrors > 0) {
|
|
arg_print_errors(stderr, scan_args.end, argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
if (scan_args.help->count > 0) {
|
|
printf("Usage: scan\n");
|
|
return 0;
|
|
}
|
|
|
|
printf("Starting WiFi Scan...\n");
|
|
wifi_scan_config_t scan_config = { .show_hidden = true };
|
|
esp_err_t err = esp_wifi_scan_start(&scan_config, true);
|
|
if (err != ESP_OK) {
|
|
printf("Scan failed: %s\n", esp_err_to_name(err));
|
|
return 1;
|
|
}
|
|
|
|
uint16_t ap_count = 0;
|
|
esp_wifi_scan_get_ap_num(&ap_count);
|
|
printf("Found %d APs:\n", ap_count);
|
|
|
|
if (ap_count > 0) {
|
|
wifi_ap_record_t *ap_list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * ap_count);
|
|
if (ap_list) {
|
|
esp_wifi_scan_get_ap_records(&ap_count, ap_list);
|
|
printf("%-32s | %-4s | %-4s | %-3s\n", "SSID", "RSSI", "CH", "Auth");
|
|
printf("----------------------------------------------------------\n");
|
|
for (int i = 0; i < ap_count; i++) {
|
|
printf("%-32s | %-4d | %-4d | %d\n", (char *)ap_list[i].ssid, ap_list[i].rssi, ap_list[i].primary, ap_list[i].authmode);
|
|
}
|
|
free(ap_list);
|
|
}
|
|
}
|
|
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) {
|
|
scan_args.help = arg_lit0("h", "help", "Help");
|
|
scan_args.end = arg_end(1);
|
|
const esp_console_cmd_t cmd = { .command = "scan", .help = "WiFi Scan", .func = &cmd_scan, .argtable = &scan_args };
|
|
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
|
}
|
|
|
|
// Don't forget to keep your existing register_wifi_cmd ...
|
|
|
|
void app_console_register_commands(void) {
|
|
register_iperf_cmd();
|
|
register_monitor_cmd();
|
|
register_scan_cmd();
|
|
// register_wifi_cmd(); // Ensure this is linked
|
|
}
|