#include #include #include #include #include #include #include #include #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" static const char *TAG = "iperf"; typedef enum { LED_OFF, 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; static void iperf_set_physical_led(uint8_t r, uint8_t g, uint8_t b) {} static void status_led_task(void *arg) { bool toggle = false; while (1) { switch (s_led_state) { case LED_BLUE_SOLID: iperf_set_physical_led(0, 0, 64); vTaskDelay(pdMS_TO_TICKS(500)); break; case LED_RED_FLASH: iperf_set_physical_led(toggle ? 64 : 0, 0, 0); vTaskDelay(pdMS_TO_TICKS(250)); toggle = !toggle; break; case LED_AMBER_SOLID: iperf_set_physical_led(32, 16, 0); vTaskDelay(pdMS_TO_TICKS(500)); break; case LED_GREEN_SOLID: iperf_set_physical_led(0, 64, 0); vTaskDelay(pdMS_TO_TICKS(500)); break; case LED_PURPLE_SOLID: iperf_set_physical_led(64, 0, 64); vTaskDelay(pdMS_TO_TICKS(200)); break; case LED_PURPLE_FLASH: iperf_set_physical_led(toggle ? 64 : 0, 0, 64); vTaskDelay(pdMS_TO_TICKS(250)); toggle = !toggle; break; default: iperf_set_physical_led(0, 0, 0); vTaskDelay(pdMS_TO_TICKS(500)); break; } } } static EventGroupHandle_t s_iperf_event_group = NULL; #define IPERF_IP_READY_BIT (1 << 0) #define IPERF_STOP_REQ_BIT (1 << 1) typedef struct { iperf_cfg_t cfg; bool finish; uint32_t total_len; uint32_t buffer_len; uint8_t *buffer; uint32_t sockfd; } iperf_ctrl_t; static iperf_ctrl_t s_iperf_ctrl = {0}; 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; 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) { 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) { xEventGroupClearBits(s_iperf_event_group, IPERF_IP_READY_BIT); s_led_state = LED_RED_FLASH; } } 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 && ip_info.ip.addr != 0) { xEventGroupSetBits(s_iperf_event_group, IPERF_IP_READY_BIT); s_led_state = LED_GREEN_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; } static void trim_whitespace(char *str) { char *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); uint32_t val = 0; size_t required_size; // --- DEFAULTS --- cfg->pacing_period_us = 10000; cfg->burst_count = 1; cfg->send_len = IPERF_UDP_TX_LEN; cfg->dport = 5001; cfg->dip = inet_addr("192.168.1.50"); if (cfg->time == 0) cfg->time = UINT32_MAX; if (err != ESP_OK) return; if (nvs_get_u32(my_handle, NVS_KEY_IPERF_PERIOD, &val) == ESP_OK && val > 0) cfg->pacing_period_us = val; if (nvs_get_u32(my_handle, NVS_KEY_IPERF_BURST, &val) == ESP_OK && val > 0) cfg->burst_count = val; if (nvs_get_u32(my_handle, NVS_KEY_IPERF_LEN, &val) == ESP_OK && val > 0) cfg->send_len = val; // READ PORT if (nvs_get_u32(my_handle, NVS_KEY_IPERF_PORT, &val) == ESP_OK && val > 0) { cfg->dport = (uint16_t)val; ESP_LOGI(TAG, "NVS Port: %d", val); } 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); 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); 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); } } 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); 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); } } nvs_close(my_handle); } 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; } static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) { if (!iperf_wait_for_ip()) return ESP_FAIL; struct sockaddr_in addr; int sockfd; struct timeval tv; sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sockfd < 0) { ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); s_led_state = LED_PURPLE_FLASH; return ESP_FAIL; } addr.sin_family = AF_INET; addr.sin_port = htons(ctrl->cfg.dport); addr.sin_addr.s_addr = ctrl->cfg.dip; char ip_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &addr.sin_addr, ip_str, INET_ADDRSTRLEN); ESP_LOGI(TAG, "Target: %s:%d", ip_str, ntohs(addr.sin_port)); uint32_t burst_count = ctrl->cfg.burst_count; uint32_t payload_len = ctrl->cfg.send_len; uint32_t pacing_period_us = ctrl->cfg.pacing_period_us; double total_mbps = (double)((uint64_t)burst_count * payload_len * 8 * (1000000.0 / pacing_period_us)) / 1000000.0; ESP_LOGI(TAG, "Pacing: %" PRIu32 " pkts every %" PRIu32 " us (Approx %.2f Mbps)", burst_count, pacing_period_us, total_mbps); s_led_state = LED_PURPLE_SOLID; uint64_t total_len = 0; uint32_t packet_count = 0; int64_t start_time_us = esp_timer_get_time(); int64_t next_send_time = start_time_us; int64_t end_time_us = (ctrl->cfg.time == UINT32_MAX) ? INT64_MAX : start_time_us + (int64_t)ctrl->cfg.time * 1000000LL; while (!ctrl->finish && esp_timer_get_time() < end_time_us) { int64_t current_time = esp_timer_get_time(); if (current_time >= next_send_time) { for (int k = 0; k < burst_count; k++) { udp_datagram *header = (udp_datagram *)ctrl->buffer; gettimeofday(&tv, NULL); header->id = htonl(packet_count); header->tv_sec = htonl(tv.tv_sec); header->tv_usec = htonl(tv.tv_usec); header->id2 = 0; if (packet_count == 0) { client_hdr_v1 *client_hdr = (client_hdr_v1 *)(ctrl->buffer + sizeof(udp_datagram)); client_hdr->flags = htonl(HEADER_VERSION1); client_hdr->numThreads = htonl(1); client_hdr->mPort = htonl(ctrl->cfg.dport); client_hdr->mBufLen = htonl(payload_len); client_hdr->mWinBand = htonl(0); client_hdr->mAmount = htonl(-(int)(10000)); } int send_len = sendto(sockfd, ctrl->buffer, payload_len, 0, (struct sockaddr *)&addr, sizeof(addr)); if (send_len > 0) { total_len += send_len; packet_count++; } else { ESP_LOGE(TAG, "UDP send failed: %d", errno); s_led_state = LED_PURPLE_FLASH; goto exit_client; } } next_send_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)); } } exit_client: if (s_led_state != LED_PURPLE_FLASH) s_led_state = LED_GREEN_SOLID; close(sockfd); return ESP_OK; } static void iperf_task(void *arg) { iperf_ctrl_t *ctrl = (iperf_ctrl_t *)arg; if (ctrl->cfg.flag & IPERF_FLAG_TCP) { if (ctrl->cfg.flag & IPERF_FLAG_SERVER) iperf_start_tcp_server(ctrl); else iperf_start_tcp_client(ctrl); } else { if (ctrl->cfg.flag & IPERF_FLAG_SERVER) iperf_start_udp_server(ctrl); else iperf_start_udp_client(ctrl); } 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) { static bool led_task_started = false; if (!led_task_started) { xTaskCreate(status_led_task, "status_led", 2048, NULL, 1, NULL); led_task_started = true; } nvs_handle_t my_handle; uint8_t enabled = 1; if (nvs_open("storage", NVS_READONLY, &my_handle) == ESP_OK) { nvs_get_u8(my_handle, NVS_KEY_IPERF_ENABLE, &enabled); size_t req; if (nvs_get_str(my_handle, "mode", NULL, &req) == ESP_OK) { char m[10]; nvs_get_str(my_handle, "mode", m, &req); if (strcmp(m, "MONITOR") == 0) s_led_state = LED_BLUE_SOLID; } nvs_close(my_handle); } if (enabled == 0) return; 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; iperf_read_nvs_config(&s_iperf_ctrl.cfg); s_iperf_ctrl.finish = false; 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); s_iperf_event_group = xEventGroupCreate(); xTaskCreate(iperf_task, "iperf", 4096, &s_iperf_ctrl, IPERF_TRAFFIC_TASK_PRIORITY, &s_iperf_task_handle); } void iperf_stop(void) { if (s_iperf_task_handle != NULL) { s_iperf_ctrl.finish = true; if (s_iperf_event_group) xEventGroupSetBits(s_iperf_event_group, IPERF_STOP_REQ_BIT); } }