]>
Commit | Line | Data |
---|---|---|
7b4edcb6 MH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
c90beca7 | 4 | * Copyright (C) 2013, 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz> |
7b4edcb6 MH |
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 | ||
6392d599 MH |
20 | /** @file |
21 | * Gossen Metrawatt Metrahit 1x/2x drivers | |
c90beca7 | 22 | * @internal |
6392d599 MH |
23 | */ |
24 | ||
f5792417 MH |
25 | #include <math.h> |
26 | #include <string.h> | |
7b4edcb6 MH |
27 | #include "protocol.h" |
28 | ||
c90beca7 MH |
29 | /* Internal Headers */ |
30 | static guchar calc_chksum_14(guchar* dta); | |
31 | static int chk_msg14(struct sr_dev_inst *sdi); | |
32 | ||
f5792417 MH |
33 | /** Set or clear flags in devc->mqflags. */ |
34 | static void setmqf(struct dev_context *devc, uint64_t flags, gboolean set) | |
35 | { | |
36 | if (set) | |
37 | devc->mqflags |= flags; | |
38 | else | |
39 | devc->mqflags &= ~flags; | |
40 | } | |
41 | ||
fc348b77 UH |
42 | /** Decode current type and measured value, Metrahit 12-16. */ |
43 | static void decode_ctmv_16(uint8_t ctmv, struct dev_context *devc) | |
f5792417 MH |
44 | { |
45 | devc->mq = 0; | |
46 | devc->unit = 0; | |
47 | devc->mqflags = 0; | |
48 | ||
49 | switch (ctmv) { | |
50 | case 0x00: /* 0000 - */ | |
51 | break; | |
52 | case 0x01: /* 0001 mV DC */ | |
53 | devc->scale1000 = -1; /* Fall through */ | |
54 | case 0x02: /* 0010 V DC */ | |
55 | case 0x03: /* 0011 V AC+DC */ | |
56 | case 0x04: /* 0100 V AC */ | |
57 | devc->mq = SR_MQ_VOLTAGE; | |
58 | devc->unit = SR_UNIT_VOLT; | |
59 | if (ctmv <= 0x03) | |
60 | devc->mqflags |= SR_MQFLAG_DC; | |
61 | if (ctmv >= 0x03) { | |
62 | devc->mqflags |= SR_MQFLAG_AC; | |
873e0c12 | 63 | if (devc->model >= METRAHIT_16S) |
f5792417 | 64 | devc->mqflags |= SR_MQFLAG_RMS; |
fc348b77 | 65 | } |
f5792417 MH |
66 | break; |
67 | case 0x05: /* 0101 Hz (15S/16S only) */ | |
68 | case 0x06: /* 0110 kHz (15S/16S only) */ | |
69 | devc->mq = SR_MQ_FREQUENCY; | |
70 | devc->unit = SR_UNIT_HERTZ; | |
71 | if (ctmv == 0x06) | |
72 | devc->scale1000 = 1; | |
73 | break; | |
74 | case 0x07: /* 0111 % (15S/16S only) */ | |
75 | devc->mq = SR_MQ_DUTY_CYCLE; | |
76 | devc->unit = SR_UNIT_PERCENTAGE; | |
77 | break; | |
78 | case 0x08: /* 1000 Diode */ | |
79 | devc->mq = SR_MQ_VOLTAGE; | |
80 | devc->unit = SR_UNIT_VOLT; | |
81 | devc->mqflags |= SR_MQFLAG_DIODE; | |
82 | break; | |
83 | case 0x09: /* 1001 Ohm, °C */ | |
84 | case 0x0a: /* 1010 kOhm */ | |
85 | case 0x0b: /* 1011 MOhm */ | |
86 | devc->mq = SR_MQ_RESISTANCE; /* Changed to temp. later if req.*/ | |
87 | devc->unit = SR_UNIT_OHM; | |
88 | devc->scale1000 = ctmv - 0x09; | |
89 | break; | |
90 | case 0x0c: /* 1100 nF (15S/16S only) */ | |
91 | case 0x0d: /* 1101 µF (15S/16S only) */ | |
92 | devc->mq = SR_MQ_CAPACITANCE; | |
93 | devc->unit = SR_UNIT_FARAD; | |
94 | if (ctmv == 0x0c) | |
95 | devc->scale1000 = -3; | |
96 | else | |
97 | devc->scale1000 = -2; | |
98 | break; | |
99 | case 0x0e: /* mA, µA */ | |
100 | devc->scale1000 = -1; /* Fall through. */ | |
101 | case 0x0f: /* A */ | |
102 | devc->mq = SR_MQ_CURRENT; | |
103 | devc->unit = SR_UNIT_AMPERE; | |
873e0c12 | 104 | if (devc->model == METRAHIT_16S) |
f5792417 MH |
105 | devc->mqflags |= SR_MQFLAG_RMS; |
106 | /* 16I A only with clamp, RMS questionable. */ | |
107 | break; | |
108 | } | |
109 | } | |
110 | ||
fc348b77 UH |
111 | /** |
112 | * Decode range/sign/acdc byte special chars (Metrahit 12-16). | |
113 | * | |
1d4a2839 | 114 | * @param[in] rs Range and sign byte. |
f5792417 | 115 | */ |
fc348b77 | 116 | static void decode_rs_16(uint8_t rs, struct dev_context *devc) |
f5792417 MH |
117 | { |
118 | sr_spew("decode_rs_16(%d) scale = %f", rs, devc->scale); | |
119 | ||
1d4a2839 | 120 | if (rs & 0x04) /* Sign */ |
f5792417 MH |
121 | devc->scale *= -1.0; |
122 | ||
123 | if (devc->mq == SR_MQ_CURRENT) { | |
1d4a2839 | 124 | if (rs & 0x08) /* Current is AC */ |
f5792417 MH |
125 | devc->mqflags |= SR_MQFLAG_AC; |
126 | else | |
127 | devc->mqflags |= SR_MQFLAG_DC; | |
128 | } | |
129 | ||
130 | switch (rs & 0x03) { | |
131 | case 0: | |
fc348b77 | 132 | if (devc->mq == SR_MQ_VOLTAGE) /* V */ |
f5792417 MH |
133 | devc->scale *= 0.1; |
134 | else if (devc->mq == SR_MQ_CURRENT) /* 000.0 µA */ | |
7f7f6a2e | 135 | devc->scale *= 0.00001; |
f5792417 MH |
136 | else if (devc->mq == SR_MQ_RESISTANCE) { |
137 | if (devc->buflen >= 10) { | |
138 | /* °C with 10 byte msg type, otherwise GOhm. */ | |
139 | devc->mq = SR_MQ_TEMPERATURE; | |
140 | devc->unit = SR_UNIT_CELSIUS; | |
141 | devc->scale *= 0.01; | |
42f2f8a5 | 142 | } else if (devc->scale1000 == 2) { |
f5792417 MH |
143 | /* 16I Iso 500/1000V 3 GOhm */ |
144 | devc->scale *= 0.1; | |
145 | } | |
146 | } | |
147 | break; | |
148 | case 1: | |
149 | devc->scale *= 0.0001; | |
150 | break; | |
151 | case 2: | |
152 | devc->scale *= 0.001; | |
153 | break; | |
154 | case 3: | |
155 | devc->scale *= 0.01; | |
156 | break; | |
157 | } | |
158 | } | |
159 | ||
fc348b77 UH |
160 | /** |
161 | * Decode special chars, Metrahit 12-16. | |
162 | * | |
163 | * @param[in] spc Special characters 1 and 2 (s1 | (s2 << 4)). | |
f5792417 | 164 | */ |
fc348b77 | 165 | static void decode_spc_16(uint8_t spc, struct dev_context *devc) |
f5792417 MH |
166 | { |
167 | /* xxxx1xxx ON */ | |
168 | /* TODO: What does that mean? Power on? The 16I sets this. */ | |
169 | /* xxxxx1xx BEEP */ | |
170 | /* xxxxxx1x Low battery */ | |
171 | /* xxxxxxx1 FUSE */ | |
172 | /* 1xxxxxxx MIN */ | |
173 | setmqf(devc, SR_MQFLAG_MIN, spc & 0x80); | |
174 | ||
175 | /* x1xxxxxx MAN */ | |
176 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x40)); | |
177 | ||
178 | /* xx1xxxxx DATA */ | |
179 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x20); | |
180 | ||
181 | /* xxx1xxxx MAX */ | |
182 | setmqf(devc, SR_MQFLAG_MAX, spc & 0x10); | |
183 | } | |
184 | ||
fc348b77 UH |
185 | /** Decode current type and measured value, Metrahit 18. */ |
186 | static void decode_ctmv_18(uint8_t ctmv, struct dev_context *devc) | |
f5792417 MH |
187 | { |
188 | devc->mq = 0; | |
189 | devc->unit = 0; | |
190 | devc->mqflags = 0; | |
191 | ||
192 | switch (ctmv) { | |
193 | case 0x00: /* 0000 - */ | |
194 | break; | |
195 | case 0x01: /* 0001 V AC */ | |
196 | case 0x02: /* 0010 V AC+DC */ | |
197 | case 0x03: /* 0011 V DC */ | |
198 | devc->mq = SR_MQ_VOLTAGE; | |
199 | devc->unit = SR_UNIT_VOLT; | |
200 | if (ctmv <= 0x02) | |
201 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
202 | if (ctmv >= 0x02) | |
203 | devc->mqflags |= SR_MQFLAG_DC; | |
204 | break; | |
205 | case 0x04: /* 0100 Ohm/Ohm with buzzer */ | |
206 | devc->mq = SR_MQ_RESISTANCE; | |
207 | devc->unit = SR_UNIT_OHM; | |
208 | break; | |
fc348b77 | 209 | case 0x05: /* 0101 Diode/Diode with buzzer */ |
f5792417 MH |
210 | devc->mq = SR_MQ_VOLTAGE; |
211 | devc->unit = SR_UNIT_VOLT; | |
212 | devc->mqflags |= SR_MQFLAG_DIODE; | |
213 | break; | |
214 | case 0x06: /* 0110 °C */ | |
215 | devc->mq = SR_MQ_TEMPERATURE; | |
216 | devc->unit = SR_UNIT_CELSIUS; | |
217 | break; | |
218 | case 0x07: /* 0111 F */ | |
219 | devc->mq = SR_MQ_CAPACITANCE; | |
220 | devc->unit = SR_UNIT_FARAD; | |
221 | break; | |
222 | case 0x08: /* 1000 mA DC */ | |
223 | case 0x09: /* 1001 A DC */ | |
224 | case 0x0a: /* 1010 mA AC+DC */ | |
225 | case 0x0b: /* 1011 A AC+DC */ | |
226 | devc->mq = SR_MQ_CURRENT; | |
227 | devc->unit = SR_UNIT_AMPERE; | |
228 | devc->mqflags |= SR_MQFLAG_DC; | |
229 | if (ctmv >= 0x0a) | |
230 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
231 | if ((ctmv == 0x08) || (ctmv == 0x0a)) | |
232 | devc->scale1000 = -1; | |
233 | break; | |
234 | case 0x0c: /* 1100 Hz */ | |
235 | devc->mq = SR_MQ_FREQUENCY; | |
236 | devc->unit = SR_UNIT_HERTZ; | |
237 | break; | |
238 | case 0x0d: /* 1101 dB */ | |
239 | devc->mq = SR_MQ_VOLTAGE; | |
240 | devc->unit = SR_UNIT_DECIBEL_VOLT; | |
241 | devc->mqflags |= SR_MQFLAG_AC; /* dB available for AC only */ | |
242 | break; | |
243 | case 0x0e: /* 1110 Events AC, Events AC+DC. Actually delivers just | |
c90beca7 | 244 | * current voltage via IR, nothing more. */ |
f5792417 MH |
245 | devc->mq = SR_MQ_VOLTAGE; |
246 | devc->unit = SR_UNIT_VOLT; | |
247 | devc->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; | |
248 | break; | |
249 | case 0x0f: /* 1111 Clock */ | |
250 | devc->mq = SR_MQ_TIME; | |
251 | devc->unit = SR_UNIT_SECOND; | |
252 | devc->mqflags |= SR_MQFLAG_DURATION; | |
253 | break; | |
254 | } | |
255 | } | |
256 | ||
fc348b77 UH |
257 | /** |
258 | * Decode range/sign/acdc byte special chars, Metrahit 18. | |
259 | * | |
260 | * @param[in] rs Rance/sign byte. | |
f5792417 | 261 | */ |
fc348b77 | 262 | static void decode_rs_18(uint8_t rs, struct dev_context *devc) |
f5792417 MH |
263 | { |
264 | int range; | |
265 | ||
266 | /* Sign */ | |
267 | if (((devc->scale > 0) && (rs & 0x08)) || | |
c90beca7 | 268 | ((devc->scale < 0) && !(rs & 0x08))) |
f5792417 MH |
269 | devc->scale *= -1.0; |
270 | ||
271 | /* Range */ | |
272 | range = rs & 0x07; | |
273 | switch (devc->mq) { | |
274 | case SR_MQ_VOLTAGE: | |
275 | if (devc->unit == SR_UNIT_DECIBEL_VOLT) { | |
276 | devc->scale *= pow(10.0, -2); | |
fc348b77 UH |
277 | /* |
278 | * When entering relative mode, the device switches | |
f5792417 MH |
279 | * from 10 byte to 6 byte msg format. Unfortunately |
280 | * it switches back to 10 byte when the second value | |
281 | * is measured, so that's not sufficient to | |
282 | * identify relative mode. | |
283 | */ | |
284 | } | |
285 | else if (devc->vmains_29S) | |
286 | devc->scale *= pow(10.0, range - 2); | |
287 | else | |
288 | devc->scale *= pow(10.0, range - 5); | |
289 | break; | |
290 | case SR_MQ_CURRENT: | |
291 | if (devc->scale1000 == -1) | |
292 | devc->scale *= pow(10.0, range - 5); | |
293 | else | |
294 | devc->scale *= pow(10.0, range - 4); | |
295 | break; | |
296 | case SR_MQ_RESISTANCE: | |
297 | devc->scale *= pow(10.0, range - 2); | |
298 | break; | |
299 | case SR_MQ_FREQUENCY: | |
c90beca7 | 300 | devc->scale *= pow(10.0, range - 2); |
f5792417 MH |
301 | break; |
302 | case SR_MQ_TEMPERATURE: | |
303 | devc->scale *= pow(10.0, range - 2); | |
304 | break; | |
305 | case SR_MQ_CAPACITANCE: | |
c90beca7 | 306 | devc->scale *= pow(10.0, range - 13); |
f5792417 | 307 | break; |
c90beca7 | 308 | /* TODO: 29S Mains measurements. */ |
f5792417 MH |
309 | } |
310 | } | |
311 | ||
fc348b77 UH |
312 | /** |
313 | * Decode special chars, Metrahit 18. | |
314 | * | |
315 | * @param[in] spc Special characters 1 and 2 (s1 | (s2 << 4)). | |
f5792417 | 316 | */ |
fc348b77 | 317 | static void decode_spc_18(uint8_t spc, struct dev_context *devc) |
f5792417 MH |
318 | { |
319 | /* xxxx1xxx ZERO */ | |
320 | /* xxxxx1xx BEEP */ | |
321 | /* xxxxxx1x Low battery */ | |
322 | /* xxxxxxx1 Fuse */ | |
323 | ||
324 | if (devc->mq == SR_MQ_TIME) { | |
325 | /* xxx1xxxx Clock running: 1; stop: 0 */ | |
326 | sr_spew("Clock running: %d", spc >> 4); | |
fc348b77 | 327 | } else { |
f5792417 MH |
328 | /* 1xxxxxxx MAN */ |
329 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); | |
330 | ||
331 | /* x1xxxxxx MIN */ | |
332 | setmqf(devc, SR_MQFLAG_MIN, spc & 0x40); | |
333 | ||
334 | /* xx1xxxxx MAX */ | |
335 | setmqf(devc, SR_MQFLAG_MAX, spc & 0x20); | |
336 | ||
337 | /* xxx1xxxx DATA */ | |
338 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); | |
339 | } | |
340 | } | |
341 | ||
fc348b77 UH |
342 | /** |
343 | * Decode current type and measured value, Metrahit 2x. | |
344 | * | |
345 | * @param[in] ctmv Current type and measured value (v1 | (v2 << 4)). | |
f5792417 | 346 | */ |
fc348b77 | 347 | static void decode_ctmv_2x(uint8_t ctmv, struct dev_context *devc) |
f5792417 MH |
348 | { |
349 | if ((ctmv > 0x1c) || (!devc)) { | |
fc348b77 | 350 | sr_err("decode_ctmv_2x(%d): invalid param(s)!", ctmv); |
f5792417 MH |
351 | return; |
352 | } | |
353 | ||
354 | devc->mq = 0; | |
355 | devc->unit = 0; | |
356 | devc->mqflags = 0; | |
357 | ||
358 | switch (ctmv) { | |
359 | /* 00000 unused */ | |
360 | case 0x01: /* 00001 V DC */ | |
361 | case 0x02: /* 00010 V AC+DC */ | |
362 | case 0x03: /* 00011 V AC */ | |
363 | devc->mq = SR_MQ_VOLTAGE; | |
364 | devc->unit = SR_UNIT_VOLT; | |
365 | if (ctmv <= 0x02) | |
366 | devc->mqflags |= SR_MQFLAG_DC; | |
367 | if (ctmv >= 0x02) { | |
368 | devc->mqflags |= SR_MQFLAG_AC; | |
873e0c12 | 369 | if (devc->model >= METRAHIT_24S) |
6392d599 | 370 | devc->mqflags |= SR_MQFLAG_RMS; |
f5792417 MH |
371 | } |
372 | break; | |
373 | case 0x04: /* 00100 mA DC */ | |
374 | case 0x05: /* 00101 mA AC+DC */ | |
375 | devc->scale1000 = -1; | |
376 | case 0x06: /* 00110 A DC */ | |
377 | case 0x07: /* 00111 A AC+DC */ | |
378 | devc->mq = SR_MQ_CURRENT; | |
379 | devc->unit = SR_UNIT_AMPERE; | |
380 | devc->mqflags |= SR_MQFLAG_DC; | |
381 | if ((ctmv == 0x05) || (ctmv == 0x07)) { | |
382 | devc->mqflags |= SR_MQFLAG_AC; | |
873e0c12 | 383 | if (devc->model >= METRAHIT_24S) |
f5792417 MH |
384 | devc->mqflags |= SR_MQFLAG_RMS; |
385 | } | |
386 | break; | |
387 | case 0x08: /* 01000 Ohm */ | |
388 | devc->mq = SR_MQ_RESISTANCE; | |
389 | devc->unit = SR_UNIT_OHM; | |
390 | break; | |
391 | case 0x09: /* 01001 F */ | |
392 | devc->mq = SR_MQ_CAPACITANCE; | |
393 | devc->unit = SR_UNIT_FARAD; | |
394 | devc->scale *= 0.1; | |
395 | break; | |
c90beca7 | 396 | case 0x0a: /* 01010 V dB */ |
f5792417 MH |
397 | devc->mq = SR_MQ_VOLTAGE; |
398 | devc->unit = SR_UNIT_DECIBEL_VOLT; | |
399 | devc->mqflags |= SR_MQFLAG_AC; | |
6392d599 MH |
400 | if (devc->model >= METRAHIT_24S) |
401 | devc->mqflags |= SR_MQFLAG_RMS; | |
f5792417 MH |
402 | break; |
403 | case 0x0b: /* 01011 Hz U ACDC */ | |
404 | case 0x0c: /* 01100 Hz U AC */ | |
405 | devc->mq = SR_MQ_FREQUENCY; | |
406 | devc->unit = SR_UNIT_HERTZ; | |
407 | devc->mqflags |= SR_MQFLAG_AC; | |
408 | if (ctmv <= 0x0b) | |
409 | devc->mqflags |= SR_MQFLAG_DC; | |
410 | break; | |
411 | case 0x0d: /* 01101 W on power, mA range (29S only) */ | |
6392d599 MH |
412 | devc->scale *= 0.001; |
413 | /* Fall through! */ | |
f5792417 | 414 | case 0x0e: /* 01110 W on power, A range (29S only) */ |
f5792417 MH |
415 | devc->mq = SR_MQ_POWER; |
416 | devc->unit = SR_UNIT_WATT; | |
417 | break; | |
418 | case 0x0f: /* 01111 Diode */ | |
419 | case 0x10: /* 10000 Diode with buzzer (actually cont. with voltage) */ | |
420 | devc->unit = SR_UNIT_VOLT; | |
421 | if (ctmv == 0x0f) { | |
422 | devc->mq = SR_MQ_VOLTAGE; | |
423 | devc->mqflags |= SR_MQFLAG_DIODE; | |
fc348b77 | 424 | } else { |
f5792417 MH |
425 | devc->mq = SR_MQ_CONTINUITY; |
426 | devc->scale *= 0.00001; | |
427 | } | |
428 | devc->unit = SR_UNIT_VOLT; | |
429 | break; | |
430 | case 0x11: /* 10001 Ohm with buzzer */ | |
431 | devc->mq = SR_MQ_CONTINUITY; | |
432 | devc->unit = SR_UNIT_OHM; | |
433 | devc->scale1000 = -1; | |
434 | break; | |
435 | case 0x12: /* 10010 Temperature */ | |
436 | devc->mq = SR_MQ_TEMPERATURE; | |
437 | devc->unit = SR_UNIT_CELSIUS; | |
438 | /* This can be Fahrenheit. That is detected by range=4 later. */ | |
439 | break; | |
440 | /* 0x13 10011, 0x14 10100 unsed */ | |
441 | case 0x15: /* 10101 Press (29S only) */ | |
442 | /* TODO: What does that mean? Possibly phase shift? | |
443 | Then we need a unit/flag for it. */ | |
444 | devc->mq = SR_MQ_GAIN; | |
445 | devc->unit = SR_UNIT_PERCENTAGE; | |
446 | break; | |
447 | case 0x16: /* 10110 Pulse W (29S only) */ | |
448 | /* TODO: Own unit and flag for this! */ | |
449 | devc->mq = SR_MQ_POWER; | |
450 | devc->unit = SR_UNIT_WATT; | |
451 | break; | |
452 | case 0x17: /* 10111 TRMS V on mains (29S only) */ | |
453 | devc->mq = SR_MQ_VOLTAGE; | |
454 | devc->unit = SR_UNIT_VOLT; | |
455 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
456 | devc->vmains_29S = TRUE; | |
457 | break; | |
458 | case 0x18: /* 11000 Counter (zero crossings of a signal) */ | |
459 | devc->mq = SR_MQ_VOLTAGE; | |
460 | devc->unit = SR_UNIT_UNITLESS; | |
461 | break; | |
462 | case 0x19: /* 11001 Events U ACDC */ | |
463 | case 0x1a: /* 11010 Events U AC */ | |
464 | /* TODO: No unit or flags for this yet! */ | |
465 | devc->mq = SR_MQ_VOLTAGE; | |
466 | devc->unit = SR_UNIT_UNITLESS; | |
467 | devc->mqflags |= SR_MQFLAG_AC; | |
468 | if (ctmv <= 0x19) | |
469 | devc->mqflags |= SR_MQFLAG_DC; | |
470 | break; | |
471 | case 0x1b: /* 11011 pulse on mains (29S only) */ | |
472 | /* TODO: No unit or flags for this yet! */ | |
473 | devc->mq = SR_MQ_VOLTAGE; | |
474 | devc->unit = SR_UNIT_UNITLESS; | |
475 | devc->mqflags |= SR_MQFLAG_AC; | |
476 | break; | |
477 | case 0x1c: /* 11100 dropout on mains (29S only) */ | |
478 | /* TODO: No unit or flags for this yet! */ | |
479 | devc->mq = SR_MQ_VOLTAGE; | |
480 | devc->unit = SR_UNIT_UNITLESS; | |
481 | devc->mqflags |= SR_MQFLAG_AC; | |
482 | break; | |
d1d3b7df MH |
483 | case 0x1f: /* 11111 Undocumented: 25S in stopwatch mode. |
484 | The value is voltage, not time, so treat it such. */ | |
485 | devc->mq = SR_MQ_VOLTAGE; | |
486 | devc->unit = SR_UNIT_VOLT; | |
487 | devc->mqflags |= SR_MQFLAG_DC; | |
488 | break; | |
489 | case 0x20: /* 100000 Undocumented: 25S in event count mode. | |
490 | Value is 0 anyway. */ | |
491 | devc->mq = SR_MQ_VOLTAGE; | |
492 | devc->unit = SR_UNIT_UNITLESS; | |
493 | break; | |
f5792417 | 494 | default: |
d1d3b7df | 495 | sr_err("decode_ctmv_2x(%d, ...): Unknown ctmv!", ctmv); |
fc348b77 | 496 | break; |
f5792417 MH |
497 | } |
498 | } | |
499 | ||
fc348b77 | 500 | /** |
6392d599 | 501 | * Decode range/sign/acdc byte special chars, Metrahit 2x, table TR. |
fc348b77 | 502 | * |
6392d599 | 503 | * @param[in] rs Range/sign byte. |
f5792417 | 504 | */ |
fc348b77 | 505 | static void decode_rs_2x(uint8_t rs, struct dev_context *devc) |
f5792417 | 506 | { |
fc348b77 | 507 | int range; |
f5792417 MH |
508 | |
509 | /* Sign */ | |
510 | if (((devc->scale > 0) && (rs & 0x08)) || | |
6392d599 | 511 | ((devc->scale < 0) && !(rs & 0x08))) |
f5792417 MH |
512 | devc->scale *= -1.0; |
513 | ||
514 | /* Range */ | |
515 | range = rs & 0x07; | |
516 | switch (devc->mq) { | |
517 | case SR_MQ_VOLTAGE: | |
fc348b77 | 518 | if (devc->unit == SR_UNIT_DECIBEL_VOLT) |
f5792417 | 519 | devc->scale *= pow(10.0, -3); |
f5792417 MH |
520 | else if (devc->vmains_29S) |
521 | devc->scale *= pow(10.0, range - 2); | |
c90beca7 | 522 | else |
f5792417 | 523 | devc->scale *= pow(10.0, range - 6); |
f5792417 MH |
524 | break; |
525 | case SR_MQ_CURRENT: | |
c90beca7 MH |
526 | if (devc->scale1000 != -1) /* uA, mA */ |
527 | range += 1;/* mA and A ranges differ by 10^4, not 10^3!*/ | |
528 | devc->scale *= pow(10.0, range - 6); | |
f5792417 MH |
529 | break; |
530 | case SR_MQ_RESISTANCE: | |
531 | devc->scale *= pow(10.0, range - 3); | |
532 | break; | |
533 | case SR_MQ_FREQUENCY: | |
534 | devc->scale *= pow(10.0, range - 3); | |
535 | break; | |
536 | case SR_MQ_TEMPERATURE: | |
fc348b77 | 537 | if (range == 4) /* Indicator for °F */ |
f5792417 | 538 | devc->unit = SR_UNIT_FAHRENHEIT; |
f5792417 MH |
539 | devc->scale *= pow(10.0, - 2); |
540 | break; | |
541 | case SR_MQ_CAPACITANCE: | |
6392d599 MH |
542 | if (range == 7) |
543 | range -= 1; /* Same value as range 6 */ | |
f5792417 MH |
544 | devc->scale *= pow(10.0, range - 13); |
545 | break; | |
546 | /* TODO: 29S Mains measurements. */ | |
547 | } | |
548 | } | |
549 | ||
c90beca7 MH |
550 | /** |
551 | * Decode range/sign/acdc byte special chars, Metrahit 2x, table TR 2. | |
552 | * | |
553 | * @param[in] rs Range/sign byte. | |
554 | */ | |
555 | static void decode_rs_2x_TR2(uint8_t rs, struct dev_context *devc) | |
556 | { | |
557 | int range; | |
558 | ||
559 | /* Range */ | |
560 | range = rs & 0x07; | |
561 | switch (devc->mq) { | |
562 | case SR_MQ_CURRENT: | |
563 | if (devc->scale1000 == -1) /* mA */ | |
564 | switch(range) { | |
565 | case 0: case 1: /* 100, 300 µA */ | |
566 | devc->scale *= pow(10.0, -6); | |
567 | break; | |
568 | case 2: case 3: /* 1, 3 mA */ | |
569 | devc->scale *= pow(10.0, -5); | |
570 | break; | |
571 | case 4: case 5: /* 10, 30 mA */ | |
572 | devc->scale *= pow(10.0, -4); | |
573 | break; | |
574 | case 6: case 7: /* 100, 300 mA */ | |
575 | devc->scale *= pow(10.0, -3); | |
576 | break; | |
577 | } | |
578 | else /* A */ | |
579 | switch(range) { | |
580 | case 0: case 1: /* 1, 3 A */ | |
581 | devc->scale *= pow(10.0, -5); | |
582 | break; | |
583 | case 2: /* 10 A */ | |
584 | devc->scale *= pow(10.0, -4); | |
585 | break; | |
586 | } | |
587 | break; | |
588 | default: | |
589 | decode_rs_2x(rs, devc); | |
590 | return; | |
591 | } | |
592 | ||
593 | /* Sign */ | |
594 | if (((devc->scale > 0) && (rs & 0x08)) || | |
595 | ((devc->scale < 0) && !(rs & 0x08))) | |
596 | devc->scale *= -1.0; | |
597 | } | |
598 | ||
599 | ||
fc348b77 UH |
600 | /** |
601 | * Decode special chars (Metrahit 2x). | |
602 | * | |
603 | * @param[in] spc Special characters 1 and 2 (s1 | (s2 << 4)). | |
f5792417 | 604 | */ |
fc348b77 | 605 | static void decode_spc_2x(uint8_t spc, struct dev_context *devc) |
f5792417 MH |
606 | { |
607 | /* xxxxxxx1 Fuse */ | |
608 | ||
609 | /* xxxxxx1x Low battery */ | |
610 | ||
611 | /* xxxxx1xx BEEP */ | |
612 | ||
613 | /* xxxx1xxx ZERO */ | |
614 | ||
615 | /* xxx1xxxx DATA */ | |
616 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); | |
617 | ||
618 | /* x11xxxxx unused */ | |
619 | /* 1xxxxxxx MAN */ | |
620 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); | |
621 | } | |
622 | ||
623 | /** Clean range and sign. */ | |
624 | static void clean_rs_v(struct dev_context *devc) | |
625 | { | |
626 | devc->value = 0.0; | |
627 | devc->scale = 1.0; | |
628 | } | |
629 | ||
630 | /** Clean current type, measured variable, range and sign. */ | |
631 | static void clean_ctmv_rs_v(struct dev_context *devc) | |
632 | { | |
633 | devc->mq = 0; | |
634 | devc->unit = 0; | |
635 | devc->mqflags = 0; | |
636 | devc->scale1000 = 0; | |
637 | devc->vmains_29S = FALSE; | |
638 | clean_rs_v(devc); | |
639 | } | |
640 | ||
641 | /** Send prepared value. */ | |
642 | static void send_value(struct sr_dev_inst *sdi) | |
643 | { | |
644 | struct dev_context *devc; | |
645 | struct sr_datafeed_analog analog; | |
646 | struct sr_datafeed_packet packet; | |
647 | ||
648 | devc = sdi->priv; | |
649 | ||
650 | memset(&analog, 0, sizeof(analog)); | |
ba7dd8bb | 651 | analog.channels = sdi->channels; |
f5792417 MH |
652 | analog.num_samples = 1; |
653 | analog.mq = devc->mq; | |
654 | analog.unit = devc->unit; | |
655 | analog.mqflags = devc->mqflags; | |
656 | analog.data = &devc->value; | |
657 | ||
658 | memset(&packet, 0, sizeof(packet)); | |
659 | packet.type = SR_DF_ANALOG; | |
660 | packet.payload = &analog; | |
661 | sr_session_send(devc->cb_data, &packet); | |
662 | ||
663 | devc->num_samples++; | |
664 | } | |
665 | ||
6392d599 | 666 | /** Process 6-byte data message, Metrahit 1x/2x send mode. */ |
f5792417 MH |
667 | static void process_msg_dta_6(struct sr_dev_inst *sdi) |
668 | { | |
669 | struct dev_context *devc; | |
670 | int cnt; | |
fc348b77 | 671 | uint8_t dgt; |
f5792417 MH |
672 | |
673 | devc = sdi->priv; | |
674 | clean_rs_v(devc); | |
675 | ||
676 | /* Byte 0, range and sign */ | |
873e0c12 | 677 | if (devc->model <= METRAHIT_16X) |
f5792417 | 678 | decode_rs_16(bc(devc->buf[0]), devc); |
873e0c12 | 679 | else if (devc->model < METRAHIT_2X) |
f5792417 | 680 | decode_rs_18(bc(devc->buf[0]), devc); |
c90beca7 | 681 | else { |
f5792417 | 682 | decode_rs_2x(bc(devc->buf[0]), devc); |
c90beca7 MH |
683 | devc->scale *= 10; /* Compensate for format having only 5 digits, decode_rs_2x() assumes 6. */ |
684 | } | |
f5792417 MH |
685 | |
686 | /* Bytes 1-5, digits (ls first). */ | |
687 | for (cnt = 0; cnt < 5; cnt++) { | |
688 | dgt = bc(devc->buf[1 + cnt]); | |
689 | if (dgt >= 10) { | |
690 | /* 10 Overload; on model <= 16X also 11 possible. */ | |
691 | devc->value = NAN; | |
692 | devc->scale = 1.0; | |
693 | break; | |
694 | } | |
695 | devc->value += pow(10.0, cnt) * dgt; | |
696 | } | |
6392d599 | 697 | |
c90beca7 | 698 | sr_spew("process_msg_dta_6() value=%f scale=%f scale1000=%d", |
f5792417 MH |
699 | devc->value, devc->scale, devc->scale1000); |
700 | if (devc->value != NAN) | |
701 | devc->value *= devc->scale * pow(1000.0, devc->scale1000); | |
702 | ||
703 | /* Create and send packet. */ | |
704 | send_value(sdi); | |
705 | } | |
706 | ||
707 | /** Process 5-byte info message, Metrahit 1x/2x. */ | |
708 | static void process_msg_inf_5(struct sr_dev_inst *sdi) | |
709 | { | |
710 | struct dev_context *devc; | |
711 | enum model model; | |
712 | ||
713 | devc = sdi->priv; | |
714 | ||
715 | clean_ctmv_rs_v(devc); | |
716 | ||
717 | /* Process byte 0 */ | |
873e0c12 | 718 | model = gmc_decode_model_sm(bc(devc->buf[0])); |
f5792417 MH |
719 | if (model != devc->model) { |
720 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
873e0c12 | 721 | gmc_model_str(devc->model), gmc_model_str(model)); |
f5792417 MH |
722 | } |
723 | ||
724 | /* Process bytes 1-4 */ | |
873e0c12 | 725 | if (devc->model <= METRAHIT_16X) { |
f5792417 MH |
726 | decode_ctmv_16(bc(devc->buf[1]), devc); |
727 | decode_spc_16(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
728 | decode_rs_16(bc(devc->buf[4]), devc); | |
873e0c12 | 729 | } else if (devc->model <= METRAHIT_18S) { |
f5792417 MH |
730 | decode_ctmv_18(bc(devc->buf[1]), devc); |
731 | decode_spc_18(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
732 | decode_rs_18(bc(devc->buf[4]), devc); | |
fc348b77 | 733 | } else { /* Must be Metrahit 2x */ |
f5792417 MH |
734 | decode_ctmv_2x(bc(devc->buf[1]), devc); |
735 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
736 | decode_rs_2x(bc(devc->buf[4]), devc); | |
737 | } | |
738 | } | |
739 | ||
740 | /** Process 10-byte info/data message, Metrahit 15+. */ | |
741 | static void process_msg_inf_10(struct sr_dev_inst *sdi) | |
742 | { | |
743 | struct dev_context *devc; | |
744 | int cnt; | |
fc348b77 | 745 | uint8_t dgt; |
f5792417 MH |
746 | |
747 | devc = sdi->priv; | |
748 | ||
749 | process_msg_inf_5(sdi); | |
750 | ||
751 | /* Now decode numbers */ | |
752 | for (cnt = 0; cnt < 5; cnt++) { | |
753 | dgt = bc(devc->buf[5 + cnt]); | |
c90beca7 MH |
754 | if (dgt == 11) { /* Empty digit */ |
755 | dgt = 0; | |
756 | } | |
757 | else if (dgt >= 12) { /* Overload */ | |
f5792417 MH |
758 | devc->value = NAN; |
759 | devc->scale = 1.0; | |
760 | break; | |
761 | } | |
762 | devc->value += pow(10.0, cnt) * dgt; | |
763 | } | |
764 | sr_spew("process_msg_inf_10() value=%f scale=%f scalet=%d", | |
c90beca7 | 765 | devc->value, devc->scale, devc->scale1000); |
f5792417 MH |
766 | |
767 | if (devc->value != NAN) | |
768 | devc->value *= devc->scale * pow(1000.0, devc->scale1000); | |
769 | ||
770 | /* Create and send packet. */ | |
771 | send_value(sdi); | |
772 | } | |
773 | ||
774 | /** Decode send interval (Metrahit 2x only). */ | |
fc348b77 | 775 | static const char *decode_send_interval(uint8_t si) |
f5792417 MH |
776 | { |
777 | switch (si) { | |
fc348b77 UH |
778 | case 0x00: |
779 | return "0.05"; | |
780 | case 0x01: | |
781 | return "0.1"; | |
782 | case 0x02: | |
783 | return "0.2"; | |
784 | case 0x03: | |
785 | return "0.5"; | |
786 | case 0x04: | |
787 | return "00:01"; | |
788 | case 0x05: | |
789 | return "00:02"; | |
790 | case 0x06: | |
791 | return "00:05"; | |
792 | case 0x07: | |
793 | return "00:10"; | |
794 | case 0x08: | |
795 | return "00:20"; | |
796 | case 0x09: | |
797 | return "00:30"; | |
798 | case 0x0a: | |
799 | return "01:00"; | |
800 | case 0x0b: | |
801 | return "02:00"; | |
802 | case 0x0c: | |
803 | return "05:00"; | |
804 | case 0x0d: | |
805 | return "10:00"; | |
806 | case 0x0e: | |
807 | return "----"; | |
808 | case 0x0f: | |
809 | return "data"; | |
810 | default: | |
811 | return "Unknown value"; | |
f5792417 MH |
812 | } |
813 | } | |
814 | ||
815 | /** Process 13-byte info/data message, Metrahit 2x. */ | |
816 | static void process_msg_inf_13(struct sr_dev_inst *sdi) | |
817 | { | |
818 | struct dev_context *devc; | |
819 | enum model model; | |
820 | int cnt; | |
fc348b77 | 821 | uint8_t dgt; |
f5792417 MH |
822 | |
823 | devc = sdi->priv; | |
824 | ||
825 | clean_ctmv_rs_v(devc); | |
826 | ||
827 | /* Byte 0, model. */ | |
873e0c12 | 828 | model = gmc_decode_model_sm(bc(devc->buf[0])); |
f5792417 MH |
829 | if (model != devc->model) { |
830 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
873e0c12 | 831 | gmc_model_str(devc->model), gmc_model_str(model)); |
f5792417 MH |
832 | } |
833 | ||
834 | /* Bytes 1-4, 11. */ | |
835 | decode_ctmv_2x(bc(devc->buf[1]) | (bc(devc->buf[11]) << 4), devc); | |
836 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
837 | decode_rs_2x(bc(devc->buf[4]), devc); | |
838 | ||
839 | /* Bytes 5-10, digits (ls first). */ | |
840 | for (cnt = 0; cnt < 6; cnt++) { | |
841 | dgt = bc(devc->buf[5 + cnt]); | |
842 | if (dgt == 10) { /* Overload */ | |
843 | devc->value = NAN; | |
844 | devc->scale = 1.0; | |
845 | break; | |
846 | } | |
847 | devc->value += pow(10.0, cnt) * dgt; | |
848 | } | |
849 | sr_spew("process_msg_inf_13() value=%f scale=%f scale1000=%d mq=%d " | |
850 | "unit=%d mqflags=0x%02llx", devc->value, devc->scale, | |
851 | devc->scale1000, devc->mq, devc->unit, devc->mqflags); | |
852 | if (devc->value != NAN) | |
853 | devc->value *= devc->scale * pow(1000.0, devc->scale1000); | |
854 | ||
855 | /* Byte 12, Send Interval */ | |
856 | sr_spew("Send interval: %s", decode_send_interval(bc(devc->buf[12]))); | |
857 | ||
858 | /* Create and send packet. */ | |
859 | send_value(sdi); | |
860 | } | |
861 | ||
c90beca7 MH |
862 | /** Dump contents of 14-byte message. |
863 | * @param buf Pointer to array of 14 data bytes. | |
864 | * @param[in] raw Write only data bytes, no interpretation. | |
865 | */ | |
866 | void dump_msg14(guchar* buf, gboolean raw) | |
867 | { | |
868 | if (!buf) | |
869 | return; | |
870 | ||
871 | if (raw) | |
872 | sr_spew("msg14: 0x %02x %02x %02x %02x %02x %02x %02x %02x " | |
873 | "%02x %02x %02x %02x %02x %02x", | |
874 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], | |
875 | buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], | |
876 | buf[13]); | |
877 | else | |
878 | sr_spew("msg14: 0x a=%d c1=%02x c2=%02x cmd=%02x dta=%02x " | |
879 | "%02x %02x %02x %02x %02x %02x %02x %02x chs=%02x", | |
880 | buf[1] == 0x2b?buf[0] >> 2:buf[0] % 0x0f, buf[1], buf[2], buf[3], buf[4], buf[5], | |
881 | buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], | |
882 | buf[12], buf[13]); | |
883 | } | |
884 | ||
885 | /** Calc checksum for 14 byte message type. | |
886 | * | |
887 | * @param[in] dta Pointer to array of 13 data bytes. | |
888 | * @return Checksum. | |
889 | */ | |
890 | static guchar calc_chksum_14(guchar* dta) | |
891 | { | |
892 | guchar cnt, chs; | |
893 | ||
894 | for (chs = 0, cnt = 0; cnt < 13; cnt++) | |
895 | chs += dta[cnt]; | |
896 | ||
897 | return (64 - chs) & MASK_6BITS; | |
898 | } | |
899 | ||
900 | /** Check 14-byte message, Metrahit 2x. */ | |
901 | static int chk_msg14(struct sr_dev_inst *sdi) | |
902 | { | |
903 | struct dev_context *devc; | |
904 | int retc; | |
905 | gboolean isreq; /* Message is request to multimeter (otherwise response) */ | |
906 | uint8_t addr; /* Adaptor address */ | |
907 | ||
908 | retc = SR_OK; | |
909 | ||
910 | /* Check parameters and message */ | |
911 | if (!sdi || !(devc = sdi->priv)) | |
912 | return SR_ERR_ARG; | |
913 | ||
914 | if (devc->buflen != 14) { | |
915 | sr_err("process_msg_14(): Msg len 14 expected!"); | |
916 | return SR_ERR_ARG; | |
917 | } | |
918 | ||
919 | isreq = devc->buf[1] == 0x2b; | |
920 | if (isreq) | |
921 | addr = devc->buf[0] >> 2; | |
922 | else | |
923 | addr = devc->buf[0] & 0x0f; | |
924 | ||
925 | if ((devc->addr != addr) && !(isreq && (addr == 0))) { | |
926 | sr_err("process_msg_14(): Address mismatch, msg for other device!"); | |
927 | retc = SR_ERR_ARG; | |
928 | } | |
929 | ||
930 | if (devc->buf[1] == 0) { /* Error msg from device! */ | |
931 | retc = SR_ERR_ARG; | |
932 | switch (devc->buf[2]) { | |
933 | case 1: /* Not used */ | |
934 | sr_err("Device: Illegal error code!"); | |
935 | break; | |
936 | case 2: /* Incorrect check sum of received block */ | |
937 | sr_err("Device: Incorrect checksum in cmd!"); | |
938 | break; | |
939 | case 3: /* Incorrect length of received block */ | |
940 | sr_err("Device: Incorrect block length in cmd!"); | |
941 | break; | |
942 | case 4: /* Incorrect 2nd or 3rd byte */ | |
943 | sr_err("Device: Incorrect byte 2 or 3 in cmd!"); | |
944 | break; | |
945 | case 5: /* Parameter out of range */ | |
946 | sr_err("Device: Parameter out of range!"); | |
947 | break; | |
948 | default: | |
949 | sr_err("Device: Unknown error code!"); | |
950 | } | |
951 | retc = SR_ERR_ARG; | |
952 | } | |
953 | else if (!isreq && ((devc->buf[1] != 0x27) || (devc->buf[2] != 0x3f))) { | |
954 | sr_err("process_msg_14(): byte 1/2 unexpected!"); | |
955 | retc = SR_ERR_ARG; | |
956 | } | |
957 | ||
958 | if (calc_chksum_14(devc->buf) != devc->buf[13]) { | |
959 | sr_err("process_msg_14(): Invalid checksum!"); | |
960 | retc = SR_ERR_ARG; | |
961 | } | |
962 | ||
963 | if (retc != SR_OK) | |
964 | dump_msg14(devc->buf, TRUE); | |
965 | ||
966 | return retc; | |
967 | } | |
968 | ||
969 | /** Check 14-byte message, Metrahit 2x. */ | |
970 | SR_PRIV int process_msg14(struct sr_dev_inst *sdi) | |
971 | { | |
972 | struct dev_context *devc; | |
973 | int retc; | |
974 | uint8_t addr; | |
975 | uint8_t cnt, dgt; | |
976 | ||
977 | if ((retc = chk_msg14(sdi)) != SR_OK) | |
978 | return retc; | |
979 | ||
980 | devc = sdi->priv; | |
981 | ||
982 | clean_ctmv_rs_v(devc); | |
983 | addr = devc->buf[0] & MASK_6BITS; | |
984 | if (addr != devc->addr) | |
985 | sr_info("Device address mismatch %d/%d!", addr, devc->addr); | |
986 | ||
987 | switch (devc->buf[3]) { /* That's the command this reply is for */ | |
988 | /* 0 cannot occur, the respective message is not a 14-byte message */ | |
989 | case 1: /* Read first free and occupied address */ | |
990 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
991 | break; | |
992 | case 2: /* Clear all RAM in multimeter */ | |
993 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
994 | break; | |
995 | case 3: /* Read firmware version and status */ | |
996 | sr_spew("Cmd 3, Read firmware and status", devc->buf[3]); | |
997 | switch (devc->cmd_idx) { | |
998 | case 0: | |
999 | devc->fw_ver_maj = devc->buf[5]; | |
1000 | devc->fw_ver_min = devc->buf[4]; | |
1001 | sr_spew("Firmware version %d.%d", (int)devc->fw_ver_maj, (int)devc->fw_ver_min); | |
1002 | sr_spew("Rotary Switch Position (1..10): %d", (int)devc->buf[6]); | |
1003 | /** Docs say values 0..9, but that's not true */ | |
1004 | sr_spew("Measurement Function: %d ", (int)devc->buf[7]); | |
1005 | decode_ctmv_2x(devc->buf[7], devc); | |
1006 | sr_spew("Range: 0x%x", devc->buf[8]); | |
1007 | decode_rs_2x_TR2(devc->buf[8] & 0x0f, devc); /* Docs wrong, uses conversion table TR_2! */ | |
1008 | devc->autorng = (devc->buf[8] & 0x20) == 0; | |
1009 | // TODO 9, 10: 29S special functions | |
1010 | devc->ubatt = 0.1 * (float)devc->buf[11]; | |
1011 | devc->model = gmc_decode_model_bd(devc->buf[12]); | |
1012 | sr_spew("Model=%s, battery voltage=%2.1f V", gmc_model_str(devc->model), (double)devc->ubatt); | |
1013 | break; | |
1014 | case 1: | |
1015 | sr_spew("Internal version %d.%d", (int)devc->buf[5], (int)devc->buf[4]); | |
1016 | sr_spew("Comm mode: 0x%x", (int)devc->buf[6]); | |
1017 | sr_spew("Block cnt%%64: %d", (int)devc->buf[7]); | |
1018 | sr_spew("drpCi: %d drpCh: %d", (int)devc->buf[8], (int)devc->buf[9]); | |
1019 | // Semantics undocumented. Possibly Metrahit 29S dropouts stuff? | |
1020 | break; | |
1021 | default: | |
1022 | sr_spew("Cmd 3: Unknown cmd_idx=%d", devc->cmd_idx); | |
1023 | break; | |
1024 | } | |
1025 | break; | |
1026 | case 4: /* Set real time, date, sample rate, trigger, ... */ | |
1027 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1028 | break; | |
1029 | case 5: /* Read real time, date, sample rate, trigger... */ | |
1030 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1031 | break; | |
1032 | case 6: /* Set modes or power off */ | |
1033 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1034 | break; | |
1035 | case 7: /* Set measurement function, range, autom/man. */ | |
1036 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1037 | break; | |
1038 | case 8: /* Get one measurement value */ | |
1039 | sr_spew("Cmd 8, get one measurement value"); | |
1040 | sr_spew("Measurement Function: %d ", (int)devc->buf[5]); | |
1041 | decode_ctmv_2x(devc->buf[5], devc); | |
1042 | if (!(devc->buf[6] & 0x10)) /* If bit4=0, old data. */ | |
1043 | return SR_OK; | |
1044 | ||
1045 | decode_rs_2x_TR2(devc->buf[6] & 0x0f, devc); // The docs say conversion table TR_3, but that does not work | |
1046 | setmqf(devc, SR_MQFLAG_AUTORANGE, devc->autorng); | |
1047 | /* 6 digits */ | |
1048 | for (cnt = 0; cnt < 6; cnt++) { | |
1049 | dgt = bc(devc->buf[7 + cnt]); | |
1050 | if (dgt == 10) { /* Overload */ | |
1051 | devc->value = NAN; | |
1052 | devc->scale = 1.0; | |
1053 | break; | |
1054 | } | |
1055 | else if (dgt == 13) { /* FUSE */ | |
1056 | sr_err("FUSE!"); | |
1057 | } | |
1058 | else if (dgt == 14) { /* Function recognition mode, OPEN */ | |
1059 | sr_info("Function recognition mode, OPEN!"); | |
1060 | devc->value = NAN; | |
1061 | devc->scale = 1.0; | |
1062 | break; | |
1063 | } | |
1064 | devc->value += pow(10.0, cnt) * dgt; | |
1065 | } | |
1066 | sr_spew("process_msg14() value=%f scale=%f scale1000=%d mq=%d " | |
1067 | "unit=%d mqflags=0x%02llx", devc->value, devc->scale, | |
1068 | devc->scale1000, devc->mq, devc->unit, devc->mqflags); | |
1069 | if (devc->value != NAN) | |
1070 | devc->value *= devc->scale * pow(1000.0, devc->scale1000); | |
1071 | ||
1072 | send_value(sdi); | |
1073 | ||
1074 | break; | |
1075 | default: | |
1076 | sr_spew("Unknown cmd %d!", devc->buf[3]); | |
1077 | break; | |
1078 | } | |
1079 | ||
1080 | return SR_OK; | |
1081 | } | |
1082 | ||
1083 | /** Data reception callback function. */ | |
7b4edcb6 MH |
1084 | SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data) |
1085 | { | |
f5792417 | 1086 | struct sr_dev_inst *sdi; |
7b4edcb6 | 1087 | struct dev_context *devc; |
f5792417 | 1088 | struct sr_serial_dev_inst *serial; |
fc348b77 | 1089 | uint8_t buf, msgt; |
f5792417 | 1090 | int len; |
fc348b77 | 1091 | gdouble elapsed_s; |
7b4edcb6 MH |
1092 | |
1093 | (void)fd; | |
1094 | ||
1095 | if (!(sdi = cb_data)) | |
1096 | return TRUE; | |
1097 | ||
1098 | if (!(devc = sdi->priv)) | |
1099 | return TRUE; | |
1100 | ||
f5792417 MH |
1101 | serial = sdi->conn; |
1102 | ||
1103 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
1104 | while (GMC_BUFSIZE - devc->buflen - 1 > 0) { | |
1105 | len = serial_read(serial, devc->buf + devc->buflen, 1); | |
1106 | if (len < 1) | |
1107 | break; | |
1108 | buf = *(devc->buf + devc->buflen); | |
1109 | sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MSGC_MASK); | |
1110 | devc->buflen += len; | |
1111 | if (!devc->settings_ok) { | |
fc348b77 UH |
1112 | /* |
1113 | * If no device type/settings record processed | |
1114 | * yet, wait for one. | |
1115 | */ | |
f5792417 MH |
1116 | if ((devc->buf[0] & MSGID_MASK) != MSGID_INF) { |
1117 | devc->buflen = 0; | |
1118 | continue; | |
1119 | } | |
1120 | devc->settings_ok = TRUE; | |
1121 | } | |
1122 | ||
1123 | msgt = devc->buf[0] & MSGID_MASK; | |
1124 | switch (msgt) { | |
1125 | case MSGID_INF: | |
1126 | if (devc->buflen == 13) { | |
1127 | process_msg_inf_13(sdi); | |
1128 | devc->buflen = 0; | |
1129 | continue; | |
fc348b77 | 1130 | } else if ((devc->buflen == 10) && |
c90beca7 | 1131 | (devc->model <= METRAHIT_18S)) { |
f5792417 MH |
1132 | process_msg_inf_10(sdi); |
1133 | devc->buflen = 0; | |
1134 | continue; | |
1135 | } | |
1136 | else if ((devc->buflen >= 5) && | |
c90beca7 MH |
1137 | (devc->buf[devc->buflen - 1] & |
1138 | MSGID_MASK) != MSGID_DATA) { | |
fc348b77 UH |
1139 | /* |
1140 | * Char just received is beginning | |
1141 | * of next message. | |
1142 | */ | |
f5792417 | 1143 | process_msg_inf_5(sdi); |
fc348b77 | 1144 | devc->buf[0] = |
c90beca7 | 1145 | devc->buf[devc->buflen - 1]; |
f5792417 MH |
1146 | devc->buflen = 1; |
1147 | continue; | |
1148 | } | |
1149 | break; | |
1150 | case MSGID_DTA: | |
1151 | case MSGID_D10: | |
1152 | if (devc->buflen == 6) { | |
1153 | process_msg_dta_6(sdi); | |
1154 | devc->buflen = 0; | |
1155 | } | |
1156 | break; | |
1157 | case MSGID_DATA: | |
1158 | sr_err("Comm error, unexpected data byte!"); | |
1159 | devc->buflen = 0; | |
1160 | break; | |
1161 | } | |
1162 | } | |
1163 | } | |
1164 | ||
a84f6ad3 | 1165 | /* If number of samples or time limit reached, stop acquisition. */ |
fc348b77 | 1166 | if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) |
f5792417 | 1167 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
f5792417 MH |
1168 | |
1169 | if (devc->limit_msec) { | |
fc348b77 UH |
1170 | elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); |
1171 | if ((elapsed_s * 1000) >= devc->limit_msec) | |
f5792417 | 1172 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
7b4edcb6 MH |
1173 | } |
1174 | ||
1175 | return TRUE; | |
1176 | } | |
f5792417 | 1177 | |
c90beca7 MH |
1178 | SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data) |
1179 | { | |
1180 | struct sr_dev_inst *sdi; | |
1181 | struct dev_context *devc; | |
1182 | struct sr_serial_dev_inst *serial; | |
1183 | uint8_t buf; | |
1184 | int len; | |
1185 | gdouble elapsed_s; | |
1186 | ||
1187 | (void)fd; | |
1188 | ||
1189 | if (!(sdi = cb_data)) | |
1190 | return TRUE; | |
1191 | ||
1192 | if (!(devc = sdi->priv)) | |
1193 | return TRUE; | |
1194 | ||
1195 | serial = sdi->conn; | |
1196 | ||
1197 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
1198 | while (GMC_BUFSIZE - devc->buflen - 1 > 0) { | |
1199 | len = serial_read(serial, devc->buf + devc->buflen, 1); | |
1200 | if (len < 1) | |
1201 | break; | |
1202 | buf = *(devc->buf + devc->buflen); | |
1203 | sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MASK_6BITS); | |
1204 | devc->buf[devc->buflen] &= MASK_6BITS; | |
1205 | devc->buflen += len; | |
1206 | ||
1207 | if (devc->buflen == 14) { | |
1208 | devc->response_pending = FALSE; | |
1209 | sr_spew("gmc_mh_2x_receive_data processing msg"); | |
1210 | process_msg14(sdi); | |
1211 | devc->buflen = 0; | |
1212 | } | |
1213 | } | |
1214 | } | |
1215 | ||
a84f6ad3 | 1216 | /* If number of samples or time limit reached, stop acquisition. */ |
c90beca7 MH |
1217 | if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) |
1218 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
1219 | ||
1220 | if (devc->limit_msec) { | |
1221 | elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); | |
1222 | if ((elapsed_s * 1000) >= devc->limit_msec) | |
1223 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
1224 | } | |
1225 | ||
1226 | /* Request next data set, if required */ | |
1227 | if (sdi->status == SR_ST_ACTIVE) { | |
1228 | if (devc->response_pending) { | |
1229 | gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
1230 | if (elapsed_us > 1*1000*1000) /* Timeout! */ | |
1231 | devc->response_pending = FALSE; | |
1232 | } | |
1233 | if (!devc->response_pending) { | |
1234 | devc->cmd_seq++; | |
1235 | if (devc->cmd_seq % 10 == 0) { | |
1236 | if (req_stat14(sdi, FALSE) != SR_OK) | |
1237 | return FALSE; | |
1238 | } | |
1239 | else if (req_meas14(sdi) != SR_OK) | |
1240 | return FALSE; | |
1241 | } | |
1242 | } | |
1243 | ||
1244 | return TRUE; | |
1245 | } | |
1246 | ||
1247 | /** Create 14 (42) byte command for Metrahit 2x multimeter in bidir mode. | |
1248 | * | |
1249 | * Actually creates 42 bytes due to the encoding method used. | |
1250 | * @param[in] addr Device address (0=adapter, 1..15 multimeter; for byte 0). | |
1251 | * @param[in] func Function code (byte 3). | |
1252 | * @param[in] params Further parameters (9 bytes) | |
1253 | * @param[out] buf Buffer to create msg in (42 bytes). | |
1254 | */ | |
1255 | void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf) | |
1256 | { | |
1257 | uint8_t dta[14]; /* Unencoded message */ | |
1258 | int cnt; | |
1259 | ||
1260 | if (!params || !buf) | |
1261 | return; | |
1262 | ||
1263 | /* 0: Address */ | |
1264 | dta[0] = ((addr << 2) | 0x03) & MASK_6BITS; | |
1265 | ||
1266 | /* 1-3: Set command header */ | |
1267 | dta[1] = 0x2b; | |
1268 | dta[2] = 0x3f; | |
1269 | dta[3] = func; | |
1270 | ||
1271 | /* 4-12: Copy further parameters */ | |
1272 | for (cnt = 0; cnt < 9; cnt++) | |
1273 | dta[cnt+4] = (params[cnt] & MASK_6BITS); | |
1274 | ||
1275 | /* 13: Checksum (b complement) */ | |
1276 | dta[13] = calc_chksum_14(dta); | |
1277 | ||
1278 | /* The whole message is packed into 3 bytes per byte now (lower 6 bits only) the most | |
1279 | * peculiar way I have ever seen. Possibly to improve IR communication? */ | |
1280 | for (cnt = 0; cnt < 14; cnt++) { | |
1281 | buf[3*cnt] = (dta[cnt] & 0x01 ? 0x0f : 0) | (dta[cnt] & 0x02 ? 0xf0 : 0); | |
1282 | buf[3*cnt + 1] = (dta[cnt] & 0x04 ? 0x0f : 0) | (dta[cnt] & 0x08 ? 0xf0 : 0); | |
1283 | buf[3*cnt + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0); | |
1284 | } | |
1285 | } | |
1286 | ||
1287 | /** Request one measurement from 2x multimeter (msg 8). | |
1288 | * | |
1289 | */ | |
1290 | int req_meas14(const struct sr_dev_inst *sdi) | |
1291 | { | |
1292 | struct dev_context *devc; | |
1293 | struct sr_serial_dev_inst *serial; | |
1294 | uint8_t params[9]; | |
1295 | uint8_t msg[42]; | |
1296 | ||
1297 | if (!sdi || !(devc = sdi->priv) || !(serial = sdi->conn)) | |
1298 | return SR_ERR; | |
1299 | ||
1300 | memset(params, 0, sizeof(params)); | |
1301 | params[0] = 0; | |
1302 | devc->cmd_idx = 0; | |
1303 | create_cmd_14(devc->addr, 8, params, msg); | |
1304 | devc->req_sent_at = g_get_monotonic_time(); | |
1305 | if (serial_write(serial, msg, sizeof(msg)) == -1) { | |
1306 | return SR_ERR; | |
1307 | } | |
1308 | ||
1309 | devc->response_pending = TRUE; | |
1310 | ||
1311 | return SR_OK; | |
1312 | } | |
1313 | ||
1314 | /** Request status from 2x multimeter (msg 3). | |
1315 | * @param[in] power_on Try to power on powered off multimeter by sending additional messages. | |
1316 | */ | |
1317 | int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on) | |
1318 | { | |
1319 | struct dev_context *devc; | |
1320 | struct sr_serial_dev_inst *serial; | |
1321 | uint8_t params[9]; | |
1322 | uint8_t msg[42]; | |
1323 | ||
1324 | if (!sdi || !(devc = sdi->priv) || !(serial = sdi->conn)) | |
1325 | return SR_ERR; | |
1326 | ||
1327 | memset(params, 0, sizeof(params)); | |
1328 | params[0] = 0; | |
1329 | devc->cmd_idx = 0; | |
1330 | create_cmd_14(devc->addr, 3, params, msg); | |
1331 | ||
1332 | if (power_on) { | |
1333 | sr_info("Write some data and wait 3s to turn on powered off device..."); | |
fadd0707 | 1334 | if (serial_write(serial, msg, sizeof(msg)) < 0) |
c90beca7 | 1335 | return SR_ERR; |
fadd0707 MH |
1336 | g_usleep(1*1000*1000); |
1337 | if (serial_write(serial, msg, sizeof(msg)) < 0) | |
1338 | return SR_ERR; | |
1339 | g_usleep(1*1000*1000); | |
1340 | if (serial_write(serial, msg, sizeof(msg)) < 0) | |
1341 | return SR_ERR; | |
1342 | g_usleep(1*1000*1000); | |
c90beca7 MH |
1343 | serial_flush(serial); |
1344 | } | |
1345 | ||
1346 | /* Write message and wait for reply */ | |
1347 | devc->req_sent_at = g_get_monotonic_time(); | |
1348 | if (serial_write(serial, msg, sizeof(msg)) == -1) { | |
1349 | return SR_ERR; | |
1350 | } | |
1351 | ||
1352 | devc->response_pending = TRUE; | |
1353 | ||
1354 | return SR_OK; | |
1355 | } | |
1356 | ||
1357 | /** Decode model in "send mode". | |
1358 | * | |
1359 | * @param[in] mcode Model code. | |
1360 | * @return Model code. | |
1361 | */ | |
873e0c12 | 1362 | SR_PRIV int gmc_decode_model_sm(uint8_t mcode) |
f5792417 MH |
1363 | { |
1364 | if (mcode > 0xf) { | |
1365 | sr_err("decode_model(%d): Model code 0..15 expected!", mcode); | |
873e0c12 | 1366 | return METRAHIT_NONE; |
f5792417 MH |
1367 | } |
1368 | ||
1369 | switch(mcode) { | |
fc348b77 | 1370 | case 0x04: /* 0100b */ |
873e0c12 | 1371 | return METRAHIT_12S; |
fc348b77 | 1372 | case 0x08: /* 1000b */ |
873e0c12 | 1373 | return METRAHIT_13S14A; |
fc348b77 | 1374 | case 0x09: /* 1001b */ |
873e0c12 | 1375 | return METRAHIT_14S; |
fc348b77 | 1376 | case 0x0A: /* 1010b */ |
873e0c12 | 1377 | return METRAHIT_15S; |
fc348b77 | 1378 | case 0x0B: /* 1011b */ |
873e0c12 | 1379 | return METRAHIT_16S; |
fc348b77 | 1380 | case 0x06: /* 0110b (undocumented by GMC!) */ |
873e0c12 | 1381 | return METRAHIT_16I; |
fc348b77 | 1382 | case 0x0D: /* 1101b */ |
873e0c12 | 1383 | return METRAHIT_18S; |
fc348b77 | 1384 | case 0x02: /* 0010b */ |
873e0c12 | 1385 | return METRAHIT_22SM; |
fc348b77 | 1386 | case 0x03: /* 0011b */ |
873e0c12 | 1387 | return METRAHIT_23S; |
fc348b77 | 1388 | case 0x0f: /* 1111b */ |
873e0c12 | 1389 | return METRAHIT_24S; |
fc348b77 | 1390 | case 0x05: /* 0101b */ |
c90beca7 | 1391 | return METRAHIT_25S; |
fc348b77 | 1392 | case 0x01: /* 0001b */ |
c90beca7 | 1393 | return METRAHIT_26SM; |
fc348b77 | 1394 | case 0x0c: /* 1100b */ |
873e0c12 | 1395 | return METRAHIT_28S; |
fc348b77 | 1396 | case 0x0e: /* 1110b */ |
873e0c12 | 1397 | return METRAHIT_29S; |
f5792417 MH |
1398 | default: |
1399 | sr_err("Unknown model code %d!", mcode); | |
873e0c12 | 1400 | return METRAHIT_NONE; |
f5792417 MH |
1401 | } |
1402 | } | |
1403 | ||
6392d599 | 1404 | /** Convert GMC model code in bidirectional mode to sigrok-internal one. |
fc348b77 | 1405 | * |
6392d599 | 1406 | * @param[in] mcode Model code. |
fc348b77 | 1407 | * |
6392d599 | 1408 | * @return Model code. |
fc348b77 | 1409 | */ |
6392d599 | 1410 | SR_PRIV int gmc_decode_model_bd(uint8_t mcode) |
f5792417 | 1411 | { |
6392d599 | 1412 | switch (mcode & 0x1f) { |
fc348b77 | 1413 | case 2: |
c90beca7 MH |
1414 | if (mcode & 0x20) |
1415 | return METRAHIT_22M; | |
1416 | else | |
1417 | return METRAHIT_22S; | |
fc348b77 | 1418 | case 3: |
873e0c12 | 1419 | return METRAHIT_23S; |
fc348b77 | 1420 | case 4: |
873e0c12 | 1421 | return METRAHIT_24S; |
fc348b77 | 1422 | case 5: |
6392d599 | 1423 | return METRAHIT_25S; |
fc348b77 | 1424 | case 1: |
c90beca7 MH |
1425 | if (mcode & 0x20) |
1426 | return METRAHIT_26M; | |
1427 | else | |
1428 | return METRAHIT_26S; | |
fc348b77 | 1429 | case 12: |
873e0c12 | 1430 | return METRAHIT_28S; |
fc348b77 | 1431 | case 14: |
873e0c12 | 1432 | return METRAHIT_29S; |
f5792417 MH |
1433 | default: |
1434 | sr_err("Unknown model code %d!", mcode); | |
873e0c12 | 1435 | return METRAHIT_NONE; |
f5792417 MH |
1436 | } |
1437 | } | |
1438 | ||
6392d599 MH |
1439 | /** Convert sigrok-internal model code to string. |
1440 | * | |
1441 | * @param[in] mcode Model code. | |
1442 | * | |
1443 | * @return Model code string. | |
1444 | */ | |
873e0c12 | 1445 | SR_PRIV const char *gmc_model_str(enum model mcode) |
f5792417 MH |
1446 | { |
1447 | switch (mcode) { | |
873e0c12 | 1448 | case METRAHIT_NONE: |
f5792417 | 1449 | return "-uninitialized model variable-"; |
873e0c12 | 1450 | case METRAHIT_12S: |
f5792417 | 1451 | return "METRAHit 12S"; |
873e0c12 | 1452 | case METRAHIT_13S14A: |
f5792417 | 1453 | return "METRAHit 13S/14A"; |
873e0c12 | 1454 | case METRAHIT_14S: |
f5792417 | 1455 | return "METRAHit 14S"; |
873e0c12 | 1456 | case METRAHIT_15S: |
f5792417 | 1457 | return "METRAHit 15S"; |
873e0c12 | 1458 | case METRAHIT_16S: |
f5792417 | 1459 | return "METRAHit 16S"; |
873e0c12 | 1460 | case METRAHIT_16I: |
f5792417 | 1461 | return "METRAHit 16I"; |
873e0c12 | 1462 | case METRAHIT_18S: |
f5792417 | 1463 | return "METRAHit 18S"; |
873e0c12 | 1464 | case METRAHIT_22SM: |
f5792417 | 1465 | return "METRAHit 22S/M"; |
c90beca7 MH |
1466 | case METRAHIT_22S: |
1467 | return "METRAHit 22S"; | |
1468 | case METRAHIT_22M: | |
1469 | return "METRAHit 22M"; | |
873e0c12 | 1470 | case METRAHIT_23S: |
f5792417 | 1471 | return "METRAHit 23S"; |
873e0c12 | 1472 | case METRAHIT_24S: |
f5792417 | 1473 | return "METRAHit 24S"; |
6392d599 MH |
1474 | case METRAHIT_25S: |
1475 | return "METRAHit 25S"; | |
1476 | case METRAHIT_26SM: | |
1477 | return "METRAHit 26S/M"; | |
c90beca7 MH |
1478 | case METRAHIT_26S: |
1479 | return "METRAHit 26S"; | |
1480 | case METRAHIT_26M: | |
1481 | return "METRAHit 26M"; | |
873e0c12 | 1482 | case METRAHIT_28S: |
f5792417 | 1483 | return "METRAHit 28S"; |
873e0c12 | 1484 | case METRAHIT_29S: |
f5792417 MH |
1485 | return "METRAHit 29S"; |
1486 | default: | |
1487 | return "Unknown model code"; | |
1488 | } | |
1489 | } | |
c90beca7 MH |
1490 | |
1491 | ||
1492 | /** @copydoc sr_dev_driver.config_set | |
1493 | */ | |
1494 | SR_PRIV int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
53b4680f | 1495 | const struct sr_channel_group *cg) |
c90beca7 MH |
1496 | { |
1497 | struct dev_context *devc; | |
1498 | uint8_t params[9]; | |
1499 | uint8_t msg[42]; | |
1500 | ||
53b4680f | 1501 | (void)cg; |
c90beca7 MH |
1502 | |
1503 | if (sdi->status != SR_ST_ACTIVE) | |
1504 | return SR_ERR_DEV_CLOSED; | |
1505 | ||
1506 | if (!(devc = sdi->priv)) { | |
1507 | sr_err("sdi->priv was NULL."); | |
1508 | return SR_ERR_BUG; | |
1509 | } | |
1510 | ||
1511 | switch (key) { | |
1512 | case SR_CONF_POWER_OFF: | |
1513 | if (devc->model < METRAHIT_2X) | |
1514 | return SR_ERR_NA; | |
1515 | if (!g_variant_get_boolean(data)) | |
1516 | return SR_ERR; | |
1517 | sr_info("Powering device off."); | |
1518 | ||
1519 | memset(params, 0, sizeof(params)); | |
1520 | params[0] = 5; | |
1521 | params[1] = 5; | |
1522 | create_cmd_14(devc->addr, 6, params, msg); | |
1523 | if (serial_write(sdi->conn, msg, sizeof(msg)) == -1) | |
1524 | return SR_ERR; | |
1525 | else | |
1526 | g_usleep(2000000); /* Wait to ensure transfer before interface switched off. */ | |
1527 | break; | |
1528 | case SR_CONF_LIMIT_MSEC: | |
1529 | if (g_variant_get_uint64(data) == 0) { | |
1530 | sr_err("LIMIT_MSEC can't be 0."); | |
1531 | return SR_ERR; | |
1532 | } | |
1533 | devc->limit_msec = g_variant_get_uint64(data); | |
1534 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
7f7f6a2e | 1535 | devc->limit_msec); |
c90beca7 MH |
1536 | break; |
1537 | case SR_CONF_LIMIT_SAMPLES: | |
1538 | devc->limit_samples = g_variant_get_uint64(data); | |
1539 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
7f7f6a2e | 1540 | devc->limit_samples); |
c90beca7 MH |
1541 | break; |
1542 | default: | |
1543 | return SR_ERR_NA; | |
1544 | } | |
1545 | ||
1546 | return SR_OK; | |
1547 | } |