aboutsummaryrefslogtreecommitdiff
path: root/src/lcr/es51919.c
blob: 8d9d9d96476c8e22870baeb06eb4219936484f2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/*
 * This file is part of the libsigrok project.
 *
 * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <glib.h>
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"

#define LOG_PREFIX "es51919"

struct dev_buffer {
	/** Total size of the buffer. */
	size_t size;
	/** Amount of data currently in the buffer. */
	size_t len;
	/** Offset where the data starts in the buffer. */
	size_t offset;
	/** Space for the data. */
	uint8_t data[];
};

static struct dev_buffer *dev_buffer_new(size_t size)
{
	struct dev_buffer *dbuf;

	dbuf = g_malloc0(sizeof(struct dev_buffer) + size);
	dbuf->size = size;
	dbuf->len = 0;
	dbuf->offset = 0;

	return dbuf;
}

static void dev_buffer_destroy(struct dev_buffer *dbuf)
{
	g_free(dbuf);
}

static int dev_buffer_fill_serial(struct dev_buffer *dbuf,
				  struct sr_dev_inst *sdi)
{
	struct sr_serial_dev_inst *serial;
	int len;

	serial = sdi->conn;

	/* If we already have data, move it to the beginning of the buffer. */
	if (dbuf->len > 0 && dbuf->offset > 0)
		memmove(dbuf->data, dbuf->data + dbuf->offset, dbuf->len);

	dbuf->offset = 0;

	len = dbuf->size - dbuf->len;
	len = serial_read_nonblocking(serial, dbuf->data + dbuf->len, len);
	if (len < 0) {
		sr_err("Serial port read error: %d.", len);
		return len;
	}

	dbuf->len += len;

	return SR_OK;
}

static uint8_t *dev_buffer_packet_find(struct dev_buffer *dbuf,
				gboolean (*packet_valid)(const uint8_t *),
				size_t packet_size)
{
	size_t offset;

	while (dbuf->len >= packet_size) {
		if (packet_valid(dbuf->data + dbuf->offset)) {
			offset = dbuf->offset;
			dbuf->offset += packet_size;
			dbuf->len -= packet_size;
			return dbuf->data + offset;
		}
		dbuf->offset++;
		dbuf->len--;
	}

	return NULL;
}

struct dev_limit_counter {
	/** The current number of received samples/frames/etc. */
	uint64_t count;
	/** The limit (in number of samples/frames/etc.). */
	uint64_t limit;
};

static void dev_limit_counter_start(struct dev_limit_counter *cnt)
{
	cnt->count = 0;
}

static void dev_limit_counter_inc(struct dev_limit_counter *cnt)
{
	cnt->count++;
}

static void dev_limit_counter_limit_set(struct dev_limit_counter *cnt,
					uint64_t limit)
{
	cnt->limit = limit;
}

static gboolean dev_limit_counter_limit_reached(struct dev_limit_counter *cnt)
{
	if (cnt->limit && cnt->count >= cnt->limit) {
		sr_info("Requested counter limit reached.");
		return TRUE;
	}

	return FALSE;
}

struct dev_time_counter {
	/** The starting time of current sampling run. */
	int64_t starttime;
	/** The time limit (in milliseconds). */
	uint64_t limit;
};

static void dev_time_counter_start(struct dev_time_counter *cnt)
{
	cnt->starttime = g_get_monotonic_time();
}

static void dev_time_limit_set(struct dev_time_counter *cnt, uint64_t limit)
{
	cnt->limit = limit;
}

static gboolean dev_time_limit_reached(struct dev_time_counter *cnt)
{
	int64_t time;

	if (cnt->limit) {
		time = (g_get_monotonic_time() - cnt->starttime) / 1000;
		if (time > (int64_t)cnt->limit) {
			sr_info("Requested time limit reached.");
			return TRUE;
		}
	}

	return FALSE;
}

static void serial_conf_get(GSList *options, const char *def_serialcomm,
			    const char **conn, const char **serialcomm)
{
	struct sr_config *src;
	GSList *l;

	*conn = *serialcomm = NULL;
	for (l = options; l; l = l->next) {
		src = l->data;
		switch (src->key) {
		case SR_CONF_CONN:
			*conn = g_variant_get_string(src->data, NULL);
			break;
		case SR_CONF_SERIALCOMM:
			*serialcomm = g_variant_get_string(src->data, NULL);
			break;
		}
	}

	if (*serialcomm == NULL)
		*serialcomm = def_serialcomm;
}

