esp_netif: Support for manual set-default-netif

Closes https://github.com/espressif/esp-idf/issues/8330
This commit is contained in:
David Cermak 2022-02-21 17:54:12 +01:00
parent 0f57ef942a
commit f8907b7884
2 changed files with 38 additions and 4 deletions

View File

@ -350,6 +350,18 @@ void esp_netif_action_add_ip6_address(void *esp_netif, esp_event_base_t base, in
*/
void esp_netif_action_remove_ip6_address(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
/**
* @brief Manual configuration of the default netif
*
* This API overrides the automatic configuration of the default interface based on the route_prio
* If the selected netif is set default using this API, no other interface could be set-default disregarding
* its route_prio number (unless the selected netif gets destroyed)
*
* @param[in] esp_netif Handle to esp-netif instance
* @return ESP_OK on success
*/
esp_err_t esp_netif_set_default_netif(esp_netif_t *esp_netif);
/**
* @}
*/

View File

@ -92,6 +92,7 @@ typedef enum esp_netif_action {
ESP_NETIF_UNDEF,
ESP_NETIF_STARTED,
ESP_NETIF_STOPPED,
ESP_NETIF_SET_DEFAULT,
} esp_netif_action_t;
//
@ -103,6 +104,7 @@ static const char *TAG = "esp_netif_lwip";
static bool tcpip_initialized = false;
static esp_netif_t *s_last_default_esp_netif = NULL;
static bool s_manual_last_default_esp_netif = false;
#if !LWIP_TCPIP_CORE_LOCKING
static sys_sem_t api_sync_sem = NULL;
@ -176,7 +178,7 @@ static esp_netif_t* esp_netif_is_active(esp_netif_t *arg)
*
* @note: This function must be called from lwip thread
*/
static void esp_netif_set_default_netif(esp_netif_t *esp_netif)
static void esp_netif_set_default_netif_internal(esp_netif_t *esp_netif)
{
if (_IS_NETIF_POINT2POINT_TYPE(esp_netif, PPP_LWIP_NETIF)) {
#if CONFIG_PPP_SUPPORT
@ -199,17 +201,31 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
ESP_LOGD(TAG, "%s %p", __func__, esp_netif);
if (s_manual_last_default_esp_netif && action != ESP_NETIF_SET_DEFAULT) {
// check if manually configured default interface hasn't been destroyed
s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif);
if (esp_netif_is_active(s_last_default_esp_netif) != NULL) {
return ESP_OK; // still valid -> don't update default netif
}
// invalid -> reset the manual override and perform auto update
s_manual_last_default_esp_netif = false;
}
switch (action) {
case ESP_NETIF_SET_DEFAULT:
s_last_default_esp_netif = esp_netif;
s_manual_last_default_esp_netif = true;
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
break;
case ESP_NETIF_STARTED:
{
// check if previously default interface hasn't been destroyed in the meantime
s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif);
if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)
&& (s_last_default_esp_netif->route_prio > esp_netif->route_prio)) {
esp_netif_set_default_netif(s_last_default_esp_netif);
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
} else if (esp_netif_is_netif_up(esp_netif)) {
s_last_default_esp_netif = esp_netif;
esp_netif_set_default_netif(s_last_default_esp_netif);
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
}
}
break;
@ -235,7 +251,7 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
}
esp_netif_list_unlock();
if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)) {
esp_netif_set_default_netif(s_last_default_esp_netif);
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
}
}
break;
@ -254,6 +270,11 @@ static esp_err_t esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_neti
return esp_netif_lwip_ipc_call(esp_netif_update_default_netif_lwip, esp_netif, (void*)action);
}
esp_err_t esp_netif_set_default_netif(esp_netif_t *esp_netif)
{
return esp_netif_update_default_netif(esp_netif, ESP_NETIF_SET_DEFAULT);
}
void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d)
{
ip4_addr_t *address = (ip4_addr_t*)addr;
@ -608,6 +629,7 @@ void esp_netif_destroy(esp_netif_t *esp_netif)
if (s_last_default_esp_netif == esp_netif) {
// clear last default netif if it happens to be this just destroyed interface
s_last_default_esp_netif = NULL;
s_manual_last_default_esp_netif = false;
}
free(esp_netif);
}