main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* main.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. #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. #if defined(STM32F7xx)
  33. #include "stm32f7xx_hal.h"
  34. #elif defined(STM32F4xx)
  35. #include "stm32f4xx_hal.h"
  36. #elif defined(STM32F2xx)
  37. #include "stm32f2xx_hal.h"
  38. #endif
  39. //-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
  40. // <h>Remote Address
  41. // ====================
  42. //
  43. // <s.15>IP Address
  44. // <i>Static IPv4 Address
  45. // <i>Default: "192.168.1.1"
  46. #define REMOTE_IP "192.168.10.4"
  47. // <s.6>Port
  48. // <i> Default: "11111"
  49. #define REMOTE_PORT "11111"
  50. // </h>
  51. // <h>Protocol
  52. // ====================
  53. // <o>SSL/TLS Version<0=> SSL3 <1=> TLS1.0 <2=> TLS1.1 <3=> TLS1.2 <4=> TLS1.3
  54. #define TLS_VER 3
  55. // <s.2>Other option
  56. #define OTHER_OPTIONS ""
  57. // </h>
  58. // <h>RTC: for validate certificate date
  59. // <o>Year <1970-2099>
  60. #define RTC_YEAR 2019
  61. // <o>Month <1=>Jan<2=>Feb<3=>Mar<4=>Apr<5=>May<6=>Jun<7=>Jul<8=>Aug<9=>Sep<10=>Oct<11=>Nov<12=>Dec
  62. #define RTC_MONTH 1
  63. // <o>Day <1-31>
  64. #define RTC_DAY 1
  65. // </h>
  66. //------------- <<< end of configuration section >>> -----------------------
  67. #warning "write MPU specific Set ups\n"
  68. static void SystemClock_Config(void)
  69. {
  70. }
  71. static void MPU_Config(void)
  72. {
  73. }
  74. static void CPU_CACHE_Enable(void)
  75. {
  76. }
  77. #if defined(WOLFSSL_CMSIS_RTOS)
  78. extern uint32_t os_time;
  79. #endif
  80. uint32_t HAL_GetTick(void) {
  81. #if defined(WOLFSSL_CMSIS_RTOS)
  82. return os_time;
  83. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  84. return osKernelGetTickCount();
  85. #endif
  86. }
  87. static time_t epochTime;
  88. time_t time(time_t *t){
  89. return epochTime ;
  90. }
  91. void setTime(time_t t){
  92. epochTime = t;
  93. }
  94. double current_time(int reset)
  95. {
  96. if (reset)
  97. return 0;
  98. #if defined(WOLFSSL_CMSIS_RTOS)
  99. return (double)os_time / 1000.0;
  100. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  101. return (double)osKernelGetTickCount() / 1000.0;
  102. #endif
  103. }
  104. /*-----------------------------------------------------------------------------
  105. * Initialize a Flash Memory Card
  106. *----------------------------------------------------------------------------*/
  107. #if !defined(NO_FILESYSTEM)
  108. #include "rl_fs.h" /* FileSystem definitions */
  109. static void init_filesystem (void) {
  110. int32_t retv;
  111. retv = finit ("M0:");
  112. if (retv == fsOK) {
  113. retv = fmount ("M0:");
  114. if (retv == fsOK) {
  115. printf ("Drive M0 ready!\n");
  116. }
  117. else {
  118. printf ("Drive M0 mount failed(%d)!\n", retv);
  119. }
  120. }
  121. else {
  122. printf ("Drive M0 initialization failed!\n");
  123. }
  124. }
  125. #endif
  126. extern void client_test(void const*arg);
  127. #if defined(WOLFSSL_CMSIS_RTOSv2)
  128. void app_main(void *arg)
  129. #else
  130. void app_main(void const*arg)
  131. #endif
  132. {
  133. if(netInitialize () == netOK)
  134. client_test(arg);
  135. else printf("ERROR: netInitialize\n");
  136. }
  137. #if defined(WOLFSSL_CMSIS_RTOS)
  138. osThreadDef(app_main, osPriorityLow, 1, 32*1024);
  139. #endif
  140. /*----------------------------------------------------------------------------
  141. Main Thread 'main': Run Network
  142. *---------------------------------------------------------------------------*/
  143. #include <stdio.h>
  144. typedef struct func_args {
  145. int argc;
  146. char** argv;
  147. } func_args;
  148. int myoptind = 0;
  149. char* myoptarg = NULL;
  150. int main (void) {
  151. static char *argv[] =
  152. { "client", "-h", REMOTE_IP, "-p", REMOTE_PORT,
  153. "-v", " ", OTHER_OPTIONS } ;
  154. static func_args args =
  155. { sizeof(argv)/sizeof(*argv[0]), argv } ;
  156. char *verStr[] = { "SSL3", "TLS1.0", "TLS1.1", "TLS1.2", "TLS1.3"};
  157. #define VERSIZE 2
  158. static char ver[VERSIZE];
  159. MPU_Config(); /* Configure the MPU */
  160. CPU_CACHE_Enable(); /* Enable the CPU Cache */
  161. HAL_Init(); /* Initialize the HAL Library */
  162. SystemClock_Config(); /* Configure the System Clock */
  163. #if defined(WOLFSSL_CMSIS_RTOSv2)
  164. osKernelInitialize();
  165. #endif
  166. #if !defined(NO_FILESYSTEM)
  167. init_filesystem ();
  168. #endif
  169. #if defined(DEBUG_WOLFSSL)
  170. printf("Turning ON Debug message\n") ;
  171. wolfSSL_Debugging_ON() ;
  172. #endif
  173. snprintf(ver, VERSIZE, "%d", TLS_VER);
  174. argv[6] = ver;
  175. printf("SSL/TLS Client(%d)\n ", (int)(sizeof(argv)/sizeof(argv[0]))) ;
  176. printf(" Remote IP: %s, Port: %s\n Version: %s\n", argv[2], argv[4], verStr[TLS_VER]) ;
  177. printf(" Other options: %s\n", OTHER_OPTIONS);
  178. setTime((time_t)((RTC_YEAR-1970)*365*24*60*60) + RTC_MONTH*30*24*60*60 + RTC_DAY*24*60*60);
  179. #if defined(WOLFSSL_CMSIS_RTOS)
  180. osThreadCreate (osThread(app_main), (void *)&args);
  181. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  182. osThreadNew(app_main, (void *)&args, NULL);
  183. #endif
  184. osKernelStart();
  185. }