more on udp iperf
This commit is contained in:
parent
5ad67e71c5
commit
4c1ec326f3
|
|
@ -1,5 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
|
@ -18,30 +19,24 @@
|
|||
#include "esp_wifi.h"
|
||||
#include "iperf.h"
|
||||
|
||||
// --- LED DRIVER PLACEHOLDER ---
|
||||
// #include "led_strip.h"
|
||||
|
||||
static const char *TAG = "iperf";
|
||||
|
||||
// --- LED STATE MANAGEMENT ---
|
||||
typedef enum {
|
||||
LED_OFF,
|
||||
LED_BLUE_SOLID, // Monitor Mode
|
||||
LED_RED_FLASH, // No WiFi
|
||||
LED_AMBER_SOLID, // Connected, No IP
|
||||
LED_GREEN_SOLID, // Got IP / Ready
|
||||
LED_PURPLE_SOLID, // Transmitting
|
||||
LED_PURPLE_FLASH // Socket Error
|
||||
LED_BLUE_SOLID,
|
||||
LED_RED_FLASH,
|
||||
LED_AMBER_SOLID,
|
||||
LED_GREEN_SOLID,
|
||||
LED_PURPLE_SOLID,
|
||||
LED_PURPLE_FLASH
|
||||
} led_state_t;
|
||||
|
||||
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) {
|
||||
// Hardware LED set call here
|
||||
// Hardware LED call here
|
||||
}
|
||||
|
||||
// --- LED Task ---
|
||||
static void status_led_task(void *arg) {
|
||||
bool toggle = false;
|
||||
while (1) {
|
||||
|
|
@ -57,7 +52,6 @@ static void status_led_task(void *arg) {
|
|||
}
|
||||
}
|
||||
|
||||
// --- Synchronization ---
|
||||
static EventGroupHandle_t s_iperf_event_group = NULL;
|
||||
#define IPERF_IP_READY_BIT (1 << 0)
|
||||
#define IPERF_STOP_REQ_BIT (1 << 1)
|
||||
|
|
@ -76,38 +70,28 @@ static TaskHandle_t s_iperf_task_handle = NULL;
|
|||
static esp_event_handler_instance_t instance_any_id;
|
||||
static esp_event_handler_instance_t instance_got_ip;
|
||||
|
||||
// --- Network Event Handler ---
|
||||
static void iperf_network_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
|
||||
{
|
||||
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 == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
|
||||
s_led_state = LED_AMBER_SOLID;
|
||||
}
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
xEventGroupSetBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
||||
if (s_led_state != LED_PURPLE_SOLID && s_led_state != LED_PURPLE_FLASH) {
|
||||
s_led_state = LED_GREEN_SOLID;
|
||||
}
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if (s_led_state != LED_PURPLE_SOLID && s_led_state != LED_PURPLE_FLASH) s_led_state = LED_GREEN_SOLID;
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
xEventGroupClearBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
||||
s_led_state = LED_RED_FLASH;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Wait for IP ---
|
||||
static bool iperf_wait_for_ip(void) {
|
||||
if (!s_iperf_event_group) s_iperf_event_group = xEventGroupCreate();
|
||||
|
||||
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_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) {
|
||||
if (ip_info.ip.addr != 0) {
|
||||
if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
|
||||
xEventGroupSetBits(s_iperf_event_group, IPERF_IP_READY_BIT);
|
||||
s_led_state = LED_GREEN_SOLID;
|
||||
} else {
|
||||
|
|
@ -115,19 +99,22 @@ static bool iperf_wait_for_ip(void) {
|
|||
if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) s_led_state = LED_AMBER_SOLID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Waiting for IP address...");
|
||||
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;
|
||||
}
|
||||
|
||||
// --- Read NVS ---
|
||||
static void trim_whitespace(char *str) {
|
||||
char *end;
|
||||
end = str + strlen(str) - 1;
|
||||
while(end > str && isspace((unsigned char)*end)) end--;
|
||||
*(end+1) = 0;
|
||||
}
|
||||
|
||||
static void iperf_read_nvs_config(iperf_cfg_t *cfg) {
|
||||
nvs_handle_t my_handle;
|
||||
esp_err_t err = nvs_open("storage", NVS_READONLY, &my_handle);
|
||||
|
|
@ -135,21 +122,24 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) {
|
|||
size_t required_size;
|
||||
uint32_t val = 0;
|
||||
|
||||
// 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
|
||||
// NO bw_lim/IPERF_RATE check here anymore (Deleted)
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
if (nvs_get_str(my_handle, NVS_KEY_IPERF_DST_IP, NULL, &required_size) == ESP_OK) {
|
||||
char *ip_str = malloc(required_size);
|
||||
if (ip_str) { nvs_get_str(my_handle, NVS_KEY_IPERF_DST_IP, ip_str, &required_size); cfg->dip = inet_addr(ip_str); free(ip_str); }
|
||||
if (ip_str) {
|
||||
nvs_get_str(my_handle, NVS_KEY_IPERF_DST_IP, ip_str, &required_size);
|
||||
trim_whitespace(ip_str); cfg->dip = inet_addr(ip_str); ESP_LOGI(TAG, "NVS Target IP: %s", ip_str); free(ip_str);
|
||||
}
|
||||
}
|
||||
if (nvs_get_str(my_handle, NVS_KEY_IPERF_ROLE, NULL, &required_size) == ESP_OK) {
|
||||
char *role = malloc(required_size);
|
||||
if (role) {
|
||||
nvs_get_str(my_handle, NVS_KEY_IPERF_ROLE, role, &required_size);
|
||||
nvs_get_str(my_handle, NVS_KEY_IPERF_ROLE, role, &required_size); trim_whitespace(role);
|
||||
ESP_LOGI(TAG, "NVS Role: '%s'", role);
|
||||
if (strcmp(role, "SERVER") == 0) { cfg->flag &= ~IPERF_FLAG_CLIENT; cfg->flag |= IPERF_FLAG_SERVER; }
|
||||
else { cfg->flag &= ~IPERF_FLAG_SERVER; cfg->flag |= IPERF_FLAG_CLIENT; }
|
||||
free(role);
|
||||
|
|
@ -158,7 +148,8 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) {
|
|||
if (nvs_get_str(my_handle, NVS_KEY_IPERF_PROTO, NULL, &required_size) == ESP_OK) {
|
||||
char *proto = malloc(required_size);
|
||||
if (proto) {
|
||||
nvs_get_str(my_handle, NVS_KEY_IPERF_PROTO, proto, &required_size);
|
||||
nvs_get_str(my_handle, NVS_KEY_IPERF_PROTO, proto, &required_size); trim_whitespace(proto);
|
||||
ESP_LOGI(TAG, "NVS Proto: '%s'", proto);
|
||||
if (strcmp(proto, "TCP") == 0) { cfg->flag &= ~IPERF_FLAG_UDP; cfg->flag |= IPERF_FLAG_TCP; }
|
||||
else { cfg->flag &= ~IPERF_FLAG_TCP; cfg->flag |= IPERF_FLAG_UDP; }
|
||||
free(proto);
|
||||
|
|
@ -167,16 +158,12 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) {
|
|||
nvs_close(my_handle);
|
||||
}
|
||||
|
||||
// --- 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
|
||||
// -----------------------------------------------------------------------------
|
||||
static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl)
|
||||
{
|
||||
if (!iperf_wait_for_ip()) return ESP_FAIL;
|
||||
|
|
@ -196,18 +183,17 @@ 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;
|
||||
|
||||
// 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);
|
||||
// Calculate Rate just for logging
|
||||
double total_mbps = (double)((uint64_t)burst_count * payload_len * 8 * (1000000.0 / pacing_period_us)) / 1000000.0;
|
||||
|
||||
s_led_state = LED_PURPLE_SOLID; // Transmitting
|
||||
ESP_LOGI(TAG, "UDP Client -> Burst: %" PRIu32 " | Period: %" PRIu32 " us | Payload: %" PRIu32 " | Approx Rate: %.2f Mbps",
|
||||
burst_count, pacing_period_us, payload_len, total_mbps);
|
||||
|
||||
s_led_state = LED_PURPLE_SOLID;
|
||||
|
||||
uint64_t total_len = 0;
|
||||
uint32_t packet_count = 0;
|
||||
|
|
@ -233,7 +219,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(0); // Sent as 0 since we pace by period
|
||||
client_hdr->mWinBand = htonl(0); // Paced by period
|
||||
client_hdr->mAmount = htonl(-(int)(ctrl->cfg.time * 100));
|
||||
}
|
||||
|
||||
|
|
@ -248,14 +234,9 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl)
|
|||
goto exit_client;
|
||||
}
|
||||
}
|
||||
|
||||
// Advance by fixed period
|
||||
// FIXED VARIABLE NAME HERE:
|
||||
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;
|
||||
}
|
||||
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));
|
||||
|
|
@ -268,8 +249,7 @@ exit_client:
|
|||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void iperf_task(void *arg)
|
||||
{
|
||||
static void iperf_task(void *arg) {
|
||||
iperf_ctrl_t *ctrl = (iperf_ctrl_t *)arg;
|
||||
|
||||
if (ctrl->cfg.flag & IPERF_FLAG_TCP) {
|
||||
|
|
@ -282,13 +262,11 @@ static void iperf_task(void *arg)
|
|||
|
||||
if (ctrl->buffer) { free(ctrl->buffer); ctrl->buffer = NULL; }
|
||||
if (s_iperf_event_group) { vEventGroupDelete(s_iperf_event_group); s_iperf_event_group = NULL; }
|
||||
|
||||
s_iperf_task_handle = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void iperf_start(iperf_cfg_t *cfg)
|
||||
{
|
||||
void iperf_start(iperf_cfg_t *cfg) {
|
||||
static bool led_task_started = false;
|
||||
if (!led_task_started) {
|
||||
xTaskCreate(status_led_task, "status_led", 2048, NULL, 1, NULL);
|
||||
|
|
@ -311,10 +289,24 @@ void iperf_start(iperf_cfg_t *cfg)
|
|||
if (s_iperf_task_handle != NULL) return;
|
||||
|
||||
memcpy(&s_iperf_ctrl.cfg, cfg, sizeof(iperf_cfg_t));
|
||||
s_iperf_ctrl.cfg.flag = IPERF_FLAG_CLIENT | IPERF_FLAG_UDP; // Default
|
||||
|
||||
iperf_read_nvs_config(&s_iperf_ctrl.cfg);
|
||||
s_iperf_ctrl.finish = false;
|
||||
|
||||
s_iperf_ctrl.buffer_len = 2048;
|
||||
// Buffer logic
|
||||
uint32_t alloc_len;
|
||||
if (s_iperf_ctrl.cfg.flag & IPERF_FLAG_TCP) {
|
||||
alloc_len = s_iperf_ctrl.cfg.flag & IPERF_FLAG_SERVER ? IPERF_TCP_RX_LEN : IPERF_TCP_TX_LEN;
|
||||
} else {
|
||||
if (s_iperf_ctrl.cfg.flag & IPERF_FLAG_SERVER) alloc_len = IPERF_UDP_RX_LEN;
|
||||
else {
|
||||
alloc_len = s_iperf_ctrl.cfg.send_len > 0 ? s_iperf_ctrl.cfg.send_len : IPERF_UDP_TX_LEN;
|
||||
uint32_t min_hdr = sizeof(udp_datagram) + sizeof(client_hdr_v1);
|
||||
if (alloc_len < min_hdr) alloc_len = min_hdr;
|
||||
}
|
||||
}
|
||||
s_iperf_ctrl.buffer_len = alloc_len;
|
||||
s_iperf_ctrl.buffer = malloc(s_iperf_ctrl.buffer_len);
|
||||
memset(s_iperf_ctrl.buffer, 0, s_iperf_ctrl.buffer_len);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue