561 lines
18 KiB
C
561 lines
18 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <inttypes.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/time.h>
|
|
#include <time.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_log.h"
|
|
#include "esp_err.h"
|
|
#include "esp_timer.h"
|
|
#include "nvs_flash.h"
|
|
#include "nvs.h"
|
|
#include "esp_event.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_wifi.h"
|
|
#include "iperf.h"
|
|
#include "status_led.h"
|
|
|
|
static const char *TAG = "iperf";
|
|
|
|
// --- NVS Keys ---
|
|
#define NVS_KEY_IPERF_ENABLE "iperf_enabled"
|
|
#define NVS_KEY_IPERF_PPS "iperf_pps"
|
|
#define NVS_KEY_IPERF_ROLE "iperf_role"
|
|
#define NVS_KEY_IPERF_DST_IP "iperf_dst_ip"
|
|
#define NVS_KEY_IPERF_PORT "iperf_port"
|
|
#define NVS_KEY_IPERF_PROTO "iperf_proto"
|
|
#define NVS_KEY_IPERF_BURST "iperf_burst"
|
|
#define NVS_KEY_IPERF_LEN "iperf_len"
|
|
|
|
// --- Global Config State ---
|
|
static iperf_cfg_t s_staging_cfg = {0}; // The "Running" Config
|
|
static bool s_staging_initialized = false;
|
|
|
|
static EventGroupHandle_t s_iperf_event_group = NULL;
|
|
#define IPERF_IP_READY_BIT (1 << 0)
|
|
#define IPERF_STOP_REQ_BIT (1 << 1)
|
|
|
|
#define RATE_CHECK_INTERVAL_US 500000
|
|
#define MIN_PACING_INTERVAL_US 100
|
|
|
|
// --- Runtime Control ---
|
|
typedef struct {
|
|
iperf_cfg_t cfg;
|
|
bool finish;
|
|
uint32_t buffer_len;
|
|
uint8_t *buffer;
|
|
} iperf_ctrl_t;
|
|
|
|
static iperf_ctrl_t s_iperf_ctrl = {0}; // The "Active" Config (while task runs)
|
|
static TaskHandle_t s_iperf_task_handle = NULL;
|
|
static bool s_reload_req = false;
|
|
|
|
// Global Stats Tracker
|
|
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 {
|
|
IPERF_STATE_IDLE = 0,
|
|
IPERF_STATE_TX,
|
|
IPERF_STATE_TX_SLOW,
|
|
IPERF_STATE_TX_STALLED
|
|
} iperf_fsm_state_t;
|
|
|
|
static iperf_fsm_state_t s_current_fsm_state = IPERF_STATE_IDLE;
|
|
|
|
static int64_t s_time_tx_us = 0;
|
|
static int64_t s_time_slow_us = 0;
|
|
static int64_t s_time_stalled_us = 0;
|
|
|
|
static uint32_t s_edge_tx = 0;
|
|
static uint32_t s_edge_slow = 0;
|
|
static uint32_t s_edge_stalled = 0;
|
|
|
|
static esp_event_handler_instance_t instance_any_id;
|
|
static esp_event_handler_instance_t instance_got_ip;
|
|
|
|
// --- Packet Structures ---
|
|
typedef struct {
|
|
int32_t id;
|
|
uint32_t tv_sec;
|
|
uint32_t tv_usec;
|
|
int32_t id2;
|
|
} udp_datagram;
|
|
|
|
typedef struct {
|
|
int32_t flags;
|
|
int32_t numThreads;
|
|
int32_t mPort;
|
|
int32_t mBufLen;
|
|
int32_t mWinBand;
|
|
int32_t mAmount;
|
|
} client_hdr_v1;
|
|
|
|
|
|
// --- Helper: Defaults ---
|
|
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->dport = IPERF_DEFAULT_PORT;
|
|
cfg->time = 0; // Infinite
|
|
cfg->target_pps = 100; // Default 100 PPS
|
|
cfg->burst_count = 1;
|
|
cfg->send_len = IPERF_UDP_TX_LEN;
|
|
}
|
|
|
|
// --- Parameter Management (Init / Load / Save / Get / Set) ---
|
|
|
|
static void trim_whitespace(char *str) {
|
|
char *end = str + strlen(str) - 1;
|
|
while(end > str && isspace((unsigned char)*end)) end--;
|
|
*(end+1) = 0;
|
|
}
|
|
|
|
// Clear NVS
|
|
|
|
void iperf_param_clear(void) {
|
|
nvs_handle_t h;
|
|
if (nvs_open("storage", NVS_READWRITE, &h) == ESP_OK) {
|
|
nvs_erase_key(h, NVS_KEY_IPERF_PPS);
|
|
nvs_erase_key(h, NVS_KEY_IPERF_BURST);
|
|
nvs_erase_key(h, NVS_KEY_IPERF_LEN);
|
|
nvs_erase_key(h, NVS_KEY_IPERF_PORT);
|
|
nvs_erase_key(h, NVS_KEY_IPERF_DST_IP);
|
|
nvs_commit(h);
|
|
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_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;
|
|
|
|
size_t req;
|
|
if (nvs_get_str(h, NVS_KEY_IPERF_DST_IP, NULL, &req) == ESP_OK) {
|
|
char *ip_str = malloc(req);
|
|
if (ip_str) {
|
|
nvs_get_str(h, NVS_KEY_IPERF_DST_IP, ip_str, &req);
|
|
trim_whitespace(ip_str);
|
|
s_staging_cfg.dip = inet_addr(ip_str);
|
|
free(ip_str);
|
|
}
|
|
}
|
|
nvs_close(h);
|
|
} else {
|
|
ESP_LOGI(TAG, "No saved config found, using defaults.");
|
|
}
|
|
|
|
s_staging_initialized = true;
|
|
}
|
|
|
|
void iperf_param_get(iperf_cfg_t *out_cfg) {
|
|
if (!s_staging_initialized) iperf_param_init();
|
|
*out_cfg = s_staging_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;
|
|
|
|
// 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;
|
|
|
|
// 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);
|
|
|
|
char ip_str[32];
|
|
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);
|
|
if (err == ESP_OK && out_changed) *out_changed = true;
|
|
|
|
nvs_close(h);
|
|
return err;
|
|
}
|
|
|
|
// --- Status & Helpers ---
|
|
|
|
void iperf_get_stats(iperf_stats_t *stats) {
|
|
if (stats) {
|
|
s_stats.config_pps = s_iperf_ctrl.cfg.target_pps;
|
|
*stats = s_stats;
|
|
}
|
|
}
|
|
|
|
void iperf_print_status(void) {
|
|
iperf_get_stats(&s_stats);
|
|
|
|
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;
|
|
|
|
double pct_tx = ((double)s_time_tx_us / total_us) * 100.0;
|
|
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;
|
|
avg_bw_mbps = (float)(total_bits / duration_sec / 1000000.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
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,
|
|
avg_bw_mbps,
|
|
s_stats.running);
|
|
|
|
printf("STATES: TX=%.2fs/%.1f%% (%lu), SLOW=%.2fs/%.1f%% (%lu), STALLED=%.2fs/%.1f%% (%lu)\n",
|
|
(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);
|
|
}
|
|
|
|
// --- 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';
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
static void iperf_network_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
|
if (s_iperf_event_group == NULL) return;
|
|
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
xEventGroupSetBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
|
}
|
|
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
xEventGroupClearBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
|
status_led_set_state(LED_STATE_NO_CONFIG);
|
|
}
|
|
}
|
|
|
|
static bool iperf_wait_for_ip(void) {
|
|
if (!s_iperf_event_group) s_iperf_event_group = xEventGroupCreate();
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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));
|
|
|
|
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);
|
|
|
|
esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id);
|
|
esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip);
|
|
|
|
if (bits & IPERF_STOP_REQ_BIT) return false;
|
|
return true;
|
|
}
|
|
|
|
static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) {
|
|
if (!iperf_wait_for_ip()) return ESP_OK;
|
|
|
|
struct sockaddr_in addr;
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons(ctrl->cfg.dport);
|
|
addr.sin_addr.s_addr = ctrl->cfg.dip;
|
|
|
|
int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
if (sockfd < 0) {
|
|
ESP_LOGE(TAG, "Socket failed: %d", errno);
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
status_led_set_state(LED_STATE_TRANSMITTING_SLOW);
|
|
|
|
udp_datagram *udp_hdr = (udp_datagram *)ctrl->buffer;
|
|
client_hdr_v1 *client_hdr = (client_hdr_v1 *)(ctrl->buffer + sizeof(udp_datagram));
|
|
iperf_generate_client_hdr(&ctrl->cfg, client_hdr);
|
|
|
|
s_stats.running = true;
|
|
s_session_start_time = esp_timer_get_time();
|
|
s_session_packets = 0;
|
|
|
|
// 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;
|
|
|
|
ESP_LOGI(TAG, "UDP Started. Target: %s", inet_ntoa(addr.sin_addr));
|
|
|
|
int64_t next_send_time = esp_timer_get_time();
|
|
int64_t last_rate_check = esp_timer_get_time();
|
|
uint32_t packets_since_check = 0;
|
|
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;
|
|
|
|
while (!ctrl->finish && !s_reload_req) {
|
|
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 (xEventGroupGetBits(s_iperf_event_group) & IPERF_STOP_REQ_BIT) break;
|
|
|
|
for (int k = 0; k < ctrl->cfg.burst_count; k++) {
|
|
int64_t current_id = packet_id++;
|
|
|
|
udp_hdr->id = htonl((uint32_t)(current_id & 0xFFFFFFFF));
|
|
udp_hdr->id2 = htonl((uint32_t)((current_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);
|
|
|
|
if (sendto(sockfd, ctrl->buffer, ctrl->cfg.send_len, 0, (struct sockaddr *)&addr, sizeof(addr)) > 0) {
|
|
s_session_packets++;
|
|
packets_since_check++;
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
if (interval_us > 0) {
|
|
s_stats.actual_pps = (uint32_t)((uint64_t)packets_since_check * 1000000 / interval_us);
|
|
uint32_t threshold = (ctrl->cfg.target_pps * 3) / 4;
|
|
|
|
iperf_fsm_state_t next_state;
|
|
if (s_stats.actual_pps == 0) next_state = IPERF_STATE_TX_STALLED;
|
|
else if (s_stats.actual_pps >= threshold) next_state = IPERF_STATE_TX;
|
|
else next_state = IPERF_STATE_TX_SLOW;
|
|
|
|
switch (next_state) {
|
|
case IPERF_STATE_TX: s_time_tx_us += interval_us; break;
|
|
case IPERF_STATE_TX_SLOW: s_time_slow_us += interval_us; break;
|
|
case IPERF_STATE_TX_STALLED: s_time_stalled_us += interval_us; break;
|
|
default: break;
|
|
}
|
|
|
|
if (next_state != s_current_fsm_state) {
|
|
switch (next_state) {
|
|
case IPERF_STATE_TX: s_edge_tx++; break;
|
|
case IPERF_STATE_TX_SLOW: s_edge_slow++; break;
|
|
case IPERF_STATE_TX_STALLED: s_edge_stalled++; break;
|
|
default: break;
|
|
}
|
|
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);
|
|
}
|
|
last_rate_check = now;
|
|
packets_since_check = 0;
|
|
}
|
|
|
|
// MONOTONIC UPDATE
|
|
next_send_time += period_us;
|
|
}
|
|
|
|
close(sockfd);
|
|
s_stats.running = false;
|
|
s_session_end_time = esp_timer_get_time();
|
|
status_led_set_state(LED_STATE_CONNECTED);
|
|
return ESP_OK;
|
|
}
|
|
|
|
static void iperf_task(void *arg) {
|
|
iperf_ctrl_t *ctrl = (iperf_ctrl_t *)arg;
|
|
|
|
while (1) {
|
|
s_reload_req = false;
|
|
ctrl->finish = false;
|
|
xEventGroupClearBits(s_iperf_event_group, IPERF_STOP_REQ_BIT);
|
|
|
|
iperf_start_udp_client(ctrl);
|
|
|
|
if (s_reload_req) {
|
|
ESP_LOGI(TAG, "Task reloading config...");
|
|
if (ctrl->buffer_len < ctrl->cfg.send_len + 128) {
|
|
free(ctrl->buffer);
|
|
ctrl->buffer_len = ctrl->cfg.send_len + 128;
|
|
ctrl->buffer = calloc(1, ctrl->buffer_len);
|
|
iperf_pattern(ctrl->buffer, ctrl->buffer_len);
|
|
}
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
free(ctrl->buffer);
|
|
s_iperf_task_handle = NULL;
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
void iperf_start(void) {
|
|
if (!s_staging_initialized) iperf_param_init();
|
|
|
|
if (s_iperf_task_handle) {
|
|
ESP_LOGW(TAG, "Already running. Use 'set' to update parameters.");
|
|
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) {
|
|
iperf_pattern(s_iperf_ctrl.buffer, s_iperf_ctrl.buffer_len);
|
|
}
|
|
|
|
if (s_iperf_event_group == NULL) s_iperf_event_group = xEventGroupCreate();
|
|
|
|
xTaskCreate(iperf_task, "iperf", 4096, &s_iperf_ctrl, 5, &s_iperf_task_handle);
|
|
}
|
|
|
|
void iperf_stop(void) {
|
|
if (s_iperf_task_handle) {
|
|
s_iperf_ctrl.finish = true;
|
|
if (s_iperf_event_group) xEventGroupSetBits(s_iperf_event_group, IPERF_STOP_REQ_BIT);
|
|
}
|
|
}
|