]> sigrok.org Git - libsigrok.git/blob - src/hardware/brymen-dmm/parser.c
Build: Include <config.h> first in all source files
[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), 0);
83         if (written != sizeof(cmdout))
84                 return SR_ERR;
85
86         return SR_OK;
87 }
88
89 SR_PRIV int brymen_packet_request(struct sr_serial_dev_inst *serial)
90 {
91         return bm_send_command(BM_CMD_REQUEST_READING, 0, 0, serial);
92 }
93
94 SR_PRIV int brymen_packet_length(const uint8_t *buf, int *len)
95 {
96         struct brymen_header *hdr;
97         int packet_len;
98         size_t buflen;
99
100         buflen = *len;
101         hdr = (void *)buf;
102
103         /* Did we receive a complete header yet? */
104         if (buflen < sizeof(*hdr))
105                 return PACKET_NEED_MORE_DATA;
106
107         if (hdr->dle != 0x10 || hdr->stx != 0x02)
108                 return PACKET_INVALID_HEADER;
109
110         /* Our packet includes the header, the payload, and the tail. */
111         packet_len = sizeof(*hdr) + hdr->len + sizeof(struct brymen_tail);
112
113         /* In case we pick up an invalid header, limit our search. */
114         if (packet_len > MAX_PACKET_LEN) {
115                 sr_spew("Header specifies an invalid payload length: %i.",
116                         hdr->len);
117                 return PACKET_INVALID_HEADER;
118         }
119
120         *len = packet_len;
121         sr_spew("Expecting a %d-byte packet.", *len);
122         return PACKET_HEADER_OK;
123 }
124
125 SR_PRIV gboolean brymen_packet_is_valid(const uint8_t *buf)
126 {
127         struct brymen_header *hdr;
128         struct brymen_tail *tail;
129         int i;
130         uint8_t chksum = 0;
131         uint8_t *payload;
132         
133         payload = (uint8_t *)(buf + sizeof(struct brymen_header));
134
135         hdr = (void *)buf;
136         tail = (void *)(payload + hdr->len);
137         
138         for (i = 0; i< hdr->len; i++)
139                 chksum ^= payload[i];
140         
141         if (tail->checksum != chksum) {
142                 sr_dbg("Packet has invalid checksum 0x%.2x. Expected 0x%.2x.",
143                        chksum, tail->checksum);
144                 return FALSE;
145         }
146         
147         return TRUE;
148 }
149
150 static int parse_value(const char *strbuf, int len, float *floatval)
151 {
152         int s, d;
153         char str[32];
154
155         if (strstr(strbuf, "OL")) {
156                 sr_dbg("Overlimit.");
157                 *floatval = INFINITY;
158                 return SR_OK;
159         }
160
161         memset(str, 0, sizeof(str));
162         /* Spaces may interfere with parsing the exponent. Strip them. */
163         for (s = 0, d = 0; s < len; s++) {
164                 if (strbuf[s] != ' ')
165                         str[d++] = strbuf[s];
166         }
167         if (sr_atof_ascii(str, floatval) != SR_OK)
168                 return SR_ERR;
169
170         return SR_OK;
171 }
172
173 static void parse_flags(const uint8_t *buf, struct brymen_flags *info)
174 {
175         info->is_low_batt       = (buf[4 + 3] & (1 << 7)) != 0;
176
177         info->is_decibel        = (buf[4 + 1] & (1 << 5)) != 0;
178         info->is_duty_cycle     = (buf[4 + 1] & (1 << 3)) != 0;
179         info->is_hertz          = (buf[4 + 1] & (1 << 2)) != 0;
180         info->is_amp            = (buf[4 + 1] & (1 << 1)) != 0;
181         info->is_beep           = (buf[4 + 1] & (1 << 0)) != 0;
182
183         info->is_ohm            = (buf[4 + 0] & (1 << 7)) != 0;
184         info->is_fahrenheit     = (buf[4 + 0] & (1 << 6)) != 0;
185         info->is_celsius        = (buf[4 + 0] & (1 << 5)) != 0;
186         info->is_diode          = (buf[4 + 0] & (1 << 4)) != 0;
187         info->is_capacitance    = (buf[4 + 0] & (1 << 3)) != 0;
188         info->is_volt           = (buf[4 + 0] & (1 << 2)) != 0;
189         info->is_dc             = (buf[4 + 0] & (1 << 1)) != 0;
190         info->is_ac             = (buf[4 + 0] & (1 << 0)) != 0;
191 }
192
193 SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
194                 struct sr_datafeed_analog *analog, void *info)
195 {
196         struct brymen_flags flags;
197         struct brymen_header *hdr;
198         uint8_t *bfunc;
199         int asciilen;
200
201         (void)info;
202
203         hdr = (void *)buf;
204         bfunc = (uint8_t *)(buf + sizeof(struct brymen_header));
205
206         analog->mqflags = 0;
207
208         /* Give some debug info about the package. */
209         asciilen = hdr->len - 4;
210         sr_dbg("DMM flags: %.2x %.2x %.2x %.2x",
211                bfunc[3], bfunc[2], bfunc[1], bfunc[0]);
212         /* Value is an ASCII string. */
213         sr_dbg("DMM packet: \"%.*s\"", asciilen, bfunc + 4);
214
215         parse_flags(buf, &flags);
216         if (parse_value((const char *)(bfunc + 4), asciilen, floatval) != SR_OK)
217                 return SR_ERR;
218
219         if (flags.is_volt) {
220                 analog->mq = SR_MQ_VOLTAGE;
221                 analog->unit = SR_UNIT_VOLT;
222         }
223         if (flags.is_amp) {
224                 analog->mq = SR_MQ_CURRENT;
225                 analog->unit = SR_UNIT_AMPERE;
226         }
227         if (flags.is_ohm) {
228                 if (flags.is_beep)
229                         analog->mq = SR_MQ_CONTINUITY;
230                 else
231                         analog->mq = SR_MQ_RESISTANCE;
232                 analog->unit = SR_UNIT_OHM;
233         }
234         if (flags.is_hertz) {
235                 analog->mq = SR_MQ_FREQUENCY;
236                 analog->unit = SR_UNIT_HERTZ;
237         }
238         if (flags.is_duty_cycle) {
239                 analog->mq = SR_MQ_DUTY_CYCLE;
240                 analog->unit = SR_UNIT_PERCENTAGE;
241         }
242         if (flags.is_capacitance) {
243                 analog->mq = SR_MQ_CAPACITANCE;
244                 analog->unit = SR_UNIT_FARAD;
245         }
246         if (flags.is_fahrenheit) {
247                 analog->mq = SR_MQ_TEMPERATURE;
248                 analog->unit = SR_UNIT_FAHRENHEIT;
249         }
250         if (flags.is_celsius) {
251                 analog->mq = SR_MQ_TEMPERATURE;
252                 analog->unit = SR_UNIT_CELSIUS;
253         }
254         if (flags.is_capacitance) {
255                 analog->mq = SR_MQ_CAPACITANCE;
256                 analog->unit = SR_UNIT_FARAD;
257         }
258
259         /*
260          * The high-end Brymen models have a configurable reference impedance.
261          * When the reference impedance is changed, the DMM sends one packet
262          * with the value of the new reference impedance. Both decibel and ohm
263          * flags are set in this case, so we must be careful to correctly
264          * identify the value as ohm, not dBmW.
265          */
266         if (flags.is_decibel && !flags.is_ohm) {
267                 analog->mq = SR_MQ_POWER;
268                 analog->unit = SR_UNIT_DECIBEL_MW;
269                 /*
270                  * For some reason, dBm measurements are sent by the multimeter
271                  * with a value three orders of magnitude smaller than the
272                  * displayed value.
273                  */
274                 *floatval *= 1000;
275         }
276
277         if (flags.is_diode)
278                 analog->mqflags |= SR_MQFLAG_DIODE;
279         /* We can have both AC+DC in a single measurement. */
280         if (flags.is_ac)
281                 analog->mqflags |= SR_MQFLAG_AC;
282         if (flags.is_dc)
283                 analog->mqflags |= SR_MQFLAG_DC;
284
285         if (flags.is_low_batt)
286                 sr_info("Low battery!");
287
288         return SR_OK;
289 }