libwebsockets
Lightweight C library for HTML5 websockets
lws-esp32.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation:
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  *
21  * included from libwebsockets.h
22  */
23 
24 typedef int lws_sockfd_type;
25 typedef int lws_filefd_type;
26 
27 /*
28  * Later lwip (at least 2.1.12) already defines these in its own headers
29  * protected by the same test as used here... if POLLIN / POLLOUT already exist
30  * then assume no need to declare those and struct pollfd.
31  *
32  * Older lwip needs these declarations done here.
33  */
34 
35 #if !defined(POLLIN) && !defined(POLLOUT)
36 
37 struct pollfd {
38  lws_sockfd_type fd;
39  short events;
40  short revents;
41 };
42 #define POLLIN 0x0001
43 #define POLLPRI 0x0002
44 #define POLLOUT 0x0004
45 #define POLLERR 0x0008
46 #define POLLHUP 0x0010
47 #define POLLNVAL 0x0020
48 
49 #endif
50 
51 #if defined(LWS_AMAZON_RTOS)
52 #include <FreeRTOS.h>
53 #include <event_groups.h>
54 #include <string.h>
55 #include "timers.h"
56 #else /* LWS_AMAZON_RTOS */
57 #include <freertos/FreeRTOS.h>
58 #include <freertos/event_groups.h>
59 #include <string.h>
60 #include "esp_wifi.h"
61 #include "esp_system.h"
62 #include "esp_event.h"
63 #include "esp_event_loop.h"
64 #include "nvs.h"
65 #include "driver/gpio.h"
66 #include "esp_spi_flash.h"
67 #include "freertos/timers.h"
68 #endif /* LWS_AMAZON_RTOS */
69 
70 #if !defined(CONFIG_FREERTOS_HZ)
71 #define CONFIG_FREERTOS_HZ 100
72 #endif
73 
74 typedef TimerHandle_t uv_timer_t;
75 typedef void uv_cb_t(uv_timer_t *);
76 typedef void * uv_handle_t;
77 
78 struct timer_mapping {
79  uv_cb_t *cb;
80  uv_timer_t *t;
81 };
82 
83 #define UV_VERSION_MAJOR 1
84 
85 #define lws_uv_getloop(a, b) (NULL)
86 
87 static LWS_INLINE void uv_timer_init(void *l, uv_timer_t *t)
88 {
89  (void)l;
90  *t = NULL;
91 }
92 
93 extern void esp32_uvtimer_cb(TimerHandle_t t);
94 
95 static LWS_INLINE void uv_timer_start(uv_timer_t *t, uv_cb_t *cb, int first, int rep)
96 {
97  struct timer_mapping *tm = (struct timer_mapping *)malloc(sizeof(*tm));
98 
99  if (!tm)
100  return;
101 
102  tm->t = t;
103  tm->cb = cb;
104 
105  *t = xTimerCreate("x", pdMS_TO_TICKS(first), !!rep, tm,
106  (TimerCallbackFunction_t)esp32_uvtimer_cb);
107  xTimerStart(*t, 0);
108 }
109 
110 static LWS_INLINE void uv_timer_stop(uv_timer_t *t)
111 {
112  xTimerStop(*t, 0);
113 }
114 
115 static LWS_INLINE void uv_close(uv_handle_t *h, void *v)
116 {
117  free(pvTimerGetTimerID((uv_timer_t)h));
118  xTimerDelete(*(uv_timer_t *)h, 0);
119 }
120 
121 
122 #if !defined(LWS_AMAZON_RTOS)
123 
124 /* ESP32 helper declarations */
125 
126 #include <mdns.h>
127 #include <esp_partition.h>
128 
129 #define LWS_PLUGIN_STATIC
130 #define LWS_MAGIC_REBOOT_TYPE_ADS 0x50001ffc
131 #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
132 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
133 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
134 #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA 0xfac0eeee
135 
136 /* user code provides these */
137 
138 extern void
139 lws_esp32_identify_physical_device(void);
140 
141 /* lws-plat-esp32 provides these */
142 
143 typedef void (*lws_cb_scan_done)(uint16_t count, wifi_ap_record_t *recs, void *arg);
144 
145 enum genled_state {
146  LWSESP32_GENLED__INIT,
147  LWSESP32_GENLED__LOST_NETWORK,
148  LWSESP32_GENLED__NO_NETWORK,
149  LWSESP32_GENLED__CONN_AP,
150  LWSESP32_GENLED__GOT_IP,
151  LWSESP32_GENLED__OK,
152 };
153 
155  struct lws_group_member *next;
156  uint64_t last_seen;
157  char model[16];
158  char role[16];
159  char host[32];
160  char mac[20];
161  int width, height;
162  struct ip4_addr addr;
163  struct ip6_addr addrv6;
164  uint8_t flags;
165 };
166 
167 #define LWS_SYSTEM_GROUP_MEMBER_ADD 1
168 #define LWS_SYSTEM_GROUP_MEMBER_CHANGE 2
169 #define LWS_SYSTEM_GROUP_MEMBER_REMOVE 3
170 
171 #define LWS_GROUP_FLAG_SELF 1
172 
173 struct lws_esp32 {
174  char sta_ip[16];
175  char sta_mask[16];
176  char sta_gw[16];
177  char serial[16];
178  char opts[16];
179  char model[16];
180  char group[16];
181  char role[16];
182  char ssid[4][64];
183  char password[4][64];
184  char active_ssid[64];
185  char access_pw[16];
186  char hostname[32];
187  char mac[20];
188  char le_dns[64];
189  char le_email[64];
190  char region;
191  char inet;
192  char conn_ap;
193 
194  enum genled_state genled;
195  uint64_t genled_t;
196 
197  lws_cb_scan_done scan_consumer;
198  void *scan_consumer_arg;
199  struct lws_group_member *first;
200  int extant_group_members;
201 
202  char acme;
203  char upload;
204 
205  volatile char button_is_down;
206 };
207 
209  uint32_t romfs;
210  uint32_t romfs_len;
211  uint32_t json;
212  uint32_t json_len;
213 };
214 
215 extern struct lws_esp32 lws_esp32;
216 struct lws_vhost;
217 
218 extern esp_err_t
219 lws_esp32_event_passthru(void *ctx, system_event_t *event);
220 extern void
221 lws_esp32_wlan_config(void);
222 extern void
223 lws_esp32_wlan_start_ap(void);
224 extern void
225 lws_esp32_wlan_start_station(void);
227 extern void
228 lws_esp32_set_creation_defaults(struct lws_context_creation_info *info);
229 extern struct lws_context *
230 lws_esp32_init(struct lws_context_creation_info *, struct lws_vhost **pvh);
231 extern int
232 lws_esp32_wlan_nvs_get(int retry);
233 extern esp_err_t
234 lws_nvs_set_str(nvs_handle handle, const char* key, const char* value);
235 extern void
236 lws_esp32_restart_guided(uint32_t type);
237 extern const esp_partition_t *
238 lws_esp_ota_get_boot_partition(void);
239 extern int
240 lws_esp32_get_image_info(const esp_partition_t *part, struct lws_esp32_image *i, char *json, int json_len);
241 extern int
242 lws_esp32_leds_network_indication(void);
243 
244 extern uint32_t lws_esp32_get_reboot_type(void);
245 extern uint16_t lws_esp32_sine_interp(int n);
246 
247 /* required in external code by esp32 plat (may just return if no leds) */
248 extern void lws_esp32_leds_timer_cb(TimerHandle_t th);
249 
250 #endif /* LWS_AMAZON_RTOS */
lws_esp32_image
Definition: lws-esp32.h:208
pollfd
Definition: lws-esp32.h:37
lws_context_creation_info
Definition: lws-context-vhost.h:181
pollfd::revents
short revents
Definition: lws-esp32.h:40
pollfd::events
short events
Definition: lws-esp32.h:39
pollfd::fd
lws_sockfd_type fd
Definition: lws-esp32.h:38
lws_group_member
Definition: lws-esp32.h:154
timer_mapping
Definition: lws-esp32.h:78
lws_esp32
Definition: lws-esp32.h:173