wifi_connect.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 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_CONNECT_IPV6
  21. static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL;
  22. #endif
  23. #if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
  24. #define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
  25. #elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
  26. #define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
  27. #endif
  28. #if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
  29. #define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  30. #elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
  31. #define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  32. #endif
  33. #if CONFIG_EXAMPLE_WIFI_AUTH_OPEN
  34. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
  35. #elif CONFIG_EXAMPLE_WIFI_AUTH_WEP
  36. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
  37. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK
  38. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
  39. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK
  40. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
  41. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
  42. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
  43. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
  44. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE
  45. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK
  46. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
  47. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
  48. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
  49. #elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK
  50. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
  51. #endif
  52. static int s_retry_num = 0;
  53. static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base,
  54. int32_t event_id, void *event_data)
  55. {
  56. s_retry_num++;
  57. if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
  58. ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num);
  59. /* let example_wifi_sta_do_connect() return */
  60. if (s_semph_get_ip_addrs) {
  61. xSemaphoreGive(s_semph_get_ip_addrs);
  62. }
  63. #if CONFIG_EXAMPLE_CONNECT_IPV6
  64. if (s_semph_get_ip6_addrs) {
  65. xSemaphoreGive(s_semph_get_ip6_addrs);
  66. }
  67. #endif
  68. return;
  69. }
  70. ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
  71. esp_err_t err = esp_wifi_connect();
  72. if (err == ESP_ERR_WIFI_NOT_STARTED) {
  73. return;
  74. }
  75. ESP_ERROR_CHECK(err);
  76. }
  77. static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base,
  78. int32_t event_id, void *event_data)
  79. {
  80. #if CONFIG_EXAMPLE_CONNECT_IPV6
  81. esp_netif_create_ip6_linklocal(esp_netif);
  82. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  83. }
  84. static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base,
  85. int32_t event_id, void *event_data)
  86. {
  87. s_retry_num = 0;
  88. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  89. if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) {
  90. return;
  91. }
  92. ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
  93. if (s_semph_get_ip_addrs) {
  94. xSemaphoreGive(s_semph_get_ip_addrs);
  95. } else {
  96. ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&event->ip_info.ip));
  97. }
  98. }
  99. #if CONFIG_EXAMPLE_CONNECT_IPV6
  100. static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base,
  101. int32_t event_id, void *event_data)
  102. {
  103. ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
  104. if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) {
  105. return;
  106. }
  107. esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
  108. ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
  109. IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
  110. if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
  111. if (s_semph_get_ip6_addrs) {
  112. xSemaphoreGive(s_semph_get_ip6_addrs);
  113. } else {
  114. ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
  115. }
  116. }
  117. }
  118. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  119. void example_wifi_start(void)
  120. {
  121. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  122. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  123. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
  124. // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
  125. esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_STA;
  126. esp_netif_config.route_prio = 128;
  127. s_example_sta_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
  128. esp_wifi_set_default_wifi_sta_handlers();
  129. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  130. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  131. ESP_ERROR_CHECK(esp_wifi_start());
  132. }
  133. void example_wifi_stop(void)
  134. {
  135. esp_err_t err = esp_wifi_stop();
  136. if (err == ESP_ERR_WIFI_NOT_INIT) {
  137. return;
  138. }
  139. ESP_ERROR_CHECK(err);
  140. ESP_ERROR_CHECK(esp_wifi_deinit());
  141. ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_sta_netif));
  142. esp_netif_destroy(s_example_sta_netif);
  143. s_example_sta_netif = NULL;
  144. }
  145. esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
  146. {
  147. if (wait) {
  148. s_semph_get_ip_addrs = xSemaphoreCreateBinary();
  149. if (s_semph_get_ip_addrs == NULL) {
  150. return ESP_ERR_NO_MEM;
  151. }
  152. #if CONFIG_EXAMPLE_CONNECT_IPV6
  153. s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
  154. if (s_semph_get_ip6_addrs == NULL) {
  155. vSemaphoreDelete(s_semph_get_ip_addrs);
  156. return ESP_ERR_NO_MEM;
  157. }
  158. #endif
  159. }
  160. s_retry_num = 0;
  161. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL));
  162. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL));
  163. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif));
  164. #if CONFIG_EXAMPLE_CONNECT_IPV6
  165. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6, NULL));
  166. #endif
  167. ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
  168. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  169. esp_err_t ret = esp_wifi_connect();
  170. if (ret != ESP_OK) {
  171. ESP_LOGE(TAG, "WiFi connect failed! ret:%x", ret);
  172. return ret;
  173. }
  174. if (wait) {
  175. ESP_LOGI(TAG, "Waiting for IP(s)");
  176. #if CONFIG_EXAMPLE_CONNECT_IPV4
  177. xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
  178. #endif
  179. #if CONFIG_EXAMPLE_CONNECT_IPV6
  180. xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY);
  181. #endif
  182. if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
  183. return ESP_FAIL;
  184. }
  185. }
  186. return ESP_OK;
  187. }
  188. esp_err_t example_wifi_sta_do_disconnect(void)
  189. {
  190. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect));
  191. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip));
  192. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect));
  193. #if CONFIG_EXAMPLE_CONNECT_IPV6
  194. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6));
  195. #endif
  196. if (s_semph_get_ip_addrs) {
  197. vSemaphoreDelete(s_semph_get_ip_addrs);
  198. }
  199. #if CONFIG_EXAMPLE_CONNECT_IPV6
  200. if (s_semph_get_ip6_addrs) {
  201. vSemaphoreDelete(s_semph_get_ip6_addrs);
  202. }
  203. #endif
  204. return esp_wifi_disconnect();
  205. }
  206. void example_wifi_shutdown(void)
  207. {
  208. example_wifi_sta_do_disconnect();
  209. example_wifi_stop();
  210. }
  211. esp_err_t example_wifi_connect(void)
  212. {
  213. ESP_LOGI(TAG, "Start example_connect.");
  214. example_wifi_start();
  215. wifi_config_t wifi_config = {
  216. .sta = {
  217. #if !CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
  218. .ssid = CONFIG_EXAMPLE_WIFI_SSID,
  219. .password = CONFIG_EXAMPLE_WIFI_PASSWORD,
  220. #endif
  221. .scan_method = EXAMPLE_WIFI_SCAN_METHOD,
  222. .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD,
  223. .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD,
  224. .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD,
  225. },
  226. };
  227. #if CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
  228. example_configure_stdin_stdout();
  229. char buf[sizeof(wifi_config.sta.ssid)+sizeof(wifi_config.sta.password)+2] = {0};
  230. ESP_LOGI(TAG, "Please input ssid password:");
  231. fgets(buf, sizeof(buf), stdin);
  232. int len = strlen(buf);
  233. buf[len-1] = '\0'; /* removes '\n' */
  234. memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid));
  235. char *rest = NULL;
  236. char *temp = strtok_r(buf, " ", &rest);
  237. strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid));
  238. memset(wifi_config.sta.password, 0, sizeof(wifi_config.sta.password));
  239. temp = strtok_r(NULL, " ", &rest);
  240. if (temp) {
  241. strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password));
  242. } else {
  243. wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
  244. }
  245. #endif
  246. return example_wifi_sta_do_connect(wifi_config, true);
  247. }
  248. #endif /* CONFIG_EXAMPLE_CONNECT_WIFI */