/* * 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" // ============================================================================ // COMMAND: iperf // ============================================================================ static struct { struct arg_lit *start, *stop, *status, *save, *reload; struct arg_lit *clear_nvs; 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"); 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 Set Dest IP\n"); printf(" --pps Set Packets Per Sec\n"); return 0; } // --- Actions: NVS Management --- if (iperf_args.clear_nvs->count > 0) { iperf_param_clear(); printf("iPerf Configuration cleared (Reset to defaults).\n"); app_console_update_prompt(); return 0; } if (iperf_args.reload->count > 0) { iperf_param_init(); // Force re-read printf("Configuration reloaded from NVS.\n"); } // --- Actions: Parameter Updates --- bool config_changed = false; iperf_cfg_t cfg; iperf_param_get(&cfg); // Get current staging 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; } 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"); iperf_args.port = arg_int0("p", "port", "", "Port"); iperf_args.pps = arg_int0(NULL, "pps", "", "PPS"); iperf_args.len = arg_int0(NULL, "len", "", "Len"); iperf_args.burst = arg_int0(NULL, "burst", "", "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)); }