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
|
#include <Arduino.h>
#include <string.h>
#include "http_server.h"
#include "config.h"
#include "wifi.h"
#include "config.def.h"
#include "logging.h"
namespace homekit {
static const char CONTENT_TYPE_HTML[] = "text/html; charset=utf-8";
static const char CONTENT_TYPE_CSS[] = "text/css";
static const char CONTENT_TYPE_JS[] = "application/javascript";
static const char CONTENT_TYPE_JSON[] = "application/json";
static const char CONTENT_TYPE_FAVICON[] = "image/x-icon";
static const char JSON_STATUS_FMT[] = "{\"node_id\":\"%s\""
#ifdef DEBUG
",\"configured\":%d"
",\"crc\":%u"
",\"fl_n\":%d"
",\"fl_w\":%d"
#endif
"}";
static const size_t JSON_BUF_SIZE = 192;
static const char NODE_ID_ERROR[] = "?";
static const char FIELD_NODE_ID[] = "nid";
static const char FIELD_SSID[] = "ssid";
static const char FIELD_PSK[] = "psk";
static const char MSG_IS_INVALID[] = " is invalid";
static const char MSG_IS_MISSING[] = " is missing";
static const char GZIP[] = "gzip";
static const char CONTENT_ENCODING[] = "Content-Encoding";
static const char NOT_FOUND[] = "Not Found";
static void do_restart() {
ESP.restart();
}
void HttpServer::start() {
_server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest* req) { sendGzip(req, files::style_css, CONTENT_TYPE_CSS); });
_server.on("/app.js", HTTP_GET, [](AsyncWebServerRequest* req) { sendGzip(req, files::app_js, CONTENT_TYPE_JS); });
_server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest* req) { sendGzip(req, files::favicon_ico, CONTENT_TYPE_FAVICON); });
_server.on("/", HTTP_GET, [&](AsyncWebServerRequest* req) { sendGzip(req, files::index_html, CONTENT_TYPE_HTML); });
_server.on("/status", HTTP_GET, [](AsyncWebServerRequest* req) {
char json_buf[JSON_BUF_SIZE];
auto cfg = config::read();
char *ssid, *psk;
wifi::getConfig(cfg, &ssid, &psk, nullptr);
if (!isValid(cfg) || !cfg.flags.node_configured) {
sprintf(json_buf, JSON_STATUS_FMT
, DEFAULT_NODE_ID
#ifdef DEBUG
, 0
, cfg.crc
, cfg.flags.node_configured
, cfg.flags.wifi_configured
#endif
);
} else {
char escaped_node_id[32];
char *escaped_node_id_res = cfg.escapeNodeId(escaped_node_id, 32);
sprintf(json_buf, JSON_STATUS_FMT
, escaped_node_id_res == nullptr ? NODE_ID_ERROR : escaped_node_id
#ifdef DEBUG
, 1
, cfg.crc
, cfg.flags.node_configured
, cfg.flags.wifi_configured
#endif
);
}
req->send(200, CONTENT_TYPE_JSON, json_buf);
});
_server.on("/status", HTTP_POST, [&](AsyncWebServerRequest* req) {
auto cfg = config::read();
char *s;
if (!handleInputStr(req, FIELD_SSID, 32, &s)) return;
strncpy(cfg.wifi_ssid, s, 32);
PRINTF("saving ssid: %s\n", cfg.wifi_ssid);
if (!handleInputStr(req, FIELD_PSK, 63, &s)) return;
strncpy(cfg.wifi_psk, s, 63);
PRINTF("saving psk: %s\n", cfg.wifi_psk);
if (!handleInputStr(req, FIELD_NODE_ID, 16, &s)) return;
strcpy(cfg.node_id, s);
PRINTF("saving node id: %s\n", cfg.node_id);
cfg.flags.node_configured = 1;
cfg.flags.wifi_configured = 1;
if (!config::write(cfg)) {
PRINTLN("eeprom write error");
return sendError(req, "eeprom error");
}
restartTimer.once(0, do_restart);
});
_server.on("/reset", HTTP_POST, [&](AsyncWebServerRequest* req) {
config::erase();
restartTimer.once(0, do_restart);
});
_server.on("/heap", HTTP_GET, [](AsyncWebServerRequest* req) {
req->send(200, CONTENT_TYPE_HTML, String(ESP.getFreeHeap()));
});
_server.on("/scan", HTTP_GET, [&](AsyncWebServerRequest* req) {
int i = 0;
size_t len;
const char* ssid;
bool enough = false;
bzero(reinterpret_cast<uint8_t*>(buf1k), ARRAY_SIZE(buf1k));
char* cur = buf1k;
strncpy(cur, "{\"list\":[", ARRAY_SIZE(buf1k));
cur += 9;
for (auto& res: *_scanResults) {
ssid = res.ssid.c_str();
len = res.ssid.length();
// new item (array with 2 items)
*cur++ = '[';
// 1. ssid (string)
*cur++ = '"';
for (size_t j = 0; j < len; j++) {
if (*(ssid+j) == '"')
*cur++ = '\\';
*cur++ = *(ssid+j);
}
*cur++ = '"';
*cur++ = ',';
// 2. rssi (number)
cur += sprintf(cur, "%d", res.rssi);
// close array
*cur++ = ']';
if (cur - buf1k >= ARRAY_SIZE(buf1k)-40)
enough = true;
if (i < _scanResults->size()-1 || enough)
*cur++ = ',';
if (enough)
break;
i++;
}
*cur++ = ']';
*cur++ = '}';
*cur++ = '\0';
req->send(200, CONTENT_TYPE_JSON, buf1k);
});
_server.on("/update", HTTP_POST, [&](AsyncWebServerRequest* req) {
char json_buf[16];
bool should_reboot = !Update.hasError();
sprintf(json_buf, "{\"result\":%d}", should_reboot ? 1 : 0);
auto resp = req->beginResponse(200, CONTENT_TYPE_JSON, json_buf);
req->send(resp);
if (should_reboot) restartTimer.once(1, do_restart);
}, [&](AsyncWebServerRequest *req, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {
PRINTF("update start: %s\n", filename.c_str());
Update.runAsync(true);
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000))
Update.printError(Serial);
}
if (!Update.hasError() && len) {
if (Update.write(data, len) != len) {
Update.printError(Serial);
}
}
if (final) { // if the final flag is set then this is the last frame of data
if (Update.end(true)) {
PRINTF("update success: %uB\n", index+len);
} else {
Update.printError(Serial);
}
}
});
_server.onNotFound([](AsyncWebServerRequest* req) {
req->send(404, CONTENT_TYPE_HTML, NOT_FOUND);
});
_server.begin();
}
void HttpServer::sendGzip(AsyncWebServerRequest* req, StaticFile file, const char* content_type) {
auto resp = req->beginResponse_P(200, content_type, file.content, file.size);
resp->addHeader(CONTENT_ENCODING, GZIP);
req->send(resp);
}
void HttpServer::sendError(AsyncWebServerRequest* req, const String& message) {
char buf[32];
if (snprintf(buf, 32, "error: %s", message.c_str()) == 32)
buf[31] = '\0';
req->send(400, CONTENT_TYPE_HTML, buf);
}
bool HttpServer::handleInputStr(AsyncWebServerRequest *req,
const char *field_name,
size_t max_len,
char **dst) {
const char* s;
size_t len;
if (!req->hasParam(field_name, true)) {
sendError(req, String(field_name) + String(MSG_IS_MISSING));
return false;
}
s = req->getParam(field_name, true)->value().c_str();
len = strlen(s);
if (!len || len > max_len) {
sendError(req, String(FIELD_NODE_ID) + String(MSG_IS_INVALID));
return false;
}
*dst = (char*)s;
return true;
}
}
|