wolfssl_MDK_ARM.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* wolfssl_KEIL_RL.c
  2. *
  3. * Copyright (C) 2006-2020 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. /***************************************************************************************/
  22. /** This file is for defining functions for specific to KEIL-RL. **/
  23. /***************************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #if defined(WOLFSSL_MDK_ARM)
  29. #include <stdio.h>
  30. #include <string.h>
  31. #if defined(WOLFSSL_MDK5)
  32. #include "cmsis_os.h"
  33. #include "rl_fs.h"
  34. #include "rl_net.h"
  35. #else
  36. #include "rtl.h"
  37. #endif
  38. #include "wolfssl_MDK_ARM.h"
  39. #endif
  40. #include "wolfssl_MDK_ARM.h"
  41. #include <wolfssl/wolfcrypt/visibility.h>
  42. #include <wolfssl/wolfcrypt/logging.h>
  43. #if defined (WOLFSSL_CMSIS_RTOS)
  44. #define os_dly_wait(t) osDelay(10*t)
  45. #endif
  46. /** KEIL-RL TCPnet ****/
  47. /** TCPnet BSD socket does not have following functions. **/
  48. #if defined(WOLFSSL_KEIL_TCP_NET)
  49. char *inet_ntoa(struct in_addr in)
  50. {
  51. #define NAMESIZE 16
  52. static char name[NAMESIZE] ;
  53. sprintf(name, "%d.%d.%d.%d", (in.s_addr>>24)&0xff, (in.s_addr>>16)&0xff, (in.s_addr>>8)&0xff, in.s_addr&0xff) ;
  54. return name ;
  55. }
  56. unsigned long inet_addr(const char *cp)
  57. {
  58. unsigned int a[4] ; unsigned long ret ;
  59. sscanf(cp, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]) ;
  60. ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
  61. return(ret) ;
  62. }
  63. /*** tcp_connect is actually associated with following syassl_tcp_connect. ***/
  64. int wolfssl_connect(int sd, const struct sockaddr* sa, int sz)
  65. {
  66. int ret = 0 ;
  67. #if defined(WOLFSSL_KEIL_TCP_NET)
  68. SOCKADDR_IN addr ;
  69. addr = *(SOCKADDR_IN *)sa ;
  70. do {
  71. #undef connect /* Go to KEIL TCPnet connect */
  72. ret = connect(sd, (SOCKADDR *)&addr, sizeof(addr)) ;
  73. os_dly_wait(50);
  74. } while(ret == SCK_EWOULDBLOCK) ;
  75. #ifdef DEBUG_WOLFSSL
  76. {
  77. char msg[50] ;
  78. sprintf(msg, "BSD Connect return code: %d\n", ret) ;
  79. WOLFSSL_MSG(msg) ;
  80. }
  81. #endif
  82. #endif /* WOLFSSL_KEIL_TCP_NET */
  83. return(ret ) ;
  84. }
  85. int wolfssl_accept(int sd, struct sockaddr *addr, int *addrlen)
  86. {
  87. int ret = 0 ;
  88. #if defined(WOLFSSL_KEIL_TCP_NET)
  89. while(1) {
  90. #undef accept /* Go to KEIL TCPnet accept */
  91. ret = accept(sd, addr, addrlen) ;
  92. if(ret != SCK_EWOULDBLOCK) break ;
  93. os_dly_wait(1);
  94. }
  95. #ifdef DEBUG_WOLFSSL
  96. {
  97. char msg[50] ;
  98. sprintf(msg, "BSD Accept return code: %d\n", ret) ;
  99. WOLFSSL_MSG(msg) ;
  100. }
  101. #endif
  102. #endif /* WOLFSSL_KEIL_TCP_NET */
  103. return(ret ) ;
  104. }
  105. int wolfssl_recv(int sd, void *buf, size_t len, int flags)
  106. {
  107. int ret = 0;
  108. #if defined(WOLFSSL_KEIL_TCP_NET)
  109. while(1) {
  110. #undef recv /* Go to KEIL TCPnet recv */
  111. ret = recv(sd, buf, len, flags) ;
  112. if((ret != SCK_EWOULDBLOCK) &&( ret != SCK_ETIMEOUT)) break ;
  113. os_dly_wait(1);
  114. }
  115. #ifdef DEBUG_WOLFSSL
  116. {
  117. char msg[50] ;
  118. sprintf(msg, "BSD Recv return code: %d\n", ret) ;
  119. WOLFSSL_MSG(msg) ;
  120. }
  121. #endif
  122. #endif /* WOLFSSL_KEIL_TCP_NET */
  123. return(ret ) ;
  124. }
  125. int wolfssl_send(int sd, const void *buf, size_t len, int flags)
  126. {
  127. int ret = 0 ;
  128. #if defined(WOLFSSL_KEIL_TCP_NET)
  129. while(1) {
  130. #undef send /* Go to KEIL TCPnet send */
  131. ret = send(sd, buf, len, flags) ;
  132. if(ret != SCK_EWOULDBLOCK) break ;
  133. os_dly_wait(1);
  134. }
  135. #ifdef DEBUG_WOLFSSL
  136. {
  137. char msg[50] ;
  138. sprintf(msg, "BSD Send return code: %d\n", ret) ;
  139. WOLFSSL_MSG(msg) ;
  140. }
  141. #endif
  142. #endif /* WOLFSSL_KEIL_TCP_NET */
  143. return(ret) ;
  144. }
  145. #endif /* WOLFSSL_KEIL_TCP_NET */
  146. #if defined(WOLFSSL_KEIL_TCP_NET)
  147. void wolfssl_sleep(int t)
  148. {
  149. #if defined(HAVE_KEIL_RTX)
  150. os_dly_wait(t/1000+1) ;
  151. #endif
  152. }
  153. int wolfssl_tcp_select(int sd, int timeout)
  154. {
  155. return 0 ;
  156. }
  157. #endif
  158. FILE * wolfSSL_fopen(const char *name, const char *openmode)
  159. {
  160. int i ; FILE * ret ;
  161. #define PATHSIZE 100
  162. char path[PATHSIZE] ; char *p ;
  163. if(strlen(name) > PATHSIZE)return(NULL) ;
  164. for(i = 0; i<= strlen(name); i++) {
  165. if(name[i] == '/')path[i] = '\\' ;
  166. else path[i] = name[i] ;
  167. }
  168. if(path[0] == '.' && path[1] == '\\') p = path + 2 ;
  169. else p = path ;
  170. ret = fopen (p, openmode) ;
  171. return(ret) ;
  172. }
  173. #define getkey getchar
  174. #define sendchar putchar
  175. char * wolfssl_fgets ( char * str, int num, FILE * f )
  176. {
  177. int i ;
  178. for(i = 0 ; i< num ; i++) {
  179. while((str[i] = getkey()) == 0) {
  180. #if defined (HAVE_KEIL_RTX) && !defined(WOLFSSL_CMSIS_RTOS)
  181. os_tsk_pass ();
  182. #elif defined(WOLFSSL_CMSIS_RTOS)
  183. osThreadYield ();
  184. #endif
  185. }
  186. if(str[i] == '\n' || str[i] == '\012' || str[i] == '\015') {
  187. sendchar('\n') ;
  188. str[i++] = '\n' ;
  189. str[i] = '\0' ;
  190. break ;
  191. } else if(str[i] == '\010') { /* BS */
  192. if(i) { /* erace one char */
  193. sendchar('\010') ; sendchar(' ') ; sendchar('\010') ;
  194. i = (i>0 ? (i-2) : -1 ) ;
  195. continue ;
  196. }
  197. } else if(str[i] == '\033' || str[i] == '\004' ) { /* ESC or ^D */
  198. str[i] = '\0' ;
  199. return(0) ;
  200. }
  201. sendchar(str[i]) ;
  202. }
  203. return(str) ;
  204. }