esp_netif_objects.c 5.1 KB

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