]> sigrok.org Git - libsigrok.git/blob - src/dmm/es519xx.c
Build: Include <config.h> first in all source files
[libsigrok.git] / src / dmm / es519xx.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 /*
23  * Cyrustek ES519XX protocol parser.
24  *
25  * Communication parameters: Unidirectional, 2400/7o1 or 19230/7o1
26  */
27
28 #include <config.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <math.h>
32 #include <glib.h>
33 #include <libsigrok/libsigrok.h>
34 #include "libsigrok-internal.h"
35
36 #define LOG_PREFIX "es519xx"
37
38 /* Factors for the respective measurement mode (0 means "invalid"). */
39 static const float factors_2400_11b[9][8] = {
40         {1e-4,  1e-3,  1e-2,  1e-1, 1,    0,    0,    0   }, /* V */
41         {1e-7,  1e-6,  0,     0,    0,    0,    0,    0   }, /* uA */
42         {1e-5,  1e-4,  0,     0,    0,    0,    0,    0   }, /* mA */
43         {1e-2,  0,     0,     0,    0,    0,    0,    0   }, /* A */
44         {1e1,   1e2,   1e3,   1e4,  1e5,  1e6,  0,    0   }, /* RPM */
45         {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0   }, /* Resistance */
46         {1,     1e1,   1e2,   1e3,  1e4,  1e5,  0,    0   }, /* Frequency */
47         {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
48         {1e-3,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
49 };
50 static const float factors_19200_11b_5digits[9][8] = {
51         {1e-4,  1e-3,  1e-2,  1e-1, 1e-5, 0,    0,    0},    /* V */
52         {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* uA */
53         {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* mA */
54         {0,     1e-3,  0,     0,    0,    0,    0,    0},    /* A */
55         {1e-4,  1e-3,  1e-2,  1e-1, 1,    0,    0,    0},    /* Manual A */
56         {1e-2,  1e-1,  1,     1e1,  1e2,  1e3,  1e4,  0},    /* Resistance */
57         {1e-1,  0,     1,     1e1,  1e2,  1e3,  1e4,  0},    /* Frequency */
58         {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
59         {1e-4,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
60 };
61 static const float factors_19200_11b_clampmeter[9][8] = {
62         {1e-3,  1e-2,  1e-1,  1,    1e-4, 0,    0,    0},    /* V */
63         {1e-7,  1e-6,  0,     0,    0,    0,    0,    0},    /* uA */
64         {1e-5,  1e-4,  0,     0,    0,    0,    0,    0},    /* mA */
65         {1e-2,  0,     0,     0,    0,    0,    0,    0},    /* A */
66         {1e-3,  1e-2,  1e-1,  1,    0,    0,    0,    0},    /* Manual A */
67         {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0},    /* Resistance */
68         {1e-1,  0,     1,     1e1,  1e2,  1e3,  1e4,  0},    /* Frequency */
69         {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
70         {1e-3,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
71 };
72 static const float factors_19200_11b[9][8] = {
73         {1e-3,  1e-2,  1e-1,  1,    1e-4, 0,    0,    0},    /* V */
74         {1e-7,  1e-6,  0,     0,    0,    0,    0,    0},    /* uA */
75         {1e-5,  1e-4,  0,     0,    0,    0,    0,    0},    /* mA */
76         {1e-3,  1e-2,  0,     0,    0,    0,    0,    0},    /* A */
77         {0,     0,     0,     0,    0,    0,    0,    0},    /* Manual A */
78         {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0},    /* Resistance */
79         {1,     1e1,   1e2,   1e3,  1e4,  0,    0,    0},    /* Frequency */
80         {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 0},    /* Capacitance */
81         {1e-3,  0,     0,     0,    0,    0,    0,    0},    /* Diode */
82 };
83 static const float factors_19200_14b[9][8] = {
84         {1e-4,  1e-3,  1e-2,  1e-1, 1e-5, 0,    0,    0},    /* V */
85         {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* uA */
86         {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* mA */
87         {1e-3,  0,     0,     0,    0,    0,    0,    0},    /* A */
88         {1e-4,  1e-3,  1e-2,  1e-1, 1,    0,    0,    0},    /* Manual A */
89         {1e-2,  1e-1,  1,     1e1,  1e2,  1e3,  1e4,  0},    /* Resistance */
90         {1e-2,  1e-1,  0,     1,    1e1,  1e2,  1e3,  1e4},  /* Frequency */
91         {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
92         {1e-4,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
93 };
94
95 static int parse_value(const uint8_t *buf, struct es519xx_info *info,
96                        float *result)
97 {
98         int i, intval, num_digits;
99         float floatval;
100
101         num_digits = 4 + ((info->packet_size == 14) ? 1 : 0);
102
103         /* Bytes 1-4 (or 5): Value (4 or 5 decimal digits) */
104         if (info->is_ol) {
105                 sr_spew("Over limit.");
106                 *result = INFINITY;
107                 return SR_OK;
108         } else if (info->is_ul) {
109                 sr_spew("Under limit.");
110                 *result = INFINITY;
111                 return SR_OK;
112         } else if (!isdigit(buf[1]) || !isdigit(buf[2]) ||
113                    !isdigit(buf[3]) || !isdigit(buf[4]) ||
114                    (num_digits == 5 && !isdigit(buf[5]))) {
115                 sr_dbg("Value contained invalid digits: %02x %02x %02x %02x "
116                        "(%c %c %c %c).", buf[1], buf[2], buf[3], buf[4],
117                        buf[1], buf[2], buf[3], buf[4]);
118                 return SR_ERR;
119         }
120         intval = (info->is_digit4) ? 1 : 0;
121         for (i = 0; i < num_digits; i++)
122                 intval = 10 * intval + (buf[i + 1] - '0');
123
124         /* Apply sign. */
125         intval *= info->is_sign ? -1 : 1;
126
127         floatval = (float)intval;
128
129         /* Note: The decimal point position will be parsed later. */
130
131         sr_spew("The display value is %f.", floatval);
132
133         *result = floatval;
134
135         return SR_OK;
136 }
137
138 static int parse_range(uint8_t b, float *floatval,
139                        const struct es519xx_info *info)
140 {
141         int idx, mode;
142         float factor = 0;
143
144         idx = b - '0';
145
146         if (idx < 0 || idx > 7) {
147                 sr_dbg("Invalid range byte / index: 0x%02x / 0x%02x.", b, idx);
148                 return SR_ERR;
149         }
150
151         /* Parse range byte (depends on the measurement mode). */
152         if (info->is_voltage)
153                 mode = 0; /* V */
154         else if (info->is_current && info->is_micro)
155                 mode = 1; /* uA */
156         else if (info->is_current && info->is_milli)
157                 mode = 2; /* mA */
158         else if (info->is_current && info->is_auto)
159                 mode = 3; /* A */
160         else if (info->is_current && !info->is_auto)
161                 mode = 4; /* Manual A */
162         else if (info->is_rpm)
163                 /* Not a typo, it's really index 4 in factors_2400_11b[][]. */
164                 mode = 4; /* RPM */
165         else if (info->is_resistance || info->is_continuity)
166                 mode = 5; /* Resistance */
167         else if (info->is_frequency)
168                 mode = 6; /* Frequency */
169         else if (info->is_capacitance)
170                 mode = 7; /* Capacitance */
171         else if (info->is_diode)
172                 mode = 8; /* Diode */
173         else if (info->is_duty_cycle)
174                 mode = 0; /* Dummy, unused */
175         else {
176                 sr_dbg("Invalid mode, range byte was: 0x%02x.", b);
177                 return SR_ERR;
178         }
179
180         if (info->is_vbar) {
181                 if (info->is_micro)
182                         factor = (const float[]){1e-1, 1}[idx];
183                 else if (info->is_milli)
184                         factor = (const float[]){1e-2, 1e-1}[idx];
185         }
186         else if (info->is_duty_cycle)
187                 factor = 1e-1;
188         else if (info->baudrate == 2400)
189                 factor = factors_2400_11b[mode][idx];
190         else if (info->fivedigits)
191                 factor = factors_19200_11b_5digits[mode][idx];
192         else if (info->clampmeter)
193                 factor = factors_19200_11b_clampmeter[mode][idx];
194         else if (info->packet_size == 11)
195                 factor = factors_19200_11b[mode][idx];
196         else if (info->packet_size == 14)
197                 factor = factors_19200_14b[mode][idx];
198
199         if (factor == 0) {
200                 sr_dbg("Invalid factor for range byte: 0x%02x.", b);
201                 return SR_ERR;
202         }
203
204         /* Apply respective factor (mode-dependent) on the value. */
205         *floatval *= factor;
206         sr_dbg("Applying factor %f, new value is %f.", factor, *floatval);
207
208         return SR_OK;
209 }
210
211 static void parse_flags(const uint8_t *buf, struct es519xx_info *info)
212 {
213         int function, status;
214
215         function = 5 + ((info->packet_size == 14) ? 1 : 0);
216         status = function + 1;
217
218         /* Status byte */
219         if (info->alt_functions) {
220                 info->is_sign  = (buf[status] & (1 << 3)) != 0;
221                 info->is_batt  = (buf[status] & (1 << 2)) != 0; /* Bat. low */
222                 info->is_ol    = (buf[status] & (1 << 1)) != 0; /* Overflow */
223                 info->is_ol   |= (buf[status] & (1 << 0)) != 0; /* Overflow */
224         } else {
225                 info->is_judge = (buf[status] & (1 << 3)) != 0;
226                 info->is_sign  = (buf[status] & (1 << 2)) != 0;
227                 info->is_batt  = (buf[status] & (1 << 1)) != 0; /* Bat. low */
228                 info->is_ol    = (buf[status] & (1 << 0)) != 0; /* Overflow */
229         }
230
231         if (info->packet_size == 14) {
232                 /* Option 1 byte */
233                 info->is_max  = (buf[8] & (1 << 3)) != 0;
234                 info->is_min  = (buf[8] & (1 << 2)) != 0;
235                 info->is_rel  = (buf[8] & (1 << 1)) != 0;
236                 info->is_rmr  = (buf[8] & (1 << 0)) != 0;
237
238                 /* Option 2 byte */
239                 info->is_ul   = (buf[9] & (1 << 3)) != 0; /* Underflow */
240                 info->is_pmax = (buf[9] & (1 << 2)) != 0; /* Max. peak value */
241                 info->is_pmin = (buf[9] & (1 << 1)) != 0; /* Min. peak value */
242
243                 /* Option 3 byte */
244                 info->is_dc   = (buf[10] & (1 << 3)) != 0;
245                 info->is_ac   = (buf[10] & (1 << 2)) != 0;
246                 info->is_auto = (buf[10] & (1 << 1)) != 0;
247                 info->is_vahz = (buf[10] & (1 << 0)) != 0;
248
249                 /* LPF: Low-pass filter(s) */
250                 if (info->selectable_lpf) {
251                         /* Option 4 byte */
252                         info->is_hold = (buf[11] & (1 << 3)) != 0;
253                         info->is_vbar = (buf[11] & (1 << 2)) != 0;
254                         info->is_lpf1 = (buf[11] & (1 << 1)) != 0;
255                         info->is_lpf0 = (buf[11] & (1 << 0)) != 0;
256                 } else {
257                         /* Option 4 byte */
258                         info->is_vbar = (buf[11] & (1 << 2)) != 0;
259                         info->is_hold = (buf[11] & (1 << 1)) != 0;
260                         info->is_lpf1 = (buf[11] & (1 << 0)) != 0;
261                 }
262         } else if (info->alt_functions) {
263                 /* Option 2 byte */
264                 info->is_dc   = (buf[8] & (1 << 3)) != 0;
265                 info->is_auto = (buf[8] & (1 << 2)) != 0;
266                 info->is_apo  = (buf[8] & (1 << 0)) != 0;
267                 info->is_ac   = !info->is_dc;
268         } else {
269                 /* Option 1 byte */
270                 if (info->baudrate == 2400) {
271                         info->is_pmax   = (buf[7] & (1 << 3)) != 0;
272                         info->is_pmin   = (buf[7] & (1 << 2)) != 0;
273                         info->is_vahz   = (buf[7] & (1 << 0)) != 0;
274                 } else if (info->fivedigits) {
275                         info->is_ul     = (buf[7] & (1 << 3)) != 0;
276                         info->is_pmax   = (buf[7] & (1 << 2)) != 0;
277                         info->is_pmin   = (buf[7] & (1 << 1)) != 0;
278                         info->is_digit4 = (buf[7] & (1 << 0)) != 0;
279                 } else if (info->clampmeter) {
280                         info->is_ul     = (buf[7] & (1 << 3)) != 0;
281                         info->is_vasel  = (buf[7] & (1 << 2)) != 0;
282                         info->is_vbar   = (buf[7] & (1 << 1)) != 0;
283                 } else {
284                         info->is_hold   = (buf[7] & (1 << 3)) != 0;
285                         info->is_max    = (buf[7] & (1 << 2)) != 0;
286                         info->is_min    = (buf[7] & (1 << 1)) != 0;
287                 }
288
289                 /* Option 2 byte */
290                 info->is_dc   = (buf[8] & (1 << 3)) != 0;
291                 info->is_ac   = (buf[8] & (1 << 2)) != 0;
292                 info->is_auto = (buf[8] & (1 << 1)) != 0;
293                 if (info->baudrate == 2400)
294                         info->is_apo  = (buf[8] & (1 << 0)) != 0;
295                 else
296                         info->is_vahz = (buf[8] & (1 << 0)) != 0;
297         }
298
299         /* Function byte */
300         if (info->alt_functions) {
301                 switch (buf[function]) {
302                 case 0x3f: /* A */
303                         info->is_current = info->is_auto = TRUE;
304                         break;
305                 case 0x3e: /* uA */
306                         info->is_current = info->is_micro = info->is_auto = TRUE;
307                         break;
308                 case 0x3d: /* mA */
309                         info->is_current = info->is_milli = info->is_auto = TRUE;
310                         break;
311                 case 0x3c: /* V */
312                         info->is_voltage = TRUE;
313                         break;
314                 case 0x37: /* Resistance */
315                         info->is_resistance = TRUE;
316                         break;
317                 case 0x36: /* Continuity */
318                         info->is_continuity = TRUE;
319                         break;
320                 case 0x3b: /* Diode */
321                         info->is_diode = TRUE;
322                         break;
323                 case 0x3a: /* Frequency */
324                         info->is_frequency = TRUE;
325                         break;
326                 case 0x34: /* ADP0 */
327                 case 0x35: /* ADP0 */
328                         info->is_adp0 = TRUE;
329                         break;
330                 case 0x38: /* ADP1 */
331                 case 0x39: /* ADP1 */
332                         info->is_adp1 = TRUE;
333                         break;
334                 case 0x32: /* ADP2 */
335                 case 0x33: /* ADP2 */
336                         info->is_adp2 = TRUE;
337                         break;
338                 case 0x30: /* ADP3 */
339                 case 0x31: /* ADP3 */
340                         info->is_adp3 = TRUE;
341                         break;
342                 default:
343                         sr_dbg("Invalid function byte: 0x%02x.", buf[function]);
344                         break;
345                 }
346         } else {
347                 /* Note: Some of these mappings are fixed up later. */
348                 switch (buf[function]) {
349                 case 0x3b: /* V */
350                         info->is_voltage = TRUE;
351                         break;
352                 case 0x3d: /* uA */
353                         info->is_current = info->is_micro = info->is_auto = TRUE;
354                         break;
355                 case 0x3f: /* mA */
356                         info->is_current = info->is_milli = info->is_auto = TRUE;
357                         break;
358                 case 0x30: /* A */
359                         info->is_current = info->is_auto = TRUE;
360                         break;
361                 case 0x39: /* Manual A */
362                         info->is_current = TRUE;
363                         info->is_auto = FALSE; /* Manual mode */
364                         break;
365                 case 0x33: /* Resistance */
366                         info->is_resistance = TRUE;
367                         break;
368                 case 0x35: /* Continuity */
369                         info->is_continuity = TRUE;
370                         break;
371                 case 0x31: /* Diode */
372                         info->is_diode = TRUE;
373                         break;
374                 case 0x32: /* Frequency / RPM / duty cycle */
375                         if (info->packet_size == 14) {
376                                 if (info->is_judge)
377                                         info->is_duty_cycle = TRUE;
378                                 else
379                                         info->is_frequency = TRUE;
380                         } else {
381                                 if (info->is_judge)
382                                         info->is_rpm = TRUE;
383                                 else
384                                         info->is_frequency = TRUE;
385                         }
386                         break;
387                 case 0x36: /* Capacitance */
388                         info->is_capacitance = TRUE;
389                         break;
390                 case 0x34: /* Temperature */
391                         info->is_temperature = TRUE;
392                         if (info->is_judge)
393                                 info->is_celsius = TRUE;
394                         else
395                                 info->is_fahrenheit = TRUE;
396                         /* IMPORTANT: The digits always represent Celsius! */
397                         break;
398                 case 0x3e: /* ADP0 */
399                         info->is_adp0 = TRUE;
400                         break;
401                 case 0x3c: /* ADP1 */
402                         info->is_adp1 = TRUE;
403                         break;
404                 case 0x38: /* ADP2 */
405                         info->is_adp2 = TRUE;
406                         break;
407                 case 0x3a: /* ADP3 */
408                         info->is_adp3 = TRUE;
409                         break;
410                 default:
411                         sr_dbg("Invalid function byte: 0x%02x.", buf[function]);
412                         break;
413                 }
414         }
415
416         if (info->is_vahz && (info->is_voltage || info->is_current)) {
417                 info->is_voltage = FALSE;
418                 info->is_current = FALSE;
419                 info->is_milli = info->is_micro = FALSE;
420                 if (info->packet_size == 14) {
421                         if (info->is_judge)
422                                 info->is_duty_cycle = TRUE;
423                         else
424                                 info->is_frequency = TRUE;
425                 } else {
426                         if (info->is_judge)
427                                 info->is_rpm = TRUE;
428                         else
429                                 info->is_frequency = TRUE;
430                 }
431         }
432
433         if (info->is_current && (info->is_micro || info->is_milli) && info->is_vasel) {
434                 info->is_current = info->is_auto = FALSE;
435                 info->is_voltage = TRUE;
436         }
437
438         if (info->baudrate == 2400) {
439                 /* Inverted mapping between mA and A, and no manual A. */
440                 if (info->is_current && (info->is_milli || !info->is_auto)) {
441                         info->is_milli = !info->is_milli;
442                         info->is_auto = TRUE;
443                 }
444         }
445 }
446
447 static void handle_flags(struct sr_datafeed_analog *analog,
448                          float *floatval, const struct es519xx_info *info)
449 {
450         /*
451          * Note: is_micro etc. are not used directly to multiply/divide
452          * floatval, this is handled via parse_range() and factors[][].
453          */
454
455         /* Measurement modes */
456         if (info->is_voltage) {
457                 analog->mq = SR_MQ_VOLTAGE;
458                 analog->unit = SR_UNIT_VOLT;
459         }
460         if (info->is_current) {
461                 analog->mq = SR_MQ_CURRENT;
462                 analog->unit = SR_UNIT_AMPERE;
463         }
464         if (info->is_resistance) {
465                 analog->mq = SR_MQ_RESISTANCE;
466                 analog->unit = SR_UNIT_OHM;
467         }
468         if (info->is_frequency) {
469                 analog->mq = SR_MQ_FREQUENCY;
470                 analog->unit = SR_UNIT_HERTZ;
471         }
472         if (info->is_capacitance) {
473                 analog->mq = SR_MQ_CAPACITANCE;
474                 analog->unit = SR_UNIT_FARAD;
475         }
476         if (info->is_temperature && info->is_celsius) {
477                 analog->mq = SR_MQ_TEMPERATURE;
478                 analog->unit = SR_UNIT_CELSIUS;
479         }
480         if (info->is_temperature && info->is_fahrenheit) {
481                 analog->mq = SR_MQ_TEMPERATURE;
482                 analog->unit = SR_UNIT_FAHRENHEIT;
483         }
484         if (info->is_continuity) {
485                 analog->mq = SR_MQ_CONTINUITY;
486                 analog->unit = SR_UNIT_BOOLEAN;
487                 *floatval = (*floatval < 0.0 || *floatval > 25.0) ? 0.0 : 1.0;
488         }
489         if (info->is_diode) {
490                 analog->mq = SR_MQ_VOLTAGE;
491                 analog->unit = SR_UNIT_VOLT;
492         }
493         if (info->is_rpm) {
494                 analog->mq = SR_MQ_FREQUENCY;
495                 analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
496         }
497         if (info->is_duty_cycle) {
498                 analog->mq = SR_MQ_DUTY_CYCLE;
499                 analog->unit = SR_UNIT_PERCENTAGE;
500         }
501
502         /* Measurement related flags */
503         if (info->is_ac)
504                 analog->mqflags |= SR_MQFLAG_AC;
505         if (info->is_dc)
506                 analog->mqflags |= SR_MQFLAG_DC;
507         if (info->is_auto)
508                 analog->mqflags |= SR_MQFLAG_AUTORANGE;
509         if (info->is_diode)
510                 analog->mqflags |= SR_MQFLAG_DIODE;
511         if (info->is_hold)
512                 /*
513                 * Note: HOLD only affects the number displayed on the LCD,
514                 * but not the value sent via the protocol! It also does not
515                 * affect the bargraph on the LCD.
516                 */
517                 analog->mqflags |= SR_MQFLAG_HOLD;
518         if (info->is_max)
519                 analog->mqflags |= SR_MQFLAG_MAX;
520         if (info->is_min)
521                 analog->mqflags |= SR_MQFLAG_MIN;
522         if (info->is_rel)
523                 analog->mqflags |= SR_MQFLAG_RELATIVE;
524
525         /* Other flags */
526         if (info->is_judge)
527                 sr_spew("Judge bit is set.");
528         if (info->is_batt)
529                 sr_spew("Battery is low.");
530         if (info->is_ol)
531                 sr_spew("Input overflow.");
532         if (info->is_ul)
533                 sr_spew("Input underflow.");
534         if (info->is_pmax)
535                 sr_spew("pMAX active, LCD shows max. peak value.");
536         if (info->is_pmin)
537                 sr_spew("pMIN active, LCD shows min. peak value.");
538         if (info->is_vahz)
539                 sr_spew("VAHZ active.");
540         if (info->is_apo)
541                 sr_spew("Auto-Power-Off enabled.");
542         if (info->is_vbar)
543                 sr_spew("VBAR active.");
544         if ((!info->selectable_lpf && info->is_lpf1) ||
545             (info->selectable_lpf && (!info->is_lpf0 || !info->is_lpf1)))
546                 sr_spew("Low-pass filter feature is active.");
547 }
548
549 static gboolean flags_valid(const struct es519xx_info *info)
550 {
551         int count;
552
553         /* Does the packet have more than one multiplier? */
554         count  = (info->is_micro) ? 1 : 0;
555         count += (info->is_milli) ? 1 : 0;
556         if (count > 1) {
557                 sr_dbg("More than one multiplier detected in packet.");
558                 return FALSE;
559         }
560
561         /* Does the packet "measure" more than one type of value? */
562         count  = (info->is_voltage) ? 1 : 0;
563         count += (info->is_current) ? 1 : 0;
564         count += (info->is_resistance) ? 1 : 0;
565         count += (info->is_frequency) ? 1 : 0;
566         count += (info->is_capacitance) ? 1 : 0;
567         count += (info->is_temperature) ? 1 : 0;
568         count += (info->is_continuity) ? 1 : 0;
569         count += (info->is_diode) ? 1 : 0;
570         count += (info->is_rpm) ? 1 : 0;
571         if (count > 1) {
572                 sr_dbg("More than one measurement type detected in packet.");
573                 return FALSE;
574         }
575
576         /* Both AC and DC set? */
577         if (info->is_ac && info->is_dc) {
578                 sr_dbg("Both AC and DC flags detected in packet.");
579                 return FALSE;
580         }
581
582         return TRUE;
583 }
584
585 static gboolean sr_es519xx_packet_valid(const uint8_t *buf,
586                                         struct es519xx_info *info)
587 {
588         int s;
589
590         s = info->packet_size;
591
592         if (s == 11 && memcmp(buf, buf + s, s))
593                 return FALSE;
594
595         if (buf[s - 2] != '\r' || buf[s - 1] != '\n')
596                 return FALSE;
597
598         parse_flags(buf, info);
599
600         if (!flags_valid(info))
601                 return FALSE;
602
603         return TRUE;
604 }
605
606 static int sr_es519xx_parse(const uint8_t *buf, float *floatval,
607                             struct sr_datafeed_analog *analog,
608                             struct es519xx_info *info)
609 {
610         int ret;
611
612         if (!sr_es519xx_packet_valid(buf, info))
613                 return SR_ERR;
614
615         if ((ret = parse_value(buf, info, floatval)) != SR_OK) {
616                 sr_dbg("Error parsing value: %d.", ret);
617                 return ret;
618         }
619
620         if ((ret = parse_range(buf[0], floatval, info)) != SR_OK)
621                 return ret;
622
623         handle_flags(analog, floatval, info);
624         return SR_OK;
625 }
626
627 /*
628  * Functions for 2400 baud / 11 bytes protocols.
629  * This includes ES51962, ES51971, ES51972, ES51978 and ES51989.
630  */
631 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf)
632 {
633         struct es519xx_info info;
634
635         memset(&info, 0, sizeof(struct es519xx_info));
636         info.baudrate = 2400;
637         info.packet_size = 11;
638
639         return sr_es519xx_packet_valid(buf, &info);
640 }
641
642 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
643                                 struct sr_datafeed_analog *analog, void *info)
644 {
645         struct es519xx_info *info_local;
646
647         info_local = info;
648         memset(info_local, 0, sizeof(struct es519xx_info));
649         info_local->baudrate = 2400;
650         info_local->packet_size = 11;
651
652         return sr_es519xx_parse(buf, floatval, analog, info);
653 }
654
655 /*
656  * Functions for 2400 baud / 11 byte protocols.
657  * This includes ES51960, ES51977 and ES51988.
658  */
659 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf)
660 {
661         struct es519xx_info info;
662
663         memset(&info, 0, sizeof(struct es519xx_info));
664         info.baudrate = 2400;
665         info.packet_size = 11;
666         info.alt_functions = TRUE;
667
668         return sr_es519xx_packet_valid(buf, &info);
669 }
670
671 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
672                 float *floatval, struct sr_datafeed_analog *analog, void *info)
673 {
674         struct es519xx_info *info_local;
675
676         info_local = info;
677         memset(info_local, 0, sizeof(struct es519xx_info));
678         info_local->baudrate = 2400;
679         info_local->packet_size = 11;
680         info_local->alt_functions = TRUE;
681
682         return sr_es519xx_parse(buf, floatval, analog, info);
683 }
684
685 /*
686  * Functions for 19200 baud / 11 bytes protocols with 5 digits display.
687  * This includes ES51911, ES51916 and ES51918.
688  */
689 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf)
690 {
691         struct es519xx_info info;
692
693         memset(&info, 0, sizeof(struct es519xx_info));
694         info.baudrate = 19200;
695         info.packet_size = 11;
696         info.fivedigits = TRUE;
697
698         return sr_es519xx_packet_valid(buf, &info);
699 }
700
701 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
702                 float *floatval, struct sr_datafeed_analog *analog, void *info)
703 {
704         struct es519xx_info *info_local;
705
706         info_local = info;
707         memset(info_local, 0, sizeof(struct es519xx_info));
708         info_local->baudrate = 19200;
709         info_local->packet_size = 11;
710         info_local->fivedigits = TRUE;
711
712         return sr_es519xx_parse(buf, floatval, analog, info);
713 }
714
715 /*
716  * Functions for 19200 baud / 11 bytes protocols with clamp meter support.
717  * This includes ES51967 and ES51969.
718  */
719 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf)
720 {
721         struct es519xx_info info;
722
723         memset(&info, 0, sizeof(struct es519xx_info));
724         info.baudrate = 19200;
725         info.packet_size = 11;
726         info.clampmeter = TRUE;
727
728         return sr_es519xx_packet_valid(buf, &info);
729 }
730
731 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
732                 float *floatval, struct sr_datafeed_analog *analog, void *info)
733 {
734         struct es519xx_info *info_local;
735
736         info_local = info;
737         memset(info_local, 0, sizeof(struct es519xx_info));
738         info_local->baudrate = 19200;
739         info_local->packet_size = 11;
740         info_local->clampmeter = TRUE;
741
742         return sr_es519xx_parse(buf, floatval, analog, info);
743 }
744
745 /*
746  * Functions for 19200 baud / 11 bytes protocols.
747  * This includes ES51981, ES51982, ES51983, ES51984 and ES51986.
748  */
749 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf)
750 {
751         struct es519xx_info info;
752
753         memset(&info, 0, sizeof(struct es519xx_info));
754         info.baudrate = 19200;
755         info.packet_size = 11;
756
757         return sr_es519xx_packet_valid(buf, &info);
758 }
759
760 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
761                         struct sr_datafeed_analog *analog, void *info)
762 {
763         struct es519xx_info *info_local;
764
765         info_local = info;
766         memset(info_local, 0, sizeof(struct es519xx_info));
767         info_local->baudrate = 19200;
768         info_local->packet_size = 11;
769
770         return sr_es519xx_parse(buf, floatval, analog, info);
771 }
772
773 /*
774  * Functions for 19200 baud / 14 bytes protocols.
775  * This includes ES51921 and ES51922.
776  */
777 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf)
778 {
779         struct es519xx_info info;
780
781         memset(&info, 0, sizeof(struct es519xx_info));
782         info.baudrate = 19200;
783         info.packet_size = 14;
784
785         return sr_es519xx_packet_valid(buf, &info);
786 }
787
788 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
789                         struct sr_datafeed_analog *analog, void *info)
790 {
791         struct es519xx_info *info_local;
792
793         info_local = info;
794         memset(info_local, 0, sizeof(struct es519xx_info));
795         info_local->baudrate = 19200;
796         info_local->packet_size = 14;
797
798         return sr_es519xx_parse(buf, floatval, analog, info);
799 }
800
801 /*
802  * Functions for 19200 baud / 14 bytes protocols with selectable LPF.
803  * This includes ES51931 and ES51932.
804  */
805 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf)
806 {
807         struct es519xx_info info;
808
809         memset(&info, 0, sizeof(struct es519xx_info));
810         info.baudrate = 19200;
811         info.packet_size = 14;
812         info.selectable_lpf = TRUE;
813
814         return sr_es519xx_packet_valid(buf, &info);
815 }
816
817 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
818                 float *floatval, struct sr_datafeed_analog *analog, void *info)
819 {
820         struct es519xx_info *info_local;
821
822         info_local = info;
823         memset(info_local, 0, sizeof(struct es519xx_info));
824         info_local->baudrate = 19200;
825         info_local->packet_size = 14;
826         info_local->selectable_lpf = TRUE;
827
828         return sr_es519xx_parse(buf, floatval, analog, info);
829 }