diff --git a/examples/ethernet/iperf/main/ethernet_iperf_main.c b/examples/ethernet/iperf/main/ethernet_iperf_main.c index a1259dd0c1c..04155b54768 100644 --- a/examples/ethernet/iperf/main/ethernet_iperf_main.c +++ b/examples/ethernet/iperf/main/ethernet_iperf_main.c @@ -23,6 +23,8 @@ static const char *TAG = "eth_example"; static esp_eth_handle_t *s_eth_handles = NULL; static uint8_t s_eth_port_cnt = 0; +static SemaphoreHandle_t ip_got_sem; + #if CONFIG_EXAMPLE_STORE_HISTORY #define MOUNT_PATH "/data" @@ -43,8 +45,20 @@ static void initialize_filesystem(void) } #endif // CONFIG_EXAMPLE_STORE_HISTORY +static void got_ip_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + xSemaphoreGive(ip_got_sem); +} + void init_ethernet_and_netif(void) { + ip_got_sem = xSemaphoreCreateBinary(); + if (ip_got_sem == NULL) { + ESP_LOGE(TAG, "Failed to create semaphore"); + return; + } + ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(example_eth_init(&s_eth_handles, &s_eth_port_cnt)); @@ -71,9 +85,15 @@ void init_ethernet_and_netif(void) ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(s_eth_handles[i]))); } + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); + for (int i = 0; i < s_eth_port_cnt; i++) { ESP_ERROR_CHECK(esp_eth_start(s_eth_handles[i])); } + + if (xSemaphoreTake(ip_got_sem, portMAX_DELAY) != pdTRUE) { + ESP_LOGE(TAG, "Timeout waiting for ETH IP"); + } } void app_main(void) diff --git a/examples/ethernet/iperf/pytest_eth_iperf.py b/examples/ethernet/iperf/pytest_eth_iperf.py index 36fcc53b356..5fa3a956e70 100644 --- a/examples/ethernet/iperf/pytest_eth_iperf.py +++ b/examples/ethernet/iperf/pytest_eth_iperf.py @@ -30,6 +30,12 @@ except ImportError: NO_BANDWIDTH_LIMIT = -1 # iperf send bandwidth is not limited +def get_ip_and_wait_prompt(dut: Dut) -> Any: + dut_ip = dut.expect(r'esp_netif_handlers: .+ ip: (\d+\.\d+\.\d+\.\d+),').group(1) + dut.expect('iperf>') + return dut_ip + + class IperfTestUtilityEth(IperfUtility.IperfTestUtility): """iperf test implementation""" @@ -52,9 +58,7 @@ class IperfTestUtilityEth(IperfUtility.IperfTestUtility): except subprocess.CalledProcessError: pass self.dut.write('restart') - self.dut.expect("Type 'help' to get the list of commands.") - self.dut.expect('iperf>') - dut_ip = self.dut.expect(r'esp_netif_handlers: .+ ip: (\d+\.\d+\.\d+\.\d+),').group(1) + dut_ip = get_ip_and_wait_prompt(self.dut) rssi = 0 return dut_ip, rssi @@ -73,12 +77,11 @@ def test_esp_eth_iperf( 2. compare with the pre-defined pass standard """ - # 1. wait for DUT - dut.expect_exact('iperf>') + # 1. wait for DUT to be ready + dut_ip = get_ip_and_wait_prompt(dut) # 2. preparing pc_iperf_log_file = os.path.join(dut.logdir, 'pc_iperf_log.md') - dut_ip = dut.expect(r'esp_netif_handlers: .+ ip: (\d+\.\d+\.\d+\.\d+),').group(1) pc_nic_ip = get_host_ip4_by_dest_ip(dut_ip) test_result = { 'tcp_tx': IperfUtility.TestResult('tcp', 'tx', 'ethernet'),