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