Repository navigation
Expand file tree
/
Copy pathweb_control.cpp
More file actions
559 lines (490 loc) · 18.5 KB
/
Copy pathweb_control.cpp
File metadata and controls
559 lines (490 loc) · 18.5 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "web_control.h"
#if FEATURE_WIFI
#include <Arduino.h>
#include <ESPmDNS.h>
#include <WebServer.h>
#include <WiFi.h>
#include "display.h"
#include "group_sync.h"
#include "ota.h"
#include "remote_control.h"
#include "settings.h"
#include "signal_light.h"
#include "web_page.h"
namespace web_control {
namespace {
WebServer server(WEB_SERVER_PORT);
bool accessPointMode = false;
bool running = false;
char addressText[16] = "";
// Networking is a small state machine rather than a one-shot at boot, so a
// unit that comes up before the building's router still ends up on it.
enum class Net : uint8_t {
Connecting, // station attempt in flight
Connected, // on the stored network
Dropped, // was connected, waiting to see if it returns
Ap, // serving our own access point
};
Net netState = Net::Ap;
uint32_t stateSinceMs = 0;
// Newest measurement, refreshed from loop() and read by the status endpoint.
sound_level::Reading latest = {0.0f, sound_level::Quality::BelowNoiseFloor};
const char* modeName(signal_light::Mode mode) {
switch (mode) {
case signal_light::Mode::Auto: return "auto";
case signal_light::Mode::Manual: return "manual";
default: return "off";
}
}
const char* zoneName(signal_light::Zone zone) {
switch (zone) {
case signal_light::Zone::Quiet: return "quiet";
case signal_light::Zone::Warn: return "warn";
case signal_light::Zone::Loud: return "loud";
default: return "unknown";
}
}
const char* qualityName(sound_level::Quality quality) {
switch (quality) {
case sound_level::Quality::Overload: return "overload";
case sound_level::Quality::BelowNoiseFloor: return "quiet";
default: return "ok";
}
}
// Hand-rolled rather than pulling in a JSON library: the document is fixed
// and small. Every value goes through one of the appenders below, each of
// which opens and closes its own quoting. An earlier version let one field's
// closing quote live in the next field's literal, which reads compactly right
// up until someone inserts a number in the middle and silently invalidates
// the whole document.
void appendEscaped(String& out, const char* value) {
for (const char* c = value; *c != '\0'; c++) {
if (*c == '"' || *c == '\\') {
out += '\\';
out += *c;
} else if (uint8_t(*c) >= 0x20) {
out += *c;
}
}
}
void appendKey(String& out, const char* key) {
if (out.length() > 1) out += ',';
out += '"';
out += key;
out += "\":";
}
void appendStr(String& out, const char* key, const char* value) {
appendKey(out, key);
out += '"';
appendEscaped(out, value);
out += '"';
}
void appendInt(String& out, const char* key, long value) {
appendKey(out, key);
out += value;
}
void appendBool(String& out, const char* key, bool value) {
appendKey(out, key);
out += value ? "true" : "false";
}
void appendFixed(String& out, const char* key, float value, int decimals) {
appendKey(out, key);
out += String(value, decimals);
}
// For a value that is itself a JSON document - the peer roster is an array of
// objects, each assembled with these same appenders, so nothing anywhere is
// quoted by hand.
void appendRaw(String& out, const char* key, const String& value) {
appendKey(out, key);
out += value;
}
void sendState() {
const Settings& s = settings::get();
const String ssid = accessPointMode ? String(WIFI_AP_SSID) : WiFi.SSID();
const IPAddress ip = accessPointMode ? WiFi.softAPIP() : WiFi.localIP();
char color[8];
snprintf(color, sizeof(color), "%06lX",
static_cast<unsigned long>(signal_light::manualColor() & 0xFFFFFF));
String out;
out.reserve(384);
out += '{';
appendStr(out, "name", PRODUCT_NAME);
appendStr(out, "version", FIRMWARE_VERSION);
appendFixed(out, "db", latest.leqDb, 1);
appendStr(out, "units", DB_UNITS);
appendStr(out, "quality", qualityName(latest.quality));
appendStr(out, "mode", modeName(signal_light::mode()));
appendStr(out, "zone", zoneName(signal_light::zone()));
appendStr(out, "color", color);
appendInt(out, "dbMin", s.dbMin);
appendInt(out, "dbMax", s.dbMax);
appendInt(out, "brightness", s.brightness);
appendInt(out, "displayBrightness", s.displayBrightness);
appendStr(out, "group", settings::groupName());
appendBool(out, "groupLevel", s.groupLevel);
appendInt(out, "zones", s.zones);
appendInt(out, "combine", s.combine);
appendInt(out, "inactiveLevel", s.inactiveLevel);
appendInt(out, "peers", group_sync::peerCount());
appendInt(out, "channel", group_sync::channel());
appendBool(out, "groupActive", group_sync::active());
appendStr(out, "unit", group_sync::localName());
appendInt(out, "coverage", group_sync::zoneCoverage());
appendInt(out, "overlap", group_sync::zoneOverlap());
appendStr(out, "preset", settings::presetName(settings::activePreset()));
// The roster is the one array in the document. Each entry is built with
// the same appenders, starting from its own opening brace, so appendKey's
// comma handling works inside it unchanged.
group_sync::PeerInfo peers[GROUP_MAX_PEERS];
const uint8_t n = group_sync::peerList(peers, GROUP_MAX_PEERS);
String roster;
roster.reserve(32 + n * 64);
roster += '[';
for (uint8_t i = 0; i < n; i++) {
if (i) roster += ',';
String entry;
entry += '{';
appendStr(entry, "id", peers[i].id);
appendStr(entry, "name", peers[i].name);
appendStr(entry, "ip", peers[i].ip);
appendFixed(entry, "db", peers[i].levelDb, 1);
appendInt(entry, "zones", peers[i].zones);
appendBool(entry, "follows", peers[i].followsGroup);
appendInt(entry, "inactiveLevel", peers[i].inactiveLevel);
appendInt(entry, "ageMs", peers[i].ageMs);
entry += '}';
roster += entry;
}
roster += ']';
appendRaw(out, "roster", roster);
appendStr(out, "net", accessPointMode ? "ap" : "sta");
appendStr(out, "ssid", ssid.c_str());
appendStr(out, "ip", ip.toString().c_str());
appendStr(out, "test", signal_light::selfTestLabel());
appendStr(out, "irCode",
remote_control::haveLastCode() ? remote_control::lastCodeHex() : "");
appendStr(out, "irProtocol",
remote_control::haveLastCode() ? remote_control::lastProtocol() : "");
appendBool(out, "irMapped", remote_control::lastCodeMapped());
appendInt(out, "irAgeMs", remote_control::lastCodeAgeMs());
#if FEATURE_OTA
appendBool(out, "ota", ota::available());
appendStr(out, "otaUser", ota::username());
#else
appendBool(out, "ota", false);
appendStr(out, "otaUser", "");
#endif
out += '}';
server.send(200, "application/json", out);
}
void handleRoot() { server.send_P(200, "text/html", WEB_PAGE); }
// Deliberately separate from /api/state: answering "what is running on that
// unit" should not require pulling a live measurement, and after an
// over-the-air update this is the thing worth checking.
void sendVersion() {
server.send(200, "application/json",
"{\"name\":\"" PRODUCT_NAME_JSON "\",\"version\":\"" FIRMWARE_VERSION_JSON "\"}");
}
// Thresholds and brightness. Every value is clamped by the settings module,
// so a malformed or hostile request cannot produce an unusable device.
void handleSet() {
if (server.hasArg("dbMin")) settings::setDbMin(server.arg("dbMin").toInt());
if (server.hasArg("dbMax")) settings::setDbMax(server.arg("dbMax").toInt());
if (server.hasArg("brightness")) {
settings::setBrightness(server.arg("brightness").toInt());
signal_light::setBrightness(settings::get().brightness);
}
if (server.hasArg("dbMin") || server.hasArg("dbMax") || server.hasArg("brightness")) {
group_sync::publishSettings();
}
if (server.hasArg("displayBrightness")) {
settings::setDisplayBrightness(server.arg("displayBrightness").toInt());
display::setBrightness(settings::get().displayBrightness);
}
sendState();
}
// Group membership and how this unit behaves within it. Changing the name
// needs the radio re-initialised, which is simplest to do by restarting.
void handleGroup() {
bool restart = false;
if (server.hasArg("group")) {
if (strcmp(server.arg("group").c_str(), settings::groupName()) != 0) {
settings::setGroupName(server.arg("group").c_str());
restart = true;
}
}
if (server.hasArg("groupLevel")) {
settings::setGroupLevel(server.arg("groupLevel").toInt() != 0);
}
if (server.hasArg("unit")) settings::setUnitName(server.arg("unit").c_str());
if (server.hasArg("zones")) settings::setZones(server.arg("zones").toInt());
if (server.hasArg("combine")) {
settings::setCombine(server.arg("combine").toInt());
// Group-wide, so the rest of the group is told rather than left to
// disagree about how readings are combined.
group_sync::publishSettings();
}
if (server.hasArg("inactiveLevel")) {
settings::setInactiveLevel(server.arg("inactiveLevel").toInt());
}
const Settings& s = settings::get();
signal_light::setZones(s.zones, s.inactiveLevel);
if (restart) {
// Flush the new name before the reset, or it would be lost with the
// deferred write still pending.
settings::flush();
server.send(200, "application/json", "{\"restarting\":true}");
server.client().flush();
delay(200);
ESP.restart();
return;
}
sendState();
}
// Configures one peer's per-unit settings from here, so a stack can be laid
// out without visiting each unit's own page. Nothing is acknowledged: the
// roster shows the change landing, because every unit advertises its own
// zones in the periodic broadcast.
void handlePeer() {
if (!server.hasArg("id")) {
server.send(400, "application/json", "{\"error\":\"id required\"}");
return;
}
const Settings& s = settings::get();
const uint8_t zones = server.hasArg("zones") ? server.arg("zones").toInt() : ZONE_MASK_ALL;
const bool follows = server.hasArg("groupLevel") ? server.arg("groupLevel").toInt() != 0 : true;
const uint8_t inactive =
server.hasArg("inactiveLevel") ? server.arg("inactiveLevel").toInt() : s.inactiveLevel;
group_sync::PeerConfig config = {};
config.zones = zones;
config.followsGroup = follows;
config.inactiveLevel = inactive;
config.displayBrightness = server.hasArg("displayBrightness")
? server.arg("displayBrightness").toInt()
: s.displayBrightness;
if (server.hasArg("name")) {
strncpy(config.name, server.arg("name").c_str(), GROUP_NAME_MAX);
config.name[GROUP_NAME_MAX] = '\0';
}
if (!group_sync::publishPeerConfig(server.arg("id").c_str(), config)) {
server.send(400, "application/json", "{\"error\":\"unknown peer\"}");
return;
}
sendState();
}
// Starts the LED self test. Deliberately its own route rather than a mode, so
// it cannot be reached by accident and always leaves the light as it found it.
void handleTest() {
signal_light::startSelfTest();
signal_light::tick();
group_sync::publishSelfTest();
sendState();
}
void handleMode() {
const String mode = server.arg("mode");
if (mode == "auto") {
signal_light::setMode(signal_light::Mode::Auto);
} else if (mode == "off") {
signal_light::setMode(signal_light::Mode::Off);
} else if (mode == "manual") {
// strtoul over the raw hex, so an unparseable colour lands on 0 (black)
// rather than anything undefined.
const uint32_t rgb = strtoul(server.arg("color").c_str(), nullptr, 16) & 0xFFFFFF;
signal_light::setManualColor(rgb);
} else {
server.send(400, "text/plain", "unknown mode");
return;
}
group_sync::publishMode();
sendState();
}
void handlePreset() {
const String name = server.arg("preset");
if (name == "exam") {
settings::applyPreset(settings::Preset::Exam);
} else if (name == "quiet") {
settings::applyPreset(settings::Preset::QuietWork);
} else if (name == "group") {
settings::applyPreset(settings::Preset::GroupWork);
} else {
server.send(400, "text/plain", "unknown preset");
return;
}
group_sync::publishSettings();
sendState();
}
// Credentials are written straight to NVS and take effect on restart, which
// avoids having to tear down and rebuild the network stack underneath a live
// request.
void handleWifi() {
if (!server.hasArg("ssid")) {
server.send(400, "text/plain", "ssid required");
return;
}
settings::setWifiCredentials(server.arg("ssid").c_str(), server.arg("pass").c_str());
server.send(200, "application/json", "{\"restarting\":true}");
server.client().flush();
delay(200); // let the response reach the browser before the radio drops
ESP.restart();
}
void rememberAddress(const IPAddress& ip) {
strncpy(addressText, ip.toString().c_str(), sizeof(addressText) - 1);
addressText[sizeof(addressText) - 1] = '\0';
}
void enterState(Net next) {
netState = next;
stateSinceMs = millis();
}
// Kicks off a station attempt without waiting for it. Callers poll the state
// machine; nothing here may block, because tick() runs it.
bool startStationAttempt() {
const char* ssid = settings::wifiSsid();
if (ssid[0] == '\0') return false;
Serial.printf("wifi: joining %s\n", ssid);
WiFi.mode(WIFI_STA);
WiFi.setHostname(WIFI_HOSTNAME);
// Power save parks the radio between beacons, which silently drops incoming
// ESP-NOW packets. This is a mains-powered device, so the trade is easy.
WiFi.setSleep(false);
WiFi.begin(ssid, settings::wifiPassword());
enterState(Net::Connecting);
return true;
}
bool startAccessPoint() {
WiFi.mode(WIFI_AP);
WiFi.setHostname(WIFI_HOSTNAME);
// An empty password yields an open network, which softAP expects as nullptr.
const char* password = WIFI_AP_PASSWORD[0] ? WIFI_AP_PASSWORD : nullptr;
WiFi.setSleep(false);
// Pinned rather than left to the default so that several units falling back
// to their own access points still share a channel, which is what lets them
// hear each other over ESP-NOW.
if (!WiFi.softAP(WIFI_AP_SSID, password, WIFI_AP_CHANNEL)) {
Serial.println(F("wifi: could not start the access point"));
return false;
}
accessPointMode = true;
rememberAddress(WiFi.softAPIP());
group_sync::setAddress(uint32_t(WiFi.softAPIP()));
enterState(Net::Ap);
// Same reason as onStationUp(): the ESP-NOW peer is bound to an interface
// when it is added, so changing mode leaves it pointing at one that is no
// longer carrying traffic. Both directions of the switch rebuild it.
group_sync::begin();
Serial.print(F("wifi: access point "));
Serial.print(WIFI_AP_SSID);
Serial.print(F(", http://"));
Serial.println(WiFi.softAPIP());
return true;
}
void onStationUp() {
accessPointMode = false;
rememberAddress(WiFi.localIP());
enterState(Net::Connected);
Serial.print(F("wifi: connected, http://"));
Serial.println(addressText);
group_sync::setAddress(uint32_t(WiFi.localIP()));
MDNS.end();
if (MDNS.begin(WIFI_HOSTNAME)) MDNS.addService("http", "tcp", WEB_SERVER_PORT);
// The channel almost certainly changed, which invalidates the ESP-NOW
// broadcast peer, so the group is rebuilt on the new one.
group_sync::begin();
}
// Called from tick(). Every branch returns promptly; none of them wait.
void maintainNetwork() {
const uint32_t now = millis();
switch (netState) {
case Net::Connecting:
if (WiFi.status() == WL_CONNECTED) {
onStationUp();
} else if (now - stateSinceMs >= WIFI_CONNECT_TIMEOUT_MS) {
Serial.println(F("wifi: timed out, falling back to the access point"));
WiFi.disconnect(true);
startAccessPoint();
}
break;
case Net::Connected:
if (WiFi.status() != WL_CONNECTED) {
Serial.println(F("wifi: connection dropped"));
enterState(Net::Dropped);
} else if (WiFi.localIP().toString() != String(addressText)) {
// A lease change would otherwise leave the screen and the web page
// advertising an address that no longer reaches the unit.
rememberAddress(WiFi.localIP());
Serial.print(F("wifi: address is now "));
Serial.println(addressText);
}
break;
case Net::Dropped:
// The core retries on its own; this is the point at which we stop
// believing it and make the unit reachable some other way.
if (WiFi.status() == WL_CONNECTED) {
onStationUp();
} else if (now - stateSinceMs >= WIFI_FALLBACK_AFTER_MS) {
startAccessPoint();
}
break;
case Net::Ap:
if (settings::wifiSsid()[0] == '\0') break;
if (now - stateSinceMs < WIFI_RETRY_INTERVAL_MS) break;
// Retrying tears the access point down, so not while someone is on it.
if (WiFi.softAPgetStationNum() > 0) {
enterState(Net::Ap); // reset the timer; try again once they leave
break;
}
startStationAttempt();
break;
}
}
} // namespace
bool begin() {
// Establish a known state rather than relying on static initialisation, so
// begin() is a real reset point.
running = false;
addressText[0] = '\0';
latest = {0.0f, sound_level::Quality::BelowNoiseFloor};
// Blocking here is fine: nothing else is running yet, and a unit that
// reaches its network at boot should be on it before the first frame.
if (startStationAttempt()) {
while (netState == Net::Connecting) {
maintainNetwork();
signal_light::tick();
if (netState == Net::Connecting) delay(20);
}
} else if (!startAccessPoint()) {
return false;
}
if (netState == Net::Ap && addressText[0] == '\0' && !startAccessPoint()) return false;
if (MDNS.begin(WIFI_HOSTNAME)) {
MDNS.addService("http", "tcp", WEB_SERVER_PORT);
Serial.printf("wifi: also at http://%s.local/\n", WIFI_HOSTNAME);
}
server.on("/", HTTP_GET, handleRoot);
server.on("/api/state", HTTP_GET, sendState);
server.on("/api/version", HTTP_GET, sendVersion);
server.on("/api/set", HTTP_POST, handleSet);
server.on("/api/mode", HTTP_POST, handleMode);
server.on("/api/test", HTTP_POST, handleTest);
server.on("/api/group", HTTP_POST, handleGroup);
server.on("/api/peer", HTTP_POST, handlePeer);
server.on("/api/preset", HTTP_POST, handlePreset);
server.on("/api/wifi", HTTP_POST, handleWifi);
#if FEATURE_OTA
ota::registerRoutes(server);
#endif
// Anything else redirects to the page, so a captive-portal probe or a
// mistyped path still lands somewhere useful.
server.onNotFound(handleRoot);
server.begin();
running = true;
return true;
}
const char* address() { return addressText; }
void tick() {
if (!running) return;
server.handleClient();
maintainNetwork();
}
void publishLevel(const sound_level::Reading& reading) { latest = reading; }
} // namespace web_control
#endif // FEATURE_WIFI