main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* main.c
  2. *
  3. * Copyright (C) 2006-2023 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 "cmsis_os.h" /* CMSIS RTOS definitions */
  26. #include "rl_net.h" /* Network definitions */
  27. #include <time.h>
  28. #if defined(STM32F7xx)
  29. #include "stm32f7xx_hal.h"
  30. #elif defined(STM32F4xx)
  31. #include "stm32f4xx_hal.h"
  32. #elif defined(STM32F2xx)
  33. #include "stm32f2xx_hal.h"
  34. #endif
  35. //-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
  36. // <h>Server parameter
  37. // ====================
  38. // <s.6>Port
  39. // <i> Default: "11111"
  40. #define SERVER_PORT "11111"
  41. // </h>
  42. // <h>Protocol
  43. // ====================
  44. // <o>SSL/TLS Version<0=> SSL3 <1=> TLS1.0 <2=> TLS1.1 <3=> TLS1.2 <4=> TLS1.3
  45. #define TLS_VER 3
  46. // <s.2>Other option
  47. #define OTHER_OPTIONS ""
  48. // </h>
  49. // <h>RTC: for validate certificate date
  50. // <o>Year <1970-2099>
  51. #define RTC_YEAR 2018
  52. // <o>Month <1=>Jan<2=>Feb<3=>Mar<4=>Apr<5=>May<6=>Jun<7=>Jul<8=>Aut<9=>Sep<10=>Oct<11=>Nov<12=>Dec
  53. #define RTC_MONTH 1
  54. // <o>Day <1-31>
  55. #define RTC_DAY 1
  56. // </h>
  57. //------------- <<< end of configuration section >>> -----------------------
  58. #warning "write MPU specific Set ups\n"
  59. static void SystemClock_Config (void) {
  60. }
  61. static void MPU_Config (void) {
  62. }
  63. static void CPU_CACHE_Enable (void) {
  64. }
  65. /*-----------------------------------------------------------------------------
  66. * Initialize a Flash Memory Card
  67. *----------------------------------------------------------------------------*/
  68. #if !defined(NO_FILESYSTEM)
  69. #include "rl_fs.h" /* FileSystem definitions */
  70. static void init_filesystem (void) {
  71. int32_t retv;
  72. retv = finit ("M0:");
  73. if (retv == fsOK) {
  74. retv = fmount ("M0:");
  75. if (retv == fsOK) {
  76. printf ("Drive M0 ready!\n");
  77. }
  78. else {
  79. printf ("Drive M0 mount failed(%d)!\n", retv);
  80. }
  81. }
  82. else {
  83. printf ("Drive M0 initialization failed!\n");
  84. }
  85. }
  86. #endif
  87. void net_loop(void const *arg)
  88. {
  89. while(1) {
  90. net_main ();
  91. osThreadYield ();
  92. }
  93. }
  94. osThreadDef(net_loop, osPriorityLow, 2, 0);
  95. #ifdef RTE_CMSIS_RTOS_RTX
  96. extern uint32_t os_time;
  97. static time_t epochTime;
  98. uint32_t HAL_GetTick(void) {
  99. return os_time;
  100. }
  101. time_t time(time_t *t){
  102. return epochTime ;
  103. }
  104. void setTime(time_t t){
  105. epochTime = t;
  106. }
  107. #endif
  108. #ifdef WOLFSSL_CURRTIME_OSTICK
  109. #include <stdint.h>
  110. extern uint32_t os_time;
  111. double current_time(int reset)
  112. {
  113. if(reset) os_time = 0 ;
  114. return (double)os_time /1000.0;
  115. }
  116. #else
  117. #include <stdint.h>
  118. #define DWT ((DWT_Type *) (0xE0001000UL) )
  119. typedef struct
  120. {
  121. uint32_t CTRL; /*!< Offset: 0x000 (R/W) Control Register */
  122. uint32_t CYCCNT; /*!< Offset: 0x004 (R/W) Cycle Count Register */
  123. } DWT_Type;
  124. extern uint32_t SystemCoreClock ;
  125. double current_time(int reset)
  126. {
  127. if(reset) DWT->CYCCNT = 0 ;
  128. return ((double)DWT->CYCCNT/SystemCoreClock) ;
  129. }
  130. #endif
  131. /*----------------------------------------------------------------------------
  132. Main Thread 'main': Run Network
  133. *---------------------------------------------------------------------------*/
  134. #include <stdio.h>
  135. typedef struct func_args {
  136. int argc;
  137. char** argv;
  138. } func_args;
  139. extern void echoserver_test(func_args * args) ;
  140. int myoptind = 0;
  141. char* myoptarg = NULL;
  142. int main (void) {
  143. static char *argv[] =
  144. { "server" } ;
  145. static func_args args = { 1, argv } ;
  146. MPU_Config(); /* Configure the MPU */
  147. CPU_CACHE_Enable(); /* Enable the CPU Cache */
  148. HAL_Init(); /* Initialize the HAL Library */
  149. SystemClock_Config(); /* Configure the System Clock */
  150. #if !defined(NO_FILESYSTEM)
  151. init_filesystem ();
  152. #endif
  153. net_initialize ();
  154. #if defined(DEBUG_WOLFSSL)
  155. printf("Turning ON Debug message\n") ;
  156. wolfSSL_Debugging_ON() ;
  157. #endif
  158. setTime((RTC_YEAR-1970)*365*24*60*60 + RTC_MONTH*30*24*60*60 + RTC_DAY*24*60*60);
  159. osThreadCreate (osThread(net_loop), NULL);
  160. echoserver_test(&args) ;
  161. printf("echoserver: Terminated\n") ;
  162. while(1)
  163. osDelay(1000);
  164. }