]> sigrok.org Git - libsigrok.git/blob - src/libsigrok-internal.h
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / libsigrok-internal.h
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.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 #ifndef LIBSIGROK_LIBSIGROK_INTERNAL_H
21 #define LIBSIGROK_LIBSIGROK_INTERNAL_H
22
23 #include "config.h"
24
25 #include <glib.h>
26 #ifdef HAVE_LIBHIDAPI
27 #include <hidapi.h>
28 #endif
29 #ifdef HAVE_LIBSERIALPORT
30 #include <libserialport.h>
31 #endif
32 #ifdef HAVE_LIBUSB_1_0
33 #include <libusb.h>
34 #endif
35 #include <stdarg.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 struct zip;
41 struct zip_stat;
42
43 /**
44  * @file
45  *
46  * libsigrok private header file, only to be used internally.
47  */
48
49 /*--- Macros ----------------------------------------------------------------*/
50
51 #ifndef ARRAY_SIZE
52 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
53 #endif
54
55 #ifndef ARRAY_AND_SIZE
56 #define ARRAY_AND_SIZE(a) (a), ARRAY_SIZE(a)
57 #endif
58
59 #ifndef G_SOURCE_FUNC
60 #define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) /* Since 2.58. */
61 #endif
62
63 #define SR_RECEIVE_DATA_CALLBACK(f) \
64         ((sr_receive_data_callback) (void (*)(void)) (f))
65
66 /**
67  * Read a 8 bits unsigned integer out of memory.
68  * @param x a pointer to the input memory
69  * @return the corresponding unsigned integer
70  */
71 static inline uint8_t read_u8(const uint8_t *p)
72 {
73         return p[0];
74 }
75 #define R8(x)   read_u8((const uint8_t *)(x))
76
77 /**
78  * Read a 16 bits big endian unsigned integer out of memory.
79  * @param x a pointer to the input memory
80  * @return the corresponding unsigned integer
81  */
82 static inline uint16_t read_u16be(const uint8_t *p)
83 {
84         uint16_t u;
85
86         u = 0;
87         u <<= 8; u |= p[0];
88         u <<= 8; u |= p[1];
89
90         return u;
91 }
92 #define RB16(x) read_u16be((const uint8_t *)(x))
93
94 /**
95  * Read a 16 bits little endian unsigned integer out of memory.
96  * @param x a pointer to the input memory
97  * @return the corresponding unsigned integer
98  */
99 static inline uint16_t read_u16le(const uint8_t *p)
100 {
101         uint16_t u;
102
103         u = 0;
104         u <<= 8; u |= p[1];
105         u <<= 8; u |= p[0];
106
107         return u;
108 }
109 #define RL16(x) read_u16le((const uint8_t *)(x))
110
111 /**
112  * Read a 16 bits big endian signed integer out of memory.
113  * @param x a pointer to the input memory
114  * @return the corresponding signed integer
115  */
116 static inline int16_t read_i16be(const uint8_t *p)
117 {
118         uint16_t u;
119         int16_t i;
120
121         u = read_u16be(p);
122         i = (int16_t)u;
123
124         return i;
125 }
126 #define RB16S(x) read_i16be((const uint8_t *)(x))
127
128 /**
129  * Read a 16 bits little endian signed integer out of memory.
130  * @param x a pointer to the input memory
131  * @return the corresponding signed integer
132  */
133 static inline int16_t read_i16le(const uint8_t *p)
134 {
135         uint16_t u;
136         int16_t i;
137
138         u = read_u16le(p);
139         i = (int16_t)u;
140
141         return i;
142 }
143 #define RL16S(x) read_i16le((const uint8_t *)(x))
144
145 /**
146  * Read a 24 bits little endian unsigned integer out of memory.
147  * @param x a pointer to the input memory
148  * @return the corresponding unsigned integer
149  */
150 static inline uint32_t read_u24le(const uint8_t *p)
151 {
152         uint32_t u;
153
154         u = 0;
155         u <<= 8; u |= p[2];
156         u <<= 8; u |= p[1];
157         u <<= 8; u |= p[0];
158
159         return u;
160 }
161
162 /**
163  * Read a 32 bits big endian unsigned integer out of memory.
164  * @param x a pointer to the input memory
165  * @return the corresponding unsigned integer
166  */
167 static inline uint32_t read_u32be(const uint8_t *p)
168 {
169         uint32_t u;
170
171         u = 0;
172         u <<= 8; u |= p[0];
173         u <<= 8; u |= p[1];
174         u <<= 8; u |= p[2];
175         u <<= 8; u |= p[3];
176
177         return u;
178 }
179 #define RB32(x) read_u32be((const uint8_t *)(x))
180
181 /**
182  * Read a 32 bits little endian unsigned integer out of memory.
183  * @param x a pointer to the input memory
184  * @return the corresponding unsigned integer
185  */
186 static inline uint32_t read_u32le(const uint8_t *p)
187 {
188         uint32_t u;
189
190         u = 0;
191         u <<= 8; u |= p[3];
192         u <<= 8; u |= p[2];
193         u <<= 8; u |= p[1];
194         u <<= 8; u |= p[0];
195
196         return u;
197 }
198 #define RL32(x) read_u32le((const uint8_t *)(x))
199
200 /**
201  * Read a 32 bits big endian signed integer out of memory.
202  * @param x a pointer to the input memory
203  * @return the corresponding signed integer
204  */
205 static inline int32_t read_i32be(const uint8_t *p)
206 {
207         uint32_t u;
208         int32_t i;
209
210         u = read_u32be(p);
211         i = (int32_t)u;
212
213         return i;
214 }
215 #define RB32S(x) read_i32be((const uint8_t *)(x))
216
217 /**
218  * Read a 32 bits little endian signed integer out of memory.
219  * @param x a pointer to the input memory
220  * @return the corresponding signed integer
221  */
222 static inline int32_t read_i32le(const uint8_t *p)
223 {
224         uint32_t u;
225         int32_t i;
226
227         u = read_u32le(p);
228         i = (int32_t)u;
229
230         return i;
231 }
232 #define RL32S(x) read_i32le((const uint8_t *)(x))
233
234 /**
235  * Read a 64 bits big endian unsigned integer out of memory.
236  * @param x a pointer to the input memory
237  * @return the corresponding unsigned integer
238  */
239 static inline uint64_t read_u64be(const uint8_t *p)
240 {
241         uint64_t u;
242
243         u = 0;
244         u <<= 8; u |= p[0];
245         u <<= 8; u |= p[1];
246         u <<= 8; u |= p[2];
247         u <<= 8; u |= p[3];
248         u <<= 8; u |= p[4];
249         u <<= 8; u |= p[5];
250         u <<= 8; u |= p[6];
251         u <<= 8; u |= p[7];
252
253         return u;
254 }
255 #define RB64(x) read_u64be((const uint8_t *)(x))
256
257 /**
258  * Read a 64 bits little endian unsigned integer out of memory.
259  * @param x a pointer to the input memory
260  * @return the corresponding unsigned integer
261  */
262 static inline uint64_t read_u64le(const uint8_t *p)
263 {
264         uint64_t u;
265
266         u = 0;
267         u <<= 8; u |= p[7];
268         u <<= 8; u |= p[6];
269         u <<= 8; u |= p[5];
270         u <<= 8; u |= p[4];
271         u <<= 8; u |= p[3];
272         u <<= 8; u |= p[2];
273         u <<= 8; u |= p[1];
274         u <<= 8; u |= p[0];
275
276         return u;
277 }
278 #define RL64(x) read_u64le((const uint8_t *)(x))
279
280 /**
281  * Read a 64 bits big endian signed integer out of memory.
282  * @param x a pointer to the input memory
283  * @return the corresponding unsigned integer
284  */
285 static inline int64_t read_i64be(const uint8_t *p)
286 {
287         uint64_t u;
288         int64_t i;
289
290         u = read_u64be(p);
291         i = (int64_t)u;
292
293         return i;
294 }
295 #define RB64S(x) read_i64be((const uint8_t *)(x))
296
297 /**
298  * Read a 64 bits little endian signed integer out of memory.
299  * @param x a pointer to the input memory
300  * @return the corresponding unsigned integer
301  */
302 static inline int64_t read_i64le(const uint8_t *p)
303 {
304         uint64_t u;
305         int64_t i;
306
307         u = read_u64le(p);
308         i = (int64_t)u;
309
310         return i;
311 }
312 #define RL64S(x) read_i64le((const uint8_t *)(x))
313
314 /**
315  * Read a 32 bits big endian float out of memory.
316  * @param x a pointer to the input memory
317  * @return the corresponding float
318  */
319 static inline float read_fltbe(const uint8_t *p)
320 {
321         /*
322          * Implementor's note: Strictly speaking the "union" trick
323          * is not portable. But this phrase was found to work on the
324          * project's supported platforms, and serve well until a more
325          * appropriate phrase is found.
326          */
327         union { uint32_t u32; float flt; } u;
328         float f;
329
330         u.u32 = read_u32be(p);
331         f = u.flt;
332
333         return f;
334 }
335 #define RBFL(x) read_fltbe((const uint8_t *)(x))
336
337 /**
338  * Read a 32 bits little endian float out of memory.
339  * @param x a pointer to the input memory
340  * @return the corresponding float
341  */
342 static inline float read_fltle(const uint8_t *p)
343 {
344         /*
345          * Implementor's note: Strictly speaking the "union" trick
346          * is not portable. But this phrase was found to work on the
347          * project's supported platforms, and serve well until a more
348          * appropriate phrase is found.
349          */
350         union { uint32_t u32; float flt; } u;
351         float f;
352
353         u.u32 = read_u32le(p);
354         f = u.flt;
355
356         return f;
357 }
358 #define RLFL(x) read_fltle((const uint8_t *)(x))
359
360 /**
361  * Write a 8 bits unsigned integer to memory.
362  * @param p a pointer to the output memory
363  * @param x the input unsigned integer
364  */
365 static inline void write_u8(uint8_t *p, uint8_t x)
366 {
367         p[0] = x;
368 }
369 #define W8(p, x) write_u8((uint8_t *)(p), (uint8_t)(x))
370
371 /**
372  * Write a 16 bits unsigned integer to memory stored as big endian.
373  * @param p a pointer to the output memory
374  * @param x the input unsigned integer
375  */
376 static inline void write_u16be(uint8_t *p, uint16_t x)
377 {
378         p[1] = x & 0xff; x >>= 8;
379         p[0] = x & 0xff; x >>= 8;
380 }
381 #define WB16(p, x) write_u16be((uint8_t *)(p), (uint16_t)(x))
382
383 /**
384  * Write a 16 bits unsigned integer to memory stored as little endian.
385  * @param p a pointer to the output memory
386  * @param x the input unsigned integer
387  */
388 static inline void write_u16le(uint8_t *p, uint16_t x)
389 {
390         p[0] = x & 0xff; x >>= 8;
391         p[1] = x & 0xff; x >>= 8;
392 }
393 #define WL16(p, x) write_u16le((uint8_t *)(p), (uint16_t)(x))
394
395 /**
396  * Write a 32 bits unsigned integer to memory stored as big endian.
397  * @param p a pointer to the output memory
398  * @param x the input unsigned integer
399  */
400 static inline void write_u32be(uint8_t *p, uint32_t x)
401 {
402         p[3] = x & 0xff; x >>= 8;
403         p[2] = x & 0xff; x >>= 8;
404         p[1] = x & 0xff; x >>= 8;
405         p[0] = x & 0xff; x >>= 8;
406 }
407 #define WB32(p, x) write_u32be((uint8_t *)(p), (uint32_t)(x))
408
409 /**
410  * Write a 32 bits unsigned integer to memory stored as little endian.
411  * @param p a pointer to the output memory
412  * @param x the input unsigned integer
413  */
414 static inline void write_u32le(uint8_t *p, uint32_t x)
415 {
416         p[0] = x & 0xff; x >>= 8;
417         p[1] = x & 0xff; x >>= 8;
418         p[2] = x & 0xff; x >>= 8;
419         p[3] = x & 0xff; x >>= 8;
420 }
421 #define WL32(p, x) write_u32le((uint8_t *)(p), (uint32_t)(x))
422
423 /**
424  * Write a 32 bits float to memory stored as big endian.
425  * @param p a pointer to the output memory
426  * @param x the input float
427  */
428 static inline void write_fltbe(uint8_t *p, float x)
429 {
430         union { uint32_t u; float f; } u;
431         u.f = x;
432         write_u32be(p, u.u);
433 }
434 #define WBFL(p, x) write_fltbe((uint8_t *)(p), (x))
435
436 /**
437  * Write a 32 bits float to memory stored as little endian.
438  * @param p a pointer to the output memory
439  * @param x the input float
440  */
441 static inline void write_fltle(uint8_t *p, float x)
442 {
443         union { uint32_t u; float f; } u;
444         u.f = x;
445         write_u32le(p, u.u);
446 }
447 #define WLFL(p, x) write_fltle((uint8_t *)(p), float (x))
448
449 /* Endianess conversion helpers with read/write position increment. */
450
451 /**
452  * Read unsigned 8bit integer from raw memory, increment read position.
453  * @param[in, out] p Pointer into byte stream.
454  * @return Retrieved integer value, unsigned.
455  */
456 static inline uint8_t read_u8_inc(const uint8_t **p)
457 {
458         uint8_t v;
459
460         if (!p || !*p)
461                 return 0;
462         v = read_u8(*p);
463         *p += sizeof(v);
464
465         return v;
466 }
467
468 /**
469  * Read unsigned 16bit integer from raw memory (big endian format), increment read position.
470  * @param[in, out] p Pointer into byte stream.
471  * @return Retrieved integer value, unsigned.
472  */
473 static inline uint16_t read_u16be_inc(const uint8_t **p)
474 {
475         uint16_t v;
476
477         if (!p || !*p)
478                 return 0;
479         v = read_u16be(*p);
480         *p += sizeof(v);
481
482         return v;
483 }
484
485 /**
486  * Read unsigned 16bit integer from raw memory (little endian format), increment read position.
487  * @param[in, out] p Pointer into byte stream.
488  * @return Retrieved integer value, unsigned.
489  */
490 static inline uint16_t read_u16le_inc(const uint8_t **p)
491 {
492         uint16_t v;
493
494         if (!p || !*p)
495                 return 0;
496         v = read_u16le(*p);
497         *p += sizeof(v);
498
499         return v;
500 }
501
502 /**
503  * Read unsigned 32bit integer from raw memory (big endian format), increment read position.
504  * @param[in, out] p Pointer into byte stream.
505  * @return Retrieved integer value, unsigned.
506  */
507 static inline uint32_t read_u32be_inc(const uint8_t **p)
508 {
509         uint32_t v;
510
511         if (!p || !*p)
512                 return 0;
513         v = read_u32be(*p);
514         *p += sizeof(v);
515
516         return v;
517 }
518
519 /**
520  * Read unsigned 24bit integer from raw memory (little endian format), increment read position.
521  * @param[in, out] p Pointer into byte stream.
522  * @return Retrieved integer value, unsigned.
523  */
524 static inline uint32_t read_u24le_inc(const uint8_t **p)
525 {
526         uint32_t v;
527
528         if (!p || !*p)
529                 return 0;
530         v = read_u24le(*p);
531         *p += 3 * sizeof(uint8_t);
532
533         return v;
534 }
535
536 /**
537  * Read unsigned 32bit integer from raw memory (little endian format), increment read position.
538  * @param[in, out] p Pointer into byte stream.
539  * @return Retrieved integer value, unsigned.
540  */
541 static inline uint32_t read_u32le_inc(const uint8_t **p)
542 {
543         uint32_t v;
544
545         if (!p || !*p)
546                 return 0;
547         v = read_u32le(*p);
548         *p += sizeof(v);
549
550         return v;
551 }
552
553 /**
554  * Read unsigned 64bit integer from raw memory (big endian format), increment read position.
555  * @param[in, out] p Pointer into byte stream.
556  * @return Retrieved integer value, unsigned.
557  */
558 static inline uint64_t read_u64be_inc(const uint8_t **p)
559 {
560         uint64_t v;
561
562         if (!p || !*p)
563                 return 0;
564         v = read_u64be(*p);
565         *p += sizeof(v);
566
567         return v;
568 }
569
570 /**
571  * Read unsigned 64bit integer from raw memory (little endian format), increment read position.
572  * @param[in, out] p Pointer into byte stream.
573  * @return Retrieved integer value, unsigned.
574  */
575 static inline uint64_t read_u64le_inc(const uint8_t **p)
576 {
577         uint64_t v;
578
579         if (!p || !*p)
580                 return 0;
581         v = read_u64le(*p);
582         *p += sizeof(v);
583
584         return v;
585 }
586
587 /**
588  * Write unsigned 8bit integer to raw memory, increment write position.
589  * @param[in, out] p Pointer into byte stream.
590  * @param[in] x Value to write.
591  */
592 static inline void write_u8_inc(uint8_t **p, uint8_t x)
593 {
594         if (!p || !*p)
595                 return;
596         write_u8(*p, x);
597         *p += sizeof(x);
598 }
599
600 /**
601  * Write unsigned 16bit big endian integer to raw memory, increment write position.
602  * @param[in, out] p Pointer into byte stream.
603  * @param[in] x Value to write.
604  */
605 static inline void write_u16be_inc(uint8_t **p, uint16_t x)
606 {
607         if (!p || !*p)
608                 return;
609         write_u16be(*p, x);
610         *p += sizeof(x);
611 }
612
613 /**
614  * Write unsigned 16bit little endian integer to raw memory, increment write position.
615  * @param[in, out] p Pointer into byte stream.
616  * @param[in] x Value to write.
617  */
618 static inline void write_u16le_inc(uint8_t **p, uint16_t x)
619 {
620         if (!p || !*p)
621                 return;
622         write_u16le(*p, x);
623         *p += sizeof(x);
624 }
625
626 /**
627  * Write unsigned 32bit big endian integer to raw memory, increment write position.
628  * @param[in, out] p Pointer into byte stream.
629  * @param[in] x Value to write.
630  */
631 static inline void write_u32be_inc(uint8_t **p, uint32_t x)
632 {
633         if (!p || !*p)
634                 return;
635         write_u32be(*p, x);
636         *p += sizeof(x);
637 }
638
639 /**
640  * Write unsigned 32bit little endian integer to raw memory, increment write position.
641  * @param[in, out] p Pointer into byte stream.
642  * @param[in] x Value to write.
643  */
644 static inline void write_u32le_inc(uint8_t **p, uint32_t x)
645 {
646         if (!p || !*p)
647                 return;
648         write_u32le(*p, x);
649         *p += sizeof(x);
650 }
651
652 /* Portability fixes for FreeBSD. */
653 #ifdef __FreeBSD__
654 #define LIBUSB_CLASS_APPLICATION 0xfe
655 #define libusb_has_capability(x) 0
656 #define libusb_handle_events_timeout_completed(ctx, tv, c) \
657         libusb_handle_events_timeout(ctx, tv)
658 #endif
659
660 /* Static definitions of structs ending with an all-zero entry are a
661  * problem when compiling with -Wmissing-field-initializers: GCC
662  * suppresses the warning only with { 0 }, clang wants { } */
663 #ifdef __clang__
664 #define ALL_ZERO { }
665 #else
666 #define ALL_ZERO { 0 }
667 #endif
668
669 #ifdef __APPLE__
670 #define SR_DRIVER_LIST_SECTION "__DATA,__sr_driver_list"
671 #else
672 #define SR_DRIVER_LIST_SECTION "__sr_driver_list"
673 #endif
674
675 /**
676  * Register a list of hardware drivers.
677  *
678  * This macro can be used to register multiple hardware drivers to the library.
679  * This is useful when a driver supports multiple similar but slightly
680  * different devices that require different sr_dev_driver struct definitions.
681  *
682  * For registering only a single driver see SR_REGISTER_DEV_DRIVER().
683  *
684  * Example:
685  * @code{c}
686  * #define MY_DRIVER(_name) \
687  *     &(struct sr_dev_driver){ \
688  *         .name = _name, \
689  *         ...
690  *     };
691  *
692  * SR_REGISTER_DEV_DRIVER_LIST(my_driver_infos,
693  *     MY_DRIVER("driver 1"),
694  *     MY_DRIVER("driver 2"),
695  *     ...
696  * );
697  * @endcode
698  *
699  * @param name Name to use for the driver list identifier.
700  * @param ... Comma separated list of pointers to sr_dev_driver structs.
701  */
702 #define SR_REGISTER_DEV_DRIVER_LIST(name, ...) \
703         static const struct sr_dev_driver *name[] \
704                 __attribute__((section (SR_DRIVER_LIST_SECTION), used, \
705                         aligned(sizeof(struct sr_dev_driver *)))) \
706                 = { \
707                         __VA_ARGS__ \
708                 };
709
710 /**
711  * Register a hardware driver.
712  *
713  * This macro is used to register a hardware driver with the library. It has
714  * to be used in order to make the driver accessible to applications using the
715  * library.
716  *
717  * The macro invocation should be placed directly under the struct
718  * sr_dev_driver definition.
719  *
720  * Example:
721  * @code{c}
722  * static struct sr_dev_driver driver_info = {
723  *     .name = "driver",
724  *     ....
725  * };
726  * SR_REGISTER_DEV_DRIVER(driver_info);
727  * @endcode
728  *
729  * @param name Identifier name of sr_dev_driver struct to register.
730  */
731 #define SR_REGISTER_DEV_DRIVER(name) \
732         SR_REGISTER_DEV_DRIVER_LIST(name##_list, &name);
733
734 SR_API void sr_drivers_init(struct sr_context *context);
735
736 struct sr_context {
737         struct sr_dev_driver **driver_list;
738 #ifdef HAVE_LIBUSB_1_0
739         libusb_context *libusb_ctx;
740 #endif
741         sr_resource_open_callback resource_open_cb;
742         sr_resource_close_callback resource_close_cb;
743         sr_resource_read_callback resource_read_cb;
744         void *resource_cb_data;
745 };
746
747 /** Input module metadata keys. */
748 enum sr_input_meta_keys {
749         /** The input filename, if there is one. */
750         SR_INPUT_META_FILENAME = 0x01,
751         /** The input file's size in bytes. */
752         SR_INPUT_META_FILESIZE = 0x02,
753         /** The first 128 bytes of the file, provided as a GString. */
754         SR_INPUT_META_HEADER = 0x04,
755
756         /** The module cannot identify a file without this metadata. */
757         SR_INPUT_META_REQUIRED = 0x80,
758 };
759
760 /** Input (file) module struct. */
761 struct sr_input {
762         /**
763          * A pointer to this input module's 'struct sr_input_module'.
764          */
765         const struct sr_input_module *module;
766         GString *buf;
767         struct sr_dev_inst *sdi;
768         gboolean sdi_ready;
769         void *priv;
770 };
771
772 /** Input (file) module driver. */
773 struct sr_input_module {
774         /**
775          * A unique ID for this input module, suitable for use in command-line
776          * clients, [a-z0-9-]. Must not be NULL.
777          */
778         const char *id;
779
780         /**
781          * A unique name for this input module, suitable for use in GUI
782          * clients, can contain UTF-8. Must not be NULL.
783          */
784         const char *name;
785
786         /**
787          * A short description of the input module. Must not be NULL.
788          *
789          * This can be displayed by frontends, e.g. when selecting the input
790          * module for saving a file.
791          */
792         const char *desc;
793
794         /**
795          * A NULL terminated array of strings containing a list of file name
796          * extensions typical for the input file format, or NULL if there is
797          * no typical extension for this file format.
798          */
799         const char *const *exts;
800
801         /**
802          * Zero-terminated list of metadata items the module needs to be able
803          * to identify an input stream. Can be all-zero, if the module cannot
804          * identify streams at all, i.e. has to be forced into use.
805          *
806          * Each item is one of:
807          *   SR_INPUT_META_FILENAME
808          *   SR_INPUT_META_FILESIZE
809          *   SR_INPUT_META_HEADER
810          *
811          * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
812          * identify a stream without the given metadata.
813          */
814         const uint8_t metadata[8];
815
816         /**
817          * Returns a NULL-terminated list of options this module can take.
818          * Can be NULL, if the module has no options.
819          */
820         const struct sr_option *(*options) (void);
821
822         /**
823          * Check if this input module can load and parse the specified stream.
824          *
825          * @param[in] metadata Metadata the module can use to identify the stream.
826          * @param[out] confidence "Strength" of the detection.
827          *   Specialized handlers can take precedence over generic/basic support.
828          *
829          * @retval SR_OK This module knows the format.
830          * @retval SR_ERR_NA There wasn't enough data for this module to
831          *   positively identify the format.
832          * @retval SR_ERR_DATA This module knows the format, but cannot handle
833          *   it. This means the stream is either corrupt, or indicates a
834          *   feature that the module does not support.
835          * @retval SR_ERR This module does not know the format.
836          *
837          * Lower numeric values of 'confidence' mean that the input module
838          * stronger believes in its capability to handle this specific format.
839          * This way, multiple input modules can claim support for a format,
840          * and the application can pick the best match, or try fallbacks
841          * in case of errors. This approach also copes with formats that
842          * are unreliable to detect in the absence of magic signatures.
843          */
844         int (*format_match) (GHashTable *metadata, unsigned int *confidence);
845
846         /**
847          * Initialize the input module.
848          *
849          * @retval SR_OK Success
850          * @retval other Negative error code.
851          */
852         int (*init) (struct sr_input *in, GHashTable *options);
853
854         /**
855          * Send data to the specified input instance.
856          *
857          * When an input module instance is created with sr_input_new(), this
858          * function is used to feed data to the instance.
859          *
860          * As enough data gets fed into this function to completely populate
861          * the device instance associated with this input instance, this is
862          * guaranteed to return the moment it's ready. This gives the caller
863          * the chance to examine the device instance, attach session callbacks
864          * and so on.
865          *
866          * @retval SR_OK Success
867          * @retval other Negative error code.
868          */
869         int (*receive) (struct sr_input *in, GString *buf);
870
871         /**
872          * Signal the input module no more data will come.
873          *
874          * This will cause the module to process any data it may have buffered.
875          * The SR_DF_END packet will also typically be sent at this time.
876          */
877         int (*end) (struct sr_input *in);
878
879         /**
880          * Reset the input module's input handling structures.
881          *
882          * Causes the input module to reset its internal state so that we can
883          * re-send the input data from the beginning without having to
884          * re-create the entire input module.
885          *
886          * @retval SR_OK Success.
887          * @retval other Negative error code.
888          */
889         int (*reset) (struct sr_input *in);
890
891         /**
892          * This function is called after the caller is finished using
893          * the input module, and can be used to free any internal
894          * resources the module may keep.
895          *
896          * This function is optional.
897          *
898          * @retval SR_OK Success
899          * @retval other Negative error code.
900          */
901         void (*cleanup) (struct sr_input *in);
902 };
903
904 /** Output module instance. */
905 struct sr_output {
906         /** A pointer to this output's module. */
907         const struct sr_output_module *module;
908
909         /**
910          * The device for which this output module is creating output. This
911          * can be used by the module to find out channel names and numbers.
912          */
913         const struct sr_dev_inst *sdi;
914
915         /**
916          * The name of the file that the data should be written to.
917          */
918         const char *filename;
919
920         /**
921          * A generic pointer which can be used by the module to keep internal
922          * state between calls into its callback functions.
923          *
924          * For example, the module might store a pointer to a chunk of output
925          * there, and only flush it when it reaches a certain size.
926          */
927         void *priv;
928 };
929
930 /** Output module driver. */
931 struct sr_output_module {
932         /**
933          * A unique ID for this output module, suitable for use in command-line
934          * clients, [a-z0-9-]. Must not be NULL.
935          */
936         const char *id;
937
938         /**
939          * A unique name for this output module, suitable for use in GUI
940          * clients, can contain UTF-8. Must not be NULL.
941          */
942         const char *name;
943
944         /**
945          * A short description of the output module. Must not be NULL.
946          *
947          * This can be displayed by frontends, e.g. when selecting the output
948          * module for saving a file.
949          */
950         const char *desc;
951
952         /**
953          * A NULL terminated array of strings containing a list of file name
954          * extensions typical for the input file format, or NULL if there is
955          * no typical extension for this file format.
956          */
957         const char *const *exts;
958
959         /**
960          * Bitfield containing flags that describe certain properties
961          * this output module may or may not have.
962          * @see sr_output_flags
963          */
964         const uint64_t flags;
965
966         /**
967          * Returns a NULL-terminated list of options this module can take.
968          * Can be NULL, if the module has no options.
969          */
970         const struct sr_option *(*options) (void);
971
972         /**
973          * This function is called once, at the beginning of an output stream.
974          *
975          * The device struct will be available in the output struct passed in,
976          * as well as the param field -- which may be NULL or an empty string,
977          * if no parameter was passed.
978          *
979          * The module can use this to initialize itself, create a struct for
980          * keeping state and storing it in the <code>internal</code> field.
981          *
982          * @param o Pointer to the respective 'struct sr_output'.
983          *
984          * @retval SR_OK Success
985          * @retval other Negative error code.
986          */
987         int (*init) (struct sr_output *o, GHashTable *options);
988
989         /**
990          * This function is passed a copy of every packet in the data feed.
991          * Any output generated by the output module in response to the
992          * packet should be returned in a newly allocated GString
993          * <code>out</code>, which will be freed by the caller.
994          *
995          * Packets not of interest to the output module can just be ignored,
996          * and the <code>out</code> parameter set to NULL.
997          *
998          * @param o Pointer to the respective 'struct sr_output'.
999          * @param sdi The device instance that generated the packet.
1000          * @param packet The complete packet.
1001          * @param out A pointer where a GString * should be stored if
1002          * the module generates output, or NULL if not.
1003          *
1004          * @retval SR_OK Success
1005          * @retval other Negative error code.
1006          */
1007         int (*receive) (const struct sr_output *o,
1008                         const struct sr_datafeed_packet *packet, GString **out);
1009
1010         /**
1011          * This function is called after the caller is finished using
1012          * the output module, and can be used to free any internal
1013          * resources the module may keep.
1014          *
1015          * @retval SR_OK Success
1016          * @retval other Negative error code.
1017          */
1018         int (*cleanup) (struct sr_output *o);
1019 };
1020
1021 /** Transform module instance. */
1022 struct sr_transform {
1023         /** A pointer to this transform's module. */
1024         const struct sr_transform_module *module;
1025
1026         /**
1027          * The device for which this transform module is used. This
1028          * can be used by the module to find out channel names and numbers.
1029          */
1030         const struct sr_dev_inst *sdi;
1031
1032         /**
1033          * A generic pointer which can be used by the module to keep internal
1034          * state between calls into its callback functions.
1035          */
1036         void *priv;
1037 };
1038
1039 struct sr_transform_module {
1040         /**
1041          * A unique ID for this transform module, suitable for use in
1042          * command-line clients, [a-z0-9-]. Must not be NULL.
1043          */
1044         const char *id;
1045
1046         /**
1047          * A unique name for this transform module, suitable for use in GUI
1048          * clients, can contain UTF-8. Must not be NULL.
1049          */
1050         const char *name;
1051
1052         /**
1053          * A short description of the transform module. Must not be NULL.
1054          *
1055          * This can be displayed by frontends, e.g. when selecting
1056          * which transform module(s) to add.
1057          */
1058         const char *desc;
1059
1060         /**
1061          * Returns a NULL-terminated list of options this transform module
1062          * can take. Can be NULL, if the transform module has no options.
1063          */
1064         const struct sr_option *(*options) (void);
1065
1066         /**
1067          * This function is called once, at the beginning of a stream.
1068          *
1069          * @param t Pointer to the respective 'struct sr_transform'.
1070          * @param options Hash table of options for this transform module.
1071          *                Can be NULL if no options are to be used.
1072          *
1073          * @retval SR_OK Success
1074          * @retval other Negative error code.
1075          */
1076         int (*init) (struct sr_transform *t, GHashTable *options);
1077
1078         /**
1079          * This function is passed a pointer to every packet in the data feed.
1080          *
1081          * It can either return (in packet_out) a pointer to another packet
1082          * (possibly the exact same packet it got as input), or NULL.
1083          *
1084          * @param t Pointer to the respective 'struct sr_transform'.
1085          * @param packet_in Pointer to a datafeed packet.
1086          * @param packet_out Pointer to the resulting datafeed packet after
1087          *                   this function was run. If NULL, the transform
1088          *                   module intentionally didn't output a new packet.
1089          *
1090          * @retval SR_OK Success
1091          * @retval other Negative error code.
1092          */
1093         int (*receive) (const struct sr_transform *t,
1094                         struct sr_datafeed_packet *packet_in,
1095                         struct sr_datafeed_packet **packet_out);
1096
1097         /**
1098          * This function is called after the caller is finished using
1099          * the transform module, and can be used to free any internal
1100          * resources the module may keep.
1101          *
1102          * @retval SR_OK Success
1103          * @retval other Negative error code.
1104          */
1105         int (*cleanup) (struct sr_transform *t);
1106 };
1107
1108 #ifdef HAVE_LIBUSB_1_0
1109 /** USB device instance */
1110 struct sr_usb_dev_inst {
1111         /** USB bus */
1112         uint8_t bus;
1113         /** Device address on USB bus */
1114         uint8_t address;
1115         /** libusb device handle */
1116         struct libusb_device_handle *devhdl;
1117 };
1118 #endif
1119
1120 struct sr_serial_dev_inst;
1121 #ifdef HAVE_SERIAL_COMM
1122 struct ser_lib_functions;
1123 struct ser_hid_chip_functions;
1124 struct sr_bt_desc;
1125 typedef void (*serial_rx_chunk_callback)(struct sr_serial_dev_inst *serial,
1126         void *cb_data, const void *buf, size_t count);
1127 struct sr_serial_dev_inst {
1128         /** Port name, e.g. '/dev/tty42'. */
1129         char *port;
1130         /** Comm params for serial_set_paramstr(). */
1131         char *serialcomm;
1132         struct ser_lib_functions *lib_funcs;
1133         struct {
1134                 int bit_rate;
1135                 int data_bits;
1136                 int parity_bits;
1137                 int stop_bits;
1138         } comm_params;
1139         GString *rcv_buffer;
1140         serial_rx_chunk_callback rx_chunk_cb_func;
1141         void *rx_chunk_cb_data;
1142 #ifdef HAVE_LIBSERIALPORT
1143         /** libserialport port handle */
1144         struct sp_port *sp_data;
1145 #endif
1146 #ifdef HAVE_LIBHIDAPI
1147         enum ser_hid_chip_t {
1148                 SER_HID_CHIP_UNKNOWN,           /**!< place holder */
1149                 SER_HID_CHIP_BTC_BU86X,         /**!< Brymen BU86x */
1150                 SER_HID_CHIP_SIL_CP2110,        /**!< SiLabs CP2110 */
1151                 SER_HID_CHIP_VICTOR_DMM,        /**!< Victor 70/86 DMM cable */
1152                 SER_HID_CHIP_WCH_CH9325,        /**!< WCH CH9325 */
1153                 SER_HID_CHIP_LAST,              /**!< sentinel */
1154         } hid_chip;
1155         struct ser_hid_chip_functions *hid_chip_funcs;
1156         char *usb_path;
1157         char *usb_serno;
1158         const char *hid_path;
1159         hid_device *hid_dev;
1160         GSList *hid_source_args;
1161 #endif
1162 #ifdef HAVE_BLUETOOTH
1163         enum ser_bt_conn_t {
1164                 SER_BT_CONN_UNKNOWN,    /**!< place holder */
1165                 SER_BT_CONN_RFCOMM,     /**!< BT classic, RFCOMM channel */
1166                 SER_BT_CONN_BLE122,     /**!< BLE, BLE122 module, indications */
1167                 SER_BT_CONN_NRF51,      /**!< BLE, Nordic nRF51, notifications */
1168                 SER_BT_CONN_CC254x,     /**!< BLE, TI CC254x, notifications */
1169                 SER_BT_CONN_MAX,        /**!< sentinel */
1170         } bt_conn_type;
1171         char *bt_addr_local;
1172         char *bt_addr_remote;
1173         size_t bt_rfcomm_channel;
1174         uint16_t bt_notify_handle_read;
1175         uint16_t bt_notify_handle_write;
1176         uint16_t bt_notify_handle_cccd;
1177         uint16_t bt_notify_value_cccd;
1178         struct sr_bt_desc *bt_desc;
1179         GSList *bt_source_args;
1180 #endif
1181 };
1182 #endif
1183
1184 struct sr_usbtmc_dev_inst {
1185         char *device;
1186         int fd;
1187 };
1188
1189 /* Private driver context. */
1190 struct drv_context {
1191         /** sigrok context */
1192         struct sr_context *sr_ctx;
1193         GSList *instances;
1194 };
1195
1196 /*--- log.c -----------------------------------------------------------------*/
1197
1198 #if defined(_WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
1199 /*
1200  * On MinGW, we need to specify the gnu_printf format flavor or GCC
1201  * will assume non-standard Microsoft printf syntax.
1202  */
1203 SR_PRIV int sr_log(int loglevel, const char *format, ...)
1204                 __attribute__((__format__ (__gnu_printf__, 2, 3)));
1205 #else
1206 SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
1207 #endif
1208
1209 /* Message logging helpers with subsystem-specific prefix string. */
1210 #define sr_spew(...)    sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
1211 #define sr_dbg(...)     sr_log(SR_LOG_DBG,  LOG_PREFIX ": " __VA_ARGS__)
1212 #define sr_info(...)    sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
1213 #define sr_warn(...)    sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
1214 #define sr_err(...)     sr_log(SR_LOG_ERR,  LOG_PREFIX ": " __VA_ARGS__)
1215
1216 /*--- device.c --------------------------------------------------------------*/
1217
1218 /** Scan options supported by a driver. */
1219 #define SR_CONF_SCAN_OPTIONS 0x7FFF0000
1220
1221 /** Device options for a particular device. */
1222 #define SR_CONF_DEVICE_OPTIONS 0x7FFF0001
1223
1224 /** Mask for separating config keys from capabilities. */
1225 #define SR_CONF_MASK 0x1fffffff
1226
1227 /** Values for the changes argument of sr_dev_driver.config_channel_set. */
1228 enum {
1229         /** The enabled state of the channel has been changed. */
1230         SR_CHANNEL_SET_ENABLED = 1 << 0,
1231 };
1232
1233 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
1234                 int index, int type, gboolean enabled, const char *name);
1235 SR_PRIV void sr_channel_free(struct sr_channel *ch);
1236 SR_PRIV void sr_channel_free_cb(void *p);
1237 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
1238                 struct sr_channel *cur_channel);
1239 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2);
1240 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2);
1241
1242 /** Device instance data */
1243 struct sr_dev_inst {
1244         /** Device driver. */
1245         struct sr_dev_driver *driver;
1246         /** Device instance status. SR_ST_NOT_FOUND, etc. */
1247         int status;
1248         /** Device instance type. SR_INST_USB, etc. */
1249         int inst_type;
1250         /** Device vendor. */
1251         char *vendor;
1252         /** Device model. */
1253         char *model;
1254         /** Device version. */
1255         char *version;
1256         /** Serial number. */
1257         char *serial_num;
1258         /** Connection string to uniquely identify devices. */
1259         char *connection_id;
1260         /** List of channels. */
1261         GSList *channels;
1262         /** List of sr_channel_group structs */
1263         GSList *channel_groups;
1264         /** Device instance connection data (used?) */
1265         void *conn;
1266         /** Device instance private data (used?) */
1267         void *priv;
1268         /** Session to which this device is currently assigned. */
1269         struct sr_session *session;
1270 };
1271
1272 /* Generic device instances */
1273 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
1274
1275 #ifdef HAVE_LIBUSB_1_0
1276 /* USB-specific instances */
1277 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
1278                 uint8_t address, struct libusb_device_handle *hdl);
1279 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
1280 #endif
1281
1282 #ifdef HAVE_SERIAL_COMM
1283 #ifndef HAVE_LIBSERIALPORT
1284 /*
1285  * Some identifiers which initially got provided by libserialport are
1286  * used internally within the libsigrok serial layer's implementation,
1287  * while libserialport no longer is the exclusive provider of serial
1288  * communication support. Declare the identifiers here so they remain
1289  * available across all build configurations.
1290  */
1291 enum libsp_parity {
1292         SP_PARITY_NONE = 0,
1293         SP_PARITY_ODD = 1,
1294         SP_PARITY_EVEN = 2,
1295         SP_PARITY_MARK = 3,
1296         SP_PARITY_SPACE = 4,
1297 };
1298
1299 enum libsp_flowcontrol {
1300         SP_FLOWCONTROL_NONE = 0,
1301         SP_FLOWCONTROL_XONXOFF = 1,
1302         SP_FLOWCONTROL_RTSCTS = 2,
1303         SP_FLOWCONTROL_DTRDSR = 3,
1304 };
1305 #endif
1306
1307 /* Serial-specific instances */
1308 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
1309                 const char *serialcomm);
1310 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
1311 #endif
1312
1313 /* USBTMC-specific instances */
1314 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
1315 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
1316
1317 /*--- hwdriver.c ------------------------------------------------------------*/
1318
1319 SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
1320 SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
1321 SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
1322 SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
1323 SR_PRIV void sr_config_free(struct sr_config *src);
1324 SR_PRIV int sr_dev_acquisition_start(struct sr_dev_inst *sdi);
1325 SR_PRIV int sr_dev_acquisition_stop(struct sr_dev_inst *sdi);
1326
1327 /*--- session.c -------------------------------------------------------------*/
1328
1329 struct sr_session {
1330         /** Context this session exists in. */
1331         struct sr_context *ctx;
1332         /** List of struct sr_dev_inst pointers. */
1333         GSList *devs;
1334         /** List of struct sr_dev_inst pointers owned by this session. */
1335         GSList *owned_devs;
1336         /** List of struct datafeed_callback pointers. */
1337         GSList *datafeed_callbacks;
1338         GSList *transforms;
1339         struct sr_trigger *trigger;
1340
1341         /** Callback to invoke on session stop. */
1342         sr_session_stopped_callback stopped_callback;
1343         /** User data to be passed to the session stop callback. */
1344         void *stopped_cb_data;
1345
1346         /** Mutex protecting the main context pointer. */
1347         GMutex main_mutex;
1348         /** Context of the session main loop. */
1349         GMainContext *main_context;
1350
1351         /** Registered event sources for this session. */
1352         GHashTable *event_sources;
1353         /** Session main loop. */
1354         GMainLoop *main_loop;
1355         /** ID of idle source for dispatching the session stop notification. */
1356         unsigned int stop_check_id;
1357         /** Whether the session has been started. */
1358         gboolean running;
1359 };
1360
1361 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
1362                 void *key, GSource *source);
1363 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
1364                 void *key);
1365 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1366                 void *key, GSource *source);
1367 SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1368                 void *key, gintptr fd, int events, int timeout,
1369                 sr_receive_data_callback cb, void *cb_data);
1370
1371 SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
1372                 int events, int timeout, sr_receive_data_callback cb, void *cb_data);
1373 SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
1374                 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1375                 void *cb_data);
1376 SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
1377                 GIOChannel *channel, int events, int timeout,
1378                 sr_receive_data_callback cb, void *cb_data);
1379 SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
1380 SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
1381                 GPollFD *pollfd);
1382 SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
1383                 GIOChannel *channel);
1384
1385 SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi,
1386                 uint32_t key, GVariant *var);
1387 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
1388                 const struct sr_datafeed_packet *packet);
1389 SR_PRIV int sr_sessionfile_check(const char *filename);
1390 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename,
1391                 struct sr_session **session);
1392
1393 /*--- session_file.c --------------------------------------------------------*/
1394
1395 #if !HAVE_ZIP_DISCARD
1396 /* Replace zip_discard() if not available. */
1397 #define zip_discard(zip) sr_zip_discard(zip)
1398 SR_PRIV void sr_zip_discard(struct zip *archive);
1399 #endif
1400
1401 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
1402                         const struct zip_stat *entry);
1403
1404 /*--- analog.c --------------------------------------------------------------*/
1405
1406 SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
1407                            struct sr_analog_encoding *encoding,
1408                            struct sr_analog_meaning *meaning,
1409                            struct sr_analog_spec *spec,
1410                            int digits);
1411
1412 /*--- std.c -----------------------------------------------------------------*/
1413
1414 typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
1415 typedef void (*std_dev_clear_callback)(void *priv);
1416
1417 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx);
1418 SR_PRIV int std_cleanup(const struct sr_dev_driver *di);
1419 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
1420 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
1421 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
1422 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
1423 #ifdef HAVE_SERIAL_COMM
1424 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
1425 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
1426 #endif
1427 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi);
1428 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi);
1429 SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi);
1430 SR_PRIV int std_session_send_df_frame_begin(const struct sr_dev_inst *sdi);
1431 SR_PRIV int std_session_send_df_frame_end(const struct sr_dev_inst *sdi);
1432 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
1433                 std_dev_clear_callback clear_private);
1434 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver);
1435 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di);
1436 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
1437 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices);
1438
1439 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
1440         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
1441         const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
1442         size_t drvsize, const uint32_t devopts[], size_t devsize);
1443
1444 extern SR_PRIV const uint32_t NO_OPTS[1];
1445
1446 #define STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts) \
1447         std_opts_config_list(key, data, sdi, cg, ARRAY_AND_SIZE(scanopts), \
1448                 ARRAY_AND_SIZE(drvopts), ARRAY_AND_SIZE(devopts))
1449
1450 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n);
1451 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n);
1452 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n);
1453 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n);
1454 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step);
1455 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]);
1456 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double dmin, const double dmax, const double dstep);
1457
1458 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high);
1459 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high);
1460
1461 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n);
1462 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n);
1463 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n);
1464 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n);
1465
1466 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n);
1467
1468 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n);
1469 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n);
1470 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n);
1471
1472 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n);
1473 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n);
1474
1475 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n);
1476 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
1477 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
1478
1479 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
1480
1481 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
1482         int baudrate, int bits, int parity, int stopbits,
1483         int flowcontrol, int rts, int dtr);
1484
1485 /*--- resource.c ------------------------------------------------------------*/
1486
1487 SR_PRIV int64_t sr_file_get_size(FILE *file);
1488
1489 SR_PRIV int sr_resource_open(struct sr_context *ctx,
1490                 struct sr_resource *res, int type, const char *name)
1491                 G_GNUC_WARN_UNUSED_RESULT;
1492 SR_PRIV int sr_resource_close(struct sr_context *ctx,
1493                 struct sr_resource *res);
1494 SR_PRIV gssize sr_resource_read(struct sr_context *ctx,
1495                 const struct sr_resource *res, void *buf, size_t count)
1496                 G_GNUC_WARN_UNUSED_RESULT;
1497 SR_PRIV void *sr_resource_load(struct sr_context *ctx, int type,
1498                 const char *name, size_t *size, size_t max_size)
1499                 G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
1500
1501 /*--- strutil.c -------------------------------------------------------------*/
1502
1503 SR_PRIV int sr_atol(const char *str, long *ret);
1504 SR_PRIV int sr_atol_base(const char *str, long *ret, char **end, int base);
1505 SR_PRIV int sr_atoi(const char *str, int *ret);
1506 SR_PRIV int sr_atod(const char *str, double *ret);
1507 SR_PRIV int sr_atof(const char *str, float *ret);
1508 SR_PRIV int sr_atod_ascii(const char *str, double *ret);
1509 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
1510
1511 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
1512 SR_PRIV void sr_hexdump_free(GString *s);
1513
1514 /*--- soft-trigger.c --------------------------------------------------------*/
1515
1516 struct soft_trigger_logic {
1517         const struct sr_dev_inst *sdi;
1518         const struct sr_trigger *trigger;
1519         int count;
1520         int unitsize;
1521         int cur_stage;
1522         uint8_t *prev_sample;
1523         uint8_t *pre_trigger_buffer;
1524         uint8_t *pre_trigger_head;
1525         int pre_trigger_size;
1526         int pre_trigger_fill;
1527 };
1528
1529 SR_PRIV int logic_channel_unitsize(GSList *channels);
1530 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
1531                 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
1532                 int pre_trigger_samples);
1533 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
1534 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
1535                 int len, int *pre_trigger_samples);
1536
1537 /*--- serial.c --------------------------------------------------------------*/
1538
1539 #ifdef HAVE_SERIAL_COMM
1540 enum {
1541         SERIAL_RDWR = 1,
1542         SERIAL_RDONLY = 2,
1543 };
1544
1545 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
1546
1547 typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
1548                 const char *desc);
1549 typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
1550
1551 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
1552 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
1553 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
1554 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
1555 SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
1556 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
1557                 const void *buf, size_t count, unsigned int timeout_ms);
1558 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
1559                 const void *buf, size_t count);
1560 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
1561                 size_t count, unsigned int timeout_ms);
1562 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
1563                 size_t count);
1564 SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
1565                 serial_rx_chunk_callback cb, void *cb_data);
1566 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
1567                 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
1568 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
1569                 const char *paramstr);
1570 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
1571                 int *buflen, gint64 timeout_ms);
1572 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
1573                                  uint8_t *buf, size_t *buflen,
1574                                  size_t packet_size,
1575                                  packet_valid_callback is_valid,
1576                                  uint64_t timeout_ms);
1577 SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
1578                                       const char **serial_options);
1579 SR_PRIV int serial_source_add(struct sr_session *session,
1580                 struct sr_serial_dev_inst *serial, int events, int timeout,
1581                 sr_receive_data_callback cb, void *cb_data);
1582 SR_PRIV int serial_source_remove(struct sr_session *session,
1583                 struct sr_serial_dev_inst *serial);
1584 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
1585 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
1586
1587 SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial);
1588 SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial);
1589 SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
1590                 const uint8_t *data, size_t len);
1591 SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
1592                 uint8_t *data, size_t len);
1593
1594 struct ser_lib_functions {
1595         int (*open)(struct sr_serial_dev_inst *serial, int flags);
1596         int (*close)(struct sr_serial_dev_inst *serial);
1597         int (*flush)(struct sr_serial_dev_inst *serial);
1598         int (*drain)(struct sr_serial_dev_inst *serial);
1599         int (*write)(struct sr_serial_dev_inst *serial,
1600                         const void *buf, size_t count,
1601                         int nonblocking, unsigned int timeout_ms);
1602         int (*read)(struct sr_serial_dev_inst *serial,
1603                         void *buf, size_t count,
1604                         int nonblocking, unsigned int timeout_ms);
1605         int (*set_params)(struct sr_serial_dev_inst *serial,
1606                         int baudrate, int bits, int parity, int stopbits,
1607                         int flowcontrol, int rts, int dtr);
1608         int (*setup_source_add)(struct sr_session *session,
1609                         struct sr_serial_dev_inst *serial,
1610                         int events, int timeout,
1611                         sr_receive_data_callback cb, void *cb_data);
1612         int (*setup_source_remove)(struct sr_session *session,
1613                         struct sr_serial_dev_inst *serial);
1614         GSList *(*list)(GSList *list, sr_ser_list_append_t append);
1615         GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
1616                         uint16_t vendor_id, uint16_t product_id);
1617         int (*get_frame_format)(struct sr_serial_dev_inst *serial,
1618                         int *baud, int *bits);
1619         size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
1620 };
1621 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
1622 SR_PRIV int ser_name_is_hid(struct sr_serial_dev_inst *serial);
1623 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_hid;
1624 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial);
1625 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt;
1626
1627 #ifdef HAVE_LIBHIDAPI
1628 struct vid_pid_item {
1629         uint16_t vid, pid;
1630 };
1631
1632 struct ser_hid_chip_functions {
1633         const char *chipname;
1634         const char *chipdesc;
1635         const struct vid_pid_item *vid_pid_items;
1636         const int max_bytes_per_request;
1637         int (*set_params)(struct sr_serial_dev_inst *serial,
1638                         int baudrate, int bits, int parity, int stopbits,
1639                         int flowcontrol, int rts, int dtr);
1640         int (*read_bytes)(struct sr_serial_dev_inst *serial,
1641                         uint8_t *data, int space, unsigned int timeout);
1642         int (*write_bytes)(struct sr_serial_dev_inst *serial,
1643                         const uint8_t *data, int space);
1644         int (*flush)(struct sr_serial_dev_inst *serial);
1645         int (*drain)(struct sr_serial_dev_inst *serial);
1646 };
1647 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x;
1648 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325;
1649 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110;
1650 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor;
1651 SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid);
1652 #endif
1653 #endif
1654
1655 /*--- bt/ API ---------------------------------------------------------------*/
1656
1657 #ifdef HAVE_BLUETOOTH
1658 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx);
1659
1660 struct sr_bt_desc;
1661 typedef void (*sr_bt_scan_cb)(void *cb_data, const char *addr, const char *name);
1662 typedef int (*sr_bt_data_cb)(void *cb_data, uint8_t *data, size_t dlen);
1663
1664 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void);
1665 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc);
1666
1667 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
1668         sr_bt_scan_cb cb, void *cb_data);
1669 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
1670         sr_bt_data_cb cb, void *cb_data);
1671 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr);
1672 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr);
1673 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel);
1674 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
1675         uint16_t read_handle, uint16_t write_handle,
1676         uint16_t cccd_handle, uint16_t cccd_value);
1677
1678 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration);
1679 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration);
1680
1681 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc);
1682 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc);
1683 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc);
1684
1685 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc,
1686         void *data, size_t len);
1687 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
1688         const void *data, size_t len);
1689
1690 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc);
1691 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc);
1692 #endif
1693
1694 /*--- ezusb.c ---------------------------------------------------------------*/
1695
1696 #ifdef HAVE_LIBUSB_1_0
1697 SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
1698 SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
1699                                    const char *name);
1700 SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
1701                                   int configuration, const char *name);
1702 #endif
1703
1704 /*--- usb.c -----------------------------------------------------------------*/
1705
1706 #ifdef HAVE_LIBUSB_1_0
1707 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
1708 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
1709 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
1710 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
1711                 int timeout, sr_receive_data_callback cb, void *cb_data);
1712 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
1713 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
1714 SR_PRIV gboolean usb_match_manuf_prod(libusb_device *dev,
1715                 const char *manufacturer, const char *product);
1716 #endif
1717
1718 /*--- binary_helpers.c ------------------------------------------------------*/
1719
1720 /** Binary value type */
1721 enum binary_value_type {
1722         BVT_UINT8 = 0,
1723         BVT_BE_UINT8 = BVT_UINT8,
1724         BVT_LE_UINT8 = BVT_UINT8,
1725
1726         BVT_BE_UINT16,
1727         BVT_BE_UINT32,
1728         BVT_BE_UINT64,
1729         BVT_BE_FLOAT,
1730
1731         BVT_LE_UINT16,
1732         BVT_LE_UINT32,
1733         BVT_LE_UINT64,
1734         BVT_LE_FLOAT,
1735 };
1736
1737 /** Binary value specification */
1738 struct binary_value_spec {
1739         /** Offset into binary blob */
1740         size_t offset;
1741         /** Data type to decode */
1742         enum binary_value_type type;
1743         /** Scale factor to get native units */
1744         float scale;
1745 };
1746
1747 /** Binary channel definition */
1748 struct binary_analog_channel {
1749         /** Channel name */
1750         const char *name;
1751         /** Binary value in data stream */
1752         struct binary_value_spec spec;
1753         /** Significant digits */
1754         int digits;
1755         /** Measured quantity */
1756         enum sr_mq mq;
1757         /** Measured unit */
1758         enum sr_unit unit;
1759 };
1760
1761 /**
1762  * Read extract a value from a binary blob.
1763  *
1764  * @param out Pointer to output buffer.
1765  * @param spec Binary value specification
1766  * @param data Pointer to binary blob
1767  * @param length Size of binary blob
1768  * @return SR_OK on success, SR_ERR_* error code on failure.
1769  */
1770 SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, const void *data, size_t length);
1771
1772 /**
1773  * Send an analog channel packet based on a binary analog channel
1774  * specification.
1775  *
1776  * @param sdi Device instance
1777  * @param ch Sigrok channel
1778  * @param spec Channel specification
1779  * @param data Pointer to binary blob
1780  * @param length Size of binary blob
1781  * @return SR_OK on success, SR_ERR_* error code on failure.
1782  */
1783 SR_PRIV int bv_send_analog_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch,
1784                                    const struct binary_analog_channel *spec, const void *data, size_t length);
1785
1786 /*--- crc.c -----------------------------------------------------------------*/
1787
1788 #define SR_CRC16_DEFAULT_INIT 0xffffU
1789
1790 /**
1791  * Calculate a CRC16 checksum using the 0x8005 polynomial.
1792  *
1793  * This CRC16 flavor is also known as CRC16-ANSI or CRC16-MODBUS.
1794  *
1795  * @param crc Initial value (typically 0xffff)
1796  * @param buffer Input buffer
1797  * @param len Buffer length
1798  * @return Checksum
1799  */
1800 SR_PRIV uint16_t sr_crc16(uint16_t crc, const uint8_t *buffer, int len);
1801
1802 /*--- modbus/modbus.c -------------------------------------------------------*/
1803
1804 struct sr_modbus_dev_inst {
1805         const char *name;
1806         const char *prefix;
1807         int priv_size;
1808         GSList *(*scan)(int modbusaddr);
1809         int (*dev_inst_new)(void *priv, const char *resource,
1810                 char **params, const char *serialcomm, int modbusaddr);
1811         int (*open)(void *priv);
1812         int (*source_add)(struct sr_session *session, void *priv, int events,
1813                 int timeout, sr_receive_data_callback cb, void *cb_data);
1814         int (*source_remove)(struct sr_session *session, void *priv);
1815         int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
1816         int (*read_begin)(void *priv, uint8_t *function_code);
1817         int (*read_data)(void *priv, uint8_t *buf, int maxlen);
1818         int (*read_end)(void *priv);
1819         int (*close)(void *priv);
1820         void (*free)(void *priv);
1821         unsigned int read_timeout_ms;
1822         void *priv;
1823 };
1824
1825 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
1826                 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
1827 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
1828                 const char *serialcomm, int modbusaddr);
1829 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
1830 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
1831                 struct sr_modbus_dev_inst *modbus, int events, int timeout,
1832                 sr_receive_data_callback cb, void *cb_data);
1833 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
1834                 struct sr_modbus_dev_inst *modbus);
1835 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
1836                               uint8_t *request, int request_size);
1837 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
1838                             uint8_t *reply, int reply_size);
1839 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
1840                                     uint8_t *request, int request_size,
1841                                     uint8_t *reply, int reply_size);
1842 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
1843                                  int address, int nb_coils, uint8_t *coils);
1844 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
1845                                              int address, int nb_registers,
1846                                              uint16_t *registers);
1847 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
1848                                  int address, int value);
1849 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
1850                                                int address, int nb_registers,
1851                                                uint16_t *registers);
1852 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
1853 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
1854
1855 /*--- dmm/es519xx.c ---------------------------------------------------------*/
1856
1857 /**
1858  * All 11-byte es519xx chips repeat each block twice for each conversion cycle
1859  * so always read 2 blocks at a time.
1860  */
1861 #define ES519XX_11B_PACKET_SIZE (11 * 2)
1862 #define ES519XX_14B_PACKET_SIZE 14
1863
1864 struct es519xx_info {
1865         gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
1866         gboolean is_milli, is_resistance, is_continuity, is_diode;
1867         gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
1868         gboolean is_temperature, is_celsius, is_fahrenheit;
1869         gboolean is_adp0, is_adp1, is_adp2, is_adp3;
1870         gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
1871         gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
1872         gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
1873         uint32_t baudrate;
1874         int packet_size;
1875         gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
1876         int digits;
1877 };
1878
1879 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
1880 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
1881                 struct sr_datafeed_analog *analog, void *info);
1882 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
1883 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
1884                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1885 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
1886 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
1887                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1888 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
1889 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
1890                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1891 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
1892 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
1893                 struct sr_datafeed_analog *analog, void *info);
1894 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
1895 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
1896                 struct sr_datafeed_analog *analog, void *info);
1897 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
1898 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
1899                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1900
1901 /*--- dmm/fs9922.c ----------------------------------------------------------*/
1902
1903 #define FS9922_PACKET_SIZE 14
1904
1905 struct fs9922_info {
1906         gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
1907         gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
1908         gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
1909         gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
1910         gboolean is_celsius, is_fahrenheit;
1911         int bargraph_sign, bargraph_value;
1912 };
1913
1914 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
1915 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
1916                             struct sr_datafeed_analog *analog, void *info);
1917 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
1918
1919 /*--- dmm/fs9721.c ----------------------------------------------------------*/
1920
1921 #define FS9721_PACKET_SIZE 14
1922
1923 struct fs9721_info {
1924         gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
1925         gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
1926         gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
1927         gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
1928 };
1929
1930 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
1931 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
1932                             struct sr_datafeed_analog *analog, void *info);
1933 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
1934 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
1935 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
1936 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
1937 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
1938
1939 /*--- dmm/ms2115b.c ---------------------------------------------------------*/
1940
1941 #define MS2115B_PACKET_SIZE 9
1942
1943 enum ms2115b_display {
1944         MS2115B_DISPLAY_MAIN,
1945         MS2115B_DISPLAY_SUB,
1946         MS2115B_DISPLAY_COUNT,
1947 };
1948
1949 struct ms2115b_info {
1950         /* Selected channel. */
1951         size_t ch_idx;
1952         gboolean is_ac, is_dc, is_auto;
1953         gboolean is_diode, is_beep, is_farad;
1954         gboolean is_ohm, is_ampere, is_volt, is_hz;
1955         gboolean is_duty_cycle, is_percent;
1956 };
1957
1958 extern SR_PRIV const char *ms2115b_channel_formats[];
1959 SR_PRIV gboolean sr_ms2115b_packet_valid(const uint8_t *buf);
1960 SR_PRIV int sr_ms2115b_parse(const uint8_t *buf, float *floatval,
1961         struct sr_datafeed_analog *analog, void *info);
1962
1963 /*--- dmm/ms8250d.c ---------------------------------------------------------*/
1964
1965 #define MS8250D_PACKET_SIZE 18
1966
1967 struct ms8250d_info {
1968         gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
1969         gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
1970         gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
1971         gboolean is_ncv, is_min, is_max, is_sign, is_autotimer;
1972 };
1973
1974 SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf);
1975 SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
1976                              struct sr_datafeed_analog *analog, void *info);
1977
1978 /*--- dmm/dtm0660.c ---------------------------------------------------------*/
1979
1980 #define DTM0660_PACKET_SIZE 15
1981
1982 struct dtm0660_info {
1983         gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
1984         gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
1985         gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
1986         gboolean is_degf, is_degc, is_c2c1_01, is_c2c1_00, is_apo, is_min;
1987         gboolean is_minmax, is_max, is_sign;
1988 };
1989
1990 SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf);
1991 SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
1992                         struct sr_datafeed_analog *analog, void *info);
1993
1994 /*--- dmm/m2110.c -----------------------------------------------------------*/
1995
1996 #define BBCGM_M2110_PACKET_SIZE 9
1997
1998 /* Dummy info struct. The parser does not use it. */
1999 struct m2110_info { int dummy; };
2000
2001 SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
2002 SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
2003                              struct sr_datafeed_analog *analog, void *info);
2004
2005 /*--- dmm/metex14.c ---------------------------------------------------------*/
2006
2007 #define METEX14_PACKET_SIZE 14
2008
2009 struct metex14_info {
2010         size_t ch_idx;
2011         gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
2012         gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
2013         gboolean is_hertz, is_ohm, is_celsius, is_fahrenheit, is_watt;
2014         gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2015         gboolean is_gain, is_decibel, is_power, is_decibel_mw, is_power_factor;
2016         gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
2017 };
2018
2019 #ifdef HAVE_SERIAL_COMM
2020 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
2021 #endif
2022 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
2023 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
2024                              struct sr_datafeed_analog *analog, void *info);
2025 SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf);
2026 SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
2027                              struct sr_datafeed_analog *analog, void *info);
2028
2029 /*--- dmm/rs9lcd.c ----------------------------------------------------------*/
2030
2031 #define RS9LCD_PACKET_SIZE 9
2032
2033 /* Dummy info struct. The parser does not use it. */
2034 struct rs9lcd_info { int dummy; };
2035
2036 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
2037 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
2038                             struct sr_datafeed_analog *analog, void *info);
2039
2040 /*--- dmm/bm25x.c -----------------------------------------------------------*/
2041
2042 #define BRYMEN_BM25X_PACKET_SIZE 15
2043
2044 /* Dummy info struct. The parser does not use it. */
2045 struct bm25x_info { int dummy; };
2046
2047 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
2048 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
2049                              struct sr_datafeed_analog *analog, void *info);
2050
2051 /*--- dmm/bm86x.c -----------------------------------------------------------*/
2052
2053 #define BRYMEN_BM86X_PACKET_SIZE 24
2054 #define BRYMEN_BM86X_DISPLAY_COUNT 2
2055
2056 struct brymen_bm86x_info { size_t ch_idx; };
2057
2058 #ifdef HAVE_SERIAL_COMM
2059 SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial);
2060 #endif
2061 SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf);
2062 SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *floatval,
2063                 struct sr_datafeed_analog *analog, void *info);
2064
2065 /*--- dmm/ut71x.c -----------------------------------------------------------*/
2066
2067 #define UT71X_PACKET_SIZE 11
2068
2069 struct ut71x_info {
2070         gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
2071         gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
2072         gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
2073         gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
2074 };
2075
2076 SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
2077 SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
2078                 struct sr_datafeed_analog *analog, void *info);
2079
2080 /*--- dmm/vc870.c -----------------------------------------------------------*/
2081
2082 #define VC870_PACKET_SIZE 23
2083
2084 struct vc870_info {
2085         gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
2086         gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
2087         gboolean is_current, is_micro, is_milli, is_power;
2088         gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_rms_value;
2089         gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
2090         gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
2091         gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
2092         gboolean is_lo, is_hi, is_open2;
2093
2094         gboolean is_frequency, is_dual_display, is_auto;
2095 };
2096
2097 SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
2098 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
2099                 struct sr_datafeed_analog *analog, void *info);
2100
2101 /*--- dmm/vc96.c ------------------------------------------------------------*/
2102
2103 #define VC96_PACKET_SIZE 13
2104
2105 struct vc96_info {
2106         size_t ch_idx;
2107         gboolean is_ac, is_dc, is_resistance, is_diode, is_ampere, is_volt;
2108         gboolean is_ohm, is_micro, is_milli, is_kilo, is_mega, is_hfe;
2109         gboolean is_unitless;
2110 };
2111
2112 SR_PRIV gboolean sr_vc96_packet_valid(const uint8_t *buf);
2113 SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval,
2114                 struct sr_datafeed_analog *analog, void *info);
2115
2116 /*--- lcr/es51919.c ---------------------------------------------------------*/
2117
2118 /* Acquisition details which apply to all supported serial-lcr devices. */
2119 struct lcr_parse_info {
2120         size_t ch_idx;
2121         uint64_t output_freq;
2122         const char *circuit_model;
2123 };
2124
2125 #define ES51919_PACKET_SIZE     17
2126 #define ES51919_CHANNEL_COUNT   2
2127 #define ES51919_COMM_PARAM      "9600/8n1/rts=1/dtr=1"
2128
2129 SR_PRIV int es51919_config_get(uint32_t key, GVariant **data,
2130         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2131 SR_PRIV int es51919_config_set(uint32_t key, GVariant *data,
2132         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2133 SR_PRIV int es51919_config_list(uint32_t key, GVariant **data,
2134         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2135 SR_PRIV gboolean es51919_packet_valid(const uint8_t *pkt);
2136 SR_PRIV int es51919_packet_parse(const uint8_t *pkt, float *floatval,
2137         struct sr_datafeed_analog *analog, void *info);
2138
2139 /*--- lcr/vc4080.c ----------------------------------------------------------*/
2140
2141 /* Note: Also uses 'struct lcr_parse_info' from es51919 above. */
2142
2143 #define VC4080_PACKET_SIZE      39
2144 #define VC4080_COMM_PARAM       "1200/8n1"
2145 #define VC4080_WITH_DQ_CHANS    0 /* Enable separate D/Q channels? */
2146
2147 enum vc4080_display {
2148         VC4080_DISPLAY_PRIMARY,
2149         VC4080_DISPLAY_SECONDARY,
2150 #if VC4080_WITH_DQ_CHANS
2151         VC4080_DISPLAY_D_VALUE,
2152         VC4080_DISPLAY_Q_VALUE,
2153 #endif
2154         VC4080_CHANNEL_COUNT,
2155 };
2156
2157 extern SR_PRIV const char *vc4080_channel_formats[VC4080_CHANNEL_COUNT];
2158
2159 SR_PRIV int vc4080_config_list(uint32_t key, GVariant **data,
2160         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2161 SR_PRIV int vc4080_packet_request(struct sr_serial_dev_inst *serial);
2162 SR_PRIV gboolean vc4080_packet_valid(const uint8_t *pkt);
2163 SR_PRIV int vc4080_packet_parse(const uint8_t *pkt, float *floatval,
2164         struct sr_datafeed_analog *analog, void *info);
2165
2166 /*--- dmm/ut372.c -----------------------------------------------------------*/
2167
2168 #define UT372_PACKET_SIZE 27
2169
2170 struct ut372_info {
2171         int dummy;
2172 };
2173
2174 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
2175 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
2176                 struct sr_datafeed_analog *analog, void *info);
2177
2178 /*--- dmm/asycii.c ----------------------------------------------------------*/
2179
2180 #define ASYCII_PACKET_SIZE 16
2181
2182 struct asycii_info {
2183         gboolean is_ac, is_dc, is_ac_and_dc;
2184         gboolean is_resistance, is_capacitance, is_diode, is_gain;
2185         gboolean is_frequency, is_duty_cycle, is_duty_pos, is_duty_neg;
2186         gboolean is_pulse_width, is_period_pos, is_period_neg;
2187         gboolean is_pulse_count, is_count_pos, is_count_neg;
2188         gboolean is_ampere, is_volt, is_volt_ampere, is_farad, is_ohm;
2189         gboolean is_hertz, is_percent, is_seconds, is_decibel;
2190         gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2191         gboolean is_unitless;
2192         gboolean is_peak_min, is_peak_max;
2193         gboolean is_invalid;
2194 };
2195
2196 #ifdef HAVE_SERIAL_COMM
2197 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
2198 #endif
2199 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);
2200 SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
2201                             struct sr_datafeed_analog *analog, void *info);
2202
2203 /*--- dmm/eev121gw.c --------------------------------------------------------*/
2204
2205 #define EEV121GW_PACKET_SIZE 19
2206
2207 enum eev121gw_display {
2208         EEV121GW_DISPLAY_MAIN,
2209         EEV121GW_DISPLAY_SUB,
2210         EEV121GW_DISPLAY_BAR,
2211         EEV121GW_DISPLAY_COUNT,
2212 };
2213
2214 struct eev121gw_info {
2215         /* Selected channel. */
2216         size_t ch_idx;
2217         /*
2218          * Measured value, number and sign/overflow flags, scale factor
2219          * and significant digits.
2220          */
2221         uint32_t uint_value;
2222         gboolean is_ofl, is_neg;
2223         int factor, digits;
2224         /* Currently active mode (meter's function). */
2225         gboolean is_ac, is_dc, is_voltage, is_current, is_power, is_gain;
2226         gboolean is_resistance, is_capacitance, is_diode, is_temperature;
2227         gboolean is_continuity, is_frequency, is_period, is_duty_cycle;
2228         /* Quantities associated with mode/function. */
2229         gboolean is_ampere, is_volt, is_volt_ampere, is_dbm;
2230         gboolean is_ohm, is_farad, is_celsius, is_fahrenheit;
2231         gboolean is_hertz, is_seconds, is_percent, is_loop_current;
2232         gboolean is_unitless, is_logic;
2233         /* Other indicators. */
2234         gboolean is_min, is_max, is_avg, is_1ms_peak, is_rel, is_hold;
2235         gboolean is_low_pass, is_mem, is_bt, is_auto_range, is_test;
2236         gboolean is_auto_poweroff, is_low_batt;
2237 };
2238
2239 extern SR_PRIV const char *eev121gw_channel_formats[];
2240 SR_PRIV gboolean sr_eev121gw_packet_valid(const uint8_t *buf);
2241 SR_PRIV int sr_eev121gw_3displays_parse(const uint8_t *buf, float *floatval,
2242                 struct sr_datafeed_analog *analog, void *info);
2243
2244 /*--- scale/kern.c ----------------------------------------------------------*/
2245
2246 struct kern_info {
2247         gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
2248         gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
2249         gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
2250         int buflen;
2251 };
2252
2253 SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
2254 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
2255                 struct sr_datafeed_analog *analog, void *info);
2256
2257 /*--- sw_limits.c -----------------------------------------------------------*/
2258
2259 struct sr_sw_limits {
2260         uint64_t limit_samples;
2261         uint64_t limit_frames;
2262         uint64_t limit_msec;
2263         uint64_t samples_read;
2264         uint64_t frames_read;
2265         uint64_t start_time;
2266 };
2267
2268 SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
2269         GVariant **data);
2270 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
2271         GVariant *data);
2272 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
2273 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
2274 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
2275         uint64_t samples_read);
2276 SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
2277         uint64_t frames_read);
2278 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
2279
2280 #endif