esp_netif_objects.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_netif.h"
  7. #include "sys/queue.h"
  8. #include "esp_log.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/semphr.h"
  11. #include "esp_netif_private.h"
  12. #include <string.h>
  13. //
  14. // Purpose of this module is to provide list of esp-netif structures
  15. // - this module has no dependency on a specific network stack (lwip)
  16. //
  17. static const char *TAG = "esp_netif_objects";
  18. typedef struct slist_netifs_s slist_netifs_t;
  19. struct slist_netifs_s {
  20. esp_netif_t *netif;
  21. SLIST_ENTRY(slist_netifs_s) next;
  22. };
  23. SLIST_HEAD(slisthead, slist_netifs_s) s_head = { .slh_first = NULL, };
  24. static size_t s_esp_netif_counter = 0;
  25. static SemaphoreHandle_t s_list_lock = NULL;
  26. ESP_EVENT_DEFINE_BASE(IP_EVENT);
  27. esp_err_t esp_netif_list_lock(void)
  28. {
  29. if (s_list_lock == NULL) {
  30. s_list_lock = xSemaphoreCreateMutex();
  31. if (s_list_lock == NULL) {
  32. return ESP_ERR_NO_MEM;
  33. }
  34. }
  35. xSemaphoreTake(s_list_lock, portMAX_DELAY);
  36. return ESP_OK;
  37. }
  38. void esp_netif_list_unlock(void)
  39. {
  40. assert(s_list_lock);
  41. xSemaphoreGive(s_list_lock);
  42. if (s_esp_netif_counter == 0) {
  43. vQueueDelete(s_list_lock);
  44. s_list_lock = NULL;
  45. }
  46. }
  47. //
  48. // List manipulation functions
  49. //
  50. esp_err_t esp_netif_add_to_list(esp_netif_t *netif)
  51. {
  52. esp_err_t ret;
  53. struct slist_netifs_s *item = calloc(1, sizeof(struct slist_netifs_s));
  54. ESP_LOGD(TAG, "%s %p", __func__, netif);
  55. if (item == NULL) {
  56. return ESP_ERR_NO_MEM;
  57. }
  58. item->netif = netif;
  59. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  60. free(item);
  61. return ret;
  62. }
  63. SLIST_INSERT_HEAD(&s_head, item, next);
  64. ++s_esp_netif_counter;
  65. ESP_LOGD(TAG, "%s netif added successfully (total netifs: %d)", __func__, s_esp_netif_counter);
  66. esp_netif_list_unlock();
  67. return ESP_OK;
  68. }
  69. esp_err_t esp_netif_remove_from_list(esp_netif_t *netif)
  70. {
  71. struct slist_netifs_s *item;
  72. esp_err_t ret;
  73. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  74. return ret;
  75. }
  76. ESP_LOGV(TAG, "%s %p", __func__, netif);
  77. SLIST_FOREACH(item, &s_head, next) {
  78. if (item->netif == netif) {
  79. SLIST_REMOVE(&s_head, item, slist_netifs_s, next);
  80. assert(s_esp_netif_counter > 0);
  81. --s_esp_netif_counter;
  82. ESP_LOGD(TAG, "%s netif successfully removed (total netifs: %d)", __func__, s_esp_netif_counter);
  83. free(item);
  84. esp_netif_list_unlock();
  85. return ESP_OK;
  86. }
  87. }
  88. esp_netif_list_unlock();
  89. return ESP_ERR_NOT_FOUND;
  90. }
  91. size_t esp_netif_get_nr_of_ifs(void)
  92. {
  93. return s_esp_netif_counter;
  94. }
  95. esp_netif_t* esp_netif_next(esp_netif_t* netif)
  96. {
  97. esp_err_t ret;
  98. esp_netif_t* result;
  99. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  100. ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
  101. return NULL;
  102. }
  103. result = esp_netif_next_unsafe(netif);
  104. esp_netif_list_unlock();
  105. return result;
  106. }
  107. esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif)
  108. {
  109. ESP_LOGV(TAG, "%s %p", __func__, netif);
  110. struct slist_netifs_s *item;
  111. // Getting the first netif if argument is NULL
  112. if (netif == NULL) {
  113. item = SLIST_FIRST(&s_head);
  114. return (item == NULL) ? NULL : item->netif;
  115. }
  116. // otherwise the next one (after the supplied netif)
  117. SLIST_FOREACH(item, &s_head, next) {
  118. if (item->netif == netif) {
  119. item = SLIST_NEXT(item, next);
  120. return (item == NULL) ? NULL : item->netif;
  121. }
  122. }
  123. return NULL;
  124. }
  125. bool esp_netif_is_netif_listed(esp_netif_t *esp_netif)
  126. {
  127. struct slist_netifs_s *item;
  128. esp_err_t ret;
  129. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  130. ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
  131. return false;
  132. }
  133. SLIST_FOREACH(item, &s_head, next) {
  134. if (item->netif == esp_netif) {
  135. esp_netif_list_unlock();
  136. return true;
  137. }
  138. }
  139. esp_netif_list_unlock();
  140. return false;
  141. }
  142. esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
  143. {
  144. struct slist_netifs_s *item;
  145. esp_err_t ret;
  146. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  147. ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
  148. return NULL;
  149. }
  150. SLIST_FOREACH(item, &s_head, next) {
  151. esp_netif_t *esp_netif = item->netif;
  152. if (strcmp(if_key, esp_netif_get_ifkey(esp_netif)) == 0) {
  153. esp_netif_list_unlock();
  154. return esp_netif;
  155. }
  156. }
  157. esp_netif_list_unlock();
  158. return NULL;
  159. }