]>
Commit | Line | Data |
---|---|---|
6bcb3ee8 JH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.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 | ||
20 | #include <stdint.h> | |
21 | #include <string.h> | |
22 | #include <math.h> | |
23 | #include <glib.h> | |
24 | #include "libsigrok.h" | |
25 | #include "libsigrok-internal.h" | |
26 | ||
27 | #define LOG_PREFIX "es51919" | |
28 | ||
29 | struct dev_buffer { | |
30 | /** Total size of the buffer. */ | |
31 | size_t size; | |
32 | /** Amount of data currently in the buffer. */ | |
33 | size_t len; | |
34 | /** Offset where the data starts in the buffer. */ | |
35 | size_t offset; | |
36 | /** Space for the data. */ | |
37 | uint8_t data[]; | |
38 | }; | |
39 | ||
40 | static struct dev_buffer *dev_buffer_new(size_t size) | |
41 | { | |
42 | struct dev_buffer *dbuf; | |
43 | ||
44 | if (!(dbuf = g_try_malloc(sizeof(struct dev_buffer) + size))) { | |
45 | sr_err("Dev buffer malloc failed (size=%zu).", size); | |
46 | return NULL; | |
47 | } | |
48 | ||
49 | dbuf->size = size; | |
50 | dbuf->len = 0; | |
51 | dbuf->offset = 0; | |
52 | ||
53 | return dbuf; | |
54 | } | |
55 | ||
56 | static void dev_buffer_destroy(struct dev_buffer *dbuf) | |
57 | { | |
58 | g_free(dbuf); | |
59 | } | |
60 | ||
61 | static int dev_buffer_fill_serial(struct dev_buffer *dbuf, | |
62 | struct sr_dev_inst *sdi) | |
63 | { | |
64 | struct sr_serial_dev_inst *serial; | |
65 | int len; | |
66 | ||
67 | serial = sdi->conn; | |
68 | ||
69 | /* If we already have data, move it to the beginning of the buffer. */ | |
70 | if (dbuf->len > 0 && dbuf->offset > 0) | |
71 | memmove(dbuf->data, dbuf->data + dbuf->offset, dbuf->len); | |
72 | ||
73 | dbuf->offset = 0; | |
74 | ||
75 | len = dbuf->size - dbuf->len; | |
76 | len = serial_read_nonblocking(serial, dbuf->data + dbuf->len, len); | |
77 | if (len < 0) { | |
78 | sr_err("Serial port read error: %d.", len); | |
79 | return len; | |
80 | } | |
81 | ||
82 | dbuf->len += len; | |
83 | ||
84 | return SR_OK; | |
85 | } | |
86 | ||
87 | static uint8_t *dev_buffer_packet_find(struct dev_buffer *dbuf, | |
88 | gboolean (*packet_valid)(const uint8_t *), | |
89 | size_t packet_size) | |
90 | { | |
91 | size_t offset; | |
92 | ||
93 | while (dbuf->len >= packet_size) { | |
94 | if (packet_valid(dbuf->data + dbuf->offset)) { | |
95 | offset = dbuf->offset; | |
96 | dbuf->offset += packet_size; | |
97 | dbuf->len -= packet_size; | |
98 | return dbuf->data + offset; | |
99 | } | |
100 | dbuf->offset++; | |
101 | dbuf->len--; | |
102 | } | |
103 | ||
104 | return NULL; | |
105 | } | |
106 | ||
107 | struct dev_sample_counter { | |
108 | /** The current number of already received samples. */ | |
109 | uint64_t count; | |
110 | /** The current sampling limit (in number of samples). */ | |
111 | uint64_t limit; | |
112 | }; | |
113 | ||
114 | static void dev_sample_counter_start(struct dev_sample_counter *cnt) | |
115 | { | |
116 | cnt->count = 0; | |
117 | } | |
118 | ||
119 | static void dev_sample_counter_inc(struct dev_sample_counter *cnt) | |
120 | { | |
121 | cnt->count++; | |
122 | } | |
123 | ||
124 | static void dev_sample_limit_set(struct dev_sample_counter *cnt, uint64_t limit) | |
125 | { | |
126 | cnt->limit = limit; | |
127 | } | |
128 | ||
129 | static gboolean dev_sample_limit_reached(struct dev_sample_counter *cnt) | |
130 | { | |
131 | if (cnt->limit && cnt->count >= cnt->limit) { | |
132 | sr_info("Requested sample limit reached."); | |
133 | return TRUE; | |
134 | } | |
135 | ||
136 | return FALSE; | |
137 | } | |
138 | ||
139 | struct dev_time_counter { | |
140 | /** The starting time of current sampling run. */ | |
141 | int64_t starttime; | |
142 | /** The time limit (in milliseconds). */ | |
143 | uint64_t limit; | |
144 | }; | |
145 | ||
146 | static void dev_time_counter_start(struct dev_time_counter *cnt) | |
147 | { | |
148 | cnt->starttime = g_get_monotonic_time(); | |
149 | } | |
150 | ||
151 | static void dev_time_limit_set(struct dev_time_counter *cnt, uint64_t limit) | |
152 | { | |
153 | cnt->limit = limit; | |
154 | } | |
155 | ||
156 | static gboolean dev_time_limit_reached(struct dev_time_counter *cnt) | |
157 | { | |
158 | int64_t time; | |
159 | ||
160 | if (cnt->limit) { | |
161 | time = (g_get_monotonic_time() - cnt->starttime) / 1000; | |
162 | if (time > (int64_t)cnt->limit) { | |
163 | sr_info("Requested time limit reached."); | |
164 | return TRUE; | |
165 | } | |
166 | } | |
167 | ||
168 | return FALSE; | |
169 | } | |
170 | ||
171 | static void serial_conf_get(GSList *options, const char *def_serialcomm, | |
172 | const char **conn, const char **serialcomm) | |
173 | { | |
174 | struct sr_config *src; | |
175 | GSList *l; | |
176 | ||
177 | *conn = *serialcomm = NULL; | |
178 | for (l = options; l; l = l->next) { | |
179 | src = l->data; | |
180 | switch (src->key) { | |
181 | case SR_CONF_CONN: | |
182 | *conn = g_variant_get_string(src->data, NULL); | |
183 | break; | |
184 | case SR_CONF_SERIALCOMM: | |
185 | *serialcomm = g_variant_get_string(src->data, NULL); | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
190 | if (*serialcomm == NULL) | |
191 | *serialcomm = def_serialcomm; | |
192 | } | |
193 | ||
194 | static struct sr_serial_dev_inst *serial_dev_new(GSList *options, | |
195 | const char *def_serialcomm) | |
196 | ||
197 | { | |
198 | const char *conn, *serialcomm; | |
199 | ||
200 | serial_conf_get(options, def_serialcomm, &conn, &serialcomm); | |
201 | ||
202 | if (!conn) | |
203 | return NULL; | |
204 | ||
205 | return sr_serial_dev_inst_new(conn, serialcomm); | |
206 | } | |
207 | ||
208 | static int serial_stream_check_buf(struct sr_serial_dev_inst *serial, | |
209 | uint8_t *buf, size_t buflen, | |
210 | size_t packet_size, | |
211 | packet_valid_callback is_valid, | |
212 | uint64_t timeout_ms, int baudrate) | |
213 | { | |
214 | size_t len, dropped; | |
215 | int ret; | |
216 | ||
217 | if ((ret = serial_open(serial, SERIAL_RDWR)) != SR_OK) | |
218 | return ret; | |
219 | ||
220 | serial_flush(serial); | |
221 | ||
222 | len = buflen; | |
223 | ret = serial_stream_detect(serial, buf, &len, packet_size, | |
224 | is_valid, timeout_ms, baudrate); | |
225 | ||
226 | serial_close(serial); | |
227 | ||
228 | if (ret != SR_OK) | |
229 | return ret; | |
230 | ||
231 | /* | |
232 | * If we dropped more than two packets worth of data, something is | |
233 | * wrong. We shouldn't quit however, since the dropped bytes might be | |
234 | * just zeroes at the beginning of the stream. Those can occur as a | |
235 | * combination of the nonstandard cable that ships with some devices | |
236 | * and the serial port or USB to serial adapter. | |
237 | */ | |
238 | dropped = len - packet_size; | |
239 | if (dropped > 2 * packet_size) | |
240 | sr_warn("Had to drop too much data."); | |
241 | ||
242 | return SR_OK; | |
243 | } | |
244 | ||
245 | static int serial_stream_check(struct sr_serial_dev_inst *serial, | |
246 | size_t packet_size, | |
247 | packet_valid_callback is_valid, | |
248 | uint64_t timeout_ms, int baudrate) | |
249 | { | |
250 | uint8_t buf[128]; | |
251 | ||
252 | return serial_stream_check_buf(serial, buf, sizeof(buf), packet_size, | |
253 | is_valid, timeout_ms, baudrate); | |
254 | } | |
255 | ||
256 | struct std_opt_desc { | |
257 | const uint32_t *scanopts; | |
258 | const int num_scanopts; | |
259 | const uint32_t *devopts; | |
260 | const int num_devopts; | |
261 | }; | |
262 | ||
263 | static int std_config_list(uint32_t key, GVariant **data, | |
264 | const struct std_opt_desc *d) | |
265 | { | |
266 | switch (key) { | |
267 | case SR_CONF_SCAN_OPTIONS: | |
268 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
269 | d->scanopts, d->num_scanopts, sizeof(uint32_t)); | |
270 | break; | |
271 | case SR_CONF_DEVICE_OPTIONS: | |
272 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
273 | d->devopts, d->num_devopts, sizeof(uint32_t)); | |
274 | break; | |
275 | default: | |
276 | return SR_ERR_NA; | |
277 | } | |
278 | ||
279 | return SR_OK; | |
280 | } | |
281 | ||
282 | static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg) | |
283 | { | |
284 | struct sr_datafeed_packet packet; | |
285 | struct sr_datafeed_meta meta; | |
286 | ||
287 | memset(&meta, 0, sizeof(meta)); | |
288 | ||
289 | packet.type = SR_DF_META; | |
290 | packet.payload = &meta; | |
291 | ||
292 | meta.config = g_slist_append(meta.config, cfg); | |
293 | ||
294 | return sr_session_send(sdi, &packet); | |
295 | } | |
296 | ||
a42a39ac JH |
297 | static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key, |
298 | GVariant *var) | |
299 | { | |
300 | struct sr_config *cfg; | |
301 | int ret; | |
302 | ||
303 | cfg = sr_config_new(key, var); | |
304 | if (!cfg) | |
305 | return SR_ERR; | |
306 | ||
307 | ret = send_config_update(sdi, cfg); | |
308 | sr_config_free(cfg); | |
309 | ||
310 | return ret; | |
311 | ||
312 | } | |
313 | ||
6bcb3ee8 JH |
314 | /* |
315 | * Cyrustek ES51919 LCR chipset host protocol. | |
316 | * | |
317 | * Public official documentation does not contain the protocol | |
318 | * description, so this is all based on reverse engineering. | |
319 | * | |
320 | * Packet structure (17 bytes): | |
321 | * | |
322 | * 0x00: header1 ?? (0x00) | |
323 | * 0x01: header2 ?? (0x0d) | |
324 | * | |
325 | * 0x02: flags | |
326 | * bit 0 = hold enabled | |
327 | * bit 1 = reference shown (in delta mode) | |
328 | * bit 2 = delta mode | |
329 | * bit 3 = calibration mode | |
330 | * bit 4 = sorting mode | |
331 | * bit 5 = LCR mode | |
332 | * bit 6 = auto mode | |
333 | * bit 7 = parallel measurement (vs. serial) | |
334 | * | |
335 | * 0x03: config | |
336 | * bit 0-4 = ??? (0x10) | |
337 | * bit 5-7 = test frequency | |
338 | * 0 = 100 Hz | |
339 | * 1 = 120 Hz | |
340 | * 2 = 1 kHz | |
341 | * 3 = 10 kHz | |
342 | * 4 = 100 kHz | |
343 | * 5 = 0 Hz (DC) | |
344 | * | |
345 | * 0x04: tolerance (sorting mode) | |
346 | * 0 = not set | |
347 | * 3 = +-0.25% | |
348 | * 4 = +-0.5% | |
349 | * 5 = +-1% | |
350 | * 6 = +-2% | |
351 | * 7 = +-5% | |
352 | * 8 = +-10% | |
353 | * 9 = +-20% | |
354 | * 10 = -20+80% | |
355 | * | |
356 | * 0x05-0x09: primary measurement | |
357 | * 0x05: measured quantity | |
358 | * 1 = inductance | |
359 | * 2 = capacitance | |
360 | * 3 = resistance | |
361 | * 4 = DC resistance | |
362 | * 0x06: measurement MSB (0x4e20 = 20000 = outside limits) | |
363 | * 0x07: measurement LSB | |
364 | * 0x08: measurement info | |
365 | * bit 0-2 = decimal point multiplier (10^-val) | |
366 | * bit 3-7 = unit | |
367 | * 0 = no unit | |
368 | * 1 = Ohm | |
369 | * 2 = kOhm | |
370 | * 3 = MOhm | |
371 | * 5 = uH | |
372 | * 6 = mH | |
373 | * 7 = H | |
374 | * 8 = kH | |
375 | * 9 = pF | |
376 | * 10 = nF | |
377 | * 11 = uF | |
378 | * 12 = mF | |
379 | * 13 = % | |
380 | * 14 = degree | |
381 | * 0x09: measurement status | |
382 | * bit 0-3 = status | |
383 | * 0 = normal (measurement shown) | |
384 | * 1 = blank (nothing shown) | |
385 | * 2 = lines ("----") | |
99d090d8 | 386 | * 3 = outside limits ("OL") |
6bcb3ee8 JH |
387 | * 7 = pass ("PASS") |
388 | * 8 = fail ("FAIL") | |
389 | * 9 = open ("OPEn") | |
390 | * 10 = shorted ("Srt") | |
391 | * bit 4-6 = ??? (maybe part of same field with 0-3) | |
392 | * bit 7 = ??? (some independent flag) | |
393 | * | |
394 | * 0x0a-0x0e: secondary measurement | |
395 | * 0x0a: measured quantity | |
396 | * 0 = none | |
397 | * 1 = dissipation factor | |
398 | * 2 = quality factor | |
399 | * 3 = parallel AC resistance / ESR | |
400 | * 4 = phase angle | |
401 | * 0x0b-0x0e: like primary measurement | |
402 | * | |
403 | * 0x0f: footer1 (0x0d) ? | |
404 | * 0x10: footer2 (0x0a) ? | |
405 | */ | |
406 | ||
407 | #define PACKET_SIZE 17 | |
408 | ||
409 | static const uint64_t frequencies[] = { | |
410 | 100, 120, 1000, 10000, 100000, 0, | |
411 | }; | |
412 | ||
a42a39ac JH |
413 | enum { QUANT_AUTO = 5, }; |
414 | ||
415 | static const char *const quantities1[] = { | |
416 | "NONE", "INDUCTANCE", "CAPACITANCE", "RESISTANCE", "RESISTANCE", "AUTO", | |
417 | }; | |
418 | ||
419 | static const char *const list_quantities1[] = { | |
420 | "NONE", "INDUCTANCE", "CAPACITANCE", "RESISTANCE", "AUTO", | |
421 | }; | |
422 | ||
423 | static const char *const quantities2[] = { | |
424 | "NONE", "DISSIPATION", "QUALITY", "RESISTANCE", "ANGLE", "AUTO", | |
425 | }; | |
426 | ||
427 | enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, }; | |
428 | ||
429 | static const char *const models[] = { | |
430 | "NONE", "PARALLEL", "SERIES", "AUTO", | |
431 | }; | |
432 | ||
6bcb3ee8 JH |
433 | /** Private, per-device-instance driver context. */ |
434 | struct dev_context { | |
435 | /** Opaque pointer passed in by the frontend. */ | |
436 | void *cb_data; | |
437 | ||
438 | /** The number of samples. */ | |
439 | struct dev_sample_counter sample_count; | |
440 | ||
441 | /** The time limit counter. */ | |
442 | struct dev_time_counter time_count; | |
443 | ||
444 | /** Data buffer. */ | |
445 | struct dev_buffer *buf; | |
446 | ||
447 | /** The frequency of the test signal (index to frequencies[]). */ | |
448 | unsigned int freq; | |
a42a39ac JH |
449 | |
450 | /** Measured primary quantity (index to quantities1[]). */ | |
451 | unsigned int quant1; | |
452 | ||
453 | /** Measured secondary quantity (index to quantities2[]). */ | |
454 | unsigned int quant2; | |
455 | ||
456 | /** Equivalent circuit model (index to models[]). */ | |
457 | unsigned int model; | |
6bcb3ee8 JH |
458 | }; |
459 | ||
a42a39ac | 460 | static const uint8_t *pkt_to_buf(const uint8_t *pkt, int is_secondary) |
6bcb3ee8 | 461 | { |
a42a39ac JH |
462 | return is_secondary ? pkt + 10 : pkt + 5; |
463 | } | |
464 | ||
465 | static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel) | |
466 | { | |
467 | const uint8_t *buf; | |
468 | ||
469 | buf = pkt_to_buf(pkt, is_secondary); | |
470 | ||
6bcb3ee8 JH |
471 | switch (is_secondary << 8 | buf[0]) { |
472 | case 0x001: | |
c7c8994c JH |
473 | return is_parallel ? |
474 | SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE; | |
6bcb3ee8 JH |
475 | case 0x002: |
476 | return is_parallel ? | |
c7c8994c | 477 | SR_MQ_PARALLEL_CAPACITANCE : SR_MQ_SERIES_CAPACITANCE; |
6bcb3ee8 JH |
478 | case 0x003: |
479 | case 0x103: | |
480 | return is_parallel ? | |
c7c8994c | 481 | SR_MQ_PARALLEL_RESISTANCE : SR_MQ_SERIES_RESISTANCE; |
6bcb3ee8 JH |
482 | case 0x004: |
483 | return SR_MQ_RESISTANCE; | |
484 | case 0x100: | |
485 | return SR_MQ_DIFFERENCE; | |
486 | case 0x101: | |
487 | return SR_MQ_DISSIPATION_FACTOR; | |
488 | case 0x102: | |
489 | return SR_MQ_QUALITY_FACTOR; | |
490 | case 0x104: | |
491 | return SR_MQ_PHASE_ANGLE; | |
492 | } | |
493 | ||
494 | sr_err("Unknown quantity 0x%03x.", is_secondary << 8 | buf[0]); | |
495 | ||
496 | return -1; | |
497 | } | |
498 | ||
499 | static float parse_value(const uint8_t *buf) | |
500 | { | |
501 | static const float decimals[] = { | |
502 | 1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7 | |
503 | }; | |
504 | int16_t val; | |
505 | ||
506 | val = (buf[1] << 8) | buf[2]; | |
507 | return (float)val * decimals[buf[3] & 7]; | |
508 | } | |
509 | ||
510 | static void parse_measurement(const uint8_t *pkt, float *floatval, | |
511 | struct sr_datafeed_analog *analog, | |
512 | int is_secondary) | |
513 | { | |
514 | static const struct { | |
515 | int unit; | |
516 | float mult; | |
517 | } units[] = { | |
518 | { SR_UNIT_UNITLESS, 1 }, /* no unit */ | |
519 | { SR_UNIT_OHM, 1 }, /* Ohm */ | |
520 | { SR_UNIT_OHM, 1e3 }, /* kOhm */ | |
521 | { SR_UNIT_OHM, 1e6 }, /* MOhm */ | |
522 | { -1, 0 }, /* ??? */ | |
523 | { SR_UNIT_HENRY, 1e-6 }, /* uH */ | |
524 | { SR_UNIT_HENRY, 1e-3 }, /* mH */ | |
525 | { SR_UNIT_HENRY, 1 }, /* H */ | |
526 | { SR_UNIT_HENRY, 1e3 }, /* kH */ | |
527 | { SR_UNIT_FARAD, 1e-12 }, /* pF */ | |
528 | { SR_UNIT_FARAD, 1e-9 }, /* nF */ | |
529 | { SR_UNIT_FARAD, 1e-6 }, /* uF */ | |
530 | { SR_UNIT_FARAD, 1e-3 }, /* mF */ | |
531 | { SR_UNIT_PERCENTAGE, 1 }, /* % */ | |
532 | { SR_UNIT_DEGREE, 1 } /* degree */ | |
533 | }; | |
534 | const uint8_t *buf; | |
535 | int state; | |
536 | ||
a42a39ac | 537 | buf = pkt_to_buf(pkt, is_secondary); |
6bcb3ee8 JH |
538 | |
539 | analog->mq = -1; | |
540 | analog->mqflags = 0; | |
541 | ||
542 | state = buf[4] & 0xf; | |
543 | ||
544 | if (state != 0 && state != 3) | |
545 | return; | |
546 | ||
547 | if (pkt[2] & 0x18) { | |
548 | /* Calibration and Sorting modes not supported. */ | |
549 | return; | |
550 | } | |
551 | ||
552 | if (!is_secondary) { | |
553 | if (pkt[2] & 0x01) | |
554 | analog->mqflags |= SR_MQFLAG_HOLD; | |
555 | if (pkt[2] & 0x02) | |
556 | analog->mqflags |= SR_MQFLAG_REFERENCE; | |
6bcb3ee8 JH |
557 | } else { |
558 | if (pkt[2] & 0x04) | |
559 | analog->mqflags |= SR_MQFLAG_RELATIVE; | |
560 | } | |
561 | ||
a42a39ac | 562 | if ((analog->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) < 0) |
6bcb3ee8 JH |
563 | return; |
564 | ||
565 | if ((buf[3] >> 3) >= ARRAY_SIZE(units)) { | |
566 | sr_err("Unknown unit %u.", buf[3] >> 3); | |
567 | analog->mq = -1; | |
568 | return; | |
569 | } | |
570 | ||
571 | analog->unit = units[buf[3] >> 3].unit; | |
572 | ||
573 | *floatval = parse_value(buf); | |
574 | *floatval *= (state == 0) ? units[buf[3] >> 3].mult : INFINITY; | |
575 | } | |
576 | ||
a42a39ac | 577 | static unsigned int parse_freq(const uint8_t *pkt) |
6bcb3ee8 JH |
578 | { |
579 | unsigned int freq; | |
580 | ||
581 | freq = pkt[3] >> 5; | |
582 | ||
583 | if (freq >= ARRAY_SIZE(frequencies)) { | |
584 | sr_err("Unknown frequency %u.", freq); | |
585 | freq = ARRAY_SIZE(frequencies) - 1; | |
586 | } | |
587 | ||
588 | return freq; | |
589 | } | |
590 | ||
a42a39ac JH |
591 | static unsigned int parse_quant(const uint8_t *pkt, int is_secondary) |
592 | { | |
593 | const uint8_t *buf; | |
594 | ||
595 | if (pkt[2] & 0x20) | |
596 | return QUANT_AUTO; | |
597 | ||
598 | buf = pkt_to_buf(pkt, is_secondary); | |
599 | ||
600 | return buf[0]; | |
601 | } | |
602 | ||
603 | static unsigned int parse_model(const uint8_t *pkt) | |
604 | { | |
605 | if (pkt[2] & 0x40) | |
606 | return MODEL_AUTO; | |
607 | else if (parse_mq(pkt, 0, 0) == SR_MQ_RESISTANCE) | |
608 | return MODEL_NONE; | |
609 | else if (pkt[2] & 0x80) | |
610 | return MODEL_PAR; | |
611 | else | |
612 | return MODEL_SER; | |
613 | ||
614 | } | |
615 | ||
6bcb3ee8 JH |
616 | static gboolean packet_valid(const uint8_t *pkt) |
617 | { | |
618 | /* | |
619 | * If the first two bytes of the packet are indeed a constant | |
620 | * header, they should be checked too. Since we don't know it | |
621 | * for sure, we'll just check the last two for now since they | |
622 | * seem to be constant just like in the other Cyrustek chipset | |
623 | * protocols. | |
624 | */ | |
625 | if (pkt[15] == 0xd && pkt[16] == 0xa) | |
626 | return TRUE; | |
627 | ||
628 | return FALSE; | |
629 | } | |
630 | ||
a42a39ac JH |
631 | static int do_config_update(struct sr_dev_inst *sdi, uint32_t key, |
632 | GVariant *var) | |
6bcb3ee8 | 633 | { |
6bcb3ee8 | 634 | struct dev_context *devc; |
6bcb3ee8 JH |
635 | |
636 | devc = sdi->priv; | |
637 | ||
a42a39ac JH |
638 | return send_config_update_key(devc->cb_data, key, var); |
639 | } | |
6bcb3ee8 | 640 | |
a42a39ac JH |
641 | static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq) |
642 | { | |
643 | return do_config_update(sdi, SR_CONF_OUTPUT_FREQUENCY, | |
644 | g_variant_new_uint64(frequencies[freq])); | |
645 | } | |
6bcb3ee8 | 646 | |
a42a39ac JH |
647 | static int send_quant1_update(struct sr_dev_inst *sdi, unsigned int quant) |
648 | { | |
649 | return do_config_update(sdi, SR_CONF_MEASURED_QUANTITY, | |
650 | g_variant_new_string(quantities1[quant])); | |
651 | } | |
6bcb3ee8 | 652 | |
a42a39ac JH |
653 | static int send_quant2_update(struct sr_dev_inst *sdi, unsigned int quant) |
654 | { | |
655 | return do_config_update(sdi, SR_CONF_MEASURED_2ND_QUANTITY, | |
656 | g_variant_new_string(quantities2[quant])); | |
657 | } | |
658 | ||
659 | static int send_model_update(struct sr_dev_inst *sdi, unsigned int model) | |
660 | { | |
661 | return do_config_update(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL, | |
662 | g_variant_new_string(models[model])); | |
6bcb3ee8 JH |
663 | } |
664 | ||
665 | static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt) | |
666 | { | |
667 | struct sr_datafeed_packet packet; | |
668 | struct sr_datafeed_analog analog; | |
669 | struct dev_context *devc; | |
a42a39ac | 670 | unsigned int val; |
6bcb3ee8 JH |
671 | float floatval; |
672 | int count; | |
673 | ||
674 | devc = sdi->priv; | |
675 | ||
a42a39ac JH |
676 | val = parse_freq(pkt); |
677 | if (val != devc->freq) { | |
678 | if (send_freq_update(sdi, val) == SR_OK) | |
679 | devc->freq = val; | |
680 | else | |
681 | return; | |
682 | } | |
683 | ||
684 | val = parse_quant(pkt, 0); | |
685 | if (val != devc->quant1) { | |
686 | if (send_quant1_update(sdi, val) == SR_OK) | |
687 | devc->quant1 = val; | |
688 | else | |
689 | return; | |
690 | } | |
691 | ||
692 | val = parse_quant(pkt, 1); | |
693 | if (val != devc->quant2) { | |
694 | if (send_quant2_update(sdi, val) == SR_OK) | |
695 | devc->quant2 = val; | |
696 | else | |
697 | return; | |
698 | } | |
699 | ||
700 | val = parse_model(pkt); | |
701 | if (val != devc->model) { | |
702 | if (send_model_update(sdi, val) == SR_OK) | |
703 | devc->model = val; | |
6bcb3ee8 JH |
704 | else |
705 | return; | |
706 | } | |
707 | ||
708 | count = 0; | |
709 | ||
710 | memset(&analog, 0, sizeof(analog)); | |
711 | ||
712 | analog.num_samples = 1; | |
713 | analog.data = &floatval; | |
714 | ||
715 | packet.type = SR_DF_ANALOG; | |
716 | packet.payload = &analog; | |
717 | ||
718 | analog.channels = g_slist_append(NULL, sdi->channels->data); | |
719 | ||
720 | parse_measurement(pkt, &floatval, &analog, 0); | |
721 | if (analog.mq >= 0) { | |
722 | if (sr_session_send(devc->cb_data, &packet) == SR_OK) | |
723 | count++; | |
724 | } | |
725 | ||
726 | analog.channels = g_slist_append(NULL, sdi->channels->next->data); | |
727 | ||
728 | parse_measurement(pkt, &floatval, &analog, 1); | |
729 | if (analog.mq >= 0) { | |
730 | if (sr_session_send(devc->cb_data, &packet) == SR_OK) | |
731 | count++; | |
732 | } | |
733 | ||
734 | if (count > 0) | |
735 | dev_sample_counter_inc(&devc->sample_count); | |
736 | } | |
737 | ||
738 | static int handle_new_data(struct sr_dev_inst *sdi) | |
739 | { | |
740 | struct dev_context *devc; | |
741 | uint8_t *pkt; | |
742 | int ret; | |
743 | ||
744 | devc = sdi->priv; | |
745 | ||
746 | ret = dev_buffer_fill_serial(devc->buf, sdi); | |
747 | if (ret < 0) | |
748 | return ret; | |
749 | ||
750 | while ((pkt = dev_buffer_packet_find(devc->buf, packet_valid, | |
751 | PACKET_SIZE))) | |
752 | handle_packet(sdi, pkt); | |
753 | ||
754 | return SR_OK; | |
755 | } | |
756 | ||
757 | static int receive_data(int fd, int revents, void *cb_data) | |
758 | { | |
759 | struct sr_dev_inst *sdi; | |
760 | struct dev_context *devc; | |
761 | ||
762 | (void)fd; | |
763 | ||
764 | if (!(sdi = cb_data)) | |
765 | return TRUE; | |
766 | ||
767 | if (!(devc = sdi->priv)) | |
768 | return TRUE; | |
769 | ||
770 | if (revents == G_IO_IN) { | |
771 | /* Serial data arrived. */ | |
772 | handle_new_data(sdi); | |
773 | } | |
774 | ||
775 | if (dev_sample_limit_reached(&devc->sample_count) || | |
776 | dev_time_limit_reached(&devc->time_count)) | |
777 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
778 | ||
779 | return TRUE; | |
780 | } | |
781 | ||
782 | static int add_channel(struct sr_dev_inst *sdi, const char *name) | |
783 | { | |
784 | struct sr_channel *ch; | |
785 | ||
786 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, name))) | |
787 | return SR_ERR; | |
788 | ||
789 | sdi->channels = g_slist_append(sdi->channels, ch); | |
790 | ||
791 | return SR_OK; | |
792 | } | |
793 | ||
794 | static const char *const channel_names[] = { "P1", "P2" }; | |
795 | ||
796 | static int setup_channels(struct sr_dev_inst *sdi) | |
797 | { | |
798 | unsigned int i; | |
799 | int ret; | |
800 | ||
801 | ret = SR_ERR_BUG; | |
802 | ||
803 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) { | |
804 | ret = add_channel(sdi, channel_names[i]); | |
805 | if (ret != SR_OK) | |
806 | break; | |
807 | } | |
808 | ||
809 | return ret; | |
810 | } | |
811 | ||
812 | SR_PRIV void es51919_serial_clean(void *priv) | |
813 | { | |
814 | struct dev_context *devc; | |
815 | ||
816 | if (!(devc = priv)) | |
817 | return; | |
818 | ||
819 | dev_buffer_destroy(devc->buf); | |
820 | g_free(devc); | |
821 | } | |
822 | ||
823 | SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options, | |
824 | const char *vendor, | |
825 | const char *model) | |
826 | { | |
827 | struct sr_serial_dev_inst *serial; | |
828 | struct sr_dev_inst *sdi; | |
829 | struct dev_context *devc; | |
830 | int ret; | |
831 | ||
832 | serial = NULL; | |
833 | sdi = NULL; | |
834 | devc = NULL; | |
835 | ||
836 | if (!(serial = serial_dev_new(options, "9600/8n1/rts=1/dtr=1"))) | |
837 | goto scan_cleanup; | |
838 | ||
839 | ret = serial_stream_check(serial, PACKET_SIZE, packet_valid, | |
840 | 3000, 9600); | |
841 | if (ret != SR_OK) | |
842 | goto scan_cleanup; | |
843 | ||
844 | sr_info("Found device on port %s.", serial->port); | |
845 | ||
0af636be UH |
846 | sdi = sr_dev_inst_new(); |
847 | sdi->status = SR_ST_INACTIVE; | |
848 | sdi->vendor = g_strdup(vendor); | |
849 | sdi->model = g_strdup(model); | |
6bcb3ee8 JH |
850 | |
851 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
852 | sr_err("Device context malloc failed."); | |
853 | goto scan_cleanup; | |
854 | } | |
855 | ||
856 | if (!(devc->buf = dev_buffer_new(PACKET_SIZE * 8))) | |
857 | goto scan_cleanup; | |
858 | ||
6bcb3ee8 JH |
859 | sdi->inst_type = SR_INST_SERIAL; |
860 | sdi->conn = serial; | |
861 | ||
862 | sdi->priv = devc; | |
863 | ||
864 | if (setup_channels(sdi) != SR_OK) | |
865 | goto scan_cleanup; | |
866 | ||
867 | return sdi; | |
868 | ||
869 | scan_cleanup: | |
870 | es51919_serial_clean(devc); | |
871 | if (sdi) | |
872 | sr_dev_inst_free(sdi); | |
873 | if (serial) | |
874 | sr_serial_dev_inst_free(serial); | |
875 | ||
876 | return NULL; | |
877 | } | |
878 | ||
879 | SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data, | |
880 | const struct sr_dev_inst *sdi, | |
881 | const struct sr_channel_group *cg) | |
882 | { | |
883 | struct dev_context *devc; | |
884 | ||
885 | (void)cg; | |
886 | ||
887 | if (!(devc = sdi->priv)) | |
888 | return SR_ERR_BUG; | |
889 | ||
890 | switch (key) { | |
891 | case SR_CONF_OUTPUT_FREQUENCY: | |
892 | *data = g_variant_new_uint64(frequencies[devc->freq]); | |
893 | break; | |
a42a39ac JH |
894 | case SR_CONF_MEASURED_QUANTITY: |
895 | *data = g_variant_new_string(quantities1[devc->quant1]); | |
896 | break; | |
897 | case SR_CONF_MEASURED_2ND_QUANTITY: | |
898 | *data = g_variant_new_string(quantities2[devc->quant2]); | |
899 | break; | |
900 | case SR_CONF_EQUIV_CIRCUIT_MODEL: | |
901 | *data = g_variant_new_string(models[devc->model]); | |
902 | break; | |
6bcb3ee8 JH |
903 | default: |
904 | sr_spew("%s: Unsupported key %u", __func__, key); | |
905 | return SR_ERR_NA; | |
906 | } | |
907 | ||
908 | return SR_OK; | |
909 | } | |
910 | ||
911 | SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data, | |
912 | const struct sr_dev_inst *sdi, | |
913 | const struct sr_channel_group *cg) | |
914 | { | |
915 | struct dev_context *devc; | |
916 | uint64_t val; | |
917 | ||
918 | (void)cg; | |
919 | ||
920 | if (!(devc = sdi->priv)) | |
921 | return SR_ERR_BUG; | |
922 | ||
923 | switch (key) { | |
924 | case SR_CONF_LIMIT_MSEC: | |
925 | val = g_variant_get_uint64(data); | |
926 | dev_time_limit_set(&devc->time_count, val); | |
927 | sr_dbg("Setting time limit to %" PRIu64 ".", val); | |
928 | break; | |
929 | case SR_CONF_LIMIT_SAMPLES: | |
930 | val = g_variant_get_uint64(data); | |
931 | dev_sample_limit_set(&devc->sample_count, val); | |
932 | sr_dbg("Setting sample limit to %" PRIu64 ".", val); | |
933 | break; | |
934 | default: | |
935 | sr_spew("%s: Unsupported key %u", __func__, key); | |
936 | return SR_ERR_NA; | |
937 | } | |
938 | ||
939 | return SR_OK; | |
940 | } | |
941 | ||
942 | static const uint32_t scanopts[] = { | |
943 | SR_CONF_CONN, | |
944 | SR_CONF_SERIALCOMM, | |
945 | }; | |
946 | ||
947 | static const uint32_t devopts[] = { | |
b9a348f5 | 948 | SR_CONF_LCRMETER, |
6bcb3ee8 JH |
949 | SR_CONF_CONTINUOUS, |
950 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
951 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
952 | SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST, | |
a42a39ac JH |
953 | SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_LIST, |
954 | SR_CONF_MEASURED_2ND_QUANTITY | SR_CONF_GET | SR_CONF_LIST, | |
955 | SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST, | |
6bcb3ee8 JH |
956 | }; |
957 | ||
958 | static const struct std_opt_desc opts = { | |
959 | scanopts, ARRAY_SIZE(scanopts), | |
960 | devopts, ARRAY_SIZE(devopts), | |
961 | }; | |
962 | ||
963 | SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data, | |
964 | const struct sr_dev_inst *sdi, | |
965 | const struct sr_channel_group *cg) | |
966 | { | |
967 | (void)sdi; | |
968 | (void)cg; | |
969 | ||
970 | if (std_config_list(key, data, &opts) == SR_OK) | |
971 | return SR_OK; | |
972 | ||
973 | switch (key) { | |
974 | case SR_CONF_OUTPUT_FREQUENCY: | |
975 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, | |
976 | frequencies, ARRAY_SIZE(frequencies), sizeof(uint64_t)); | |
977 | break; | |
a42a39ac JH |
978 | case SR_CONF_MEASURED_QUANTITY: |
979 | *data = g_variant_new_strv(list_quantities1, | |
980 | ARRAY_SIZE(list_quantities1)); | |
981 | break; | |
982 | case SR_CONF_MEASURED_2ND_QUANTITY: | |
983 | *data = g_variant_new_strv(quantities2, | |
984 | ARRAY_SIZE(quantities2)); | |
985 | break; | |
986 | case SR_CONF_EQUIV_CIRCUIT_MODEL: | |
987 | *data = g_variant_new_strv(models, ARRAY_SIZE(models)); | |
988 | break; | |
6bcb3ee8 JH |
989 | default: |
990 | sr_spew("%s: Unsupported key %u", __func__, key); | |
991 | return SR_ERR_NA; | |
992 | } | |
993 | ||
994 | return SR_OK; | |
995 | } | |
996 | ||
997 | SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi, | |
998 | void *cb_data) | |
999 | { | |
1000 | struct dev_context *devc; | |
1001 | struct sr_serial_dev_inst *serial; | |
1002 | ||
1003 | if (sdi->status != SR_ST_ACTIVE) | |
1004 | return SR_ERR_DEV_CLOSED; | |
1005 | ||
1006 | if (!(devc = sdi->priv)) | |
1007 | return SR_ERR_BUG; | |
1008 | ||
1009 | devc->cb_data = cb_data; | |
1010 | ||
1011 | dev_sample_counter_start(&devc->sample_count); | |
1012 | dev_time_counter_start(&devc->time_count); | |
1013 | ||
1014 | /* Send header packet to the session bus. */ | |
1015 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
1016 | ||
1017 | /* Poll every 50ms, or whenever some data comes in. */ | |
1018 | serial = sdi->conn; | |
1019 | serial_source_add(sdi->session, serial, G_IO_IN, 50, | |
1020 | receive_data, (void *)sdi); | |
1021 | ||
1022 | return SR_OK; | |
1023 | } | |
1024 | ||
1025 | SR_PRIV int es51919_serial_acquisition_stop(struct sr_dev_inst *sdi, | |
1026 | void *cb_data) | |
1027 | { | |
1028 | return std_serial_dev_acquisition_stop(sdi, cb_data, | |
1029 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
1030 | } |