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