1 // Copyright 2018 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 #ifndef __ESP_SYS_SELECT_H__
16 #define __ESP_SYS_SELECT_H__
17 
18 /* Newlib 2.2.0 does not provide sys/select.h, and fd_set is defined in sys/types.h */
19 #include <sys/types.h>
20 #ifndef fd_set
21 #include_next <sys/select.h>
22 #else // fd_set
23 #include <sys/time.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
30 
31 #ifdef __cplusplus
32 } // extern "C"
33 #endif
34 
35 #endif // fd_set
36 
37 #if defined(FD_ISSET) || defined(FD_SET) || defined(FD_CLR)
38 #undef FD_SET
39 #undef FD_CLR
40 #undef FD_ISSET
41 
42 #define __FD_SAFE_SET(n, code) do { if ((unsigned)(n) < FD_SETSIZE) { code; } } while(0)
43 #define __FD_SAFE_GET(n, code) (((unsigned)(n) < FD_SETSIZE) ? (code) : 0)
44 
45 #define FD_SET(n, p)	__FD_SAFE_SET(n, ((p)->fds_bits[(n) / NFDBITS] |=  (1L << ((n) % NFDBITS))))
46 #define FD_CLR(n, p)	__FD_SAFE_SET(n, ((p)->fds_bits[(n) / NFDBITS] &= ~(1L << ((n) % NFDBITS))))
47 #define FD_ISSET(n, p)	__FD_SAFE_GET(n, ((p)->fds_bits[(n) / NFDBITS] &   (1L << ((n) % NFDBITS))))
48 #endif // FD_ISSET || FD_SET || FD_CLR
49 
50 #endif //__ESP_SYS_SELECT_H__
51