1
0

main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include "wolfssl/wolfcrypt/settings.h"
  25. #include "rl_net.h" /* Network definitions */
  26. #include <time.h>
  27. #if defined(WOLFSSL_CMSIS_RTOS)
  28. #include "cmsis_os.h"
  29. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  30. #include "cmsis_os2.h"
  31. #endif
  32. /* Dummy definition for test RTC */
  33. #define RTC_YEAR 2019
  34. #if defined(STM32F7xx)
  35. #include "stm32f7xx_hal.h"
  36. #elif defined(STM32F4xx)
  37. #include "stm32f4xx_hal.h"
  38. #elif defined(STM32F2xx)
  39. #include "stm32f2xx_hal.h"
  40. #endif
  41. //-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
  42. // <h>Server parameter
  43. // ====================
  44. // <s.6>Port
  45. // <i> Default: "11111"
  46. #define SERVER_PORT "11111"
  47. // </h>
  48. // <h>Protocol
  49. // ====================
  50. // <o>SSL/TLS Version<0=> SSL3 <1=> TLS1.0 <2=> TLS1.1 <3=> TLS1.2 <4=> TLS1.3
  51. #define TLS_VER 3
  52. // <s.2>Other option
  53. #define OTHER_OPTIONS ""
  54. // </h>
  55. // <h>RTC: for validate certificate date
  56. // <o>Year <1970-2099>
  57. #define RTC_YEAR 2023
  58. #define RTC_MONTH 1
  59. #define RTC_DAY 1
  60. // </h>
  61. //------------- <<< end of configuration section >>> -----------------------
  62. #warning "write MPU specific Set ups\n"
  63. static void SystemClock_Config (void) {
  64. }
  65. static void MPU_Config (void) {
  66. }
  67. static void CPU_CACHE_Enable (void) {
  68. }
  69. /*-----------------------------------------------------------------------------
  70. * Initialize a Flash Memory Card
  71. *----------------------------------------------------------------------------*/
  72. #if !defined(NO_FILESYSTEM)
  73. #include "rl_fs.h" /* FileSystem definitions */
  74. static void init_filesystem(void)
  75. {
  76. int32_t retv;
  77. retv = finit ("M0:");
  78. if (retv == fsOK) {
  79. retv = fmount ("M0:");
  80. if (retv == fsOK) {
  81. printf ("Drive M0 ready!\n");
  82. }
  83. else {
  84. printf ("Drive M0 mount failed(%d)!\n", retv);
  85. }
  86. }
  87. else {
  88. printf ("Drive M0 initialization failed!\n");
  89. }
  90. }
  91. #endif
  92. #if defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)
  93. #if defined(WOLFSSL_CMSIS_RTOS)
  94. extern uint32_t os_time;
  95. #endif
  96. uint32_t HAL_GetTick(void)
  97. {
  98. #if defined(WOLFSSL_CMSIS_RTOS)
  99. return os_time;
  100. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  101. return osKernelGetTickCount();
  102. #endif
  103. }
  104. double current_time(int reset)
  105. {
  106. #if defined(WOLFSSL_CMSIS_RTOS)
  107. return (double)os_time / 1000.0;
  108. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  109. return (double)osKernelGetTickCount() / 1000.0;
  110. #endif
  111. }
  112. #else
  113. #include <stdint.h>
  114. #define DWT ((DWT_Type *) (0xE0001000UL) )
  115. typedef struct
  116. {
  117. uint32_t CTRL; /*!< Offset: 0x000 (R/W) Control Register */
  118. uint32_t CYCCNT; /*!< Offset: 0x004 (R/W) Cycle Count Register */
  119. } DWT_Type;
  120. extern uint32_t SystemCoreClock;
  121. double current_time(int reset)
  122. {
  123. if (reset)
  124. DWT->CYCCNT = 0;
  125. return ((double)DWT->CYCCNT / SystemCoreClock);
  126. }
  127. #endif
  128. static time_t epochTime;
  129. time_t time(time_t *t)
  130. {
  131. return epochTime;
  132. }
  133. void setTime(time_t t)
  134. {
  135. epochTime = t;
  136. }
  137. extern void server_test(void const *arg);
  138. #if defined(WOLFSSL_CMSIS_RTOSv2)
  139. void app_main(void *arg)
  140. #else
  141. void app_main(void const*arg)
  142. #endif
  143. {
  144. if (netInitialize() == netOK)
  145. server_test(arg);
  146. else
  147. printf("ERROR: netInitialize\n");
  148. }
  149. #if defined(WOLFSSL_CMSIS_RTOS)
  150. osThreadDef(app_main, osPriorityLow, 1, 32 * 1024);
  151. #endif
  152. /*----------------------------------------------------------------------------
  153. Main Thread 'main': Run Network
  154. *---------------------------------------------------------------------------*/
  155. #include <stdio.h>
  156. typedef struct func_args {
  157. int argc;
  158. char** argv;
  159. } func_args;
  160. int myoptind = 0;
  161. char *myoptarg = NULL;
  162. int main(void)
  163. {
  164. static char *argv[] =
  165. { "server", "-p", SERVER_PORT,
  166. "-v", " ", OTHER_OPTIONS };
  167. static func_args args =
  168. { sizeof(argv)/sizeof(*argv[0]), argv };
  169. char *verStr[] = { "SSL3", "TLS1.0", "TLS1.1", "TLS1.2", "TLS1.3"};
  170. #define VERSIZE 2
  171. char ver[VERSIZE];
  172. MPU_Config(); /* Configure the MPU */
  173. CPU_CACHE_Enable(); /* Enable the CPU Cache */
  174. HAL_Init(); /* Initialize the HAL Library */
  175. SystemClock_Config(); /* Configure the System Clock */
  176. #if !defined(NO_FILESYSTEM)
  177. init_filesystem ();
  178. #endif
  179. #if defined(WOLFSSL_CMSIS_RTOSv2)
  180. osKernelInitialize();
  181. #endif
  182. #if defined(DEBUG_WOLFSSL)
  183. printf("Turning ON Debug message\n");
  184. wolfSSL_Debugging_ON();
  185. #endif
  186. snprintf(ver, VERSIZE, "%d", TLS_VER);
  187. argv[4] = ver;
  188. printf("SSL/TLS Server\n ");
  189. printf(" Server Port: %s\n Version: %s\n", argv[2], verStr[TLS_VER]);
  190. printf(" Other options: %s\n", OTHER_OPTIONS);
  191. setTime((RTC_YEAR-1970)*365*24*60*60 +
  192. RTC_MONTH*30*24*60*60 +
  193. RTC_DAY*24*60*60);
  194. #if defined(WOLFSSL_CMSIS_RTOS)
  195. osThreadCreate(osThread(app_main), (void *)&args);
  196. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  197. osThreadNew(app_main, (void *)&args, NULL);
  198. #endif
  199. osKernelStart();
  200. }