main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* main.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 "sdkconfig.h"
  22. #include "main.h"
  23. /* ESP specific */
  24. #include <nvs_flash.h>
  25. #include <esp_log.h>
  26. #include <esp_event.h>
  27. /* wolfSSL */
  28. /* Always include wolfcrypt/settings.h before any other wolfSSL file. */
  29. /* Reminder: settings.h pulls in user_settings.h; don't include it here. */
  30. #ifdef WOLFSSL_USER_SETTINGS
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #ifndef WOLFSSL_ESPIDF
  33. #warning "Problem with wolfSSL user_settings."
  34. #warning "Check components/wolfssl/include"
  35. #endif
  36. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  37. #else
  38. /* Define WOLFSSL_USER_SETTINGS project wide for settings.h to include */
  39. /* wolfSSL user settings in ./components/wolfssl/include/user_settings.h */
  40. #error "Missing WOLFSSL_USER_SETTINGS in CMakeLists or Makefile:\
  41. CFLAGS +=-DWOLFSSL_USER_SETTINGS"
  42. #endif
  43. /* this project */
  44. #include "server-tls.h"
  45. #include "time_helper.h"
  46. #ifdef CONFIG_IDF_TARGET_ESP32H2
  47. /* There's no WiFi on ESP32-H2.
  48. * For wired ethernet, see:
  49. * https://github.com/wolfSSL/wolfssl-examples/tree/master/ESP32/TLS13-ENC28J60-client */
  50. #else
  51. #include "wifi_connect.h"
  52. /*
  53. * Note ModBus TCP cannot be disabled on ESP8266 tos-sdk/v3.4
  54. * See https://github.com/espressif/esp-modbus/issues/2
  55. */
  56. #endif
  57. #ifdef WOLFSSL_TRACK_MEMORY
  58. #include <wolfssl/wolfcrypt/mem_track.h>
  59. #endif
  60. static const char* TAG = "main";
  61. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  62. && defined(WOLFSSL_ATECC508A)
  63. #include "wolfssl/wolfcrypt/port/atmel/atmel.h"
  64. /* when you want to use a custom slot allocation */
  65. /* enable the definition CUSTOM_SLOT_ALLOCATION. */
  66. #if defined(CUSTOM_SLOT_ALLOCATION)
  67. static byte mSlotList[ATECC_MAX_SLOT];
  68. int atmel_set_slot_allocator(atmel_slot_alloc_cb alloc, atmel_slot_dealloc_cb dealloc);
  69. /* initialize slot array */
  70. void my_atmel_slotInit()
  71. {
  72. int i;
  73. for(i = 0;i < ATECC_MAX_SLOT;i++) {
  74. mSlotList[i] = ATECC_INVALID_SLOT;
  75. }
  76. }
  77. /* allocate slot depending on slotType */
  78. int my_atmel_alloc(int slotType)
  79. {
  80. int i, slot = -1;
  81. switch(slotType){
  82. case ATMEL_SLOT_ENCKEY:
  83. slot = 4;
  84. break;
  85. case ATMEL_SLOT_DEVICE:
  86. slot = 0;
  87. break;
  88. case ATMEL_SLOT_ECDHE:
  89. slot = 0;
  90. break;
  91. case ATMEL_SLOT_ECDHE_ENC:
  92. slot = 4;
  93. break;
  94. case ATMEL_SLOT_ANY:
  95. for(i = 0;i < ATECC_MAX_SLOT;i++){
  96. if(mSlotList[i] == ATECC_INVALID_SLOT){
  97. slot = i;
  98. break;
  99. }
  100. }
  101. }
  102. return slot;
  103. }
  104. /* free slot array */
  105. void my_atmel_free(int slotId)
  106. {
  107. if(slotId >= 0 && slotId < ATECC_MAX_SLOT){
  108. mSlotList[slotId] = ATECC_INVALID_SLOT;
  109. }
  110. }
  111. #endif /* CUSTOM_SLOT_ALLOCATION */
  112. #endif /* WOLFSSL_ESPWROOM32SE && HAVE_PK_CALLBACK && WOLFSSL_ATECC508A */
  113. /* Entry for FreeRTOS */
  114. void app_main(void)
  115. {
  116. int stack_start = 0;
  117. int this_heap = 0;
  118. esp_err_t ret = 0;
  119. ESP_LOGI(TAG, "---------------- wolfSSL TLS Server Example ------------");
  120. ESP_LOGI(TAG, "--------------------------------------------------------");
  121. ESP_LOGI(TAG, "--------------------------------------------------------");
  122. ESP_LOGI(TAG, "---------------------- BEGIN MAIN ----------------------");
  123. ESP_LOGI(TAG, "--------------------------------------------------------");
  124. ESP_LOGI(TAG, "--------------------------------------------------------");
  125. #if !defined(CONFIG_WOLFSSL_EXAMPLE_NAME_TLS_SERVER)
  126. ESP_LOGW(TAG, "Warning: Example wolfSSL misconfigured? Check menuconfig.");
  127. #endif
  128. #ifdef ESP_SDK_MEM_LIB_VERSION
  129. sdk_init_meminfo();
  130. #endif
  131. #ifdef ESP_TASK_MAIN_STACK
  132. ESP_LOGI(TAG, "ESP_TASK_MAIN_STACK: %d", ESP_TASK_MAIN_STACK);
  133. #endif
  134. #ifdef TASK_EXTRA_STACK_SIZE
  135. ESP_LOGI(TAG, "TASK_EXTRA_STACK_SIZE: %d", TASK_EXTRA_STACK_SIZE);
  136. #endif
  137. #ifdef SINGLE_THREADED
  138. ESP_LOGI(TAG, "Single threaded");
  139. #else
  140. ESP_LOGI(TAG, "CONFIG_ESP_MAIN_TASK_STACK_SIZE = %d bytes (%d words)",
  141. CONFIG_ESP_MAIN_TASK_STACK_SIZE,
  142. (int)(CONFIG_ESP_MAIN_TASK_STACK_SIZE / sizeof(void*)));
  143. #ifdef INCLUDE_uxTaskGetStackHighWaterMark
  144. {
  145. /* Returns the high water mark of the stack associated with xTask. That is,
  146. * the minimum free stack space there has been (in bytes not words, unlike
  147. * vanilla FreeRTOS) since the task started. The smaller the returned
  148. * number the closer the task has come to overflowing its stack.
  149. * see Espressif api-reference/system/freertos_idf
  150. */
  151. stack_start = uxTaskGetStackHighWaterMark(NULL);
  152. #ifdef ESP_SDK_MEM_LIB_VERSION
  153. {
  154. sdk_var_whereis("stack_start", &stack_start);
  155. }
  156. #endif
  157. ESP_LOGI(TAG, "Stack Start HWM: %d bytes", stack_start);
  158. }
  159. #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
  160. #endif /* SINGLE_THREADED */
  161. #ifdef HAVE_VERSION_EXTENDED_INFO
  162. esp_ShowExtendedSystemInfo();
  163. #endif
  164. #ifdef DEBUG_WOLFSSL
  165. wolfSSL_Debugging_OFF();
  166. #endif
  167. #ifdef CONFIG_IDF_TARGET_ESP32H2
  168. ESP_LOGE(TAG, "No WiFi on the ESP32-H2 and ethernet not yet supported");
  169. while (1) {
  170. vTaskDelay(60000);
  171. }
  172. #endif
  173. /* Set time for cert validation.
  174. * Some lwIP APIs, including SNTP functions, are not thread safe. */
  175. ret = set_time(); /* need to setup NTP before WiFi */
  176. /* Optionally erase flash */
  177. /* ESP_ERROR_CHECK(nvs_flash_erase()); */
  178. #ifdef FOUND_PROTOCOL_EXAMPLES_DIR
  179. ESP_LOGI(TAG, "FOUND_PROTOCOL_EXAMPLES_DIR active, using example code.");
  180. ESP_ERROR_CHECK(nvs_flash_init());
  181. #if defined(CONFIG_IDF_TARGET_ESP32H2)
  182. ESP_LOGE(TAG, "There's no WiFi on ESP32-H2.");
  183. #else
  184. #ifdef CONFIG_EXAMPLE_WIFI_SSID
  185. if (XSTRCMP(CONFIG_EXAMPLE_WIFI_SSID, "myssid") == 0) {
  186. ESP_LOGW(TAG, "WARNING: CONFIG_EXAMPLE_WIFI_SSID is myssid.");
  187. ESP_LOGW(TAG, " Do you have a WiFi AP called myssid, or ");
  188. ESP_LOGW(TAG, " did you forget the ESP-IDF configuration?");
  189. }
  190. #else
  191. #define CONFIG_EXAMPLE_WIFI_SSID "myssid"
  192. ESP_LOGW(TAG, "WARNING: CONFIG_EXAMPLE_WIFI_SSID not defined.");
  193. #endif
  194. ESP_ERROR_CHECK(esp_netif_init());
  195. ESP_ERROR_CHECK(esp_event_loop_create_default());
  196. ESP_ERROR_CHECK(example_connect());
  197. #endif
  198. #else
  199. ESP_ERROR_CHECK(nvs_flash_init());
  200. /* Initialize NVS */
  201. ret = nvs_flash_init();
  202. #if defined(CONFIG_IDF_TARGET_ESP8266)
  203. {
  204. if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
  205. ESP_ERROR_CHECK(nvs_flash_erase());
  206. ret = nvs_flash_init();
  207. }
  208. }
  209. #else
  210. {
  211. /* Non-ESP8266 initialization is slightly different */
  212. if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
  213. ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  214. ESP_ERROR_CHECK(nvs_flash_erase());
  215. ret = nvs_flash_init();
  216. }
  217. }
  218. #endif /* else not CONFIG_IDF_TARGET_ESP8266 */
  219. ESP_ERROR_CHECK(ret);
  220. #if defined(CONFIG_IDF_TARGET_ESP32H2)
  221. ESP_LOGE(TAG, "There's no WiFi on ESP32-H2. ");
  222. #else
  223. /* Initialize WiFi */
  224. ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  225. ret = wifi_init_sta();
  226. while (ret != 0) {
  227. ESP_LOGI(TAG, "Waiting...");
  228. vTaskDelay(60000 / portTICK_PERIOD_MS);
  229. ESP_LOGI(TAG, "Trying WiFi again...");
  230. ret = wifi_init_sta();
  231. }
  232. #endif /* else not CONFIG_IDF_TARGET_ESP32H2 */
  233. #endif /* else FOUND_PROTOCOL_EXAMPLES_DIR not found */
  234. /* Once we are connected to the network, start & wait for NTP time */
  235. ret = set_time_wait_for_ntp();
  236. if (ret < -1) {
  237. /* a value of -1 means there was no NTP server, so no need to wait */
  238. ESP_LOGI(TAG, "Waiting 10 more seconds for NTP to complete." );
  239. vTaskDelay(10000 / portTICK_PERIOD_MS); /* brute-force solution */
  240. esp_show_current_datetime();
  241. }
  242. #if defined(SINGLE_THREADED)
  243. /* just call the task */
  244. tls_smp_server_task((void*)NULL);
  245. #else
  246. tls_args args[1] = {0};
  247. /* start a thread with the task */
  248. /* HWM is maximum amount of stack space that has been unused, in bytes
  249. * not words (unlike vanilla freeRTOS). */
  250. this_heap = esp_get_free_heap_size();
  251. ESP_LOGI(TAG, "Initial Stack Used (before wolfSSL Server): %d bytes",
  252. CONFIG_ESP_MAIN_TASK_STACK_SIZE
  253. - (uxTaskGetStackHighWaterMark(NULL))
  254. );
  255. ESP_LOGI(TAG, "Starting TLS Server task...\n");
  256. ESP_LOGI(TAG, "main tls_smp_client_init heap @ %p = %d",
  257. &this_heap, this_heap);
  258. tls_smp_server_init(args); /* NULL will use the DEFAULT_PORT value */
  259. #endif
  260. /* Done */
  261. #ifdef SINGLE_THREADED
  262. ESP_LOGV(TAG, "\n\nDone!\n\n");
  263. while (1);
  264. #else
  265. ESP_LOGV(TAG, "\n\nvTaskDelete...\n\n");
  266. vTaskDelete(NULL);
  267. /* done */
  268. while (1) {
  269. ESP_LOGV(TAG, "\n\nLoop...\n\n");
  270. #ifdef INCLUDE_uxTaskGetStackHighWaterMark
  271. ESP_LOGI(TAG, "Stack HWM: %d", uxTaskGetStackHighWaterMark(NULL));
  272. ESP_LOGI(TAG, "Stack used: %d", CONFIG_ESP_MAIN_TASK_STACK_SIZE
  273. - (uxTaskGetStackHighWaterMark(NULL) ));
  274. #endif
  275. vTaskDelay(60000);
  276. } /* done while */
  277. #endif /* else not SINGLE_THREADED */
  278. } /* app_main */