use sub commands for iperf major command

This commit is contained in:
Robert McMahon 2025-12-22 12:27:12 -08:00
parent b4b40de64d
commit 9974174d5b
1 changed files with 178 additions and 90 deletions

View File

@ -30,7 +30,6 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "esp_log.h" #include "esp_log.h"
@ -40,112 +39,201 @@
#include "iperf.h" #include "iperf.h"
#include "app_console.h" #include "app_console.h"
// --- Forward Declarations ---
static int iperf_do_start(int argc, char **argv);
static int iperf_do_stop(int argc, char **argv);
static int iperf_do_status(int argc, char **argv);
static int iperf_do_set(int argc, char **argv);
static int iperf_do_save(int argc, char **argv);
static int iperf_do_reload(int argc, char **argv);
static int iperf_do_clear(int argc, char **argv);
// ============================================================================ // ============================================================================
// COMMAND: iperf // COMMAND: iperf (Dispatcher)
// ============================================================================ // ============================================================================
static struct { static void print_iperf_usage(void) {
struct arg_lit *start, *stop, *status, *save, *reload; printf("Usage: iperf <subcommand> [args]\n");
struct arg_lit *clear_nvs; printf("Subcommands:\n");
struct arg_str *ip; printf(" start Start traffic generation\n");
struct arg_int *port, *pps, *len, *burst; printf(" stop Stop traffic generation\n");
struct arg_lit *help; printf(" status Show current statistics\n");
struct arg_end *end; printf(" set Configure parameters (IP, Port, PPS, etc.)\n");
} iperf_args; printf(" save Save configuration to NVS\n");
printf(" reload Reload configuration from NVS\n");
printf(" clear Reset configuration to defaults\n");
printf("\nType 'iperf <subcommand> --help' for details.\n");
}
static int cmd_iperf(int argc, char **argv) { static int cmd_iperf(int argc, char **argv) {
int nerrors = arg_parse(argc, argv, (void **)&iperf_args); if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
if (nerrors > 0) { print_iperf_usage();
arg_print_errors(stderr, iperf_args.end, argv[0]);
return 1;
}
if (iperf_args.help->count > 0) {
printf("Usage: iperf [options]\n");
printf(" --start Start traffic\n");
printf(" --stop Stop traffic\n");
printf(" --save Save config to NVS\n");
printf(" --reload Reload config from NVS\n");
printf(" --clear-nvs Reset to defaults\n");
printf(" -c <ip> Set Dest IP\n");
printf(" --pps <n> Set Packets Per Sec\n");
return 0; return 0;
} }
// --- Actions: NVS Management --- if (strcmp(argv[1], "start") == 0) return iperf_do_start(argc - 1, &argv[1]);
if (iperf_args.clear_nvs->count > 0) { if (strcmp(argv[1], "stop") == 0) return iperf_do_stop(argc - 1, &argv[1]);
iperf_param_clear(); if (strcmp(argv[1], "status") == 0) return iperf_do_status(argc - 1, &argv[1]);
printf("iPerf Configuration cleared (Reset to defaults).\n"); if (strcmp(argv[1], "set") == 0) return iperf_do_set(argc - 1, &argv[1]);
app_console_update_prompt(); if (strcmp(argv[1], "save") == 0) return iperf_do_save(argc - 1, &argv[1]);
return 0; if (strcmp(argv[1], "reload") == 0) return iperf_do_reload(argc - 1, &argv[1]);
} if (strcmp(argv[1], "clear") == 0) return iperf_do_clear(argc - 1, &argv[1]);
if (iperf_args.reload->count > 0) { printf("Unknown subcommand '%s'.\n", argv[1]);
iperf_param_init(); // Force re-read print_iperf_usage();
printf("Configuration reloaded from NVS.\n"); return 1;
} }
// --- Actions: Parameter Updates --- // ----------------------------------------------------------------------------
bool config_changed = false; // Sub-command: start
iperf_cfg_t cfg; // ----------------------------------------------------------------------------
iperf_param_get(&cfg); // Get current staging static int iperf_do_start(int argc, char **argv) {
iperf_start();
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) {
if (iperf_args.pps->ival[0] > 0) {
cfg.target_pps = (uint32_t)iperf_args.pps->ival[0];
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");
}
}
// --- Actions: Execution ---
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();
app_console_update_prompt();
return 0; return 0;
} }
// ----------------------------------------------------------------------------
// Sub-command: stop
// ----------------------------------------------------------------------------
static int iperf_do_stop(int argc, char **argv) {
iperf_stop();
return 0;
}
// ----------------------------------------------------------------------------
// Sub-command: status
// ----------------------------------------------------------------------------
static int iperf_do_status(int argc, char **argv) {
iperf_print_status();
return 0;
}
// ----------------------------------------------------------------------------
// Sub-command: set (Configuration)
// ----------------------------------------------------------------------------
static struct {
struct arg_str *ip;
struct arg_int *port;
struct arg_int *pps;
struct arg_int *len;
struct arg_int *burst;
struct arg_lit *help;
struct arg_end *end;
} set_args;
static int iperf_do_set(int argc, char **argv) {
// REVERTED: Now uses standard iPerf syntax ("client" / "-c")
set_args.ip = arg_str0("c", "client", "<ip>", "Destination IP");
set_args.port = arg_int0("p", "port", "<port>", "Destination Port");
set_args.pps = arg_int0(NULL, "pps", "<n>", "Packets Per Second");
set_args.len = arg_int0("l", "len", "<bytes>", "Packet Length");
set_args.burst = arg_int0("b", "burst", "<count>", "Burst Count");
set_args.help = arg_lit0("h", "help", "Help");
set_args.end = arg_end(20);
int nerrors = arg_parse(argc, argv, (void **)&set_args);
if (nerrors > 0) {
// --- CUSTOM ERROR HINTING ---
// Check if user tried to use "--ip"
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "--ip") == 0) {
printf("Error: Invalid option '--ip'. Did you mean '--client' (or -c) to set the destination IP?\n");
return 1;
}
}
// Fallback to standard error
arg_print_errors(stderr, set_args.end, argv[0]);
return 1;
}
if (set_args.help->count > 0) {
printf("Usage: iperf set [options]\n");
arg_print_glossary(stdout, (void **)&set_args, " %-25s %s\n");
return 0;
}
iperf_cfg_t cfg;
iperf_param_get(&cfg);
bool changed = false;
if (set_args.ip->count > 0) {
cfg.dip = inet_addr(set_args.ip->sval[0]);
changed = true;
}
if (set_args.port->count > 0) {
cfg.dport = (uint16_t)set_args.port->ival[0];
changed = true;
}
if (set_args.len->count > 0) {
cfg.send_len = (uint32_t)set_args.len->ival[0];
changed = true;
}
if (set_args.burst->count > 0) {
cfg.burst_count = (uint32_t)set_args.burst->ival[0];
changed = true;
}
if (set_args.pps->count > 0) {
if (set_args.pps->ival[0] > 0) {
cfg.target_pps = (uint32_t)set_args.pps->ival[0];
changed = true;
} else {
printf("Error: PPS must be > 0.\n");
return 1;
}
}
if (changed) {
iperf_param_set(&cfg);
printf("Configuration updated (RAM only). Run 'iperf save' to persist.\n");
} else {
printf("No changes specified.\n");
}
return 0;
}
// ----------------------------------------------------------------------------
// Sub-command: save
// ----------------------------------------------------------------------------
static int iperf_do_save(int argc, char **argv) {
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");
}
return 0;
}
// ----------------------------------------------------------------------------
// Sub-command: reload
// ----------------------------------------------------------------------------
static int iperf_do_reload(int argc, char **argv) {
iperf_param_init(); // Force re-read from NVS
printf("Configuration reloaded from NVS.\n");
return 0;
}
// ----------------------------------------------------------------------------
// Sub-command: clear
// ----------------------------------------------------------------------------
static int iperf_do_clear(int argc, char **argv) {
iperf_param_clear();
printf("iPerf Configuration cleared (Reset to defaults).\n");
return 0;
}
// ----------------------------------------------------------------------------
// Registration
// ----------------------------------------------------------------------------
void register_iperf_cmd(void) { 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.clear_nvs = arg_lit0(NULL, "clear-nvs", "Clear NVS");
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 = { const esp_console_cmd_t cmd = {
.command = "iperf", .command = "iperf",
.help = "Traffic Gen", .help = "Traffic Gen: start, stop, set, status",
.hint = "<subcommand> [args]",
.func = &cmd_iperf, .func = &cmd_iperf,
.argtable = &iperf_args .argtable = NULL
}; };
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
} }