Skip to content

Commit

Permalink
Merge pull request #507 from lealem47/improvePack
Browse files Browse the repository at this point in the history
CubePack: Better USER_IO support & cleanup warnings
  • Loading branch information
dgarske authored Apr 3, 2023
2 parents d3dfb15 + 29ea1af commit 5b73e3a
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 30 deletions.
16 changes: 12 additions & 4 deletions ide/STM32CUBE/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# wolfSSH for STM32 Cube IDE
# wolfSSH for STM32 Cube IDE

The wolfSSH Cube Pack can be found [here](https://www.wolfssl.com/files/ide/I-CUBE-wolfSSH.pack) and is dependent on the `wolfCrypt` library.

1. The first step is to set up the wolfCrypt library in your ST project following the guide here [https://github.com/wolfSSL/wolfssl/blob/master/IDE/STM32Cube/README.md](https://github.com/wolfSSL/wolfssl/blob/master/IDE/STM32Cube/README.md). To run the wolfSSH unit tests, name the entry function `wolfSSHTest` instead of `wolfCryptDemo`.

2. Then install the wolfSSH Cube Pack in the same manner as the wolfSSL pack with CUBEMX.

3. Open the project `.ioc` file and click the `Software Packs` drop down menu and then `Select Components`. Expand the `wolfSSH` pack and check all the components.
3. Open the project `.ioc` file and click the `Software Packs` drop down menu and then `Select Components`. Expand the `wolfSSH` pack and check all the components.

4. In the `Software Packs` configuration category of the `.ioc` file, click on the wolfSSH pack and enable the library by checking the box.

5. Since LwIP is a dependency for the Cube Pack, enable LwIP in the `Middleware` configuration category of the project. Also enable the `LWIP_DNS` option in the LwIP configuration settings.
5. The Pack defaults to using custom IO provided by the user. Modify `ide/STM32CUBE/userio_template.h` to supply the custom IO. If you'd like to use LwIP instead, configure the wolfSSH IO settings in the `.ioc` to enable LwIP compatibilty. You'll also have to enable LwIP in the `Middleware` configuration category of the project. Enable the `LWIP_DNS` option in the LwIP configuration settings.

6. Save your changes and select yes to the prompt asking about generating code.

Expand All @@ -19,4 +19,12 @@ The wolfSSH Cube Pack can be found [here](https://www.wolfssl.com/files/ide/I-CU
## Notes
- Make sure to make [these changes](https://github.com/wolfSSL/wolfssl/tree/master/IDE/STM32Cube#stm32-printf) to redirect the printf's to the UART.

- If looking to enable filesystem support, the pack assumes the user has defined their own filesystem in `wolfssh/myFilesystem.h`. That file will originally contain a dummy filesystem. If going the FATFS route, make sure to replace `#define WOLFSSH_USER_FILESYSTEM` with `#define WOLFSSH_FATFS` in the `wolfSSL.I-CUBE-wolfSSH_conf.h` header file. The wolfSSL Cube Pack also defaults to disabling filesystem support so make sure to remove `#define NO_FILESYSTEM` from `wolfSSL.I-CUBE-wolfSSL_conf.h`.
- If looking to enable filesystem support (required for SFTP), the pack assumes the user has defined their own filesystem in `wolfssh/myFilesystem.h`. That file will originally contain a dummy filesystem. If going the FATFS route, make sure to replace `#define WOLFSSH_USER_FILESYSTEM` with `#define WOLFSSH_FATFS` in the `wolfSSL.I-CUBE-wolfSSH_conf.h` header file. The wolfSSL Cube Pack also defaults to disabling filesystem support so make sure to remove `#define NO_FILESYSTEM` from `wolfSSL.I-CUBE-wolfSSL_conf.h`.

- If building with LwIP and you encounter the error `multiple definition of 'errno'` in `Middlewares/Third_Party/LwIP/system/OS/sys_arch.c`, modify the file as shown below.
```
#if defined(LWIP_SOCKET_SET_ERRNO) && defined(LWIP_PROVIDE_ERRNO)
- int errno;
+ extern int errno;
#endif
```
10 changes: 7 additions & 3 deletions ide/STM32CUBE/default_conf.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ extern ${variable.value} ${variable.name};
/* ------------------------------------------------------------------------- */
/* wolfSSH IO */
/* ------------------------------------------------------------------------- */
#define WOLFSSH_LWIP
/* Remove the LWIP define and uncomment the line below to use user defined IO */
/* #define WOLFSSL_USER_IO */
#if defined(WOLFSSH_CONF_IO) && WOLFSSH_CONF_IO == 2
#define WOLFSSH_LWIP
#else
#define WOLFSSH_USER_IO
#endif

/* To be defined for the target Socket API */
#define WSTARTTCP()
Expand Down Expand Up @@ -140,6 +142,8 @@ extern ${variable.value} ${variable.name};
#define CURVE25519_SMALL
#define HAVE_ED25519

#define WOLFSSH_IGNORE_FILE_WARN

typedef unsigned int size_t;

/* defines for unit tests */
Expand Down
42 changes: 21 additions & 21 deletions ide/STM32CUBE/myFilesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,107 +40,107 @@ typedef struct { int i; } stat_t;
#define WFD int
enum { O_RDWR, O_RDONLY, O_WRONLY, O_APPEND, O_CREAT, O_TRUNC, O_EXCL } ;

static int WFOPEN(FILE **f, const char *n, const char *m){
static inline int WFOPEN(FILE **f, const char *n, const char *m){
(void) n; (void) m; (void)f;
return NULL;
}

static int WFCLOSE(FILE *f) {
static inline int WFCLOSE(FILE *f) {
(void) f;
return 0;
}

static size_t WFREAD(void *b, size_t s, size_t n, FILE *f) {
static inline size_t WFREAD(void *b, size_t s, size_t n, FILE *f) {
(void) b; (void) s; (void) n; (void) f;
return 0;
}

static size_t WFWRITE(const void *b, size_t s, size_t n, FILE *f) {
static inline size_t WFWRITE(const void *b, size_t s, size_t n, FILE *f) {
(void) b; (void) s; (void) n; (void) f;
return 0;
}

static int WFSEEK(FILE *f, long int p, int m) {
static inline int WFSEEK(FILE *f, long int p, int m) {
(void) f; (void) p; (void) m;
return 0;
}

static long int WFTELL(FILE *f) {
static inline long int WFTELL(FILE *f) {
(void) f;
return 0;
}
static void WREWIND(FILE *f) {
static inline void WREWIND(FILE *f) {
(void) f;
}

static int WOPEN (const char* n, int f, int m) {
static inline int WOPEN (const char* n, int f, int m) {
(void) f; (void) n; (void) m;
return 0;
}

static int WCLOSE(int f) {
static inline int WCLOSE(int f) {
(void) f;
return 0;
}

static size_t WPREAD(int f, void* b, size_t c, off_t *o) {
static inline size_t WPREAD(int f, void* b, size_t c, off_t *o) {
(void) f; (void) b; (void) c; (void)o;
return 0;
}

static size_t WPWRITE(int f, void* b, size_t c, off_t *o) {
static inline size_t WPWRITE(int f, void* b, size_t c, off_t *o) {
(void) f; (void) b; (void) c; (void)o;
return 0;
}

static char *WGETCWD(void *fs, char *f, size_t l){
static inline char *WGETCWD(void *fs, char *f, size_t l){
(void) fs; (void) f; (void) l;
return 0;
}

static int WRMDIR(void *fs, const char *p){
static inline int WRMDIR(void *fs, const char *p){
(void) p;
return 0;
}

static int WMKDIR(void *fs, const char *p, mode_t m) {
static inline int WMKDIR(void *fs, const char *p, mode_t m) {
(void) p; (void) m;
return 0;
}

static int WREMOVE(void *fs, const char *p){
static inline int WREMOVE(void *fs, const char *p){
(void) fs; (void) p;
return 0;
}

static int WRENAME(void *fs, const char *p, const char *np){
static inline int WRENAME(void *fs, const char *p, const char *np){
(void) fs; (void) p; (void)np;
return 0;
}

static int WSTAT(const char *p, stat_t *b) {
static inline int WSTAT(const char *p, stat_t *b) {
(void) p; (void)b;
return 0;
}

static int WLSTAT(const char *p, stat_t *b) {
static inline int WLSTAT(const char *p, stat_t *b) {
(void) p; (void)b;
return 0;
}

static int WCHMOD(void *fs, const char *p, mode_t m) {
static inline int WCHMOD(void *fs, const char *p, mode_t m) {
(void) fs; (void) p; (void)m;
return 0;
}

static int SFTP_GetAttributes(void* fs, const char* fileName,
static inline int SFTP_GetAttributes(void* fs, const char* fileName,
void* atr, byte link, void* heap) {
(void)fs; (void)fileName; (void)atr; (void)link; (void)heap;
return 0;

}

static int SFTP_GetAttributes_Handle(void* ssh, byte* handle, int handleSz,
static inline int SFTP_GetAttributes_Handle(void* ssh, byte* handle, int handleSz,
void* atr) {
(void)ssh; (void)handle; (void)handleSz;

Expand Down
110 changes: 110 additions & 0 deletions ide/STM32CUBE/userio_template.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* userio_template.h
*
* Copyright (C) 2014-2023 wolfSSL Inc.
*
* This file is part of wolfSSH.
*
* wolfSSH 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 3 of the License, or
* (at your option) any later version.
*
* wolfSSH 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 wolfSSH. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef USERIO_TEMPLATE_H
#define USERIO_TEMPLATE_H

#ifdef WOLFSSH_USER_IO

#define SOCK_STREAM 1
#define SOCK_DGRAM 2
#define SOCK_RAW 3

#define SOL_SOCKET 0xfff
#define SO_REUSEADDR 0x0004

#define AF_INET 2
#define INADDR_ANY ((uint32_t)0x00000000UL)

#define socklen_t uint32_t

typedef struct { int s_addr; } in_addr;

struct sockaddr { int i; };

typedef struct sockaddr sockaddr;

struct sockaddr_in{
int sin_len;
int sin_family;
int sin_port;
in_addr sin_addr;
};

typedef struct sockaddr_in sockaddr_in;

struct hostent{
char *h_name;
int h_length;
char **h_addr_list;
};

typedef struct hostent hostent;

static inline int inet_addr(const char* n){
(void) n;
return 0;
}

static inline int htons(unsigned int n){
(void) n;
return 0;
}

static inline int ntohs(unsigned int n){
(void) n;
return 0;
}

static inline int socket(int d, int t, int p) {
(void) d; (void) t; (void) p;
return 0;
}

static inline int setsockopt(int s, int l, int n, const void *o,
socklen_t len) {
(void) s; (void) l; (void) n; (void) o; (void) len;
return 0;
}

static inline int getsockname(int s, struct sockaddr *n, socklen_t* len) {
(void) s; (void) n; (void) len;
return 0;
}

static inline int bind(int s, const struct sockaddr *n, socklen_t l) {
(void) s; (void) n; (void) l;
return 0;
}

static inline int listen(int s, int b) {
(void) s; (void) b;
return 0;
}

static inline struct hostent* gethostbyname(const char* n) {
(void) n;
return NULL;
}

#endif /* WOLFSSH_USER_IO */

#endif
5 changes: 3 additions & 2 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@


/* Check for if compiling misc.c when not needed. */
#if !defined(WOLFSSH_MISC_INCLUDED) && !defined(NO_INLINE)
#if !defined(WOLFSSH_MISC_INCLUDED) && !defined(NO_INLINE) && \
!defined(WOLFSSH_IGNORE_FILE_WARN)
#define MISC_WARNING "misc.c does not need to be compiled when using inline (NO_INLINE not defined))"

#ifndef _MSC_VER
Expand All @@ -60,7 +61,7 @@
#pragma message("warning: " MISC_WARNING)
#endif

#else /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
#else /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE && !WOLFSSH_IGNORE_FILE_WARN */


#ifndef min
Expand Down
10 changes: 10 additions & 0 deletions wolfssh/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@
#endif
#define SOCKET_T int
#define NUM_SOCKETS 5
#elif defined(WOLFSSH_USER_IO)
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include "userio_template.h"
#ifndef SO_NOSIGPIPE
#include <signal.h> /* ignore SIGPIPE */
#endif
#define SOCKET_T int
#define NUM_SOCKETS 5
#else /* USE_WINDOWS_API */
#include <unistd.h>
#include <sys/socket.h>
Expand Down

0 comments on commit 5b73e3a

Please sign in to comment.