/* * cmd_iperf.c * * Copyright (c) 2025 Umber Networks & Robert McMahon * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #include #include "esp_log.h" #include "esp_console.h" #include "argtable3/argtable3.h" #include "arpa/inet.h" #include "iperf.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 (Dispatcher) // ============================================================================ static void print_iperf_usage(void) { printf("Usage: iperf [args]\n"); printf("Subcommands:\n"); printf(" start Start traffic generation\n"); printf(" stop Stop traffic generation\n"); printf(" status Show current statistics\n"); printf(" set Configure parameters (IP, Port, PPS, etc.)\n"); printf(" save Save configuration to NVS\n"); printf(" reload Reload configuration from NVS\n"); printf(" clear Reset configuration to defaults\n"); printf("\nType 'iperf --help' for details.\n"); } static int cmd_iperf(int argc, char **argv) { if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { print_iperf_usage(); return 0; } if (strcmp(argv[1], "start") == 0) return iperf_do_start(argc - 1, &argv[1]); if (strcmp(argv[1], "stop") == 0) return iperf_do_stop(argc - 1, &argv[1]); if (strcmp(argv[1], "status") == 0) return iperf_do_status(argc - 1, &argv[1]); if (strcmp(argv[1], "set") == 0) return iperf_do_set(argc - 1, &argv[1]); if (strcmp(argv[1], "save") == 0) return iperf_do_save(argc - 1, &argv[1]); 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]); printf("Unknown subcommand '%s'.\n", argv[1]); print_iperf_usage(); return 1; } // ---------------------------------------------------------------------------- // Sub-command: start // ---------------------------------------------------------------------------- static int iperf_do_start(int argc, char **argv) { iperf_start(); 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", "", "Destination IP"); set_args.port = arg_int0("p", "port", "", "Destination Port"); set_args.pps = arg_int0(NULL, "pps", "", "Packets Per Second"); set_args.len = arg_int0("l", "len", "", "Packet Length"); set_args.burst = arg_int0("b", "burst", "", "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) { const esp_console_cmd_t cmd = { .command = "iperf", .help = "Traffic Gen: start, stop, set, status", .hint = " [args]", .func = &cmd_iperf, .argtable = NULL }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); }