connect.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection.
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include "protocol_examples_common.h"
  9. #include "sdkconfig.h"
  10. #include "esp_event.h"
  11. #include "esp_wifi.h"
  12. #include "esp_eth.h"
  13. #include "esp_log.h"
  14. #include "tcpip_adapter.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. #define GOT_IPV4_BIT BIT(0)
  21. #define GOT_IPV6_BIT BIT(1)
  22. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  23. #define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT)
  24. #else
  25. #define CONNECTED_BITS (GOT_IPV4_BIT)
  26. #endif
  27. static EventGroupHandle_t s_connect_event_group;
  28. static ip4_addr_t s_ip_addr;
  29. static const char *s_connection_name;
  30. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  31. static ip6_addr_t s_ipv6_addr;
  32. #endif
  33. static const char *TAG = "example_connect";
  34. /* set up connection, Wi-Fi or Ethernet */
  35. static void start();
  36. /* tear down connection, release resources */
  37. static void stop();
  38. static void on_got_ip(void *arg, esp_event_base_t event_base,
  39. int32_t event_id, void *event_data)
  40. {
  41. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  42. memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
  43. xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);
  44. }
  45. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  46. static void on_got_ipv6(void *arg, esp_event_base_t event_base,
  47. int32_t event_id, void *event_data)
  48. {
  49. ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
  50. memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
  51. xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT);
  52. }
  53. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  54. esp_err_t example_connect()
  55. {
  56. if (s_connect_event_group != NULL) {
  57. return ESP_ERR_INVALID_STATE;
  58. }
  59. s_connect_event_group = xEventGroupCreate();
  60. start();
  61. xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY);
  62. ESP_LOGI(TAG, "Connected to %s", s_connection_name);
  63. ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
  64. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  65. ESP_LOGI(TAG, "IPv6 address: " IPV6STR, IPV62STR(s_ipv6_addr));
  66. #endif
  67. return ESP_OK;
  68. }
  69. esp_err_t example_disconnect()
  70. {
  71. if (s_connect_event_group == NULL) {
  72. return ESP_ERR_INVALID_STATE;
  73. }
  74. vEventGroupDelete(s_connect_event_group);
  75. s_connect_event_group = NULL;
  76. stop();
  77. ESP_LOGI(TAG, "Disconnected from %s", s_connection_name);
  78. s_connection_name = NULL;
  79. return ESP_OK;
  80. }
  81. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  82. static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
  83. int32_t event_id, void *event_data)
  84. {
  85. ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
  86. ESP_ERROR_CHECK(esp_wifi_connect());
  87. }
  88. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  89. static void on_wifi_connect(void *arg, esp_event_base_t event_base,
  90. int32_t event_id, void *event_data)
  91. {
  92. tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
  93. }
  94. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  95. static void start()
  96. {
  97. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  98. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  99. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
  100. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
  101. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  102. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL));
  103. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
  104. #endif
  105. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  106. wifi_config_t wifi_config = {
  107. .sta = {
  108. .ssid = CONFIG_EXAMPLE_WIFI_SSID,
  109. .password = CONFIG_EXAMPLE_WIFI_PASSWORD,
  110. },
  111. };
  112. ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
  113. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  114. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  115. ESP_ERROR_CHECK(esp_wifi_start());
  116. ESP_ERROR_CHECK(esp_wifi_connect());
  117. s_connection_name = CONFIG_EXAMPLE_WIFI_SSID;
  118. }
  119. static void stop()
  120. {
  121. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect));
  122. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip));
  123. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  124. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
  125. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect));
  126. #endif
  127. ESP_ERROR_CHECK(esp_wifi_stop());
  128. ESP_ERROR_CHECK(esp_wifi_deinit());
  129. }
  130. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  131. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  132. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  133. /** Event handler for Ethernet events */
  134. static void on_eth_event(void *arg, esp_event_base_t event_base,
  135. int32_t event_id, void *event_data)
  136. {
  137. switch (event_id) {
  138. case ETHERNET_EVENT_CONNECTED:
  139. ESP_LOGI(TAG, "Ethernet Link Up");
  140. tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH);
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  147. static esp_eth_handle_t s_eth_handle = NULL;
  148. static esp_eth_mac_t *s_mac = NULL;
  149. static esp_eth_phy_t *s_phy = NULL;
  150. static void start()
  151. {
  152. ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
  153. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));
  154. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  155. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL));
  156. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
  157. #endif
  158. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  159. s_mac = esp_eth_mac_new_esp32(&mac_config);
  160. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  161. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  162. s_phy = esp_eth_phy_new_ip101(&phy_config);
  163. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  164. s_phy = esp_eth_phy_new_rtl8201(&phy_config);
  165. #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
  166. s_phy = esp_eth_phy_new_lan8720(&phy_config);
  167. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  168. s_phy = esp_eth_phy_new_dp83848(&phy_config);
  169. #endif
  170. esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
  171. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
  172. s_connection_name = "Ethernet";
  173. }
  174. static void stop()
  175. {
  176. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip));
  177. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  178. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
  179. ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event));
  180. #endif
  181. ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
  182. ESP_ERROR_CHECK(s_phy->del(s_phy));
  183. ESP_ERROR_CHECK(s_mac->del(s_mac));
  184. }
  185. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET