]> sigrok.org Git - libsigrok.git/blame - hardware/brymen-dmm/parser.c
build: Portability fixes.
[libsigrok.git] / hardware / brymen-dmm / parser.c
CommitLineData
601fb67c 1/*
c5d6f5cc 2 * This file is part of the libsigrok project.
601fb67c
AG
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 "protocol.h"
601fb67c 21
c5d6f5cc
UH
22#define MAX_PACKET_LEN 22
23
24/* Flags passed from the DMM. */
601fb67c 25struct brymen_flags {
c5d6f5cc
UH
26 gboolean is_low_batt, is_decibel, is_duty_cycle, is_hertz, is_amp;
27 gboolean is_beep, is_ohm, is_fahrenheit, is_celsius, is_capacitance;
28 gboolean is_diode, is_volt, is_dc, is_ac;
601fb67c
AG
29};
30
31struct bm850_command {
32 uint8_t dle;
33 uint8_t stx;
34 uint8_t cmd;
35 uint8_t arg[2];
36 uint8_t checksum;
37 uint8_t dle2;
38 uint8_t etx;
39};
40
41struct brymen_header {
42 uint8_t dle;
43 uint8_t stx;
44 uint8_t cmd;
45 uint8_t len;
46};
47
48struct brymen_tail {
49 uint8_t checksum;
50 uint8_t dle;
51 uint8_t etx;
52};
53
54/*
55 * We only have one command because we only support the BM-857. However, the
c5d6f5cc
UH
56 * driver is easily extensible to support more models, as the protocols are
57 * very similar.
601fb67c
AG
58 */
59enum {
60 BM_CMD_REQUEST_READING = 0x00,
61};
62
601fb67c
AG
63static int bm_send_command(uint8_t command, uint8_t arg1, uint8_t arg2,
64 struct sr_serial_dev_inst *serial)
65{
c5d6f5cc
UH
66 struct bm850_command cmdout;
67 int written;
68
69 cmdout.dle = 0x10;
70 cmdout.stx = 0x02;
71 cmdout.cmd = command;
72 cmdout.arg[0] = arg1;
73 cmdout.arg[1] = arg2;
74 cmdout.checksum = arg1 ^ arg2;
75 cmdout.dle2 = 0x10;
76 cmdout.etx = 0x03;
77
78 /* TODO: How to compute the checksum? Hardware seems to ignore it. */
79
80 /* Request reading. */
81 written = serial_write(serial, &cmdout, sizeof(cmdout));
82 if (written != sizeof(cmdout))
83 return SR_ERR;
84
85 return SR_OK;
601fb67c
AG
86}
87
88SR_PRIV int brymen_packet_request(struct sr_serial_dev_inst *serial)
89{
90 return bm_send_command(BM_CMD_REQUEST_READING, 0, 0, serial);
91}
92
93SR_PRIV int brymen_packet_length(const uint8_t *buf, int *len)
94{
95 struct brymen_header *hdr;
601fb67c 96 int packet_len;
c5d6f5cc
UH
97 size_t buflen;
98
99 buflen = *len;
100 hdr = (void *)buf;
101
601fb67c 102 /* Did we receive a complete header yet? */
c5d6f5cc 103 if (buflen < sizeof(*hdr))
601fb67c 104 return PACKET_NEED_MORE_DATA;
c5d6f5cc 105
601fb67c
AG
106 if (hdr->dle != 0x10 || hdr->stx != 0x02)
107 return PACKET_INVALID_HEADER;
c5d6f5cc
UH
108
109 /* Our packet includes the header, the payload, and the tail. */
601fb67c 110 packet_len = sizeof(*hdr) + hdr->len + sizeof(struct brymen_tail);
c5d6f5cc
UH
111
112 /* In case we pick up an invalid header, limit our search. */
113 if (packet_len > MAX_PACKET_LEN) {
601fb67c
AG
114 sr_spew("Header specifies an invalid payload length: %i.",
115 hdr->len);
116 return PACKET_INVALID_HEADER;
117 }
c5d6f5cc 118
601fb67c
AG
119 *len = packet_len;
120 sr_spew("Expecting a %d-byte packet.", *len);
121 return PACKET_HEADER_OK;
122}
123
124SR_PRIV gboolean brymen_packet_is_valid(const uint8_t *buf)
125{
126 struct brymen_header *hdr;
127 struct brymen_tail *tail;
128 int i;
129 uint8_t chksum = 0;
c5d6f5cc 130 uint8_t *payload;
601fb67c 131
c5d6f5cc
UH
132 payload = (uint8_t *)(buf + sizeof(struct brymen_header));
133
134 hdr = (void *)buf;
135 tail = (void *)(payload + hdr->len);
601fb67c
AG
136
137 for (i = 0; i< hdr->len; i++)
138 chksum ^= payload[i];
139
140 if (tail->checksum != chksum) {
c5d6f5cc 141 sr_dbg("Packet has invalid checksum 0x%.2x. Expected 0x%.2x.",
601fb67c
AG
142 chksum, tail->checksum);
143 return FALSE;
144 }
145
146 return TRUE;
147}
148
c5d6f5cc 149static int parse_value(const char *strbuf, int len, float *floatval)
601fb67c
AG
150{
151 int s, d;
152 char str[32];
153
154 if (strstr(strbuf, "OL")) {
155 sr_dbg("Overlimit.");
156 *floatval = INFINITY;
157 return SR_OK;
158 }
159
160 memset(str, 0, sizeof(str));
933e63a1
BV
161 /* Spaces may interfere with parsing the exponent. Strip them. */
162 for (s = 0, d = 0; s < len; s++) {
601fb67c
AG
163 if (strbuf[s] != ' ')
164 str[d++] = strbuf[s];
933e63a1
BV
165 }
166 if (sr_atof_ascii(str, floatval) != SR_OK)
167 return SR_ERR;
601fb67c
AG
168
169 return SR_OK;
170}
c5d6f5cc 171
601fb67c
AG
172static void parse_flags(const uint8_t *buf, struct brymen_flags *info)
173{
c5d6f5cc
UH
174 info->is_low_batt = (buf[4 + 3] & (1 << 7)) != 0;
175
176 info->is_decibel = (buf[4 + 1] & (1 << 5)) != 0;
177 info->is_duty_cycle = (buf[4 + 1] & (1 << 3)) != 0;
178 info->is_hertz = (buf[4 + 1] & (1 << 2)) != 0;
179 info->is_amp = (buf[4 + 1] & (1 << 1)) != 0;
180 info->is_beep = (buf[4 + 1] & (1 << 0)) != 0;
181
182 info->is_ohm = (buf[4 + 0] & (1 << 7)) != 0;
183 info->is_fahrenheit = (buf[4 + 0] & (1 << 6)) != 0;
184 info->is_celsius = (buf[4 + 0] & (1 << 5)) != 0;
185 info->is_diode = (buf[4 + 0] & (1 << 4)) != 0;
186 info->is_capacitance = (buf[4 + 0] & (1 << 3)) != 0;
187 info->is_volt = (buf[4 + 0] & (1 << 2)) != 0;
188 info->is_dc = (buf[4 + 0] & (1 << 1)) != 0;
189 info->is_ac = (buf[4 + 0] & (1 << 0)) != 0;
601fb67c
AG
190}
191
933e63a1
BV
192SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
193 struct sr_datafeed_analog *analog, void *info)
601fb67c
AG
194{
195 struct brymen_flags flags;
c5d6f5cc
UH
196 struct brymen_header *hdr;
197 uint8_t *bfunc;
601fb67c
AG
198 int asciilen;
199
200 (void)info;
c5d6f5cc
UH
201
202 hdr = (void *)buf;
203 bfunc = (uint8_t *)(buf + sizeof(struct brymen_header));
204
601fb67c
AG
205 analog->mqflags = 0;
206
c5d6f5cc 207 /* Give some debug info about the package. */
601fb67c
AG
208 asciilen = hdr->len - 4;
209 sr_dbg("DMM flags: %.2x %.2x %.2x %.2x",
210 bfunc[3], bfunc[2], bfunc[1], bfunc[0]);
c5d6f5cc 211 /* Value is an ASCII string. */
601fb67c
AG
212 sr_dbg("DMM packet: \"%.*s\"", asciilen, bfunc + 4);
213
214 parse_flags(buf, &flags);
933e63a1
BV
215 if (parse_value((const char *)(bfunc + 4), asciilen, floatval) != SR_OK)
216 return SR_ERR;
601fb67c 217
c5d6f5cc 218 if (flags.is_volt) {
601fb67c
AG
219 analog->mq = SR_MQ_VOLTAGE;
220 analog->unit = SR_UNIT_VOLT;
221 }
c5d6f5cc 222 if (flags.is_amp) {
601fb67c
AG
223 analog->mq = SR_MQ_CURRENT;
224 analog->unit = SR_UNIT_AMPERE;
225 }
c5d6f5cc
UH
226 if (flags.is_ohm) {
227 if (flags.is_beep)
601fb67c
AG
228 analog->mq = SR_MQ_CONTINUITY;
229 else
230 analog->mq = SR_MQ_RESISTANCE;
231 analog->unit = SR_UNIT_OHM;
232 }
c5d6f5cc 233 if (flags.is_hertz) {
601fb67c
AG
234 analog->mq = SR_MQ_FREQUENCY;
235 analog->unit = SR_UNIT_HERTZ;
236 }
c5d6f5cc 237 if (flags.is_duty_cycle) {
601fb67c
AG
238 analog->mq = SR_MQ_DUTY_CYCLE;
239 analog->unit = SR_UNIT_PERCENTAGE;
240 }
c5d6f5cc 241 if (flags.is_capacitance) {
601fb67c
AG
242 analog->mq = SR_MQ_CAPACITANCE;
243 analog->unit = SR_UNIT_FARAD;
244 }
c5d6f5cc 245 if (flags.is_fahrenheit) {
601fb67c
AG
246 analog->mq = SR_MQ_TEMPERATURE;
247 analog->unit = SR_UNIT_FAHRENHEIT;
248 }
c5d6f5cc 249 if (flags.is_celsius) {
601fb67c
AG
250 analog->mq = SR_MQ_TEMPERATURE;
251 analog->unit = SR_UNIT_CELSIUS;
252 }
c5d6f5cc 253 if (flags.is_capacitance) {
601fb67c
AG
254 analog->mq = SR_MQ_CAPACITANCE;
255 analog->unit = SR_UNIT_FARAD;
256 }
c5d6f5cc 257
601fb67c 258 /*
c5d6f5cc 259 * The high-end Brymen models have a configurable reference impedance.
601fb67c
AG
260 * When the reference impedance is changed, the DMM sends one packet
261 * with the value of the new reference impedance. Both decibel and ohm
262 * flags are set in this case, so we must be careful to correctly
c5d6f5cc 263 * identify the value as ohm, not dBmW.
601fb67c 264 */
c5d6f5cc 265 if (flags.is_decibel && !flags.is_ohm) {
601fb67c
AG
266 analog->mq = SR_MQ_POWER;
267 analog->unit = SR_UNIT_DECIBEL_MW;
268 /*
269 * For some reason, dBm measurements are sent by the multimeter
270 * with a value three orders of magnitude smaller than the
271 * displayed value.
c5d6f5cc 272 */
601fb67c
AG
273 *floatval *= 1000;
274 }
275
c5d6f5cc 276 if (flags.is_diode)
601fb67c 277 analog->mqflags |= SR_MQFLAG_DIODE;
c5d6f5cc
UH
278 /* We can have both AC+DC in a single measurement. */
279 if (flags.is_ac)
601fb67c 280 analog->mqflags |= SR_MQFLAG_AC;
c5d6f5cc 281 if (flags.is_dc)
601fb67c
AG
282 analog->mqflags |= SR_MQFLAG_DC;
283
c5d6f5cc 284 if (flags.is_low_batt)
601fb67c
AG
285 sr_info("Low battery!");
286
287 return SR_OK;
c5d6f5cc 288}