1 // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 #include <stdbool.h>
17 #include <stdarg.h>
18 #include <sys/errno.h>
19 #include <sys/lock.h>
20 #include <sys/fcntl.h>
21 #include "esp_attr.h"
22 #include "esp_vfs.h"
23 #include "sdkconfig.h"
24 #include "lwip/sockets.h"
25 #include "lwip/sys.h"
26 
27 #ifndef CONFIG_VFS_SUPPORT_IO
28 #error This file should only be built when CONFIG_VFS_SUPPORT_IO=y
29 #endif
30 
31 _Static_assert(MAX_FDS >= CONFIG_LWIP_MAX_SOCKETS, "MAX_FDS < CONFIG_LWIP_MAX_SOCKETS");
32 
33 #ifdef CONFIG_VFS_SUPPORT_SELECT
34 
lwip_stop_socket_select(void * sem)35 static void lwip_stop_socket_select(void *sem)
36 {
37     sys_sem_signal(sem); //socket_select will return
38 }
39 
lwip_stop_socket_select_isr(void * sem,BaseType_t * woken)40 static void lwip_stop_socket_select_isr(void *sem, BaseType_t *woken)
41 {
42     if (sys_sem_signal_isr(sem) && woken) {
43         *woken = pdTRUE;
44     }
45 }
46 
lwip_get_socket_select_semaphore(void)47 static void *lwip_get_socket_select_semaphore(void)
48 {
49     /* Calling this from the same process as select() will ensure that the semaphore won't be allocated from
50      * ISR (lwip_stop_socket_select_isr).
51      */
52     return (void *) sys_thread_sem_get();
53 }
54 #else // CONFIG_VFS_SUPPORT_SELECT
55 
select(int nfds,fd_set * readfds,fd_set * writefds,fd_set * errorfds,struct timeval * timeout)56 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
57 {
58     return lwip_select(nfds, readfds, writefds, errorfds, timeout);
59 }
60 
61 #endif // CONFIG_VFS_SUPPORT_SELECT
62 
lwip_fcntl_r_wrapper(int fd,int cmd,int arg)63 static int lwip_fcntl_r_wrapper(int fd, int cmd, int arg)
64 {
65     return lwip_fcntl(fd, cmd, arg);
66 }
67 
lwip_ioctl_r_wrapper(int fd,int cmd,va_list args)68 static int lwip_ioctl_r_wrapper(int fd, int cmd, va_list args)
69 {
70     return lwip_ioctl(fd, cmd, va_arg(args, void *));
71 }
72 
lwip_fstat(int fd,struct stat * st)73 static int lwip_fstat(int fd, struct stat * st)
74 {
75     if (st == NULL || fd < LWIP_SOCKET_OFFSET || fd > (MAX_FDS - 1)) {
76         errno = EBADF;
77         return -1;
78     }
79     memset(st, 0, sizeof(*st));
80     /* set the stat mode to socket type */
81     st->st_mode = S_IFSOCK;
82     return 0;
83 }
84 
esp_vfs_lwip_sockets_register(void)85 void esp_vfs_lwip_sockets_register(void)
86 {
87     esp_vfs_t vfs = {
88         .flags = ESP_VFS_FLAG_DEFAULT,
89         .write = &lwip_write,
90         .open = NULL,
91         .fstat = &lwip_fstat,
92         .close = &lwip_close,
93         .read = &lwip_read,
94         .fcntl = &lwip_fcntl_r_wrapper,
95         .ioctl = &lwip_ioctl_r_wrapper,
96 #ifdef CONFIG_VFS_SUPPORT_SELECT
97         .socket_select = &lwip_select,
98         .get_socket_select_semaphore = &lwip_get_socket_select_semaphore,
99         .stop_socket_select = &lwip_stop_socket_select,
100         .stop_socket_select_isr = &lwip_stop_socket_select_isr,
101 #endif // CONFIG_VFS_SUPPORT_SELECT
102     };
103     /* Non-LWIP file descriptors are from 0 to (LWIP_SOCKET_OFFSET-1). LWIP
104      * file descriptors are registered from LWIP_SOCKET_OFFSET to
105      * MAX_FDS-1.
106      */
107 
108     ESP_ERROR_CHECK(esp_vfs_register_fd_range(&vfs, NULL, LWIP_SOCKET_OFFSET, MAX_FDS));
109 }
110