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 struct pollfd {
28  lws_sockfd_type fd;
29  short events;
30  short revents;
31 };
32 #define POLLIN 0x0001
33 #define POLLPRI 0x0002
34 #define POLLOUT 0x0004
35 #define POLLERR 0x0008
36 #define POLLHUP 0x0010
37 #define POLLNVAL 0x0020
38 
39 #include <freertos/FreeRTOS.h>
40 #include <freertos/event_groups.h>
41 #include <string.h>
42 #include "esp_wifi.h"
43 #include "esp_system.h"
44 #include "esp_event.h"
45 #include "esp_event_loop.h"
46 #include "nvs.h"
47 #include "driver/gpio.h"
48 #include "esp_spi_flash.h"
49 #include "freertos/timers.h"
50 
51 #if !defined(CONFIG_FREERTOS_HZ)
52 #define CONFIG_FREERTOS_HZ 100
53 #endif
54 
55 typedef TimerHandle_t uv_timer_t;
56 typedef void uv_cb_t(uv_timer_t *);
57 typedef void * uv_handle_t;
58 
59 struct timer_mapping {
60  uv_cb_t *cb;
61  uv_timer_t *t;
62 };
63 
64 #define UV_VERSION_MAJOR 1
65 
66 #define lws_uv_getloop(a, b) (NULL)
67 
68 static LWS_INLINE void uv_timer_init(void *l, uv_timer_t *t)
69 {
70  (void)l;
71  *t = NULL;
72 }
73 
74 extern void esp32_uvtimer_cb(TimerHandle_t t);
75 
76 static LWS_INLINE void uv_timer_start(uv_timer_t *t, uv_cb_t *cb, int first, int rep)
77 {
78  struct timer_mapping *tm = (struct timer_mapping *)malloc(sizeof(*tm));
79 
80  if (!tm)
81  return;
82 
83  tm->t = t;
84  tm->cb = cb;
85 
86  *t = xTimerCreate("x", pdMS_TO_TICKS(first), !!rep, tm,
87  (TimerCallbackFunction_t)esp32_uvtimer_cb);
88  xTimerStart(*t, 0);
89 }
90 
91 static LWS_INLINE void uv_timer_stop(uv_timer_t *t)
92 {
93  xTimerStop(*t, 0);
94 }
95 
96 static LWS_INLINE void uv_close(uv_handle_t *h, void *v)
97 {
98  free(pvTimerGetTimerID((uv_timer_t)h));
99  xTimerDelete(*(uv_timer_t *)h, 0);
100 }
101 
102 /* ESP32 helper declarations */
103 
104 #include <mdns.h>
105 #include <esp_partition.h>
106 
107 #define LWS_PLUGIN_STATIC
108 #define LWS_MAGIC_REBOOT_TYPE_ADS 0x50001ffc
109 #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
110 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
111 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
112 #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA 0xfac0eeee
113 
114 /* user code provides these */
115 
116 extern void
117 lws_esp32_identify_physical_device(void);
118 
119 /* lws-plat-esp32 provides these */
120 
121 typedef void (*lws_cb_scan_done)(uint16_t count, wifi_ap_record_t *recs, void *arg);
122 
123 enum genled_state {
124  LWSESP32_GENLED__INIT,
125  LWSESP32_GENLED__LOST_NETWORK,
126  LWSESP32_GENLED__NO_NETWORK,
127  LWSESP32_GENLED__CONN_AP,
128  LWSESP32_GENLED__GOT_IP,
129  LWSESP32_GENLED__OK,
130 };
131 
133  struct lws_group_member *next;
134  uint64_t last_seen;
135  char model[16];
136  char role[16];
137  char host[32];
138  char mac[20];
139  int width, height;
140  struct ip4_addr addr;
141  struct ip6_addr addrv6;
142  uint8_t flags;
143 };
144 
145 #define LWS_SYSTEM_GROUP_MEMBER_ADD 1
146 #define LWS_SYSTEM_GROUP_MEMBER_CHANGE 2
147 #define LWS_SYSTEM_GROUP_MEMBER_REMOVE 3
148 
149 #define LWS_GROUP_FLAG_SELF 1
150 
151 struct lws_esp32 {
152  char sta_ip[16];
153  char sta_mask[16];
154  char sta_gw[16];
155  char serial[16];
156  char opts[16];
157  char model[16];
158  char group[16];
159  char role[16];
160  char ssid[4][64];
161  char password[4][64];
162  char active_ssid[64];
163  char access_pw[16];
164  char hostname[32];
165  char mac[20];
166  char le_dns[64];
167  char le_email[64];
168  char region;
169  char inet;
170  char conn_ap;
171 
172  enum genled_state genled;
173  uint64_t genled_t;
174 
175  lws_cb_scan_done scan_consumer;
176  void *scan_consumer_arg;
177  struct lws_group_member *first;
178  int extant_group_members;
179 
180  char acme;
181  char upload;
182 
183  volatile char button_is_down;
184 };
185 
187  uint32_t romfs;
188  uint32_t romfs_len;
189  uint32_t json;
190  uint32_t json_len;
191 };
192 
193 extern struct lws_esp32 lws_esp32;
194 struct lws_vhost;
195 
196 extern esp_err_t
197 lws_esp32_event_passthru(void *ctx, system_event_t *event);
198 extern void
199 lws_esp32_wlan_config(void);
200 extern void
201 lws_esp32_wlan_start_ap(void);
202 extern void
203 lws_esp32_wlan_start_station(void);
205 extern void
206 lws_esp32_set_creation_defaults(struct lws_context_creation_info *info);
207 extern struct lws_context *
208 lws_esp32_init(struct lws_context_creation_info *, struct lws_vhost **pvh);
209 extern int
210 lws_esp32_wlan_nvs_get(int retry);
211 extern esp_err_t
212 lws_nvs_set_str(nvs_handle handle, const char* key, const char* value);
213 extern void
214 lws_esp32_restart_guided(uint32_t type);
215 extern const esp_partition_t *
216 lws_esp_ota_get_boot_partition(void);
217 extern int
218 lws_esp32_get_image_info(const esp_partition_t *part, struct lws_esp32_image *i, char *json, int json_len);
219 extern int
220 lws_esp32_leds_network_indication(void);
221 
222 extern uint32_t lws_esp32_get_reboot_type(void);
223 extern uint16_t lws_esp32_sine_interp(int n);
224 
225 /* required in external code by esp32 plat (may just return if no leds) */
226 extern void lws_esp32_leds_timer_cb(TimerHandle_t th);
Definition: lws-esp32.h:151
Definition: lws-context-vhost.h:223
short events
Definition: lws-esp32.h:29
Definition: lws-esp32.h:132
Definition: lws-esp32.h:186
Definition: lws-esp32.h:27
Definition: lws-esp32.h:59
short revents
Definition: lws-esp32.h:30
lws_sockfd_type fd
Definition: lws-esp32.h:28