wifi_connect.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection.
  7. This example code is in the Public Domain (or CC0 licensed, at your option.)
  8. Unless required by applicable law or agreed to in writing, this
  9. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  10. CONDITIONS OF ANY KIND, either express or implied.
  11. */
  12. #include <string.h>
  13. #include "protocol_examples_common.h"
  14. #include "example_common_private.h"
  15. #include "esp_log.h"
  16. #if CONFIG_EXAMPLE_CONNECT_WIFI
  17. static const char *TAG = "example_connect";
  18. static esp_netif_t *s_example_sta_netif = NULL;
  19. static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
  20. #if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
  21. #define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
  22. #elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
  23. #define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
  24. #endif
  25. #if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
  26. #define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  27. #elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
  28. #define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  29. #endif
  30. #if CONFIG_EXAMPLE_WIFI_AUTH_OPEN
  31. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
  32. #elif CONFIG_EXAMPLE_WIFI_AUTH_WEP
  33. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
  34. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK
  35. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
  36. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK
  37. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
  38. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
  39. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
  40. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
  41. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE
  42. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK
  43. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
  44. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
  45. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
  46. #elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK
  47. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
  48. #endif
  49. static int s_retry_num = 0;
  50. static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base,
  51. int32_t event_id, void *event_data)
  52. {
  53. s_retry_num++;
  54. if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
  55. ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num);
  56. if (s_semph_get_ip_addrs) {
  57. /* let example_wifi_sta_do_connect() return */
  58. xSemaphoreGive(s_semph_get_ip_addrs);
  59. #if CONFIG_EXAMPLE_CONNECT_IPV6
  60. xSemaphoreGive(s_semph_get_ip_addrs);
  61. #endif
  62. }
  63. return;
  64. }
  65. ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
  66. esp_err_t err = esp_wifi_connect();
  67. if (err == ESP_ERR_WIFI_NOT_STARTED) {
  68. return;
  69. }
  70. ESP_ERROR_CHECK(err);
  71. }
  72. static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base,
  73. int32_t event_id, void *event_data)
  74. {
  75. #if CONFIG_EXAMPLE_CONNECT_IPV6
  76. esp_netif_create_ip6_linklocal(esp_netif);
  77. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  78. }
  79. static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base,
  80. int32_t event_id, void *event_data)
  81. {
  82. s_retry_num = 0;
  83. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  84. if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) {
  85. return;
  86. }
  87. ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
  88. if (s_semph_get_ip_addrs) {
  89. xSemaphoreGive(s_semph_get_ip_addrs);
  90. } else {
  91. ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&event->ip_info.ip));
  92. }
  93. }
  94. #if CONFIG_EXAMPLE_CONNECT_IPV6
  95. static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base,
  96. int32_t event_id, void *event_data)
  97. {
  98. ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
  99. if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) {
  100. return;
  101. }
  102. esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
  103. ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
  104. IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
  105. if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
  106. if (s_semph_get_ip_addrs) {
  107. xSemaphoreGive(s_semph_get_ip_addrs);
  108. } else {
  109. ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
  110. }
  111. }
  112. }
  113. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  114. void example_wifi_start(void)
  115. {
  116. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  117. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  118. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
  119. // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
  120. esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_STA;
  121. esp_netif_config.route_prio = 128;
  122. s_example_sta_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
  123. esp_wifi_set_default_wifi_sta_handlers();
  124. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  125. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  126. ESP_ERROR_CHECK(esp_wifi_start());
  127. }
  128. void example_wifi_stop(void)
  129. {
  130. esp_err_t err = esp_wifi_stop();
  131. if (err == ESP_ERR_WIFI_NOT_INIT) {
  132. return;
  133. }
  134. ESP_ERROR_CHECK(err);
  135. ESP_ERROR_CHECK(esp_wifi_deinit());
  136. ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_sta_netif));
  137. esp_netif_destroy(s_example_sta_netif);
  138. s_example_sta_netif = NULL;
  139. }
  140. esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
  141. {
  142. if (wait) {
  143. s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
  144. }
  145. s_retry_num = 0;
  146. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL));
  147. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL));
  148. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif));
  149. #if CONFIG_EXAMPLE_CONNECT_IPV6
  150. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6, NULL));
  151. #endif
  152. ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
  153. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  154. esp_err_t ret = esp_wifi_connect();
  155. if (ret != ESP_OK) {
  156. ESP_LOGE(TAG, "WiFi connect failed! ret:%x", ret);
  157. return ret;
  158. }
  159. if (wait) {
  160. ESP_LOGI(TAG, "Waiting for IP(s)");
  161. for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
  162. xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
  163. }
  164. if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
  165. return ESP_FAIL;
  166. }
  167. }
  168. return ESP_OK;
  169. }
  170. esp_err_t example_wifi_sta_do_disconnect(void)
  171. {
  172. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect));
  173. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip));
  174. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect));
  175. #if CONFIG_EXAMPLE_CONNECT_IPV6
  176. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6));
  177. #endif
  178. if (s_semph_get_ip_addrs) {
  179. vSemaphoreDelete(s_semph_get_ip_addrs);
  180. }
  181. return esp_wifi_disconnect();
  182. }
  183. void example_wifi_shutdown(void)
  184. {
  185. example_wifi_sta_do_disconnect();
  186. example_wifi_stop();
  187. }
  188. esp_err_t example_wifi_connect(void)
  189. {
  190. ESP_LOGI(TAG, "Start example_connect.");
  191. example_wifi_start();
  192. wifi_config_t wifi_config = {
  193. .sta = {
  194. #if !CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
  195. .ssid = CONFIG_EXAMPLE_WIFI_SSID,
  196. .password = CONFIG_EXAMPLE_WIFI_PASSWORD,
  197. #endif
  198. .scan_method = EXAMPLE_WIFI_SCAN_METHOD,
  199. .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD,
  200. .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD,
  201. .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD,
  202. },
  203. };
  204. #if CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
  205. example_configure_stdin_stdout();
  206. char buf[sizeof(wifi_config.sta.ssid)+sizeof(wifi_config.sta.password)+2] = {0};
  207. ESP_LOGI(TAG, "Please input ssid password:");
  208. fgets(buf, sizeof(buf), stdin);
  209. int len = strlen(buf);
  210. buf[len-1] = '\0'; /* removes '\n' */
  211. memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid));
  212. char *rest = NULL;
  213. char *temp = strtok_r(buf, " ", &rest);
  214. strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid));
  215. memset(wifi_config.sta.password, 0, sizeof(wifi_config.sta.password));
  216. temp = strtok_r(NULL, " ", &rest);
  217. if (temp) {
  218. strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password));
  219. } else {
  220. wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
  221. }
  222. #endif
  223. return example_wifi_sta_do_connect(wifi_config, true);
  224. }
  225. #endif /* CONFIG_EXAMPLE_CONNECT_WIFI */