-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
761 lines (724 loc) · 28.9 KB
/
Copy pathmain.cpp
File metadata and controls
761 lines (724 loc) · 28.9 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fmt/format.h>
#include <fstream>
#include <iomanip>
#include <set>
#include "json.hpp"
using json = nlohmann::json;
#include "dmx.h"
#include "event.h"
#include "radiora2.h"
#include "relay.h"
#include "util.h"
#include "ws.h"
// Monitor the health of our process by sending regular heart beats. If the
// heartbeats stop, kill the child process and restart it from the parent.
static int childFd[2] = { -1, -1 };
static bool initialized = false;
static void setDMX(DMX& dmx, const json& dimmer, int level, bool fade) {
// Apply a dimmer curve and low trim level. Also, fade the color temperature.
static std::map<int, int> early;
if (initialized && !early.empty()) {
for (const auto& [id, v] : early) {
dmx.set(id, v, false);
}
early.clear();
}
unsigned offset = !!(dimmer.size() > 0 && dimmer[0].is_number());
const auto& ids = dimmer.size() > offset ? dimmer[offset] : "[]"_json;
const auto& curve = dimmer.size() > offset+1 ? dimmer[offset+1] : "[]"_json;
const auto& trim = dimmer.size() > offset+2 ? dimmer[offset+2] : "0"_json;
for (unsigned i = 0; i < (ids.is_array() ? ids.size() : 0); i++) {
if (!ids[i].is_number()) continue;
int id = ids[i].get<int>();
if (id <= 0 || id > 512) continue;
double exp = curve.is_array() && curve.size() > i && curve[i].is_number()
? curve[i].get<double>() : 1.0;
double t = trim.is_number() ? trim.get<double>() : 0;
int v = pow((level*(100.0-t)/100.0+t)/10000, exp)*255;
if (initialized) {
dmx.set(id, v, fade);
} else {
early[id] = v;
}
}
}
static void readLine(RadioRA2& ra2, DMX& dmx, Relay& relay,
const std::string& line, const std::string& context,
bool fade) {
DBG("readLine(\"" << line << "\", \"" << context << "\")");
if (Util::starts_with(line, "~OUTPUT,")) {
// When an output device changes levels, we expect a line of the form
// "~OUTPUT,<dev>,1,<level>". If this was a dummy device that stands in for
// a DMX load, the user can specify the DMX info in the device name.
// This makes the "site.json" file unnecessary and allows the user to
// design their entire system inside of the Lutron software.
// The additional information needed is provided to us in the "context".
// A ":" after the device name includes the JSON string that we
// subsequently need to pass to setDMX().
auto comma = strchr(&line[8], ',');
if (!memcmp(",1,", comma, 3)) {
// Check whether the "context" references a DMX load.
auto args = context.find(':');
if (args != std::string::npos) {
// Lutron outputs the level as a number in the range 0..100 with two
// decimals precision. Convert it to an integer in the range 0..10000.
int level = 100*atoi(comma + 3);
auto decimal = strchr(comma + 3, '.');
if (decimal && decimal[1] >= '0' && decimal[1] <= '9') {
level += 10*(decimal[1] - '0');
if (decimal[2] >= '0' && decimal[2] <= '9') {
level += decimal[2] - '0';
}
}
if (context[args + 1] == '[') {
DBG("Found in-line DMX info");
setDMX(dmx, json::parse("[" + context.substr(args + 1) + "]"),
std::min(std::max(0, level), 10000), fade);
} else if (initialized) {
// Some dimmers are supposed to be darker at night and brighter
// during the day. A ":<low>/<high>/<from>-<to>" parameter can
// override the Lutron defaults.
char *endptr;
errno = 0;
auto low = strtol(&context[args + 1], &endptr, 0);
auto hi = strtol(*endptr ? endptr + 1 : "", &endptr, 0);
auto from = strtol(*endptr ? endptr + 1 : "", &endptr, 0);
auto to = strtol(*endptr ? endptr + 1 : "", &endptr, 0);
if (!errno && low >= 0 && low <= 100 && hi >= 0 && hi <= 100 &&
from >= 0 && from <= 2400 && to >= 0 && to <= 2400 &&
level > 150 && abs(level - hi*100) > 250 &&
(abs(level - low*100) < 200 || level - 750 < low*100)) {
int now = Util::timeOfDay();
if ((now >= from && now < to) == (to > from)) {
static std::map<int, int> suppress;
int id = atoi(std::string(line, 8, comma-&line[8]).c_str());
const auto it = suppress.find(id);
if (it == suppress.end() || Util::millis() - it->second > 2000) {
ra2.command(fmt::format("#OUTPUT,{},1,{}.00", id, hi));
}
suppress[id] = Util::millis();
}
}
}
}
}
} else if (Util::starts_with(line, "~DEVICE,") &&
Util::ends_with(line, ",3")) {
const auto dev = atoi(&line[8]);
// Pico remotes aren't output devices, but we can track their buttons and
// make them behave like virtual key events for a keypad. Again, this
// information can be encoded using the Pico label.
// Button presses will be reported with a line of the form
// "~DEVICE,<pico>,<button>,3".
// We also look at keypads, and interpret any in-line information as
// instructions when to toggle relay outputs.
auto args = context.find(':');
if (args != std::string::npos) {
switch (ra2.deviceType(dev)) {
case RadioRA2::DEV_PICO_KEYPAD: {
auto json = json::parse("[" + context.substr(args + 1) + "]");
switch (json.size()) {
case 1: // This button controls an output and behaves like "TOGGLE"
// directive in a "site.json" file.
ra2.toggleOutput(json[0].get<int>());
break;
case 2:{// This button forwards the button press to a different keypad,
// and behaves like the "DEVICE" directive in a "site.json" file.
const int otherKp = json[0].get<int>();
const int otherBt = json[1].get<int>();
ra2.command(fmt::format("#DEVICE,{},{},3", otherKp, otherBt));
ra2.command(fmt::format("#DEVICE,{},{},4", otherKp, otherBt));
break; }
default:
break;
}
break; }
case RadioRA2::DEV_SEETOUCH_KEYPAD:
case RadioRA2::DEV_HYBRID_SEETOUCH_KEYPAD: {
std::string cond = Util::trim(context.substr(args + 1));
const bool sense = !(cond.size() > 0 && cond[0] == '!');
if (!sense) {
cond.erase(0, 1);
}
auto comma = cond.find(',');
int condPin = -1;
if (comma != std::string::npos) {
condPin = atoi(cond.c_str());
cond = Util::trim(cond.substr(comma + 1));
}
char *endptr;
int actionPin = (int)strtoul(cond.c_str(), &endptr, 10);
if (condPin < 0 || relay.get(condPin) == sense) {
bool slow = false;
for (; *endptr; ++endptr) slow |= (*endptr == 'S');
relay.toggle(actionPin, slow);
}
break; }
default:
break;
}
}
}
}
static void runScript(Event& event, RadioRA2& ra2, const std::string& script) {
ra2.updateEnvironment();
// Create a non-blocking pipe for the script's output
int pipefd[2];
if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)) {
DBG("Failed to create pipe");
return;
}
pid_t pid = fork();
if (pid == 0) {
// Child process
close(pipefd[0]); // Close the read end
dup2(pipefd[1], 1); // Redirect stdout to the pipe
close(pipefd[1]); // Close the original write end
// Unblock signals! The daemon blocked them, but the script
// needs them to function correctly (e.g. ctrl+c, SIGTERM).
sigset_t mask;
sigemptyset(&mask);
sigprocmask(SIG_SETMASK, &mask, nullptr);
// Replace process with the shell
execl("/bin/sh", "sh", "-c", script.c_str(), (char *)nullptr);
_exit(127); // If exec fails
} else if (pid > 0) {
// Parent process
close(pipefd[1]); // Close the write end so we get EOF when child is done
// Monitor the pipe for output asynchronously
event.addPollFd(pipefd[0], POLLIN,
[&ra2, fd = pipefd[0], buf = std::string()](pollfd *pfd) mutable {
char buffer[1024];
ssize_t rc = read(fd, buffer, sizeof(buffer));
if (rc > 0) {
// Data received: Buffer it and extract complete lines
buf.append(buffer, rc);
size_t pos;
while ((pos = buf.find('\n')) != std::string::npos) {
std::string line = Util::trim(buf.substr(0, pos));
if (!line.empty()) {
ra2.command(line);
}
buf.erase(0, pos + 1);
}
return true;
} else if (rc < 0 &&
(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) {
// No data ready, keep listening
return true;
} else {
// EOF or error. The script finished or crashed. Process any final
// lingering data.
std::string line = Util::trim(buf);
if (!line.empty()) {
ra2.command(line);
}
// Stop monitoring this FD. Note that we do not call waitpid() here.
// The main loop handles SIGCHLD.
close(fd);
return false;
}
});
} else {
close(pipefd[0]);
close(pipefd[1]);
DBG("Failed to fork script process");
}
}
static void augmentConfig(Event& event, const json& site, RadioRA2& ra2,
DMX& dmx, Relay& relay) {
// Out of the box, our code does not implement any policy and won't really
// change the behavior of the Lutron device. But given a "site.json"
// configuration file, it can integrate non-Lutron devices into the
// existing RadioRA2 system.
// Iterate over all "DMX" object definitions and add virtual outputs
// for DMX fixtures that are represented by dummy objects in the
// Lutron system.
if (site.contains("DMX")) {
const auto& dmxIds = site["DMX"];
for (const auto& [name, params] : dmxIds.items()) {
if (!params.is_array() || params.size() <= 0 || !params[0].is_number()) {
continue;
}
ra2.addOutput(
fmt::format("{}{}", RadioRA2::DMXALIAS, params[0].get<int>()),
[&dmx, params](int level, bool fade) {
setDMX(dmx, params, level, fade);
});
}
}
// Iterate over all "WATCH" objects and attach actions that should
// trigger when an output changes.
if (site.contains("WATCH")) {
const auto& watch = site["WATCH"];
for (const auto& [id_, script] : watch.items()) {
if (id_ == "TIMECLOCK") {
ra2.monitorTimeclock([&event, &ra2, &script](const std::string& s) {
unsetenv("KEYPAD");
unsetenv("BUTTON");
unsetenv("ON");
unsetenv("LONG");
unsetenv("NUMTAPS");
unsetenv("OUTPUT");
unsetenv("LEVEL");
unsetenv("level");
setenv("TIMECLOCK", s.c_str(), 1);
runScript(event, ra2, script);
});
} else {
const auto id = atoi(id_.c_str());
ra2.monitorOutput(id, [id, &event, &ra2, &script](int level) {
unsetenv("KEYPAD");
unsetenv("BUTTON");
unsetenv("ON");
unsetenv("LONG");
unsetenv("NUMTAPS");
unsetenv("TIMECLOCK");
setenv("OUTPUT", fmt::format("{}", id).c_str(), 1);
setenv("LEVEL",
fmt::format("{}.{:02}", level/100, level%100).c_str(), 1);
setenv("level", fmt::format("{}", level).c_str(), 1);
runScript(event, ra2, script);
});
}
}
}
// The I2C object allows us to define virtual GPIO pins that need to be
// addressed through an I2C bus instead.
if (site.contains("I2C")) {
const auto& i2c = site["I2C"];
for (const auto& [id_, def] : i2c.items()) {
const auto id = atoi(id_.c_str());
relay.i2c(id, def["BUS"].get<int>(), def["DEV"].get<int>(),
def["ADDR"].get<int>(), def["BIT"].get<int>());
}
}
// Iterate over all "KEYPAD" object definitions and add new assignments
// to the various keypad buttons.
if (site.contains("KEYPAD")) {
const auto& keypad = site["KEYPAD"];
for (const auto& [kp, buttons] : keypad.items()) {
for (const auto& [bt, actions] : buttons.items()) {
for (const auto& [at, rule] : actions.items()) {
if (at == "DMX" && site.contains("DMX")) {
// Register DMX light fixtures with the "RadioRA2" object.
// This is the most fundamental feature that we implement. It
// makes DMX light fixtures behave just the same as native
// Lutron output devices.
for (const auto& [output, level] : rule.items()) {
const auto& dmxIds = site["DMX"];
const auto& params = dmxIds.find(output);
if (params == dmxIds.end()) {
DBG("Cannot find DMX fixture \"" << output << "\"");
continue;
}
ra2.addToButton(
atoi(kp.c_str()), atoi(bt.c_str()),
ra2.addOutput(output,
[&, dimmer = *params](int level, bool fade) {
setDMX(dmx, dimmer, level, fade);
}),
level.get<int>());
}
} else if (at == "TOGGLE") {
// Some devices (e.g. Pico remote) have artificial constraints,
// forcing a button to enable a scene instead of allowing it to
// be a toggle button. For these buttons, we don't assign any
// fixtures in the Lutron controller and instead implement the
// toggle function ourselves. This works by aliasing the physical
// output device to a virtual copy that can be attached to a
// callback.
for (const auto& out : rule) {
ra2.addToButton(
atoi(kp.c_str()), atoi(bt.c_str()),
ra2.addOutput(
fmt::format("{}{}", RadioRA2::ALIAS, out.get<int>()),
[&, out](int level, auto) {
ra2.command(fmt::format(
"#OUTPUT,{},1,{}.{:02}",
out.get<int>(), level/100, level%100));
}), 100, true);
}
} else if (at == "DEVICE") {
// An alternative way to achieve a similar goal is for the
// Pico remote to simulate a button press on a different keypad.
const int otherKp = rule[0].get<int>();
const int otherBt = rule[1].get<int>();
ra2.addToButton(
atoi(kp.c_str()), atoi(bt.c_str()),
ra2.addOutput(fmt::format("DEV:{}/{}", otherKp, otherBt),
[&, otherKp, otherBt](auto, auto) {
ra2.command(fmt::format("#DEVICE,{},{},3",otherKp,otherBt));
ra2.command(fmt::format("#DEVICE,{},{},4",otherKp,otherBt));
}), 0);
} else if (at == "SCRIPT") {
// Sometimes, none of the built-in rules can do the job. Branch out
// to a helper script instead.
auto script = rule.get<std::string>();
if (!script.empty()) {
ra2.addButtonListener(
atoi(kp.c_str()), atoi(bt.c_str()),
[script, &event, &ra2](
int kp, int bt, bool on, bool isLong, int num) {
unsetenv("TIMECLOCK");
unsetenv("OUTPUT");
unsetenv("LEVEL");
setenv("KEYPAD", fmt::format("{}", kp).c_str(), 1);
setenv("BUTTON", fmt::format("{}", bt).c_str(), 1);
setenv("ON", fmt::format("{}", on).c_str(), 1);
if (isLong) setenv("LONG", "1", 1);
else unsetenv("LONG");
if (num) setenv("NUMTAPS", fmt::format("{}", num).c_str(), 1);
else unsetenv("NUMTAPS");
runScript(event, ra2, script);
});
}
} else if (at == "RELAY" && site.contains("GPIO")) {
// We can control GPIO inputs and outputs that frequently have
// relays attached. Currently, only momentary push buttons are
// implemented for output pins. But that could be extended as
// needed.
// A GPIO rule is a two-element vector that specifies a
// prerequisite condition (i.e. a GPIO input), and an action to
// take (i.e. a GPIO output). The condition can be omitted by
// using an empty string. And it can be inverted by either
// preceding the GPIO definition or the rule definition with a
// "!".
auto cond = rule[0].get<std::string>();
const auto& action = rule[1].get<std::string>();
bool sense = !(cond.size() > 0 && cond[0] == '!');
if (!sense) {
// Invert the sense of the GPIO input for this rule only.
cond.erase(0, 1);
}
const auto& gpio = site["GPIO"];
int condPin = -1;
if (!cond.empty()) {
// Globally invert the sense of this GPIO pin. This becomes
// more complicated, as the JSON implementation doesn't fully
// behave like STL containers, so we can't use std::find_if().
for (auto& [k,v] : gpio.items()) {
if (k.rfind(cond, k[0] == '!') <= 1) {
condPin = v; sense ^= k[0] == '!'; break;
}
}
}
// Also look up the output pin that should be toggled. We detect
// any flags that might be present.
int actionPin = -1;
bool slow = false;
for (auto& [k,v] : gpio.items()) {
auto flags = std::find(k.begin(), k.end(), '/');
if (std::string(k.begin(), flags) == action) {
actionPin = v.get<int>();
for (; flags != k.end(); ++flags) {
slow |= (*flags == 'S');
}
break;
}
}
// If we were able to successfully parse the GPIO rule, set up
// a callback function that will be invoked any time the user
// pushes a button on the keypad.
if ((cond.empty() || condPin >= 0) && actionPin >= 0) {
ra2.addToButton(
atoi(kp.c_str()), atoi(bt.c_str()),
ra2.addOutput(fmt::format("RELAY:{}/{}", condPin, actionPin),
[&, sense, condPin, actionPin, slow](auto, auto) {
if (condPin < 0 || relay.get(condPin) == sense) {
relay.toggle(actionPin, slow);
}
}), -1);
} else {
DBG("Cannot parse GPIO rule");
}
} else {
DBG("Unknown event type: " << at);
}
}
}
}
}
}
static void updateUI(WS* ws, Event& event, int kp, int led,
bool state, int level) {
if (!ws) {
return;
}
static std::map<std::pair<int, int>, std::pair<bool, int>> cache;
if (!cache.size()) {
// Batch multiple updates into a single broadcast message.
event.addTimeout(100, [ws]() {
std::string s;
for (const auto& [ k, v ] : cache) {
s += fmt::format("{},{},{},{}.{:02} ",
k.first, k.second, (int)v.first, v.second/100, v.second%100);
}
s.pop_back();
cache.clear();
ws->broadcast(s);
});
}
cache[std::make_pair(kp, led)] = std::make_pair(state, level);
}
static void dmxRemoteServer(Event& event) {
#ifndef NDEBUG
// By setting the DMXSERVER environment variable to an empty string, we
// become a proxy for DMX requests.
const char *dmxsrv = getenv("DMXSERVER");
if (dmxsrv && !*dmxsrv) {
DBG("Running in remote server mode");
DMX dmx(event);
event.loop();
exit(0);
}
#endif
}
static std::vector<int> keypadOrder(const json& site, const RadioRA2& ra2) {
// The "KEYPAD ORDER" parameter is optional and sets a prefered display
// order for the keypads in the web UI.
std::vector<int> order;
if (site.contains("KEYPAD ORDER")) {
for (const auto& kp : site["KEYPAD ORDER"]) {
if (kp.is_string()) {
int id = ra2.getKeypad(kp.get<std::string>());
if (id >= 0) {
order.push_back(id);
}
} else if (kp.is_number()) {
order.push_back(kp.get<int>());
}
}
}
return order;
}
static void server() {
// Read the "site.json" file, if present. Some of the data will be needed
// early to initialize global state. Other data will be used at a later point
// to augment the information that we retrieve from the Lutron controller.
json site("{}"_json);
const std::string& fname = "site.json";
{
std::ifstream ifs(fname);
if ((ifs.rdstate() & std::ifstream::failbit) != 0) {
DBG("Failed to read \"" << fname << "\"");
} else {
json cfg = json::parse(ifs, nullptr, false, true);
if (cfg.is_discarded()) {
DBG("Failed to parse \"" << fname << "\"");
} else {
site = std::move(cfg);
}
}
}
// Create all the different objects that make up our server and connect
// them to each other. Then enter the event loop.
Event event;
// Block signals so they can be handled via the file descriptor
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT); // Handle ctrl+c gracefully too
sigaddset(&mask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0) {
DBG("Failed to block signals");
}
// Create a file descriptor for signals
int sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
if (sfd >= 0) {
event.addPollFd(sfd, POLLIN, [&](pollfd* pfd) {
struct signalfd_siginfo fdsi;
if (read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi)) {
if (fdsi.ssi_signo == SIGCHLD) {
while (waitpid(-1, nullptr, WNOHANG) > 0) { }
} else {
DBG("Received signal " << fdsi.ssi_signo << ", exiting...");
event.exitLoop(); // This triggers the graceful shutdown
}
}
return true;
});
}
dmxRemoteServer(event); // For debugging purposes only
DBG("Starting...");
DMX dmx(
event,
site.contains("DMX SERIAL") ? site["DMX SERIAL"].get<std::string>() : "");
Relay relay(event);
WS *ws = nullptr;
RadioRA2 ra2(
event, site.contains("REPEATER") ? site["REPEATER"].get<std::string>() : "",
site.contains("USER") ? site["USER"].get<std::string>() : "",
site.contains("PASSWORD") ? site["PASSWORD"].get<std::string>() : "");
ra2.oninit([&]() {
augmentConfig(event, site, ra2, dmx, relay); initialized = true;})
.oninput([&](const std::string& line, const std::string&context,bool fade){
readLine(ra2, dmx, relay, line, context, fade); })
.onledstate([&](int kp, int led, bool state, int level) {
updateUI(ws, event, kp, led, state, level); })
// Communicate with parent process. This allows the watchdog
// to kill us, if we become unresponsive. And it also allows
// us to request a restart, if the automation schema changed
// unexpectedly.
.onheartbeat([](){ if (childFd[1] >= 0 && write(childFd[1], "", 1));})
.onschemainvalid([](){ if (childFd[1] < 0 || !write(childFd[1], "\1", 1)) {
DBG("Stale cached data"); _exit(1);}});
WS ws_(&event,
site.contains("HTTP PORT") ? site["HTTP PORT"].get<int>() : 8080);
ws_.onkeypadreq([&]() { return ra2.getKeypads(keypadOrder(site, ra2)); })
.oncommand([&](const std::string& s) { ra2.command(s); });
ws = &ws_;
event.loop();
}
int main(int argc, char *argv[]) {
// Check command line arguments
bool monitor = true;
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "--no-monitor") || !strcmp(argv[i], "-d")) {
monitor = false;
}
}
// If requested, run in standalone mode (no watchdog).
// This is useful for debugging with gdb.
if (!monitor) {
server();
return 0;
}
// In production mode (and now by default in debug mode too), wrap server
// with a helper process that restarts in case of unexpected crashes,
// missed heartbeat signals, or when the schema changes.
for (;;) {
// Communication pipe between parent and child process.
if (childFd[0] >= 0) close(childFd[0]);
if (pipe2(childFd, O_CLOEXEC | O_NONBLOCK)) {
return 1;
}
const auto p = fork();
if (p == 0) {
close(childFd[0]);
server();
exit(0);
} else if (p > 0) {
// In the parent process, implement a watchdog timer that kills and
// restarts the child, if we don't see a regular heartbeat.
close(childFd[1]);
Event event;
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT);
// Parent also needs to block SIGCHLD so signalfd can catch it
sigaddset(&mask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0) {
DBG("Failed to block signals in parent");
}
int sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
bool stopping = false; // Flag indicating if we are intentionally stopping
if (sfd >= 0) {
event.addPollFd(sfd, POLLIN, [&](pollfd *pfd) {
struct signalfd_siginfo fdsi;
if (read(sfd, &fdsi, sizeof(fdsi)) == sizeof(fdsi)) {
if (fdsi.ssi_signo == SIGCHLD) {
// Child state changed. Do not reap it here. We need to exit the
// loop so the main waitpid() below can retrieve the exit status
event.exitLoop();
} else {
// SIGTERM or SIGINT
stopping = true;
// Forward the signal to the child to ensure it stops
kill(p, fdsi.ssi_signo);
// Do not exit the loop yet. We must wait for the child to close
// the pipe (childFd[0]), which happens when it exits.
// Force kill, if it doesn't exit within 5 seconds
event.addTimeout(5000, [p]() {
DBG("Child stuck, forcing SIGKILL");
kill(p, SIGKILL);
});
}
}
return true;
});
}
bool restart = false;
void *tmo = nullptr;
const auto resetTmo = [&]() {
event.removeTimeout(tmo);
tmo = event.addTimeout(120*1000, [&]() {
restart = true;
kill(p, SIGTERM);
// Force kill, if it doesn't exit within 5 seconds
event.addTimeout(5000, [p]() { kill(p, SIGKILL); });
});
};
resetTmo();
// Use the event loop to keep track of heartbeats, requests for restart,
// and process termination (resulting in an EOF condition on the pipe).
for (;;) {
event.removePollFd(childFd[0]);
event.addPollFd(childFd[0], POLLIN, [&](auto) {
char ch = 0;
ssize_t i = read(childFd[0], &ch, 1);
if (i == 1 || (i < 0 && (errno == EAGAIN || errno == EINTR))) {
if (ch) {
// Child requested to be restarted. This typically happens because
// the Lutron device changed the automation schema.
kill(p, SIGTERM);
event.addTimeout(5000, [p]() { kill(p, SIGKILL); });
restart = true;
} else {
// We received a heartbeat signal.
resetTmo();
return true;
}
}
// If the child exited (EOF), leave the event loop.
event.exitLoop();
return false;
});
event.loop();
int status = 0;
const auto rc = waitpid(p, &status, 0);
if (rc < 0) {
// If waitpid() failed unexpectedly, kill the child process.
if (errno == EINTR) { continue; }
if (errno != ECHILD) { kill(p, SIGKILL); }
return 1;
}
if (stopping) {
// We were asked to stop (SIGTERM/SIGINT), so we do not restart.
return 0;
}
if (WIFEXITED(status) && !WEXITSTATUS(status)) {
// If the child terminated normally (exit code 0), then so should we.
return 0;
} else if (restart || !WIFSIGNALED(status) || WCOREDUMP(status)) {
// If the child requested to be restarted, or if it crashed
// unexpectedly (non-zero exit or core dump), start a new instance.
// Note that straightforward SIGTERM/SIGKILL without core dump will
// not cause a restart unless "restart" flag was set, or we implement
// specific logic for that. Currently, if killed by external signal
// that is not core dump, we exit. This is reasonable behavior.
break;
} else {
// Any other signal suggests that the child was told to quit
// (e.g. the user pressed ctrl-c or systemctl sent SIGTERM directly
// to child).
// Exit now.
return 1;
}
}
} else {
// If we failed to fork() a child process, there isn't much else we
// can do.
return 1;
}
}
return 0;
}