]> sigrok.org Git - libsigrok.git/blame - src/hardware/brymen-bm86x/protocol.c
Add sr_dev_acquisition_stop(), factor out SR_ERR_DEV_CLOSED check.
[libsigrok.git] / src / hardware / brymen-bm86x / protocol.c
CommitLineData
8d9c8554
AJ
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
6ec6c43b 20#include <config.h>
ecaa89af
AJ
21#include <string.h>
22#include <math.h>
8d9c8554
AJ
23#include "protocol.h"
24
ecaa89af
AJ
25#define USB_TIMEOUT 500
26
329733d9 27static const char char_map[128] = {
ecaa89af
AJ
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
47static int brymen_bm86x_parse_digits(const unsigned char *buf, int length,
48 char *str, float *floatval,
1baf30f5 49 char *temp_unit, int *digits, int flag)
ecaa89af
AJ
50{
51 char c, *p = str;
52 int i, ret;
53
1baf30f5
AJ
54 *digits = INT_MIN;
55
ecaa89af
AJ
56 if (buf[0] & flag)
57 *p++ = '-';
58 for (i = 0; i < length; i++) {
1baf30f5 59 if (i && i < 5 && buf[i+1] & 0x01) {
ecaa89af 60 *p++ = '.';
1baf30f5
AJ
61 *digits = 0;
62 }
ecaa89af
AJ
63 c = char_map[buf[i+1] >> 1];
64 if (i == 5 && (c == 'C' || c == 'F'))
65 *temp_unit = c;
1baf30f5 66 else if (c) {
ecaa89af 67 *p++ = c;
1baf30f5
AJ
68 (*digits)++;
69 }
ecaa89af
AJ
70 }
71 *p = 0;
72
1baf30f5
AJ
73 if (*digits < 0)
74 *digits = 0;
75
ecaa89af
AJ
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
84static void brymen_bm86x_parse(unsigned char *buf, float *floatval,
a46f869b 85 struct sr_datafeed_analog *analog)
ecaa89af
AJ
86{
87 char str[16], temp_unit;
1baf30f5 88 int ret1, ret2, digits[2], over_limit;
ecaa89af
AJ
89
90 ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0],
1baf30f5 91 &temp_unit, &digits[0], 0x80);
ecaa89af
AJ
92 over_limit = strstr(str, "0L") || strstr(str, "0.L");
93 ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1],
1baf30f5 94 &temp_unit, &digits[1], 0x10);
ecaa89af
AJ
95
96 /* main display */
97 if (ret1 == SR_OK || over_limit) {
98 /* SI unit */
99 if (buf[8] & 0x01) {
a46f869b
UH
100 analog[0].meaning->mq = SR_MQ_VOLTAGE;
101 analog[0].meaning->unit = SR_UNIT_VOLT;
ecaa89af 102 if (!strcmp(str, "diod"))
a46f869b 103 analog[0].meaning->mqflags |= SR_MQFLAG_DIODE;
ecaa89af 104 } else if (buf[14] & 0x80) {
a46f869b
UH
105 analog[0].meaning->mq = SR_MQ_CURRENT;
106 analog[0].meaning->unit = SR_UNIT_AMPERE;
ecaa89af 107 } else if (buf[14] & 0x20) {
a46f869b
UH
108 analog[0].meaning->mq = SR_MQ_CAPACITANCE;
109 analog[0].meaning->unit = SR_UNIT_FARAD;
ecaa89af 110 } else if (buf[14] & 0x10) {
a46f869b
UH
111 analog[0].meaning->mq = SR_MQ_CONDUCTANCE;
112 analog[0].meaning->unit = SR_UNIT_SIEMENS;
ecaa89af 113 } else if (buf[15] & 0x01) {
a46f869b
UH
114 analog[0].meaning->mq = SR_MQ_FREQUENCY;
115 analog[0].meaning->unit = SR_UNIT_HERTZ;
ecaa89af 116 } else if (buf[10] & 0x01) {
a46f869b
UH
117 analog[0].meaning->mq = SR_MQ_CONTINUITY;
118 analog[0].meaning->unit = SR_UNIT_OHM;
ecaa89af 119 } else if (buf[15] & 0x10) {
a46f869b
UH
120 analog[0].meaning->mq = SR_MQ_RESISTANCE;
121 analog[0].meaning->unit = SR_UNIT_OHM;
ecaa89af 122 } else if (buf[15] & 0x02) {
a46f869b
UH
123 analog[0].meaning->mq = SR_MQ_POWER;
124 analog[0].meaning->unit = SR_UNIT_DECIBEL_MW;
ecaa89af 125 } else if (buf[15] & 0x80) {
a46f869b
UH
126 analog[0].meaning->mq = SR_MQ_DUTY_CYCLE;
127 analog[0].meaning->unit = SR_UNIT_PERCENTAGE;
ecaa89af 128 } else if (buf[ 2] & 0x0A) {
a46f869b 129 analog[0].meaning->mq = SR_MQ_TEMPERATURE;
ecaa89af 130 if (temp_unit == 'F')
a46f869b 131 analog[0].meaning->unit = SR_UNIT_FAHRENHEIT;
ecaa89af 132 else
a46f869b 133 analog[0].meaning->unit = SR_UNIT_CELSIUS;
ecaa89af
AJ
134 }
135
136 /* when MIN MAX and AVG are displayed at the same time, remove them */
137 if ((buf[1] & 0xE0) == 0xE0)
138 buf[1] &= ~0xE0;
139
140 /* AC/DC/Auto flags */
a46f869b
UH
141 if (buf[1] & 0x10) analog[0].meaning->mqflags |= SR_MQFLAG_DC;
142 if (buf[2] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AC;
143 if (buf[1] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AUTORANGE;
144 if (buf[1] & 0x08) analog[0].meaning->mqflags |= SR_MQFLAG_HOLD;
145 if (buf[1] & 0x20) analog[0].meaning->mqflags |= SR_MQFLAG_MAX;
146 if (buf[1] & 0x40) analog[0].meaning->mqflags |= SR_MQFLAG_MIN;
147 if (buf[1] & 0x80) analog[0].meaning->mqflags |= SR_MQFLAG_AVG;
148 if (buf[3] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_RELATIVE;
ecaa89af
AJ
149
150 /* when dBm is displayed, remove the m suffix so that it is
151 not considered as the 10e-3 SI prefix */
152 if (buf[15] & 0x02)
153 buf[15] &= ~0x04;
154
155 /* SI prefix */
d9251a2c
UH
156 if (buf[14] & 0x40) { floatval[0] *= 1e-9; digits[0] += 9; } /* n */
157 if (buf[15] & 0x08) { floatval[0] *= 1e-6; digits[0] += 6; } /* µ */
158 if (buf[15] & 0x04) { floatval[0] *= 1e-3; digits[0] += 3; } /* m */
159 if (buf[15] & 0x40) { floatval[0] *= 1e3; digits[0] -= 3; } /* k */
160 if (buf[15] & 0x20) { floatval[0] *= 1e6; digits[0] -= 6; } /* M */
ecaa89af
AJ
161
162 if (over_limit) floatval[0] = INFINITY;
1baf30f5
AJ
163
164 analog[0].encoding->digits = digits[0];
165 analog[0].spec->spec_digits = digits[0];
ecaa89af
AJ
166 }
167
168 /* secondary display */
169 if (ret2 == SR_OK) {
170 /* SI unit */
171 if (buf[14] & 0x08) {
a46f869b
UH
172 analog[1].meaning->mq = SR_MQ_VOLTAGE;
173 analog[1].meaning->unit = SR_UNIT_VOLT;
ecaa89af 174 } else if (buf[9] & 0x04) {
a46f869b
UH
175 analog[1].meaning->mq = SR_MQ_CURRENT;
176 analog[1].meaning->unit = SR_UNIT_AMPERE;
af1e487e 177 } else if (buf[9] & 0x08) {
a46f869b
UH
178 analog[1].meaning->mq = SR_MQ_CURRENT;
179 analog[1].meaning->unit = SR_UNIT_PERCENTAGE;
ecaa89af 180 } else if (buf[14] & 0x04) {
a46f869b
UH
181 analog[1].meaning->mq = SR_MQ_FREQUENCY;
182 analog[1].meaning->unit = SR_UNIT_HERTZ;
ecaa89af 183 } else if (buf[9] & 0x40) {
a46f869b 184 analog[1].meaning->mq = SR_MQ_TEMPERATURE;
ecaa89af 185 if (temp_unit == 'F')
a46f869b 186 analog[1].meaning->unit = SR_UNIT_FAHRENHEIT;
ecaa89af 187 else
a46f869b 188 analog[1].meaning->unit = SR_UNIT_CELSIUS;
ecaa89af
AJ
189 }
190
191 /* AC flag */
a46f869b 192 if (buf[9] & 0x20) analog[1].meaning->mqflags |= SR_MQFLAG_AC;
ecaa89af
AJ
193
194 /* SI prefix */
d9251a2c
UH
195 if (buf[ 9] & 0x01) { floatval[1] *= 1e-6; digits[1] += 6; } /* µ */
196 if (buf[ 9] & 0x02) { floatval[1] *= 1e-3; digits[1] += 3; } /* m */
197 if (buf[14] & 0x02) { floatval[1] *= 1e3; digits[1] -= 3; } /* k */
198 if (buf[14] & 0x01) { floatval[1] *= 1e6; digits[1] -= 6; } /* M */
1baf30f5
AJ
199
200 analog[1].encoding->digits = digits[1];
201 analog[1].spec->spec_digits = digits[1];
ecaa89af
AJ
202 }
203
204 if (buf[9] & 0x80)
205 sr_spew("Battery is low.");
206}
207
208static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi,
209 unsigned char *buf)
210{
211 struct dev_context *devc;
212 struct sr_datafeed_packet packet;
a46f869b
UH
213 struct sr_datafeed_analog analog[2];
214 struct sr_analog_encoding encoding[2];
215 struct sr_analog_meaning meaning[2];
216 struct sr_analog_spec spec[2];
ecaa89af
AJ
217 float floatval[2];
218
219 devc = sdi->priv;
220
869c8375 221 /* Note: digits/spec_digits will be overridden later. */
a46f869b
UH
222 sr_analog_init(&analog[0], &encoding[0], &meaning[0], &spec[0], 0);
223 sr_analog_init(&analog[1], &encoding[1], &meaning[1], &spec[1], 0);
ecaa89af 224
ecaa89af
AJ
225 brymen_bm86x_parse(buf, floatval, analog);
226
a46f869b 227 if (analog[0].meaning->mq != 0) {
ecaa89af
AJ
228 /* Got a measurement. */
229 analog[0].num_samples = 1;
230 analog[0].data = &floatval[0];
a46f869b
UH
231 analog[0].meaning->channels = g_slist_append(NULL, sdi->channels->data);
232 packet.type = SR_DF_ANALOG;
ecaa89af 233 packet.payload = &analog[0];
102f1239 234 sr_session_send(sdi, &packet);
a46f869b 235 g_slist_free(analog[0].meaning->channels);
ecaa89af
AJ
236 }
237
a46f869b 238 if (analog[1].meaning->mq != 0) {
ecaa89af
AJ
239 /* Got a measurement. */
240 analog[1].num_samples = 1;
241 analog[1].data = &floatval[1];
a46f869b
UH
242 analog[1].meaning->channels = g_slist_append(NULL, sdi->channels->next->data);
243 packet.type = SR_DF_ANALOG;
ecaa89af 244 packet.payload = &analog[1];
102f1239 245 sr_session_send(sdi, &packet);
a46f869b 246 g_slist_free(analog[1].meaning->channels);
ecaa89af
AJ
247 }
248
a46f869b 249 if (analog[0].meaning->mq != 0 || analog[1].meaning->mq != 0)
ab939ebb 250 sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
ecaa89af
AJ
251}
252
253static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi)
254{
255 struct sr_usb_dev_inst *usb;
256 unsigned char buf[] = { 0x00, 0x86, 0x66 };
257 int ret;
258
259 usb = sdi->conn;
260
261 sr_dbg("Sending HID set report.");
262 ret = libusb_control_transfer(usb->devhdl,
d9251a2c 263 LIBUSB_REQUEST_TYPE_CLASS |
ecaa89af
AJ
264 LIBUSB_RECIPIENT_INTERFACE |
265 LIBUSB_ENDPOINT_OUT,
266 9, /* bRequest: HID set_report */
267 0x300, /* wValue: HID feature, report num 0 */
268 0, /* wIndex: interface 0 */
269 buf, sizeof(buf), USB_TIMEOUT);
270
271 if (ret < 0) {
272 sr_err("HID feature report error: %s.", libusb_error_name(ret));
273 return SR_ERR;
274 }
275
276 if (ret != sizeof(buf)) {
0c536bcd 277 sr_err("Short packet: sent %d/%zu bytes.", ret, sizeof(buf));
ecaa89af
AJ
278 return SR_ERR;
279 }
280
281 return SR_OK;
282}
283
284static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi)
285{
286 struct dev_context *devc;
287 struct sr_usb_dev_inst *usb;
288 unsigned char buf[24];
289 int ret, transferred;
290
291 devc = sdi->priv;
292 usb = sdi->conn;
293
294 sr_dbg("Reading HID interrupt report.");
295 /* Get data from EP1 using an interrupt transfer. */
296 ret = libusb_interrupt_transfer(usb->devhdl,
297 LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */
298 buf, sizeof(buf),
299 &transferred, USB_TIMEOUT);
300
301 if (ret == LIBUSB_ERROR_TIMEOUT) {
302 if (++devc->interrupt_pending > 3)
303 devc->interrupt_pending = 0;
304 return SR_OK;
305 }
306
307 if (ret < 0) {
308 sr_err("USB receive error: %s.", libusb_error_name(ret));
309 return SR_ERR;
310 }
311
312 if (transferred != sizeof(buf)) {
6433156c 313 sr_err("Short packet: received %d/%zu bytes.", transferred, sizeof(buf));
ecaa89af
AJ
314 return SR_ERR;
315 }
316
317 devc->interrupt_pending = 0;
318 brymen_bm86x_handle_packet(sdi, buf);
319
320 return SR_OK;
321}
322
8d9c8554
AJ
323SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data)
324{
ecaa89af 325 struct sr_dev_inst *sdi;
8d9c8554
AJ
326 struct dev_context *devc;
327
328 (void)fd;
ecaa89af 329 (void)revents;
8d9c8554
AJ
330
331 if (!(sdi = cb_data))
332 return TRUE;
333
334 if (!(devc = sdi->priv))
335 return TRUE;
336
ecaa89af
AJ
337 if (!devc->interrupt_pending) {
338 if (brymen_bm86x_send_command(sdi))
339 return FALSE;
340 devc->interrupt_pending = 1;
341 }
342
343 if (brymen_bm86x_read_interrupt(sdi))
344 return FALSE;
345
ab939ebb 346 if (sr_sw_limits_check(&devc->sw_limits))
d2f7c417 347 sr_dev_acquisition_stop(sdi);
8d9c8554
AJ
348
349 return TRUE;
350}