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