FreeRTOS-Plus-TCP fixes

This commit is contained in:
cpq 2021-05-19 00:00:32 +01:00
parent 47e5e984df
commit dbc2356768
4 changed files with 54 additions and 58 deletions

View File

@ -2890,7 +2890,7 @@ static void close_conn(struct mg_connection *c) {
static void setsockopts(struct mg_connection *c) { static void setsockopts(struct mg_connection *c) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP #if MG_ARCH == MG_ARCH_FREERTOS_TCP
FreeRTOS_FD_SET(c->fd, c->mgr->ss, eSELECT_READ | eSELECT_EXCEPT); (void) c;
#else #else
int on = 1; int on = 1;
#if !defined(SOL_TCP) #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); SOCKET fd = accept(FD(lsn), &usa.sa, &sa_len);
if (fd == INVALID_SOCKET) { if (fd == INVALID_SOCKET) {
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO)); 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) { } else if ((long) fd >= FD_SETSIZE) {
LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE)); LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE));
closesocket(fd); closesocket(fd);
@ -3075,7 +3075,8 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP #if MG_ARCH == MG_ARCH_FREERTOS_TCP
struct mg_connection *c; struct mg_connection *c;
for (c = mgr->conns; c != NULL; c = c->next) { 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)) if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0))
FreeRTOS_FD_SET(c->fd, mgr->ss, eSELECT_WRITE); 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); EventBits_t bits = FreeRTOS_FD_ISSET(c->fd, mgr->ss);
c->is_readable = bits & (eSELECT_READ | eSELECT_EXCEPT) ? 1 : 0; c->is_readable = bits & (eSELECT_READ | eSELECT_EXCEPT) ? 1 : 0;
c->is_writable = bits & eSELECT_WRITE ? 1 : 0; c->is_writable = bits & eSELECT_WRITE ? 1 : 0;
FreeRTOS_FD_CLR(c->fd, mgr->ss,
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
} }
#else #else
struct timeval tv = {ms / 1000, (ms % 1000) * 1000}; struct timeval tv = {ms / 1000, (ms % 1000) * 1000};

View File

@ -123,7 +123,6 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
@ -131,11 +130,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <FreeRTOS_IP.h> #include <FreeRTOS_IP.h>
@ -182,29 +177,29 @@ static inline void *mg_calloc(int cnt, size_t size) {
#define free(a) vPortFree(a) #define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a) #define malloc(a) pvPortMalloc(a)
// Again, why not a clean retarget, but instead this.. #define gmtime_r(a, b) gmtime(a)
#ifdef MG_ENABLE_FF
#include <ff_stdio.h>
#undef FILE #if !defined(__GNUC__)
#define FILE FF_FILE // copied from GCC on ARM; for some reason useconds are signed
#define stat(a, b) ff_stat((a), (b)) typedef long suseconds_t;
#define fopen(a, b) ff_fopen((a), (b)) struct timeval {
#define fclose(a) ff_fclose(a) time_t tv_sec;
#define fread(a, b, c, d) ff_fread((a), (b), (c), (d)) suseconds_t tv_usec;
#define fwrite(a, b, c, d) ff_fwrite((a), (b), (c), (d)) };
#define vfprintf ff_vfprintf #endif
#define fprintf ff_fprintf
#define remove(a) ff_remove(a)
#define rename(a, b) ff_rename((a), (b), 1)
static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) { #ifndef EINPROGRESS
char *buf = NULL; #define EINPROGRESS pdFREERTOS_ERRNO_EINPROGRESS
int n = mg_vasprintf(&buf, 0, fmt, ap); #endif
if (buf != NULL) ff_fwrite(buf, 1, n, fp), free(buf); #ifndef EWOULDBLOCK
return n; #define EWOULDBLOCK pdFREERTOS_ERRNO_EWOULDBLOCK
} #endif
#endif // MG_ENABLE_FF #ifndef EAGAIN
#define EAGAIN pdFREERTOS_ERRNO_EAGAIN
#endif
#ifndef EINTR
#define EINTR pdFREERTOS_ERRNO_EINTR
#endif
#endif // MG_ARCH == MG_ARCH_FREERTOS_TCP #endif // MG_ARCH == MG_ARCH_FREERTOS_TCP

