#include "app_console.h" #include "esp_console.h" #include "esp_log.h" #include "argtable3/argtable3.h" #include "wifi_cfg.h" #include "iperf.h" #include // ============================================================================ // COMMAND: iperf // ============================================================================ static struct { struct arg_lit *start; struct arg_lit *stop; struct arg_lit *status; struct arg_int *pps; 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 [start|stop|status] [--pps ]\n"); return 0; } if (iperf_args.stop->count > 0) { iperf_stop(); return 0; } if (iperf_args.pps->count > 0) { int val = iperf_args.pps->ival[0]; if (val > 0) { iperf_set_pps((uint32_t)val); } else { printf("Error: PPS must be > 0\n"); } return 0; } if (iperf_args.status->count > 0) { iperf_print_status(); return 0; } if (iperf_args.start->count > 0) { // Start using saved NVS config iperf_cfg_t cfg = { .time = 0 }; iperf_start(&cfg); return 0; } return 0; } static void register_iperf_cmd(void) { iperf_args.start = arg_lit0(NULL, "start", "Start iperf traffic"); iperf_args.stop = arg_lit0(NULL, "stop", "Stop iperf traffic"); iperf_args.status = arg_lit0(NULL, "status", "Show current statistics"); iperf_args.pps = arg_int0(NULL, "pps", "", "Set packets per second"); iperf_args.help = arg_lit0(NULL, "help", "Show help"); iperf_args.end = arg_end(20); const esp_console_cmd_t cmd = { .command = "iperf", .help = "Control iperf traffic generator", .hint = NULL, .func = &cmd_iperf, .argtable = &iperf_args }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } // ============================================================================ // COMMAND: wifi_config // ============================================================================ static struct { struct arg_str *ssid; struct arg_str *pass; struct arg_str *ip; struct arg_lit *dhcp; struct arg_lit *help; struct arg_end *end; } wifi_args; static int cmd_wifi_config(int argc, char **argv) { int nerrors = arg_parse(argc, argv, (void **)&wifi_args); if (nerrors > 0) { arg_print_errors(stderr, wifi_args.end, argv[0]); return 1; } if (wifi_args.help->count > 0) { printf("Usage: wifi_config -s -p [-i ] [-d]\n"); return 0; } if (wifi_args.ssid->count == 0) { printf("Error: SSID is required (-s)\n"); return 1; } const char* ssid = wifi_args.ssid->sval[0]; const char* pass = (wifi_args.pass->count > 0) ? wifi_args.pass->sval[0] : ""; const char* ip = (wifi_args.ip->count > 0) ? wifi_args.ip->sval[0] : NULL; bool dhcp = (wifi_args.dhcp->count > 0); printf("Saving WiFi Config: SSID='%s' DHCP=%d\n", ssid, dhcp); wifi_cfg_set_credentials(ssid, pass); if (ip) { char mask[] = "255.255.255.0"; char gw[32]; // FIXED: Use strlcpy instead of strncpy to prevent truncation warnings strlcpy(gw, ip, sizeof(gw)); char *last_dot = strrchr(gw, '.'); if (last_dot) strcpy(last_dot, ".1"); wifi_cfg_set_static_ip(ip, mask, gw); wifi_cfg_set_dhcp(false); } else { wifi_cfg_set_dhcp(dhcp); } printf("Config saved. Rebooting to apply...\n"); esp_restart(); return 0; } static void register_wifi_cmd(void) { wifi_args.ssid = arg_str0("s", "ssid", "", "WiFi SSID"); wifi_args.pass = arg_str0("p", "password", "", "WiFi Password"); wifi_args.ip = arg_str0("i", "ip", "", "Static IP"); wifi_args.dhcp = arg_lit0("d", "dhcp", "Enable DHCP"); wifi_args.help = arg_lit0("h", "help", "Show help"); wifi_args.end = arg_end(20); const esp_console_cmd_t cmd = { .command = "wifi_config", .help = "Configure WiFi credentials", .hint = NULL, .func = &cmd_wifi_config, .argtable = &wifi_args }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } void app_console_register_commands(void) { register_iperf_cmd(); register_wifi_cmd(); }