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