1  /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * Included from lib/private-lib-core.h if LWS_PLAT_FREERTOS
25  */
26 
27 #if !defined(LWS_ESP_PLATFORM)
28 #define SOMAXCONN 3
29 #endif
30 
31 #if defined(LWS_AMAZON_RTOS)
32  int
33  open(const char *path, int oflag, ...);
34 #else
35  #include <fcntl.h>
36 #endif
37 
38  #include <strings.h>
39  #include <unistd.h>
40  #include <sys/stat.h>
41  #include <sys/types.h>
42  #include <sys/time.h>
43  #include <netdb.h>
44 
45  #ifndef __cplusplus
46   #include <errno.h>
47  #endif
48  #include <signal.h>
49 #if defined(LWS_AMAZON_RTOS)
50 const char *
51 gai_strerror(int);
52 #else
53  #include <sys/socket.h>
54 #endif
55 
56 #if defined(LWS_AMAZON_RTOS)
57  #include "FreeRTOS.h"
58 #if defined(LWS_WITH_SYS_ASYNC_DNS)
59  #include "FreeRTOS_IP.h"
60 #endif
61  #include "timers.h"
62  #include <esp_attr.h>
63 #else
64  #include "freertos/timers.h"
65  #include <esp_attr.h>
66  #include <esp_system.h>
67  #include <esp_task_wdt.h>
68 #endif
69 
70 #if defined(LWS_WITH_ESP32)
71 #include "lwip/apps/sntp.h"
72 #include <errno.h>
73 #endif
74 
75 typedef SemaphoreHandle_t lws_mutex_t;
76 #define lws_mutex_init(x)	x = xSemaphoreCreateMutex()
77 #define lws_mutex_destroy(x)	vSemaphoreDelete(x)
78 #define lws_mutex_lock(x)	xSemaphoreTake(x, portMAX_DELAY)
79 #define lws_mutex_unlock(x)	xSemaphoreGive(x)
80 
81 #include <lwip/sockets.h>
82 
83  #if defined(LWS_BUILTIN_GETIFADDRS)
84   #include "./misc/getifaddrs.h"
85  #endif
86 
87  #define LWS_ERRNO errno
88  #define LWS_EAGAIN EAGAIN
89  #define LWS_EALREADY EALREADY
90  #define LWS_EINPROGRESS EINPROGRESS
91  #define LWS_EINTR EINTR
92  #define LWS_EISCONN EISCONN
93  #define LWS_ENOTCONN ENOTCONN
94  #define LWS_EWOULDBLOCK EWOULDBLOCK
95  #define LWS_EADDRINUSE EADDRINUSE
96 
97  #define lws_set_blocking_send(wsi)
98 
99  #ifndef LWS_NO_FORK
100   #ifdef LWS_HAVE_SYS_PRCTL_H
101    #include <sys/prctl.h>
102   #endif
103  #endif
104 
105 #if !defined(MSG_NOSIGNAL)
106 #define MSG_NOSIGNAL 0
107 #endif
108 
109 #define compatible_close(x) close(x)
110 #define lws_plat_socket_offset() LWIP_SOCKET_OFFSET
111 #define wsi_from_fd(A,B)  A->lws_lookup[B - lws_plat_socket_offset()]
112 
113 struct lws_context;
114 struct lws;
115 
116 int
117 insert_wsi(const struct lws_context *context, struct lws *wsi);
118 
119 #define delete_from_fd(A,B) A->lws_lookup[B - lws_plat_socket_offset()] = 0
120 
121 #define LWS_PLAT_TIMER_TYPE		TimerHandle_t
122 #define LWS_PLAT_TIMER_CB(name, var)	void name(TimerHandle_t var)
123 #define LWS_PLAT_TIMER_CB_GET_OPAQUE(x) pvTimerGetTimerID(x)
124 #define LWS_PLAT_TIMER_CREATE(name, interval, repeat, opaque, cb) \
125 	xTimerCreate(name, pdMS_TO_TICKS(interval) ? pdMS_TO_TICKS(interval) : 1, \
126 			repeat ? pdTRUE : 0, opaque, cb)
127 #define LWS_PLAT_TIMER_DELETE(ptr)	xTimerDelete(ptr, 0)
128 #define LWS_PLAT_TIMER_START(ptr)	xTimerStart(ptr, 0)
129 #define LWS_PLAT_TIMER_STOP(ptr)	xTimerStop(ptr, 0)
130 
131 
132