static struct sr_serial_dev_inst *serial_dev_new(GSList *options,
						 const char *def_serialcomm)

{
	const char *conn, *serialcomm;

	serial_conf_get(options, def_serialcomm, &conn, &serialcomm);

	if (!conn)
		return NULL;

	return sr_serial_dev_inst_new(conn, serialcomm);
}

static int serial_stream_check_buf(struct sr_serial_dev_inst *serial,
				   uint8_t *buf, size_t buflen,
				   size_t packet_size,
				   packet_valid_callback is_valid,
				   uint64_t timeout_ms, int baudrate)
{
	size_t len, dropped;
	int ret;

	if ((ret = serial_open(serial, SERIAL_RDWR)) != SR_OK)
		return ret;

	serial_flush(serial);

	len = buflen;
	ret = serial_stream_detect(serial, buf, &len, packet_size,
				   is_valid, timeout_ms, baudrate);

	serial_close(serial);

	if (ret != SR_OK)
		return ret;

	/*
	 * If we dropped more than two packets worth of data, something is
	 * wrong. We shouldn't quit however, since the dropped bytes might be
	 * just zeroes at the beginning of the stream. Those can occur as a
	 * combination of the nonstandard cable that ships with some devices
	 * and the serial port or USB to serial adapter.
	 */
	dropped = len - packet_size;
	if (dropped > 2 * packet_size)
		sr_warn("Had to drop too much data.");

	return SR_OK;
}

static int serial_stream_check(struct sr_serial_dev_inst *serial,
			       size_t packet_size,
			       packet_valid_callback is_valid,
			       uint64_t timeout_ms, int baudrate)
{
	uint8_t buf[128];

	return serial_stream_check_buf(serial, buf, sizeof(buf), packet_size,
				       is_valid, timeout_ms, baudrate);
}

struct std_opt_desc {
	const uint32_t *scanopts;
	const int num_scanopts;
	const uint32_t *devopts;
	const int num_devopts;
};

static int std_config_list(uint32_t key, GVariant **data,
			   const struct std_opt_desc *d)
{
	switch (key) {
	case SR_CONF_SCAN_OPTIONS:
		*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
			d->scanopts, d->num_scanopts, sizeof(uint32_t));
		break;
	case SR_CONF_DEVICE_OPTIONS:
		*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
			d->devopts, d->num_devopts, sizeof(uint32_t));
		break;
	default:
		return SR_ERR_NA;
	}

	return SR_OK;
}

static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg)
{
	struct sr_datafeed_packet packet;
	struct sr_datafeed_meta meta;

	memset(&meta, 0, sizeof(meta));

	packet.type = SR_DF_META;
	packet.payload = &meta;

	meta.config = g_slist_append(meta.config, cfg);

	return sr_session_send(sdi, &packet);
}

static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key,
				  GVariant *var)
{
	struct sr_config *cfg;
	int ret;

	cfg = sr_config_new(key, var);
	if (!cfg)
		return SR_ERR;

	ret = send_config_update(sdi, cfg);
	sr_config_free(cfg);

	return ret;
}

