From 1ea05536ab43aaea4dd891e6730f24ead16f9cea Mon Sep 17 00:00:00 2001 From: Bob Date: Wed, 10 Dec 2025 15:59:02 -0800 Subject: [PATCH] working traffic - purple LED still not working --- components/iperf/iperf.c | 166 ++++++++++++++++++++++++++------------- 1 file changed, 111 insertions(+), 55 deletions(-) diff --git a/components/iperf/iperf.c b/components/iperf/iperf.c index f45f842..1400e9c 100644 --- a/components/iperf/iperf.c +++ b/components/iperf/iperf.c @@ -6,6 +6,8 @@ #include #include #include +#include "freertos/task.h" +#include "led_strip.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" @@ -21,11 +23,9 @@ static const char *TAG = "iperf"; -// --- LED Handle (Private Static) --- -// This solves the linker error. We set this via iperf_init_led() +// --- LED Handle --- static led_strip_handle_t s_led_strip = NULL; -// --- Init Function --- void iperf_init_led(led_strip_handle_t handle) { s_led_strip = handle; } @@ -36,6 +36,7 @@ typedef enum { LED_GREEN_SOLID, LED_PURPLE_SOLID, LED_PURPLE_FLASH } led_state_t; +// Volatile ensures immediate visibility static volatile led_state_t s_led_state = LED_RED_FLASH; static void iperf_set_physical_led(uint8_t r, uint8_t g, uint8_t b) { @@ -45,17 +46,43 @@ static void iperf_set_physical_led(uint8_t r, uint8_t g, uint8_t b) { } } +// --- LED Task (Aggressive Refresh) --- 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; + 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: + // Refresh Green less often to avoid bus contention + iperf_set_physical_led(0, 64, 0); + vTaskDelay(pdMS_TO_TICKS(1000)); + break; + case LED_PURPLE_SOLID: + // Aggressive refresh to overwrite main.c + iperf_set_physical_led(64, 0, 64); + vTaskDelay(pdMS_TO_TICKS(50)); + break; + case LED_PURPLE_FLASH: + iperf_set_physical_led(toggle ? 64 : 0, 0, 64); + vTaskDelay(pdMS_TO_TICKS(100)); + toggle = !toggle; + break; + default: + iperf_set_physical_led(0, 0, 0); + vTaskDelay(pdMS_TO_TICKS(500)); + break; } } } @@ -81,15 +108,16 @@ 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; - // Only update state if we are NOT currently running the test (Purple) - bool is_running = (s_led_state == LED_PURPLE_SOLID || s_led_state == LED_PURPLE_FLASH); + // Prevent network events from overwriting the "Active Transmit" state + bool is_active_transmit = (s_led_state == LED_PURPLE_SOLID || s_led_state == LED_PURPLE_FLASH); if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) { - if (!is_running) s_led_state = LED_AMBER_SOLID; + if (!is_active_transmit) 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 (!is_running) s_led_state = LED_GREEN_SOLID; + // Only go green if we aren't already running the test + if (!is_active_transmit) 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); @@ -107,7 +135,7 @@ static bool iperf_wait_for_ip(void) { 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); - // Don't set Green here, let the loop handle the transition + // Don't change LED here, let event handler or transmit loop do it } } ESP_LOGI(TAG, "Waiting for IP address..."); @@ -159,12 +187,11 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) { free(ip_str); } } - + // ... Role/Proto ... 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); @@ -174,7 +201,6 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) { 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); @@ -183,6 +209,22 @@ static void iperf_read_nvs_config(iperf_cfg_t *cfg) { nvs_close(my_handle); } +#if 0 +static void print_all_task_priorities(void) { + char *task_list_buffer = malloc(1024); // Allocate buffer for list + if (task_list_buffer) { + // vTaskList populates the buffer with: Name, State, Priority, Stack, TaskNum + vTaskList(task_list_buffer); + ESP_LOGI(TAG, "\nTask List:\nName\t\tState\tPrio\tStack\tNum\n%s", task_list_buffer); + free(task_list_buffer); + } else { + ESP_LOGE(TAG, "Failed to allocate buffer for task list"); + } +} + +// Note: You must ensure CONFIG_FREERTOS_USE_TRACE_FACILITY and CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS are enabled in your menuconfig for vTaskList to work. If they aren't, this function will be empty or not compile. If you can't change menuconfig, let me know, and I can give you a simpler way to just check the current task's priority. +#endif + 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; } @@ -195,7 +237,7 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) struct sockaddr_in addr; int sockfd; - struct timeval tv; + struct timespec ts; sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sockfd < 0) { @@ -219,55 +261,69 @@ static esp_err_t iperf_start_udp_client(iperf_ctrl_t *ctrl) 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); - // --- FORCE PURPLE (TRANSMITTING) --- +#if 0 + print_all_task_priorities(); +#endif + // Force LED to Purple immediately s_led_state = LED_PURPLE_SOLID; - iperf_set_physical_led(64, 0, 64); // Immediate feedback + iperf_set_physical_led(64, 0, 64); 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(); + int64_t time_to_wait = next_send_time - current_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; + if (time_to_wait > 0) { + // If the wait is long (> 2ms), sleep to save power and let lower priority tasks run + if (time_to_wait > 2000) { + vTaskDelay(pdMS_TO_TICKS(time_to_wait / 1000)); + } + // If the wait is short, spin but yield to other ready tasks (like WiFi/TCP-IP) + else { + while (esp_timer_get_time() < next_send_time) { + taskYIELD(); } } - 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)); } + + for (int k = 0; k < burst_count; k++) { + udp_datagram *header = (udp_datagram *)ctrl->buffer; + clock_gettime(CLOCK_MONOTONIC, &ts); + header->id = htonl(packet_count); + header->tv_sec = htonl(ts.tv_sec); + header->tv_usec = htonl(ts.tv_nsec / 1000); + 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(ntohs(addr.sin_port)); + 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; } exit_client: @@ -309,7 +365,6 @@ void iperf_start(iperf_cfg_t *cfg) { } nvs_close(my_handle); } - if (enabled == 0) return; if (s_iperf_task_handle != NULL) return; @@ -319,6 +374,7 @@ void iperf_start(iperf_cfg_t *cfg) { iperf_read_nvs_config(&s_iperf_ctrl.cfg); s_iperf_ctrl.finish = false; + // 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;