2 * Linux Interfece for Arexx Data Loggers
4 * (c) 2011 Martin Mares <mj@ucw.cz>
13 #include <libusb-1.0/libusb.h>
15 typedef unsigned char byte;
16 static libusb_context *usb_ctxt;
17 static libusb_device_handle *devh;
19 static int debug_packets = 0;
21 #define TIME_OFFSET 946681200 // Timestamp of 2000-01-01 00:00:00
23 static void die(char *msg, ...)
27 vfprintf(stderr, msg, args);
33 static void find_device(void)
35 libusb_device **devlist;
36 ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
38 die("Cannot enumerate USB devices: error %d\n", (int) devn);
40 for (ssize_t i=0; i<devn; i++) {
41 struct libusb_device_descriptor desc;
42 libusb_device *dev = devlist[i];
43 if (!libusb_get_device_descriptor(dev, &desc)) {
44 if (desc.idVendor == 0x0451 && desc.idProduct == 0x3211) {
45 printf("Arexx USB receiver found at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
47 if (err = libusb_open(dev, &devh))
48 die("libusb_open() failed: error %d\n", err);
49 if (err = libusb_claim_interface(devh, 0))
50 die("libusb_claim_interface() failed: error %d\n", err);
51 libusb_free_device_list(devlist, 1);
57 die("No Arexx USB receiver found");
60 static void dump_packet(byte *pkt)
62 for (int i=0; i<64; i++) {
65 printf(" %02x", pkt[i]);
71 static int send_and_receive(byte *req, byte *reply)
74 if (err = libusb_bulk_transfer(devh, 0x01, req, 64, &transferred, 200)) {
75 fprintf(stderr, "Transmit error: %d\n", err);
79 printf(">> xmit %d bytes\n", transferred);
82 if (err = libusb_bulk_transfer(devh, 0x81, reply, 64, &transferred, 200)) {
83 fprintf(stderr, "Receive error: %d\n", err);
87 printf("<< recv %d bytes\n", transferred);
88 while (transferred < 64)
89 reply[transferred++] = 0xff;
95 static unsigned int get_be16(byte *p)
97 return p[1] | (p[0] << 8);
100 static unsigned int get_le16(byte *p)
102 return p[0] | (p[1] << 8);
105 static unsigned int get_le32(byte *p)
107 return get_le16(p) | (get_le16(p+2) << 16);
110 static void put_le16(byte *p, unsigned int x)
116 static void put_le32(byte *p, unsigned int x)
119 put_le16(p+2, x>>16);
122 static void raw_point(int t, int id, int raw)
125 * The binary blob provided by Arexx contains an embedded XML fragment
126 * with descriptions of all known sensor types. If you want to see it,
127 * grep the blob for "<deviceinfo>". The meanings of the parameters are
130 * m1, m2 Device type matches if raw_sensor_id & m1 == m2
131 * type Unit measured by the sensor (1=Celsius, 2=RH%, 3=CO2 ppm)
132 * dm User-visible sensor ID = raw_sensor_id & dm
133 * i 1 if the raw value is signed
134 * p[] Coefficients of transformation polynomial (x^0 first)
135 * vLo, vUp Upper and lower bound on the final value
136 * scale Scaling function:
137 * 0 = identity (default)
140 * 3 = (x < 0) ? 0 : log10(x)
141 * 4 = (x < 0) ? 0 : log(x)
143 * The raw values are transformed this way:
144 * - sign-extend if signed
145 * - apply the transformation polynomial
146 * - apply the scaling function
147 * - drop if outside the interval [vLo,vUp]
149 * This function applies the necessary transform for sensors we've
156 int idhi = id & 0xf000;
158 if (idhi == 0x1000) {
163 } else if (idhi == 0x2000) {
170 } else if (idhi == 0x4000) {
177 z = -2.8e-6*z*z + 0.0405*z - 4;
182 } else if (idhi == 0x6000) {
192 z = (z + 1.9184e-7) * z;
193 z = (z - 1.0998e-3) * z;
201 printf("### Unknown sensor ID 0x%04x\n", id);
205 if (z < lo || z > hi) {
206 printf("### Sensor %d: value %f out of range\n", id, z);
210 printf("\t-> %f %s\n", z, unit);
213 static int parse_packet(byte *reply)
216 printf("### Unknown packet type %02x\n", reply[0]);
223 byte *p = reply + pos;
225 if (!len || len == 0xff)
227 if (len < 9 || len > 10) {
228 printf("### Unknown packet length %02x\n", len);
231 if (pos + len > 64) {
232 printf("### Packet truncated\n");
235 int id = get_le16(p+1);
236 int raw = get_be16(p+3);
237 int t = get_le32(p+5);
238 printf("... %02x: id=%d raw=%d t=%d", len, id, raw, t);
240 printf(" q=%d", p[9]);
242 raw_point(t, id, raw);
250 static void set_clock(void)
252 puts("### Syncing time");
254 byte req[64], reply[64];
257 time_t t = time(NULL);
258 put_le32(req+1, t-TIME_OFFSET);
259 send_and_receive(req, reply);
263 send_and_receive(req, reply);
271 if (err = libusb_init(&usb_ctxt))
272 die("Cannot initialize libusb: error %d", err);
273 libusb_set_debug(usb_ctxt, 3);
279 byte req[64], reply[64];
280 memset(req, 0, sizeof(req));
282 if (send_and_receive(req, reply)) {
283 if (parse_packet(reply))