diff --git a/components/iperf/iperf.c b/components/iperf/iperf.c index 6fc17c9..92bf275 100644 --- a/components/iperf/iperf.c +++ b/components/iperf/iperf.c @@ -18,6 +18,9 @@ #include "esp_wifi.h" #include "iperf.h" +// --- LED DRIVER PLACEHOLDER --- +// #include "led_strip.h" + static const char *TAG = "iperf"; // --- LED STATE MANAGEMENT --- @@ -35,7 +38,7 @@ static led_state_t s_led_state = LED_RED_FLASH; // --- Helper: Set Physical LED --- static void iperf_set_physical_led(uint8_t r, uint8_t g, uint8_t b) { - // Implement hardware specific LED driver here + // Hardware LED set call here } // --- LED Task --- @@ -132,7 +135,10 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) { size_t required_size; uint32_t val = 0; - if (nvs_get_u32(my_handle, NVS_KEY_IPERF_RATE, &val) == ESP_OK && val > 0) cfg->bw_lim = val; + // READ PERIOD (u32 microseconds) + if (nvs_get_u32(my_handle, NVS_KEY_IPERF_PERIOD, &val) == ESP_OK && val > 0) cfg->pacing_period_us = val; + else cfg->pacing_period_us = 10000; // Default 10ms if missing + if (nvs_get_u32(my_handle, NVS_KEY_IPERF_BURST, &val) == ESP_OK && val > 0) cfg->burst_count = val; else cfg->burst_count = 1; if (nvs_get_u32(my_handle, NVS_KEY_IPERF_LEN, &val) == ESP_OK && val > 0) cfg->send_len = val; else cfg->send_len = IPERF_UDP_TX_LEN; @@ -161,25 +167,12 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) { nvs_close(my_handle); } -// --- Stubbed / Unused Functions (Marked to silence warnings) --- -static void __attribute__((unused)) socket_send(int sockfd, const uint8_t *buffer, int len) { - // Stub -} -static int __attribute__((unused)) socket_recv(int sockfd, uint8_t *buffer, int len, TickType_t timeout_ticks) { - return 0; // Stub -} -static esp_err_t iperf_start_tcp_server(iperf_ctrl_t *ctrl) { - ESP_LOGW(TAG, "TCP Server not implemented"); - return ESP_FAIL; -} -static esp_err_t iperf_start_tcp_client(iperf_ctrl_t *ctrl) { - ESP_LOGW(TAG, "TCP Client not implemented"); - return ESP_FAIL; -} -static esp_err_t iperf_start_udp_server(iperf_ctrl_t *ctrl) { - ESP_LOGW(TAG, "UDP Server not implemented"); - return ESP_FAIL; -} +// --- Stubbed / Unused Functions --- +static void __attribute__((unused)) socket_send(int sockfd, const uint8_t *buffer, int len) {} +static int __attribute__((unused)) socket_recv(int sockfd, uint8_t *buffer, int len, TickType_t timeout_ticks) { return 0; } +static esp_err_t iperf_start_tcp_server(iperf_ctrl_t *ctrl) { ESP_LOGW(TAG, "TCP Server not implemented"); return ESP_FAIL; } +static esp_err_t iperf_start_tcp_client(iperf_ctrl_t *ctrl) { ESP_LOGW(TAG, "TCP Client not implemented"); return ESP_FAIL; } +static esp_err_t iperf_start_udp_server(iperf_ctrl_t *ctrl) { ESP_LOGW(TAG, "UDP Server not implemented"); return ESP_FAIL; } // ----------------------------------------------------------------------------- // MAIN UDP CLIENT @@ -203,15 +196,16 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) addr.sin_port = htons(ctrl->cfg.dport); addr.sin_addr.s_addr = ctrl->cfg.dip; + // --- SETUP PACING FROM CONFIG/NVS --- uint32_t burst_count = ctrl->cfg.burst_count ? ctrl->cfg.burst_count : 1; uint32_t payload_len = ctrl->cfg.send_len ? ctrl->cfg.send_len : IPERF_UDP_TX_LEN; - double target_bandwidth_mbps = (double)ctrl->cfg.bw_lim; - if (target_bandwidth_mbps <= 0) target_bandwidth_mbps = 1.0; - double target_bps = target_bandwidth_mbps * 1000000.0; - double total_pps = target_bps / (payload_len * 8.0); - double bursts_per_sec = total_pps / (double)burst_count; - double pacing_interval_us = 1000000.0 / bursts_per_sec; + // Direct Period Pacing (No rate calculation needed) + // Default to 10ms (10000us) if not set + uint32_t pacing_period_us = ctrl->cfg.pacing_period_us ? ctrl->cfg.pacing_period_us : 10000; + + ESP_LOGI(TAG, "UDP Client -> Burst: %" PRIu32 " pkts | Period: %" PRIu32 " us | Payload: %" PRIu32 " bytes", + burst_count, pacing_period_us, payload_len); s_led_state = LED_PURPLE_SOLID; // Transmitting @@ -220,7 +214,6 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) int64_t start_time_us = esp_timer_get_time(); int64_t next_send_time = start_time_us; int64_t end_time_us = start_time_us + ((int64_t)ctrl->cfg.time * 1000000LL); - double interval_accum = 0.0; while (!ctrl->finish && esp_timer_get_time() < end_time_us) { int64_t current_time = esp_timer_get_time(); @@ -240,7 +233,7 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) client_hdr->numThreads = htonl(1); client_hdr->mPort = htonl(ctrl->cfg.dport); client_hdr->mBufLen = htonl(payload_len); - client_hdr->mWinBand = htonl((int)target_bps); + client_hdr->mWinBand = htonl(0); // Sent as 0 since we pace by period client_hdr->mAmount = htonl(-(int)(ctrl->cfg.time * 100)); } @@ -255,10 +248,14 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) goto exit_client; } } - interval_accum += pacing_interval_us; - int64_t steps = (int64_t)interval_accum; - if (steps > 0) { next_send_time += steps; interval_accum -= steps; } - if (esp_timer_get_time() > next_send_time + 4000) next_send_time = esp_timer_get_time() + (int64_t)pacing_interval_us; + + // Advance by fixed period + next_send_time += pacing_period_us; + + // Lag prevention + if (esp_timer_get_time() > next_send_time + 4000) { + next_send_time = esp_timer_get_time() + pacing_period_us; + } } else { int64_t wait = next_send_time - current_time; if (wait > 2000) vTaskDelay(pdMS_TO_TICKS(wait/1000)); diff --git a/components/iperf/iperf.h b/components/iperf/iperf.h index f642932..e914f2e 100644 --- a/components/iperf/iperf.h +++ b/components/iperf/iperf.h @@ -4,13 +4,11 @@ #include #include -// --- Configuration Flags --- #define IPERF_FLAG_CLIENT (1 << 0) #define IPERF_FLAG_SERVER (1 << 1) #define IPERF_FLAG_TCP (1 << 2) #define IPERF_FLAG_UDP (1 << 3) -// --- Defaults --- #define IPERF_DEFAULT_PORT 5001 #define IPERF_DEFAULT_INTERVAL 3 #define IPERF_DEFAULT_TIME 30 @@ -20,37 +18,38 @@ #define IPERF_SOCKET_RX_TIMEOUT 10 #define IPERF_SOCKET_ACCEPT_TIMEOUT 5 -// --- Buffer Sizes --- -#define IPERF_UDP_TX_LEN (1470) // Default UDP Payload +// Default buffer sizes +#define IPERF_UDP_TX_LEN (1470) #define IPERF_UDP_RX_LEN (16 << 10) #define IPERF_TCP_TX_LEN (16 << 10) #define IPERF_TCP_RX_LEN (16 << 10) -// --- NVS Storage Keys --- +// NVS Keys #define NVS_KEY_IPERF_ENABLE "iperf_enabled" // 0=Disabled, 1=Enabled -#define NVS_KEY_IPERF_RATE "iperf_rate" // Target Bandwidth (Mbps) +#define NVS_KEY_IPERF_PERIOD "iperf_period" // Period in microseconds (u32) #define NVS_KEY_IPERF_ROLE "iperf_role" // "CLIENT" or "SERVER" #define NVS_KEY_IPERF_DST_IP "iperf_dst_ip" // Target IP String #define NVS_KEY_IPERF_PROTO "iperf_proto" // "UDP" or "TCP" -#define NVS_KEY_IPERF_BURST "iperf_burst" // Packets per schedule tick +#define NVS_KEY_IPERF_BURST "iperf_burst" // Packets per period #define NVS_KEY_IPERF_LEN "iperf_len" // UDP Payload Length -// --- Main Configuration Structure --- typedef struct { - uint32_t flag; // Client/Server | TCP/UDP flags - uint8_t type; // (Internal use) - uint32_t dip; // Destination IP (Network Byte Order) - uint16_t dport; // Destination Port + uint32_t flag; + uint8_t type; + uint32_t dip; // Destination IP + uint16_t dport; // Dest Port uint16_t sport; // Source Port - uint32_t interval; // Report Interval (seconds) + uint32_t interval; uint32_t time; // Test Duration (seconds) - uint32_t bw_lim; // Bandwidth Limit (Mbps) - uint32_t burst_count;// Burst Mode: Packets per schedule tick + + // Pacing Config + uint32_t pacing_period_us; // Period between bursts in microseconds + uint32_t burst_count; // Packets per period + uint32_t send_len; // User defined Payload Length uint32_t buffer_len; // Internally calculated buffer size } iperf_cfg_t; -// --- Traffic Statistics Structure --- typedef struct { uint64_t total_len; uint32_t buffer_len; @@ -62,9 +61,7 @@ typedef struct { uint32_t udp_packet_counter; } iperf_traffic_t; -// --- Iperf 2.0.5+ Compatible Headers --- - -// Standard UDP Datagram Header (Present in EVERY packet) +// Standard UDP Datagram Header typedef struct { int32_t id; // Sequence Number uint32_t tv_sec; // Timestamp Seconds @@ -72,32 +69,20 @@ typedef struct { uint32_t id2; // 64-bit seq / Padding } udp_datagram; -// Client Header (Sent ONLY in the first UDP packet of a stream) +// Client Header typedef struct { - int32_t flags; // Flags (Version, Dual Test, etc.) + int32_t flags; // Flags (Version, etc.) int32_t numThreads; // Parallel threads int32_t mPort; // Port int32_t mBufLen; // Buffer Length - int32_t mWinBand; // Target Bandwidth - int32_t mAmount; // Duration / Bytes (negative = time) + int32_t mWinBand; // Target Bandwidth (Legacy field, sent as 0 or placeholder) + int32_t mAmount; // Duration / Bytes } client_hdr_v1; -// Version Flag for Client Header #define HEADER_VERSION1 0x80000000 -// --- Public API --- -/** - * @brief Start the Iperf task. - * * Reads configuration from NVS ("storage" partition) to override defaults. - * If NVS_KEY_IPERF_ENABLE is 0, this function returns immediately. - * * @param cfg Pointer to initial configuration (can be overridden by NVS) - */ +// Public API void iperf_start(iperf_cfg_t *cfg); - -/** - * @brief Stop the Iperf task. - * * Signals the running task to finish and close sockets. - */ void iperf_stop(void); #endif // IPERF_H diff --git a/esp32_deploy.py b/esp32_deploy.py index 2ec7128..565d25f 100755 --- a/esp32_deploy.py +++ b/esp32_deploy.py @@ -133,20 +133,21 @@ class UnifiedDeployWorker: async def _send_config(self, writer): csi_val = '1' if self.args.csi_enable else '0' - role_str = "CLIENT" if self.args.iperf_server: role_str = "SERVER" elif self.args.iperf_client: role_str = "CLIENT" - - # Enable Logic: 1=Yes, 0=No iperf_enable_val = '0' if self.args.no_iperf else '1' + # Convert period (seconds) to microseconds (integer) + period_us = int(self.args.iperf_period * 1000000) + config_str = ( f"CFG\nSSID:{self.args.ssid}\nPASS:{self.args.password}\nIP:{self.target_ip}\n" f"MASK:{self.args.netmask}\nGW:{self.args.gateway}\nDHCP:0\nBAND:{self.args.band}\n" f"BW:{self.args.bandwidth}\nPOWERSAVE:{self.args.powersave}\nMODE:{self.args.mode}\n" f"MON_CH:{self.args.monitor_channel}\nCSI:{csi_val}\n" - f"IPERF_RATE:{self.args.iperf_rate}\nIPERF_ROLE:{role_str}\n" + f"IPERF_PERIOD_US:{period_us}\n" # <--- Changed from RATE to PERIOD_US + f"IPERF_ROLE:{role_str}\n" f"IPERF_PROTO:{self.args.iperf_proto}\nIPERF_DEST_IP:{self.args.iperf_dest_ip}\n" f"IPERF_BURST:{self.args.iperf_burst}\nIPERF_LEN:{self.args.iperf_len}\n" f"IPERF_ENABLED:{iperf_enable_val}\n" @@ -170,32 +171,26 @@ class UnifiedDeployWorker: def parse_args(): parser = argparse.ArgumentParser(description='ESP32 Unified Deployment Tool') - # Operation Mode parser.add_argument('--config-only', action='store_true', help='Configure only') parser.add_argument('--flash-only', action='store_true', help='Flash only') parser.add_argument('--flash-erase', action='store_true', help='Erase flash first') - - # Build/Flash parser.add_argument('-d', '--dir', default=os.getcwd(), help='Project dir') parser.add_argument('-b', '--baud', type=int, default=460800, help='Flash baud') - parser.add_argument('--devices', type=str, help='Device list /dev/ttyUSB0,/dev/ttyUSB1') + parser.add_argument('--devices', type=str, help='Device list') parser.add_argument('--max-concurrent', type=int, default=None, help='Max concurrent flash') - - # Network parser.add_argument('--start-ip', required=True, help='Start IP') parser.add_argument('-s', '--ssid', default='ClubHouse2G', help='SSID') parser.add_argument('-P', '--password', default='ez2remember', help='Password') parser.add_argument('-g', '--gateway', default='192.168.1.1', help='Gateway') parser.add_argument('-m', '--netmask', default='255.255.255.0', help='Netmask') - - # WiFi parser.add_argument('--band', default='2.4G', choices=['2.4G', '5G'], help='Band') parser.add_argument('-B', '--bandwidth', default='HT20', choices=['HT20', 'HT40', 'VHT80'], help='BW') parser.add_argument('-ps', '--powersave', default='NONE', help='Power save') - # Iperf - parser.add_argument('--iperf-rate', type=int, default=10, help='Mbps') - parser.add_argument('--iperf-burst', type=int, default=1, help='Packets/tick') + # Iperf Settings (Updated) + parser.add_argument('--iperf-period', type=float, default=0.01, + help='Pacing period in seconds (default: 0.01s = 10ms)') + parser.add_argument('--iperf-burst', type=int, default=1, help='Packets per period') parser.add_argument('--iperf-len', type=int, default=1470, help='Payload len') parser.add_argument('--iperf-proto', default='UDP', choices=['UDP', 'TCP'], help='Proto') parser.add_argument('--iperf-dest-ip', default='192.168.1.50', help='Dest IP') @@ -205,7 +200,6 @@ def parse_args(): g.add_argument('--iperf-client', action='store_true') g.add_argument('--iperf-server', action='store_true') - # Mode parser.add_argument('-M', '--mode', default='STA', choices=['STA', 'MONITOR']) parser.add_argument('-mc', '--monitor-channel', type=int, default=36) parser.add_argument('--csi', dest='csi_enable', action='store_true') @@ -232,7 +226,6 @@ async def run_deployment(args): if proc.returncode != 0: print(f"{Colors.RED}Build Failed:\n{stderr.decode()}{Colors.RESET}"); return print(f"{Colors.GREEN}Build Complete{Colors.RESET}") - # Detect Devices if args.devices: devs = [type('obj', (object,), {'device': d.strip()}) for d in args.devices.split(',')] else: @@ -243,7 +236,6 @@ async def run_deployment(args): print(f"{Colors.GREEN}Found {len(devs)} devices{Colors.RESET}") start_ip = ipaddress.IPv4Address(args.start_ip) - # Concurrency max_c = args.max_concurrent if args.max_concurrent else (1 if args.devices and not args.config_only else DEFAULT_MAX_CONCURRENT_FLASH) flash_sem = asyncio.Semaphore(max_c) @@ -262,4 +254,5 @@ def main(): try: asyncio.run(run_deployment(parse_args())) except KeyboardInterrupt: sys.exit(1) -if __name__ == '__main__': main() +if __name__ == '__main__': + main()