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