]>
Commit | Line | Data |
---|---|---|
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 <config.h> | |
21 | #include <stdint.h> | |
22 | #include <string.h> | |
23 | #include <math.h> | |
24 | #include <glib.h> | |
25 | #include <libsigrok/libsigrok.h> | |
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 | ||
45 | dbuf = g_malloc0(sizeof(struct dev_buffer) + size); | |
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 | ||
104 | struct dev_limit_counter { | |
105 | /** The current number of received samples/frames/etc. */ | |
106 | uint64_t count; | |
107 | /** The limit (in number of samples/frames/etc.). */ | |
108 | uint64_t limit; | |
109 | }; | |
110 | ||
111 | static void dev_limit_counter_start(struct dev_limit_counter *cnt) | |
112 | { | |
113 | cnt->count = 0; | |
114 | } | |
115 | ||
116 | static void dev_limit_counter_inc(struct dev_limit_counter *cnt) | |
117 | { | |
118 | cnt->count++; | |
119 | } | |
120 | ||
121 | static void dev_limit_counter_limit_set(struct dev_limit_counter *cnt, | |
122 | uint64_t limit) | |
123 | { | |
124 | cnt->limit = limit; | |
125 | } | |
126 | ||
127 | static gboolean dev_limit_counter_limit_reached(struct dev_limit_counter *cnt) | |
128 | { | |
129 | if (cnt->limit && cnt->count >= cnt->limit) { | |
130 | sr_info("Requested counter limit reached."); | |
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 | static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg) | |
255 | { | |
256 | struct sr_datafeed_packet packet; | |
257 | struct sr_datafeed_meta meta; | |
258 | ||
259 | memset(&meta, 0, sizeof(meta)); | |
260 | ||
261 | packet.type = SR_DF_META; | |
262 | packet.payload = &meta; | |
263 | ||
264 | meta.config = g_slist_append(meta.config, cfg); | |
265 | ||
266 | return sr_session_send(sdi, &packet); | |
267 | } | |
268 | ||
269 | static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key, | |
270 | GVariant *var) | |
271 | { | |
272 | struct sr_config *cfg; | |
273 | int ret; | |
274 | ||
275 | cfg = sr_config_new(key, var); | |
276 | if (!cfg) | |
277 | return SR_ERR; | |
278 | ||
279 | ret = send_config_update(sdi, cfg); | |
280 | sr_config_free(cfg); | |
281 | ||
282 | return ret; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Cyrustek ES51919 LCR chipset host protocol. | |
287 | * | |
288 | * Public official documentation does not contain the protocol | |
289 | * description, so this is all based on reverse engineering. | |
290 | * | |
291 | * Packet structure (17 bytes): | |
292 | * | |
293 | * 0x00: header1 ?? (0x00) | |
294 | * 0x01: header2 ?? (0x0d) | |
295 | * | |
296 | * 0x02: flags | |
297 | * bit 0 = hold enabled | |
298 | * bit 1 = reference shown (in delta mode) | |
299 | * bit 2 = delta mode | |
300 | * bit 3 = calibration mode | |
301 | * bit 4 = sorting mode | |
302 | * bit 5 = LCR mode | |
303 | * bit 6 = auto mode | |
304 | * bit 7 = parallel measurement (vs. serial) | |
305 | * | |
306 | * 0x03: config | |
307 | * bit 0-4 = ??? (0x10) | |
308 | * bit 5-7 = test frequency | |
309 | * 0 = 100 Hz | |
310 | * 1 = 120 Hz | |
311 | * 2 = 1 kHz | |
312 | * 3 = 10 kHz | |
313 | * 4 = 100 kHz | |
314 | * 5 = 0 Hz (DC) | |
315 | * | |
316 | * 0x04: tolerance (sorting mode) | |
317 | * 0 = not set | |
318 | * 3 = +-0.25% | |
319 | * 4 = +-0.5% | |
320 | * 5 = +-1% | |
321 | * 6 = +-2% | |
322 | * 7 = +-5% | |
323 | * 8 = +-10% | |
324 | * 9 = +-20% | |
325 | * 10 = -20+80% | |
326 | * | |
327 | * 0x05-0x09: primary measurement | |
328 | * 0x05: measured quantity | |
329 | * 1 = inductance | |
330 | * 2 = capacitance | |
331 | * 3 = resistance | |
332 | * 4 = DC resistance | |
333 | * 0x06: measurement MSB (0x4e20 = 20000 = outside limits) | |
334 | * 0x07: measurement LSB | |
335 | * 0x08: measurement info | |
336 | * bit 0-2 = decimal point multiplier (10^-val) | |
337 | * bit 3-7 = unit | |
338 | * 0 = no unit | |
339 | * 1 = Ohm | |
340 | * 2 = kOhm | |
341 | * 3 = MOhm | |
342 | * 5 = uH | |
343 | * 6 = mH | |
344 | * 7 = H | |
345 | * 8 = kH | |
346 | * 9 = pF | |
347 | * 10 = nF | |
348 | * 11 = uF | |
349 | * 12 = mF | |
350 | * 13 = % | |
351 | * 14 = degree | |
352 | * 0x09: measurement status | |
353 | * bit 0-3 = status | |
354 | * 0 = normal (measurement shown) | |
355 | * 1 = blank (nothing shown) | |
356 | * 2 = lines ("----") | |
357 | * 3 = outside limits ("OL") | |
358 | * 7 = pass ("PASS") | |
359 | * 8 = fail ("FAIL") | |
360 | * 9 = open ("OPEn") | |
361 | * 10 = shorted ("Srt") | |
362 | * bit 4-6 = ??? (maybe part of same field with 0-3) | |
363 | * bit 7 = ??? (some independent flag) | |
364 | * | |
365 | * 0x0a-0x0e: secondary measurement | |
366 | * 0x0a: measured quantity | |
367 | * 0 = none | |
368 | * 1 = dissipation factor | |
369 | * 2 = quality factor | |
370 | * 3 = parallel AC resistance / ESR | |
371 | * 4 = phase angle | |
372 | * 0x0b-0x0e: like primary measurement | |
373 | * | |
374 | * 0x0f: footer1 (0x0d) ? | |
375 | * 0x10: footer2 (0x0a) ? | |
376 | */ | |
377 | ||
378 | #define PACKET_SIZE 17 | |
379 | ||
380 | static const double frequencies[] = { | |
381 | 100, 120, 1000, 10000, 100000, 0, | |
382 | }; | |
383 | ||
384 | enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, }; | |
385 | ||
386 | static const char *const models[] = { | |
387 | "NONE", "PARALLEL", "SERIES", "AUTO", | |
388 | }; | |
389 | ||
390 | struct dev_context { | |
391 | struct dev_limit_counter frame_count; | |
392 | ||
393 | struct dev_time_counter time_count; | |
394 | ||
395 | struct dev_buffer *buf; | |
396 | ||
397 | /** The frequency of the test signal (index to frequencies[]). */ | |
398 | unsigned int freq; | |
399 | ||
400 | /** Equivalent circuit model (index to models[]). */ | |
401 | unsigned int model; | |
402 | }; | |
403 | ||
404 | static const uint8_t *pkt_to_buf(const uint8_t *pkt, int is_secondary) | |
405 | { | |
406 | return is_secondary ? pkt + 10 : pkt + 5; | |
407 | } | |
408 | ||
409 | static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel) | |
410 | { | |
411 | const uint8_t *buf; | |
412 | ||
413 | buf = pkt_to_buf(pkt, is_secondary); | |
414 | ||
415 | switch (is_secondary << 8 | buf[0]) { | |
416 | case 0x001: | |
417 | return is_parallel ? | |
418 | SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE; | |
419 | case 0x002: | |
420 | return is_parallel ? | |
421 | SR_MQ_PARALLEL_CAPACITANCE : SR_MQ_SERIES_CAPACITANCE; | |
422 | case 0x003: | |
423 | case 0x103: | |
424 | return is_parallel ? | |
425 | SR_MQ_PARALLEL_RESISTANCE : SR_MQ_SERIES_RESISTANCE; | |
426 | case 0x004: | |
427 | return SR_MQ_RESISTANCE; | |
428 | case 0x100: | |
429 | return SR_MQ_DIFFERENCE; | |
430 | case 0x101: | |
431 | return SR_MQ_DISSIPATION_FACTOR; | |
432 | case 0x102: | |
433 | return SR_MQ_QUALITY_FACTOR; | |
434 | case 0x104: | |
435 | return SR_MQ_PHASE_ANGLE; | |
436 | } | |
437 | ||
438 | sr_err("Unknown quantity 0x%03x.", is_secondary << 8 | buf[0]); | |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
443 | static float parse_value(const uint8_t *buf, int *digits) | |
444 | { | |
445 | static const int exponents[] = {0, -1, -2, -3, -4, -5, -6, -7}; | |
446 | int exponent; | |
447 | int16_t val; | |
448 | ||
449 | exponent = exponents[buf[3] & 7]; | |
450 | *digits = -exponent; | |
451 | val = (buf[1] << 8) | buf[2]; | |
452 | return (float)val * powf(10, exponent); | |
453 | } | |
454 | ||
455 | static void parse_measurement(const uint8_t *pkt, float *floatval, | |
456 | struct sr_datafeed_analog *analog, | |
457 | int is_secondary) | |
458 | { | |
459 | static const struct { | |
460 | int unit; | |
461 | int exponent; | |
462 | } units[] = { | |
463 | { SR_UNIT_UNITLESS, 0 }, /* no unit */ | |
464 | { SR_UNIT_OHM, 0 }, /* Ohm */ | |
465 | { SR_UNIT_OHM, 3 }, /* kOhm */ | |
466 | { SR_UNIT_OHM, 6 }, /* MOhm */ | |
467 | { -1, 0 }, /* ??? */ | |
468 | { SR_UNIT_HENRY, -6 }, /* uH */ | |
469 | { SR_UNIT_HENRY, -3 }, /* mH */ | |
470 | { SR_UNIT_HENRY, 0 }, /* H */ | |
471 | { SR_UNIT_HENRY, 3 }, /* kH */ | |
472 | { SR_UNIT_FARAD, -12 }, /* pF */ | |
473 | { SR_UNIT_FARAD, -9 }, /* nF */ | |
474 | { SR_UNIT_FARAD, -6 }, /* uF */ | |
475 | { SR_UNIT_FARAD, -3 }, /* mF */ | |
476 | { SR_UNIT_PERCENTAGE, 0 }, /* % */ | |
477 | { SR_UNIT_DEGREE, 0 }, /* degree */ | |
478 | }; | |
479 | const uint8_t *buf; | |
480 | int digits, exponent; | |
481 | int state; | |
482 | ||
483 | buf = pkt_to_buf(pkt, is_secondary); | |
484 | ||
485 | analog->meaning->mq = 0; | |
486 | analog->meaning->mqflags = 0; | |
487 | ||
488 | state = buf[4] & 0xf; | |
489 | ||
490 | if (state != 0 && state != 3) | |
491 | return; | |
492 | ||
493 | if (pkt[2] & 0x18) { | |
494 | /* Calibration and Sorting modes not supported. */ | |
495 | return; | |
496 | } | |
497 | ||
498 | if (!is_secondary) { | |
499 | if (pkt[2] & 0x01) | |
500 | analog->meaning->mqflags |= SR_MQFLAG_HOLD; | |
501 | if (pkt[2] & 0x02) | |
502 | analog->meaning->mqflags |= SR_MQFLAG_REFERENCE; | |
503 | } else { | |
504 | if (pkt[2] & 0x04) | |
505 | analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; | |
506 | } | |
507 | ||
508 | if ((analog->meaning->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) == 0) | |
509 | return; | |
510 | ||
511 | if ((buf[3] >> 3) >= ARRAY_SIZE(units)) { | |
512 | sr_err("Unknown unit %u.", buf[3] >> 3); | |
513 | analog->meaning->mq = 0; | |
514 | return; | |
515 | } | |
516 | ||
517 | analog->meaning->unit = units[buf[3] >> 3].unit; | |
518 | ||
519 | exponent = units[buf[3] >> 3].exponent; | |
520 | *floatval = parse_value(buf, &digits); | |
521 | *floatval *= (state == 0) ? powf(10, exponent) : INFINITY; | |
522 | analog->encoding->digits = digits - exponent; | |
523 | analog->spec->spec_digits = digits - exponent; | |
524 | } | |
525 | ||
526 | static unsigned int parse_freq(const uint8_t *pkt) | |
527 | { | |
528 | unsigned int freq; | |
529 | ||
530 | freq = pkt[3] >> 5; | |
531 | ||
532 | if (freq >= ARRAY_SIZE(frequencies)) { | |
533 | sr_err("Unknown frequency %u.", freq); | |
534 | freq = ARRAY_SIZE(frequencies) - 1; | |
535 | } | |
536 | ||
537 | return freq; | |
538 | } | |
539 | ||
540 | static unsigned int parse_model(const uint8_t *pkt) | |
541 | { | |
542 | if (pkt[2] & 0x40) | |
543 | return MODEL_AUTO; | |
544 | else if (parse_mq(pkt, 0, 0) == SR_MQ_RESISTANCE) | |
545 | return MODEL_NONE; | |
546 | else if (pkt[2] & 0x80) | |
547 | return MODEL_PAR; | |
548 | else | |
549 | return MODEL_SER; | |
550 | } | |
551 | ||
552 | static gboolean packet_valid(const uint8_t *pkt) | |
553 | { | |
554 | /* | |
555 | * If the first two bytes of the packet are indeed a constant | |
556 | * header, they should be checked too. Since we don't know it | |
557 | * for sure, we'll just check the last two for now since they | |
558 | * seem to be constant just like in the other Cyrustek chipset | |
559 | * protocols. | |
560 | */ | |
561 | if (pkt[15] == 0xd && pkt[16] == 0xa) | |
562 | return TRUE; | |
563 | ||
564 | return FALSE; | |
565 | } | |
566 | ||
567 | static int do_config_update(struct sr_dev_inst *sdi, uint32_t key, | |
568 | GVariant *var) | |
569 | { | |
570 | return send_config_update_key(sdi, key, var); | |
571 | } | |
572 | ||
573 | static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq) | |
574 | { | |
575 | return do_config_update(sdi, SR_CONF_OUTPUT_FREQUENCY, | |
576 | g_variant_new_double(frequencies[freq])); | |
577 | } | |
578 | ||
579 | static int send_model_update(struct sr_dev_inst *sdi, unsigned int model) | |
580 | { | |
581 | return do_config_update(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL, | |
582 | g_variant_new_string(models[model])); | |
583 | } | |
584 | ||
585 | static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt) | |
586 | { | |
587 | struct sr_datafeed_packet packet; | |
588 | struct sr_datafeed_analog analog; | |
589 | struct sr_analog_encoding encoding; | |
590 | struct sr_analog_meaning meaning; | |
591 | struct sr_analog_spec spec; | |
592 | struct dev_context *devc; | |
593 | unsigned int val; | |
594 | float floatval; | |
595 | gboolean frame; | |
596 | struct sr_channel *channel; | |
597 | ||
598 | devc = sdi->priv; | |
599 | ||
600 | val = parse_freq(pkt); | |
601 | if (val != devc->freq) { | |
602 | if (send_freq_update(sdi, val) == SR_OK) | |
603 | devc->freq = val; | |
604 | else | |
605 | return; | |
606 | } | |
607 | ||
608 | val = parse_model(pkt); | |
609 | if (val != devc->model) { | |
610 | if (send_model_update(sdi, val) == SR_OK) | |
611 | devc->model = val; | |
612 | else | |
613 | return; | |
614 | } | |
615 | ||
616 | frame = FALSE; | |
617 | ||
618 | /* Note: digits/spec_digits will be overridden later. */ | |
619 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); | |
620 | ||
621 | analog.num_samples = 1; | |
622 | analog.data = &floatval; | |
623 | ||
624 | channel = sdi->channels->data; | |
625 | analog.meaning->channels = g_slist_append(NULL, channel); | |
626 | ||
627 | parse_measurement(pkt, &floatval, &analog, 0); | |
628 | if (analog.meaning->mq != 0 && channel->enabled) { | |
629 | if (!frame) { | |
630 | packet.type = SR_DF_FRAME_BEGIN; | |
631 | sr_session_send(sdi, &packet); | |
632 | frame = TRUE; | |
633 | } | |
634 | ||
635 | packet.type = SR_DF_ANALOG; | |
636 | packet.payload = &analog; | |
637 | ||
638 | sr_session_send(sdi, &packet); | |
639 | } | |
640 | ||
641 | g_slist_free(analog.meaning->channels); | |
642 | ||
643 | channel = sdi->channels->next->data; | |
644 | analog.meaning->channels = g_slist_append(NULL, channel); | |
645 | ||
646 | parse_measurement(pkt, &floatval, &analog, 1); | |
647 | if (analog.meaning->mq != 0 && channel->enabled) { | |
648 | if (!frame) { | |
649 | packet.type = SR_DF_FRAME_BEGIN; | |
650 | sr_session_send(sdi, &packet); | |
651 | frame = TRUE; | |
652 | } | |
653 | ||
654 | packet.type = SR_DF_ANALOG; | |
655 | packet.payload = &analog; | |
656 | ||
657 | sr_session_send(sdi, &packet); | |
658 | } | |
659 | ||
660 | g_slist_free(analog.meaning->channels); | |
661 | ||
662 | if (frame) { | |
663 | packet.type = SR_DF_FRAME_END; | |
664 | sr_session_send(sdi, &packet); | |
665 | dev_limit_counter_inc(&devc->frame_count); | |
666 | } | |
667 | } | |
668 | ||
669 | static int handle_new_data(struct sr_dev_inst *sdi) | |
670 | { | |
671 | struct dev_context *devc; | |
672 | uint8_t *pkt; | |
673 | int ret; | |
674 | ||
675 | devc = sdi->priv; | |
676 | ||
677 | ret = dev_buffer_fill_serial(devc->buf, sdi); | |
678 | if (ret < 0) | |
679 | return ret; | |
680 | ||
681 | while ((pkt = dev_buffer_packet_find(devc->buf, packet_valid, | |
682 | PACKET_SIZE))) | |
683 | handle_packet(sdi, pkt); | |
684 | ||
685 | return SR_OK; | |
686 | } | |
687 | ||
688 | static int receive_data(int fd, int revents, void *cb_data) | |
689 | { | |
690 | struct sr_dev_inst *sdi; | |
691 | struct dev_context *devc; | |
692 | ||
693 | (void)fd; | |
694 | ||
695 | if (!(sdi = cb_data)) | |
696 | return TRUE; | |
697 | ||
698 | if (!(devc = sdi->priv)) | |
699 | return TRUE; | |
700 | ||
701 | if (revents == G_IO_IN) { | |
702 | /* Serial data arrived. */ | |
703 | handle_new_data(sdi); | |
704 | } | |
705 | ||
706 | if (dev_limit_counter_limit_reached(&devc->frame_count) || | |
707 | dev_time_limit_reached(&devc->time_count)) | |
708 | sr_dev_acquisition_stop(sdi); | |
709 | ||
710 | return TRUE; | |
711 | } | |
712 | ||
713 | static const char *const channel_names[] = { "P1", "P2" }; | |
714 | ||
715 | static int setup_channels(struct sr_dev_inst *sdi) | |
716 | { | |
717 | unsigned int i; | |
718 | ||
719 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) | |
720 | sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]); | |
721 | ||
722 | return SR_OK; | |
723 | } | |
724 | ||
725 | SR_PRIV void es51919_serial_clean(void *priv) | |
726 | { | |
727 | struct dev_context *devc; | |
728 | ||
729 | if (!(devc = priv)) | |
730 | return; | |
731 | ||
732 | dev_buffer_destroy(devc->buf); | |
733 | } | |
734 | ||
735 | SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options, | |
736 | const char *vendor, | |
737 | const char *model) | |
738 | { | |
739 | struct sr_serial_dev_inst *serial; | |
740 | struct sr_dev_inst *sdi; | |
741 | struct dev_context *devc; | |
742 | int ret; | |
743 | ||
744 | serial = NULL; | |
745 | sdi = NULL; | |
746 | devc = NULL; | |
747 | ||
748 | if (!(serial = serial_dev_new(options, "9600/8n1/rts=1/dtr=1"))) | |
749 | goto scan_cleanup; | |
750 | ||
751 | ret = serial_stream_check(serial, PACKET_SIZE, packet_valid, | |
752 | 3000, 9600); | |
753 | if (ret != SR_OK) | |
754 | goto scan_cleanup; | |
755 | ||
756 | sr_info("Found device on port %s.", serial->port); | |
757 | ||
758 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
759 | sdi->status = SR_ST_INACTIVE; | |
760 | sdi->vendor = g_strdup(vendor); | |
761 | sdi->model = g_strdup(model); | |
762 | devc = g_malloc0(sizeof(struct dev_context)); | |
763 | devc->buf = dev_buffer_new(PACKET_SIZE * 8); | |
764 | sdi->inst_type = SR_INST_SERIAL; | |
765 | sdi->conn = serial; | |
766 | sdi->priv = devc; | |
767 | ||
768 | if (setup_channels(sdi) != SR_OK) | |
769 | goto scan_cleanup; | |
770 | ||
771 | return sdi; | |
772 | ||
773 | scan_cleanup: | |
774 | es51919_serial_clean(devc); | |
775 | sr_dev_inst_free(sdi); | |
776 | sr_serial_dev_inst_free(serial); | |
777 | ||
778 | return NULL; | |
779 | } | |
780 | ||
781 | SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data, | |
782 | const struct sr_dev_inst *sdi, | |
783 | const struct sr_channel_group *cg) | |
784 | { | |
785 | struct dev_context *devc; | |
786 | ||
787 | (void)cg; | |
788 | ||
789 | devc = sdi->priv; | |
790 | ||
791 | switch (key) { | |
792 | case SR_CONF_OUTPUT_FREQUENCY: | |
793 | *data = g_variant_new_double(frequencies[devc->freq]); | |
794 | break; | |
795 | case SR_CONF_EQUIV_CIRCUIT_MODEL: | |
796 | *data = g_variant_new_string(models[devc->model]); | |
797 | break; | |
798 | default: | |
799 | return SR_ERR_NA; | |
800 | } | |
801 | ||
802 | return SR_OK; | |
803 | } | |
804 | ||
805 | SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data, | |
806 | const struct sr_dev_inst *sdi, | |
807 | const struct sr_channel_group *cg) | |
808 | { | |
809 | struct dev_context *devc; | |
810 | uint64_t val; | |
811 | ||
812 | (void)cg; | |
813 | ||
814 | if (!(devc = sdi->priv)) | |
815 | return SR_ERR_BUG; | |
816 | ||
817 | switch (key) { | |
818 | case SR_CONF_LIMIT_MSEC: | |
819 | val = g_variant_get_uint64(data); | |
820 | dev_time_limit_set(&devc->time_count, val); | |
821 | sr_dbg("Setting time limit to %" PRIu64 ".", val); | |
822 | break; | |
823 | case SR_CONF_LIMIT_FRAMES: | |
824 | val = g_variant_get_uint64(data); | |
825 | dev_limit_counter_limit_set(&devc->frame_count, val); | |
826 | sr_dbg("Setting frame limit to %" PRIu64 ".", val); | |
827 | break; | |
828 | default: | |
829 | sr_spew("%s: Unsupported key %u", __func__, key); | |
830 | return SR_ERR_NA; | |
831 | } | |
832 | ||
833 | return SR_OK; | |
834 | } | |
835 | ||
836 | static const uint32_t scanopts[] = { | |
837 | SR_CONF_CONN, | |
838 | SR_CONF_SERIALCOMM, | |
839 | }; | |
840 | ||
841 | static const uint32_t drvopts[] = { | |
842 | SR_CONF_LCRMETER, | |
843 | }; | |
844 | ||
845 | static const uint32_t devopts[] = { | |
846 | SR_CONF_CONTINUOUS, | |
847 | SR_CONF_LIMIT_FRAMES | SR_CONF_SET, | |
848 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
849 | SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST, | |
850 | SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST, | |
851 | }; | |
852 | ||
853 | SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data, | |
854 | const struct sr_dev_inst *sdi, | |
855 | const struct sr_channel_group *cg) | |
856 | { | |
857 | switch (key) { | |
858 | case SR_CONF_SCAN_OPTIONS: | |
859 | case SR_CONF_DEVICE_OPTIONS: | |
860 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
861 | case SR_CONF_OUTPUT_FREQUENCY: | |
862 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_DOUBLE, | |
863 | ARRAY_AND_SIZE(frequencies), sizeof(double)); | |
864 | break; | |
865 | case SR_CONF_EQUIV_CIRCUIT_MODEL: | |
866 | *data = g_variant_new_strv(ARRAY_AND_SIZE(models)); | |
867 | break; | |
868 | default: | |
869 | return SR_ERR_NA; | |
870 | } | |
871 | ||
872 | return SR_OK; | |
873 | } | |
874 | ||
875 | SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi) | |
876 | { | |
877 | struct dev_context *devc; | |
878 | struct sr_serial_dev_inst *serial; | |
879 | ||
880 | if (!(devc = sdi->priv)) | |
881 | return SR_ERR_BUG; | |
882 | ||
883 | dev_limit_counter_start(&devc->frame_count); | |
884 | dev_time_counter_start(&devc->time_count); | |
885 | ||
886 | std_session_send_df_header(sdi); | |
887 | ||
888 | serial = sdi->conn; | |
889 | serial_source_add(sdi->session, serial, G_IO_IN, 50, | |
890 | receive_data, (void *)sdi); | |
891 | ||
892 | return SR_OK; | |
893 | } |