From 099c7c054195abffc5866ff7f736fe5728338df8 Mon Sep 17 00:00:00 2001 From: Steffen Beyer Date: Wed, 14 Aug 2024 21:40:12 +0200 Subject: [PATCH] fix(examples): Timeout waiting for IP addrs in protocol_examples_common For example, in case a unique local address is preferred (via sdkconfig) but not available on the network, this patch prevents initialisation from hanging indefinitely. Instead, we wait up to 10s for an IPv4 address and another 10s for the preferred type of IPv6 address. In case of timeout, a warning is logged and the program continues. --- .../protocol_examples_common/eth_connect.c | 8 ++++++-- .../protocol_examples_common/wifi_connect.c | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/common_components/protocol_examples_common/eth_connect.c b/examples/common_components/protocol_examples_common/eth_connect.c index 8e89dbe28e..b7d792f360 100644 --- a/examples/common_components/protocol_examples_common/eth_connect.c +++ b/examples/common_components/protocol_examples_common/eth_connect.c @@ -232,10 +232,14 @@ esp_err_t example_ethernet_connect(void) eth_start(); ESP_LOGI(TAG, "Waiting for IP(s)."); #if CONFIG_EXAMPLE_CONNECT_IPV4 - xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); + if (xSemaphoreTake(s_semph_get_ip_addrs, pdMS_TO_TICKS(10000)) == pdFALSE) { + ESP_LOGW(TAG, "Gave up waiting for IPv4 address."); + } #endif #if CONFIG_EXAMPLE_CONNECT_IPV6 - xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); + if (xSemaphoreTake(s_semph_get_ip6_addrs, pdMS_TO_TICKS(10000)) == pdFALSE) { + ESP_LOGW(TAG, "Gave up waiting for (preferred) IPv6 address."); + } #endif return ESP_OK; } diff --git a/examples/common_components/protocol_examples_common/wifi_connect.c b/examples/common_components/protocol_examples_common/wifi_connect.c index ed95bccb2f..8d5c815a85 100644 --- a/examples/common_components/protocol_examples_common/wifi_connect.c +++ b/examples/common_components/protocol_examples_common/wifi_connect.c @@ -166,10 +166,14 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) if (wait) { ESP_LOGI(TAG, "Waiting for IP(s)"); #if CONFIG_EXAMPLE_CONNECT_IPV4 - xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); + if (xSemaphoreTake(s_semph_get_ip_addrs, pdMS_TO_TICKS(10000)) == pdFALSE) { + ESP_LOGW(TAG, "Gave up waiting for IPv4 address."); + } #endif #if CONFIG_EXAMPLE_CONNECT_IPV6 - xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); + if (xSemaphoreTake(s_semph_get_ip6_addrs, pdMS_TO_TICKS(10000)) == pdFALSE) { + ESP_LOGW(TAG, "Gave up waiting for (preferred) IPv6 address."); + } #endif if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { return ESP_FAIL;