From dbc2356768c035d5c34a4fa6f55e8dc6ac3d6294 Mon Sep 17 00:00:00 2001 From: cpq Date: Wed, 19 May 2021 00:00:32 +0100 Subject: [PATCH] FreeRTOS-Plus-TCP fixes --- mongoose.c | 9 +++++--- mongoose.h | 47 ++++++++++++++++++----------------------- src/arch_freertos_tcp.h | 47 ++++++++++++++++++----------------------- src/sock.c | 9 +++++--- 4 files changed, 54 insertions(+), 58 deletions(-) diff --git a/mongoose.c b/mongoose.c index 5263efcc..d7c79f99 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2890,7 +2890,7 @@ static void close_conn(struct mg_connection *c) { static void setsockopts(struct mg_connection *c) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP - FreeRTOS_FD_SET(c->fd, c->mgr->ss, eSELECT_READ | eSELECT_EXCEPT); + (void) c; #else int on = 1; #if !defined(SOL_TCP) @@ -2974,7 +2974,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { SOCKET fd = accept(FD(lsn), &usa.sa, &sa_len); if (fd == INVALID_SOCKET) { LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO)); -#if !defined(_WIN32) +#if (!defined(_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP)) } else if ((long) fd >= FD_SETSIZE) { LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE)); closesocket(fd); @@ -3075,7 +3075,8 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP struct mg_connection *c; for (c = mgr->conns; c != NULL; c = c->next) { - FreeRTOS_FD_CLR(c->fd, mgr->ss, eSELECT_WRITE); + if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) continue; + FreeRTOS_FD_SET(c->fd, mgr->ss, eSELECT_READ | eSELECT_EXCEPT); if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0)) FreeRTOS_FD_SET(c->fd, mgr->ss, eSELECT_WRITE); } @@ -3084,6 +3085,8 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { EventBits_t bits = FreeRTOS_FD_ISSET(c->fd, mgr->ss); c->is_readable = bits & (eSELECT_READ | eSELECT_EXCEPT) ? 1 : 0; c->is_writable = bits & eSELECT_WRITE ? 1 : 0; + FreeRTOS_FD_CLR(c->fd, mgr->ss, + eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE); } #else struct timeval tv = {ms / 1000, (ms % 1000) * 1000}; diff --git a/mongoose.h b/mongoose.h index 72c0d38a..9d56f03f 100644 --- a/mongoose.h +++ b/mongoose.h @@ -123,7 +123,6 @@ #include #include -#include #include #include #include @@ -131,11 +130,7 @@ #include #include #include -#include -#include -#include #include -#include #include #include @@ -182,29 +177,29 @@ static inline void *mg_calloc(int cnt, size_t size) { #define free(a) vPortFree(a) #define malloc(a) pvPortMalloc(a) -// Again, why not a clean retarget, but instead this.. -#ifdef MG_ENABLE_FF -#include +#define gmtime_r(a, b) gmtime(a) -#undef FILE -#define FILE FF_FILE -#define stat(a, b) ff_stat((a), (b)) -#define fopen(a, b) ff_fopen((a), (b)) -#define fclose(a) ff_fclose(a) -#define fread(a, b, c, d) ff_fread((a), (b), (c), (d)) -#define fwrite(a, b, c, d) ff_fwrite((a), (b), (c), (d)) -#define vfprintf ff_vfprintf -#define fprintf ff_fprintf -#define remove(a) ff_remove(a) -#define rename(a, b) ff_rename((a), (b), 1) +#if !defined(__GNUC__) +// copied from GCC on ARM; for some reason useconds are signed +typedef long suseconds_t; +struct timeval { + time_t tv_sec; + suseconds_t tv_usec; +}; +#endif -static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) { - char *buf = NULL; - int n = mg_vasprintf(&buf, 0, fmt, ap); - if (buf != NULL) ff_fwrite(buf, 1, n, fp), free(buf); - return n; -} -#endif // MG_ENABLE_FF +#ifndef EINPROGRESS +#define EINPROGRESS pdFREERTOS_ERRNO_EINPROGRESS +#endif +#ifndef EWOULDBLOCK +#define EWOULDBLOCK pdFREERTOS_ERRNO_EWOULDBLOCK +#endif +#ifndef EAGAIN +#define EAGAIN pdFREERTOS_ERRNO_EAGAIN +#endif +#ifndef EINTR +#define EINTR pdFREERTOS_ERRNO_EINTR +#endif #endif // MG_ARCH == MG_ARCH_FREERTOS_TCP diff --git a/src/arch_freertos_tcp.h b/src/arch_freertos_tcp.h index 12f1fd3e..c6ab15e0 100644 --- a/src/arch_freertos_tcp.h +++ b/src/arch_freertos_tcp.h @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -12,11 +11,7 @@ #include #include #include -#include -#include -#include #include -#include #include #include @@ -63,28 +58,28 @@ static inline void *mg_calloc(int cnt, size_t size) { #define free(a) vPortFree(a) #define malloc(a) pvPortMalloc(a) -// Again, why not a clean retarget, but instead this.. -#ifdef MG_ENABLE_FF -#include +#define gmtime_r(a, b) gmtime(a) -#undef FILE -#define FILE FF_FILE -#define stat(a, b) ff_stat((a), (b)) -#define fopen(a, b) ff_fopen((a), (b)) -#define fclose(a) ff_fclose(a) -#define fread(a, b, c, d) ff_fread((a), (b), (c), (d)) -#define fwrite(a, b, c, d) ff_fwrite((a), (b), (c), (d)) -#define vfprintf ff_vfprintf -#define fprintf ff_fprintf -#define remove(a) ff_remove(a) -#define rename(a, b) ff_rename((a), (b), 1) +#if !defined(__GNUC__) +// copied from GCC on ARM; for some reason useconds are signed +typedef long suseconds_t; +struct timeval { + time_t tv_sec; + suseconds_t tv_usec; +}; +#endif -static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) { - char *buf = NULL; - int n = mg_vasprintf(&buf, 0, fmt, ap); - if (buf != NULL) ff_fwrite(buf, 1, n, fp), free(buf); - return n; -} -#endif // MG_ENABLE_FF +#ifndef EINPROGRESS +#define EINPROGRESS pdFREERTOS_ERRNO_EINPROGRESS +#endif +#ifndef EWOULDBLOCK +#define EWOULDBLOCK pdFREERTOS_ERRNO_EWOULDBLOCK +#endif +#ifndef EAGAIN +#define EAGAIN pdFREERTOS_ERRNO_EAGAIN +#endif +#ifndef EINTR +#define EINTR pdFREERTOS_ERRNO_EINTR +#endif #endif // MG_ARCH == MG_ARCH_FREERTOS_TCP diff --git a/src/sock.c b/src/sock.c index 5491b728..a0cae704 100644 --- a/src/sock.c +++ b/src/sock.c @@ -306,7 +306,7 @@ static void close_conn(struct mg_connection *c) { static void setsockopts(struct mg_connection *c) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP - FreeRTOS_FD_SET(c->fd, c->mgr->ss, eSELECT_READ | eSELECT_EXCEPT); + (void) c; #else int on = 1; #if !defined(SOL_TCP) @@ -390,7 +390,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { SOCKET fd = accept(FD(lsn), &usa.sa, &sa_len); if (fd == INVALID_SOCKET) { LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO)); -#if !defined(_WIN32) +#if (!defined(_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP)) } else if ((long) fd >= FD_SETSIZE) { LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE)); closesocket(fd); @@ -491,7 +491,8 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP struct mg_connection *c; for (c = mgr->conns; c != NULL; c = c->next) { - FreeRTOS_FD_CLR(c->fd, mgr->ss, eSELECT_WRITE); + if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) continue; + FreeRTOS_FD_SET(c->fd, mgr->ss, eSELECT_READ | eSELECT_EXCEPT); if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0)) FreeRTOS_FD_SET(c->fd, mgr->ss, eSELECT_WRITE); } @@ -500,6 +501,8 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { EventBits_t bits = FreeRTOS_FD_ISSET(c->fd, mgr->ss); c->is_readable = bits & (eSELECT_READ | eSELECT_EXCEPT) ? 1 : 0; c->is_writable = bits & eSELECT_WRITE ? 1 : 0; + FreeRTOS_FD_CLR(c->fd, mgr->ss, + eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE); } #else struct timeval tv = {ms / 1000, (ms % 1000) * 1000};