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