connect.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * SPDX-FileCopyrightText: 2022 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. esp_netif_ip_info_t ip;
  56. for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) {
  57. netif = esp_netif_next(netif);
  58. if (example_is_our_netif(prefix, netif)) {
  59. ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
  60. ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
  61. ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
  62. #if CONFIG_EXAMPLE_CONNECT_IPV6
  63. esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
  64. int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
  65. for (int j = 0; j < ip6_addrs; ++j) {
  66. esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j]));
  67. ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), example_ipv6_addr_types_to_str[ipv6_type]);
  68. }
  69. #endif
  70. }
  71. }
  72. }
  73. esp_err_t example_connect(void)
  74. {
  75. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  76. if (example_ethernet_connect() != ESP_OK) {
  77. return ESP_FAIL;
  78. }
  79. ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown));
  80. #endif
  81. #if CONFIG_EXAMPLE_CONNECT_WIFI
  82. if (example_wifi_connect() != ESP_OK) {
  83. return ESP_FAIL;
  84. }
  85. ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown));
  86. #endif
  87. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  88. example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH);
  89. #endif
  90. #if CONFIG_EXAMPLE_CONNECT_WIFI
  91. example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA);
  92. #endif
  93. return ESP_OK;
  94. }
  95. esp_err_t example_disconnect(void)
  96. {
  97. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  98. example_ethernet_shutdown();
  99. ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_ethernet_shutdown));
  100. #endif
  101. #if CONFIG_EXAMPLE_CONNECT_WIFI
  102. example_wifi_shutdown();
  103. ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_wifi_shutdown));
  104. #endif
  105. return ESP_OK;
  106. }