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