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