]> sigrok.org Git - libsigrok.git/blob - src/lcr/es51919.c
deree-de5000: Drop SR_CONF_MEASURED_QUANTITY functionality.
[libsigrok.git] / src / lcr / es51919.c
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/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         dbuf = g_malloc0(sizeof(struct dev_buffer) + size);
45         dbuf->size = size;
46         dbuf->len = 0;
47         dbuf->offset = 0;
48
49         return dbuf;
50 }
51
52 static void dev_buffer_destroy(struct dev_buffer *dbuf)
53 {
54         g_free(dbuf);
55 }
56
57 static 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
83 static 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
103 struct dev_limit_counter {
104         /** The current number of received samples/frames/etc. */
105         uint64_t count;
106         /** The limit (in number of samples/frames/etc.). */
107         uint64_t limit;
108 };
109
110 static void dev_limit_counter_start(struct dev_limit_counter *cnt)
111 {
112         cnt->count = 0;
113 }
114
115 static void dev_limit_counter_inc(struct dev_limit_counter *cnt)
116 {
117         cnt->count++;
118 }
119
120 static void dev_limit_counter_limit_set(struct dev_limit_counter *cnt,
121                                         uint64_t limit)
122 {
123         cnt->limit = limit;
124 }
125
126 static gboolean dev_limit_counter_limit_reached(struct dev_limit_counter *cnt)
127 {
128         if (cnt->limit && cnt->count >= cnt->limit) {
129                 sr_info("Requested counter limit reached.");
130                 return TRUE;
131         }
132
133         return FALSE;
134 }
135
136 struct dev_time_counter {
137         /** The starting time of current sampling run. */
138         int64_t starttime;
139         /** The time limit (in milliseconds). */
140         uint64_t limit;
141 };
142
143 static void dev_time_counter_start(struct dev_time_counter *cnt)
144 {
145         cnt->starttime = g_get_monotonic_time();
146 }
147
148 static void dev_time_limit_set(struct dev_time_counter *cnt, uint64_t limit)
149 {
150         cnt->limit = limit;
151 }
152
153 static gboolean dev_time_limit_reached(struct dev_time_counter *cnt)
154 {
155         int64_t time;
156
157         if (cnt->limit) {
158                 time = (g_get_monotonic_time() - cnt->starttime) / 1000;
159                 if (time > (int64_t)cnt->limit) {
160                         sr_info("Requested time limit reached.");
161                         return TRUE;
162                 }
163         }
164
165         return FALSE;
166 }
167
168 static void serial_conf_get(GSList *options, const char *def_serialcomm,
169                             const char **conn, const char **serialcomm)
170 {
171         struct sr_config *src;
172         GSList *l;
173
174         *conn = *serialcomm = NULL;
175         for (l = options; l; l = l->next) {
176                 src = l->data;
177                 switch (src->key) {
178                 case SR_CONF_CONN:
179                         *conn = g_variant_get_string(src->data, NULL);
180                         break;
181                 case SR_CONF_SERIALCOMM:
182                         *serialcomm = g_variant_get_string(src->data, NULL);
183                         break;
184                 }
185         }
186
187         if (*serialcomm == NULL)
188                 *serialcomm = def_serialcomm;
189 }
190
191 static struct sr_serial_dev_inst *serial_dev_new(GSList *options,
192                                                  const char *def_serialcomm)
193
194 {
195         const char *conn, *serialcomm;
196
197         serial_conf_get(options, def_serialcomm, &conn, &serialcomm);
198
199         if (!conn)
200                 return NULL;
201
202         return sr_serial_dev_inst_new(conn, serialcomm);
203 }
204
205 static int serial_stream_check_buf(struct sr_serial_dev_inst *serial,
206                                    uint8_t *buf, size_t buflen,
207                                    size_t packet_size,
208                                    packet_valid_callback is_valid,
209                                    uint64_t timeout_ms, int baudrate)
210 {
211         size_t len, dropped;
212         int ret;
213
214         if ((ret = serial_open(serial, SERIAL_RDWR)) != SR_OK)
215                 return ret;
216
217         serial_flush(serial);
218
219         len = buflen;
220         ret = serial_stream_detect(serial, buf, &len, packet_size,
221                                    is_valid, timeout_ms, baudrate);
222
223         serial_close(serial);
224
225         if (ret != SR_OK)
226                 return ret;
227
228         /*
229          * If we dropped more than two packets worth of data, something is
230          * wrong. We shouldn't quit however, since the dropped bytes might be
231          * just zeroes at the beginning of the stream. Those can occur as a
232          * combination of the nonstandard cable that ships with some devices
233          * and the serial port or USB to serial adapter.
234          */
235         dropped = len - packet_size;
236         if (dropped > 2 * packet_size)
237                 sr_warn("Had to drop too much data.");
238
239         return SR_OK;
240 }
241
242 static int serial_stream_check(struct sr_serial_dev_inst *serial,
243                                size_t packet_size,
244                                packet_valid_callback is_valid,
245                                uint64_t timeout_ms, int baudrate)
246 {
247         uint8_t buf[128];
248
249         return serial_stream_check_buf(serial, buf, sizeof(buf), packet_size,
250                                        is_valid, timeout_ms, baudrate);
251 }
252
253 struct std_opt_desc {
254         const uint32_t *scanopts;
255         const int num_scanopts;
256         const uint32_t *devopts;
257         const int num_devopts;
258 };
259
260 static int std_config_list(uint32_t key, GVariant **data,
261                            const struct std_opt_desc *d)
262 {
263         switch (key) {
264         case SR_CONF_SCAN_OPTIONS:
265                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
266                         d->scanopts, d->num_scanopts, sizeof(uint32_t));
267                 break;
268         case SR_CONF_DEVICE_OPTIONS:
269                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
270                         d->devopts, d->num_devopts, sizeof(uint32_t));
271                 break;
272         default:
273                 return SR_ERR_NA;
274         }
275
276         return SR_OK;
277 }
278
279 static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg)
280 {
281         struct sr_datafeed_packet packet;
282         struct sr_datafeed_meta meta;
283
284         memset(&meta, 0, sizeof(meta));
285
286         packet.type = SR_DF_META;
287         packet.payload = &meta;
288
289         meta.config = g_slist_append(meta.config, cfg);
290
291         return sr_session_send(sdi, &packet);
292 }
293
294 static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key,
295                                   GVariant *var)
296 {
297         struct sr_config *cfg;
298         int ret;
299
300         cfg = sr_config_new(key, var);
301         if (!cfg)
302                 return SR_ERR;
303
304         ret = send_config_update(sdi, cfg);
305         sr_config_free(cfg);
306
307         return ret;
308 }
309
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 ("----")
382  *                       3 = outside limits ("OL")
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
405 static const double frequencies[] = {
406         100, 120, 1000, 10000, 100000, 0,
407 };
408
409 enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, };
410
411 static const char *const models[] = {
412         "NONE", "PARALLEL", "SERIES", "AUTO",
413 };
414
415 /** Private, per-device-instance driver context. */
416 struct dev_context {
417         /** Opaque pointer passed in by the frontend. */
418         void *cb_data;
419
420         /** The number of frames. */
421         struct dev_limit_counter frame_count;
422
423         /** The time limit counter. */
424         struct dev_time_counter time_count;
425
426         /** Data buffer. */
427         struct dev_buffer *buf;
428
429         /** The frequency of the test signal (index to frequencies[]). */
430         unsigned int freq;
431
432         /** Equivalent circuit model (index to models[]). */
433         unsigned int model;
434 };
435
436 static const uint8_t *pkt_to_buf(const uint8_t *pkt, int is_secondary)
437 {
438         return is_secondary ? pkt + 10 : pkt + 5;
439 }
440
441 static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel)
442 {
443         const uint8_t *buf;
444
445         buf = pkt_to_buf(pkt, is_secondary);
446
447         switch (is_secondary << 8 | buf[0]) {
448         case 0x001:
449                 return is_parallel ?
450                         SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE;
451         case 0x002:
452                 return is_parallel ?
453                         SR_MQ_PARALLEL_CAPACITANCE : SR_MQ_SERIES_CAPACITANCE;
454         case 0x003:
455         case 0x103:
456                 return is_parallel ?
457                         SR_MQ_PARALLEL_RESISTANCE : SR_MQ_SERIES_RESISTANCE;
458         case 0x004:
459                 return SR_MQ_RESISTANCE;
460         case 0x100:
461                 return SR_MQ_DIFFERENCE;
462         case 0x101:
463                 return SR_MQ_DISSIPATION_FACTOR;
464         case 0x102:
465                 return SR_MQ_QUALITY_FACTOR;
466         case 0x104:
467                 return SR_MQ_PHASE_ANGLE;
468         }
469
470         sr_err("Unknown quantity 0x%03x.", is_secondary << 8 | buf[0]);
471
472         return -1;
473 }
474
475 static float parse_value(const uint8_t *buf)
476 {
477         static const float decimals[] = {
478                 1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7
479         };
480         int16_t val;
481
482         val = (buf[1] << 8) | buf[2];
483         return (float)val * decimals[buf[3] & 7];
484 }
485
486 static void parse_measurement(const uint8_t *pkt, float *floatval,
487                               struct sr_datafeed_analog *analog,
488                               int is_secondary)
489 {
490         static const struct {
491                 int unit;
492                 float mult;
493         } units[] = {
494                 { SR_UNIT_UNITLESS, 1 },        /* no unit */
495                 { SR_UNIT_OHM, 1 },             /* Ohm     */
496                 { SR_UNIT_OHM, 1e3 },           /* kOhm    */
497                 { SR_UNIT_OHM, 1e6 },           /* MOhm    */
498                 { -1, 0 },                      /* ???     */
499                 { SR_UNIT_HENRY, 1e-6 },        /* uH      */
500                 { SR_UNIT_HENRY, 1e-3 },        /* mH      */
501                 { SR_UNIT_HENRY, 1 },           /* H       */
502                 { SR_UNIT_HENRY, 1e3 },         /* kH      */
503                 { SR_UNIT_FARAD, 1e-12 },       /* pF      */
504                 { SR_UNIT_FARAD, 1e-9 },        /* nF      */
505                 { SR_UNIT_FARAD, 1e-6 },        /* uF      */
506                 { SR_UNIT_FARAD, 1e-3 },        /* mF      */
507                 { SR_UNIT_PERCENTAGE, 1 },      /* %       */
508                 { SR_UNIT_DEGREE, 1 }           /* degree  */
509         };
510         const uint8_t *buf;
511         int state;
512
513         buf = pkt_to_buf(pkt, is_secondary);
514
515         analog->mq = -1;
516         analog->mqflags = 0;
517
518         state = buf[4] & 0xf;
519
520         if (state != 0 && state != 3)
521                 return;
522
523         if (pkt[2] & 0x18) {
524                 /* Calibration and Sorting modes not supported. */
525                 return;
526         }
527
528         if (!is_secondary) {
529                 if (pkt[2] & 0x01)
530                         analog->mqflags |= SR_MQFLAG_HOLD;
531                 if (pkt[2] & 0x02)
532                         analog->mqflags |= SR_MQFLAG_REFERENCE;
533         } else {
534                 if (pkt[2] & 0x04)
535                         analog->mqflags |= SR_MQFLAG_RELATIVE;
536         }
537
538         if ((analog->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) < 0)
539                 return;
540
541         if ((buf[3] >> 3) >= ARRAY_SIZE(units)) {
542                 sr_err("Unknown unit %u.", buf[3] >> 3);
543                 analog->mq = -1;
544                 return;
545         }
546
547         analog->unit = units[buf[3] >> 3].unit;
548
549         *floatval = parse_value(buf);
550         *floatval *= (state == 0) ? units[buf[3] >> 3].mult : INFINITY;
551 }
552
553 static unsigned int parse_freq(const uint8_t *pkt)
554 {
555         unsigned int freq;
556
557         freq = pkt[3] >> 5;
558
559         if (freq >= ARRAY_SIZE(frequencies)) {
560                 sr_err("Unknown frequency %u.", freq);
561                 freq = ARRAY_SIZE(frequencies) - 1;
562         }
563
564         return freq;
565 }
566
567 static unsigned int parse_model(const uint8_t *pkt)
568 {
569         if (pkt[2] & 0x40)
570                 return MODEL_AUTO;
571         else if (parse_mq(pkt, 0, 0) == SR_MQ_RESISTANCE)
572                 return MODEL_NONE;
573         else if (pkt[2] & 0x80)
574                 return MODEL_PAR;
575         else
576                 return MODEL_SER;
577 }
578
579 static gboolean packet_valid(const uint8_t *pkt)
580 {
581         /*
582          * If the first two bytes of the packet are indeed a constant
583          * header, they should be checked too. Since we don't know it
584          * for sure, we'll just check the last two for now since they
585          * seem to be constant just like in the other Cyrustek chipset
586          * protocols.
587          */
588         if (pkt[15] == 0xd && pkt[16] == 0xa)
589                 return TRUE;
590
591         return FALSE;
592 }
593
594 static int do_config_update(struct sr_dev_inst *sdi, uint32_t key,
595                             GVariant *var)
596 {
597         struct dev_context *devc;
598
599         devc = sdi->priv;
600
601         return send_config_update_key(devc->cb_data, key, var);
602 }
603
604 static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq)
605 {
606         return do_config_update(sdi, SR_CONF_OUTPUT_FREQUENCY,
607                                 g_variant_new_double(frequencies[freq]));
608 }
609
610 static int send_model_update(struct sr_dev_inst *sdi, unsigned int model)
611 {
612         return do_config_update(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL,
613                                 g_variant_new_string(models[model]));
614 }
615
616 static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
617 {
618         struct sr_datafeed_packet packet;
619         struct sr_datafeed_analog analog;
620         struct dev_context *devc;
621         unsigned int val;
622         float floatval;
623         gboolean frame;
624
625         devc = sdi->priv;
626
627         val = parse_freq(pkt);
628         if (val != devc->freq) {
629                 if (send_freq_update(sdi, val) == SR_OK)
630                         devc->freq = val;
631                 else
632                         return;
633         }
634
635         val = parse_model(pkt);
636         if (val != devc->model) {
637                 if (send_model_update(sdi, val) == SR_OK)
638                         devc->model = val;
639                 else
640                         return;
641         }
642
643         frame = FALSE;
644
645         memset(&analog, 0, sizeof(analog));
646
647         analog.num_samples = 1;
648         analog.data = &floatval;
649
650         analog.channels = g_slist_append(NULL, sdi->channels->data);
651
652         parse_measurement(pkt, &floatval, &analog, 0);
653         if (analog.mq >= 0) {
654                 if (!frame) {
655                         packet.type = SR_DF_FRAME_BEGIN;
656                         sr_session_send(devc->cb_data, &packet);
657                         frame = TRUE;
658                 }
659
660                 packet.type = SR_DF_ANALOG;
661                 packet.payload = &analog;
662
663                 sr_session_send(devc->cb_data, &packet);
664         }
665
666         g_slist_free(analog.channels);
667         analog.channels = g_slist_append(NULL, sdi->channels->next->data);
668
669         parse_measurement(pkt, &floatval, &analog, 1);
670         if (analog.mq >= 0) {
671                 if (!frame) {
672                         packet.type = SR_DF_FRAME_BEGIN;
673                         sr_session_send(devc->cb_data, &packet);
674                         frame = TRUE;
675                 }
676
677                 packet.type = SR_DF_ANALOG;
678                 packet.payload = &analog;
679
680                 sr_session_send(devc->cb_data, &packet);
681         }
682
683         g_slist_free(analog.channels);
684
685         if (frame) {
686                 packet.type = SR_DF_FRAME_END;
687                 sr_session_send(devc->cb_data, &packet);
688                 dev_limit_counter_inc(&devc->frame_count);
689         }
690 }
691
692 static int handle_new_data(struct sr_dev_inst *sdi)
693 {
694         struct dev_context *devc;
695         uint8_t *pkt;
696         int ret;
697
698         devc = sdi->priv;
699
700         ret = dev_buffer_fill_serial(devc->buf, sdi);
701         if (ret < 0)
702                 return ret;
703
704         while ((pkt = dev_buffer_packet_find(devc->buf, packet_valid,
705                                              PACKET_SIZE)))
706                 handle_packet(sdi, pkt);
707
708         return SR_OK;
709 }
710
711 static int receive_data(int fd, int revents, void *cb_data)
712 {
713         struct sr_dev_inst *sdi;
714         struct dev_context *devc;
715
716         (void)fd;
717
718         if (!(sdi = cb_data))
719                 return TRUE;
720
721         if (!(devc = sdi->priv))
722                 return TRUE;
723
724         if (revents == G_IO_IN) {
725                 /* Serial data arrived. */
726                 handle_new_data(sdi);
727         }
728
729         if (dev_limit_counter_limit_reached(&devc->frame_count) ||
730             dev_time_limit_reached(&devc->time_count))
731                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
732
733         return TRUE;
734 }
735
736 static const char *const channel_names[] = { "P1", "P2" };
737
738 static int setup_channels(struct sr_dev_inst *sdi)
739 {
740         unsigned int i;
741         int ret;
742
743         ret = SR_ERR_BUG;
744
745         for (i = 0; i < ARRAY_SIZE(channel_names); i++)
746                 sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);
747
748         return ret;
749 }
750
751 SR_PRIV void es51919_serial_clean(void *priv)
752 {
753         struct dev_context *devc;
754
755         if (!(devc = priv))
756                 return;
757
758         dev_buffer_destroy(devc->buf);
759         g_free(devc);
760 }
761
762 SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
763                                                 const char *vendor,
764                                                 const char *model)
765 {
766         struct sr_serial_dev_inst *serial;
767         struct sr_dev_inst *sdi;
768         struct dev_context *devc;
769         int ret;
770
771         serial = NULL;
772         sdi = NULL;
773         devc = NULL;
774
775         if (!(serial = serial_dev_new(options, "9600/8n1/rts=1/dtr=1")))
776                 goto scan_cleanup;
777
778         ret = serial_stream_check(serial, PACKET_SIZE, packet_valid,
779                                   3000, 9600);
780         if (ret != SR_OK)
781                 goto scan_cleanup;
782
783         sr_info("Found device on port %s.", serial->port);
784
785         sdi = g_malloc0(sizeof(struct sr_dev_inst));
786         sdi->status = SR_ST_INACTIVE;
787         sdi->vendor = g_strdup(vendor);
788         sdi->model = g_strdup(model);
789         devc = g_malloc0(sizeof(struct dev_context));
790         devc->buf = dev_buffer_new(PACKET_SIZE * 8);
791         sdi->inst_type = SR_INST_SERIAL;
792         sdi->conn = serial;
793         sdi->priv = devc;
794
795         if (setup_channels(sdi) != SR_OK)
796                 goto scan_cleanup;
797
798         return sdi;
799
800 scan_cleanup:
801         es51919_serial_clean(devc);
802         if (sdi)
803                 sr_dev_inst_free(sdi);
804         if (serial)
805                 sr_serial_dev_inst_free(serial);
806
807         return NULL;
808 }
809
810 SR_PRIV int es51919_serial_config_get(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
816         (void)cg;
817
818         if (!(devc = sdi->priv))
819                 return SR_ERR_BUG;
820
821         switch (key) {
822         case SR_CONF_OUTPUT_FREQUENCY:
823                 *data = g_variant_new_double(frequencies[devc->freq]);
824                 break;
825         case SR_CONF_EQUIV_CIRCUIT_MODEL:
826                 *data = g_variant_new_string(models[devc->model]);
827                 break;
828         default:
829                 sr_spew("%s: Unsupported key %u", __func__, key);
830                 return SR_ERR_NA;
831         }
832
833         return SR_OK;
834 }
835
836 SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data,
837                                       const struct sr_dev_inst *sdi,
838                                       const struct sr_channel_group *cg)
839 {
840         struct dev_context *devc;
841         uint64_t val;
842
843         (void)cg;
844
845         if (!(devc = sdi->priv))
846                 return SR_ERR_BUG;
847
848         switch (key) {
849         case SR_CONF_LIMIT_MSEC:
850                 val = g_variant_get_uint64(data);
851                 dev_time_limit_set(&devc->time_count, val);
852                 sr_dbg("Setting time limit to %" PRIu64 ".", val);
853                 break;
854         case SR_CONF_LIMIT_FRAMES:
855                 val = g_variant_get_uint64(data);
856                 dev_limit_counter_limit_set(&devc->frame_count, val);
857                 sr_dbg("Setting frame limit to %" PRIu64 ".", val);
858                 break;
859         default:
860                 sr_spew("%s: Unsupported key %u", __func__, key);
861                 return SR_ERR_NA;
862         }
863
864         return SR_OK;
865 }
866
867 static const uint32_t scanopts[] = {
868         SR_CONF_CONN,
869         SR_CONF_SERIALCOMM,
870 };
871
872 static const uint32_t devopts[] = {
873         SR_CONF_LCRMETER,
874         SR_CONF_CONTINUOUS,
875         SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
876         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
877         SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST,
878         SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST,
879 };
880
881 static const struct std_opt_desc opts = {
882         scanopts, ARRAY_SIZE(scanopts),
883         devopts, ARRAY_SIZE(devopts),
884 };
885
886 SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data,
887                                        const struct sr_dev_inst *sdi,
888                                        const struct sr_channel_group *cg)
889 {
890         (void)sdi;
891         (void)cg;
892
893         if (std_config_list(key, data, &opts) == SR_OK)
894                 return SR_OK;
895
896         switch (key) {
897         case SR_CONF_OUTPUT_FREQUENCY:
898                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_DOUBLE,
899                         frequencies, ARRAY_SIZE(frequencies), sizeof(double));
900                 break;
901         case SR_CONF_EQUIV_CIRCUIT_MODEL:
902                 *data = g_variant_new_strv(models, ARRAY_SIZE(models));
903                 break;
904         default:
905                 sr_spew("%s: Unsupported key %u", __func__, key);
906                 return SR_ERR_NA;
907         }
908
909         return SR_OK;
910 }
911
912 SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi,
913                                              void *cb_data)
914 {
915         struct dev_context *devc;
916         struct sr_serial_dev_inst *serial;
917
918         if (sdi->status != SR_ST_ACTIVE)
919                 return SR_ERR_DEV_CLOSED;
920
921         if (!(devc = sdi->priv))
922                 return SR_ERR_BUG;
923
924         devc->cb_data = cb_data;
925
926         dev_limit_counter_start(&devc->frame_count);
927         dev_time_counter_start(&devc->time_count);
928
929         /* Send header packet to the session bus. */
930         std_session_send_df_header(cb_data, LOG_PREFIX);
931
932         /* Poll every 50ms, or whenever some data comes in. */
933         serial = sdi->conn;
934         serial_source_add(sdi->session, serial, G_IO_IN, 50,
935                           receive_data, (void *)sdi);
936
937         return SR_OK;
938 }
939
940 SR_PRIV int es51919_serial_acquisition_stop(struct sr_dev_inst *sdi,
941                                             void *cb_data)
942 {
943         return std_serial_dev_acquisition_stop(sdi, cb_data,
944                         std_serial_dev_close, sdi->conn, LOG_PREFIX);
945 }