add ping support
This commit is contained in:
parent
e8f7e2f75c
commit
46f0cdb07b
|
|
@ -1,3 +1,3 @@
|
|||
idf_component_register(SRCS "app_console.c"
|
||||
INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES console wifi_cfg wifi_controller iperf nvs_flash gps_sync)
|
||||
PRIV_REQUIRES console iperf wifi_cfg wifi_controller nvs_flash gps_sync lwip)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@
|
|||
#include "esp_wifi.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "ping/ping_sock.h"
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <inttypes.h>
|
||||
|
|
@ -553,6 +555,125 @@ static void register_wifi_cmd(void) {
|
|||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
||||
}
|
||||
|
||||
#include "ping/ping_sock.h" // Add this include
|
||||
|
||||
// ... [Existing Includes] ...
|
||||
|
||||
static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args) {
|
||||
uint8_t ttl;
|
||||
uint16_t seqno;
|
||||
uint32_t elapsed_time, recv_len;
|
||||
ip_addr_t target_addr;
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
|
||||
printf("%" PRIu32 " bytes from %s: icmp_seq=%u ttl=%u time=%" PRIu32 " ms\n",
|
||||
recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
|
||||
}
|
||||
|
||||
static void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args) {
|
||||
uint16_t seqno;
|
||||
ip_addr_t target_addr;
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
|
||||
printf("From %s: icmp_seq=%u timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
|
||||
}
|
||||
|
||||
static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args) {
|
||||
uint32_t transmitted;
|
||||
uint32_t received;
|
||||
uint32_t total_time_ms;
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
|
||||
printf("\n--- ping statistics ---\n");
|
||||
printf("%" PRIu32 " packets transmitted, %" PRIu32 " received, %" PRIu32 "%% packet loss, time %" PRIu32 "ms\n",
|
||||
transmitted, received, (transmitted - received) * 100 / transmitted, total_time_ms);
|
||||
esp_ping_delete_session(hdl);
|
||||
}
|
||||
|
||||
static struct {
|
||||
struct arg_str *host;
|
||||
struct arg_int *count;
|
||||
struct arg_int *interval;
|
||||
struct arg_end *end;
|
||||
} ping_args;
|
||||
|
||||
static int cmd_ping(int argc, char **argv) {
|
||||
int nerrors = arg_parse(argc, argv, (void **)&ping_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, ping_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();
|
||||
|
||||
// Parse Args
|
||||
if (ping_args.count->count > 0) {
|
||||
config.count = ping_args.count->ival[0];
|
||||
}
|
||||
if (ping_args.interval->count > 0) {
|
||||
config.interval_ms = ping_args.interval->ival[0] * 1000;
|
||||
}
|
||||
|
||||
// Parse Target IP
|
||||
ip_addr_t target_addr;
|
||||
struct addrinfo hint;
|
||||
struct addrinfo *res = NULL;
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
memset(&target_addr, 0, sizeof(target_addr));
|
||||
|
||||
// Check if simple IP string
|
||||
if (inet_aton(ping_args.host->sval[0], &target_addr.u_addr.ip4)) {
|
||||
target_addr.type = IPADDR_TYPE_V4;
|
||||
} else {
|
||||
// Resolve Hostname
|
||||
printf("Resolving %s...\n", ping_args.host->sval[0]);
|
||||
if (getaddrinfo(ping_args.host->sval[0], NULL, &hint, &res) != 0) {
|
||||
printf("ping: unknown host %s\n", ping_args.host->sval[0]);
|
||||
return 1;
|
||||
}
|
||||
struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr;
|
||||
inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
|
||||
target_addr.type = IPADDR_TYPE_V4;
|
||||
freeaddrinfo(res);
|
||||
}
|
||||
|
||||
config.target_addr = target_addr;
|
||||
config.task_stack_size = 4096; // Ensure enough stack
|
||||
|
||||
esp_ping_callbacks_t cbs = {
|
||||
.on_ping_success = cmd_ping_on_ping_success,
|
||||
.on_ping_timeout = cmd_ping_on_ping_timeout,
|
||||
.on_ping_end = cmd_ping_on_ping_end,
|
||||
.cb_args = NULL
|
||||
};
|
||||
|
||||
esp_ping_handle_t ping;
|
||||
esp_ping_new_session(&config, &cbs, &ping);
|
||||
esp_ping_start(ping);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_ping(void) {
|
||||
ping_args.host = arg_str1(NULL, NULL, "<host>", "Host address or name");
|
||||
ping_args.count = arg_int0("c", "count", "<n>", "Stop after <n> replies");
|
||||
ping_args.interval = arg_int0("i", "interval", "<seconds>", "Wait interval");
|
||||
ping_args.end = arg_end(1);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "ping",
|
||||
.help = "Send ICMP ECHO_REQUEST to network hosts",
|
||||
.hint = NULL,
|
||||
.func = &cmd_ping,
|
||||
.argtable = &ping_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
||||
}
|
||||
|
||||
void app_console_register_commands(void) {
|
||||
register_iperf_cmd();
|
||||
register_monitor_cmd();
|
||||
|
|
@ -560,4 +681,5 @@ void app_console_register_commands(void) {
|
|||
register_wifi_cmd();
|
||||
register_nvs_cmd();
|
||||
register_gps_cmd();
|
||||
register_ping();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ idf_component_register(
|
|||
# Only if iperf.h needs types from these (unlikely based on your code):
|
||||
REQUIRES lwip led_strip
|
||||
# Internal implementation details only:
|
||||
PRIV_REQUIRES esp_event esp_timer nvs_flash esp_netif esp_wifi status_led
|
||||
PRIV_REQUIRES esp_event esp_timer nvs_flash esp_netif esp_wifi status_led gps_sync
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,49 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file iperf.c
|
||||
* @brief ESP32 iPerf Traffic Generator (UDP Client Only)
|
||||
*
|
||||
* This module implements a lightweight UDP traffic generator compatible with iPerf2.
|
||||
* It features:
|
||||
* - Precise packet pacing (PPS) using monotonic timers (drift-free).
|
||||
* - Finite State Machine (FSM) to detect stalls and slow links.
|
||||
* - Non-Volatile Storage (NVS) for persistent configuration.
|
||||
* - Detailed error tracking (ENOMEM vs Route errors).
|
||||
* - GPS Timestamp integration for status reporting.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
|
@ -7,6 +53,7 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
|
@ -20,6 +67,7 @@
|
|||
#include "esp_wifi.h"
|
||||
#include "iperf.h"
|
||||
#include "status_led.h"
|
||||
#include "gps_sync.h"
|
||||
|
||||
static const char *TAG = "iperf";
|
||||
|
||||
|
|
@ -34,7 +82,7 @@ static const char *TAG = "iperf";
|
|||
#define NVS_KEY_IPERF_LEN "iperf_len"
|
||||
|
||||
// --- Global Config State ---
|
||||
static iperf_cfg_t s_staging_cfg = {0}; // The "Running" Config
|
||||
static iperf_cfg_t s_staging_cfg = {0};
|
||||
static bool s_staging_initialized = false;
|
||||
|
||||
static EventGroupHandle_t s_iperf_event_group = NULL;
|
||||
|
|
@ -52,7 +100,7 @@ typedef struct {
|
|||
uint8_t *buffer;
|
||||
} iperf_ctrl_t;
|
||||
|
||||
static iperf_ctrl_t s_iperf_ctrl = {0}; // The "Active" Config (while task runs)
|
||||
static iperf_ctrl_t s_iperf_ctrl = {0};
|
||||
static TaskHandle_t s_iperf_task_handle = NULL;
|
||||
static bool s_reload_req = false;
|
||||
|
||||
|
|
@ -60,7 +108,6 @@ static bool s_reload_req = false;
|
|||
static iperf_stats_t s_stats = {0};
|
||||
static int64_t s_session_start_time = 0;
|
||||
static int64_t s_session_end_time = 0;
|
||||
static uint64_t s_session_packets = 0;
|
||||
|
||||
// --- FSM State & Stats ---
|
||||
typedef enum {
|
||||
|
|
@ -105,15 +152,14 @@ typedef struct {
|
|||
static void set_defaults(iperf_cfg_t *cfg) {
|
||||
memset(cfg, 0, sizeof(iperf_cfg_t));
|
||||
cfg->flag = IPERF_FLAG_CLIENT | IPERF_FLAG_UDP;
|
||||
cfg->dip = 0; // 0.0.0.0
|
||||
cfg->dip = 0;
|
||||
cfg->dport = IPERF_DEFAULT_PORT;
|
||||
cfg->time = 0; // Infinite
|
||||
cfg->target_pps = 100; // Default 100 PPS
|
||||
cfg->target_pps = 100;
|
||||
cfg->burst_count = 1;
|
||||
cfg->send_len = IPERF_UDP_TX_LEN;
|
||||
}
|
||||
|
||||
// --- Parameter Management (Init / Load / Save / Get / Set) ---
|
||||
// --- Parameter Management ---
|
||||
|
||||
static void trim_whitespace(char *str) {
|
||||
char *end = str + strlen(str) - 1;
|
||||
|
|
@ -121,8 +167,6 @@ static void trim_whitespace(char *str) {
|
|||
*(end+1) = 0;
|
||||
}
|
||||
|
||||
// Clear NVS
|
||||
|
||||
void iperf_param_clear(void) {
|
||||
nvs_handle_t h;
|
||||
if (nvs_open("storage", NVS_READWRITE, &h) == ESP_OK) {
|
||||
|
|
@ -135,27 +179,18 @@ void iperf_param_clear(void) {
|
|||
nvs_close(h);
|
||||
ESP_LOGI(TAG, "iPerf NVS configuration cleared.");
|
||||
}
|
||||
|
||||
// Reset RAM to defaults to match the "Empty" NVS state
|
||||
set_defaults(&s_staging_cfg);
|
||||
}
|
||||
|
||||
void iperf_param_init(void) {
|
||||
if (s_staging_initialized) return;
|
||||
|
||||
set_defaults(&s_staging_cfg);
|
||||
|
||||
// Load from NVS
|
||||
nvs_handle_t h;
|
||||
if (nvs_open("storage", NVS_READONLY, &h) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Loading saved config from NVS...");
|
||||
|
||||
uint32_t val;
|
||||
// Direct Load: No conversion needed
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_PPS, &val) == ESP_OK && val > 0) {
|
||||
s_staging_cfg.target_pps = val;
|
||||
}
|
||||
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_PPS, &val) == ESP_OK && val > 0) s_staging_cfg.target_pps = val;
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_BURST, &val) == ESP_OK) s_staging_cfg.burst_count = val;
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_LEN, &val) == ESP_OK) s_staging_cfg.send_len = val;
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_PORT, &val) == ESP_OK) s_staging_cfg.dport = (uint16_t)val;
|
||||
|
|
@ -171,10 +206,7 @@ void iperf_param_init(void) {
|
|||
}
|
||||
}
|
||||
nvs_close(h);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "No saved config found, using defaults.");
|
||||
}
|
||||
|
||||
s_staging_initialized = true;
|
||||
}
|
||||
|
||||
|
|
@ -185,98 +217,55 @@ void iperf_param_get(iperf_cfg_t *out_cfg) {
|
|||
|
||||
void iperf_param_set(const iperf_cfg_t *new_cfg) {
|
||||
if (!s_staging_initialized) iperf_param_init();
|
||||
|
||||
// Update Staging
|
||||
s_staging_cfg = *new_cfg;
|
||||
|
||||
// Hot Reload Logic
|
||||
if (s_iperf_task_handle) {
|
||||
ESP_LOGI(TAG, "Hot reloading parameters...");
|
||||
s_iperf_ctrl.cfg = s_staging_cfg;
|
||||
s_reload_req = true;
|
||||
|
||||
// Stop current internal loop to pick up new config
|
||||
if (s_iperf_event_group) xEventGroupSetBits(s_iperf_event_group, IPERF_STOP_REQ_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Dirty Check ---
|
||||
bool iperf_param_is_unsaved(void) {
|
||||
if (!s_staging_initialized) return false;
|
||||
|
||||
nvs_handle_t h;
|
||||
if (nvs_open("storage", NVS_READONLY, &h) != ESP_OK) return false;
|
||||
|
||||
uint32_t val;
|
||||
bool match = true;
|
||||
|
||||
// Direct Compare: No conversion needed
|
||||
uint32_t saved_pps = 0;
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_PPS, &val) == ESP_OK) saved_pps = val;
|
||||
if (s_staging_cfg.target_pps != saved_pps) match = false;
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_PPS, &val) == ESP_OK) { if (s_staging_cfg.target_pps != val) match = false; }
|
||||
else if (s_staging_cfg.target_pps != 100) match = false;
|
||||
|
||||
// Standard Fields
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_BURST, &val) == ESP_OK) { if (s_staging_cfg.burst_count != val) match = false; }
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_LEN, &val) == ESP_OK) { if (s_staging_cfg.send_len != val) match = false; }
|
||||
|
||||
uint32_t saved_port = 0;
|
||||
if (nvs_get_u32(h, NVS_KEY_IPERF_PORT, &val) == ESP_OK) saved_port = val;
|
||||
if (s_staging_cfg.dport != (uint16_t)saved_port) match = false;
|
||||
|
||||
// IP String
|
||||
size_t req;
|
||||
char staging_ip[32];
|
||||
struct in_addr daddr; daddr.s_addr = s_staging_cfg.dip;
|
||||
inet_ntop(AF_INET, &daddr, staging_ip, sizeof(staging_ip));
|
||||
|
||||
if (nvs_get_str(h, NVS_KEY_IPERF_DST_IP, NULL, &req) == ESP_OK) {
|
||||
char *saved_ip = malloc(req);
|
||||
if (saved_ip) {
|
||||
nvs_get_str(h, NVS_KEY_IPERF_DST_IP, saved_ip, &req);
|
||||
trim_whitespace(saved_ip);
|
||||
if (strcmp(saved_ip, staging_ip) != 0) match = false;
|
||||
free(saved_ip);
|
||||
}
|
||||
} else {
|
||||
if (s_staging_cfg.dip != 0) match = false;
|
||||
}
|
||||
// ...
|
||||
|
||||
nvs_close(h);
|
||||
return !match;
|
||||
}
|
||||
|
||||
// --- Save with Check ---
|
||||
esp_err_t iperf_param_save(bool *out_changed) {
|
||||
if (out_changed) *out_changed = false;
|
||||
if (!iperf_param_is_unsaved()) {
|
||||
ESP_LOGI(TAG, "Config matches NVS. No write needed.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
nvs_handle_t h;
|
||||
esp_err_t err = nvs_open("storage", NVS_READWRITE, &h);
|
||||
if (err != ESP_OK) return err;
|
||||
if (nvs_open("storage", NVS_READWRITE, &h) != ESP_OK) return ESP_FAIL;
|
||||
|
||||
// Direct Save: No conversion needed
|
||||
nvs_set_u32(h, NVS_KEY_IPERF_PPS, s_staging_cfg.target_pps);
|
||||
nvs_set_u32(h, NVS_KEY_IPERF_BURST, s_staging_cfg.burst_count);
|
||||
nvs_set_u32(h, NVS_KEY_IPERF_LEN, s_staging_cfg.send_len);
|
||||
nvs_set_u32(h, NVS_KEY_IPERF_PORT, (uint32_t)s_staging_cfg.dport);
|
||||
nvs_set_u32(h, NVS_KEY_IPERF_PORT, s_staging_cfg.dport);
|
||||
|
||||
char ip_str[32];
|
||||
struct in_addr daddr;
|
||||
daddr.s_addr = s_staging_cfg.dip;
|
||||
struct in_addr daddr; daddr.s_addr = s_staging_cfg.dip;
|
||||
inet_ntop(AF_INET, &daddr, ip_str, sizeof(ip_str));
|
||||
nvs_set_str(h, NVS_KEY_IPERF_DST_IP, ip_str);
|
||||
|
||||
err = nvs_commit(h);
|
||||
esp_err_t err = nvs_commit(h);
|
||||
if (err == ESP_OK && out_changed) *out_changed = true;
|
||||
|
||||
nvs_close(h);
|
||||
return err;
|
||||
}
|
||||
|
||||
// --- Status & Helpers ---
|
||||
// --- Status ---
|
||||
|
||||
void iperf_get_stats(iperf_stats_t *stats) {
|
||||
if (stats) {
|
||||
|
|
@ -288,16 +277,24 @@ void iperf_get_stats(iperf_stats_t *stats) {
|
|||
void iperf_print_status(void) {
|
||||
iperf_get_stats(&s_stats);
|
||||
|
||||
gps_timestamp_t ts = gps_get_timestamp();
|
||||
if (ts.synced && ts.gps_us > 0) {
|
||||
time_t now_sec = ts.gps_us / 1000000;
|
||||
struct tm tm_info;
|
||||
gmtime_r(&now_sec, &tm_info);
|
||||
char time_buf[64];
|
||||
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S UTC", &tm_info);
|
||||
printf("TIME: %s\n", time_buf);
|
||||
} else {
|
||||
printf("TIME: <Not Synced>\n");
|
||||
}
|
||||
|
||||
char dst_ip[32] = "0.0.0.0";
|
||||
struct in_addr daddr;
|
||||
|
||||
// Show active config if running, otherwise staging
|
||||
if (s_stats.running) daddr.s_addr = s_iperf_ctrl.cfg.dip;
|
||||
else daddr.s_addr = s_staging_cfg.dip;
|
||||
|
||||
inet_ntop(AF_INET, &daddr, dst_ip, sizeof(dst_ip));
|
||||
|
||||
// Calculate Percentages
|
||||
double total_us = (double)(s_time_tx_us + s_time_slow_us + s_time_stalled_us);
|
||||
if (total_us < 1.0) total_us = 1.0;
|
||||
|
||||
|
|
@ -305,14 +302,13 @@ void iperf_print_status(void) {
|
|||
double pct_slow = ((double)s_time_slow_us / total_us) * 100.0;
|
||||
double pct_stalled = ((double)s_time_stalled_us / total_us) * 100.0;
|
||||
|
||||
// Bandwidth Calculation
|
||||
float avg_bw_mbps = 0.0f;
|
||||
if (s_session_start_time > 0) {
|
||||
int64_t end_t = (s_stats.running) ? esp_timer_get_time() : s_session_end_time;
|
||||
if (end_t > s_session_start_time) {
|
||||
double duration_sec = (double)(end_t - s_session_start_time) / 1000000.0;
|
||||
if (duration_sec > 0.001) {
|
||||
double total_bits = (double)s_session_packets * (double)s_iperf_ctrl.cfg.send_len * 8.0;
|
||||
double total_bits = (double)s_stats.total_packets * (double)s_iperf_ctrl.cfg.send_len * 8.0;
|
||||
avg_bw_mbps = (float)(total_bits / duration_sec / 1000000.0);
|
||||
}
|
||||
}
|
||||
|
|
@ -321,7 +317,7 @@ void iperf_print_status(void) {
|
|||
printf("IPERF: Dest=%s:%u, Pkts=%llu, BW=%.2f Mbps, Running=%d\n",
|
||||
dst_ip,
|
||||
s_stats.running ? s_iperf_ctrl.cfg.dport : s_staging_cfg.dport,
|
||||
s_session_packets,
|
||||
s_stats.total_packets,
|
||||
avg_bw_mbps,
|
||||
s_stats.running);
|
||||
|
||||
|
|
@ -329,19 +325,24 @@ void iperf_print_status(void) {
|
|||
(double)s_time_tx_us/1000000.0, pct_tx, (unsigned long)s_edge_tx,
|
||||
(double)s_time_slow_us/1000000.0, pct_slow, (unsigned long)s_edge_slow,
|
||||
(double)s_time_stalled_us/1000000.0, pct_stalled, (unsigned long)s_edge_stalled);
|
||||
|
||||
if (s_stats.err_mem > 0 || s_stats.err_route > 0 || s_stats.err_other > 0) {
|
||||
printf("ERRORS: ENOMEM=%lu, EHOST=%lu, OTHER=%lu\n",
|
||||
(unsigned long)s_stats.err_mem,
|
||||
(unsigned long)s_stats.err_route,
|
||||
(unsigned long)s_stats.err_other);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Core Logic ---
|
||||
|
||||
static void iperf_pattern(uint8_t *buf, uint32_t len) {
|
||||
for (uint32_t i = 0; i < len; i++) {
|
||||
buf[i] = (i % 10) + '0';
|
||||
}
|
||||
for (uint32_t i = 0; i < len; i++) buf[i] = (i % 10) + '0';
|
||||
}
|
||||
|
||||
static void iperf_generate_client_hdr(iperf_cfg_t *cfg, client_hdr_v1 *hdr) {
|
||||
memset(hdr, 0, sizeof(client_hdr_v1));
|
||||
hdr->flags = htonl(HEADER_SEQNO64B);
|
||||
hdr->flags = htonl(0x08000000); // HEADER_SEQNO64B
|
||||
}
|
||||
|
||||
static void iperf_network_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
||||
|
|
@ -351,6 +352,8 @@ static void iperf_network_event_handler(void* arg, esp_event_base_t event_base,
|
|||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
xEventGroupClearBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
||||
|
||||
// --- ADDED: Set Yellow on Disconnect ---
|
||||
status_led_set_state(LED_STATE_NO_CONFIG);
|
||||
}
|
||||
}
|
||||
|
|
@ -361,14 +364,15 @@ static bool iperf_wait_for_ip(void) {
|
|||
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (netif) {
|
||||
esp_netif_ip_info_t ip_info;
|
||||
if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
|
||||
return true;
|
||||
}
|
||||
if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) return true;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &iperf_network_event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &iperf_network_event_handler, NULL, &instance_got_ip));
|
||||
|
||||
// --- ADDED: Set Blue Blink while waiting ---
|
||||
status_led_set_state(LED_STATE_WAITING);
|
||||
|
||||
ESP_LOGI(TAG, "Waiting for IP...");
|
||||
EventBits_t bits = xEventGroupWaitBits(s_iperf_event_group, IPERF_IP_READY_BIT | IPERF_STOP_REQ_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
|
|
@ -380,6 +384,7 @@ static bool iperf_wait_for_ip(void) {
|
|||
}
|
||||
|
||||
static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
||||
// Note: status_led_set_state(LED_STATE_WAITING) happens inside if needed
|
||||
if (!iperf_wait_for_ip()) return ESP_OK;
|
||||
|
||||
struct sockaddr_in addr;
|
||||
|
|
@ -390,9 +395,11 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|||
int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (sockfd < 0) {
|
||||
ESP_LOGE(TAG, "Socket failed: %d", errno);
|
||||
status_led_set_state(LED_STATE_FAILED); // Red Blink on Error
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// --- Set Purple Slow Pulse (Starting) ---
|
||||
status_led_set_state(LED_STATE_TRANSMITTING_SLOW);
|
||||
|
||||
udp_datagram *udp_hdr = (udp_datagram *)ctrl->buffer;
|
||||
|
|
@ -400,10 +407,11 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|||
iperf_generate_client_hdr(&ctrl->cfg, client_hdr);
|
||||
|
||||
s_stats.running = true;
|
||||
s_session_start_time = esp_timer_get_time();
|
||||
s_session_packets = 0;
|
||||
s_stats.err_mem = 0; s_stats.err_route = 0; s_stats.err_other = 0;
|
||||
s_stats.total_packets = 0;
|
||||
|
||||
s_session_start_time = esp_timer_get_time();
|
||||
|
||||
// Reset FSM
|
||||
s_time_tx_us = 0; s_time_slow_us = 0; s_time_stalled_us = 0;
|
||||
s_edge_tx = 0; s_edge_slow = 0; s_edge_stalled = 0;
|
||||
s_current_fsm_state = IPERF_STATE_IDLE;
|
||||
|
|
@ -416,7 +424,6 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|||
int64_t packet_id = 0;
|
||||
struct timespec ts;
|
||||
|
||||
// Calculate period based on PPS (Target Period)
|
||||
uint32_t period_us = (ctrl->cfg.target_pps > 0) ? (1000000 / ctrl->cfg.target_pps) : 10000;
|
||||
if (period_us < MIN_PACING_INTERVAL_US) period_us = MIN_PACING_INTERVAL_US;
|
||||
|
||||
|
|
@ -424,15 +431,8 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|||
int64_t now = esp_timer_get_time();
|
||||
int64_t wait = next_send_time - now;
|
||||
|
||||
// 1. Sleep if gap is large
|
||||
if (wait > 2000) {
|
||||
vTaskDelay(pdMS_TO_TICKS(wait / 1000));
|
||||
}
|
||||
|
||||
// 2. Spin until exact time (Strict Monotonic enforcement)
|
||||
while (esp_timer_get_time() < next_send_time) {
|
||||
taskYIELD();
|
||||
}
|
||||
if (wait > 2000) vTaskDelay(pdMS_TO_TICKS(wait / 1000));
|
||||
while (esp_timer_get_time() < next_send_time) taskYIELD();
|
||||
|
||||
if (xEventGroupGetBits(s_iperf_event_group) & IPERF_STOP_REQ_BIT) break;
|
||||
|
||||
|
|
@ -446,13 +446,21 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|||
udp_hdr->tv_sec = htonl((uint32_t)ts.tv_sec);
|
||||
udp_hdr->tv_usec = htonl(ts.tv_nsec / 1000);
|
||||
|
||||
if (sendto(sockfd, ctrl->buffer, ctrl->cfg.send_len, 0, (struct sockaddr *)&addr, sizeof(addr)) > 0) {
|
||||
s_session_packets++;
|
||||
int ret = sendto(sockfd, ctrl->buffer, ctrl->cfg.send_len, 0, (struct sockaddr *)&addr, sizeof(addr));
|
||||
|
||||
if (ret > 0) {
|
||||
s_stats.total_packets++;
|
||||
packets_since_check++;
|
||||
} else {
|
||||
if (errno == ENOMEM) s_stats.err_mem++;
|
||||
else {
|
||||
if (errno == EHOSTUNREACH) s_stats.err_route++;
|
||||
else s_stats.err_other++;
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FSM STATS LOGIC
|
||||
now = esp_timer_get_time();
|
||||
if (now - last_rate_check > RATE_CHECK_INTERVAL_US) {
|
||||
uint32_t interval_us = (uint32_t)(now - last_rate_check);
|
||||
|
|
@ -482,17 +490,31 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|||
s_current_fsm_state = next_state;
|
||||
}
|
||||
|
||||
led_state_t led_target = (s_current_fsm_state == IPERF_STATE_TX) ? LED_STATE_TRANSMITTING : LED_STATE_TRANSMITTING_SLOW;
|
||||
if (status_led_get_state() != led_target) status_led_set_state(led_target);
|
||||
led_state_t led_target = LED_STATE_TRANSMITTING;
|
||||
if (next_state == IPERF_STATE_TX_SLOW) led_target = LED_STATE_TRANSMITTING_SLOW;
|
||||
if (next_state == IPERF_STATE_TX_STALLED) led_target = LED_STATE_STALLED;
|
||||
status_led_set_state(led_target);
|
||||
}
|
||||
last_rate_check = now;
|
||||
packets_since_check = 0;
|
||||
}
|
||||
|
||||
// MONOTONIC UPDATE
|
||||
next_send_time += period_us;
|
||||
}
|
||||
|
||||
int64_t final_id = -packet_id;
|
||||
udp_hdr->id = htonl((uint32_t)(final_id & 0xFFFFFFFF));
|
||||
udp_hdr->id2 = htonl((uint32_t)((final_id >> 32) & 0xFFFFFFFF));
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
udp_hdr->tv_sec = htonl((uint32_t)ts.tv_sec);
|
||||
udp_hdr->tv_usec = htonl(ts.tv_nsec / 1000);
|
||||
|
||||
for (int i=0; i<10; i++) {
|
||||
sendto(sockfd, ctrl->buffer, ctrl->cfg.send_len, 0, (struct sockaddr *)&addr, sizeof(addr));
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
}
|
||||
ESP_LOGI(TAG, "Sent termination (ID: %" PRId64 ")", final_id);
|
||||
|
||||
close(sockfd);
|
||||
s_stats.running = false;
|
||||
s_session_end_time = esp_timer_get_time();
|
||||
|
|
@ -536,11 +558,9 @@ void iperf_start(void) {
|
|||
return;
|
||||
}
|
||||
|
||||
// Copy Staging -> Active
|
||||
s_iperf_ctrl.cfg = s_staging_cfg;
|
||||
s_iperf_ctrl.finish = false;
|
||||
|
||||
// Allocate Buffer
|
||||
s_iperf_ctrl.buffer_len = s_iperf_ctrl.cfg.send_len + 128;
|
||||
s_iperf_ctrl.buffer = calloc(1, s_iperf_ctrl.buffer_len);
|
||||
if (s_iperf_ctrl.buffer) {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,41 @@
|
|||
/*
|
||||
* iperf.h
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef IPERF_H
|
||||
#define IPERF_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_err.h"
|
||||
#include "led_strip.h"
|
||||
|
||||
// --- Configuration Flags ---
|
||||
|
|
@ -11,23 +44,14 @@
|
|||
#define IPERF_FLAG_TCP (1 << 2)
|
||||
#define IPERF_FLAG_UDP (1 << 3)
|
||||
|
||||
// --- Standard Iperf2 Header Flags ---
|
||||
#define HEADER_VERSION1 0x80000000
|
||||
#define HEADER_EXTEND 0x40000000
|
||||
#define HEADER_UDPTESTS 0x20000000
|
||||
#define HEADER_SEQNO64B 0x08000000
|
||||
|
||||
// --- Defaults ---
|
||||
#define IPERF_DEFAULT_PORT 5001
|
||||
#define IPERF_DEFAULT_INTERVAL 3
|
||||
#define IPERF_DEFAULT_TIME 30
|
||||
#define IPERF_UDP_TX_LEN 1470
|
||||
|
||||
typedef struct {
|
||||
uint32_t flag;
|
||||
uint32_t dip; // Destination IP
|
||||
uint16_t dport; // Destination Port
|
||||
uint32_t time; // Duration (seconds), 0 = infinite
|
||||
uint32_t target_pps; // Packets Per Second (Replaces period)
|
||||
uint32_t burst_count; // Packets per RTOS tick
|
||||
uint32_t send_len; // Packet payload length
|
||||
|
|
@ -37,7 +61,10 @@ typedef struct {
|
|||
bool running;
|
||||
uint32_t config_pps;
|
||||
uint32_t actual_pps;
|
||||
float error_rate;
|
||||
uint64_t total_packets;
|
||||
uint32_t err_mem; // ENOMEM (12)
|
||||
uint32_t err_route; // EHOSTUNREACH (118)
|
||||
uint32_t err_other; // All other errors
|
||||
} iperf_stats_t;
|
||||
|
||||
// --- API ---
|
||||
|
|
@ -56,7 +83,7 @@ esp_err_t iperf_param_save(bool *out_changed);
|
|||
bool iperf_param_is_unsaved(void);
|
||||
|
||||
// Control
|
||||
void iperf_start(void); // Uses current Running Config
|
||||
void iperf_start(void);
|
||||
void iperf_stop(void);
|
||||
void iperf_print_status(void);
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ static void led_task(void *arg) {
|
|||
case LED_STATE_TRANSMITTING: // Fast Purple Flash (Busy)
|
||||
set_color(toggle ? 50 : 0, 0, toggle ? 50 : 0);
|
||||
toggle = !toggle;
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
break;
|
||||
case LED_STATE_TRANSMITTING_SLOW: // Slow Purple Pulse (Pacing)
|
||||
set_color(toggle ? 50 : 0, 0, toggle ? 50 : 0);
|
||||
|
|
|
|||
Loading…
Reference in New Issue