Skip to content

Commit

Permalink
feat(example/protocols): Add default wait for any address to eth_connect
Browse files Browse the repository at this point in the history
  • Loading branch information
sgryphon committed Feb 24, 2024
1 parent a3c3f4e commit bf58e6c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/common_components/protocol_examples_common/eth_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
static const char *TAG = "ethernet_connect";
static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
#if CONFIG_EXAMPLE_CONNECT_IPV6
#if CONFIG_EXAMPLE_CONNECT_PREF_ANY
#else
static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL;
#endif
#endif

static esp_netif_t *eth_start(void);
static void eth_stop(void);
Expand Down Expand Up @@ -55,7 +58,11 @@ static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
#if CONFIG_EXAMPLE_CONNECT_PREF_ANY
xSemaphoreGive(s_semph_get_ip_addrs);
#else
xSemaphoreGive(s_semph_get_ip6_addrs);
#endif
}
}

Expand Down Expand Up @@ -206,14 +213,23 @@ void example_ethernet_shutdown(void)
vSemaphoreDelete(s_semph_get_ip_addrs);
s_semph_get_ip_addrs = NULL;
#if CONFIG_EXAMPLE_CONNECT_IPV6
#if CONFIG_EXAMPLE_CONNECT_PREF_ANY
#else
vSemaphoreDelete(s_semph_get_ip6_addrs);
s_semph_get_ip6_addrs = NULL;
#endif
#endif
eth_stop();
}

esp_err_t example_ethernet_connect(void)
{
#if CONFIG_EXAMPLE_CONNECT_PREF_ANY
s_semph_get_ip_addrs = xSemaphoreCreateBinary();
if (s_semph_get_ip_addrs == NULL) {
return ESP_ERR_NO_MEM;
}
#else
#if CONFIG_EXAMPLE_CONNECT_IPV4
s_semph_get_ip_addrs = xSemaphoreCreateBinary();
if (s_semph_get_ip_addrs == NULL) {
Expand All @@ -226,14 +242,19 @@ esp_err_t example_ethernet_connect(void)
vSemaphoreDelete(s_semph_get_ip_addrs);
return ESP_ERR_NO_MEM;
}
#endif
#endif
eth_start();
ESP_LOGI(TAG, "Waiting for IP(s).");
#if CONFIG_EXAMPLE_CONNECT_PREF_ANY
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
#else
#if CONFIG_EXAMPLE_CONNECT_IPV4
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
#endif
#if CONFIG_EXAMPLE_CONNECT_IPV6
xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY);
#endif
#endif
return ESP_OK;
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ esp_err_t example_ppp_connect(void)
#endif // CONNECT_PPP_DEVICE

ESP_LOGI(TAG, "Waiting for IP address");
// Note: CONFIG_EXAMPLE_CONNECT_PREF_ANY is ignored; PPP always waits for ANY (does not wait for all)
EventBits_t bits = xEventGroupWaitBits(s_event_group, CONNECT_BITS, pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & CONNECTION_FAILED) {
ESP_LOGE(TAG, "Connection failed!");
Expand Down

0 comments on commit bf58e6c

Please sign in to comment.