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