View File

@ -4,7 +4,6 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
@ -12,11 +11,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <FreeRTOS_IP.h> #include <FreeRTOS_IP.h>
@ -63,28 +58,28 @@ static inline void *mg_calloc(int cnt, size_t size) {
#define free(a) vPortFree(a) #define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a) #define malloc(a) pvPortMalloc(a)
// Again, why not a clean retarget, but instead this.. #define gmtime_r(a, b) gmtime(a)
#ifdef MG_ENABLE_FF
#include <ff_stdio.h>
#undef FILE #if !defined(__GNUC__)
#define FILE FF_FILE // copied from GCC on ARM; for some reason useconds are signed
#define stat(a, b) ff_stat((a), (b)) typedef long suseconds_t;
#define fopen(a, b) ff_fopen((a), (b)) struct timeval {
#define fclose(a) ff_fclose(a) time_t tv_sec;
#define fread(a, b, c, d) ff_fread((a), (b), (c), (d)) suseconds_t tv_usec;
#define fwrite(a, b, c, d) ff_fwrite((a), (b), (c), (d)) };
#define vfprintf ff_vfprintf #endif
#define fprintf ff_fprintf
#define remove(a) ff_remove(a)
#define rename(a, b) ff_rename((a), (b), 1)
static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) { #ifndef EINPROGRESS
char *buf = NULL; #define EINPROGRESS pdFREERTOS_ERRNO_EINPROGRESS
int n = mg_vasprintf(&buf, 0, fmt, ap); #endif
if (buf != NULL) ff_fwrite(buf, 1, n, fp), free(buf); #ifndef EWOULDBLOCK
return n; #define EWOULDBLOCK pdFREERTOS_ERRNO_EWOULDBLOCK
} #endif
#endif // MG_ENABLE_FF #ifndef EAGAIN
#define EAGAIN pdFREERTOS_ERRNO_EAGAIN
#endif
#ifndef EINTR
#define EINTR pdFREERTOS_ERRNO_EINTR
#endif
#endif // MG_ARCH == MG_ARCH_FREERTOS_TCP #endif // MG_ARCH == MG_ARCH_FREERTOS_TCP

View File

@ -306,7 +306,7 @@ static void close_conn(struct mg_connection *c) {
static void setsockopts(struct mg_connection *c) { static void setsockopts(struct mg_connection *c) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP #if MG_ARCH == MG_ARCH_FREERTOS_TCP
FreeRTOS_FD_SET(c->fd, c->mgr->ss, eSELECT_READ | eSELECT_EXCEPT); (void) c;
#else #else
int on = 1; int on = 1;
#if !defined(SOL_TCP) #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); SOCKET fd = accept(FD(lsn), &usa.sa, &sa_len);
if (fd == INVALID_SOCKET) { if (fd == INVALID_SOCKET) {
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO)); 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) { } else if ((long) fd >= FD_SETSIZE) {
LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE)); LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE));
closesocket(fd); closesocket(fd);
@ -491,7 +491,8 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP #if MG_ARCH == MG_ARCH_FREERTOS_TCP
struct mg_connection *c; struct mg_connection *c;
for (c = mgr->conns; c != NULL; c = c->next) { 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)) if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0))
FreeRTOS_FD_SET(c->fd, mgr->ss, eSELECT_WRITE); 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); EventBits_t bits = FreeRTOS_FD_ISSET(c->fd, mgr->ss);
c->is_readable = bits & (eSELECT_READ | eSELECT_EXCEPT) ? 1 : 0; c->is_readable = bits & (eSELECT_READ | eSELECT_EXCEPT) ? 1 : 0;
c->is_writable = bits & eSELECT_WRITE ? 1 : 0; c->is_writable = bits & eSELECT_WRITE ? 1 : 0;
FreeRTOS_FD_CLR(c->fd, mgr->ss,
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
} }
#else #else
struct timeval tv = {ms / 1000, (ms % 1000) * 1000}; struct timeval tv = {ms / 1000, (ms % 1000) * 1000};