esp_netif_objects.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 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 "esp_netif_private.h"
  10. #include <string.h>
  11. //
  12. // Purpose of this module is to provide list of esp-netif structures
  13. // - this module has no dependency on a specific network stack (lwip)
  14. //
  15. static const char *TAG = "esp_netif_objects";
  16. typedef struct slist_netifs_s slist_netifs_t;
  17. struct slist_netifs_s {
  18. esp_netif_t *netif;
  19. SLIST_ENTRY(slist_netifs_s) next;
  20. };
  21. SLIST_HEAD(slisthead, slist_netifs_s) s_head = { .slh_first = NULL, };
  22. static size_t s_esp_netif_counter = 0;
  23. ESP_EVENT_DEFINE_BASE(IP_EVENT);
  24. //
  25. // List manipulation functions
  26. //
  27. esp_err_t esp_netif_add_to_list_unsafe(esp_netif_t *netif)
  28. {
  29. struct slist_netifs_s *item = calloc(1, sizeof(struct slist_netifs_s));
  30. ESP_LOGV(TAG, "%s %p", __func__, netif);
  31. if (item == NULL) {
  32. return ESP_ERR_NO_MEM;
  33. }
  34. item->netif = netif;
  35. SLIST_INSERT_HEAD(&s_head, item, next);
  36. ++s_esp_netif_counter;
  37. ESP_LOGD(TAG, "%s netif added successfully (total netifs: %" PRIu32 ")", __func__, (uint32_t)s_esp_netif_counter);
  38. return ESP_OK;
  39. }
  40. esp_err_t esp_netif_remove_from_list_unsafe(esp_netif_t *netif)
  41. {
  42. struct slist_netifs_s *item;
  43. ESP_LOGV(TAG, "%s %p", __func__, netif);
  44. SLIST_FOREACH(item, &s_head, next) {
  45. if (item->netif == netif) {
  46. SLIST_REMOVE(&s_head, item, slist_netifs_s, next);
  47. assert(s_esp_netif_counter > 0);
  48. --s_esp_netif_counter;
  49. ESP_LOGD(TAG, "%s netif successfully removed (total netifs: %" PRIu32 ")", __func__, (uint32_t)s_esp_netif_counter);
  50. free(item);
  51. return ESP_OK;
  52. }
  53. }
  54. return ESP_ERR_NOT_FOUND;
  55. }
  56. size_t esp_netif_get_nr_of_ifs(void)
  57. {
  58. return s_esp_netif_counter;
  59. }
  60. // This API is inherently unsafe
  61. // suggest that users call from esp_netif_tcpip_exec()
  62. esp_netif_t* esp_netif_next(esp_netif_t* netif)
  63. {
  64. return esp_netif_next_unsafe(netif);
  65. }
  66. esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif)
  67. {
  68. ESP_LOGV(TAG, "%s %p", __func__, netif);
  69. struct slist_netifs_s *item;
  70. // Getting the first netif if argument is NULL
  71. if (netif == NULL) {
  72. item = SLIST_FIRST(&s_head);
  73. return (item == NULL) ? NULL : item->netif;
  74. }
  75. // otherwise the next one (after the supplied netif)
  76. SLIST_FOREACH(item, &s_head, next) {
  77. if (item->netif == netif) {
  78. item = SLIST_NEXT(item, next);
  79. return (item == NULL) ? NULL : item->netif;
  80. }
  81. }
  82. return NULL;
  83. }
  84. bool esp_netif_is_netif_listed(esp_netif_t *esp_netif)
  85. {
  86. struct slist_netifs_s *item;
  87. SLIST_FOREACH(item, &s_head, next) {
  88. if (item->netif == esp_netif) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. esp_netif_t *esp_netif_get_handle_from_ifkey_unsafe(const char *if_key)
  95. {
  96. struct slist_netifs_s *item;
  97. SLIST_FOREACH(item, &s_head, next) {
  98. esp_netif_t *esp_netif = item->netif;
  99. if (strcmp(if_key, esp_netif_get_ifkey(esp_netif)) == 0) {
  100. return esp_netif;
  101. }
  102. }
  103. return NULL;
  104. }