From ebdd2f983bec275058addf7fed46bbbbd0c4dc50 Mon Sep 17 00:00:00 2001 From: Our Air Quality Date: Thu, 19 Oct 2017 03:33:32 +1000 Subject: [PATCH] lwip: define LWIP_POSIX_SOCKETS_IO_NAMES as zero (#457) This patch gets the newlib standard stream descriptors playing well with the lwip socket descriptors and the spiffs file descriptors. The LWIP_SOCKET_OFFSET is now defined to be 3, rather than zero, to avoid clashing with the standard stream descriptors, and the SPIFFS_FILEHDL_OFFSET is bumped up to start after the lwip descriptors. --- core/newlib_syscalls.c | 59 ++++++++++++++++--- examples/http_get/http_get.c | 1 + examples/http_get_bearssl/http_get_bearssl.c | 1 + .../tls_server_bearssl/tls_server_bearssl.c | 1 + extras/mbedtls/net_lwip.c | 1 + extras/paho_mqtt_c/MQTTESP8266.c | 1 + extras/spiffs/esp_spiffs.c | 3 +- extras/spiffs/spiffs_config.h | 10 +++- extras/wificfg/wificfg.c | 7 ++- lwip/include/arch/cc.h | 1 + lwip/include/lwipopts.h | 28 +++++++++ lwip/lwip | 2 +- 12 files changed, 102 insertions(+), 13 deletions(-) diff --git a/core/newlib_syscalls.c b/core/newlib_syscalls.c index d095ba9..610b570 100644 --- a/core/newlib_syscalls.c +++ b/core/newlib_syscalls.c @@ -14,6 +14,21 @@ #include #include #include +#include + +/* + * The file descriptor index space is allocated in blocks. The first block of 3 + * is for newlib I/O the stdin stdout and stderr. The next block of + * MEMP_NUM_NETCONN is allocated for lwip sockets, and the remainer to file + * system descriptors. The newlib default FD_SETSIZE is 64. + */ +#if LWIP_SOCKET_OFFSET < 3 +#error Expecting a LWIP_SOCKET_OFFSET >= 3, to allow room for the standard I/O descriptors. +#endif +#define FILE_DESCRIPTOR_OFFSET (LWIP_SOCKET_OFFSET + MEMP_NUM_NETCONN) +#if FILE_DESCRIPTOR_OFFSET > FD_SETSIZE +#error Too many lwip sockets for the FD_SETSIZE. +#endif extern void *xPortSupervisorStackPointer; @@ -81,10 +96,17 @@ __attribute__((weak)) long _write_filesystem_r(struct _reent *r, int fd, const c __attribute__((weak)) long _write_r(struct _reent *r, int fd, const char *ptr, int len ) { - if(fd != r->_stdout->_file) { + if (fd >= FILE_DESCRIPTOR_OFFSET) { return _write_filesystem_r(r, fd, ptr, len); } - return current_stdout_write_r(r, fd, ptr, len); + if (fd >= LWIP_SOCKET_OFFSET) { + return lwip_write(fd, ptr, len); + } + if (fd == r->_stdout->_file) { + current_stdout_write_r(r, fd, ptr, len); + } + r->_errno = EBADF; + return -1; } /* syscall implementation for stdio read from UART */ @@ -109,10 +131,36 @@ __attribute__((weak)) long _read_filesystem_r( struct _reent *r, int fd, char *p __attribute__((weak)) long _read_r( struct _reent *r, int fd, char *ptr, int len ) { - if(fd != r->_stdin->_file) { + if (fd >= FILE_DESCRIPTOR_OFFSET) { return _read_filesystem_r(r, fd, ptr, len); } - return _read_stdin_r(r, fd, ptr, len); + if (fd >= LWIP_SOCKET_OFFSET) { + return lwip_read(fd, ptr, len); + } + if (fd == r->_stdin->_file) { + _read_stdin_r(r, fd, ptr, len); + } + r->_errno = EBADF; + return -1; +} + +/* default implementation, replace in a filesystem */ +__attribute__((weak)) int _close_filesystem_r(struct _reent *r, int fd) +{ + r->_errno = EBADF; + return -1; +} + +__attribute__((weak)) int _close_r(struct _reent *r, int fd) +{ + if (fd >= FILE_DESCRIPTOR_OFFSET) { + return _close_filesystem_r(r, fd); + } + if (fd >= LWIP_SOCKET_OFFSET) { + return lwip_close(fd); + } + r->_errno = EBADF; + return -1; } /* Stub syscall implementations follow, to allow compiling newlib functions that @@ -121,9 +169,6 @@ __attribute__((weak)) long _read_r( struct _reent *r, int fd, char *ptr, int len __attribute__((weak, alias("syscall_returns_enosys"))) int _open_r(struct _reent *r, const char *pathname, int flags, int mode); -__attribute__((weak, alias("syscall_returns_enosys"))) -int _close_r(struct _reent *r, int fd); - __attribute__((weak, alias("syscall_returns_enosys"))) int _unlink_r(struct _reent *r, const char *path); diff --git a/examples/http_get/http_get.c b/examples/http_get/http_get.c index 766dd81..7b6fad6 100644 --- a/examples/http_get/http_get.c +++ b/examples/http_get/http_get.c @@ -7,6 +7,7 @@ #include "espressif/esp_common.h" #include "esp/uart.h" +#include #include #include "FreeRTOS.h" diff --git a/examples/http_get_bearssl/http_get_bearssl.c b/examples/http_get_bearssl/http_get_bearssl.c index 593b423..3fe123c 100644 --- a/examples/http_get_bearssl/http_get_bearssl.c +++ b/examples/http_get_bearssl/http_get_bearssl.c @@ -13,6 +13,7 @@ #include "esp/uart.h" #include "esp/hwrand.h" +#include #include #include "FreeRTOS.h" diff --git a/examples/tls_server_bearssl/tls_server_bearssl.c b/examples/tls_server_bearssl/tls_server_bearssl.c index b1e48dc..79e231d 100644 --- a/examples/tls_server_bearssl/tls_server_bearssl.c +++ b/examples/tls_server_bearssl/tls_server_bearssl.c @@ -19,6 +19,7 @@ #include "esp/uart.h" #include "esp/hwrand.h" +#include #include #include "FreeRTOS.h" diff --git a/extras/mbedtls/net_lwip.c b/extras/mbedtls/net_lwip.c index 67453c8..3101380 100644 --- a/extras/mbedtls/net_lwip.c +++ b/extras/mbedtls/net_lwip.c @@ -21,6 +21,7 @@ * This file is part of mbed TLS (https://tls.mbed.org) */ +#include #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else diff --git a/extras/paho_mqtt_c/MQTTESP8266.c b/extras/paho_mqtt_c/MQTTESP8266.c index a6f1e75..23c6f01 100644 --- a/extras/paho_mqtt_c/MQTTESP8266.c +++ b/extras/paho_mqtt_c/MQTTESP8266.c @@ -20,6 +20,7 @@ */ #include +#include #include #include #include diff --git a/extras/spiffs/esp_spiffs.c b/extras/spiffs/esp_spiffs.c index 58f1d33..93ea24a 100644 --- a/extras/spiffs/esp_spiffs.c +++ b/extras/spiffs/esp_spiffs.c @@ -149,7 +149,8 @@ int _open_r(struct _reent *r, const char *pathname, int flags, int mode) return SPIFFS_open(&fs, pathname, spiffs_flags, mode); } -int _close_r(struct _reent *r, int fd) +// This implementation replaces implementation in core/newlib_syscals.c +int _close_filesystem_r(struct _reent *r, int fd) { return SPIFFS_close(&fs, (spiffs_file)fd); } diff --git a/extras/spiffs/spiffs_config.h b/extras/spiffs/spiffs_config.h index 0c848a5..52c3aea 100644 --- a/extras/spiffs/spiffs_config.h +++ b/extras/spiffs/spiffs_config.h @@ -198,7 +198,15 @@ typedef unsigned char u8_t; // NB: This adds config field fh_ix_offset in the configuration struct when // mounting, which must be defined. #ifndef SPIFFS_FILEHDL_OFFSET -#define SPIFFS_FILEHDL_OFFSET 1 +#define SPIFFS_FILEHDL_OFFSET 17 +// Not ideal having to use a literal above, which is necessary because this file +// is also included by tools that run on the host, but at least do some checks +// when building the target code. +#ifdef LWIP_SOCKET_OFFSET +#if SPIFFS_FILEHDL_OFFSET < (LWIP_SOCKET_OFFSET + MEMP_NUM_NETCONN) +#error SPIFFS_FILEHDL_OFFSET clashes with lwip sockets range. +#endif +#endif #endif // Enable this to compile a read only version of spiffs. diff --git a/extras/wificfg/wificfg.c b/extras/wificfg/wificfg.c index 708af29..c2ca1ed 100644 --- a/extras/wificfg/wificfg.c +++ b/extras/wificfg/wificfg.c @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -1722,9 +1723,9 @@ static void dns_task(void *pvParameters) const struct ifreq ifreq0 = { "en0" }; const struct ifreq ifreq1 = { "en1" }; - lwip_setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, - sdk_wifi_get_opmode() == STATIONAP_MODE ? &ifreq1 : &ifreq0, - sizeof(ifreq0)); + setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, + sdk_wifi_get_opmode() == STATIONAP_MODE ? &ifreq1 : &ifreq0, + sizeof(ifreq0)); for (;;) { char buffer[96]; diff --git a/lwip/include/arch/cc.h b/lwip/include/arch/cc.h index e7cf291..5787157 100644 --- a/lwip/include/arch/cc.h +++ b/lwip/include/arch/cc.h @@ -39,6 +39,7 @@ #include #include #include +#include struct ip4_addr; struct esf_buf; diff --git a/lwip/include/lwipopts.h b/lwip/include/lwipopts.h index 4d69eda..30282b5 100644 --- a/lwip/include/lwipopts.h +++ b/lwip/include/lwipopts.h @@ -144,6 +144,15 @@ ------------------------------------------------ */ +/** + * MEMP_NUM_NETCONN: the number of struct netconns. + * (only needed if you use the sequential API, like api_lib.c) + * This also sets the number of lwip socket descriptors. + */ +#ifndef MEMP_NUM_NETCONN +#define MEMP_NUM_NETCONN 12 +#endif + /* -------------------------------- ---------- ARP options ------- @@ -549,6 +558,25 @@ ---------- Socket options ---------- ------------------------------------ */ + +/** + * LWIP_POSIX_SOCKETS_IO_NAMES==1: Enable POSIX-style sockets functions names. + * Disable this option if you use a POSIX operating system that uses the same + * names (read, write & close). (only used if you use sockets.c) + */ +#define LWIP_POSIX_SOCKETS_IO_NAMES 0 + +/** + * LWIP_SOCKET_OFFSET==n: Increases the file descriptor number created by LwIP with n. + * This can be useful when there are multiple APIs which create file descriptors. + * When they all start with a different offset and you won't make them overlap you can + * re implement read/write/close/ioctl/fnctl to send the requested action to the right + * library (sharing select will need more work though). + */ +#ifndef LWIP_SOCKET_OFFSET +#define LWIP_SOCKET_OFFSET 3 +#endif + /** * LWIP_SO_SNDTIMEO==1: Enable send timeout for sockets/netconns and * SO_SNDTIMEO processing. diff --git a/lwip/lwip b/lwip/lwip index 74676d4..f372198 160000 --- a/lwip/lwip +++ b/lwip/lwip @@ -1 +1 @@ -Subproject commit 74676d46f0bc5ed82515f8e247008b7c45ec6cf6 +Subproject commit f372198834caf642c4b2d29d98fc1e120e3ce2b2