]> mj.ucw.cz Git - arexx.git/blob - arexxd.c
0358db145ba9d1d36953c9123f7f71c61918a486
[arexx.git] / arexxd.c
1 /*
2  *      Linux Interfece for Arexx Data Loggers
3  *
4  *      (c) 2011-2012 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 DEFAULT_LOG_DIR "/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 static int debug_usb;
33 static char *log_dir = DEFAULT_LOG_DIR;
34
35 static void die(char *fmt, ...)
36 {
37         va_list args;
38         va_start(args, fmt);
39         if (use_syslog)
40                 vsyslog(LOG_CRIT, fmt, args);
41         else {
42                 vfprintf(stderr, fmt, args);
43                 fprintf(stderr, "\n");
44         }
45         va_end(args);
46         exit(1);
47 }
48
49 static void log_error(char *fmt, ...)
50 {
51         va_list args;
52         va_start(args, fmt);
53         if (use_syslog)
54                 vsyslog(LOG_ERR, fmt, args);
55         else {
56                 vfprintf(stderr, fmt, args);
57                 fprintf(stderr, "\n");
58         }
59         va_end(args);
60 }
61
62 static void log_info(char *fmt, ...)
63 {
64         va_list args;
65         va_start(args, fmt);
66         if (use_syslog)
67                 vsyslog(LOG_INFO, fmt, args);
68         else {
69                 vfprintf(stderr, fmt, args);
70                 fprintf(stderr, "\n");
71         }
72         va_end(args);
73 }
74
75 static void log_pkt(char *fmt, ...)
76 {
77         if (!debug_packets)
78                 return;
79         va_list args;
80         va_start(args, fmt);
81         vprintf(fmt, args);
82         va_end(args);
83 }
84
85 /*** RRD interface ***/
86
87 #define MAX_ARGS 20
88 #define MAX_ARG_SIZE 1024
89
90 static int arg_cnt;
91 static char *arg_ptr[MAX_ARGS+1];
92 static char arg_buf[MAX_ARG_SIZE];
93 static int arg_pos;
94
95 static void arg_new(void)
96 {
97         arg_cnt = 1;
98         arg_pos = 0;
99         arg_ptr[0] = "rrdtool";
100 }
101
102 static void arg_push(const char *fmt, ...)
103 {
104         if (arg_cnt >= MAX_ARGS)
105                 die("MAX_ARGS exceeded");
106         va_list va;
107         va_start(va, fmt);
108         int len = 1 + vsnprintf(arg_buf + arg_pos, MAX_ARG_SIZE - arg_pos, fmt, va);
109         if (arg_pos + len > MAX_ARG_SIZE)
110                 die("MAX_ARG_SIZE exceeded");
111         arg_ptr[arg_cnt++] = arg_buf + arg_pos;
112         arg_ptr[arg_cnt] = NULL;
113         arg_pos += len;
114 }
115
116 static void rrd_point(time_t t, const char *name, double val, char *unit)
117 {
118         char rr_name[256];
119         snprintf(rr_name, sizeof(rr_name), "sensor-%s.rrd", name);
120
121         struct stat st;
122         if (stat(rr_name, &st) < 0 || !st.st_size) {
123                 // We have to create the RRD
124                 log_info("Creating %s", rr_name);
125                 arg_new();
126                 arg_push(rr_name);
127                 arg_push("--start");
128                 arg_push("%d", (int) time(NULL) - 28*86400);
129                 arg_push("--step");
130                 arg_push("60");
131                 if (!strcmp(unit, "%RH"))
132                         arg_push("DS:rh:GAUGE:300:0:100");
133                 else if (!strcmp(unit, "ppm"))
134                         arg_push("DS:ppm:GAUGE:300:0:1000000");
135                 else
136                         arg_push("DS:temp:GAUGE:300:-200:200");
137                 arg_push("RRA:AVERAGE:0.25:1:20160");           // Last 14 days with full resolution
138                 arg_push("RRA:AVERAGE:0.25:60:88800");          // Last 10 years with 1h resolution
139                 arg_push("RRA:MIN:0.25:60:88800");              // including minima and maxima
140                 arg_push("RRA:MAX:0.25:60:88800");
141                 rrd_create(arg_cnt, arg_ptr);
142                 if (rrd_test_error()) {
143                         log_error("rrd_create on %s failed: %s", rr_name, rrd_get_error());
144                         rrd_clear_error();
145                         return;
146                 }
147         }
148
149         arg_new();
150         arg_push(rr_name);
151         arg_push("%d:%f", t, val);
152         rrd_update(arg_cnt, arg_ptr);
153         if (rrd_test_error()) {
154                 log_error("rrd_update on %s failed: %s", rr_name, rrd_get_error());
155                 rrd_clear_error();
156         }
157 }
158
159 /*** Transforms ***/
160
161 #define TIME_OFFSET 946681200           // Timestamp of 2000-01-01 00:00:00
162
163 static int data_point_counter;          // Since last log message
164
165 static double correct_point(int id, double val, const char **name)
166 {
167         /*
168          *  Manually calculated corrections and renames for my sensors.
169          *  Replace with your formulae.
170          */
171         switch (id) {
172                 case 10415:
173                         *name = "ursarium";
174                         return val - 0.93;
175                 case 10707:
176                         *name = "balcony";
177                         return val - 0.71;
178                 case 19246:
179                         *name = "catarium";
180                         return val + 0.49;
181                 case 19247:
182                         *name = "catarium-rh";
183                         return val;
184                 case 12133:
185                         *name = "outside";
186                         return val + 0.44;
187                 default:
188                         return val;
189         }
190 }
191
192 static void cooked_point(time_t t, int id, double val, char *unit, int q)
193 {
194         char namebuf[16];
195         snprintf(namebuf, sizeof(namebuf), "%d", id);
196         const char *name = namebuf;
197
198         double val2 = correct_point(id, val, &name);
199
200         if (debug_raw_data) {
201                 struct tm tm;
202                 localtime_r(&t, &tm);
203                 char tbuf[64];
204                 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
205                 printf("== %s id=%d name=%s val=%.3f val2=%.3f unit=%s q=%d\n", tbuf, id, name, val, val2, unit, q);
206         }
207
208         data_point_counter++;
209         rrd_point(t, name, val2, unit);
210 }
211
212 static void raw_point(int t, int id, int raw, int q)
213 {
214         /*
215          *  The binary blob provided by Arexx contains an embedded XML fragment
216          *  with descriptions of all known sensor types. If you want to see it,
217          *  grep the blob for "<deviceinfo>". The meanings of the parameters are
218          *  as follows:
219          *
220          *      m1, m2          Device type matches if (raw_sensor_id & m1) == m2
221          *      type            Unit measured by the sensor (1=Celsius, 2=RH%, 3=CO2 ppm)
222          *      dm              User-visible sensor ID = raw_sensor_id & dm
223          *      i               1 if the raw value is signed
224          *      p[]             Coefficients of transformation polynomial (x^0 first)
225          *      vLo, vUp        Upper and lower bound on the final value
226          *      scale           Scaling function:
227          *                              0 = identity (default)
228          *                              1 = 10^x
229          *                              2 = exp(x)
230          *                              3 = (x < 0) ? 0 : log10(x)
231          *                              4 = (x < 0) ? 0 : log(x)
232          *
233          *  The raw values are transformed this way:
234          *      - sign-extend if signed
235          *      - apply the transformation polynomial
236          *      - apply the scaling function
237          *      - drop if outside the interval [vLo,vUp]
238          *
239          *  This function applies the necessary transform for sensors we've
240          *  seen in the wild. We deliberately ignore the "dm" parameter as we want
241          *  to report different channels of a single sensor as multiple sensors.
242          */
243
244         double z = raw;
245         double hi, lo;
246         char *unit;
247         int idhi = id & 0xf000;
248
249         if (idhi == 0x1000) {
250                 z = 0.02*z - 273.15;
251                 lo = -200;
252                 hi = 600;
253                 unit = "C";
254         } else if (idhi == 0x2000) {
255                 if (raw >= 0x8000)
256                         z -= 0x10000;
257                 z /= 128;
258                 lo = -60;
259                 hi = 125;
260                 unit = "C";
261         } else if (idhi == 0x4000) {
262                 if (!(id & 1)) {
263                         z = z/100 - 39.6;
264                         lo = -60;
265                         hi = 125;
266                         unit = "C";
267                 } else {
268                         z = -2.8e-6*z*z + 0.0405*z - 4;
269                         lo = 0;
270                         hi = 100.1;
271                         unit = "%RH";
272                 }
273         } else if (idhi == 0x6000) {
274                 if (!(id & 1)) {
275                         if (raw >= 0x8000)
276                                 z -= 0x10000;
277                         z /= 128;
278                         lo = -60;
279                         hi = 125;
280                         unit = "C";
281                 } else {
282                         z = -3.8123e-11*z;
283                         z = (z + 1.9184e-7) * z;
284                         z = (z - 1.0998e-3) * z;
285                         z += 6.56;
286                         z = pow(10, z);
287                         lo = 0;
288                         hi = 1e6;
289                         unit = "ppm";
290                 }
291         } else {
292                 log_error("Unknown sensor type 0x%04x", id);
293                 return;
294         }
295
296         if (z < lo || z > hi) {
297                 log_error("Sensor %d: value %f out of range", id, z);
298                 return;
299         }
300
301         cooked_point(t + TIME_OFFSET, id, z, unit, q);
302 }
303
304 /*** USB interface ***/
305
306 static int rx_endpoint, tx_endpoint;
307
308 static int parse_descriptors(libusb_device *dev)
309 {
310         int err;
311         struct libusb_config_descriptor *desc;
312
313         if (err = libusb_get_active_config_descriptor(dev, &desc)) {
314                 log_error("libusb_get_config_descriptor failed: error %d", err);
315                 return 0;
316         }
317         if (desc->bNumInterfaces != 1) {
318                 log_error("Unexpected number of interfaces: %d", desc->bNumInterfaces);
319                 goto failed;
320         }
321
322         const struct libusb_interface *iface = &desc->interface[0];
323         if (iface->num_altsetting != 1) {
324                 log_error("Unexpected number of alternate interface settings: %d", iface->num_altsetting);
325                 goto failed;
326         }
327
328         const struct libusb_interface_descriptor *ifd = &iface->altsetting[0];
329         if (ifd->bNumEndpoints != 2) {
330                 log_error("Unexpected number of endpoints: %d", ifd->bNumEndpoints);
331                 goto failed;
332         }
333
334         rx_endpoint = tx_endpoint = -1;
335         for (int i=0; i<2; i++) {
336                 const struct libusb_endpoint_descriptor *epd = &ifd->endpoint[i];
337                 if (epd->bEndpointAddress & 0x80)
338                         rx_endpoint = epd->bEndpointAddress;
339                 else
340                         tx_endpoint = epd->bEndpointAddress;
341         }
342         if (rx_endpoint < 0 || tx_endpoint < 0) {
343                 log_error("Failed to identify endpoints");
344                 goto failed;
345         }
346
347         log_pkt("Found endpoints: rx==%02x tx=%02x\n", rx_endpoint, tx_endpoint);
348         libusb_free_config_descriptor(desc);
349         return 1;
350
351 failed:
352         libusb_free_config_descriptor(desc);
353         return 0;
354 }
355
356 static int find_device(void)
357 {
358         libusb_device **devlist;
359         ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
360         if (devn < 0) {
361                 log_error("Cannot enumerate USB devices: error %d", (int) devn);
362                 return 0;
363         }
364
365         for (ssize_t i=0; i<devn; i++) {
366                 struct libusb_device_descriptor desc;
367                 libusb_device *dev = devlist[i];
368                 if (!libusb_get_device_descriptor(dev, &desc)) {
369                         if (desc.idVendor == 0x0451 && desc.idProduct == 0x3211) {
370                                 log_info("Arexx data logger found at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
371                                 if (!parse_descriptors(dev))
372                                         continue;
373                                 int err;
374                                 if (err = libusb_open(dev, &devh)) {
375                                         log_error("libusb_open() failed: error %d", err);
376                                         goto failed;
377                                 }
378                                 if (err = libusb_claim_interface(devh, 0)) {
379                                         log_error("libusb_claim_interface() failed: error %d", err);
380                                         libusb_close(devh);
381                                         goto failed;
382                                 }
383                                 libusb_free_device_list(devlist, 1);
384                                 return 1;
385                         }
386                 }
387         }
388
389 failed:
390         libusb_free_device_list(devlist, 1);
391         return 0;
392 }
393
394 static void release_device(void)
395 {
396         libusb_release_interface(devh, 0);
397         libusb_reset_device(devh);
398         libusb_close(devh);
399         devh = NULL;
400 }
401
402 static void dump_packet(byte *pkt)
403 {
404         for (int i=0; i<64; i++) {
405                 if (!(i % 16))
406                         log_pkt("\t%02x:", i);
407                 log_pkt(" %02x", pkt[i]);
408                 if (i % 16 == 15)
409                         log_pkt("\n");
410         }
411 }
412
413 static int send_and_receive(byte *req, byte *reply)
414 {
415         if (debug_packets) {
416                 time_t t = time(NULL);
417                 struct tm tm;
418                 localtime_r(&t, &tm);
419
420                 char tbuf[64];
421                 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
422                 log_pkt("## %s\n", tbuf);
423         }
424
425         int err, transferred;
426         if (err = libusb_bulk_transfer(devh, tx_endpoint, req, 64, &transferred, 200)) {
427                 if (err == LIBUSB_ERROR_TIMEOUT) {
428                         log_pkt(">> xmit timed out\n");
429                         return 0;
430                 }
431                 log_pkt(">> xmit error %d\n", err);
432                 log_error("Transmit error: %d", err);
433                 return err;
434         }
435         if (debug_packets) {
436                 log_pkt(">> xmit %d bytes\n", transferred);
437                 dump_packet(req);
438         }
439         if (err = libusb_bulk_transfer(devh, rx_endpoint, reply, 64, &transferred, 200)) {
440                 if (err == LIBUSB_ERROR_TIMEOUT) {
441                         log_pkt("<< recv timed out\n");
442                         return 0;
443                 }
444                 log_pkt("<< recv error %d\n", err);
445                 log_error("Receive error: %d", err);
446                 return err;
447         }
448         if (debug_packets)
449                 log_pkt("<< recv %d bytes\n", transferred);
450         while (transferred < 64)
451                 reply[transferred++] = 0xff;
452         if (debug_packets)
453                 dump_packet(reply);
454         return 1;
455 }
456
457 static unsigned int get_be16(byte *p)
458 {
459         return p[1] | (p[0] << 8);
460 }
461
462 static unsigned int get_le16(byte *p)
463 {
464         return p[0] | (p[1] << 8);
465 }
466
467 static unsigned int get_le32(byte *p)
468 {
469         return get_le16(p) | (get_le16(p+2) << 16);
470 }
471
472 static void put_le16(byte *p, unsigned int x)
473 {
474         p[0] = x;
475         p[1] = x >> 8;
476 }
477
478 static void put_le32(byte *p, unsigned int x)
479 {
480         put_le16(p, x);
481         put_le16(p+2, x>>16);
482 }
483
484 static int parse_packet(byte *reply)
485 {
486         if (reply[0]) {
487                 log_error("Unknown packet type %02x", reply[0]);
488                 return 0;
489         }
490
491         int pos = 1;
492         int points = 0;
493         while (pos < 64) {
494                 byte *p = reply + pos;
495                 int len = p[0];
496                 if (!len || len == 0xff)
497                         break;
498                 if (len < 9 || len > 10) {
499                         log_error("Unknown tuple length %02x", len);
500                         break;
501                 }
502                 if (pos + len > 64) {
503                         log_error("Tuple truncated");
504                         break;
505                 }
506                 int id = get_le16(p+1);
507                 int raw = get_be16(p+3);
508                 int t = get_le32(p+5);
509                 int q = (len > 9) ? p[9] : -1;
510                 if (debug_raw_data) {
511                         printf("... %02x: id=%d raw=%d t=%d", len, id, raw, t);
512                         if (len > 9)
513                                 printf(" q=%d", q);
514                         printf("\n");
515                 }
516                 raw_point(t, id, raw, q);
517                 pos += len;
518                 points++;
519         }
520
521         return points;
522 }
523
524 static void set_clock(void)
525 {
526         byte req[64], reply[64];
527         memset(req, 0, 64);
528         req[0] = 4;
529         time_t t = time(NULL);
530         put_le32(req+1, t-TIME_OFFSET);
531         send_and_receive(req, reply);
532
533 #if 0
534         /*
535          *  Original software also sends a packet with type 3 and the timestamp,
536          *  but it does not make any sense, especially as they ignore the sensor
537          *  readings in the answer.
538          */
539         req[0] = 3;
540         send_and_receive(req, reply);
541         parse_packet(reply);
542 #endif
543 }
544
545 /*** Main ***/
546
547 static sigset_t term_sigs;
548 static volatile sig_atomic_t want_shutdown;
549
550 static void sigterm_handler(int sig __attribute__((unused)))
551 {
552         want_shutdown = 1;
553 }
554
555 static void interruptible_msleep(int ms)
556 {
557         sigprocmask(SIG_UNBLOCK, &term_sigs, NULL);
558         struct timespec ts = { .tv_sec = ms/1000, .tv_nsec = (ms%1000) * 1000000 };
559         nanosleep(&ts, NULL);
560         sigprocmask(SIG_BLOCK, &term_sigs, NULL);
561 }
562
563 static const struct option long_options[] = {
564         { "debug",              0, NULL, 'd' },
565         { "log-dir",            1, NULL, 'l' },
566         { "debug-packets",      0, NULL, 'p' },
567         { "debug-raw",          0, NULL, 'r' },
568         { NULL,                 0, NULL, 0 },
569 };
570
571 static void usage(void)
572 {
573         fprintf(stderr, "\n\
574 Usage: arexxd <options>\n\
575 \n\
576 Options:\n\
577 -d, --debug             Debug mode (no chdir, no fork, no syslog)\n\
578 -l, --log-dir=<dir>     Directory where all received data should be stored\n\
579 -p, --debug-packets     Log all packets sent and received\n\
580 -r, --debug-raw         Log conversion from raw values\n\
581 -u, --debug-usb         Enable libusb debug messages (to stdout/stderr)\n\
582 ");
583         exit(1);
584 }
585
586 int main(int argc, char **argv)
587 {
588         int opt;
589         while ((opt = getopt_long(argc, argv, "dl:pru", long_options, NULL)) >= 0)
590                 switch (opt) {
591                         case 'd':
592                                 debug_mode++;
593                                 break;
594                         case 'l':
595                                 log_dir = optarg;
596                                 break;
597                         case 'p':
598                                 debug_packets++;
599                                 break;
600                         case 'r':
601                                 debug_raw_data++;
602                                 break;
603                         case 'u':
604                                 debug_usb++;
605                                 break;
606                         default:
607                                 usage();
608                 }
609         if (optind < argc)
610                 usage();
611
612         int err;
613         if (err = libusb_init(&usb_ctxt))
614                 die("Cannot initialize libusb: error %d", err);
615         if (debug_usb)
616                 libusb_set_debug(usb_ctxt, 3);
617
618         if (!debug_mode) {
619                 if (chdir(log_dir) < 0)
620                         die("Cannot change directory to %s: %m", log_dir);
621                 if (debug_packets || debug_raw_data) {
622                         close(1);
623                         if (open("debug", O_WRONLY | O_CREAT | O_APPEND, 0666) < 0)
624                                 die("Cannot open debug log: %m");
625                         setlinebuf(stdout);
626                 }
627                 openlog("arexxd", LOG_NDELAY, LOG_DAEMON);
628                 pid_t pid = fork();
629                 if (pid < 0)
630                         die("fork() failed: %m");
631                 if (pid)
632                         return 0;
633                 setsid();
634                 use_syslog = 1;
635         }
636
637         struct sigaction sa = { .sa_handler = sigterm_handler };
638         sigaction(SIGTERM, &sa, NULL);
639         sigaction(SIGINT, &sa, NULL);
640
641         sigemptyset(&term_sigs);
642         sigaddset(&term_sigs, SIGTERM);
643         sigaddset(&term_sigs, SIGINT);
644         sigprocmask(SIG_BLOCK, &term_sigs, NULL);
645
646         int inited = 0;
647         while (!want_shutdown) {
648                 if (!find_device()) {
649                         if (!inited) {
650                                 inited = 1;
651                                 log_error("Data logger not connected, waiting until it appears");
652                         }
653                         interruptible_msleep(30000);
654                         continue;
655                 }
656                 log_info("Listening");
657
658                 time_t last_sync = 0;
659                 time_t last_show = 0;
660                 int want_stats = 0;
661                 int want_sleep = 0;
662                 data_point_counter = 0;
663                 while (!want_shutdown) {
664                         time_t now = time(NULL);
665                         if (now > last_sync + 900) {
666                                 log_info("Synchronizing data logger time");
667                                 set_clock();
668                                 last_sync = now;
669                         }
670                         if (want_stats && now > last_show + 300) {
671                                 log_info("Stats: received %d data points", data_point_counter);
672                                 data_point_counter = 0;
673                                 last_show = now;
674                         }
675
676                         byte req[64], reply[64];
677                         memset(req, 0, sizeof(req));
678                         req[0] = 3;
679                         err = send_and_receive(req, reply);
680                         if (err < 0)
681                                 break;
682                         want_sleep = 1;
683                         if (err > 0 && parse_packet(reply))
684                                 want_sleep = 0;
685                         if (want_sleep) {
686                                 interruptible_msleep(4000);
687                                 want_stats = 1;
688                         } else
689                                 interruptible_msleep(5);
690                 }
691
692                 log_info("Disconnecting data logger");
693                 release_device();
694                 inited = 0;
695                 interruptible_msleep(10000);
696         }
697
698         log_info("Terminated");
699         return 0;
700 }