mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-18 13:02:21 +02:00
examples: common connect to also support no connection flow if no inteface chosen
This is useful for testing if there's no need for external network and connection * Original commit: espressif/esp-idf@085d2b8d25
This commit is contained in:
committed by
suren-gabrielyan-espressif
parent
ad67a23097
commit
07f57523c8
@ -177,6 +177,7 @@ menu "Example Connection Configuration"
|
|||||||
config EXAMPLE_CONNECT_IPV6
|
config EXAMPLE_CONNECT_IPV6
|
||||||
bool "Obtain IPv6 address"
|
bool "Obtain IPv6 address"
|
||||||
default y
|
default y
|
||||||
|
depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET
|
||||||
help
|
help
|
||||||
By default, examples will wait until IPv4 and IPv6 local link addresses are obtained.
|
By default, examples will wait until IPv4 and IPv6 local link addresses are obtained.
|
||||||
Disable this option if the network does not support IPv6.
|
Disable this option if the network does not support IPv6.
|
||||||
|
@ -43,9 +43,10 @@
|
|||||||
#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces)
|
#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define EXAMPLE_DO_CONNECT CONFIG_EXAMPLE_CONNECT_WIFI || CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
|
||||||
static int s_active_interfaces = 0;
|
static int s_active_interfaces = 0;
|
||||||
static xSemaphoreHandle s_semph_get_ip_addrs;
|
static xSemaphoreHandle s_semph_get_ip_addrs;
|
||||||
static esp_ip4_addr_t s_ip_addr;
|
|
||||||
static esp_netif_t *s_example_esp_netif = NULL;
|
static esp_netif_t *s_example_esp_netif = NULL;
|
||||||
|
|
||||||
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
||||||
@ -102,7 +103,11 @@ static void start(void)
|
|||||||
s_example_esp_netif = NULL;
|
s_example_esp_netif = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if EXAMPLE_DO_CONNECT
|
||||||
|
/* create semaphore if at least one interface is active */
|
||||||
s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
|
s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tear down connection, release resources */
|
/* tear down connection, release resources */
|
||||||
@ -119,6 +124,9 @@ static void stop(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if EXAMPLE_DO_CONNECT
|
||||||
|
static esp_ip4_addr_t s_ip_addr;
|
||||||
|
|
||||||
static void on_got_ip(void *arg, esp_event_base_t event_base,
|
static void on_got_ip(void *arg, esp_event_base_t event_base,
|
||||||
int32_t event_id, void *event_data)
|
int32_t event_id, void *event_data)
|
||||||
{
|
{
|
||||||
@ -131,6 +139,7 @@ static void on_got_ip(void *arg, esp_event_base_t event_base,
|
|||||||
memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
|
memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
|
||||||
xSemaphoreGive(s_semph_get_ip_addrs);
|
xSemaphoreGive(s_semph_get_ip_addrs);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
||||||
|
|
||||||
@ -155,9 +164,11 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base,
|
|||||||
|
|
||||||
esp_err_t example_connect(void)
|
esp_err_t example_connect(void)
|
||||||
{
|
{
|
||||||
|
#if EXAMPLE_DO_CONNECT
|
||||||
if (s_semph_get_ip_addrs != NULL) {
|
if (s_semph_get_ip_addrs != NULL) {
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
start();
|
start();
|
||||||
ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
|
ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
|
||||||
ESP_LOGI(TAG, "Waiting for IP(s)");
|
ESP_LOGI(TAG, "Waiting for IP(s)");
|
||||||
|
@ -24,6 +24,11 @@ extern "C" {
|
|||||||
#define EXAMPLE_INTERFACE get_example_netif()
|
#define EXAMPLE_INTERFACE get_example_netif()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined (CONFIG_EXAMPLE_CONNECT_ETHERNET) && !defined (CONFIG_EXAMPLE_CONNECT_WIFI)
|
||||||
|
// This is useful for some tests which do not need a network connection
|
||||||
|
#define EXAMPLE_INTERFACE NULL
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Configure Wi-Fi or Ethernet, connect, wait for IP
|
* @brief Configure Wi-Fi or Ethernet, connect, wait for IP
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user