/*
 * Cyrustek ES51919 LCR chipset host protocol.
 *
 * Public official documentation does not contain the protocol
 * description, so this is all based on reverse engineering.
 *
 * Packet structure (17 bytes):
 *
 * 0x00: header1 ?? (0x00)
 * 0x01: header2 ?? (0x0d)
 *
 * 0x02: flags
 *         bit 0 = hold enabled
 *         bit 1 = reference shown (in delta mode)
 *         bit 2 = delta mode
 *         bit 3 = calibration mode
 *         bit 4 = sorting mode
 *         bit 5 = LCR mode
 *         bit 6 = auto mode
 *         bit 7 = parallel measurement (vs. serial)
 *
 * 0x03: config
 *         bit 0-4 = ??? (0x10)
 *         bit 5-7 = test frequency
 *                     0 = 100 Hz
 *                     1 = 120 Hz
 *                     2 = 1 kHz
 *                     3 = 10 kHz
 *                     4 = 100 kHz
 *                     5 = 0 Hz (DC)
 *
 * 0x04: tolerance (sorting mode)
 *         0 = not set
 *         3 = +-0.25%
 *         4 = +-0.5%
 *         5 = +-1%
 *         6 = +-2%
 *         7 = +-5%
 *         8 = +-10%
 *         9 = +-20%
 *        10 = -20+80%
 *
 * 0x05-0x09: primary measurement
 *   0x05: measured quantity
 *           1 = inductance
 *           2 = capacitance
 *           3 = resistance
 *           4 = DC resistance
 *   0x06: measurement MSB  (0x4e20 = 20000 = outside limits)
 *   0x07: measurement LSB
 *   0x08: measurement info
 *           bit 0-2 = decimal point multiplier (10^-val)
 *           bit 3-7 = unit
 *                       0 = no unit
 *                       1 = Ohm
 *                       2 = kOhm
 *                       3 = MOhm
 *                       5 = uH
 *                       6 = mH
 *                       7 = H
 *                       8 = kH
 *                       9 = pF
 *                       10 = nF
 *                       11 = uF
 *                       12 = mF
 *                       13 = %
 *                       14 = degree
 *   0x09: measurement status
 *           bit 0-3 = status
 *                       0 = normal (measurement shown)
 *                       1 = blank (nothing shown)
 *                       2 = lines ("----")
 *                       3 = outside limits ("OL")
 *                       7 = pass ("PASS")
 *                       8 = fail ("FAIL")
 *                       9 = open ("OPEn")
 *                      10 = shorted ("Srt")
 *           bit 4-6 = ??? (maybe part of same field with 0-3)
 *           bit 7   = ??? (some independent flag)
 *
 * 0x0a-0x0e: secondary measurement
 *   0x0a: measured quantity
 *           0 = none
 *           1 = dissipation factor
 *           2 = quality factor
 *           3 = parallel AC resistance / ESR
 *           4 = phase angle
 *   0x0b-0x0e: like primary measurement
 *
 * 0x0f: footer1 (0x0d) ?
 * 0x10: footer2 (0x0a) ?
 */

#define PACKET_SIZE 17

static const double frequencies[] = {
	100, 120, 1000, 10000, 100000, 0,
};

enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, };

static const char *const models[] = {
	"NONE", "PARALLEL", "SERIES", "AUTO",
};

/** Private, per-device-instance driver context. */
struct dev_context {
	/** The number of frames. */
	struct dev_limit_counter frame_count;

	/** The time limit counter. */
	struct dev_time_counter time_count;

	/** Data buffer. */
	struct dev_buffer *buf;

	/** The frequency of the test signal (index to frequencies[]). */
	unsigned int freq;

	/** Equivalent circuit model (index to models[]). */
	unsigned int model;
};

static const uint8_t *pkt_to_buf(const uint8_t *pkt, int is_secondary)
{
	return is_secondary ? pkt + 10 : pkt + 5;
}

static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel)
{
	const uint8_t *buf;

	buf = pkt_to_buf(pkt, is_secondary);

	switch (is_secondary << 8 | buf[0]) {
	case 0x001:
		return is_parallel ?
			SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE;
	case 0x002:
		return is_parallel ?
			SR_MQ_PARALLEL_CAPACITANCE : SR_MQ_SERIES_CAPACITANCE;
	case 0x003:
	case 0x103:
		return is_parallel ?
			SR_MQ_PARALLEL_RESISTANCE : SR_MQ_SERIES_RESISTANCE;
	case 0x004:
		return SR_MQ_RESISTANCE;
	case 0x100:
		return SR_MQ_DIFFERENCE;
	case 0x101:
		return SR_MQ_DISSIPATION_FACTOR;
	case 0x102:
		return SR_MQ_QUALITY_FACTOR;
	case 0x104:
		return SR_MQ_PHASE_ANGLE;
	}

	sr_err("Unknown quantity 0x%03x.", is_secondary << 8 | buf[0]);

	return 0;
}

static float parse_value(const uint8_t *buf, int *digits)
{
	static const int exponents[] = {0, -1, -2, -3, -4, -5, -6, -7};
	int exponent;
	int16_t val;

	exponent = exponents[buf[3] & 7];
	*digits = -exponent;
	val = (buf[1] << 8) | buf[2];
	return (float)val * powf(10, exponent);
}

