mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
esp-netif: Improve esp_netif_is_netif_listed and esp_netif_get_handle_from_ifkey
Each esp_netif_next_unsafe() call needs to iterate the s_head list. It is inefficient with a do-while loop + esp_netif_next_unsafe() call. Use SLIST_FOREACH instead to simplify the code and speed-up the matching. While at it, also fix returning NULL from esp_netif_is_netif_listed(). Signed-off-by: Axel Lin <axel.lin@gmail.com>
This commit is contained in:
parent
c13afea635
commit
5209af379a
@ -151,39 +151,39 @@ esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif)
|
|||||||
|
|
||||||
bool esp_netif_is_netif_listed(esp_netif_t *esp_netif)
|
bool esp_netif_is_netif_listed(esp_netif_t *esp_netif)
|
||||||
{
|
{
|
||||||
|
struct slist_netifs_s *item;
|
||||||
esp_err_t ret;
|
esp_err_t ret;
|
||||||
if ((ret = esp_netif_list_lock()) != ESP_OK) {
|
if ((ret = esp_netif_list_lock()) != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
|
ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// looking for the netif in the list of registered interfaces
|
SLIST_FOREACH(item, &s_head, next) {
|
||||||
esp_netif_t *it = esp_netif_next_unsafe(NULL);
|
if (item->netif == esp_netif) {
|
||||||
do {
|
|
||||||
if (it && it == esp_netif) {
|
|
||||||
esp_netif_list_unlock();
|
esp_netif_list_unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} while (NULL != (it = esp_netif_next_unsafe(it)));
|
}
|
||||||
esp_netif_list_unlock();
|
esp_netif_list_unlock();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
|
esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
|
||||||
{
|
{
|
||||||
|
struct slist_netifs_s *item;
|
||||||
esp_err_t ret;
|
esp_err_t ret;
|
||||||
if ((ret = esp_netif_list_lock()) != ESP_OK) {
|
if ((ret = esp_netif_list_lock()) != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
|
ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_netif_t *esp_netif = esp_netif_next_unsafe(NULL);
|
SLIST_FOREACH(item, &s_head, next) {
|
||||||
do {
|
esp_netif_t *esp_netif = item->netif;
|
||||||
if (esp_netif && strcmp(if_key, esp_netif_get_ifkey(esp_netif))==0) {
|
if (strcmp(if_key, esp_netif_get_ifkey(esp_netif)) == 0) {
|
||||||
esp_netif_list_unlock();
|
esp_netif_list_unlock();
|
||||||
return esp_netif;
|
return esp_netif;
|
||||||
}
|
}
|
||||||
} while (NULL != (esp_netif = esp_netif_next_unsafe(esp_netif)));
|
}
|
||||||
esp_netif_list_unlock();
|
esp_netif_list_unlock();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user