]> mj.ucw.cz Git - arexx.git/blob - arexx.c
The first experimental version
[arexx.git] / arexx.c
1 /*
2  *      Linux Interfece for Arexx Data Loggers
3  *
4  *      (c) 2011 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 <math.h>
13 #include <libusb-1.0/libusb.h>
14
15 typedef unsigned char byte;
16 static libusb_context *usb_ctxt;
17 static libusb_device_handle *devh;
18
19 static int debug_packets = 0;
20
21 #define TIME_OFFSET 946681200           // Timestamp of 2000-01-01 00:00:00
22
23 static void die(char *msg, ...)
24 {
25         va_list args;
26         va_start(args, msg);
27         vfprintf(stderr, msg, args);
28         fputc('\n', stderr);
29         va_end(args);
30         exit(1);
31 }
32
33 static void find_device(void)
34 {
35         libusb_device **devlist;
36         ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
37         if (devn < 0)
38                 die("Cannot enumerate USB devices: error %d\n", (int) devn);
39
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));
46                                 int err;
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);
52                                 return;
53                         }
54                 }
55         }
56
57         die("No Arexx USB receiver found");
58 }
59
60 static void dump_packet(byte *pkt)
61 {
62         for (int i=0; i<64; i++) {
63                 if (!(i % 16))
64                         printf("\t%02x:", i);
65                 printf(" %02x", pkt[i]);
66                 if (i % 16 == 15)
67                         printf("\n");
68         }
69 }
70
71 static int send_and_receive(byte *req, byte *reply)
72 {
73         int err, transferred;
74         if (err = libusb_bulk_transfer(devh, 0x01, req, 64, &transferred, 200)) {
75                 fprintf(stderr, "Transmit error: %d\n", err);
76                 return 0;
77         }
78         if (debug_packets) {
79                 printf(">> xmit %d bytes\n", transferred);
80                 dump_packet(req);
81         }
82         if (err = libusb_bulk_transfer(devh, 0x81, reply, 64, &transferred, 200)) {
83                 fprintf(stderr, "Receive error: %d\n", err);
84                 return 0;
85         }
86         if (debug_packets)
87                 printf("<< recv %d bytes\n", transferred);
88         while (transferred < 64)
89                 reply[transferred++] = 0xff;
90         if (debug_packets)
91                 dump_packet(reply);
92         return 1;
93 }
94
95 static unsigned int get_be16(byte *p)
96 {
97         return p[1] | (p[0] << 8);
98 }
99
100 static unsigned int get_le16(byte *p)
101 {
102         return p[0] | (p[1] << 8);
103 }
104
105 static unsigned int get_le32(byte *p)
106 {
107         return get_le16(p) | (get_le16(p+2) << 16);
108 }
109
110 static void put_le16(byte *p, unsigned int x)
111 {
112         p[0] = x;
113         p[1] = x >> 8;
114 }
115
116 static void put_le32(byte *p, unsigned int x)
117 {
118         put_le16(p, x);
119         put_le16(p+2, x>>16);
120 }
121
122 static void raw_point(int t, int id, int raw)
123 {
124         /*
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
128          *  as follows:
129          *
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)
138          *                              1 = 10^x
139          *                              2 = exp(x)
140          *                              3 = (x < 0) ? 0 : log10(x)
141          *                              4 = (x < 0) ? 0 : log(x)
142          *
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]
148          *
149          *  This function applies the necessary transform for sensors we've
150          *  seen in the wild.
151          */
152
153         double z = raw;
154         double hi, lo;
155         char *unit;
156         int idhi = id & 0xf000;
157
158         if (idhi == 0x1000) {
159                 z = 0.02*z - 273.15;
160                 lo = -200;
161                 hi = 600;
162                 unit = "C";
163         } else if (idhi == 0x2000) {
164                 if (raw >= 0x8000)
165                         z -= 0x10000;
166                 z /= 128;
167                 lo = -60;
168                 hi = 125;
169                 unit = "C";
170         } else if (idhi == 0x4000) {
171                 if (!(id & 1)) {
172                         z = z/100 - 39.6;
173                         lo = -60;
174                         hi = 125;
175                         unit = "C";
176                 } else {
177                         z = -2.8e-6*z*z + 0.0405*z - 4;
178                         lo = 0;
179                         hi = 100.1;
180                         unit = "%RH";
181                 }
182         } else if (idhi == 0x6000) {
183                 if (!(id & 1)) {
184                         if (raw >= 0x8000)
185                                 z -= 0x10000;
186                         z /= 128;
187                         lo = -60;
188                         hi = 125;
189                         unit = "C";
190                 } else {
191                         z = -3.8123e-11*z;
192                         z = (z + 1.9184e-7) * z;
193                         z = (z - 1.0998e-3) * z;
194                         z += 6.56;
195                         z = pow(10, z);
196                         lo = 0;
197                         hi = 1e6;
198                         unit = "ppm";
199                 }
200         } else {
201                 printf("### Unknown sensor ID 0x%04x\n", id);
202                 return;
203         }
204
205         if (z < lo || z > hi) {
206                 printf("### Sensor %d: value %f out of range\n", id, z);
207                 return;
208         }
209
210         printf("\t-> %f %s\n", z, unit);
211 }
212
213 static int parse_packet(byte *reply)
214 {
215         if (reply[0]) {
216                 printf("### Unknown packet type %02x\n", reply[0]);
217                 return 0;
218         }
219
220         int pos = 1;
221         int points = 0;
222         while (pos < 64) {
223                 byte *p = reply + pos;
224                 int len = p[0];
225                 if (!len || len == 0xff)
226                         break;
227                 if (len < 9 || len > 10) {
228                         printf("### Unknown packet length %02x\n", len);
229                         break;
230                 }
231                 if (pos + len > 64) {
232                         printf("### Packet truncated\n");
233                         break;
234                 }
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);
239                 if (len > 9)
240                         printf(" q=%d", p[9]);
241                 printf("\n");
242                 raw_point(t, id, raw);
243                 pos += len;
244                 points++;
245         }
246
247         return points;
248 }
249
250 static void set_clock(void)
251 {
252         puts("### Syncing time");
253
254         byte req[64], reply[64];
255         memset(req, 0, 64);
256         req[0] = 4;
257         time_t t = time(NULL);
258         put_le32(req+1, t-TIME_OFFSET);
259         send_and_receive(req, reply);
260
261 #if 0
262         req[0] = 3;
263         send_and_receive(req, reply);
264         parse_packet(reply);
265 #endif
266 }
267
268 int main(void)
269 {
270         int err;
271         if (err = libusb_init(&usb_ctxt))
272                 die("Cannot initialize libusb: error %d", err);
273         libusb_set_debug(usb_ctxt, 3);
274
275         find_device();
276         set_clock();
277
278         for (;;) {
279                 byte req[64], reply[64];
280                 memset(req, 0, sizeof(req));
281                 req[0] = 3;
282                 if (send_and_receive(req, reply)) {
283                         if (parse_packet(reply))
284                                 continue;
285                 }
286                 sleep(4);
287         }
288
289         return 0;
290 }