wifi_connect.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* wifi_connect.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include "wifi_connect.h"
  22. #include <freertos/FreeRTOS.h>
  23. #include <freertos/task.h>
  24. #include <freertos/event_groups.h>
  25. #include <esp_wifi.h>
  26. #include <esp_log.h>
  27. /* wolfSSL */
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #include <wolfssl/version.h>
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #ifndef WOLFSSL_ESPIDF
  32. #warning "Problem with wolfSSL user_settings."
  33. #warning "Check components/wolfssl/include"
  34. #endif
  35. #if ESP_IDF_VERSION_MAJOR >= 5
  36. #elif ESP_IDF_VERSION_MAJOR >= 4
  37. #include "protocol_examples_common.h"
  38. #else
  39. const static int CONNECTED_BIT = BIT0;
  40. static EventGroupHandle_t wifi_event_group;
  41. #endif
  42. #if defined(ESP_IDF_VERSION_MAJOR) && defined(ESP_IDF_VERSION_MINOR)
  43. #if ESP_IDF_VERSION_MAJOR >= 4
  44. /* likely using examples, see wifi_connect.h */
  45. #else
  46. /* TODO - still supporting pre V4 ? */
  47. const static int CONNECTED_BIT = BIT0;
  48. static EventGroupHandle_t wifi_event_group;
  49. #endif
  50. #if (ESP_IDF_VERSION_MAJOR == 5)
  51. #define HAS_WPA3_FEATURES
  52. #else
  53. #undef HAS_WPA3_FEATURES
  54. #endif
  55. #else
  56. /* TODO Consider pre IDF v5? */
  57. #endif
  58. /* breadcrumb prefix for logging */
  59. const static char *TAG = "wifi_connect";
  60. #if ESP_IDF_VERSION_MAJOR < 4
  61. /* event handler for wifi events */
  62. static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
  63. {
  64. switch (event->event_id)
  65. {
  66. case SYSTEM_EVENT_STA_START:
  67. esp_wifi_connect();
  68. break;
  69. case SYSTEM_EVENT_STA_GOT_IP:
  70. #if ESP_IDF_VERSION_MAJOR >= 4
  71. ESP_LOGI(TAG, "got ip:" IPSTR "\n",
  72. IP2STR(&event->event_info.got_ip.ip_info.ip));
  73. #else
  74. ESP_LOGI(TAG, "got ip:%s",
  75. ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  76. #endif
  77. /* see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos_idf.html */
  78. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  79. break;
  80. case SYSTEM_EVENT_STA_DISCONNECTED:
  81. esp_wifi_connect();
  82. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  83. break;
  84. default:
  85. break;
  86. }
  87. return ESP_OK;
  88. }
  89. #else
  90. #ifdef CONFIG_ESP_MAXIMUM_RETRY
  91. #define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
  92. #else
  93. #define CONFIG_ESP_MAXIMUM_RETRY 5
  94. #endif
  95. #if CONFIG_ESP_WIFI_AUTH_OPEN
  96. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
  97. #elif CONFIG_ESP_WIFI_AUTH_WEP
  98. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
  99. #elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
  100. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
  101. #elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
  102. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
  103. #elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
  104. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
  105. #elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
  106. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
  107. #elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
  108. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
  109. #elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
  110. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
  111. #endif
  112. #ifndef ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD
  113. #define CONFIG_ESP_WIFI_AUTH_WPA2_PSK 1
  114. #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD CONFIG_ESP_WIFI_AUTH_WPA2_PSK
  115. #endif
  116. /* FreeRTOS event group to signal when we are connected*/
  117. static EventGroupHandle_t s_wifi_event_group;
  118. /* The event group allows multiple bits for each event, but we only care about two events:
  119. * - we are connected to the AP with an IP
  120. * - we failed to connect after the maximum amount of retries */
  121. #define WIFI_CONNECTED_BIT BIT0
  122. #define WIFI_FAIL_BIT BIT1
  123. static int s_retry_num = 0;
  124. ip_event_got_ip_t* event;
  125. static void event_handler(void* arg,
  126. esp_event_base_t event_base,
  127. int32_t event_id,
  128. void* event_data)
  129. {
  130. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  131. esp_wifi_connect();
  132. }
  133. else if (event_base == WIFI_EVENT &&
  134. event_id == WIFI_EVENT_STA_DISCONNECTED) {
  135. if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
  136. esp_wifi_connect();
  137. s_retry_num++;
  138. ESP_LOGI(TAG, "retry to connect to the AP");
  139. }
  140. else {
  141. xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  142. }
  143. ESP_LOGI(TAG, "connect to the AP fail");
  144. }
  145. else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  146. event = (ip_event_got_ip_t*) event_data;
  147. wifi_show_ip();
  148. s_retry_num = 0;
  149. xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  150. }
  151. }
  152. int wifi_init_sta(void)
  153. {
  154. int ret = ESP_OK;
  155. s_wifi_event_group = xEventGroupCreate();
  156. ESP_ERROR_CHECK(esp_netif_init());
  157. ESP_ERROR_CHECK(esp_event_loop_create_default());
  158. esp_netif_create_default_wifi_sta();
  159. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  160. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  161. esp_event_handler_instance_t instance_any_id;
  162. esp_event_handler_instance_t instance_got_ip;
  163. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  164. ESP_EVENT_ANY_ID,
  165. &event_handler,
  166. NULL,
  167. &instance_any_id));
  168. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
  169. IP_EVENT_STA_GOT_IP,
  170. &event_handler,
  171. NULL,
  172. &instance_got_ip));
  173. wifi_config_t wifi_config = {
  174. .sta = {
  175. .ssid = EXAMPLE_ESP_WIFI_SSID,
  176. .password = EXAMPLE_ESP_WIFI_PASS,
  177. /* Authmode threshold resets to WPA2 as default if password matches
  178. * WPA2 standards (pasword len => 8). If you want to connect the
  179. * device to deprecated WEP/WPA networks, Please set the threshold
  180. * value WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with
  181. * length and format matching to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK
  182. * standards. */
  183. .threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
  184. #ifdef HAS_WPA3_FEATURES
  185. .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
  186. #endif
  187. },
  188. };
  189. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  190. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  191. #ifdef CONFIG_EXAMPLE_WIFI_SSID
  192. if (XSTRCMP(CONFIG_EXAMPLE_WIFI_SSID, "myssid") == 0) {
  193. ESP_LOGW(TAG, "WARNING: CONFIG_EXAMPLE_WIFI_SSID is \"myssid\".");
  194. ESP_LOGW(TAG, " Do you have a WiFi AP called \"myssid\", ");
  195. ESP_LOGW(TAG, " or did you forget the ESP-IDF configuration?");
  196. }
  197. #else
  198. ESP_LOGW(TAG, "WARNING: CONFIG_EXAMPLE_WIFI_SSID not defined.");
  199. #endif
  200. ESP_ERROR_CHECK(esp_wifi_start() );
  201. ESP_LOGI(TAG, "wifi_init_sta finished.");
  202. /* Waiting until either the connection is established (WIFI_CONNECTED_BIT)
  203. * or connection failed for the maximum number of re-tries (WIFI_FAIL_BIT).
  204. * The bits are set by event_handler() (see above) */
  205. EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  206. WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  207. pdFALSE,
  208. pdFALSE,
  209. portMAX_DELAY);
  210. /* xEventGroupWaitBits() returns the bits before the call returned,
  211. * hence we can test which event actually happened. */
  212. #if defined(SHOW_SSID_AND_PASSWORD)
  213. ESP_LOGW(TAG, "Undefine SHOW_SSID_AND_PASSWORD to not show SSID/password");
  214. if (bits & WIFI_CONNECTED_BIT) {
  215. ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
  216. EXAMPLE_ESP_WIFI_SSID,
  217. EXAMPLE_ESP_WIFI_PASS);
  218. }
  219. else if (bits & WIFI_FAIL_BIT) {
  220. ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
  221. EXAMPLE_ESP_WIFI_SSID,
  222. EXAMPLE_ESP_WIFI_PASS);
  223. }
  224. else {
  225. ESP_LOGE(TAG, "UNEXPECTED EVENT");
  226. }
  227. #else
  228. if (bits & WIFI_CONNECTED_BIT) {
  229. ESP_LOGI(TAG, "Connected to AP");
  230. }
  231. else if (bits & WIFI_FAIL_BIT) {
  232. ESP_LOGI(TAG, "Failed to connect to AP");
  233. ret = -1;
  234. }
  235. else {
  236. ESP_LOGE(TAG, "AP UNEXPECTED EVENT");
  237. ret = -2;
  238. }
  239. #endif
  240. return ret;
  241. }
  242. int wifi_show_ip(void)
  243. {
  244. /* ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); */
  245. return 0;
  246. }
  247. #endif