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