wifi_connect.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* wifi_connect.c
  2. *
  3. * Copyright (C) 2006-2023 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. /*ESP specific */
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/task.h"
  24. #include "freertos/event_groups.h"
  25. #include "wifi_connect.h"
  26. #include "lwip/sockets.h"
  27. #include "lwip/netdb.h"
  28. #include "lwip/apps/sntp.h"
  29. #include "nvs_flash.h"
  30. #if ESP_IDF_VERSION_MAJOR >= 4
  31. #include "protocol_examples_common.h"
  32. #else
  33. const static int CONNECTED_BIT = BIT0;
  34. static EventGroupHandle_t wifi_event_group;
  35. #endif
  36. /* proto-type */
  37. extern void tls_smp_client_task();
  38. static void tls_smp_client_init();
  39. const static char *TAG = "tls_client";
  40. static void set_time()
  41. {
  42. /* set dummy wallclock time. */
  43. struct timeval utctime;
  44. struct timezone tz;
  45. struct strftime_buf;
  46. time_t now;
  47. struct tm timeinfo;
  48. char strftime_buf[64];
  49. /* please update the time if seeing unknown failure when loading cert. */
  50. /* this could cause TLS communication failure due to time expiration */
  51. /* incleasing 31536000 seconds is close to spend 356 days. */
  52. utctime.tv_sec = 1645797600; /* dummy time: Fri 25 Feb 2022 02:00:00 2022 */
  53. utctime.tv_usec = 0;
  54. tz.tz_minuteswest = 0;
  55. tz.tz_dsttime = 0;
  56. settimeofday(&utctime, &tz);
  57. time(&now);
  58. localtime_r(&now, &timeinfo);
  59. strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
  60. ESP_LOGI(TAG, "The current date/time is: %s", strftime_buf);
  61. #if ESP_IDF_VERSION_MAJOR < 4
  62. /* wait until wifi connect */
  63. xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  64. false, true, portMAX_DELAY);
  65. #endif
  66. /* now we start client tasks. */
  67. tls_smp_client_init();
  68. }
  69. /* create task */
  70. static void tls_smp_client_init(void)
  71. {
  72. int ret;
  73. #if ESP_IDF_VERSION_MAJOR >= 4
  74. TaskHandle_t _handle;
  75. #else
  76. xTaskHandle _handle;
  77. #endif
  78. /* http://esp32.info/docs/esp_idf/html/dd/d3c/group__xTaskCreate.html */
  79. ret = xTaskCreate(tls_smp_client_task,
  80. TLS_SMP_CLIENT_TASK_NAME,
  81. TLS_SMP_CLIENT_TASK_WORDS,
  82. NULL,
  83. TLS_SMP_CLIENT_TASK_PRIORITY,
  84. &_handle);
  85. if (ret != pdPASS) {
  86. ESP_LOGI(TAG, "create thread %s failed", TLS_SMP_CLIENT_TASK_NAME);
  87. }
  88. }
  89. #if ESP_IDF_VERSION_MAJOR < 4
  90. /* event handler for wifi events */
  91. static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
  92. {
  93. switch (event->event_id)
  94. {
  95. case SYSTEM_EVENT_STA_START:
  96. esp_wifi_connect();
  97. break;
  98. case SYSTEM_EVENT_STA_GOT_IP:
  99. #if ESP_IDF_VERSION_MAJOR >= 4
  100. ESP_LOGI(TAG, "got ip:" IPSTR "\n",
  101. IP2STR(&event->event_info.got_ip.ip_info.ip));
  102. #else
  103. ESP_LOGI(TAG, "got ip:%s",
  104. ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  105. #endif
  106. /* http://esp32.info/docs/esp_idf/html/dd/d08/group__xEventGroupSetBits.html */
  107. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  108. break;
  109. case SYSTEM_EVENT_STA_DISCONNECTED:
  110. esp_wifi_connect();
  111. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  112. break;
  113. default:
  114. break;
  115. }
  116. return ESP_OK;
  117. }
  118. #endif
  119. /* entry point */
  120. void app_main(void)
  121. {
  122. ESP_LOGI(TAG, "Start app_main...");
  123. ESP_ERROR_CHECK(nvs_flash_init());
  124. ESP_LOGI(TAG, "Initialize wifi");
  125. #if (ESP_IDF_VERSION_MAJOR >= 4 && ESP_IDF_VERSION_MINOR >= 1) || \
  126. (ESP_IDF_VERSION_MAJOR > 5)
  127. esp_netif_init();
  128. #else
  129. tcpip_adapter_init();
  130. #endif
  131. /* */
  132. #if ESP_IDF_VERSION_MAJOR >= 4
  133. ESP_ERROR_CHECK(esp_event_loop_create_default());
  134. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  135. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  136. * examples/protocols/README.md for more information about this function.
  137. */
  138. ESP_ERROR_CHECK(example_connect());
  139. #else
  140. wifi_event_group = xEventGroupCreate();
  141. ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL));
  142. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  143. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  144. wifi_config_t wifi_config = {
  145. .sta = {
  146. .ssid = TLS_SMP_WIFI_SSID,
  147. .password = TLS_SMP_WIFI_PASS,
  148. },
  149. };
  150. /* WiFi station mode */
  151. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  152. /* Wifi Set the configuration of the ESP32 STA or AP */
  153. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  154. /* Start Wifi */
  155. ESP_ERROR_CHECK(esp_wifi_start() );
  156. ESP_LOGI(TAG, "wifi_init_sta finished.");
  157. ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
  158. TLS_SMP_WIFI_SSID, TLS_SMP_WIFI_PASS);
  159. #endif
  160. ESP_LOGI(TAG, "Set dummy time...");
  161. set_time();
  162. }