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