]> sigrok.org Git - libsigrok.git/blame - src/hardware/fluke-dmm/protocol.c
fluke-dmm: sort models by numbers, no behaviour change
[libsigrok.git] / src / hardware / fluke-dmm / protocol.c
CommitLineData
d38d2ef0 1/*
50985c20 2 * This file is part of the libsigrok project.
d38d2ef0
BV
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.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
6ec6c43b 20#include <config.h>
d38d2ef0
BV
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
4cea0ff7 24#include <glib.h>
c1aae900 25#include <libsigrok/libsigrok.h>
4cea0ff7 26#include "libsigrok-internal.h"
2cb232a9 27#include "protocol.h"
d38d2ef0 28
d2e3ce76
MP
29static int count_digits(const char *str) {
30 int digits;
31
32 while (*str && *str != ' ' && *str != ',' && *str != '.')
33 str++;
34
35 digits = 0;
36 if (*str == '.') {
37 str++;
38 while (*str && *str != ' ' && *str != ',') {
39 str++;
40 digits++;
41 }
42 }
43
44 return digits;
45}
46
19ab8e36 47static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens)
fe31f8b9 48{
19ab8e36
AS
49 struct dev_context *devc;
50 struct sr_datafeed_packet packet;
51 struct sr_datafeed_analog analog;
3f5cf2a0
UH
52 struct sr_analog_encoding encoding;
53 struct sr_analog_meaning meaning;
54 struct sr_analog_spec spec;
fe31f8b9
BV
55 float fvalue;
56 char *e, *u;
57 gboolean is_oor;
d2e3ce76 58 int digits;
fe31f8b9 59
19ab8e36
AS
60 devc = sdi->priv;
61
4cea0ff7 62 if (strcmp(tokens[0], "QM") || !tokens[1])
19ab8e36 63 return;
fe31f8b9
BV
64
65 if ((e = strstr(tokens[1], "Out of range"))) {
66 is_oor = TRUE;
67 fvalue = -1;
d2e3ce76 68 digits = 0;
0c5f2abc 69 while (*e && *e != '.')
357e341d 70 e++;
fe31f8b9
BV
71 } else {
72 is_oor = FALSE;
357e341d
BV
73 /* Delimit the float, since sr_atof_ascii() wants only
74 * a valid float here. */
75 e = tokens[1];
0c5f2abc 76 while (*e && *e != ' ')
357e341d
BV
77 e++;
78 *e++ = '\0';
ad42cfeb 79 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK) {
fe31f8b9 80 /* Happens all the time, when switching modes. */
e7f803f3 81 sr_dbg("Invalid float: '%s'", tokens[1]);
19ab8e36 82 return;
fe31f8b9 83 }
d2e3ce76 84 digits = count_digits(tokens[1]);
fe31f8b9 85 }
0c5f2abc 86 while (*e && *e == ' ')
fe31f8b9
BV
87 e++;
88
d2e3ce76 89 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
19ab8e36
AS
90 analog.data = &fvalue;
91 analog.meaning->channels = sdi->channels;
92 analog.num_samples = 1;
fe31f8b9 93 if (is_oor)
19ab8e36
AS
94 fvalue = NAN;
95 analog.meaning->mq = 0;
fe31f8b9
BV
96
97 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
19ab8e36
AS
98 analog.meaning->mq = SR_MQ_VOLTAGE;
99 analog.meaning->unit = SR_UNIT_VOLT;
fe31f8b9 100 if (!is_oor && e[0] == 'm')
19ab8e36 101 fvalue /= 1000;
fe31f8b9
BV
102 /* This catches "V AC", "V DC" and "V AC+DC". */
103 if (strstr(u, "AC"))
19ab8e36 104 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
fe31f8b9 105 if (strstr(u, "DC"))
19ab8e36 106 analog.meaning->mqflags |= SR_MQFLAG_DC;
fe31f8b9 107 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
19ab8e36 108 analog.meaning->mq = SR_MQ_VOLTAGE;
fe31f8b9 109 if (u[2] == 'm')
19ab8e36 110 analog.meaning->unit = SR_UNIT_DECIBEL_MW;
fe31f8b9 111 else
19ab8e36
AS
112 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
113 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
fe31f8b9 114 } else if ((u = strstr(e, "Ohms"))) {
19ab8e36
AS
115 analog.meaning->mq = SR_MQ_RESISTANCE;
116 analog.meaning->unit = SR_UNIT_OHM;
fe31f8b9 117 if (is_oor)
19ab8e36 118 fvalue = INFINITY;
fe31f8b9 119 else if (e[0] == 'k')
19ab8e36 120 fvalue *= 1000;
fe31f8b9 121 else if (e[0] == 'M')
19ab8e36 122 fvalue *= 1000000;
fe31f8b9 123 } else if (!strcmp(e, "nS")) {
19ab8e36
AS
124 analog.meaning->mq = SR_MQ_CONDUCTANCE;
125 analog.meaning->unit = SR_UNIT_SIEMENS;
126 *((float *)analog.data) /= 1e+9;
fe31f8b9 127 } else if ((u = strstr(e, "Farads"))) {
19ab8e36
AS
128 analog.meaning->mq = SR_MQ_CAPACITANCE;
129 analog.meaning->unit = SR_UNIT_FARAD;
fe31f8b9
BV
130 if (!is_oor) {
131 if (e[0] == 'm')
19ab8e36 132 fvalue /= 1e+3;
fe31f8b9 133 else if (e[0] == 'u')
19ab8e36 134 fvalue /= 1e+6;
fe31f8b9 135 else if (e[0] == 'n')
19ab8e36 136 fvalue /= 1e+9;
fe31f8b9
BV
137 }
138 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
19ab8e36 139 analog.meaning->mq = SR_MQ_TEMPERATURE;
fe31f8b9 140 if (u[4] == 'C')
19ab8e36 141 analog.meaning->unit = SR_UNIT_CELSIUS;
fe31f8b9 142 else
19ab8e36 143 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
fe31f8b9 144 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
19ab8e36
AS
145 analog.meaning->mq = SR_MQ_CURRENT;
146 analog.meaning->unit = SR_UNIT_AMPERE;
fe31f8b9
BV
147 /* This catches "A AC", "A DC" and "A AC+DC". */
148 if (strstr(u, "AC"))
19ab8e36 149 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
fe31f8b9 150 if (strstr(u, "DC"))
19ab8e36 151 analog.meaning->mqflags |= SR_MQFLAG_DC;
fe31f8b9
BV
152 if (!is_oor) {
153 if (e[0] == 'm')
19ab8e36 154 fvalue /= 1e+3;
fe31f8b9 155 else if (e[0] == 'u')
19ab8e36 156 fvalue /= 1e+6;
fe31f8b9
BV
157 }
158 } else if ((u = strstr(e, "Hz"))) {
19ab8e36
AS
159 analog.meaning->mq = SR_MQ_FREQUENCY;
160 analog.meaning->unit = SR_UNIT_HERTZ;
fe31f8b9 161 if (e[0] == 'k')
19ab8e36 162 fvalue *= 1e+3;
fe31f8b9 163 } else if (!strcmp(e, "%")) {
19ab8e36
AS
164 analog.meaning->mq = SR_MQ_DUTY_CYCLE;
165 analog.meaning->unit = SR_UNIT_PERCENTAGE;
fe31f8b9 166 } else if ((u = strstr(e, "ms"))) {
19ab8e36
AS
167 analog.meaning->mq = SR_MQ_PULSE_WIDTH;
168 analog.meaning->unit = SR_UNIT_SECOND;
169 fvalue /= 1e+3;
fe31f8b9
BV
170 }
171
19ab8e36
AS
172 if (analog.meaning->mq != 0) {
173 /* Got a measurement. */
174 packet.type = SR_DF_ANALOG;
175 packet.payload = &analog;
176 sr_session_send(sdi, &packet);
177 sr_sw_limits_update_samples_read(&devc->limits, 1);
fe31f8b9 178 }
fe31f8b9
BV
179}
180
19ab8e36 181static void handle_qm_28x(const struct sr_dev_inst *sdi, char **tokens)
d38d2ef0 182{
19ab8e36
AS
183 struct dev_context *devc;
184 struct sr_datafeed_packet packet;
185 struct sr_datafeed_analog analog;
3f5cf2a0
UH
186 struct sr_analog_encoding encoding;
187 struct sr_analog_meaning meaning;
188 struct sr_analog_spec spec;
d38d2ef0 189 float fvalue;
d2e3ce76 190 int digits;
d38d2ef0 191
19ab8e36
AS
192 devc = sdi->priv;
193
4cea0ff7 194 if (!tokens[1])
19ab8e36 195 return;
4cea0ff7 196
ad42cfeb 197 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK) {
357e341d 198 sr_err("Invalid float '%s'.", tokens[0]);
19ab8e36 199 return;
d38d2ef0 200 }
d2e3ce76 201 digits = count_digits(tokens[0]);
d38d2ef0 202
d2e3ce76 203 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
19ab8e36
AS
204 analog.data = &fvalue;
205 analog.meaning->channels = sdi->channels;
206 analog.num_samples = 1;
207 analog.meaning->mq = 0;
d38d2ef0
BV
208
209 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
19ab8e36
AS
210 analog.meaning->mq = SR_MQ_VOLTAGE;
211 analog.meaning->unit = SR_UNIT_VOLT;
d38d2ef0
BV
212 if (!strcmp(tokens[2], "NORMAL")) {
213 if (tokens[1][1] == 'A') {
19ab8e36
AS
214 analog.meaning->mqflags |= SR_MQFLAG_AC;
215 analog.meaning->mqflags |= SR_MQFLAG_RMS;
d38d2ef0 216 } else
19ab8e36 217 analog.meaning->mqflags |= SR_MQFLAG_DC;
d38d2ef0 218 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
19ab8e36 219 fvalue = NAN;
d38d2ef0 220 } else
19ab8e36 221 analog.meaning->mq = 0;
a28dac0a 222 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
19ab8e36 223 analog.meaning->mq = SR_MQ_VOLTAGE;
2c04dede 224 if (tokens[1][2] == 'm')
19ab8e36 225 analog.meaning->unit = SR_UNIT_DECIBEL_MW;
2c04dede 226 else
19ab8e36
AS
227 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
228 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
a28dac0a 229 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
d38d2ef0 230 if (!strcmp(tokens[2], "NORMAL")) {
19ab8e36 231 analog.meaning->mq = SR_MQ_TEMPERATURE;
d38d2ef0 232 if (tokens[1][0] == 'C')
19ab8e36 233 analog.meaning->unit = SR_UNIT_CELSIUS;
d38d2ef0 234 else
19ab8e36 235 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
d38d2ef0 236 }
a28dac0a 237 } else if (!strcmp(tokens[1], "OHM")) {
d38d2ef0 238 if (!strcmp(tokens[3], "NONE")) {
19ab8e36
AS
239 analog.meaning->mq = SR_MQ_RESISTANCE;
240 analog.meaning->unit = SR_UNIT_OHM;
d38d2ef0 241 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
19ab8e36 242 fvalue = INFINITY;
d38d2ef0 243 } else if (strcmp(tokens[2], "NORMAL"))
19ab8e36 244 analog.meaning->mq = 0;
d38d2ef0 245 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
19ab8e36
AS
246 analog.meaning->mq = SR_MQ_CONTINUITY;
247 analog.meaning->unit = SR_UNIT_BOOLEAN;
248 fvalue = 0.0;
d38d2ef0 249 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
19ab8e36
AS
250 analog.meaning->mq = SR_MQ_CONTINUITY;
251 analog.meaning->unit = SR_UNIT_BOOLEAN;
252 fvalue = 1.0;
d38d2ef0 253 }
a28dac0a 254 } else if (!strcmp(tokens[1], "F")
d38d2ef0
BV
255 && !strcmp(tokens[2], "NORMAL")
256 && !strcmp(tokens[3], "NONE")) {
19ab8e36
AS
257 analog.meaning->mq = SR_MQ_CAPACITANCE;
258 analog.meaning->unit = SR_UNIT_FARAD;
d38d2ef0 259 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
19ab8e36
AS
260 analog.meaning->mq = SR_MQ_CURRENT;
261 analog.meaning->unit = SR_UNIT_AMPERE;
d38d2ef0
BV
262 if (!strcmp(tokens[2], "NORMAL")) {
263 if (tokens[1][1] == 'A') {
19ab8e36
AS
264 analog.meaning->mqflags |= SR_MQFLAG_AC;
265 analog.meaning->mqflags |= SR_MQFLAG_RMS;
d38d2ef0 266 } else
19ab8e36 267 analog.meaning->mqflags |= SR_MQFLAG_DC;
d38d2ef0 268 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
19ab8e36 269 fvalue = NAN;
d38d2ef0 270 } else
19ab8e36
AS
271 analog.meaning->mq = 0;
272 } else if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
273 analog.meaning->mq = SR_MQ_FREQUENCY;
274 analog.meaning->unit = SR_UNIT_HERTZ;
a28dac0a 275 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
19ab8e36
AS
276 analog.meaning->mq = SR_MQ_DUTY_CYCLE;
277 analog.meaning->unit = SR_UNIT_PERCENTAGE;
a28dac0a 278 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
19ab8e36
AS
279 analog.meaning->mq = SR_MQ_PULSE_WIDTH;
280 analog.meaning->unit = SR_UNIT_SECOND;
a28dac0a 281 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
19ab8e36
AS
282 analog.meaning->mq = SR_MQ_CONDUCTANCE;
283 analog.meaning->unit = SR_UNIT_SIEMENS;
d38d2ef0
BV
284 }
285
19ab8e36
AS
286 if (analog.meaning->mq != 0) {
287 /* Got a measurement. */
288 packet.type = SR_DF_ANALOG;
289 packet.payload = &analog;
290 sr_session_send(sdi, &packet);
291 sr_sw_limits_update_samples_read(&devc->limits, 1);
d38d2ef0 292 }
d38d2ef0
BV
293}
294
11fb7110
BV
295static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
296{
297 struct dev_context *devc;
298 int meas_type, meas_unit, meas_char, i;
299
300 /* Make sure we have 7 valid tokens. */
301 for (i = 0; tokens[i] && i < 7; i++);
302 if (i != 7)
303 return;
304
305 if (strcmp(tokens[1], "1"))
306 /* Invalid measurement. */
307 return;
308
309 if (strcmp(tokens[2], "3"))
310 /* Only interested in input from the meter mode source. */
311 return;
312
313 devc = sdi->priv;
314
315 /* Measurement type 11 == absolute, 19 = relative */
316 meas_type = strtol(tokens[0], NULL, 10);
317 if (meas_type != 11 && meas_type != 19)
318 /* Device is in some mode we don't support. */
319 return;
320
321 /* We might get metadata for absolute and relative mode (if the device
322 * is in relative mode). In that case, relative takes precedence. */
323 if (meas_type == 11 && devc->meas_type == 19)
324 return;
325
326 meas_unit = strtol(tokens[3], NULL, 10);
327 if (meas_unit == 0)
328 /* Device is turned off. Really. */
329 return;
330 meas_char = strtol(tokens[4], NULL, 10);
331
55ec0b67
UH
332 devc->mq = 0;
333 devc->unit = 0;
334 devc->mqflags = 0;
335
11fb7110
BV
336 switch (meas_unit) {
337 case 1:
338 devc->mq = SR_MQ_VOLTAGE;
339 devc->unit = SR_UNIT_VOLT;
340 if (meas_char == 1)
341 devc->mqflags |= SR_MQFLAG_DC;
342 else if (meas_char == 2)
343 devc->mqflags |= SR_MQFLAG_AC;
344 else if (meas_char == 3)
345 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
346 else if (meas_char == 15)
64aa214a 347 devc->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
11fb7110
BV
348 break;
349 case 2:
350 devc->mq = SR_MQ_CURRENT;
351 devc->unit = SR_UNIT_AMPERE;
352 if (meas_char == 1)
353 devc->mqflags |= SR_MQFLAG_DC;
354 else if (meas_char == 2)
355 devc->mqflags |= SR_MQFLAG_AC;
356 else if (meas_char == 3)
357 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
358 break;
359 case 3:
360 if (meas_char == 1) {
361 devc->mq = SR_MQ_RESISTANCE;
362 devc->unit = SR_UNIT_OHM;
363 } else if (meas_char == 16) {
364 devc->mq = SR_MQ_CONTINUITY;
365 devc->unit = SR_UNIT_BOOLEAN;
366 }
367 break;
368 case 12:
369 devc->mq = SR_MQ_TEMPERATURE;
370 devc->unit = SR_UNIT_CELSIUS;
371 break;
372 case 13:
373 devc->mq = SR_MQ_TEMPERATURE;
374 devc->unit = SR_UNIT_FAHRENHEIT;
375 break;
376 default:
377 sr_dbg("unknown unit: %d", meas_unit);
378 }
3f5cf2a0 379 if (devc->mq == 0 && devc->unit == 0)
11fb7110
BV
380 return;
381
382 /* If we got here, we know how to interpret the measurement. */
383 devc->meas_type = meas_type;
384 if (meas_type == 11)
385 /* Absolute meter reading. */
386 devc->is_relative = FALSE;
387 else if (!strcmp(tokens[0], "19"))
388 /* Relative meter reading. */
389 devc->is_relative = TRUE;
390
391}
392
393static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
394{
395 struct dev_context *devc;
396 struct sr_datafeed_packet packet;
3f5cf2a0
UH
397 struct sr_datafeed_analog analog;
398 struct sr_analog_encoding encoding;
399 struct sr_analog_meaning meaning;
400 struct sr_analog_spec spec;
11fb7110 401 float fvalue;
d2e3ce76 402 int digits;
11fb7110 403
d2e3ce76 404 digits = 2;
11fb7110
BV
405 if (!strcmp(tokens[0], "9.9E+37")) {
406 /* An invalid measurement shows up on the display as "OL", but
407 * comes through like this. Since comparing 38-digit floats
408 * is rather problematic, we'll cut through this here. */
409 fvalue = NAN;
410 } else {
357e341d 411 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
11fb7110
BV
412 sr_err("Invalid float '%s'.", tokens[0]);
413 return;
414 }
d2e3ce76 415 digits = count_digits(tokens[0]);
11fb7110
BV
416 }
417
418 devc = sdi->priv;
3f5cf2a0 419 if (devc->mq == 0 || devc->unit == 0)
11fb7110
BV
420 /* Don't have valid metadata yet. */
421 return;
422
11fb7110
BV
423 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
424 fvalue = INFINITY;
425 else if (devc->mq == SR_MQ_CONTINUITY) {
426 if (isnan(fvalue))
427 fvalue = 0.0;
428 else
429 fvalue = 1.0;
430 }
431
d2e3ce76 432 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
3f5cf2a0 433 analog.meaning->channels = sdi->channels;
11fb7110
BV
434 analog.num_samples = 1;
435 analog.data = &fvalue;
3f5cf2a0
UH
436 analog.meaning->mq = devc->mq;
437 analog.meaning->unit = devc->unit;
438 analog.meaning->mqflags = 0;
439 packet.type = SR_DF_ANALOG;
11fb7110 440 packet.payload = &analog;
695dc859 441 sr_session_send(sdi, &packet);
11fb7110 442
6aacf011 443 sr_sw_limits_update_samples_read(&devc->limits, 1);
11fb7110
BV
444}
445
d38d2ef0
BV
446static void handle_line(const struct sr_dev_inst *sdi)
447{
448 struct dev_context *devc;
919681f0 449 struct sr_serial_dev_inst *serial;
11fb7110
BV
450 int num_tokens, n, i;
451 char cmd[16], **tokens;
ebf6aa30 452 int ret;
d38d2ef0
BV
453
454 devc = sdi->priv;
919681f0 455 serial = sdi->conn;
31d84da3 456 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
d38d2ef0
BV
457
458 if (devc->buflen == 1) {
459 if (devc->buf[0] != '0') {
460 /* Not just a CMD_ACK from the query command. */
31d84da3 461 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
d38d2ef0
BV
462 devc->expect_response = FALSE;
463 }
464 devc->buflen = 0;
465 return;
466 }
467
d38d2ef0 468 tokens = g_strsplit(devc->buf, ",", 0);
4cea0ff7 469 if (tokens[0]) {
ebf6aa30
GS
470 switch (devc->profile->model) {
471 case FLUKE_87:
472 case FLUKE_89:
473 case FLUKE_187:
474 case FLUKE_189:
fe31f8b9 475 devc->expect_response = FALSE;
19ab8e36 476 handle_qm_18x(sdi, tokens);
ebf6aa30 477 break;
ebf6aa30 478 case FLUKE_190:
11fb7110 479 devc->expect_response = FALSE;
ebf6aa30
GS
480 num_tokens = g_strv_length(tokens);
481 if (num_tokens < 7) {
11fb7110
BV
482 /* Response to QM <n> measurement request. */
483 handle_qm_19x_data(sdi, tokens);
ebf6aa30 484 break;
11fb7110 485 }
ebf6aa30
GS
486 /*
487 * Response to QM: This is a comma-separated list of
488 * fields with metadata about the measurement. This
489 * format can return multiple sets of metadata,
490 * split into sets of 7 tokens each.
491 */
492 devc->meas_type = 0;
493 for (i = 0; i < num_tokens; i += 7)
494 handle_qm_19x_meta(sdi, tokens + i);
495 if (devc->meas_type) {
496 /*
497 * Slip the request in now, before the main
498 * timer loop asks for metadata again.
499 */
500 n = sprintf(cmd, "QM %d\r", devc->meas_type);
501 ret = serial_write_blocking(serial,
502 cmd, n, SERIAL_WRITE_TIMEOUT_MS);
503 if (ret < 0)
504 sr_err("Cannot send QM (measurement).");
505 }
506 break;
0d68d6ba
GS
507 case FLUKE_287:
508 case FLUKE_289:
509 devc->expect_response = FALSE;
510 handle_qm_28x(sdi, tokens);
511 break;
d38d2ef0 512 }
d38d2ef0
BV
513 }
514 g_strfreev(tokens);
515 devc->buflen = 0;
d38d2ef0
BV
516}
517
518SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
519{
642e9d62 520 struct sr_dev_inst *sdi;
d38d2ef0 521 struct dev_context *devc;
919681f0 522 struct sr_serial_dev_inst *serial;
d38d2ef0
BV
523 int len;
524 int64_t now, elapsed;
525
58d03f03
BV
526 (void)fd;
527
d38d2ef0
BV
528 if (!(sdi = cb_data))
529 return TRUE;
530
531 if (!(devc = sdi->priv))
532 return TRUE;
533
919681f0 534 serial = sdi->conn;
d38d2ef0
BV
535 if (revents == G_IO_IN) {
536 /* Serial data arrived. */
0c5f2abc 537 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
707fa85a 538 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
d38d2ef0
BV
539 if (len < 1)
540 break;
541 devc->buflen++;
542 *(devc->buf + devc->buflen) = '\0';
543 if (*(devc->buf + devc->buflen - 1) == '\r') {
544 *(devc->buf + --devc->buflen) = '\0';
545 handle_line(sdi);
546 break;
547 }
548 }
549 }
550
6aacf011 551 if (sr_sw_limits_check(&devc->limits)) {
d2f7c417 552 sr_dev_acquisition_stop(sdi);
d38d2ef0
BV
553 return TRUE;
554 }
555
556 now = g_get_monotonic_time() / 1000;
557 elapsed = now - devc->cmd_sent_at;
558 /* Send query command at poll_period interval, or after 1 second
d4b11de0
BV
559 * has elapsed. This will make it easier to recover from any
560 * out-of-sync or temporary disconnect issues. */
d38d2ef0 561 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
d4b11de0 562 || elapsed > devc->profile->timeout) {
20401400 563 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
bb27c765 564 sr_err("Unable to send QM.");
d38d2ef0
BV
565 devc->cmd_sent_at = now;
566 devc->expect_response = TRUE;
567 }
568
569 return TRUE;
570}