esp_netif_objects.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. free(item);
  68. return ret;
  69. }
  70. SLIST_INSERT_HEAD(&s_head, item, next);
  71. ++s_esp_netif_counter;
  72. ESP_LOGD(TAG, "%s netif added successfully (total netifs: %d)", __func__, s_esp_netif_counter);
  73. esp_netif_list_unlock();
  74. return ESP_OK;
  75. }
  76. esp_err_t esp_netif_remove_from_list(esp_netif_t *netif)
  77. {
  78. struct slist_netifs_s *item;
  79. esp_err_t ret;
  80. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  81. return ret;
  82. }
  83. ESP_LOGV(TAG, "%s %p", __func__, netif);
  84. SLIST_FOREACH(item, &s_head, next) {
  85. if (item->netif == netif) {
  86. SLIST_REMOVE(&s_head, item, slist_netifs_s, next);
  87. assert(s_esp_netif_counter > 0);
  88. --s_esp_netif_counter;
  89. ESP_LOGD(TAG, "%s netif successfully removed (total netifs: %d)", __func__, s_esp_netif_counter);
  90. free(item);
  91. esp_netif_list_unlock();
  92. return ESP_OK;
  93. }
  94. }
  95. esp_netif_list_unlock();
  96. return ESP_ERR_NOT_FOUND;
  97. }
  98. size_t esp_netif_get_nr_of_ifs(void)
  99. {
  100. return s_esp_netif_counter;
  101. }
  102. esp_netif_t* esp_netif_next(esp_netif_t* netif)
  103. {
  104. esp_err_t ret;
  105. esp_netif_t* result;
  106. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  107. ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
  108. return NULL;
  109. }
  110. result = esp_netif_next_unsafe(netif);
  111. esp_netif_list_unlock();
  112. return result;
  113. }
  114. esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif)
  115. {
  116. ESP_LOGV(TAG, "%s %p", __func__, netif);
  117. struct slist_netifs_s *item;
  118. // Getting the first netif if argument is NULL
  119. if (netif == NULL) {
  120. item = SLIST_FIRST(&s_head);
  121. return (item == NULL) ? NULL : item->netif;
  122. }
  123. // otherwise the next one (after the supplied netif)
  124. SLIST_FOREACH(item, &s_head, next) {
  125. if (item->netif == netif) {
  126. item = SLIST_NEXT(item, next);
  127. return (item == NULL) ? NULL : item->netif;
  128. }
  129. }
  130. return NULL;
  131. }
  132. bool esp_netif_is_netif_listed(esp_netif_t *esp_netif)
  133. {
  134. struct slist_netifs_s *item;
  135. esp_err_t ret;
  136. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  137. ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
  138. return false;
  139. }
  140. SLIST_FOREACH(item, &s_head, next) {
  141. if (item->netif == esp_netif) {
  142. esp_netif_list_unlock();
  143. return true;
  144. }
  145. }
  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. struct slist_netifs_s *item;
  152. esp_err_t ret;
  153. if ((ret = esp_netif_list_lock()) != ESP_OK) {
  154. ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret);
  155. return NULL;
  156. }
  157. SLIST_FOREACH(item, &s_head, next) {
  158. esp_netif_t *esp_netif = item->netif;
  159. if (strcmp(if_key, esp_netif_get_ifkey(esp_netif)) == 0) {
  160. esp_netif_list_unlock();
  161. return esp_netif;
  162. }
  163. }
  164. esp_netif_list_unlock();
  165. return NULL;
  166. }