wifi_connect.c 5.4 KB

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