connect.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <string.h>
  7. #include "protocol_examples_common.h"
  8. #include "example_common_private.h"
  9. #include "sdkconfig.h"
  10. #include "esp_event.h"
  11. #include "esp_wifi.h"
  12. #include "esp_wifi_default.h"
  13. #include "esp_log.h"
  14. #include "esp_netif.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "freertos/event_groups.h"
  18. #include "lwip/err.h"
  19. #include "lwip/sys.h"
  20. static const char *TAG = "example_common";
  21. #if CONFIG_EXAMPLE_CONNECT_IPV6
  22. /* types of ipv6 addresses to be displayed on ipv6 events */
  23. const char *example_ipv6_addr_types_to_str[6] = {
  24. "ESP_IP6_ADDR_IS_UNKNOWN",
  25. "ESP_IP6_ADDR_IS_GLOBAL",
  26. "ESP_IP6_ADDR_IS_LINK_LOCAL",
  27. "ESP_IP6_ADDR_IS_SITE_LOCAL",
  28. "ESP_IP6_ADDR_IS_UNIQUE_LOCAL",
  29. "ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6"
  30. };
  31. #endif
  32. /**
  33. * @brief Checks the netif description if it contains specified prefix.
  34. * All netifs created withing common connect component are prefixed with the module TAG,
  35. * so it returns true if the specified netif is owned by this module
  36. */
  37. bool example_is_our_netif(const char *prefix, esp_netif_t *netif)
  38. {
  39. return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0;
  40. }
  41. esp_netif_t *get_example_netif_from_desc(const char *desc)
  42. {
  43. esp_netif_t *netif = NULL;
  44. while ((netif = esp_netif_next(netif)) != NULL) {
  45. if (strcmp(esp_netif_get_desc(netif), desc) == 0) {
  46. return netif;
  47. }
  48. }
  49. return netif;
  50. }
  51. void example_print_all_netif_ips(const char *prefix)
  52. {
  53. // iterate over active interfaces, and print out IPs of "our" netifs
  54. esp_netif_t *netif = NULL;
  55. for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) {
  56. netif = esp_netif_next(netif);
  57. if (example_is_our_netif(prefix, netif)) {
  58. ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
  59. #if CONFIG_LWIP_IPV4
  60. esp_netif_ip_info_t ip;
  61. ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
  62. ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
  63. #endif
  64. #if CONFIG_EXAMPLE_CONNECT_IPV6
  65. esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
  66. int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
  67. for (int j = 0; j < ip6_addrs; ++j) {
  68. esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j]));
  69. ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), example_ipv6_addr_types_to_str[ipv6_type]);
  70. }
  71. #endif
  72. }
  73. }
  74. }
  75. esp_err_t example_connect(void)
  76. {
  77. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  78. if (example_ethernet_connect() != ESP_OK) {
  79. return ESP_FAIL;
  80. }
  81. ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown));
  82. #endif
  83. #if CONFIG_EXAMPLE_CONNECT_WIFI
  84. if (example_wifi_connect() != ESP_OK) {
  85. return ESP_FAIL;
  86. }
  87. ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown));
  88. #endif
  89. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  90. example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH);
  91. #endif
  92. #if CONFIG_EXAMPLE_CONNECT_WIFI
  93. example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA);
  94. #endif
  95. return ESP_OK;
  96. }
  97. esp_err_t example_disconnect(void)
  98. {
  99. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  100. example_ethernet_shutdown();
  101. ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_ethernet_shutdown));
  102. #endif
  103. #if CONFIG_EXAMPLE_CONNECT_WIFI
  104. example_wifi_shutdown();
  105. ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_wifi_shutdown));
  106. #endif
  107. return ESP_OK;
  108. }