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