static void parse_measurement(const uint8_t *pkt, float *floatval,
			      struct sr_datafeed_analog *analog,
			      int is_secondary)
{
	static const struct {
		int unit;
		int exponent;
	} units[] = {
		{ SR_UNIT_UNITLESS,   0 }, /* no unit */
		{ SR_UNIT_OHM,        0 }, /* Ohm */
		{ SR_UNIT_OHM,        3 }, /* kOhm */
		{ SR_UNIT_OHM,        6 }, /* MOhm */
		{ -1,                 0 }, /* ??? */
		{ SR_UNIT_HENRY,     -6 }, /* uH */
		{ SR_UNIT_HENRY,     -3 }, /* mH */
		{ SR_UNIT_HENRY,      0 }, /* H */
		{ SR_UNIT_HENRY,      3 }, /* kH */
		{ SR_UNIT_FARAD,    -12 }, /* pF */
		{ SR_UNIT_FARAD,     -9 }, /* nF */
		{ SR_UNIT_FARAD,     -6 }, /* uF */
		{ SR_UNIT_FARAD,     -3 }, /* mF */
		{ SR_UNIT_PERCENTAGE, 0 }, /* % */
		{ SR_UNIT_DEGREE,     0 }, /* degree */
	};
	const uint8_t *buf;
	int digits, exponent;
	int state;

	buf = pkt_to_buf(pkt, is_secondary);

	analog->meaning->mq = 0;
	analog->meaning->mqflags = 0;

	state = buf[4] & 0xf;

	if (state != 0 && state != 3)
		return;

	if (pkt[2] & 0x18) {
		/* Calibration and Sorting modes not supported. */
		return;
	}

	if (!is_secondary) {
		if (pkt[2] & 0x01)
			analog->meaning->mqflags |= SR_MQFLAG_HOLD;
		if (pkt[2] & 0x02)
			analog->meaning->mqflags |= SR_MQFLAG_REFERENCE;
	} else {
		if (pkt[2] & 0x04)
			analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
	}

	if ((analog->meaning->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) == 0)
		return;

	if ((buf[3] >> 3) >= ARRAY_SIZE(units)) {
		sr_err("Unknown unit %u.", buf[3] >> 3);
		analog->meaning->mq = 0;
		return;
	}

	analog->meaning->unit = units[buf[3] >> 3].unit;

	exponent = units[buf[3] >> 3].exponent;
	*floatval = parse_value(buf, &digits);
	*floatval *= (state == 0) ? powf(10, exponent) : INFINITY;
	analog->encoding->digits = digits - exponent;
	analog->spec->spec_digits = digits - exponent;
}

static unsigned int parse_freq(const uint8_t *pkt)
{
	unsigned int freq;

	freq = pkt[3] >> 5;

	if (freq >= ARRAY_SIZE(frequencies)) {
		sr_err("Unknown frequency %u.", freq);
		freq = ARRAY_SIZE(frequencies) - 1;
	}

	return freq;
}

static unsigned int parse_model(const uint8_t *pkt)
{
	if (pkt[2] & 0x40)
		return MODEL_AUTO;
	else if (parse_mq(pkt, 0, 0) == SR_MQ_RESISTANCE)
		return MODEL_NONE;
	else if (pkt[2] & 0x80)
		return MODEL_PAR;
	else
		return MODEL_SER;
}

static gboolean packet_valid(const uint8_t *pkt)
{
	/*
	 * If the first two bytes of the packet are indeed a constant
	 * header, they should be checked too. Since we don't know it
	 * for sure, we'll just check the last two for now since they
	 * seem to be constant just like in the other Cyrustek chipset
	 * protocols.
	 */
	if (pkt[15] == 0xd && pkt[16] == 0xa)
		return TRUE;

	return FALSE;
}

static int do_config_update(struct sr_dev_inst *sdi, uint32_t key,
			    GVariant *var)
{
	return send_config_update_key(sdi, key, var);
}

static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq)
{
	return do_config_update(sdi, SR_CONF_OUTPUT_FREQUENCY,
				g_variant_new_double(frequencies[freq]));
}

