123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /* wolfssl_KEIL_RL.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
- /***************************************************************************************/
- /** This file is for defining functions for specific to KEIL-RL. **/
- /***************************************************************************************/
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <wolfssl/wolfcrypt/settings.h>
- #if defined(WOLFSSL_MDK_ARM)
- #include <stdio.h>
- #include <string.h>
- #if defined(WOLFSSL_MDK5)
- #include "cmsis_os.h"
- #include "rl_fs.h"
- #include "rl_net.h"
- #else
- #include "rtl.h"
- #endif
- #include "wolfssl_MDK_ARM.h"
- #endif
- #include "wolfssl_MDK_ARM.h"
- #include <wolfssl/wolfcrypt/visibility.h>
- #include <wolfssl/wolfcrypt/logging.h>
- #if defined (WOLFSSL_CMSIS_RTOS)
- #define os_dly_wait(t) osDelay(10*t)
- #endif
- /** KEIL-RL TCPnet ****/
- /** TCPnet BSD socket does not have following functions. **/
- #if defined(WOLFSSL_KEIL_TCP_NET)
- char *inet_ntoa(struct in_addr in)
- {
- #define NAMESIZE 16
- static char name[NAMESIZE] ;
- 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) ;
- return name ;
- }
- unsigned long inet_addr(const char *cp)
- {
- unsigned int a[4] ; unsigned long ret ;
- sscanf(cp, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]) ;
- ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
- return(ret) ;
- }
- /*** tcp_connect is actually associated with following syassl_tcp_connect. ***/
- int wolfssl_connect(int sd, const struct sockaddr* sa, int sz)
- {
- int ret = 0 ;
- #if defined(WOLFSSL_KEIL_TCP_NET)
- SOCKADDR_IN addr ;
- addr = *(SOCKADDR_IN *)sa ;
- do {
- #undef connect /* Go to KEIL TCPnet connect */
- ret = connect(sd, (SOCKADDR *)&addr, sizeof(addr)) ;
- os_dly_wait(50);
- } while(ret == SCK_EWOULDBLOCK) ;
- #ifdef DEBUG_WOLFSSL
- {
- char msg[50] ;
- sprintf(msg, "BSD Connect return code: %d\n", ret) ;
- WOLFSSL_MSG(msg) ;
- }
- #endif
- #endif /* WOLFSSL_KEIL_TCP_NET */
- return(ret ) ;
- }
- int wolfssl_accept(int sd, struct sockaddr *addr, int *addrlen)
- {
- int ret = 0 ;
- #if defined(WOLFSSL_KEIL_TCP_NET)
- while(1) {
- #undef accept /* Go to KEIL TCPnet accept */
- ret = accept(sd, addr, addrlen) ;
- if(ret != SCK_EWOULDBLOCK) break ;
- os_dly_wait(1);
- }
- #ifdef DEBUG_WOLFSSL
- {
- char msg[50] ;
- sprintf(msg, "BSD Accept return code: %d\n", ret) ;
- WOLFSSL_MSG(msg) ;
- }
- #endif
- #endif /* WOLFSSL_KEIL_TCP_NET */
- return(ret ) ;
- }
- int wolfssl_recv(int sd, void *buf, size_t len, int flags)
- {
- int ret = 0;
- #if defined(WOLFSSL_KEIL_TCP_NET)
- while(1) {
- #undef recv /* Go to KEIL TCPnet recv */
- ret = recv(sd, buf, len, flags) ;
- if((ret != SCK_EWOULDBLOCK) &&( ret != SCK_ETIMEOUT)) break ;
- os_dly_wait(1);
- }
- #ifdef DEBUG_WOLFSSL
- {
- char msg[50] ;
- sprintf(msg, "BSD Recv return code: %d\n", ret) ;
- WOLFSSL_MSG(msg) ;
- }
- #endif
- #endif /* WOLFSSL_KEIL_TCP_NET */
- return(ret ) ;
- }
- int wolfssl_send(int sd, const void *buf, size_t len, int flags)
- {
- int ret = 0 ;
- #if defined(WOLFSSL_KEIL_TCP_NET)
- while(1) {
- #undef send /* Go to KEIL TCPnet send */
- ret = send(sd, buf, len, flags) ;
- if(ret != SCK_EWOULDBLOCK) break ;
- os_dly_wait(1);
- }
- #ifdef DEBUG_WOLFSSL
- {
- char msg[50] ;
- sprintf(msg, "BSD Send return code: %d\n", ret) ;
- WOLFSSL_MSG(msg) ;
- }
- #endif
- #endif /* WOLFSSL_KEIL_TCP_NET */
- return(ret) ;
- }
- #endif /* WOLFSSL_KEIL_TCP_NET */
- #if defined(WOLFSSL_KEIL_TCP_NET)
- void wolfssl_sleep(int t)
- {
- #if defined(HAVE_KEIL_RTX)
- os_dly_wait(t/1000+1) ;
- #endif
- }
- int wolfssl_tcp_select(int sd, int timeout)
- {
- return 0 ;
- }
- #endif
- FILE * wolfSSL_fopen(const char *name, const char *openmode)
- {
- int i ; FILE * ret ;
- #define PATHSIZE 100
- char path[PATHSIZE] ; char *p ;
- if(strlen(name) > PATHSIZE)return(NULL) ;
- for(i = 0; i<= strlen(name); i++) {
- if(name[i] == '/')path[i] = '\\' ;
- else path[i] = name[i] ;
- }
- if(path[0] == '.' && path[1] == '\\') p = path + 2 ;
- else p = path ;
- ret = fopen (p, openmode) ;
- return(ret) ;
- }
- #define getkey getchar
- #define sendchar putchar
- char * wolfssl_fgets ( char * str, int num, FILE * f )
- {
- int i ;
- for(i = 0 ; i< num ; i++) {
- while((str[i] = getkey()) == 0) {
- #if defined (HAVE_KEIL_RTX) && !defined(WOLFSSL_CMSIS_RTOS)
- os_tsk_pass ();
- #elif defined(WOLFSSL_CMSIS_RTOS)
- osThreadYield ();
- #endif
- }
- if(str[i] == '\n' || str[i] == '\012' || str[i] == '\015') {
- sendchar('\n') ;
- str[i++] = '\n' ;
- str[i] = '\0' ;
- break ;
- } else if(str[i] == '\010') { /* BS */
- if(i) { /* erace one char */
- sendchar('\010') ; sendchar(' ') ; sendchar('\010') ;
- i = (i>0 ? (i-2) : -1 ) ;
- continue ;
- }
- } else if(str[i] == '\033' || str[i] == '\004' ) { /* ESC or ^D */
- str[i] = '\0' ;
- return(0) ;
- }
- sendchar(str[i]) ;
- }
- return(str) ;
- }
|