1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <esp_log.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include "esp_netif.h"
#include "esp_tls_crypto.h"
#include <esp_https_server.h>
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_tls.h"
#include <esp_wifi.h>
#include "webserver.h"
#include "spiffs.h"
#include "wifi.h"
#include "main.h"
static const char *TAG = "webserver";
#define MAX_CLIENTS 4
static httpd_handle_t server = NULL;
/*
* Helpers
*/
static char* get_value(char* buffer, const char* key, size_t key_size) {
// Doesn't handle if key can be found in a value, but thats fine for our cases
char* start = strstr(buffer, key);
if (start == NULL) return NULL;
start += key_size + 1;
char* end = strchr(start, '&');
if (end == NULL) return start;
*end = '\1';
return start;
}
/*
* Page handlers
*/
static void logi_get(httpd_req_t *req) {
/* After sending the HTTP response the old HTTP request
* headers are lost. Check if HTTP request headers can be read now. */
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
ESP_LOGI(TAG, "Served %s", req->uri);
}
else {
ESP_LOGE(TAG, "Couldn't serve %s", req->uri);
}
}
static void logi_post(httpd_req_t *req) {
/* After sending the HTTP response the old HTTP request
* headers are lost. Check if HTTP request headers can be read now. */
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
ESP_LOGI(TAG, "Processed %s", req->uri);
}
else {
ESP_LOGE(TAG, "Couldn't process %s", req->uri);
}
}
// Index page is embedded in binary
static const unsigned char* index_page = NULL;
static size_t index_page_len = 0;
static esp_err_t get_handler(httpd_req_t *req)
{
httpd_resp_send(req, (const char*)index_page, index_page_len);
logi_get(req);
return ESP_OK;
}
static const httpd_uri_t get_index = {
.uri = "/",
.method = HTTP_GET,
.handler = get_handler,
};
// Index page is embedded in binary
static const unsigned char* favicon = NULL;
static size_t favicon_len = 0;
static esp_err_t get_favicon_handler(httpd_req_t *req)
{
ESP_ERROR_CHECK(httpd_resp_set_type(req, "image/x-icon"));
httpd_resp_send(req, (const char*)favicon, favicon_len);
logi_get(req);
return ESP_OK;
}
static const httpd_uri_t get_favicon = {
.uri = "/favicon.ico",
.method = HTTP_GET,
.handler = get_favicon_handler,
};
static esp_err_t get_image_handler(httpd_req_t *req)
{
ESP_ERROR_CHECK(httpd_resp_set_type(req, "image/jpeg"));
char* resp_str = NULL;
size_t length = spiffs_read(req->uri, &resp_str);
httpd_resp_send(req, resp_str, length);
free(resp_str);
logi_get(req);
return ESP_OK;
}
static const httpd_uri_t get_image = {
.uri = SPIFFS_PREFIX "/*",
.method = HTTP_GET,
.handler = get_image_handler,
};
;
const char ssid_param[] = "ssid";
const char pass_param[] = "pass";
const char imagesave_param[] = "imagesave";
const char resolution_param[] = "resolution";
const char invertion_param[] = "invertion";
const char timelapse_period_param[] = "timelapse_period";
const char timelapse_change_param[] = "timelapse_change";
const char remote_photo_param[] = "remote_photo";
static void main_store_request(const char* value, void (*main_store)(int32_t)) {
if (*value == '%') value++;
main_store((int32_t)strtol(value, NULL, 16));
}
static esp_err_t post_config_handler(httpd_req_t *req) {
// Set buffer
char* buffer = malloc(req->content_len + 1);
httpd_req_recv(req, buffer, req->content_len);
buffer[req->content_len] = '\0'; // just in case
// Fix spaces
for (char* iter = buffer; *iter != '\0'; iter++) {
if (*iter == '+') *iter = ' ';
}
// Get values
char* remote_photo = get_value(buffer, remote_photo_param, sizeof(remote_photo_param)-1);
char* ssid = get_value(buffer, ssid_param, sizeof(ssid_param)-1);
char* pass = get_value(buffer, pass_param, sizeof(pass_param)-1);
char* imagesave = get_value(buffer, imagesave_param, sizeof(imagesave_param)-1);
char* resolution = get_value(buffer, resolution_param, sizeof(resolution_param)-1);
char* invertion = get_value(buffer, invertion_param, sizeof(invertion_param)-1);
char* timelapse_period = get_value(buffer, timelapse_period_param, sizeof(timelapse_period_param)-1);
char* timelapse_change = get_value(buffer, timelapse_change_param, sizeof(timelapse_change_param)-1);
// Fix terminating zero
for (char* iter = buffer; *iter != '\0'; iter++) {
if (*iter == '\1') *iter = '\0';
}
// Value logic
if (remote_photo != NULL) {
main_force_photo();
}
if (ssid != NULL && pass != NULL) {
wifi_store_network(ssid, pass);
}
if (imagesave != NULL) {
main_store_request(imagesave, main_store_imagesave);
}
if (resolution != NULL) {
main_store_request(resolution, main_store_resolution);
}
if (invertion != NULL) {
main_store_request(invertion, main_store_invertion);
}
if (timelapse_period != NULL) {
main_store_timelapse_period((int32_t)atoi(timelapse_period));
}
if (timelapse_change != NULL) {
if (*timelapse_change == '0') {
main_stop_timelapse();
}
else if (*timelapse_change == '1') {
main_start_timelapse();
}
}
// Final
free(buffer);
httpd_resp_send(req, "", 0);
logi_post(req);
return ESP_OK;
}
static const httpd_uri_t post_config = {
.uri = "/config",
.method = HTTP_POST,
.handler = post_config_handler,
};
static esp_err_t ws_camera_handler(httpd_req_t *req) {
if (req->method == HTTP_GET) {
ESP_LOGI(TAG, "Established websocket connection");
return ESP_OK;
}
// Nobody should send websocket data, it's only for receiving
return ESP_OK;
}
static const httpd_uri_t ws_camera = {
.uri = "/camera",
.method = HTTP_GET,
.handler = ws_camera_handler,
.is_websocket = true,
};
/*
* On network connection/disconnect
*/
static void connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
webserver_start();
}
/*
static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server) {
ESP_LOGI(TAG, "Stopping webserver");
if (stop_webserver(*server) == ESP_OK) {
*server = NULL;
} else {
ESP_LOGE(TAG, "Failed to stop http server");
}
}
}
*/
/*
* Public interface
*/
#define SET_EMBEDDED_PTR(var_store, var_size, var, conv_filename) \
extern const unsigned char var ## _start[] asm("_binary_" conv_filename "_start"); \
extern const unsigned char var ## _end[] asm("_binary_" conv_filename "_end"); \
var_store = (var ## _start); var_size = (var ## _end - var ## _start);
void webserver_init() {
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, NULL));
/* ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); */
SET_EMBEDDED_PTR(index_page, index_page_len, index_page, "index_html");
SET_EMBEDDED_PTR(favicon, favicon_len, favicon, "favicon_ico");
}
void webserver_start()
{
if (server != NULL) {
ESP_LOGW(TAG, "Tried starting the server while it was already started");
return;
}
httpd_ssl_config_t config = HTTPD_SSL_CONFIG_DEFAULT();
config.httpd.lru_purge_enable = true;
config.httpd.uri_match_fn = httpd_uri_match_wildcard;
config.httpd.max_open_sockets = MAX_CLIENTS;
SET_EMBEDDED_PTR(config.servercert, config.servercert_len, cacert, "cacert_pem");
SET_EMBEDDED_PTR(config.prvtkey_pem, config.prvtkey_len, prvtkey, "prvtkey_pem");
if (httpd_ssl_start(&server, &config) != ESP_OK) {
ESP_LOGE(TAG, "Couldn't start server");
server = NULL;
return;
}
httpd_register_uri_handler(server, &get_index);
httpd_register_uri_handler(server, &get_favicon);
httpd_register_uri_handler(server, &get_image);
httpd_register_uri_handler(server, &post_config);
httpd_register_uri_handler(server, &ws_camera);
ESP_LOGI(TAG, "Started server on port %d", config.httpd.server_port);
}
static httpd_ws_frame_t empty_value = {
.fragmented = false,
.type = HTTPD_WS_TYPE_BINARY,
.payload = NULL,
.len = 0,
};
void ws_send_bin(const char* msg, size_t size) {
httpd_ws_frame_t value = {
.fragmented = false,
.type = HTTPD_WS_TYPE_BINARY,
.payload = (unsigned char*)msg,
.len = size,
};
size_t clients = MAX_CLIENTS;
int client_fds[MAX_CLIENTS];
ESP_ERROR_CHECK(httpd_get_client_list(server, &clients, client_fds));
for (size_t i = 0; i < clients; i++) {
if (httpd_ws_get_fd_info(server, client_fds[i]) != HTTPD_WS_CLIENT_WEBSOCKET) continue;
ESP_LOGI(TAG, "Sending websocket data to fd:%d", client_fds[i]);
httpd_ws_send_data(server, client_fds[i], &value);
httpd_ws_send_data(server, client_fds[i], &empty_value);
}
}
|