2 * Linux Interfece for Arexx Data Loggers
4 * (c) 2011 Martin Mares <mj@ucw.cz>
15 #include <libusb-1.0/libusb.h>
18 typedef unsigned char byte;
19 static libusb_context *usb_ctxt;
20 static libusb_device_handle *devh;
22 static int debug_packets = 0;
23 static int debug_raw_data = 0;
25 #define TIME_OFFSET 946681200 // Timestamp of 2000-01-01 00:00:00
27 static void die(char *msg, ...)
31 vfprintf(stderr, msg, args);
37 /*** RRD interface ***/
39 #define RRD_NAME "/tmp/power.rrd"
40 #define SLOT_SIZE 10 // 10 seconds per averaging slot
42 #define MAX_ARG_SIZE 1024
45 static char *arg_ptr[MAX_ARGS+1];
46 static char arg_buf[MAX_ARG_SIZE];
49 static void arg_new(void)
53 arg_ptr[0] = "rrdtool";
56 static void arg_push(const char *fmt, ...)
58 if (arg_cnt >= MAX_ARGS)
59 die("MAX_ARGS exceeded");
62 int len = 1 + vsnprintf(arg_buf + arg_pos, MAX_ARG_SIZE - arg_pos, fmt, va);
63 if (arg_pos + len > MAX_ARG_SIZE)
64 die("MAX_ARG_SIZE exceeded");
65 arg_ptr[arg_cnt++] = arg_buf + arg_pos;
66 arg_ptr[arg_cnt] = NULL;
70 static void rrd_point(time_t t, int id, double val)
73 snprintf(rr_name, sizeof(rr_name), "sensor-%d.rrd", id);
76 if (stat(rr_name, &st) < 0 || !st.st_size) {
77 // We have to create the RRD
78 printf("### Creating %s\n", rr_name);
82 arg_push("%d", (int) time(NULL) - 28*86400);
85 arg_push("DS:temp:GAUGE:300:0:10000"); // Anything over 10 kW is considered a ghost
86 arg_push("RRA:AVERAGE:0.25:1:20160"); // Last 14 days with full resolution
87 arg_push("RRA:AVERAGE:0.25:60:88800"); // Last 10 years with 1h resolution
88 rrd_create(arg_cnt, arg_ptr);
90 die("rrd_create failed: %s", rrd_get_error());
95 arg_push("%d:%f", t, val);
96 rrd_update(arg_cnt, arg_ptr);
98 printf("### rrd_update failed: %s\n", rrd_get_error());
103 static void cooked_point(time_t t, int id, double val, char *unit, int q)
106 localtime_r(&t, &tm);
109 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
111 printf("== %s id=%d val=%.3f unit=%s q=%d\n", tbuf, id, val, unit, q);
113 rrd_point(t, id, val);
116 static void raw_point(int t, int id, int raw, int q)
119 * The binary blob provided by Arexx contains an embedded XML fragment
120 * with descriptions of all known sensor types. If you want to see it,
121 * grep the blob for "<deviceinfo>". The meanings of the parameters are
124 * m1, m2 Device type matches if (raw_sensor_id & m1) == m2
125 * type Unit measured by the sensor (1=Celsius, 2=RH%, 3=CO2 ppm)
126 * dm User-visible sensor ID = raw_sensor_id & dm
127 * i 1 if the raw value is signed
128 * p[] Coefficients of transformation polynomial (x^0 first)
129 * vLo, vUp Upper and lower bound on the final value
130 * scale Scaling function:
131 * 0 = identity (default)
134 * 3 = (x < 0) ? 0 : log10(x)
135 * 4 = (x < 0) ? 0 : log(x)
137 * The raw values are transformed this way:
138 * - sign-extend if signed
139 * - apply the transformation polynomial
140 * - apply the scaling function
141 * - drop if outside the interval [vLo,vUp]
143 * This function applies the necessary transform for sensors we've
144 * seen in the wild. We deliberately ignore the "dm" parameter as we want
145 * to report different channels of a single sensor as multiple sensors.
151 int idhi = id & 0xf000;
153 if (idhi == 0x1000) {
158 } else if (idhi == 0x2000) {
165 } else if (idhi == 0x4000) {
172 z = -2.8e-6*z*z + 0.0405*z - 4;
177 } else if (idhi == 0x6000) {
187 z = (z + 1.9184e-7) * z;
188 z = (z - 1.0998e-3) * z;
196 printf("### Unknown sensor type 0x%04x\n", id);
200 if (z < lo || z > hi) {
201 printf("### Sensor %d: value %f out of range\n", id, z);
205 cooked_point(t + TIME_OFFSET, id, z, unit, q);
208 /*** USB interface ***/
210 static void find_device(void)
212 libusb_device **devlist;
213 ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
215 die("Cannot enumerate USB devices: error %d\n", (int) devn);
217 for (ssize_t i=0; i<devn; i++) {
218 struct libusb_device_descriptor desc;
219 libusb_device *dev = devlist[i];
220 if (!libusb_get_device_descriptor(dev, &desc)) {
221 if (desc.idVendor == 0x0451 && desc.idProduct == 0x3211) {
222 printf("Arexx USB receiver found at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
224 if (err = libusb_open(dev, &devh))
225 die("libusb_open() failed: error %d\n", err);
226 if (err = libusb_claim_interface(devh, 0))
227 die("libusb_claim_interface() failed: error %d\n", err);
228 libusb_free_device_list(devlist, 1);
234 die("No Arexx USB receiver found");
237 static void dump_packet(byte *pkt)
239 for (int i=0; i<64; i++) {
241 printf("\t%02x:", i);
242 printf(" %02x", pkt[i]);
248 static int send_and_receive(byte *req, byte *reply)
250 int err, transferred;
251 if (err = libusb_bulk_transfer(devh, 0x01, req, 64, &transferred, 200)) {
252 fprintf(stderr, "Transmit error: %d\n", err);
256 printf(">> xmit %d bytes\n", transferred);
259 if (err = libusb_bulk_transfer(devh, 0x81, reply, 64, &transferred, 200)) {
260 fprintf(stderr, "Receive error: %d\n", err);
264 printf("<< recv %d bytes\n", transferred);
265 while (transferred < 64)
266 reply[transferred++] = 0xff;
272 static unsigned int get_be16(byte *p)
274 return p[1] | (p[0] << 8);
277 static unsigned int get_le16(byte *p)
279 return p[0] | (p[1] << 8);
282 static unsigned int get_le32(byte *p)
284 return get_le16(p) | (get_le16(p+2) << 16);
287 static void put_le16(byte *p, unsigned int x)
293 static void put_le32(byte *p, unsigned int x)
296 put_le16(p+2, x>>16);
299 static int parse_packet(byte *reply)
302 printf("### Unknown packet type %02x\n", reply[0]);
309 byte *p = reply + pos;
311 if (!len || len == 0xff)
313 if (len < 9 || len > 10) {
314 printf("### Unknown tuple length %02x\n", len);
317 if (pos + len > 64) {
318 printf("### Tuple truncated\n");
321 int id = get_le16(p+1);
322 int raw = get_be16(p+3);
323 int t = get_le32(p+5);
324 int q = (len > 9) ? p[9] : -1;
325 if (debug_raw_data) {
326 printf("... %02x: id=%d raw=%d t=%d", len, id, raw, t);
331 raw_point(t, id, raw, q);
339 static void set_clock(void)
341 puts("### Syncing time");
343 byte req[64], reply[64];
346 time_t t = time(NULL);
347 put_le32(req+1, t-TIME_OFFSET);
348 send_and_receive(req, reply);
352 * Original software also sends a packet with type 3 and the timestamp,
353 * but it does not make any sense, especially as they ignore the sensor
354 * readings in the answer.
357 send_and_receive(req, reply);
365 if (err = libusb_init(&usb_ctxt))
366 die("Cannot initialize libusb: error %d", err);
367 libusb_set_debug(usb_ctxt, 3);
373 byte req[64], reply[64];
374 memset(req, 0, sizeof(req));
376 if (send_and_receive(req, reply)) {
377 if (parse_packet(reply))