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