main.c 9.7 KB

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