]> mj.ucw.cz Git - arexx.git/blob - arexxd.c
TODO
[arexx.git] / arexxd.c
1 /*
2  *      Linux Interfece for Arexx Data Loggers
3  *
4  *      (c) 2011 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <math.h>
14 #include <time.h>
15 #include <getopt.h>
16 #include <syslog.h>
17 #include <signal.h>
18 #include <sys/stat.h>
19 #include <libusb-1.0/libusb.h>
20 #include <rrd.h>
21
22 #define LOG_PATH "/var/log/arexxd"
23
24 typedef unsigned char byte;
25 static libusb_context *usb_ctxt;
26 static libusb_device_handle *devh;
27
28 static int use_syslog;
29 static int debug_mode;
30 static int debug_packets;
31 static int debug_raw_data;
32
33 static void die(char *fmt, ...)
34 {
35         va_list args;
36         va_start(args, fmt);
37         if (use_syslog)
38                 vsyslog(LOG_CRIT, fmt, args);
39         else {
40                 vfprintf(stderr, fmt, args);
41                 fprintf(stderr, "\n");
42         }
43         va_end(args);
44         exit(1);
45 }
46
47 static void log_error(char *fmt, ...)
48 {
49         va_list args;
50         va_start(args, fmt);
51         if (use_syslog)
52                 vsyslog(LOG_ERR, fmt, args);
53         else {
54                 vfprintf(stderr, fmt, args);
55                 fprintf(stderr, "\n");
56         }
57         va_end(args);
58 }
59
60 static void log_info(char *fmt, ...)
61 {
62         va_list args;
63         va_start(args, fmt);
64         if (use_syslog)
65                 vsyslog(LOG_INFO, fmt, args);
66         else {
67                 vfprintf(stderr, fmt, args);
68                 fprintf(stderr, "\n");
69         }
70         va_end(args);
71 }
72
73 static void log_pkt(char *fmt, ...)
74 {
75         if (!debug_packets)
76                 return;
77         va_list args;
78         va_start(args, fmt);
79         vprintf(fmt, args);
80         va_end(args);
81 }
82
83 /*** RRD interface ***/
84
85 #define SLOT_SIZE 10                                    // 10 seconds per averaging slot
86 #define MAX_ARGS 20
87 #define MAX_ARG_SIZE 1024
88
89 static int arg_cnt;
90 static char *arg_ptr[MAX_ARGS+1];
91 static char arg_buf[MAX_ARG_SIZE];
92 static int arg_pos;
93
94 static void arg_new(void)
95 {
96         arg_cnt = 1;
97         arg_pos = 0;
98         arg_ptr[0] = "rrdtool";
99 }
100
101 static void arg_push(const char *fmt, ...)
102 {
103         if (arg_cnt >= MAX_ARGS)
104                 die("MAX_ARGS exceeded");
105         va_list va;
106         va_start(va, fmt);
107         int len = 1 + vsnprintf(arg_buf + arg_pos, MAX_ARG_SIZE - arg_pos, fmt, va);
108         if (arg_pos + len > MAX_ARG_SIZE)
109                 die("MAX_ARG_SIZE exceeded");
110         arg_ptr[arg_cnt++] = arg_buf + arg_pos;
111         arg_ptr[arg_cnt] = NULL;
112         arg_pos += len;
113 }
114
115 static void rrd_point(time_t t, int id, double val, char *unit)
116 {
117         char rr_name[256];
118         snprintf(rr_name, sizeof(rr_name), "sensor-%d.rrd", id);
119
120         struct stat st;
121         if (stat(rr_name, &st) < 0 || !st.st_size) {
122                 // We have to create the RRD
123                 log_info("Creating %s", rr_name);
124                 arg_new();
125                 arg_push(rr_name);
126                 arg_push("--start");
127                 arg_push("%d", (int) time(NULL) - 28*86400);
128                 arg_push("--step");
129                 arg_push("60");
130                 if (!strcmp(unit, "%RH"))
131                         arg_push("DS:rh:GAUGE:300:0:100");
132                 else if (!strcmp(unit, "ppm"))
133                         arg_push("DS:ppm:GAUGE:300:0:1000000");
134                 else
135                         arg_push("DS:temp:GAUGE:300:-200:200");
136                 arg_push("RRA:AVERAGE:0.25:1:20160");           // Last 14 days with full resolution
137                 arg_push("RRA:AVERAGE:0.25:60:88800");          // Last 10 years with 1h resolution
138                 arg_push("RRA:MIN:0.25:60:88800");              // including minima and maxima
139                 arg_push("RRA:MAX:0.25:60:88800");
140                 rrd_create(arg_cnt, arg_ptr);
141                 if (rrd_test_error()) {
142                         log_error("rrd_create on %s failed: %s", rr_name, rrd_get_error());
143                         return;
144                 }
145         }
146
147         arg_new();
148         arg_push(rr_name);
149         arg_push("%d:%f", t, val);
150         rrd_update(arg_cnt, arg_ptr);
151         if (rrd_test_error())
152                 log_error("rrd_update on %s failed: %s", rr_name, rrd_get_error());
153 }
154
155 /*** Transforms ***/
156
157 #define TIME_OFFSET 946681200           // Timestamp of 2000-01-01 00:00:00
158
159 static int data_point_counter;
160
161 static void cooked_point(time_t t, int id, double val, char *unit, int q)
162 {
163         if (debug_raw_data) {
164                 struct tm tm;
165                 localtime_r(&t, &tm);
166                 char tbuf[64];
167                 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
168                 printf("== %s id=%d val=%.3f unit=%s q=%d\n", tbuf, id, val, unit, q);
169         }
170
171         data_point_counter++;
172         rrd_point(t, id, val, unit);
173 }
174
175 static void raw_point(int t, int id, int raw, int q)
176 {
177         /*
178          *  The binary blob provided by Arexx contains an embedded XML fragment
179          *  with descriptions of all known sensor types. If you want to see it,
180          *  grep the blob for "<deviceinfo>". The meanings of the parameters are
181          *  as follows:
182          *
183          *      m1, m2          Device type matches if (raw_sensor_id & m1) == m2
184          *      type            Unit measured by the sensor (1=Celsius, 2=RH%, 3=CO2 ppm)
185          *      dm              User-visible sensor ID = raw_sensor_id & dm
186          *      i               1 if the raw value is signed
187          *      p[]             Coefficients of transformation polynomial (x^0 first)
188          *      vLo, vUp        Upper and lower bound on the final value
189          *      scale           Scaling function:
190          *                              0 = identity (default)
191          *                              1 = 10^x
192          *                              2 = exp(x)
193          *                              3 = (x < 0) ? 0 : log10(x)
194          *                              4 = (x < 0) ? 0 : log(x)
195          *
196          *  The raw values are transformed this way:
197          *      - sign-extend if signed
198          *      - apply the transformation polynomial
199          *      - apply the scaling function
200          *      - drop if outside the interval [vLo,vUp]
201          *
202          *  This function applies the necessary transform for sensors we've
203          *  seen in the wild. We deliberately ignore the "dm" parameter as we want
204          *  to report different channels of a single sensor as multiple sensors.
205          */
206
207         double z = raw;
208         double hi, lo;
209         char *unit;
210         int idhi = id & 0xf000;
211
212         if (idhi == 0x1000) {
213                 z = 0.02*z - 273.15;
214                 lo = -200;
215                 hi = 600;
216                 unit = "C";
217         } else if (idhi == 0x2000) {
218                 if (raw >= 0x8000)
219                         z -= 0x10000;
220                 z /= 128;
221                 lo = -60;
222                 hi = 125;
223                 unit = "C";
224         } else if (idhi == 0x4000) {
225                 if (!(id & 1)) {
226                         z = z/100 - 39.6;
227                         lo = -60;
228                         hi = 125;
229                         unit = "C";
230                 } else {
231                         z = -2.8e-6*z*z + 0.0405*z - 4;
232                         lo = 0;
233                         hi = 100.1;
234                         unit = "%RH";
235                 }
236         } else if (idhi == 0x6000) {
237                 if (!(id & 1)) {
238                         if (raw >= 0x8000)
239                                 z -= 0x10000;
240                         z /= 128;
241                         lo = -60;
242                         hi = 125;
243                         unit = "C";
244                 } else {
245                         z = -3.8123e-11*z;
246                         z = (z + 1.9184e-7) * z;
247                         z = (z - 1.0998e-3) * z;
248                         z += 6.56;
249                         z = pow(10, z);
250                         lo = 0;
251                         hi = 1e6;
252                         unit = "ppm";
253                 }
254         } else {
255                 log_error("Unknown sensor type 0x%04x", id);
256                 return;
257         }
258
259         if (z < lo || z > hi) {
260                 log_error("Sensor %d: value %f out of range", id, z);
261                 return;
262         }
263
264         cooked_point(t + TIME_OFFSET, id, z, unit, q);
265 }
266
267 /*** USB interface ***/
268
269 static int find_device(void)
270 {
271         libusb_device **devlist;
272         ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
273         if (devn < 0) {
274                 log_error("Cannot enumerate USB devices: error %d", (int) devn);
275                 return 0;
276         }
277
278         for (ssize_t i=0; i<devn; i++) {
279                 struct libusb_device_descriptor desc;
280                 libusb_device *dev = devlist[i];
281                 if (!libusb_get_device_descriptor(dev, &desc)) {
282                         if (desc.idVendor == 0x0451 && desc.idProduct == 0x3211) {
283                                 log_info("Arexx data logger found at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
284                                 int err;
285                                 if (err = libusb_open(dev, &devh)) {
286                                         log_error("libusb_open() failed: error %d", err);
287                                         goto failed;
288                                 }
289                                 if (err = libusb_claim_interface(devh, 0)) {
290                                         log_error("libusb_claim_interface() failed: error %d", err);
291                                         libusb_close(devh);
292                                         goto failed;
293                                 }
294                                 libusb_free_device_list(devlist, 1);
295                                 return 1;
296                         }
297                 }
298         }
299
300 failed:
301         libusb_free_device_list(devlist, 1);
302         return 0;
303 }
304
305 static void release_device(void)
306 {
307         libusb_close(devh);
308         devh = NULL;
309 }
310
311 static void dump_packet(byte *pkt)
312 {
313         for (int i=0; i<64; i++) {
314                 if (!(i % 16))
315                         log_pkt("\t%02x:", i);
316                 log_pkt(" %02x", pkt[i]);
317                 if (i % 16 == 15)
318                         log_pkt("\n");
319         }
320 }
321
322 static int send_and_receive(byte *req, byte *reply)
323 {
324         if (debug_packets) {
325                 time_t t = time(NULL);
326                 struct tm tm;
327                 localtime_r(&t, &tm);
328
329                 char tbuf[64];
330                 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
331                 log_pkt("## %s\n", tbuf);
332         }
333
334         int err, transferred;
335         if (err = libusb_bulk_transfer(devh, 0x01, req, 64, &transferred, 200)) {
336                 if (err == LIBUSB_ERROR_TIMEOUT) {
337                         log_pkt(">> xmit timed out\n");
338                         return 0;
339                 }
340                 log_pkt(">> xmit error %d\n", err);
341                 log_error("Transmit error: %d", err);
342                 return err;
343         }
344         if (debug_packets) {
345                 log_pkt(">> xmit %d bytes\n", transferred);
346                 dump_packet(req);
347         }
348         if (err = libusb_bulk_transfer(devh, 0x81, reply, 64, &transferred, 200)) {
349                 if (err == LIBUSB_ERROR_TIMEOUT) {
350                         log_pkt("<< recv timed out\n");
351                         return 0;
352                 }
353                 log_pkt("<< recv error %d\n", err);
354                 log_error("Receive error: %d", err);
355                 return err;
356         }
357         if (debug_packets)
358                 log_pkt("<< recv %d bytes\n", transferred);
359         while (transferred < 64)
360                 reply[transferred++] = 0xff;
361         if (debug_packets)
362                 dump_packet(reply);
363         return 1;
364 }
365
366 static unsigned int get_be16(byte *p)
367 {
368         return p[1] | (p[0] << 8);
369 }
370
371 static unsigned int get_le16(byte *p)
372 {
373         return p[0] | (p[1] << 8);
374 }
375
376 static unsigned int get_le32(byte *p)
377 {
378         return get_le16(p) | (get_le16(p+2) << 16);
379 }
380
381 static void put_le16(byte *p, unsigned int x)
382 {
383         p[0] = x;
384         p[1] = x >> 8;
385 }
386
387 static void put_le32(byte *p, unsigned int x)
388 {
389         put_le16(p, x);
390         put_le16(p+2, x>>16);
391 }
392
393 static int parse_packet(byte *reply)
394 {
395         if (reply[0]) {
396                 log_error("Unknown packet type %02x", reply[0]);
397                 return 0;
398         }
399
400         int pos = 1;
401         int points = 0;
402         while (pos < 64) {
403                 byte *p = reply + pos;
404                 int len = p[0];
405                 if (!len || len == 0xff)
406                         break;
407                 if (len < 9 || len > 10) {
408                         log_error("Unknown tuple length %02x", len);
409                         break;
410                 }
411                 if (pos + len > 64) {
412                         log_error("Tuple truncated");
413                         break;
414                 }
415                 int id = get_le16(p+1);
416                 int raw = get_be16(p+3);
417                 int t = get_le32(p+5);
418                 int q = (len > 9) ? p[9] : -1;
419                 if (debug_raw_data) {
420                         printf("... %02x: id=%d raw=%d t=%d", len, id, raw, t);
421                         if (len > 9)
422                                 printf(" q=%d", q);
423                         printf("\n");
424                 }
425                 raw_point(t, id, raw, q);
426                 pos += len;
427                 points++;
428         }
429
430         return points;
431 }
432
433 static void set_clock(void)
434 {
435         byte req[64], reply[64];
436         memset(req, 0, 64);
437         req[0] = 4;
438         time_t t = time(NULL);
439         put_le32(req+1, t-TIME_OFFSET);
440         send_and_receive(req, reply);
441
442 #if 0
443         /*
444          *  Original software also sends a packet with type 3 and the timestamp,
445          *  but it does not make any sense, especially as they ignore the sensor
446          *  readings in the answer.
447          */
448         req[0] = 3;
449         send_and_receive(req, reply);
450         parse_packet(reply);
451 #endif
452 }
453
454 /*** Main ***/
455
456 static volatile sig_atomic_t want_shutdown;
457
458 static void sigterm_handler(int sig __attribute__((unused)))
459 {
460         want_shutdown = 1;
461 }
462
463 static const struct option long_options[] = {
464         { "debug",              0, NULL, 'd' },
465         { "log-packets",        0, NULL, 'p' },
466         { NULL,                 0, NULL, 0 },
467 };
468
469 static void usage(void)
470 {
471         fprintf(stderr, "Usage: arexxd [--debug] [--log-packets]\n");
472         exit(1);
473 }
474
475 int main(int argc, char **argv)
476 {
477         int opt;
478         while ((opt = getopt_long(argc, argv, "dp", long_options, NULL)) >= 0)
479                 switch (opt) {
480                         case 'd':
481                                 debug_mode++;
482                                 break;
483                         case 'p':
484                                 debug_packets++;
485                                 debug_raw_data++;
486                                 break;
487                         default:
488                                 usage();
489                 }
490         if (optind < argc)
491                 usage();
492
493         int err;
494         if (err = libusb_init(&usb_ctxt))
495                 die("Cannot initialize libusb: error %d", err);
496         // libusb_set_debug(usb_ctxt, 3);
497
498         if (!debug_mode) {
499                 if (chdir(LOG_PATH) < 0)
500                         die("Cannot change directory to %s: %m", LOG_PATH);
501                 if (debug_packets || debug_raw_data) {
502                         close(1);
503                         if (open("debug", O_WRONLY | O_CREAT | O_APPEND, 0666) < 0)
504                                 die("Cannot open debug log: %m");
505                         setlinebuf(stdout);
506                 }
507                 openlog("arexxd", LOG_NDELAY, LOG_DAEMON);
508                 pid_t pid = fork();
509                 if (pid < 0)
510                         die("fork() failed: %m");
511                 if (pid)
512                         return 0;
513                 setsid();
514                 use_syslog = 1;
515         }
516
517         struct sigaction sa = { .sa_handler = sigterm_handler };
518         sigaction(SIGTERM, &sa, NULL);
519         sigaction(SIGINT, &sa, NULL);
520
521         sigset_t term_sigs;
522         sigemptyset(&term_sigs);
523         sigaddset(&term_sigs, SIGTERM);
524         sigaddset(&term_sigs, SIGINT);
525         sigprocmask(SIG_BLOCK, &term_sigs, NULL);
526
527         int inited = 0;
528         while (!want_shutdown) {
529                 if (!find_device()) {
530                         if (!inited) {
531                                 inited = 1;
532                                 log_error("Data logger not connected, waiting until it appears");
533                         }
534                         sleep(30);
535                         continue;
536                 }
537                 log_info("Listening");
538
539                 time_t last_sync = 0;
540                 time_t last_show = 0;
541                 int want_stats = 0;
542                 int want_sleep = 0;
543                 data_point_counter = 0;
544                 while (!want_shutdown) {
545                         time_t now = time(NULL);
546                         if (now > last_sync + 900) {
547                                 log_info("Synchronizing data logger time");
548                                 set_clock();
549                                 last_sync = now;
550                         }
551                         if (want_stats && now > last_show + 300) {
552                                 log_info("Stats: received %d data points", data_point_counter);
553                                 data_point_counter = 0;
554                                 last_show = now;
555                         }
556
557                         byte req[64], reply[64];
558                         memset(req, 0, sizeof(req));
559                         req[0] = 3;
560                         err = send_and_receive(req, reply);
561                         if (err < 0)
562                                 break;
563                         want_sleep = 1;
564                         if (err > 0 && parse_packet(reply))
565                                 want_sleep = 0;
566                         want_stats = 1;
567                         sigprocmask(SIG_UNBLOCK, &term_sigs, NULL);
568                         sleep(4);
569                         sigprocmask(SIG_BLOCK, &term_sigs, NULL);
570                 }
571
572                 log_info("Disconnecting data logger");
573                 release_device();
574                 inited = 0;
575         }
576
577         log_info("Terminated");
578         return 0;
579 }