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.
This commit is contained in:
Our Air Quality 2017-10-19 03:33:32 +10:00 committed by Johan Kanflo
parent 87a3503f93
commit ebdd2f983b
12 changed files with 102 additions and 13 deletions

View file

@ -14,6 +14,21 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdout_redirect.h> #include <stdout_redirect.h>
#include <sys/time.h> #include <sys/time.h>
#include <lwip/sockets.h>
/*
* 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; 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 ) __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 _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 */ /* 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 ) __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_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 /* 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"))) __attribute__((weak, alias("syscall_returns_enosys")))
int _open_r(struct _reent *r, const char *pathname, int flags, int mode); 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"))) __attribute__((weak, alias("syscall_returns_enosys")))
int _unlink_r(struct _reent *r, const char *path); int _unlink_r(struct _reent *r, const char *path);

View file

@ -7,6 +7,7 @@
#include "espressif/esp_common.h" #include "espressif/esp_common.h"
#include "esp/uart.h" #include "esp/uart.h"
#include <unistd.h>
#include <string.h> #include <string.h>
#include "FreeRTOS.h" #include "FreeRTOS.h"

View file

@ -13,6 +13,7 @@
#include "esp/uart.h" #include "esp/uart.h"
#include "esp/hwrand.h" #include "esp/hwrand.h"
#include <unistd.h>
#include <string.h> #include <string.h>
#include "FreeRTOS.h" #include "FreeRTOS.h"

View file

@ -19,6 +19,7 @@
#include "esp/uart.h" #include "esp/uart.h"
#include "esp/hwrand.h" #include "esp/hwrand.h"
#include <unistd.h>
#include <string.h> #include <string.h>
#include "FreeRTOS.h" #include "FreeRTOS.h"

View file

@ -21,6 +21,7 @@
* This file is part of mbed TLS (https://tls.mbed.org) * This file is part of mbed TLS (https://tls.mbed.org)
*/ */
#include <fcntl.h>
#if !defined(MBEDTLS_CONFIG_FILE) #if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h" #include "mbedtls/config.h"
#else #else

View file

@ -20,6 +20,7 @@
*/ */
#include <espressif/esp_common.h> #include <espressif/esp_common.h>
#include <unistd.h>
#include <lwip/sockets.h> #include <lwip/sockets.h>
#include <lwip/inet.h> #include <lwip/inet.h>
#include <lwip/netdb.h> #include <lwip/netdb.h>

View file

@ -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); 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); return SPIFFS_close(&fs, (spiffs_file)fd);
} }

View file

@ -198,7 +198,15 @@ typedef unsigned char u8_t;
// NB: This adds config field fh_ix_offset in the configuration struct when // NB: This adds config field fh_ix_offset in the configuration struct when
// mounting, which must be defined. // mounting, which must be defined.
#ifndef SPIFFS_FILEHDL_OFFSET #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 #endif
// Enable this to compile a read only version of spiffs. // Enable this to compile a read only version of spiffs.

View file

@ -21,6 +21,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <unistd.h>
#include <espressif/esp_common.h> #include <espressif/esp_common.h>
#include <espressif/user_interface.h> #include <espressif/user_interface.h>
@ -1722,7 +1723,7 @@ static void dns_task(void *pvParameters)
const struct ifreq ifreq0 = { "en0" }; const struct ifreq ifreq0 = { "en0" };
const struct ifreq ifreq1 = { "en1" }; const struct ifreq ifreq1 = { "en1" };
lwip_setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
sdk_wifi_get_opmode() == STATIONAP_MODE ? &ifreq1 : &ifreq0, sdk_wifi_get_opmode() == STATIONAP_MODE ? &ifreq1 : &ifreq0,
sizeof(ifreq0)); sizeof(ifreq0));

View file

@ -39,6 +39,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <esp/hwrand.h> #include <esp/hwrand.h>
#include <fcntl.h>
struct ip4_addr; struct ip4_addr;
struct esf_buf; struct esf_buf;

View file

@ -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 ------- ---------- ARP options -------
@ -549,6 +558,25 @@
---------- Socket options ---------- ---------- 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 * LWIP_SO_SNDTIMEO==1: Enable send timeout for sockets/netconns and
* SO_SNDTIMEO processing. * SO_SNDTIMEO processing.

@ -1 +1 @@
Subproject commit 74676d46f0bc5ed82515f8e247008b7c45ec6cf6 Subproject commit f372198834caf642c4b2d29d98fc1e120e3ce2b2