2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #ifndef LIBSIGROK_LIBSIGROK_INTERNAL_H
21 #define LIBSIGROK_LIBSIGROK_INTERNAL_H
29 #ifdef HAVE_LIBSERIALPORT
30 #include <libserialport.h>
32 #ifdef HAVE_LIBUSB_1_0
49 * libsigrok private header file, only to be used internally.
52 /*--- Macros ----------------------------------------------------------------*/
55 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
58 #ifndef ARRAY_AND_SIZE
59 #define ARRAY_AND_SIZE(a) (a), ARRAY_SIZE(a)
63 #define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) /* Since 2.58. */
66 #define SR_RECEIVE_DATA_CALLBACK(f) \
67 ((sr_receive_data_callback) (void (*)(void)) (f))
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
74 static inline uint8_t read_u8(const uint8_t *p)
78 #define R8(x) read_u8((const uint8_t *)(x))
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
85 static inline int8_t read_i8(const uint8_t *p)
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
95 static inline uint16_t read_u16be(const uint8_t *p)
105 #define RB16(x) read_u16be((const uint8_t *)(x))
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
112 static inline uint16_t read_u16le(const uint8_t *p)
122 #define RL16(x) read_u16le((const uint8_t *)(x))
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
129 static inline int16_t read_i16be(const uint8_t *p)
139 #define RB16S(x) read_i16be((const uint8_t *)(x))
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
146 static inline int16_t read_i16le(const uint8_t *p)
156 #define RL16S(x) read_i16le((const uint8_t *)(x))
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
163 static inline uint32_t read_u24le(const uint8_t *p)
176 * Read a 24 bits big endian unsigned integer out of memory.
177 * @param x a pointer to the input memory
178 * @return the corresponding unsigned integer
180 static inline uint32_t read_u24be(const uint8_t *p)
193 * Read a 32 bits big endian unsigned integer out of memory.
194 * @param x a pointer to the input memory
195 * @return the corresponding unsigned integer
197 static inline uint32_t read_u32be(const uint8_t *p)
209 #define RB32(x) read_u32be((const uint8_t *)(x))
212 * Read a 32 bits little endian unsigned integer out of memory.
213 * @param x a pointer to the input memory
214 * @return the corresponding unsigned integer
216 static inline uint32_t read_u32le(const uint8_t *p)
228 #define RL32(x) read_u32le((const uint8_t *)(x))
231 * Read a 32 bits big endian signed integer out of memory.
232 * @param x a pointer to the input memory
233 * @return the corresponding signed integer
235 static inline int32_t read_i32be(const uint8_t *p)
245 #define RB32S(x) read_i32be((const uint8_t *)(x))
248 * Read a 32 bits little endian signed integer out of memory.
249 * @param x a pointer to the input memory
250 * @return the corresponding signed integer
252 static inline int32_t read_i32le(const uint8_t *p)
262 #define RL32S(x) read_i32le((const uint8_t *)(x))
265 * Read a 64 bits big endian unsigned integer out of memory.
266 * @param x a pointer to the input memory
267 * @return the corresponding unsigned integer
269 static inline uint64_t read_u64be(const uint8_t *p)
285 #define RB64(x) read_u64be((const uint8_t *)(x))
288 * Read a 64 bits little endian unsigned integer out of memory.
289 * @param x a pointer to the input memory
290 * @return the corresponding unsigned integer
292 static inline uint64_t read_u64le(const uint8_t *p)
308 #define RL64(x) read_u64le((const uint8_t *)(x))
311 * Read a 64 bits big endian signed integer out of memory.
312 * @param x a pointer to the input memory
313 * @return the corresponding unsigned integer
315 static inline int64_t read_i64be(const uint8_t *p)
325 #define RB64S(x) read_i64be((const uint8_t *)(x))
328 * Read a 64 bits little endian signed integer out of memory.
329 * @param x a pointer to the input memory
330 * @return the corresponding unsigned integer
332 static inline int64_t read_i64le(const uint8_t *p)
342 #define RL64S(x) read_i64le((const uint8_t *)(x))
345 * Read a 32 bits big endian float out of memory (single precision).
346 * @param x a pointer to the input memory
347 * @return the corresponding float
349 static inline float read_fltbe(const uint8_t *p)
352 * Implementor's note: Strictly speaking the "union" trick
353 * is not portable. But this phrase was found to work on the
354 * project's supported platforms, and serve well until a more
355 * appropriate phrase is found.
357 union { uint32_t u32; float flt; } u;
360 u.u32 = read_u32be(p);
365 #define RBFL(x) read_fltbe((const uint8_t *)(x))
368 * Read a 32 bits little endian float out of memory (single precision).
369 * @param x a pointer to the input memory
370 * @return the corresponding float
372 static inline float read_fltle(const uint8_t *p)
375 * Implementor's note: Strictly speaking the "union" trick
376 * is not portable. But this phrase was found to work on the
377 * project's supported platforms, and serve well until a more
378 * appropriate phrase is found.
380 union { uint32_t u32; float flt; } u;
383 u.u32 = read_u32le(p);
388 #define RLFL(x) read_fltle((const uint8_t *)(x))
391 * Read a 64 bits big endian float out of memory (double precision).
392 * @param x a pointer to the input memory
393 * @return the corresponding floating point value
395 static inline double read_dblbe(const uint8_t *p)
398 * Implementor's note: Strictly speaking the "union" trick
399 * is not portable. But this phrase was found to work on the
400 * project's supported platforms, and serve well until a more
401 * appropriate phrase is found.
403 union { uint64_t u64; double flt; } u;
406 u.u64 = read_u64be(p);
413 * Read a 64 bits little endian float out of memory (double precision).
414 * @param x a pointer to the input memory
415 * @return the corresponding floating point value
417 static inline double read_dblle(const uint8_t *p)
420 * Implementor's note: Strictly speaking the "union" trick
421 * is not portable. But this phrase was found to work on the
422 * project's supported platforms, and serve well until a more
423 * appropriate phrase is found.
425 union { uint64_t u64; double flt; } u;
428 u.u64 = read_u64le(p);
433 #define RLDB(x) read_dblle((const uint8_t *)(x))
436 * Write a 8 bits unsigned integer to memory.
437 * @param p a pointer to the output memory
438 * @param x the input unsigned integer
440 static inline void write_u8(uint8_t *p, uint8_t x)
444 #define W8(p, x) write_u8((uint8_t *)(p), (uint8_t)(x))
447 * Write a 16 bits unsigned integer to memory stored as big endian.
448 * @param p a pointer to the output memory
449 * @param x the input unsigned integer
451 static inline void write_u16be(uint8_t *p, uint16_t x)
453 p[1] = x & 0xff; x >>= 8;
454 p[0] = x & 0xff; x >>= 8;
456 #define WB16(p, x) write_u16be((uint8_t *)(p), (uint16_t)(x))
459 * Write a 16 bits unsigned integer to memory stored as little endian.
460 * @param p a pointer to the output memory
461 * @param x the input unsigned integer
463 static inline void write_u16le(uint8_t *p, uint16_t x)
465 p[0] = x & 0xff; x >>= 8;
466 p[1] = x & 0xff; x >>= 8;
468 #define WL16(p, x) write_u16le((uint8_t *)(p), (uint16_t)(x))
471 * Write a 24 bits unsigned integer to memory stored as little endian.
472 * @param p a pointer to the output memory
473 * @param x the input unsigned integer
475 static inline void write_u24le(uint8_t *p, uint32_t x)
477 p[0] = x & 0xff; x >>= 8;
478 p[1] = x & 0xff; x >>= 8;
479 p[2] = x & 0xff; x >>= 8;
481 #define WL24(p, x) write_u24le((uint8_t *)(p), (uint32_t)(x))
484 * Write a 32 bits unsigned integer to memory stored as big endian.
485 * @param p a pointer to the output memory
486 * @param x the input unsigned integer
488 static inline void write_u32be(uint8_t *p, uint32_t x)
490 p[3] = x & 0xff; x >>= 8;
491 p[2] = x & 0xff; x >>= 8;
492 p[1] = x & 0xff; x >>= 8;
493 p[0] = x & 0xff; x >>= 8;
495 #define WB32(p, x) write_u32be((uint8_t *)(p), (uint32_t)(x))
498 * Write a 32 bits unsigned integer to memory stored as little endian.
499 * @param p a pointer to the output memory
500 * @param x the input unsigned integer
502 static inline void write_u32le(uint8_t *p, uint32_t x)
504 p[0] = x & 0xff; x >>= 8;
505 p[1] = x & 0xff; x >>= 8;
506 p[2] = x & 0xff; x >>= 8;
507 p[3] = x & 0xff; x >>= 8;
509 #define WL32(p, x) write_u32le((uint8_t *)(p), (uint32_t)(x))
512 * Write a 40 bits unsigned integer to memory stored as little endian.
513 * @param p a pointer to the output memory
514 * @param x the input unsigned integer
516 static inline void write_u40le(uint8_t *p, uint64_t x)
518 p[0] = x & 0xff; x >>= 8;
519 p[1] = x & 0xff; x >>= 8;
520 p[2] = x & 0xff; x >>= 8;
521 p[3] = x & 0xff; x >>= 8;
522 p[4] = x & 0xff; x >>= 8;
524 #define WL40(p, x) write_u40le((uint8_t *)(p), (uint64_t)(x))
527 * Write a 48 bits unsigned integer to memory stored as little endian.
528 * @param p a pointer to the output memory
529 * @param x the input unsigned integer
531 static inline void write_u48le(uint8_t *p, uint64_t x)
533 p[0] = x & 0xff; x >>= 8;
534 p[1] = x & 0xff; x >>= 8;
535 p[2] = x & 0xff; x >>= 8;
536 p[3] = x & 0xff; x >>= 8;
537 p[4] = x & 0xff; x >>= 8;
538 p[5] = x & 0xff; x >>= 8;
540 #define WL48(p, x) write_u48le((uint8_t *)(p), (uint64_t)(x))
543 * Write a 64 bits unsigned integer to memory stored as big endian.
544 * @param p a pointer to the output memory
545 * @param x the input unsigned integer
547 static inline void write_u64be(uint8_t *p, uint64_t x)
549 p[7] = x & 0xff; x >>= 8;
550 p[6] = x & 0xff; x >>= 8;
551 p[5] = x & 0xff; x >>= 8;
552 p[4] = x & 0xff; x >>= 8;
553 p[3] = x & 0xff; x >>= 8;
554 p[2] = x & 0xff; x >>= 8;
555 p[1] = x & 0xff; x >>= 8;
556 p[0] = x & 0xff; x >>= 8;
560 * Write a 64 bits unsigned integer to memory stored as little endian.
561 * @param p a pointer to the output memory
562 * @param x the input unsigned integer
564 static inline void write_u64le(uint8_t *p, uint64_t x)
566 p[0] = x & 0xff; x >>= 8;
567 p[1] = x & 0xff; x >>= 8;
568 p[2] = x & 0xff; x >>= 8;
569 p[3] = x & 0xff; x >>= 8;
570 p[4] = x & 0xff; x >>= 8;
571 p[5] = x & 0xff; x >>= 8;
572 p[6] = x & 0xff; x >>= 8;
573 p[7] = x & 0xff; x >>= 8;
575 #define WL64(p, x) write_u64le((uint8_t *)(p), (uint64_t)(x))
578 * Write a 32 bits float to memory stored as big endian.
579 * @param p a pointer to the output memory
580 * @param x the input float
582 static inline void write_fltbe(uint8_t *p, float x)
584 union { uint32_t u; float f; } u;
588 #define WBFL(p, x) write_fltbe((uint8_t *)(p), (x))
591 * Write a 32 bits float to memory stored as little endian.
592 * @param p a pointer to the output memory
593 * @param x the input float
595 static inline void write_fltle(uint8_t *p, float x)
597 union { uint32_t u; float f; } u;
601 #define WLFL(p, x) write_fltle((uint8_t *)(p), float (x))
604 * Write a 64 bits float to memory stored as little endian.
605 * @param p a pointer to the output memory
606 * @param x the input floating point value
608 static inline void write_dblle(uint8_t *p, double x)
610 union { uint64_t u; double f; } u;
614 #define WLDB(p, x) write_dblle((uint8_t *)(p), float (x))
616 /* Endianess conversion helpers with read/write position increment. */
619 * Read unsigned 8bit integer from raw memory, increment read position.
620 * @param[in, out] p Pointer into byte stream.
621 * @return Retrieved integer value, unsigned.
623 static inline uint8_t read_u8_inc(const uint8_t **p)
636 * Read unsigned 8bit integer, check length, increment read position.
637 * @param[in, out] p Pointer into byte stream.
638 * @param[in, out] l Remaining input payload length.
639 * @return Retrieved integer value, unsigned.
641 static inline uint8_t read_u8_inc_len(const uint8_t **p, size_t *l)
647 if (l && *l < sizeof(v)) {
660 * Read signed 8bit integer from raw memory, increment read position.
661 * @param[in, out] p Pointer into byte stream.
662 * @return Retrieved integer value, signed.
664 static inline int8_t read_i8_inc(const uint8_t **p)
677 * Read unsigned 16bit integer from raw memory (big endian format), increment read position.
678 * @param[in, out] p Pointer into byte stream.
679 * @return Retrieved integer value, unsigned.
681 static inline uint16_t read_u16be_inc(const uint8_t **p)
694 * Read unsigned 16bit integer from raw memory (little endian format), increment read position.
695 * @param[in, out] p Pointer into byte stream.
696 * @return Retrieved integer value, unsigned.
698 static inline uint16_t read_u16le_inc(const uint8_t **p)
711 * Read unsigned 16bit integer (LE format), check length, increment position.
712 * @param[in, out] p Pointer into byte stream.
713 * @param[in, out] l Remaining input payload length.
714 * @return Retrieved integer value, unsigned.
716 static inline uint16_t read_u16le_inc_len(const uint8_t **p, size_t *l)
722 if (l && *l < sizeof(v)) {
735 * Read signed 16bit integer from raw memory (big endian format), increment read position.
736 * @param[in, out] p Pointer into byte stream.
737 * @return Retrieved integer value, signed.
739 static inline int16_t read_i16be_inc(const uint8_t **p)
752 * Read signed 16bit integer from raw memory (little endian format), increment read position.
753 * @param[in, out] p Pointer into byte stream.
754 * @return Retrieved integer value, signed.
756 static inline int16_t read_i16le_inc(const uint8_t **p)
769 * Read unsigned 24bit integer from raw memory (little endian format), increment read position.
770 * @param[in, out] p Pointer into byte stream.
771 * @return Retrieved integer value, unsigned.
773 static inline uint32_t read_u24le_inc(const uint8_t **p)
780 *p += 3 * sizeof(uint8_t);
786 * Read unsigned 32bit integer from raw memory (big endian format), increment read position.
787 * @param[in, out] p Pointer into byte stream.
788 * @return Retrieved integer value, unsigned.
790 static inline uint32_t read_u32be_inc(const uint8_t **p)
803 * Read unsigned 32bit integer from raw memory (little endian format), increment read position.
804 * @param[in, out] p Pointer into byte stream.
805 * @return Retrieved integer value, unsigned.
807 static inline uint32_t read_u32le_inc(const uint8_t **p)
820 * Read signed 32bit integer from raw memory (big endian format), increment read position.
821 * @param[in, out] p Pointer into byte stream.
822 * @return Retrieved integer value, signed.
824 static inline int32_t read_i32be_inc(const uint8_t **p)
837 * Read signed 32bit integer from raw memory (little endian format), increment read position.
838 * @param[in, out] p Pointer into byte stream.
839 * @return Retrieved integer value, signed.
841 static inline int32_t read_i32le_inc(const uint8_t **p)
854 * Read unsigned 64bit integer from raw memory (big endian format), increment read position.
855 * @param[in, out] p Pointer into byte stream.
856 * @return Retrieved integer value, unsigned.
858 static inline uint64_t read_u64be_inc(const uint8_t **p)
871 * Read unsigned 64bit integer from raw memory (little endian format), increment read position.
872 * @param[in, out] p Pointer into byte stream.
873 * @return Retrieved integer value, unsigned.
875 static inline uint64_t read_u64le_inc(const uint8_t **p)
888 * Read 32bit float from raw memory (big endian format), increment read position.
889 * @param[in, out] p Pointer into byte stream.
890 * @return Retrieved float value.
892 static inline float read_fltbe_inc(const uint8_t **p)
905 * Read 32bit float from raw memory (little endian format), increment read position.
906 * @param[in, out] p Pointer into byte stream.
907 * @return Retrieved float value.
909 static inline float read_fltle_inc(const uint8_t **p)
922 * Read 64bit float from raw memory (big endian format), increment read position.
923 * @param[in, out] p Pointer into byte stream.
924 * @return Retrieved float value.
926 static inline double read_dblbe_inc(const uint8_t **p)
939 * Read 64bit float from raw memory (little endian format), increment read position.
940 * @param[in, out] p Pointer into byte stream.
941 * @return Retrieved float value.
943 static inline double read_dblle_inc(const uint8_t **p)
956 * Write unsigned 8bit integer to raw memory, increment write position.
957 * @param[in, out] p Pointer into byte stream.
958 * @param[in] x Value to write.
960 static inline void write_u8_inc(uint8_t **p, uint8_t x)
969 * Write unsigned 16bit big endian integer to raw memory, increment write position.
970 * @param[in, out] p Pointer into byte stream.
971 * @param[in] x Value to write.
973 static inline void write_u16be_inc(uint8_t **p, uint16_t x)
982 * Write unsigned 16bit little endian integer to raw memory, increment write position.
983 * @param[in, out] p Pointer into byte stream.
984 * @param[in] x Value to write.
986 static inline void write_u16le_inc(uint8_t **p, uint16_t x)
995 * Write unsigned 24bit liggle endian integer to raw memory, increment write position.
996 * @param[in, out] p Pointer into byte stream.
997 * @param[in] x Value to write.
999 static inline void write_u24le_inc(uint8_t **p, uint32_t x)
1004 *p += 3 * sizeof(uint8_t);
1008 * Write unsigned 32bit big endian integer to raw memory, increment write position.
1009 * @param[in, out] p Pointer into byte stream.
1010 * @param[in] x Value to write.
1012 static inline void write_u32be_inc(uint8_t **p, uint32_t x)
1021 * Write unsigned 32bit little endian integer to raw memory, increment write position.
1022 * @param[in, out] p Pointer into byte stream.
1023 * @param[in] x Value to write.
1025 static inline void write_u32le_inc(uint8_t **p, uint32_t x)
1034 * Write unsigned 40bit little endian integer to raw memory, increment write position.
1035 * @param[in, out] p Pointer into byte stream.
1036 * @param[in] x Value to write.
1038 static inline void write_u40le_inc(uint8_t **p, uint64_t x)
1043 *p += 5 * sizeof(uint8_t);
1047 * Write unsigned 48bit little endian integer to raw memory, increment write position.
1048 * @param[in, out] p Pointer into byte stream.
1049 * @param[in] x Value to write.
1051 static inline void write_u48le_inc(uint8_t **p, uint64_t x)
1056 *p += 48 / 8 * sizeof(uint8_t);
1060 * Write unsigned 64bit little endian integer to raw memory, increment write position.
1061 * @param[in, out] p Pointer into byte stream.
1062 * @param[in] x Value to write.
1064 static inline void write_u64le_inc(uint8_t **p, uint64_t x)
1073 * Write single precision little endian float to raw memory, increment write position.
1074 * @param[in, out] p Pointer into byte stream.
1075 * @param[in] x Value to write.
1077 static inline void write_fltle_inc(uint8_t **p, float x)
1086 * Write double precision little endian float to raw memory, increment write position.
1087 * @param[in, out] p Pointer into byte stream.
1088 * @param[in] x Value to write.
1090 static inline void write_dblle_inc(uint8_t **p, double x)
1098 /* Portability fixes for FreeBSD. */
1100 #define LIBUSB_CLASS_APPLICATION 0xfe
1101 #define libusb_has_capability(x) 0
1102 #define libusb_handle_events_timeout_completed(ctx, tv, c) \
1103 libusb_handle_events_timeout(ctx, tv)
1107 * Convenience for FTDI library version dependency.
1108 * - Version 1.5 introduced ftdi_tciflush(), ftdi_tcoflush(), and
1109 * ftdi_tcioflush() all within the same commit, and deprecated
1110 * ftdi_usb_purge_buffers() which suffered from inverse semantics.
1111 * The API is drop-in compatible (arguments count and data types are
1112 * identical). The libsigrok source code always flushes RX and TX at
1113 * the same time, never individually.
1115 #if defined HAVE_FTDI_TCIOFLUSH && HAVE_FTDI_TCIOFLUSH
1116 # define PURGE_FTDI_BOTH ftdi_tcioflush
1118 # define PURGE_FTDI_BOTH ftdi_usb_purge_buffers
1121 /* Static definitions of structs ending with an all-zero entry are a
1122 * problem when compiling with -Wmissing-field-initializers: GCC
1123 * suppresses the warning only with { 0 }, clang wants { } */
1125 #define ALL_ZERO { }
1127 #define ALL_ZERO { 0 }
1131 #define SR_DRIVER_LIST_SECTION "__DATA,__sr_driver_list"
1133 #define SR_DRIVER_LIST_SECTION "__sr_driver_list"
1136 #if !defined SR_DRIVER_LIST_NOREORDER && defined __has_attribute
1137 #if __has_attribute(no_reorder)
1138 #define SR_DRIVER_LIST_NOREORDER __attribute__((no_reorder))
1141 #if !defined SR_DRIVER_LIST_NOREORDER
1142 #define SR_DRIVER_LIST_NOREORDER /* EMPTY */
1146 * Register a list of hardware drivers.
1148 * This macro can be used to register multiple hardware drivers to the library.
1149 * This is useful when a driver supports multiple similar but slightly
1150 * different devices that require different sr_dev_driver struct definitions.
1152 * For registering only a single driver see SR_REGISTER_DEV_DRIVER().
1156 * #define MY_DRIVER(_name) \
1157 * &(struct sr_dev_driver){ \
1162 * SR_REGISTER_DEV_DRIVER_LIST(my_driver_infos,
1163 * MY_DRIVER("driver 1"),
1164 * MY_DRIVER("driver 2"),
1169 * @param name Name to use for the driver list identifier.
1170 * @param ... Comma separated list of pointers to sr_dev_driver structs.
1172 #define SR_REGISTER_DEV_DRIVER_LIST(name, ...) \
1173 static const struct sr_dev_driver *name[] \
1174 SR_DRIVER_LIST_NOREORDER \
1175 __attribute__((section (SR_DRIVER_LIST_SECTION), used, \
1176 aligned(sizeof(struct sr_dev_driver *)))) \
1182 * Register a hardware driver.
1184 * This macro is used to register a hardware driver with the library. It has
1185 * to be used in order to make the driver accessible to applications using the
1188 * The macro invocation should be placed directly under the struct
1189 * sr_dev_driver definition.
1193 * static struct sr_dev_driver driver_info = {
1197 * SR_REGISTER_DEV_DRIVER(driver_info);
1200 * @param name Identifier name of sr_dev_driver struct to register.
1202 #define SR_REGISTER_DEV_DRIVER(name) \
1203 SR_REGISTER_DEV_DRIVER_LIST(name##_list, &name);
1205 SR_API void sr_drivers_init(struct sr_context *context);
1208 struct sr_dev_driver **driver_list;
1209 #ifdef HAVE_LIBUSB_1_0
1210 libusb_context *libusb_ctx;
1212 sr_resource_open_callback resource_open_cb;
1213 sr_resource_close_callback resource_close_cb;
1214 sr_resource_read_callback resource_read_cb;
1215 void *resource_cb_data;
1218 /** Input module metadata keys. */
1219 enum sr_input_meta_keys {
1220 /** The input filename, if there is one. */
1221 SR_INPUT_META_FILENAME = 0x01,
1222 /** The input file's size in bytes. */
1223 SR_INPUT_META_FILESIZE = 0x02,
1224 /** The first 128 bytes of the file, provided as a GString. */
1225 SR_INPUT_META_HEADER = 0x04,
1227 /** The module cannot identify a file without this metadata. */
1228 SR_INPUT_META_REQUIRED = 0x80,
1231 /** Input (file) module struct. */
1234 * A pointer to this input module's 'struct sr_input_module'.
1236 const struct sr_input_module *module;
1238 struct sr_dev_inst *sdi;
1243 /** Input (file) module driver. */
1244 struct sr_input_module {
1246 * A unique ID for this input module, suitable for use in command-line
1247 * clients, [a-z0-9-]. Must not be NULL.
1252 * A unique name for this input module, suitable for use in GUI
1253 * clients, can contain UTF-8. Must not be NULL.
1258 * A short description of the input module. Must not be NULL.
1260 * This can be displayed by frontends, e.g. when selecting the input
1261 * module for saving a file.
1266 * A NULL terminated array of strings containing a list of file name
1267 * extensions typical for the input file format, or NULL if there is
1268 * no typical extension for this file format.
1270 const char *const *exts;
1273 * Zero-terminated list of metadata items the module needs to be able
1274 * to identify an input stream. Can be all-zero, if the module cannot
1275 * identify streams at all, i.e. has to be forced into use.
1277 * Each item is one of:
1278 * SR_INPUT_META_FILENAME
1279 * SR_INPUT_META_FILESIZE
1280 * SR_INPUT_META_HEADER
1282 * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
1283 * identify a stream without the given metadata.
1285 const uint8_t metadata[8];
1288 * Returns a NULL-terminated list of options this module can take.
1289 * Can be NULL, if the module has no options.
1291 const struct sr_option *(*options) (void);
1294 * Check if this input module can load and parse the specified stream.
1296 * @param[in] metadata Metadata the module can use to identify the stream.
1297 * @param[out] confidence "Strength" of the detection.
1298 * Specialized handlers can take precedence over generic/basic support.
1300 * @retval SR_OK This module knows the format.
1301 * @retval SR_ERR_NA There wasn't enough data for this module to
1302 * positively identify the format.
1303 * @retval SR_ERR_DATA This module knows the format, but cannot handle
1304 * it. This means the stream is either corrupt, or indicates a
1305 * feature that the module does not support.
1306 * @retval SR_ERR This module does not know the format.
1308 * Lower numeric values of 'confidence' mean that the input module
1309 * stronger believes in its capability to handle this specific format.
1310 * This way, multiple input modules can claim support for a format,
1311 * and the application can pick the best match, or try fallbacks
1312 * in case of errors. This approach also copes with formats that
1313 * are unreliable to detect in the absence of magic signatures.
1315 int (*format_match) (GHashTable *metadata, unsigned int *confidence);
1318 * Initialize the input module.
1320 * @retval SR_OK Success
1321 * @retval other Negative error code.
1323 int (*init) (struct sr_input *in, GHashTable *options);
1326 * Send data to the specified input instance.
1328 * When an input module instance is created with sr_input_new(), this
1329 * function is used to feed data to the instance.
1331 * As enough data gets fed into this function to completely populate
1332 * the device instance associated with this input instance, this is
1333 * guaranteed to return the moment it's ready. This gives the caller
1334 * the chance to examine the device instance, attach session callbacks
1337 * @retval SR_OK Success
1338 * @retval other Negative error code.
1340 int (*receive) (struct sr_input *in, GString *buf);
1343 * Signal the input module no more data will come.
1345 * This will cause the module to process any data it may have buffered.
1346 * The SR_DF_END packet will also typically be sent at this time.
1348 int (*end) (struct sr_input *in);
1351 * Reset the input module's input handling structures.
1353 * Causes the input module to reset its internal state so that we can
1354 * re-send the input data from the beginning without having to
1355 * re-create the entire input module.
1357 * @retval SR_OK Success.
1358 * @retval other Negative error code.
1360 int (*reset) (struct sr_input *in);
1363 * This function is called after the caller is finished using
1364 * the input module, and can be used to free any internal
1365 * resources the module may keep.
1367 * This function is optional.
1369 * @retval SR_OK Success
1370 * @retval other Negative error code.
1372 void (*cleanup) (struct sr_input *in);
1375 /** Output module instance. */
1377 /** A pointer to this output's module. */
1378 const struct sr_output_module *module;
1381 * The device for which this output module is creating output. This
1382 * can be used by the module to find out channel names and numbers.
1384 const struct sr_dev_inst *sdi;
1387 * The name of the file that the data should be written to.
1389 const char *filename;
1392 * A generic pointer which can be used by the module to keep internal
1393 * state between calls into its callback functions.
1395 * For example, the module might store a pointer to a chunk of output
1396 * there, and only flush it when it reaches a certain size.
1401 /** Output module driver. */
1402 struct sr_output_module {
1404 * A unique ID for this output module, suitable for use in command-line
1405 * clients, [a-z0-9-]. Must not be NULL.
1410 * A unique name for this output module, suitable for use in GUI
1411 * clients, can contain UTF-8. Must not be NULL.
1416 * A short description of the output module. Must not be NULL.
1418 * This can be displayed by frontends, e.g. when selecting the output
1419 * module for saving a file.
1424 * A NULL terminated array of strings containing a list of file name
1425 * extensions typical for the input file format, or NULL if there is
1426 * no typical extension for this file format.
1428 const char *const *exts;
1431 * Bitfield containing flags that describe certain properties
1432 * this output module may or may not have.
1433 * @see sr_output_flags
1435 const uint64_t flags;
1438 * Returns a NULL-terminated list of options this module can take.
1439 * Can be NULL, if the module has no options.
1441 const struct sr_option *(*options) (void);
1444 * This function is called once, at the beginning of an output stream.
1446 * The device struct will be available in the output struct passed in,
1447 * as well as the param field -- which may be NULL or an empty string,
1448 * if no parameter was passed.
1450 * The module can use this to initialize itself, create a struct for
1451 * keeping state and storing it in the <code>internal</code> field.
1453 * @param o Pointer to the respective 'struct sr_output'.
1455 * @retval SR_OK Success
1456 * @retval other Negative error code.
1458 int (*init) (struct sr_output *o, GHashTable *options);
1461 * This function is passed a copy of every packet in the data feed.
1462 * Any output generated by the output module in response to the
1463 * packet should be returned in a newly allocated GString
1464 * <code>out</code>, which will be freed by the caller.
1466 * Packets not of interest to the output module can just be ignored,
1467 * and the <code>out</code> parameter set to NULL.
1469 * @param o Pointer to the respective 'struct sr_output'.
1470 * @param sdi The device instance that generated the packet.
1471 * @param packet The complete packet.
1472 * @param out A pointer where a GString * should be stored if
1473 * the module generates output, or NULL if not.
1475 * @retval SR_OK Success
1476 * @retval other Negative error code.
1478 int (*receive) (const struct sr_output *o,
1479 const struct sr_datafeed_packet *packet, GString **out);
1482 * This function is called after the caller is finished using
1483 * the output module, and can be used to free any internal
1484 * resources the module may keep.
1486 * @retval SR_OK Success
1487 * @retval other Negative error code.
1489 int (*cleanup) (struct sr_output *o);
1492 /** Transform module instance. */
1493 struct sr_transform {
1494 /** A pointer to this transform's module. */
1495 const struct sr_transform_module *module;
1498 * The device for which this transform module is used. This
1499 * can be used by the module to find out channel names and numbers.
1501 const struct sr_dev_inst *sdi;
1504 * A generic pointer which can be used by the module to keep internal
1505 * state between calls into its callback functions.
1510 struct sr_transform_module {
1512 * A unique ID for this transform module, suitable for use in
1513 * command-line clients, [a-z0-9-]. Must not be NULL.
1518 * A unique name for this transform module, suitable for use in GUI
1519 * clients, can contain UTF-8. Must not be NULL.
1524 * A short description of the transform module. Must not be NULL.
1526 * This can be displayed by frontends, e.g. when selecting
1527 * which transform module(s) to add.
1532 * Returns a NULL-terminated list of options this transform module
1533 * can take. Can be NULL, if the transform module has no options.
1535 const struct sr_option *(*options) (void);
1538 * This function is called once, at the beginning of a stream.
1540 * @param t Pointer to the respective 'struct sr_transform'.
1541 * @param options Hash table of options for this transform module.
1542 * Can be NULL if no options are to be used.
1544 * @retval SR_OK Success
1545 * @retval other Negative error code.
1547 int (*init) (struct sr_transform *t, GHashTable *options);
1550 * This function is passed a pointer to every packet in the data feed.
1552 * It can either return (in packet_out) a pointer to another packet
1553 * (possibly the exact same packet it got as input), or NULL.
1555 * @param t Pointer to the respective 'struct sr_transform'.
1556 * @param packet_in Pointer to a datafeed packet.
1557 * @param packet_out Pointer to the resulting datafeed packet after
1558 * this function was run. If NULL, the transform
1559 * module intentionally didn't output a new packet.
1561 * @retval SR_OK Success
1562 * @retval other Negative error code.
1564 int (*receive) (const struct sr_transform *t,
1565 struct sr_datafeed_packet *packet_in,
1566 struct sr_datafeed_packet **packet_out);
1569 * This function is called after the caller is finished using
1570 * the transform module, and can be used to free any internal
1571 * resources the module may keep.
1573 * @retval SR_OK Success
1574 * @retval other Negative error code.
1576 int (*cleanup) (struct sr_transform *t);
1579 #ifdef HAVE_LIBUSB_1_0
1580 /** USB device instance */
1581 struct sr_usb_dev_inst {
1584 /** Device address on USB bus */
1586 /** libusb device handle */
1587 struct libusb_device_handle *devhdl;
1591 struct sr_serial_dev_inst;
1592 #ifdef HAVE_SERIAL_COMM
1593 struct ser_lib_functions;
1594 struct ser_hid_chip_functions;
1596 typedef void (*serial_rx_chunk_callback)(struct sr_serial_dev_inst *serial,
1597 void *cb_data, const void *buf, size_t count);
1598 struct sr_serial_dev_inst {
1599 /** Port name, e.g. '/dev/tty42'. */
1601 /** Comm params for serial_set_paramstr(). */
1603 struct ser_lib_functions *lib_funcs;
1610 GString *rcv_buffer;
1611 serial_rx_chunk_callback rx_chunk_cb_func;
1612 void *rx_chunk_cb_data;
1613 #ifdef HAVE_LIBSERIALPORT
1614 /** libserialport port handle */
1615 struct sp_port *sp_data;
1617 #ifdef HAVE_LIBHIDAPI
1618 enum ser_hid_chip_t {
1619 SER_HID_CHIP_UNKNOWN, /**!< place holder */
1620 SER_HID_CHIP_BTC_BU86X, /**!< Brymen BU86x */
1621 SER_HID_CHIP_SIL_CP2110, /**!< SiLabs CP2110 */
1622 SER_HID_CHIP_VICTOR_DMM, /**!< Victor 70/86 DMM cable */
1623 SER_HID_CHIP_WCH_CH9325, /**!< WCH CH9325 */
1624 SER_HID_CHIP_LAST, /**!< sentinel */
1626 struct ser_hid_chip_functions *hid_chip_funcs;
1629 const char *hid_path;
1630 hid_device *hid_dev;
1631 GSList *hid_source_args;
1633 #ifdef HAVE_BLUETOOTH
1634 enum ser_bt_conn_t {
1635 SER_BT_CONN_UNKNOWN, /**!< place holder */
1636 SER_BT_CONN_RFCOMM, /**!< BT classic, RFCOMM channel */
1637 SER_BT_CONN_BLE122, /**!< BLE, BLE122 module, indications */
1638 SER_BT_CONN_NRF51, /**!< BLE, Nordic nRF51, notifications */
1639 SER_BT_CONN_CC254x, /**!< BLE, TI CC254x, notifications */
1640 SER_BT_CONN_AC6328, /**!< BLE, JL AC6328B, notifications */
1641 SER_BT_CONN_DIALOG, /**!< BLE, dialog DA14580, notifications */
1642 SER_BT_CONN_NOTIFY, /**!< BLE, generic notifications */
1643 SER_BT_CONN_MAX, /**!< sentinel */
1645 char *bt_addr_local;
1646 char *bt_addr_remote;
1647 size_t bt_rfcomm_channel;
1648 uint16_t bt_notify_handle_read;
1649 uint16_t bt_notify_handle_write;
1650 uint16_t bt_notify_handle_cccd;
1651 uint16_t bt_notify_value_cccd;
1652 uint16_t bt_ble_mtu;
1653 struct sr_bt_desc *bt_desc;
1654 GSList *bt_source_args;
1659 struct sr_usbtmc_dev_inst {
1664 /* Private driver context. */
1665 struct drv_context {
1666 /** sigrok context */
1667 struct sr_context *sr_ctx;
1671 /*--- log.c -----------------------------------------------------------------*/
1673 #if defined(_WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
1675 * On MinGW, we need to specify the gnu_printf format flavor or GCC
1676 * will assume non-standard Microsoft printf syntax.
1678 SR_PRIV int sr_log(int loglevel, const char *format, ...)
1679 __attribute__((__format__ (__gnu_printf__, 2, 3)));
1681 SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
1684 /* Message logging helpers with subsystem-specific prefix string. */
1685 #define sr_spew(...) sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
1686 #define sr_dbg(...) sr_log(SR_LOG_DBG, LOG_PREFIX ": " __VA_ARGS__)
1687 #define sr_info(...) sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
1688 #define sr_warn(...) sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
1689 #define sr_err(...) sr_log(SR_LOG_ERR, LOG_PREFIX ": " __VA_ARGS__)
1691 /*--- device.c --------------------------------------------------------------*/
1693 /** Scan options supported by a driver. */
1694 #define SR_CONF_SCAN_OPTIONS 0x7FFF0000
1696 /** Device options for a particular device. */
1697 #define SR_CONF_DEVICE_OPTIONS 0x7FFF0001
1699 /** Mask for separating config keys from capabilities. */
1700 #define SR_CONF_MASK 0x1fffffff
1702 /** Values for the changes argument of sr_dev_driver.config_channel_set. */
1704 /** The enabled state of the channel has been changed. */
1705 SR_CHANNEL_SET_ENABLED = 1 << 0,
1708 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
1709 int index, int type, gboolean enabled, const char *name);
1710 SR_PRIV void sr_channel_free(struct sr_channel *ch);
1711 SR_PRIV void sr_channel_free_cb(void *p);
1712 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
1713 struct sr_channel *cur_channel);
1714 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2);
1715 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2);
1717 SR_PRIV struct sr_channel_group *sr_channel_group_new(struct sr_dev_inst *sdi,
1718 const char *name, void *priv);
1719 SR_PRIV void sr_channel_group_free(struct sr_channel_group *cg);
1720 SR_PRIV void sr_channel_group_free_cb(void *cg);
1722 /** Device instance data */
1723 struct sr_dev_inst {
1724 /** Device driver. */
1725 struct sr_dev_driver *driver;
1726 /** Device instance status. SR_ST_NOT_FOUND, etc. */
1728 /** Device instance type. SR_INST_USB, etc. */
1730 /** Device vendor. */
1732 /** Device model. */
1734 /** Device version. */
1736 /** Serial number. */
1738 /** Connection string to uniquely identify devices. */
1739 char *connection_id;
1740 /** List of channels. */
1742 /** List of sr_channel_group structs */
1743 GSList *channel_groups;
1744 /** Device instance connection data (used?) */
1746 /** Device instance private data (used?) */
1748 /** Session to which this device is currently assigned. */
1749 struct sr_session *session;
1752 /* Generic device instances */
1753 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
1755 #ifdef HAVE_LIBUSB_1_0
1756 /* USB-specific instances */
1757 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
1758 uint8_t address, struct libusb_device_handle *hdl);
1759 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
1760 SR_PRIV void sr_usb_dev_inst_free_cb(gpointer p); /* Glib wrapper. */
1763 #ifdef HAVE_SERIAL_COMM
1764 #ifndef HAVE_LIBSERIALPORT
1766 * Some identifiers which initially got provided by libserialport are
1767 * used internally within the libsigrok serial layer's implementation,
1768 * while libserialport no longer is the exclusive provider of serial
1769 * communication support. Declare the identifiers here so they remain
1770 * available across all build configurations.
1777 SP_PARITY_SPACE = 4,
1780 enum libsp_flowcontrol {
1781 SP_FLOWCONTROL_NONE = 0,
1782 SP_FLOWCONTROL_XONXOFF = 1,
1783 SP_FLOWCONTROL_RTSCTS = 2,
1784 SP_FLOWCONTROL_DTRDSR = 3,
1788 /* Serial-specific instances */
1789 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
1790 const char *serialcomm);
1791 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
1794 /* USBTMC-specific instances */
1795 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
1796 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
1798 /*--- hwdriver.c ------------------------------------------------------------*/
1800 SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
1801 SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
1802 SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
1803 SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
1804 SR_PRIV void sr_config_free(struct sr_config *src);
1805 SR_PRIV int sr_dev_acquisition_start(struct sr_dev_inst *sdi);
1806 SR_PRIV int sr_dev_acquisition_stop(struct sr_dev_inst *sdi);
1808 /*--- session.c -------------------------------------------------------------*/
1811 /** Context this session exists in. */
1812 struct sr_context *ctx;
1813 /** List of struct sr_dev_inst pointers. */
1815 /** List of struct sr_dev_inst pointers owned by this session. */
1817 /** List of struct datafeed_callback pointers. */
1818 GSList *datafeed_callbacks;
1820 struct sr_trigger *trigger;
1822 /** Callback to invoke on session stop. */
1823 sr_session_stopped_callback stopped_callback;
1824 /** User data to be passed to the session stop callback. */
1825 void *stopped_cb_data;
1827 /** Mutex protecting the main context pointer. */
1829 /** Context of the session main loop. */
1830 GMainContext *main_context;
1832 /** Registered event sources for this session. */
1833 GHashTable *event_sources;
1834 /** Session main loop. */
1835 GMainLoop *main_loop;
1836 /** ID of idle source for dispatching the session stop notification. */
1837 unsigned int stop_check_id;
1838 /** Whether the session has been started. */
1842 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
1843 void *key, GSource *source);
1844 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
1846 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1847 void *key, GSource *source);
1848 SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1849 void *key, gintptr fd, int events, int timeout,
1850 sr_receive_data_callback cb, void *cb_data);
1852 SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
1853 int events, int timeout, sr_receive_data_callback cb, void *cb_data);
1854 SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
1855 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1857 SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
1858 GIOChannel *channel, int events, int timeout,
1859 sr_receive_data_callback cb, void *cb_data);
1860 SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
1861 SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
1863 SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
1864 GIOChannel *channel);
1866 SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi,
1867 uint32_t key, GVariant *var);
1868 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
1869 const struct sr_datafeed_packet *packet);
1870 SR_PRIV int sr_sessionfile_check(const char *filename);
1871 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename,
1872 struct sr_session **session);
1874 /*--- session_file.c --------------------------------------------------------*/
1876 #if !HAVE_ZIP_DISCARD
1877 /* Replace zip_discard() if not available. */
1878 #define zip_discard(zip) sr_zip_discard(zip)
1879 SR_PRIV void sr_zip_discard(struct zip *archive);
1882 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
1883 const struct zip_stat *entry);
1885 /*--- analog.c --------------------------------------------------------------*/
1887 SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
1888 struct sr_analog_encoding *encoding,
1889 struct sr_analog_meaning *meaning,
1890 struct sr_analog_spec *spec,
1893 /*--- std.c -----------------------------------------------------------------*/
1895 typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
1896 typedef void (*std_dev_clear_callback)(void *priv);
1898 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx);
1899 SR_PRIV int std_cleanup(const struct sr_dev_driver *di);
1900 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
1901 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
1902 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
1903 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
1904 #ifdef HAVE_SERIAL_COMM
1905 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
1906 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
1908 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi);
1909 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi);
1910 SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi);
1911 SR_PRIV int std_session_send_df_frame_begin(const struct sr_dev_inst *sdi);
1912 SR_PRIV int std_session_send_df_frame_end(const struct sr_dev_inst *sdi);
1913 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
1914 std_dev_clear_callback clear_private);
1915 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver);
1916 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di);
1917 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
1918 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices);
1920 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
1921 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
1922 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
1923 size_t drvsize, const uint32_t devopts[], size_t devsize);
1925 extern SR_PRIV const uint32_t NO_OPTS[1];
1927 #define STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts) \
1928 std_opts_config_list(key, data, sdi, cg, ARRAY_AND_SIZE(scanopts), \
1929 ARRAY_AND_SIZE(drvopts), ARRAY_AND_SIZE(devopts))
1931 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n);
1932 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n);
1933 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n);
1934 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n);
1935 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step);
1936 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]);
1937 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double dmin, const double dmax, const double dstep);
1939 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high);
1940 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high);
1942 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n);
1943 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n);
1944 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n);
1945 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n);
1947 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n);
1949 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n);
1950 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n);
1951 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n);
1953 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n);
1954 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n);
1956 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n);
1957 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
1958 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
1960 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
1962 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
1963 int baudrate, int bits, int parity, int stopbits,
1964 int flowcontrol, int rts, int dtr);
1965 SR_PRIV int std_dummy_set_handshake(struct sr_serial_dev_inst *serial,
1968 /*--- resource.c ------------------------------------------------------------*/
1970 SR_PRIV int64_t sr_file_get_size(FILE *file);
1972 SR_PRIV int sr_resource_open(struct sr_context *ctx,
1973 struct sr_resource *res, int type, const char *name)
1974 G_GNUC_WARN_UNUSED_RESULT;
1975 SR_PRIV int sr_resource_close(struct sr_context *ctx,
1976 struct sr_resource *res);
1977 SR_PRIV gssize sr_resource_read(struct sr_context *ctx,
1978 const struct sr_resource *res, void *buf, size_t count)
1979 G_GNUC_WARN_UNUSED_RESULT;
1980 SR_PRIV void *sr_resource_load(struct sr_context *ctx, int type,
1981 const char *name, size_t *size, size_t max_size)
1982 G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
1984 /*--- strutil.c -------------------------------------------------------------*/
1986 SR_PRIV int sr_atol(const char *str, long *ret);
1987 SR_PRIV int sr_atol_base(const char *str, long *ret, char **end, int base);
1988 SR_PRIV int sr_atoul_base(const char *str, unsigned long *ret, char **end, int base);
1989 SR_PRIV int sr_atoi(const char *str, int *ret);
1990 SR_PRIV int sr_atod(const char *str, double *ret);
1991 SR_PRIV int sr_atof(const char *str, float *ret);
1992 SR_PRIV int sr_atod_ascii(const char *str, double *ret);
1993 SR_PRIV int sr_atod_ascii_digits(const char *str, double *ret, int *digits);
1994 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
1996 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
1997 SR_PRIV void sr_hexdump_free(GString *s);
1999 /*--- soft-trigger.c --------------------------------------------------------*/
2001 struct soft_trigger_logic {
2002 const struct sr_dev_inst *sdi;
2003 const struct sr_trigger *trigger;
2007 uint8_t *prev_sample;
2008 uint8_t *pre_trigger_buffer;
2009 uint8_t *pre_trigger_head;
2010 int pre_trigger_size;
2011 int pre_trigger_fill;
2014 SR_PRIV int logic_channel_unitsize(GSList *channels);
2015 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
2016 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
2017 int pre_trigger_samples);
2018 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
2019 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
2020 int len, int *pre_trigger_samples);
2022 /*--- serial.c --------------------------------------------------------------*/
2024 #ifdef HAVE_SERIAL_COMM
2030 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
2031 typedef int (*packet_valid_len_callback)(void *st,
2032 const uint8_t *p, size_t l, size_t *pl);
2034 typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
2036 typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
2038 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
2039 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
2040 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
2041 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
2042 SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
2043 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
2044 const void *buf, size_t count, unsigned int timeout_ms);
2045 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
2046 const void *buf, size_t count);
2047 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
2048 size_t count, unsigned int timeout_ms);
2049 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
2051 SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
2052 serial_rx_chunk_callback cb, void *cb_data);
2053 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
2054 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
2055 SR_PRIV int serial_set_handshake(struct sr_serial_dev_inst *serial,
2057 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
2058 const char *paramstr);
2059 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
2060 int *buflen, gint64 timeout_ms);
2061 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
2062 uint8_t *buf, size_t *buflen,
2063 size_t packet_size, packet_valid_callback is_valid,
2064 packet_valid_len_callback is_valid_len, size_t *return_size,
2065 uint64_t timeout_ms);
2066 SR_PRIV int serial_source_add(struct sr_session *session,
2067 struct sr_serial_dev_inst *serial, int events, int timeout,
2068 sr_receive_data_callback cb, void *cb_data);
2069 SR_PRIV int serial_source_remove(struct sr_session *session,
2070 struct sr_serial_dev_inst *serial);
2071 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
2072 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
2074 SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial);
2075 SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial);
2076 SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
2077 const uint8_t *data, size_t len);
2078 SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
2079 uint8_t *data, size_t len);
2081 struct ser_lib_functions {
2082 int (*open)(struct sr_serial_dev_inst *serial, int flags);
2083 int (*close)(struct sr_serial_dev_inst *serial);
2084 int (*flush)(struct sr_serial_dev_inst *serial);
2085 int (*drain)(struct sr_serial_dev_inst *serial);
2086 int (*write)(struct sr_serial_dev_inst *serial,
2087 const void *buf, size_t count,
2088 int nonblocking, unsigned int timeout_ms);
2089 int (*read)(struct sr_serial_dev_inst *serial,
2090 void *buf, size_t count,
2091 int nonblocking, unsigned int timeout_ms);
2092 int (*set_params)(struct sr_serial_dev_inst *serial,
2093 int baudrate, int bits, int parity, int stopbits,
2094 int flowcontrol, int rts, int dtr);
2095 int (*set_handshake)(struct sr_serial_dev_inst *serial,
2097 int (*setup_source_add)(struct sr_session *session,
2098 struct sr_serial_dev_inst *serial,
2099 int events, int timeout,
2100 sr_receive_data_callback cb, void *cb_data);
2101 int (*setup_source_remove)(struct sr_session *session,
2102 struct sr_serial_dev_inst *serial);
2103 GSList *(*list)(GSList *list, sr_ser_list_append_t append);
2104 GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
2105 uint16_t vendor_id, uint16_t product_id);
2106 int (*get_frame_format)(struct sr_serial_dev_inst *serial,
2107 int *baud, int *bits);
2108 size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
2110 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
2111 SR_PRIV int ser_name_is_hid(struct sr_serial_dev_inst *serial);
2112 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_hid;
2113 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial);
2114 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt;
2116 #ifdef HAVE_LIBHIDAPI
2117 struct vid_pid_item {
2121 struct ser_hid_chip_functions {
2122 const char *chipname;
2123 const char *chipdesc;
2124 const struct vid_pid_item *vid_pid_items;
2125 const int max_bytes_per_request;
2126 int (*set_params)(struct sr_serial_dev_inst *serial,
2127 int baudrate, int bits, int parity, int stopbits,
2128 int flowcontrol, int rts, int dtr);
2129 int (*read_bytes)(struct sr_serial_dev_inst *serial,
2130 uint8_t *data, int space, unsigned int timeout);
2131 int (*write_bytes)(struct sr_serial_dev_inst *serial,
2132 const uint8_t *data, int space);
2133 int (*flush)(struct sr_serial_dev_inst *serial);
2134 int (*drain)(struct sr_serial_dev_inst *serial);
2136 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x;
2137 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325;
2138 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110;
2139 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor;
2140 SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid);
2144 SR_PRIV int sr_serial_extract_options(GSList *options,
2145 const char **serial_device, const char **serial_options);
2147 /*--- bt/ API ---------------------------------------------------------------*/
2149 #ifdef HAVE_BLUETOOTH
2150 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx);
2153 typedef void (*sr_bt_scan_cb)(void *cb_data, const char *addr, const char *name);
2154 typedef int (*sr_bt_data_cb)(void *cb_data, uint8_t *data, size_t dlen);
2156 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void);
2157 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc);
2159 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
2160 sr_bt_scan_cb cb, void *cb_data);
2161 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
2162 sr_bt_data_cb cb, void *cb_data);
2163 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr);
2164 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr);
2165 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel);
2166 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
2167 uint16_t read_handle, uint16_t write_handle,
2168 uint16_t cccd_handle, uint16_t cccd_value,
2171 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration);
2172 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration);
2174 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc);
2175 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc);
2176 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc);
2178 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc,
2179 void *data, size_t len);
2180 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
2181 const void *data, size_t len);
2183 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc);
2184 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc);
2187 /*--- ezusb.c ---------------------------------------------------------------*/
2189 #ifdef HAVE_LIBUSB_1_0
2190 SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
2191 SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
2193 SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
2194 int configuration, const char *name);
2197 /*--- usb.c -----------------------------------------------------------------*/
2199 SR_PRIV int sr_usb_split_conn(const char *conn,
2200 uint16_t *vid, uint16_t *pid, uint8_t *bus, uint8_t *addr);
2201 #ifdef HAVE_LIBUSB_1_0
2202 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
2203 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
2204 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
2205 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
2206 int timeout, sr_receive_data_callback cb, void *cb_data);
2207 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
2208 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
2209 SR_PRIV gboolean usb_match_manuf_prod(libusb_device *dev,
2210 const char *manufacturer, const char *product);
2213 /*--- binary_helpers.c ------------------------------------------------------*/
2215 /** Binary value type */
2216 enum binary_value_type {
2230 /** Binary value specification */
2231 struct binary_value_spec {
2232 size_t offset; /**!< Offset into binary image */
2233 enum binary_value_type type; /**!< Data type to decode */
2237 * Read extract a value from a binary data image, ensuring no out-of-bounds
2240 * @param[out] out Pointer to output buffer (conversion result)
2241 * @param[in] spec Binary value specification
2242 * @param[in] data Pointer to binary input data
2243 * @param[in] length Size of binary input data
2245 * @return SR_OK on success, SR_ERR_* error code on failure.
2247 SR_PRIV int bv_get_value_len(float *out, const struct binary_value_spec *spec,
2248 const uint8_t *data, size_t length);
2251 * Read extract a value from a binary data image, without bound check.
2253 * @param[out] out Pointer to output buffer (conversion result)
2254 * @param[in] spec Binary value specification
2255 * @param[in] data Pointer to binary input data
2257 * @return SR_OK on success, SR_ERR_* error code on failure.
2259 SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec,
2260 const uint8_t *data);
2262 /*--- crc.c -----------------------------------------------------------------*/
2264 #define SR_CRC16_DEFAULT_INIT 0xffffU
2267 * Calculate a CRC16 checksum using the 0x8005 polynomial.
2269 * This CRC16 flavor is also known as CRC16-ANSI or CRC16-MODBUS.
2271 * @param crc Initial value (typically 0xffff)
2272 * @param buffer Input buffer
2273 * @param len Buffer length
2276 SR_PRIV uint16_t sr_crc16(uint16_t crc, const uint8_t *buffer, int len);
2278 /*--- modbus/modbus.c -------------------------------------------------------*/
2280 struct sr_modbus_dev_inst {
2284 GSList *(*scan)(int modbusaddr);
2285 int (*dev_inst_new)(void *priv, const char *resource,
2286 char **params, const char *serialcomm, int modbusaddr);
2287 int (*open)(void *priv);
2288 int (*source_add)(struct sr_session *session, void *priv, int events,
2289 int timeout, sr_receive_data_callback cb, void *cb_data);
2290 int (*source_remove)(struct sr_session *session, void *priv);
2291 int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
2292 int (*read_begin)(void *priv, uint8_t *function_code);
2293 int (*read_data)(void *priv, uint8_t *buf, int maxlen);
2294 int (*read_end)(void *priv);
2295 int (*close)(void *priv);
2296 void (*free)(void *priv);
2297 unsigned int read_timeout_ms;
2301 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
2302 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
2303 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
2304 const char *serialcomm, int modbusaddr);
2305 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
2306 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
2307 struct sr_modbus_dev_inst *modbus, int events, int timeout,
2308 sr_receive_data_callback cb, void *cb_data);
2309 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
2310 struct sr_modbus_dev_inst *modbus);
2311 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
2312 uint8_t *request, int request_size);
2313 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
2314 uint8_t *reply, int reply_size);
2315 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
2316 uint8_t *request, int request_size,
2317 uint8_t *reply, int reply_size);
2318 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
2319 int address, int nb_coils, uint8_t *coils);
2320 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
2321 int address, int nb_registers,
2322 uint16_t *registers);
2323 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
2324 int address, int value);
2325 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
2326 int address, int nb_registers,
2327 uint16_t *registers);
2328 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
2329 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
2331 /*--- dmm/es519xx.c ---------------------------------------------------------*/
2334 * All 11-byte es519xx chips repeat each block twice for each conversion cycle
2335 * so always read 2 blocks at a time.
2337 #define ES519XX_11B_PACKET_SIZE (11 * 2)
2338 #define ES519XX_14B_PACKET_SIZE 14
2340 struct es519xx_info {
2341 gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
2342 gboolean is_milli, is_resistance, is_continuity, is_diode;
2343 gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
2344 gboolean is_temperature, is_celsius, is_fahrenheit;
2345 gboolean is_adp0, is_adp1, is_adp2, is_adp3;
2346 gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
2347 gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
2348 gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
2351 gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
2355 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
2356 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
2357 struct sr_datafeed_analog *analog, void *info);
2358 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
2359 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
2360 float *floatval, struct sr_datafeed_analog *analog, void *info);
2361 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
2362 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
2363 float *floatval, struct sr_datafeed_analog *analog, void *info);
2364 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
2365 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
2366 float *floatval, struct sr_datafeed_analog *analog, void *info);
2367 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
2368 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
2369 struct sr_datafeed_analog *analog, void *info);
2370 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
2371 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
2372 struct sr_datafeed_analog *analog, void *info);
2373 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
2374 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
2375 float *floatval, struct sr_datafeed_analog *analog, void *info);
2377 /*--- dmm/fs9922.c ----------------------------------------------------------*/
2379 #define FS9922_PACKET_SIZE 14
2381 struct fs9922_info {
2382 gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
2383 gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
2384 gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
2385 gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
2386 gboolean is_celsius, is_fahrenheit;
2387 int bargraph_sign, bargraph_value;
2390 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
2391 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
2392 struct sr_datafeed_analog *analog, void *info);
2393 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
2395 /*--- dmm/fs9721.c ----------------------------------------------------------*/
2397 #define FS9721_PACKET_SIZE 14
2399 struct fs9721_info {
2400 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2401 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2402 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2403 gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
2406 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
2407 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
2408 struct sr_datafeed_analog *analog, void *info);
2409 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
2410 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
2411 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
2412 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
2413 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
2415 /*--- dmm/mm38xr.c ---------------------------------------------------------*/
2417 #define METERMAN_38XR_PACKET_SIZE 15
2419 struct meterman_38xr_info { int dummy; };
2421 SR_PRIV gboolean meterman_38xr_packet_valid(const uint8_t *buf);
2422 SR_PRIV int meterman_38xr_parse(const uint8_t *buf, float *floatval,
2423 struct sr_datafeed_analog *analog, void *info);
2425 /*--- dmm/ms2115b.c ---------------------------------------------------------*/
2427 #define MS2115B_PACKET_SIZE 9
2429 enum ms2115b_display {
2430 MS2115B_DISPLAY_MAIN,
2431 MS2115B_DISPLAY_SUB,
2432 MS2115B_DISPLAY_COUNT,
2435 struct ms2115b_info {
2436 /* Selected channel. */
2438 gboolean is_ac, is_dc, is_auto;
2439 gboolean is_diode, is_beep, is_farad;
2440 gboolean is_ohm, is_ampere, is_volt, is_hz;
2441 gboolean is_duty_cycle, is_percent;
2444 extern SR_PRIV const char *ms2115b_channel_formats[];
2445 SR_PRIV gboolean sr_ms2115b_packet_valid(const uint8_t *buf);
2446 SR_PRIV int sr_ms2115b_parse(const uint8_t *buf, float *floatval,
2447 struct sr_datafeed_analog *analog, void *info);
2449 /*--- dmm/ms8250d.c ---------------------------------------------------------*/
2451 #define MS8250D_PACKET_SIZE 18
2453 struct ms8250d_info {
2454 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2455 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2456 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2457 gboolean is_ncv, is_min, is_max, is_sign, is_autotimer;
2460 SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf);
2461 SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
2462 struct sr_datafeed_analog *analog, void *info);
2464 /*--- dmm/dtm0660.c ---------------------------------------------------------*/
2466 #define DTM0660_PACKET_SIZE 15
2468 struct dtm0660_info {
2469 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2470 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2471 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2472 gboolean is_degf, is_degc, is_c2c1_01, is_c2c1_00, is_apo, is_min;
2473 gboolean is_minmax, is_max, is_sign;
2476 SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf);
2477 SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
2478 struct sr_datafeed_analog *analog, void *info);
2480 /*--- dmm/m2110.c -----------------------------------------------------------*/
2482 #define BBCGM_M2110_PACKET_SIZE 9
2484 /* Dummy info struct. The parser does not use it. */
2485 struct m2110_info { int dummy; };
2487 SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
2488 SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
2489 struct sr_datafeed_analog *analog, void *info);
2491 /*--- dmm/metex14.c ---------------------------------------------------------*/
2493 #define METEX14_PACKET_SIZE 14
2495 struct metex14_info {
2497 gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
2498 gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
2499 gboolean is_hertz, is_ohm, is_celsius, is_fahrenheit, is_watt;
2500 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2501 gboolean is_gain, is_decibel, is_power, is_decibel_mw, is_power_factor;
2502 gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
2505 #ifdef HAVE_SERIAL_COMM
2506 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
2508 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
2509 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
2510 struct sr_datafeed_analog *analog, void *info);
2511 SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf);
2512 SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
2513 struct sr_datafeed_analog *analog, void *info);
2515 /*--- dmm/rs9lcd.c ----------------------------------------------------------*/
2517 #define RS9LCD_PACKET_SIZE 9
2519 /* Dummy info struct. The parser does not use it. */
2520 struct rs9lcd_info { int dummy; };
2522 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
2523 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
2524 struct sr_datafeed_analog *analog, void *info);
2526 /*--- dmm/bm25x.c -----------------------------------------------------------*/
2528 #define BRYMEN_BM25X_PACKET_SIZE 15
2530 /* Dummy info struct. The parser does not use it. */
2531 struct bm25x_info { int dummy; };
2533 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
2534 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
2535 struct sr_datafeed_analog *analog, void *info);
2537 /*--- dmm/bm52x.c -----------------------------------------------------------*/
2539 #define BRYMEN_BM52X_PACKET_SIZE 24
2540 #define BRYMEN_BM52X_DISPLAY_COUNT 2
2542 struct brymen_bm52x_info { size_t ch_idx; };
2544 #ifdef HAVE_SERIAL_COMM
2545 SR_PRIV int sr_brymen_bm52x_packet_request(struct sr_serial_dev_inst *serial);
2546 SR_PRIV int sr_brymen_bm82x_packet_request(struct sr_serial_dev_inst *serial);
2548 SR_PRIV gboolean sr_brymen_bm52x_packet_valid(const uint8_t *buf);
2549 SR_PRIV gboolean sr_brymen_bm82x_packet_valid(const uint8_t *buf);
2550 /* BM520s and BM820s protocols are similar, the parse routine is shared. */
2551 SR_PRIV int sr_brymen_bm52x_parse(const uint8_t *buf, float *floatval,
2552 struct sr_datafeed_analog *analog, void *info);
2554 struct brymen_bm52x_state;
2556 SR_PRIV void *brymen_bm52x_state_init(void);
2557 SR_PRIV void brymen_bm52x_state_free(void *state);
2558 SR_PRIV int brymen_bm52x_config_get(void *state, uint32_t key, GVariant **data,
2559 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2560 SR_PRIV int brymen_bm52x_config_set(void *state, uint32_t key, GVariant *data,
2561 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2562 SR_PRIV int brymen_bm52x_config_list(void *state, uint32_t key, GVariant **data,
2563 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2564 SR_PRIV int brymen_bm52x_acquire_start(void *state,
2565 const struct sr_dev_inst *sdi,
2566 sr_receive_data_callback *cb, void **cb_data);
2568 /*--- dmm/bm85x.c -----------------------------------------------------------*/
2570 #define BRYMEN_BM85x_PACKET_SIZE_MIN 4
2572 struct brymen_bm85x_info { int dummy; };
2574 #ifdef HAVE_SERIAL_COMM
2575 SR_PRIV int brymen_bm85x_after_open(struct sr_serial_dev_inst *serial);
2576 SR_PRIV int brymen_bm85x_packet_request(struct sr_serial_dev_inst *serial);
2578 SR_PRIV gboolean brymen_bm85x_packet_valid(void *state,
2579 const uint8_t *buf, size_t len, size_t *pkt_len);
2580 SR_PRIV int brymen_bm85x_parse(void *state, const uint8_t *buf, size_t len,
2581 double *floatval, struct sr_datafeed_analog *analog, void *info);
2583 /*--- dmm/bm86x.c -----------------------------------------------------------*/
2585 #define BRYMEN_BM86X_PACKET_SIZE 24
2586 #define BRYMEN_BM86X_DISPLAY_COUNT 2
2588 struct brymen_bm86x_info { size_t ch_idx; };
2590 #ifdef HAVE_SERIAL_COMM
2591 SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial);
2593 SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf);
2594 SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *floatval,
2595 struct sr_datafeed_analog *analog, void *info);
2597 /*--- dmm/ut71x.c -----------------------------------------------------------*/
2599 #define UT71X_PACKET_SIZE 11
2602 gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
2603 gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
2604 gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
2605 gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
2608 SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
2609 SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
2610 struct sr_datafeed_analog *analog, void *info);
2612 /*--- dmm/vc870.c -----------------------------------------------------------*/
2614 #define VC870_PACKET_SIZE 23
2617 gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
2618 gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
2619 gboolean is_current, is_micro, is_milli, is_power;
2620 gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_rms_value;
2621 gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
2622 gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
2623 gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
2624 gboolean is_lo, is_hi, is_open2;
2626 gboolean is_frequency, is_dual_display, is_auto;
2629 SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
2630 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
2631 struct sr_datafeed_analog *analog, void *info);
2633 /*--- dmm/vc96.c ------------------------------------------------------------*/
2635 #define VC96_PACKET_SIZE 13
2639 gboolean is_ac, is_dc, is_resistance, is_diode, is_ampere, is_volt;
2640 gboolean is_ohm, is_micro, is_milli, is_kilo, is_mega, is_hfe;
2641 gboolean is_unitless;
2644 SR_PRIV gboolean sr_vc96_packet_valid(const uint8_t *buf);
2645 SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval,
2646 struct sr_datafeed_analog *analog, void *info);
2648 /*--- lcr/es51919.c ---------------------------------------------------------*/
2650 /* Acquisition details which apply to all supported serial-lcr devices. */
2651 struct lcr_parse_info {
2653 uint64_t output_freq;
2654 const char *circuit_model;
2657 #define ES51919_PACKET_SIZE 17
2658 #define ES51919_CHANNEL_COUNT 2
2659 #define ES51919_COMM_PARAM "9600/8n1/rts=1/dtr=1"
2661 SR_PRIV int es51919_config_get(uint32_t key, GVariant **data,
2662 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2663 SR_PRIV int es51919_config_set(uint32_t key, GVariant *data,
2664 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2665 SR_PRIV int es51919_config_list(uint32_t key, GVariant **data,
2666 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2667 SR_PRIV gboolean es51919_packet_valid(const uint8_t *pkt);
2668 SR_PRIV int es51919_packet_parse(const uint8_t *pkt, float *floatval,
2669 struct sr_datafeed_analog *analog, void *info);
2671 /*--- lcr/vc4080.c ----------------------------------------------------------*/
2673 /* Note: Also uses 'struct lcr_parse_info' from es51919 above. */
2675 #define VC4080_PACKET_SIZE 39
2676 #define VC4080_COMM_PARAM "1200/8n1"
2677 #define VC4080_WITH_DQ_CHANS 0 /* Enable separate D/Q channels? */
2679 enum vc4080_display {
2680 VC4080_DISPLAY_PRIMARY,
2681 VC4080_DISPLAY_SECONDARY,
2682 #if VC4080_WITH_DQ_CHANS
2683 VC4080_DISPLAY_D_VALUE,
2684 VC4080_DISPLAY_Q_VALUE,
2686 VC4080_CHANNEL_COUNT,
2689 extern SR_PRIV const char *vc4080_channel_formats[VC4080_CHANNEL_COUNT];
2691 SR_PRIV int vc4080_config_list(uint32_t key, GVariant **data,
2692 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2693 SR_PRIV int vc4080_packet_request(struct sr_serial_dev_inst *serial);
2694 SR_PRIV gboolean vc4080_packet_valid(const uint8_t *pkt);
2695 SR_PRIV int vc4080_packet_parse(const uint8_t *pkt, float *floatval,
2696 struct sr_datafeed_analog *analog, void *info);
2698 /*--- dmm/ut372.c -----------------------------------------------------------*/
2700 #define UT372_PACKET_SIZE 27
2706 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
2707 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
2708 struct sr_datafeed_analog *analog, void *info);
2710 /*--- dmm/asycii.c ----------------------------------------------------------*/
2712 #define ASYCII_PACKET_SIZE 16
2714 struct asycii_info {
2715 gboolean is_ac, is_dc, is_ac_and_dc;
2716 gboolean is_resistance, is_capacitance, is_diode, is_gain;
2717 gboolean is_frequency, is_duty_cycle, is_duty_pos, is_duty_neg;
2718 gboolean is_pulse_width, is_period_pos, is_period_neg;
2719 gboolean is_pulse_count, is_count_pos, is_count_neg;
2720 gboolean is_ampere, is_volt, is_volt_ampere, is_farad, is_ohm;
2721 gboolean is_hertz, is_percent, is_seconds, is_decibel;
2722 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2723 gboolean is_unitless;
2724 gboolean is_peak_min, is_peak_max;
2725 gboolean is_invalid;
2728 #ifdef HAVE_SERIAL_COMM
2729 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
2731 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);
2732 SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
2733 struct sr_datafeed_analog *analog, void *info);
2735 /*--- dmm/eev121gw.c --------------------------------------------------------*/
2737 #define EEV121GW_PACKET_SIZE 19
2739 enum eev121gw_display {
2740 EEV121GW_DISPLAY_MAIN,
2741 EEV121GW_DISPLAY_SUB,
2742 EEV121GW_DISPLAY_BAR,
2743 EEV121GW_DISPLAY_COUNT,
2746 struct eev121gw_info {
2747 /* Selected channel. */
2750 * Measured value, number and sign/overflow flags, scale factor
2751 * and significant digits.
2753 uint32_t uint_value;
2754 gboolean is_ofl, is_neg;
2756 /* Currently active mode (meter's function). */
2757 gboolean is_ac, is_dc, is_voltage, is_current, is_power, is_gain;
2758 gboolean is_resistance, is_capacitance, is_diode, is_temperature;
2759 gboolean is_continuity, is_frequency, is_period, is_duty_cycle;
2760 /* Quantities associated with mode/function. */
2761 gboolean is_ampere, is_volt, is_volt_ampere, is_dbm;
2762 gboolean is_ohm, is_farad, is_celsius, is_fahrenheit;
2763 gboolean is_hertz, is_seconds, is_percent, is_loop_current;
2764 gboolean is_unitless, is_logic;
2765 /* Other indicators. */
2766 gboolean is_min, is_max, is_avg, is_1ms_peak, is_rel, is_hold;
2767 gboolean is_low_pass, is_mem, is_bt, is_auto_range, is_test;
2768 gboolean is_auto_poweroff, is_low_batt;
2771 extern SR_PRIV const char *eev121gw_channel_formats[];
2772 SR_PRIV gboolean sr_eev121gw_packet_valid(const uint8_t *buf);
2773 SR_PRIV int sr_eev121gw_3displays_parse(const uint8_t *buf, float *floatval,
2774 struct sr_datafeed_analog *analog, void *info);
2776 /*--- scale/kern.c ----------------------------------------------------------*/
2779 gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
2780 gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
2781 gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
2785 SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
2786 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
2787 struct sr_datafeed_analog *analog, void *info);
2789 /*--- sw_limits.c -----------------------------------------------------------*/
2791 struct sr_sw_limits {
2792 uint64_t limit_samples;
2793 uint64_t limit_frames;
2794 uint64_t limit_msec;
2795 uint64_t samples_read;
2796 uint64_t frames_read;
2797 uint64_t start_time;
2800 SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits, uint32_t key,
2802 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
2804 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
2805 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
2806 SR_PRIV int sr_sw_limits_get_remain(const struct sr_sw_limits *limits,
2807 uint64_t *samples, uint64_t *frames, uint64_t *msecs,
2808 gboolean *exceeded);
2809 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
2810 uint64_t samples_read);
2811 SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
2812 uint64_t frames_read);
2813 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
2815 /*--- feed_queue.h ----------------------------------------------------------*/
2817 struct feed_queue_logic;
2818 struct feed_queue_analog;
2820 SR_API struct feed_queue_logic *feed_queue_logic_alloc(
2821 const struct sr_dev_inst *sdi,
2822 size_t sample_count, size_t unit_size);
2823 SR_API int feed_queue_logic_submit(struct feed_queue_logic *q,
2824 const uint8_t *data, size_t count);
2825 SR_API int feed_queue_logic_flush(struct feed_queue_logic *q);
2826 SR_API int feed_queue_logic_send_trigger(struct feed_queue_logic *q);
2827 SR_API void feed_queue_logic_free(struct feed_queue_logic *q);
2829 SR_API struct feed_queue_analog *feed_queue_analog_alloc(
2830 const struct sr_dev_inst *sdi,
2831 size_t sample_count, int digits, struct sr_channel *ch);
2832 SR_API int feed_queue_analog_mq_unit(struct feed_queue_analog *q,
2833 enum sr_mq mq, enum sr_mqflag mq_flag, enum sr_unit unit);
2834 SR_API int feed_queue_analog_scale_offset(struct feed_queue_analog *q,
2835 const struct sr_rational *scale, const struct sr_rational *offset);
2836 SR_API int feed_queue_analog_submit(struct feed_queue_analog *q,
2837 float data, size_t count);
2838 SR_API int feed_queue_analog_flush(struct feed_queue_analog *q);
2839 SR_API void feed_queue_analog_free(struct feed_queue_analog *q);