]> sigrok.org Git - libsigrok.git/blob - src/hardware/brymen-bm86x/protocol.c
Backport recent changes from mainline.
[libsigrok.git] / src / hardware / brymen-bm86x / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <string.h>
22 #include <math.h>
23 #include "protocol.h"
24
25 #define USB_TIMEOUT 500
26
27 static const char char_map[128] = {
28         [0x20] = '-',
29         [0x5F] = '0',
30         [0x50] = '1',
31         [0x6D] = '2',
32         [0x7C] = '3',
33         [0x72] = '4',
34         [0x3E] = '5',
35         [0x3F] = '6',
36         [0x54] = '7',
37         [0x7F] = '8',
38         [0x7E] = '9',
39         [0x0F] = 'C',
40         [0x27] = 'F',
41         [0x0B] = 'L',
42         [0x79] = 'd',
43         [0x10] = 'i',
44         [0x39] = 'o',
45 };
46
47 static int brymen_bm86x_parse_digits(const unsigned char *buf, int length,
48                                      char *str, float *floatval,
49                                      char *temp_unit, int *digits, int flag)
50 {
51         char c, *p = str;
52         int i, ret;
53
54         *digits = INT_MIN;
55
56         if (buf[0] & flag)
57                 *p++ = '-';
58         for (i = 0; i < length; i++) {
59                 if (i && i < 5 && buf[i+1] & 0x01) {
60                         *p++ = '.';
61                         *digits = 0;
62                 }
63                 c = char_map[buf[i+1] >> 1];
64                 if (i == 5 && (c == 'C' || c == 'F'))
65                         *temp_unit = c;
66                 else if (c) {
67                         *p++ = c;
68                         (*digits)++;
69                 }
70         }
71         *p = 0;
72
73         if (*digits < 0)
74                 *digits = 0;
75
76         if ((ret = sr_atof_ascii(str, floatval))) {
77                 sr_dbg("invalid float string: '%s'", str);
78                 return ret;
79         }
80
81         return SR_OK;
82 }
83
84 static void brymen_bm86x_parse(unsigned char *buf, float *floatval,
85                                struct sr_datafeed_analog *analog)
86 {
87         char str[16], temp_unit;
88         int ret1, ret2, digits[2], over_limit;
89
90         ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0],
91                                          &temp_unit, &digits[0], 0x80);
92         over_limit = strstr(str, "0L") || strstr(str, "0.L");
93         ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1],
94                                          &temp_unit, &digits[1], 0x10);
95
96         /* main display */
97         if (ret1 == SR_OK || over_limit) {
98                 /* SI unit */
99                 if (buf[8] & 0x01) {
100                         analog[0].meaning->mq = SR_MQ_VOLTAGE;
101                         analog[0].meaning->unit = SR_UNIT_VOLT;
102                         if (!strcmp(str, "diod")) {
103                                 analog[0].meaning->mqflags |= SR_MQFLAG_DIODE;
104                                 analog[0].meaning->mqflags |= SR_MQFLAG_DC;
105                         }
106                 } else if (buf[14] & 0x80) {
107                         analog[0].meaning->mq = SR_MQ_CURRENT;
108                         analog[0].meaning->unit = SR_UNIT_AMPERE;
109                 } else if (buf[14] & 0x20) {
110                         analog[0].meaning->mq = SR_MQ_CAPACITANCE;
111                         analog[0].meaning->unit = SR_UNIT_FARAD;
112                 } else if (buf[14] & 0x10) {
113                         analog[0].meaning->mq = SR_MQ_CONDUCTANCE;
114                         analog[0].meaning->unit = SR_UNIT_SIEMENS;
115                 } else if (buf[15] & 0x01) {
116                         analog[0].meaning->mq = SR_MQ_FREQUENCY;
117                         analog[0].meaning->unit = SR_UNIT_HERTZ;
118                 } else if (buf[10] & 0x01) {
119                         analog[0].meaning->mq = SR_MQ_CONTINUITY;
120                         analog[0].meaning->unit = SR_UNIT_OHM;
121                 } else if (buf[15] & 0x10) {
122                         analog[0].meaning->mq = SR_MQ_RESISTANCE;
123                         analog[0].meaning->unit = SR_UNIT_OHM;
124                 } else if (buf[15] & 0x02) {
125                         analog[0].meaning->mq = SR_MQ_POWER;
126                         analog[0].meaning->unit = SR_UNIT_DECIBEL_MW;
127                 } else if (buf[15] & 0x80) {
128                         analog[0].meaning->mq = SR_MQ_DUTY_CYCLE;
129                         analog[0].meaning->unit = SR_UNIT_PERCENTAGE;
130                 } else if (buf[ 2] & 0x0A) {
131                         analog[0].meaning->mq = SR_MQ_TEMPERATURE;
132                         if (temp_unit == 'F')
133                                 analog[0].meaning->unit = SR_UNIT_FAHRENHEIT;
134                         else
135                                 analog[0].meaning->unit = SR_UNIT_CELSIUS;
136                 }
137
138                 /* when MIN MAX and AVG are displayed at the same time, remove them */
139                 if ((buf[1] & 0xE0) == 0xE0)
140                         buf[1] &= ~0xE0;
141
142                 /* AC/DC/Auto flags */
143                 if (buf[1] & 0x10)  analog[0].meaning->mqflags |= SR_MQFLAG_DC;
144                 if (buf[2] & 0x01)  analog[0].meaning->mqflags |= SR_MQFLAG_AC;
145                 if (buf[1] & 0x01)  analog[0].meaning->mqflags |= SR_MQFLAG_AUTORANGE;
146                 if (buf[1] & 0x08)  analog[0].meaning->mqflags |= SR_MQFLAG_HOLD;
147                 if (buf[1] & 0x20)  analog[0].meaning->mqflags |= SR_MQFLAG_MAX;
148                 if (buf[1] & 0x40)  analog[0].meaning->mqflags |= SR_MQFLAG_MIN;
149                 if (buf[1] & 0x80)  analog[0].meaning->mqflags |= SR_MQFLAG_AVG;
150                 if (buf[3] & 0x01)  analog[0].meaning->mqflags |= SR_MQFLAG_RELATIVE;
151
152                 /* when dBm is displayed, remove the m suffix so that it is
153                    not considered as the 10e-3 SI prefix */
154                 if (buf[15] & 0x02)
155                         buf[15] &= ~0x04;
156
157                 /* SI prefix */
158                 if (buf[14] & 0x40)  { floatval[0] *= 1e-9; digits[0] += 9; } /* n */
159                 if (buf[15] & 0x08)  { floatval[0] *= 1e-6; digits[0] += 6; } /* µ */
160                 if (buf[15] & 0x04)  { floatval[0] *= 1e-3; digits[0] += 3; } /* m */
161                 if (buf[15] & 0x40)  { floatval[0] *= 1e3;  digits[0] -= 3; } /* k */
162                 if (buf[15] & 0x20)  { floatval[0] *= 1e6;  digits[0] -= 6; } /* M */
163
164                 if (over_limit)      floatval[0] = INFINITY;
165
166                 analog[0].encoding->digits  = digits[0];
167                 analog[0].spec->spec_digits = digits[0];
168         }
169
170         /* secondary display */
171         if (ret2 == SR_OK) {
172                 /* SI unit */
173                 if (buf[14] & 0x08) {
174                         analog[1].meaning->mq = SR_MQ_VOLTAGE;
175                         analog[1].meaning->unit = SR_UNIT_VOLT;
176                 } else if (buf[9] & 0x04) {
177                         analog[1].meaning->mq = SR_MQ_CURRENT;
178                         analog[1].meaning->unit = SR_UNIT_AMPERE;
179                 } else if (buf[9] & 0x08) {
180                         analog[1].meaning->mq = SR_MQ_CURRENT;
181                         analog[1].meaning->unit = SR_UNIT_PERCENTAGE;
182                 } else if (buf[14] & 0x04) {
183                         analog[1].meaning->mq = SR_MQ_FREQUENCY;
184                         analog[1].meaning->unit = SR_UNIT_HERTZ;
185                 } else if (buf[9] & 0x40) {
186                         analog[1].meaning->mq = SR_MQ_TEMPERATURE;
187                         if (temp_unit == 'F')
188                                 analog[1].meaning->unit = SR_UNIT_FAHRENHEIT;
189                         else
190                                 analog[1].meaning->unit = SR_UNIT_CELSIUS;
191                 }
192
193                 /* AC flag */
194                 if (buf[9] & 0x20)  analog[1].meaning->mqflags |= SR_MQFLAG_AC;
195
196                 /* SI prefix */
197                 if (buf[ 9] & 0x01)  { floatval[1] *= 1e-6; digits[1] += 6; } /* µ */
198                 if (buf[ 9] & 0x02)  { floatval[1] *= 1e-3; digits[1] += 3; } /* m */
199                 if (buf[14] & 0x02)  { floatval[1] *= 1e3;  digits[1] -= 3; } /* k */
200                 if (buf[14] & 0x01)  { floatval[1] *= 1e6;  digits[1] -= 6; } /* M */
201
202                 analog[1].encoding->digits  = digits[1];
203                 analog[1].spec->spec_digits = digits[1];
204         }
205
206         if (buf[9] & 0x80)
207                 sr_spew("Battery is low.");
208 }
209
210 static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi,
211                                        unsigned char *buf)
212 {
213         struct dev_context *devc;
214         struct sr_datafeed_packet packet;
215         struct sr_datafeed_analog analog[2];
216         struct sr_analog_encoding encoding[2];
217         struct sr_analog_meaning meaning[2];
218         struct sr_analog_spec spec[2];
219         struct sr_channel *channel;
220         int sent_ch1, sent_ch2;
221         float floatval[2];
222
223         devc = sdi->priv;
224
225         /* Note: digits/spec_digits will be overridden later. */
226         sr_analog_init(&analog[0], &encoding[0], &meaning[0], &spec[0], 0);
227         sr_analog_init(&analog[1], &encoding[1], &meaning[1], &spec[1], 0);
228
229         brymen_bm86x_parse(buf, floatval, analog);
230         sent_ch1 = sent_ch2 = 0;
231
232         channel = sdi->channels->data;
233         if (analog[0].meaning->mq != 0 && channel->enabled) {
234                 /* Got a measurement. */
235                 sent_ch1 = 1;
236                 analog[0].num_samples = 1;
237                 analog[0].data = &floatval[0];
238                 analog[0].meaning->channels = g_slist_append(NULL, channel);
239                 packet.type = SR_DF_ANALOG;
240                 packet.payload = &analog[0];
241                 sr_session_send(sdi, &packet);
242                 g_slist_free(analog[0].meaning->channels);
243         }
244
245         channel = sdi->channels->next->data;
246         if (analog[1].meaning->mq != 0 && channel->enabled) {
247                 /* Got a measurement. */
248                 sent_ch2 = 1;
249                 analog[1].num_samples = 1;
250                 analog[1].data = &floatval[1];
251                 analog[1].meaning->channels = g_slist_append(NULL, channel);
252                 packet.type = SR_DF_ANALOG;
253                 packet.payload = &analog[1];
254                 sr_session_send(sdi, &packet);
255                 g_slist_free(analog[1].meaning->channels);
256         }
257
258         if (sent_ch1 || sent_ch2)
259                 sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
260 }
261
262 static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi)
263 {
264         struct sr_usb_dev_inst *usb;
265         unsigned char buf[] = { 0x00, 0x86, 0x66 };
266         int ret;
267
268         usb = sdi->conn;
269
270         sr_dbg("Sending HID set report.");
271         ret = libusb_control_transfer(usb->devhdl,
272                                       LIBUSB_REQUEST_TYPE_CLASS |
273                                       LIBUSB_RECIPIENT_INTERFACE |
274                                       LIBUSB_ENDPOINT_OUT,
275                                       9,     /* bRequest: HID set_report */
276                                       0x300, /* wValue: HID feature, report num 0 */
277                                       0,     /* wIndex: interface 0 */
278                                       buf, sizeof(buf), USB_TIMEOUT);
279
280         if (ret < 0) {
281                 sr_err("HID feature report error: %s.", libusb_error_name(ret));
282                 return SR_ERR;
283         }
284
285         if (ret != sizeof(buf)) {
286                 sr_err("Short packet: sent %d/%zu bytes.", ret, sizeof(buf));
287                 return SR_ERR;
288         }
289
290         return SR_OK;
291 }
292
293 static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi)
294 {
295         struct dev_context *devc;
296         struct sr_usb_dev_inst *usb;
297         unsigned char buf[24];
298         int ret, transferred;
299
300         devc = sdi->priv;
301         usb = sdi->conn;
302
303         sr_dbg("Reading HID interrupt report.");
304         /* Get data from EP1 using an interrupt transfer. */
305         ret = libusb_interrupt_transfer(usb->devhdl,
306                                         LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */
307                                         buf, sizeof(buf),
308                                         &transferred, USB_TIMEOUT);
309
310         if (ret == LIBUSB_ERROR_TIMEOUT) {
311                 if (++devc->interrupt_pending > 3)
312                         devc->interrupt_pending = 0;
313                 return SR_OK;
314         }
315
316         if (ret < 0) {
317                 sr_err("USB receive error: %s.", libusb_error_name(ret));
318                 return SR_ERR;
319         }
320
321         if (transferred != sizeof(buf)) {
322                 sr_err("Short packet: received %d/%zu bytes.", transferred, sizeof(buf));
323                 return SR_ERR;
324         }
325
326         devc->interrupt_pending = 0;
327         brymen_bm86x_handle_packet(sdi, buf);
328
329         return SR_OK;
330 }
331
332 SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data)
333 {
334         struct sr_dev_inst *sdi;
335         struct dev_context *devc;
336
337         (void)fd;
338         (void)revents;
339
340         if (!(sdi = cb_data))
341                 return TRUE;
342
343         if (!(devc = sdi->priv))
344                 return TRUE;
345
346         if (!devc->interrupt_pending) {
347                 if (brymen_bm86x_send_command(sdi))
348                         return FALSE;
349                 devc->interrupt_pending = 1;
350         }
351
352         if (brymen_bm86x_read_interrupt(sdi))
353                 return FALSE;
354
355         if (sr_sw_limits_check(&devc->sw_limits))
356                 sr_dev_acquisition_stop(sdi);
357
358         return TRUE;
359 }