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