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