-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
323 lines (256 loc) · 7.1 KB
/
main.cpp
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
#include <fcntl.h>
#include <unistd.h>
#include <util.h>
#include <csignal>
#include <sched.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sys/gpio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <poll.h>
#include <netinet/in.h>
#include <syslog.h>
#include <cstring>
#include <stdexcept>
static bool done = false;
static void quit(int)
{
done = true;
}
static uint64_t get_time()
{
timespec timebase;
if (-1 == clock_gettime(CLOCK_MONOTONIC, &timebase))
throw(std::runtime_error("can't get time from CLOCK_MONOTONIC"));
return timebase.tv_sec * 1000 + timebase.tv_nsec / 1000000;
}
static void sleep_until(uint64_t const timebase)
{
timespec ts;
int result;
ts.tv_sec = timebase / 1000;
ts.tv_nsec = (timebase % 1000) * 1000000;
do {
result = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, &ts);
// If the function returns 0, the full amount of time has
// elapsed. If it's greater than 0, a signal interrupted the
// timeout so simply go back and wait for the remainder of
// time. A -1 should never happen and means something is
// seriously wrong.
if (result == -1)
throw std::runtime_error("clock_nanosleep returned an error");
} while (result > 0);
}
class State {
uint64_t last_stamp;
bool last_value;
int const s_listen;
int s_client;
int const h_gpio;
static int create_listener()
{
int const s = socket(PF_INET, SOCK_STREAM, 0);
if (s == -1)
throw std::runtime_error("couldn't open listener socket");
try {
sockaddr_in addr;
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(10000);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1)
throw std::runtime_error("couldn't bind listener socket");
int const flags = fcntl(s, F_GETFL);
if (flags == -1)
throw std::runtime_error("couldn't get flags on socket");
if (fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
throw std::runtime_error("couldn't set flags on socket");
if (listen(s, 1) == -1)
throw std::runtime_error("couldn't listen on socket");
return s;
}
catch (...) {
close(s);
throw;
}
}
static int open_gpio()
{
static char const dev_name[] = "/dev/gpio0";
int const gpio = open(dev_name, O_RDWR);
if (gpio >= 0)
return gpio;
throw std::runtime_error("couldn't open GPIO device");
}
void set_client(bool const v)
{
struct gpio_req req;
std::memset(&req, 0, sizeof(req));
req.gp_pin = 18;
req.gp_value = v ? 0 : 1;
ioctl(h_gpio, GPIOWRITE, &req);
}
void set_activity(bool const v)
{
struct gpio_req req;
std::memset(&req, 0, sizeof(req));
req.gp_pin = 17;
req.gp_value = v ? 0 : 1;
ioctl(h_gpio, GPIOWRITE, &req);
}
bool read_pin() const
{
struct gpio_req req;
std::memset(&req, 0, sizeof(req));
req.gp_pin = 4;
if (ioctl(h_gpio, GPIOREAD, &req) == -1)
throw(std::runtime_error("can't read 'sump' pin state"));
return !req.gp_value;
}
void send_state()
{
if (s_client != -1 && last_stamp != 0) {
uint8_t buf[12];
buf[0] = last_stamp >> 56;
buf[1] = last_stamp >> 48;
buf[2] = last_stamp >> 40;
buf[3] = last_stamp >> 32;
buf[4] = last_stamp >> 24;
buf[5] = last_stamp >> 16;
buf[6] = last_stamp >> 8;
buf[7] = last_stamp;
buf[8] = buf[9] = buf[10] = 0;
buf[11] = last_value;
if (send(s_client, buf, sizeof(buf), MSG_NOSIGNAL) != sizeof(buf)) {
syslog(LOG_WARNING, "couldn't send to client ... "
"closing connection");
set_client(false);
close(s_client);
s_client = -1;
}
}
}
void print_addr(char buf[22], uint32_t const addr, uint16_t const port)
{
snprintf(buf, 22, "%d.%d.%d.%d:%d", uint8_t(addr >> 24),
uint8_t(addr >> 16), uint8_t(addr >> 8), uint8_t(addr), port);
}
void check_for_clients()
{
sockaddr_in addr;
socklen_t len;
int const s = accept(s_listen, reinterpret_cast<sockaddr*>(&addr), &len);
if (s != -1) {
set_client(true);
if (s_client != -1)
close(s_client);
int const val = 1;
if (-1 == setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &val,
sizeof(val)))
syslog(LOG_WARNING, "couldn't shut off EPIPE ... "
"KEEPALIVE will stay off");
else if (-1 == setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
&val, sizeof(val)))
syslog(LOG_WARNING, "couldn't enable KEEPALIVE");
s_client = s;
send_state();
char buf[22];
print_addr(buf, ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port));
syslog(LOG_INFO, "new client: %s", buf);
} else if (s_client != -1) {
pollfd fds;
fds.fd = s_client;
fds.events = POLLIN;
if (poll(&fds, 1, 0) > 0) {
char buffer[32];
if (recv(s_client, buffer, sizeof(buffer),
MSG_PEEK | MSG_DONTWAIT) == 0) {
close(s_client);
s_client = -1;
set_client(false);
}
}
}
}
public:
State() :
last_stamp(0), last_value(false), s_listen(create_listener()),
s_client(-1), h_gpio(open_gpio())
{
set_client(false);
set_activity(false);
}
~State()
{
set_client(false);
set_activity(false);
if (s_client != -1)
close(s_client);
close(s_listen);
close(h_gpio);
}
char const* pump_state() const { return last_value ? "on" : "off"; }
void update(uint64_t const stamp)
{
set_activity(true);
bool const current = read_pin();
if (last_value != current || !last_stamp) {
last_stamp = stamp;
last_value = current;
#if 0
syslog(LOG_INFO, "state: %s, @ts: %llu", pump_state(), stamp);
#endif
send_state();
}
check_for_clients();
if (!current) {
sleep_until(stamp + 20);
set_activity(false);
}
}
};
static uint32_t const delta = 50000000;
int main(int, char**)
{
// Turn into a background process. First call `daemon` to go in
// the background. Then open a connection to `syslog`. Next,
// create the PID file that the init.s framework wants to
// see. Finally, set the user ID to 'drmem'.
#if defined(NDEBUG)
if (-1 == daemon(0, 0))
return 1;
openlog("sump", LOG_NDELAY, LOG_DAEMON);
if (-1 == pidfile(0))
syslog(LOG_WARNING, "couldn't create PID file -- %m");
if (-1 == mlockall(MCL_CURRENT | MCL_FUTURE))
syslog(LOG_WARNING, "couldn't lock memory -- %m");
sched_param param;
param.sched_priority = sched_get_priority_min(SCHED_RR);
if (-1 == sched_setscheduler(0, SCHED_RR, ¶m))
syslog(LOG_WARNING, "couldn't use real-time scheduling -- %m");
if (-1 == seteuid(10000))
syslog(LOG_WARNING, "couldn't become `drmem` -- %m");
#else
openlog("sump", LOG_PERROR | LOG_NLOG | LOG_NDELAY, LOG_DAEMON);
#endif
signal(SIGINT, quit);
signal(SIGTERM, quit);
// Now we're in the main guts of the process.
try {
State state;
uint64_t timebase = get_time();
syslog(LOG_INFO, "initial time: %llu", timebase);
while (!done) {
timebase += 50;
sleep_until(timebase);
state.update(timebase);
}
syslog(LOG_INFO, "terminating");
return 0;
}
catch (std::exception const& e) {
syslog(LOG_ERR, "ERROR: %s", e.what());
}
}