static int send_model_update(struct sr_dev_inst *sdi, unsigned int model)
{
	return do_config_update(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL,
				g_variant_new_string(models[model]));
}

static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
{
	struct sr_datafeed_packet packet;
	struct sr_datafeed_analog analog;
	struct sr_analog_encoding encoding;
	struct sr_analog_meaning meaning;
	struct sr_analog_spec spec;
	struct dev_context *devc;
	unsigned int val;
	float floatval;
	gboolean frame;

	devc = sdi->priv;

	val = parse_freq(pkt);
	if (val != devc->freq) {
		if (send_freq_update(sdi, val) == SR_OK)
			devc->freq = val;
		else
			return;
	}

	val = parse_model(pkt);
	if (val != devc->model) {
		if (send_model_update(sdi, val) == SR_OK)
			devc->model = val;
		else
			return;
	}

	frame = FALSE;

	/* Note: digits/spec_digits will be overridden later. */
	sr_analog_init(&analog, &encoding, &meaning, &spec, 0);

	analog.num_samples = 1;
	analog.data = &floatval;

	analog.meaning->channels = g_slist_append(NULL, sdi->channels->data);

	parse_measurement(pkt, &floatval, &analog, 0);
	if (analog.meaning->mq != 0) {
		if (!frame) {
			packet.type = SR_DF_FRAME_BEGIN;
			sr_session_send(sdi, &packet);
			frame = TRUE;
		}

		packet.type = SR_DF_ANALOG;
		packet.payload = &analog;

		sr_session_send(sdi, &packet);
	}

	g_slist_free(analog.meaning->channels);
	analog.meaning->channels = g_slist_append(NULL, sdi->channels->next->data);

	parse_measurement(pkt, &floatval, &analog, 1);
	if (analog.meaning->mq != 0) {
		if (!frame) {
			packet.type = SR_DF_FRAME_BEGIN;
			sr_session_send(sdi, &packet);
			frame = TRUE;
		}

		packet.type = SR_DF_ANALOG;
		packet.payload = &analog;

		sr_session_send(sdi, &packet);
	}

	g_slist_free(analog.meaning->channels);

	if (frame) {
		packet.type = SR_DF_FRAME_END;
		sr_session_send(sdi, &packet);
		dev_limit_counter_inc(&devc->frame_count);
	}
}

static int handle_new_data(struct sr_dev_inst *sdi)
{
	struct dev_context *devc;
	uint8_t *pkt;
	int ret;

	devc = sdi->priv;

	ret = dev_buffer_fill_serial(devc->buf, sdi);
	if (ret < 0)
		return ret;

	while ((pkt = dev_buffer_packet_find(devc->buf, packet_valid,
					     PACKET_SIZE)))
		handle_packet(sdi, pkt);

	return SR_OK;
}

static int receive_data(int fd, int revents, void *cb_data)
{
	struct sr_dev_inst *sdi;
	struct dev_context *devc;

	(void)fd;

	if (!(sdi = cb_data))
		return TRUE;

	if (!(devc = sdi->priv))
		return TRUE;

	if (revents == G_IO_IN) {
		/* Serial data arrived. */
		handle_new_data(sdi);
	}

	if (dev_limit_counter_limit_reached(&devc->frame_count) ||
	    dev_time_limit_reached(&devc->time_count))
		sdi->driver->dev_acquisition_stop(sdi);

	return TRUE;
}

static const char *const channel_names[] = { "P1", "P2" };

static int setup_channels(struct sr_dev_inst *sdi)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(channel_names); i++)
		sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);

	return SR_OK;
}

SR_PRIV void es51919_serial_clean(void *priv)
{
	struct dev_context *devc;

	if (!(devc = priv))
		return;

	dev_buffer_destroy(devc->buf);
	g_free(devc);
}

SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
						const char *vendor,
						const char *model)
{
	struct sr_serial_dev_inst *serial;
	struct sr_dev_inst *sdi;
	struct dev_context *devc;
	int ret;

	serial = NULL;
	sdi = NULL;
	devc = NULL;

	if (!(serial = serial_dev_new(options, "9600/8n1/rts=1/dtr=1")))
		goto scan_cleanup;

	ret = serial_stream_check(serial, PACKET_SIZE, packet_valid,
				  3000, 9600);
	if (ret != SR_OK)
		goto scan_cleanup;

	sr_info("Found device on port %s.", serial->port);

	sdi = g_malloc0(sizeof(struct sr_dev_inst));
	sdi->status = SR_ST_INACTIVE;
	sdi->vendor = g_strdup(vendor);
	sdi->model = g_strdup(model);
	devc = g_malloc0(sizeof(struct dev_context));
	devc->buf = dev_buffer_new(PACKET_SIZE * 8);
	sdi->inst_type = SR_INST_SERIAL;
	sdi->conn = serial;
	sdi->priv = devc;

	if (setup_channels(sdi) != SR_OK)
		goto scan_cleanup;

	return sdi;

scan_cleanup:
	es51919_serial_clean(devc);
	sr_dev_inst_free(sdi);
	if (serial)
		sr_serial_dev_inst_free(serial);

	return NULL;
}

SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data,
				      const struct sr_dev_inst *sdi,
				      const struct sr_channel_group *cg)
{
	struct dev_context *devc;

	(void)cg;

	devc = sdi->priv;

	switch (key) {
	case SR_CONF_OUTPUT_FREQUENCY:
		*data = g_variant_new_double(frequencies[devc->freq]);
		break;
	case SR_CONF_EQUIV_CIRCUIT_MODEL:
		*data = g_variant_new_string(models[devc->model]);
		break;
	default:
		return SR_ERR_NA;
	}

	return SR_OK;
}

SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data,
				      const struct sr_dev_inst *sdi,
				      const struct sr_channel_group *cg)
{
	struct dev_context *devc;
	uint64_t val;

	(void)cg;

	if (!(devc = sdi->priv))
		return SR_ERR_BUG;

	switch (key) {
	case SR_CONF_LIMIT_MSEC:
		val = g_variant_get_uint64(data);
		dev_time_limit_set(&devc->time_count, val);
		sr_dbg("Setting time limit to %" PRIu64 ".", val);
		break;
	case SR_CONF_LIMIT_FRAMES:
		val = g_variant_get_uint64(data);
		dev_limit_counter_limit_set(&devc->frame_count, val);
		sr_dbg("Setting frame limit to %" PRIu64 ".", val);
		break;
	default:
		sr_spew("%s: Unsupported key %u", __func__, key);
		return SR_ERR_NA;
	}

	return SR_OK;
}

static const uint32_t scanopts[] = {
	SR_CONF_CONN,
	SR_CONF_SERIALCOMM,
};

static const uint32_t devopts[] = {
	SR_CONF_LCRMETER,
	SR_CONF_CONTINUOUS,
	SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
	SR_CONF_LIMIT_MSEC | SR_CONF_SET,
	SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST,
	SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST,
};

static const struct std_opt_desc opts = {
	scanopts, ARRAY_SIZE(scanopts),
	devopts, ARRAY_SIZE(devopts),
};

SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data,
				       const struct sr_dev_inst *sdi,
				       const struct sr_channel_group *cg)
{
	(void)sdi;
	(void)cg;

	if (std_config_list(key, data, &opts) == SR_OK)
		return SR_OK;

	switch (key) {
	case SR_CONF_OUTPUT_FREQUENCY:
		*data = g_variant_new_fixed_array(G_VARIANT_TYPE_DOUBLE,
			frequencies, ARRAY_SIZE(frequencies), sizeof(double));
		break;
	case SR_CONF_EQUIV_CIRCUIT_MODEL:
		*data = g_variant_new_strv(models, ARRAY_SIZE(models));
		break;
	default:
		return SR_ERR_NA;
	}

	return SR_OK;
}

SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi)
{
	struct dev_context *devc;
	struct sr_serial_dev_inst *serial;

	if (sdi->status != SR_ST_ACTIVE)
		return SR_ERR_DEV_CLOSED;

	if (!(devc = sdi->priv))
		return SR_ERR_BUG;

	dev_limit_counter_start(&devc->frame_count);
	dev_time_counter_start(&devc->time_count);

	std_session_send_df_header(sdi);

	/* Poll every 50ms, or whenever some data comes in. */
	serial = sdi->conn;
	serial_source_add(sdi->session, serial, G_IO_IN, 50,
			  receive_data, (void *)sdi);

	return SR_OK;
}