]> sigrok.org Git - libsigrok.git/blob - src/hardware/brymen-dmm/parser.c
brymen-dmm: unbreak dBm reference impedance interpretation
[libsigrok.git] / src / hardware / brymen-dmm / parser.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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 "protocol.h"
22
23 #define MAX_PACKET_LEN 22
24
25 /* Flags passed from the DMM. */
26 struct brymen_flags {
27         gboolean is_low_batt, is_decibel, is_duty_cycle, is_hertz, is_amp;
28         gboolean is_beep, is_ohm, is_fahrenheit, is_celsius, is_capacitance;
29         gboolean is_diode, is_volt, is_dc, is_ac;
30 };
31
32 struct bm850_command {
33         uint8_t dle;
34         uint8_t stx;
35         uint8_t cmd;
36         uint8_t arg[2];
37         uint8_t checksum;
38         uint8_t dle2;
39         uint8_t etx;
40 };
41
42 struct brymen_header {
43         uint8_t dle;
44         uint8_t stx;
45         uint8_t cmd;
46         uint8_t len;
47 };
48
49 struct brymen_tail {
50         uint8_t checksum;
51         uint8_t dle;
52         uint8_t etx;
53 };
54
55 /*
56  * We only have one command because we only support the BM-857. However, the
57  * driver is easily extensible to support more models, as the protocols are
58  * very similar.
59  */
60 enum {
61         BM_CMD_REQUEST_READING = 0x00,
62 };
63
64 static int bm_send_command(uint8_t command, uint8_t arg1, uint8_t arg2,
65                            struct sr_serial_dev_inst *serial)
66 {
67         struct bm850_command cmdout;
68         int written;
69
70         cmdout.dle = 0x10;
71         cmdout.stx = 0x02;
72         cmdout.cmd = command;
73         cmdout.arg[0] = arg1;
74         cmdout.arg[1] = arg2;
75         cmdout.checksum = arg1 ^ arg2;
76         cmdout.dle2 = 0x10;
77         cmdout.etx = 0x03;
78
79         /* TODO: How to compute the checksum? Hardware seems to ignore it. */
80
81         /* Request reading. */
82         written = serial_write_blocking(serial, &cmdout, sizeof(cmdout),
83                         serial_timeout(serial, sizeof(cmdout)));
84         if (written != sizeof(cmdout))
85                 return SR_ERR;
86
87         return SR_OK;
88 }
89
90 SR_PRIV int brymen_packet_request(struct sr_serial_dev_inst *serial)
91 {
92         return bm_send_command(BM_CMD_REQUEST_READING, 0, 0, serial);
93 }
94
95 SR_PRIV int brymen_packet_length(const uint8_t *buf, int *len)
96 {
97         struct brymen_header *hdr;
98         int packet_len;
99         size_t buflen;
100
101         buflen = *len;
102         hdr = (void *)buf;
103
104         /* Did we receive a complete header yet? */
105         if (buflen < sizeof(*hdr))
106                 return PACKET_NEED_MORE_DATA;
107
108         if (hdr->dle != 0x10 || hdr->stx != 0x02)
109                 return PACKET_INVALID_HEADER;
110
111         /* Our packet includes the header, the payload, and the tail. */
112         packet_len = sizeof(*hdr) + hdr->len + sizeof(struct brymen_tail);
113
114         /* In case we pick up an invalid header, limit our search. */
115         if (packet_len > MAX_PACKET_LEN) {
116                 sr_spew("Header specifies an invalid payload length: %i.",
117                         hdr->len);
118                 return PACKET_INVALID_HEADER;
119         }
120
121         *len = packet_len;
122         sr_spew("Expecting a %d-byte packet.", *len);
123         return PACKET_HEADER_OK;
124 }
125
126 SR_PRIV gboolean brymen_packet_is_valid(const uint8_t *buf)
127 {
128         struct brymen_header *hdr;
129         struct brymen_tail *tail;
130         int i;
131         uint8_t chksum = 0;
132         uint8_t *payload;
133
134         payload = (uint8_t *)(buf + sizeof(struct brymen_header));
135
136         hdr = (void *)buf;
137         tail = (void *)(payload + hdr->len);
138
139         for (i = 0; i< hdr->len; i++)
140                 chksum ^= payload[i];
141
142         if (tail->checksum != chksum) {
143                 sr_dbg("Packet has invalid checksum 0x%.2x. Expected 0x%.2x.",
144                        chksum, tail->checksum);
145                 return FALSE;
146         }
147
148         return TRUE;
149 }
150
151 static int parse_value(const char *txt, size_t len, float *floatval)
152 {
153         const char *txt_end;
154         char c, buf[32], *dst;
155         int ret;
156
157         /*
158          * The input text is not NUL terminated, the checksum follows
159          * the value text field. Spaces may interfere with the text to
160          * number conversion, especially with exponent parsing. Copy the
161          * input data to a terminated text buffer and strip spaces in the
162          * process, before running ASCIIZ string operations.
163          */
164         if (len >= sizeof(buf)) {
165                 sr_err("Insufficient text conversion buffer size.");
166                 return SR_ERR_BUG;
167         }
168         txt_end = txt + len;
169         dst = &buf[0];
170         while (txt < txt_end && *txt) {
171                 c = *txt++;
172                 if (c == ' ')
173                         continue;
174                 *dst++ = c;
175         }
176         *dst = '\0';
177
178         /* Check for overflow, or get the number value. */
179         if (strstr(buf, "+OL")) {
180                 *floatval = +INFINITY;
181                 return SR_OK;
182         }
183         if (strstr(buf, "-OL")) {
184                 *floatval = -INFINITY;
185                 return SR_OK;
186         }
187         if (strstr(buf, "OL")) {
188                 *floatval = INFINITY;
189                 return SR_OK;
190         }
191         ret = sr_atof_ascii(buf, floatval);
192         if (ret != SR_OK)
193                 return ret;
194
195         return SR_OK;
196 }
197
198 static void parse_flags(const uint8_t *bfunc, struct brymen_flags *info)
199 {
200         info->is_low_batt       = (bfunc[3] & (1 << 7)) != 0;
201
202         info->is_decibel        = (bfunc[1] & (1 << 5)) != 0;
203         info->is_duty_cycle     = (bfunc[1] & (1 << 3)) != 0;
204         info->is_hertz          = (bfunc[1] & (1 << 2)) != 0;
205         info->is_amp            = (bfunc[1] & (1 << 1)) != 0;
206         info->is_beep           = (bfunc[1] & (1 << 0)) != 0;
207
208         info->is_ohm            = (bfunc[0] & (1 << 7)) != 0;
209         info->is_fahrenheit     = (bfunc[0] & (1 << 6)) != 0;
210         info->is_celsius        = (bfunc[0] & (1 << 5)) != 0;
211         info->is_diode          = (bfunc[0] & (1 << 4)) != 0;
212         info->is_capacitance    = (bfunc[0] & (1 << 3)) != 0;
213         info->is_volt           = (bfunc[0] & (1 << 2)) != 0;
214         info->is_dc             = (bfunc[0] & (1 << 1)) != 0;
215         info->is_ac             = (bfunc[0] & (1 << 0)) != 0;
216 }
217
218 SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
219                 struct sr_datafeed_analog *analog, void *info)
220 {
221         struct brymen_flags flags;
222         struct brymen_header *hdr;
223         uint8_t *bfunc;
224         const char *txt;
225         int txtlen;
226         char *p;
227         char *unit;
228         int ret;
229
230         (void)info;
231
232         hdr = (void *)buf;
233         bfunc = (uint8_t *)(buf + sizeof(struct brymen_header));
234         txt = (const char *)&bfunc[4];
235         txtlen = hdr->len - 4;
236         sr_dbg("DMM bfunc: %02x %02x %02x %02x, text '%.*s'",
237                 bfunc[3], bfunc[2], bfunc[1], bfunc[0], txtlen, txt);
238
239         memset(&flags, 0, sizeof(flags));
240         parse_flags(bfunc, &flags);
241         if (flags.is_decibel && flags.is_ohm) {
242                 /*
243                  * The reference impedance for the dBm function is in an
244                  * unexpected format. Naive conversion of non-space chars
245                  * gives incorrect results. Isolate the 4..1200 Ohms value
246                  * instead, ignore the "0." and exponent parts of the
247                  * response text.
248                  */
249                 if (strncmp(txt, " 0.", strlen(" 0.")) == 0 && strstr(txt, " E")) {
250                         txt = &txt[strlen(" 0.")];
251                         txtlen -= strlen(" 0.");
252                         p = strchr(txt, 'E');
253                         if (p)
254                                 *p = '\0';
255                 }
256         }
257         if (flags.is_fahrenheit || flags.is_celsius) {
258                 /*
259                  * The value text in temperature mode includes the C/F
260                  * suffix between the mantissa and the exponent, which
261                  * breaks the text to number conversion. Example data:
262                  * " 0.0217CE+3". Remove the C/F unit identifier.
263                  */
264                 unit = strchr(txt, flags.is_fahrenheit ? 'F' : 'C');
265                 if (!unit)
266                         return SR_ERR;
267                 *unit = ' ';
268         }
269         ret = parse_value(txt, txtlen, floatval);
270         sr_dbg("floatval: %f, ret %d", *floatval, ret);
271         if (ret != SR_OK)
272                 return SR_ERR;
273
274         analog->meaning->mqflags = 0;
275         if (flags.is_volt) {
276                 analog->meaning->mq = SR_MQ_VOLTAGE;
277                 analog->meaning->unit = SR_UNIT_VOLT;
278         }
279         if (flags.is_amp) {
280                 analog->meaning->mq = SR_MQ_CURRENT;
281                 analog->meaning->unit = SR_UNIT_AMPERE;
282         }
283         if (flags.is_ohm) {
284                 if (flags.is_decibel)
285                         analog->meaning->mq = SR_MQ_RESISTANCE;
286                 else if (flags.is_beep)
287                         analog->meaning->mq = SR_MQ_CONTINUITY;
288                 else
289                         analog->meaning->mq = SR_MQ_RESISTANCE;
290                 analog->meaning->unit = SR_UNIT_OHM;
291         }
292         if (flags.is_hertz) {
293                 analog->meaning->mq = SR_MQ_FREQUENCY;
294                 analog->meaning->unit = SR_UNIT_HERTZ;
295         }
296         if (flags.is_duty_cycle) {
297                 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
298                 analog->meaning->unit = SR_UNIT_PERCENTAGE;
299         }
300         if (flags.is_capacitance) {
301                 analog->meaning->mq = SR_MQ_CAPACITANCE;
302                 analog->meaning->unit = SR_UNIT_FARAD;
303         }
304         if (flags.is_fahrenheit) {
305                 analog->meaning->mq = SR_MQ_TEMPERATURE;
306                 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
307         }
308         if (flags.is_celsius) {
309                 analog->meaning->mq = SR_MQ_TEMPERATURE;
310                 analog->meaning->unit = SR_UNIT_CELSIUS;
311         }
312         if (flags.is_capacitance) {
313                 analog->meaning->mq = SR_MQ_CAPACITANCE;
314                 analog->meaning->unit = SR_UNIT_FARAD;
315         }
316
317         /*
318          * The high-end Brymen models have a configurable reference
319          * impedance for dBm measurements. When the meter's function
320          * is entered, or when the reference impedance is changed, the
321          * meter sends one packet with the value of the new reference.
322          * Both decibel and ohm flags are set in this case, so we must
323          * be careful to not clobber the resistance value from above,
324          * and only provide dBm when the measurement is shown and not
325          * its reference.
326          *
327          * The meter's response values also use an unexpected scale
328          * (always off by factor 1000, as if it was Watts not mW).
329          *
330          * Example responses:
331          * bfunc: 00 00 20 80, text ' 0. 800 E+1' (reference)
332          * bfunc: 00 00 20 00, text '-0.3702 E-1' (measurement)
333          */
334         if (flags.is_decibel && !flags.is_ohm) {
335                 analog->meaning->mq = SR_MQ_POWER;
336                 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
337                 *floatval *= 1000;
338         }
339
340         if (flags.is_diode)
341                 analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
342         /* We can have both AC+DC in a single measurement. */
343         if (flags.is_ac)
344                 analog->meaning->mqflags |= SR_MQFLAG_AC;
345         if (flags.is_dc)
346                 analog->meaning->mqflags |= SR_MQFLAG_DC;
347
348         if (flags.is_low_batt)
349                 sr_warn("Low battery!");
350
351         return SR_OK;
352 }