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 32 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_u32be(const uint8_t *p)
192 #define RB32(x) read_u32be((const uint8_t *)(x))
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
199 static inline uint32_t read_u32le(const uint8_t *p)
211 #define RL32(x) read_u32le((const uint8_t *)(x))
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
218 static inline int32_t read_i32be(const uint8_t *p)
228 #define RB32S(x) read_i32be((const uint8_t *)(x))
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
235 static inline int32_t read_i32le(const uint8_t *p)
245 #define RL32S(x) read_i32le((const uint8_t *)(x))
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
252 static inline uint64_t read_u64be(const uint8_t *p)
268 #define RB64(x) read_u64be((const uint8_t *)(x))
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
275 static inline uint64_t read_u64le(const uint8_t *p)
291 #define RL64(x) read_u64le((const uint8_t *)(x))
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
298 static inline int64_t read_i64be(const uint8_t *p)
308 #define RB64S(x) read_i64be((const uint8_t *)(x))
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
315 static inline int64_t read_i64le(const uint8_t *p)
325 #define RL64S(x) read_i64le((const uint8_t *)(x))
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
332 static inline float read_fltbe(const uint8_t *p)
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.
340 union { uint32_t u32; float flt; } u;
343 u.u32 = read_u32be(p);
348 #define RBFL(x) read_fltbe((const uint8_t *)(x))
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
355 static inline float read_fltle(const uint8_t *p)
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.
363 union { uint32_t u32; float flt; } u;
366 u.u32 = read_u32le(p);
371 #define RLFL(x) read_fltle((const uint8_t *)(x))
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
378 static inline double read_dblbe(const uint8_t *p)
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.
386 union { uint64_t u64; double flt; } u;
389 u.u64 = read_u64be(p);
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
400 static inline double read_dblle(const uint8_t *p)
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.
408 union { uint64_t u64; double flt; } u;
411 u.u64 = read_u64le(p);
416 #define RLDB(x) read_dblle((const uint8_t *)(x))
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
423 static inline void write_u8(uint8_t *p, uint8_t x)
427 #define W8(p, x) write_u8((uint8_t *)(p), (uint8_t)(x))
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
434 static inline void write_u16be(uint8_t *p, uint16_t x)
436 p[1] = x & 0xff; x >>= 8;
437 p[0] = x & 0xff; x >>= 8;
439 #define WB16(p, x) write_u16be((uint8_t *)(p), (uint16_t)(x))
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
446 static inline void write_u16le(uint8_t *p, uint16_t x)
448 p[0] = x & 0xff; x >>= 8;
449 p[1] = x & 0xff; x >>= 8;
451 #define WL16(p, x) write_u16le((uint8_t *)(p), (uint16_t)(x))
454 * Write a 24 bits unsigned integer to memory stored as little endian.
455 * @param p a pointer to the output memory
456 * @param x the input unsigned integer
458 static inline void write_u24le(uint8_t *p, uint32_t x)
460 p[0] = x & 0xff; x >>= 8;
461 p[1] = x & 0xff; x >>= 8;
462 p[2] = x & 0xff; x >>= 8;
464 #define WL24(p, x) write_u24le((uint8_t *)(p), (uint32_t)(x))
467 * Write a 32 bits unsigned integer to memory stored as big endian.
468 * @param p a pointer to the output memory
469 * @param x the input unsigned integer
471 static inline void write_u32be(uint8_t *p, uint32_t x)
473 p[3] = x & 0xff; x >>= 8;
474 p[2] = x & 0xff; x >>= 8;
475 p[1] = x & 0xff; x >>= 8;
476 p[0] = x & 0xff; x >>= 8;
478 #define WB32(p, x) write_u32be((uint8_t *)(p), (uint32_t)(x))
481 * Write a 32 bits unsigned integer to memory stored as little endian.
482 * @param p a pointer to the output memory
483 * @param x the input unsigned integer
485 static inline void write_u32le(uint8_t *p, uint32_t x)
487 p[0] = x & 0xff; x >>= 8;
488 p[1] = x & 0xff; x >>= 8;
489 p[2] = x & 0xff; x >>= 8;
490 p[3] = x & 0xff; x >>= 8;
492 #define WL32(p, x) write_u32le((uint8_t *)(p), (uint32_t)(x))
495 * Write a 40 bits unsigned integer to memory stored as little endian.
496 * @param p a pointer to the output memory
497 * @param x the input unsigned integer
499 static inline void write_u40le(uint8_t *p, uint64_t x)
501 p[0] = x & 0xff; x >>= 8;
502 p[1] = x & 0xff; x >>= 8;
503 p[2] = x & 0xff; x >>= 8;
504 p[3] = x & 0xff; x >>= 8;
505 p[4] = x & 0xff; x >>= 8;
507 #define WL40(p, x) write_u40le((uint8_t *)(p), (uint64_t)(x))
510 * Write a 48 bits unsigned integer to memory stored as little endian.
511 * @param p a pointer to the output memory
512 * @param x the input unsigned integer
514 static inline void write_u48le(uint8_t *p, uint64_t x)
516 p[0] = x & 0xff; x >>= 8;
517 p[1] = x & 0xff; x >>= 8;
518 p[2] = x & 0xff; x >>= 8;
519 p[3] = x & 0xff; x >>= 8;
520 p[4] = x & 0xff; x >>= 8;
521 p[5] = x & 0xff; x >>= 8;
523 #define WL48(p, x) write_u48le((uint8_t *)(p), (uint64_t)(x))
526 * Write a 64 bits unsigned integer to memory stored as big endian.
527 * @param p a pointer to the output memory
528 * @param x the input unsigned integer
530 static inline void write_u64be(uint8_t *p, uint64_t x)
532 p[7] = x & 0xff; x >>= 8;
533 p[6] = x & 0xff; x >>= 8;
534 p[5] = x & 0xff; x >>= 8;
535 p[4] = x & 0xff; x >>= 8;
536 p[3] = x & 0xff; x >>= 8;
537 p[2] = x & 0xff; x >>= 8;
538 p[1] = x & 0xff; x >>= 8;
539 p[0] = x & 0xff; x >>= 8;
543 * Write a 64 bits unsigned integer to memory stored as little endian.
544 * @param p a pointer to the output memory
545 * @param x the input unsigned integer
547 static inline void write_u64le(uint8_t *p, uint64_t x)
549 p[0] = x & 0xff; x >>= 8;
550 p[1] = x & 0xff; x >>= 8;
551 p[2] = x & 0xff; x >>= 8;
552 p[3] = x & 0xff; x >>= 8;
553 p[4] = x & 0xff; x >>= 8;
554 p[5] = x & 0xff; x >>= 8;
555 p[6] = x & 0xff; x >>= 8;
556 p[7] = x & 0xff; x >>= 8;
558 #define WL64(p, x) write_u64le((uint8_t *)(p), (uint64_t)(x))
561 * Write a 32 bits float to memory stored as big endian.
562 * @param p a pointer to the output memory
563 * @param x the input float
565 static inline void write_fltbe(uint8_t *p, float x)
567 union { uint32_t u; float f; } u;
571 #define WBFL(p, x) write_fltbe((uint8_t *)(p), (x))
574 * Write a 32 bits float to memory stored as little endian.
575 * @param p a pointer to the output memory
576 * @param x the input float
578 static inline void write_fltle(uint8_t *p, float x)
580 union { uint32_t u; float f; } u;
584 #define WLFL(p, x) write_fltle((uint8_t *)(p), float (x))
587 * Write a 64 bits float to memory stored as little endian.
588 * @param p a pointer to the output memory
589 * @param x the input floating point value
591 static inline void write_dblle(uint8_t *p, double x)
593 union { uint64_t u; double f; } u;
597 #define WLDB(p, x) write_dblle((uint8_t *)(p), float (x))
599 /* Endianess conversion helpers with read/write position increment. */
602 * Read unsigned 8bit integer from raw memory, increment read position.
603 * @param[in, out] p Pointer into byte stream.
604 * @return Retrieved integer value, unsigned.
606 static inline uint8_t read_u8_inc(const uint8_t **p)
619 * Read unsigned 8bit integer, check length, increment read position.
620 * @param[in, out] p Pointer into byte stream.
621 * @param[in, out] l Remaining input payload length.
622 * @return Retrieved integer value, unsigned.
624 static inline uint8_t read_u8_inc_len(const uint8_t **p, size_t *l)
630 if (l && *l < sizeof(v)) {
643 * Read signed 8bit integer from raw memory, increment read position.
644 * @param[in, out] p Pointer into byte stream.
645 * @return Retrieved integer value, signed.
647 static inline int8_t read_i8_inc(const uint8_t **p)
660 * Read unsigned 16bit integer from raw memory (big endian format), increment read position.
661 * @param[in, out] p Pointer into byte stream.
662 * @return Retrieved integer value, unsigned.
664 static inline uint16_t read_u16be_inc(const uint8_t **p)
677 * Read unsigned 16bit integer from raw memory (little 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_u16le_inc(const uint8_t **p)
694 * Read unsigned 16bit integer (LE format), check length, increment position.
695 * @param[in, out] p Pointer into byte stream.
696 * @param[in, out] l Remaining input payload length.
697 * @return Retrieved integer value, unsigned.
699 static inline uint16_t read_u16le_inc_len(const uint8_t **p, size_t *l)
705 if (l && *l < sizeof(v)) {
718 * Read signed 16bit integer from raw memory (big endian format), increment read position.
719 * @param[in, out] p Pointer into byte stream.
720 * @return Retrieved integer value, signed.
722 static inline int16_t read_i16be_inc(const uint8_t **p)
735 * Read signed 16bit integer from raw memory (little 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_i16le_inc(const uint8_t **p)
752 * Read unsigned 24bit integer from raw memory (little endian format), increment read position.
753 * @param[in, out] p Pointer into byte stream.
754 * @return Retrieved integer value, unsigned.
756 static inline uint32_t read_u24le_inc(const uint8_t **p)
763 *p += 3 * sizeof(uint8_t);
769 * Read unsigned 32bit integer from raw memory (big 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_u32be_inc(const uint8_t **p)
786 * Read unsigned 32bit integer from raw memory (little 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_u32le_inc(const uint8_t **p)
803 * Read signed 32bit integer from raw memory (big endian format), increment read position.
804 * @param[in, out] p Pointer into byte stream.
805 * @return Retrieved integer value, signed.
807 static inline int32_t read_i32be_inc(const uint8_t **p)
820 * Read signed 32bit integer from raw memory (little 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_i32le_inc(const uint8_t **p)
837 * Read unsigned 64bit integer from raw memory (big endian format), increment read position.
838 * @param[in, out] p Pointer into byte stream.
839 * @return Retrieved integer value, unsigned.
841 static inline uint64_t read_u64be_inc(const uint8_t **p)
854 * Read unsigned 64bit integer from raw memory (little 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_u64le_inc(const uint8_t **p)
871 * Read 32bit float from raw memory (big endian format), increment read position.
872 * @param[in, out] p Pointer into byte stream.
873 * @return Retrieved float value.
875 static inline float read_fltbe_inc(const uint8_t **p)
888 * Read 32bit float from raw memory (little endian format), increment read position.
889 * @param[in, out] p Pointer into byte stream.
890 * @return Retrieved float value.
892 static inline float read_fltle_inc(const uint8_t **p)
905 * Read 64bit float from raw memory (big endian format), increment read position.
906 * @param[in, out] p Pointer into byte stream.
907 * @return Retrieved float value.
909 static inline double read_dblbe_inc(const uint8_t **p)
922 * Read 64bit float from raw memory (little endian format), increment read position.
923 * @param[in, out] p Pointer into byte stream.
924 * @return Retrieved float value.
926 static inline double read_dblle_inc(const uint8_t **p)
939 * Write unsigned 8bit integer to raw memory, increment write position.
940 * @param[in, out] p Pointer into byte stream.
941 * @param[in] x Value to write.
943 static inline void write_u8_inc(uint8_t **p, uint8_t x)
952 * Write unsigned 16bit big endian integer to raw memory, increment write position.
953 * @param[in, out] p Pointer into byte stream.
954 * @param[in] x Value to write.
956 static inline void write_u16be_inc(uint8_t **p, uint16_t x)
965 * Write unsigned 16bit little endian integer to raw memory, increment write position.
966 * @param[in, out] p Pointer into byte stream.
967 * @param[in] x Value to write.
969 static inline void write_u16le_inc(uint8_t **p, uint16_t x)
978 * Write unsigned 24bit liggle endian integer to raw memory, increment write position.
979 * @param[in, out] p Pointer into byte stream.
980 * @param[in] x Value to write.
982 static inline void write_u24le_inc(uint8_t **p, uint32_t x)
987 *p += 3 * sizeof(uint8_t);
991 * Write unsigned 32bit big endian integer to raw memory, increment write position.
992 * @param[in, out] p Pointer into byte stream.
993 * @param[in] x Value to write.
995 static inline void write_u32be_inc(uint8_t **p, uint32_t x)
1004 * Write unsigned 32bit little endian integer to raw memory, increment write position.
1005 * @param[in, out] p Pointer into byte stream.
1006 * @param[in] x Value to write.
1008 static inline void write_u32le_inc(uint8_t **p, uint32_t x)
1017 * Write unsigned 40bit little endian integer to raw memory, increment write position.
1018 * @param[in, out] p Pointer into byte stream.
1019 * @param[in] x Value to write.
1021 static inline void write_u40le_inc(uint8_t **p, uint64_t x)
1026 *p += 5 * sizeof(uint8_t);
1030 * Write unsigned 48bit little endian integer to raw memory, increment write position.
1031 * @param[in, out] p Pointer into byte stream.
1032 * @param[in] x Value to write.
1034 static inline void write_u48le_inc(uint8_t **p, uint64_t x)
1039 *p += 48 / 8 * sizeof(uint8_t);
1043 * Write unsigned 64bit little endian integer to raw memory, increment write position.
1044 * @param[in, out] p Pointer into byte stream.
1045 * @param[in] x Value to write.
1047 static inline void write_u64le_inc(uint8_t **p, uint64_t x)
1056 * Write single precision little endian float to raw memory, increment write position.
1057 * @param[in, out] p Pointer into byte stream.
1058 * @param[in] x Value to write.
1060 static inline void write_fltle_inc(uint8_t **p, float x)
1069 * Write double precision little endian float to raw memory, increment write position.
1070 * @param[in, out] p Pointer into byte stream.
1071 * @param[in] x Value to write.
1073 static inline void write_dblle_inc(uint8_t **p, double x)
1081 /* Portability fixes for FreeBSD. */
1083 #define LIBUSB_CLASS_APPLICATION 0xfe
1084 #define libusb_has_capability(x) 0
1085 #define libusb_handle_events_timeout_completed(ctx, tv, c) \
1086 libusb_handle_events_timeout(ctx, tv)
1090 * Convenience for FTDI library version dependency.
1091 * - Version 1.5 introduced ftdi_tciflush(), ftdi_tcoflush(), and
1092 * ftdi_tcioflush() all within the same commit, and deprecated
1093 * ftdi_usb_purge_buffers() which suffered from inverse semantics.
1094 * The API is drop-in compatible (arguments count and data types are
1095 * identical). The libsigrok source code always flushes RX and TX at
1096 * the same time, never individually.
1098 #if defined HAVE_FTDI_TCIOFLUSH && HAVE_FTDI_TCIOFLUSH
1099 # define PURGE_FTDI_BOTH ftdi_tcioflush
1101 # define PURGE_FTDI_BOTH ftdi_usb_purge_buffers
1104 /* Static definitions of structs ending with an all-zero entry are a
1105 * problem when compiling with -Wmissing-field-initializers: GCC
1106 * suppresses the warning only with { 0 }, clang wants { } */
1108 #define ALL_ZERO { }
1110 #define ALL_ZERO { 0 }
1114 #define SR_DRIVER_LIST_SECTION "__DATA,__sr_driver_list"
1116 #define SR_DRIVER_LIST_SECTION "__sr_driver_list"
1119 #if !defined SR_DRIVER_LIST_NOREORDER && defined __has_attribute
1120 #if __has_attribute(no_reorder)
1121 #define SR_DRIVER_LIST_NOREORDER __attribute__((no_reorder))
1124 #if !defined SR_DRIVER_LIST_NOREORDER
1125 #define SR_DRIVER_LIST_NOREORDER /* EMPTY */
1129 * Register a list of hardware drivers.
1131 * This macro can be used to register multiple hardware drivers to the library.
1132 * This is useful when a driver supports multiple similar but slightly
1133 * different devices that require different sr_dev_driver struct definitions.
1135 * For registering only a single driver see SR_REGISTER_DEV_DRIVER().
1139 * #define MY_DRIVER(_name) \
1140 * &(struct sr_dev_driver){ \
1145 * SR_REGISTER_DEV_DRIVER_LIST(my_driver_infos,
1146 * MY_DRIVER("driver 1"),
1147 * MY_DRIVER("driver 2"),
1152 * @param name Name to use for the driver list identifier.
1153 * @param ... Comma separated list of pointers to sr_dev_driver structs.
1155 #define SR_REGISTER_DEV_DRIVER_LIST(name, ...) \
1156 static const struct sr_dev_driver *name[] \
1157 SR_DRIVER_LIST_NOREORDER \
1158 __attribute__((section (SR_DRIVER_LIST_SECTION), used, \
1159 aligned(sizeof(struct sr_dev_driver *)))) \
1165 * Register a hardware driver.
1167 * This macro is used to register a hardware driver with the library. It has
1168 * to be used in order to make the driver accessible to applications using the
1171 * The macro invocation should be placed directly under the struct
1172 * sr_dev_driver definition.
1176 * static struct sr_dev_driver driver_info = {
1180 * SR_REGISTER_DEV_DRIVER(driver_info);
1183 * @param name Identifier name of sr_dev_driver struct to register.
1185 #define SR_REGISTER_DEV_DRIVER(name) \
1186 SR_REGISTER_DEV_DRIVER_LIST(name##_list, &name);
1188 SR_API void sr_drivers_init(struct sr_context *context);
1191 struct sr_dev_driver **driver_list;
1192 #ifdef HAVE_LIBUSB_1_0
1193 libusb_context *libusb_ctx;
1195 sr_resource_open_callback resource_open_cb;
1196 sr_resource_close_callback resource_close_cb;
1197 sr_resource_read_callback resource_read_cb;
1198 void *resource_cb_data;
1201 /** Input module metadata keys. */
1202 enum sr_input_meta_keys {
1203 /** The input filename, if there is one. */
1204 SR_INPUT_META_FILENAME = 0x01,
1205 /** The input file's size in bytes. */
1206 SR_INPUT_META_FILESIZE = 0x02,
1207 /** The first 128 bytes of the file, provided as a GString. */
1208 SR_INPUT_META_HEADER = 0x04,
1210 /** The module cannot identify a file without this metadata. */
1211 SR_INPUT_META_REQUIRED = 0x80,
1214 /** Input (file) module struct. */
1217 * A pointer to this input module's 'struct sr_input_module'.
1219 const struct sr_input_module *module;
1221 struct sr_dev_inst *sdi;
1226 /** Input (file) module driver. */
1227 struct sr_input_module {
1229 * A unique ID for this input module, suitable for use in command-line
1230 * clients, [a-z0-9-]. Must not be NULL.
1235 * A unique name for this input module, suitable for use in GUI
1236 * clients, can contain UTF-8. Must not be NULL.
1241 * A short description of the input module. Must not be NULL.
1243 * This can be displayed by frontends, e.g. when selecting the input
1244 * module for saving a file.
1249 * A NULL terminated array of strings containing a list of file name
1250 * extensions typical for the input file format, or NULL if there is
1251 * no typical extension for this file format.
1253 const char *const *exts;
1256 * Zero-terminated list of metadata items the module needs to be able
1257 * to identify an input stream. Can be all-zero, if the module cannot
1258 * identify streams at all, i.e. has to be forced into use.
1260 * Each item is one of:
1261 * SR_INPUT_META_FILENAME
1262 * SR_INPUT_META_FILESIZE
1263 * SR_INPUT_META_HEADER
1265 * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
1266 * identify a stream without the given metadata.
1268 const uint8_t metadata[8];
1271 * Returns a NULL-terminated list of options this module can take.
1272 * Can be NULL, if the module has no options.
1274 const struct sr_option *(*options) (void);
1277 * Check if this input module can load and parse the specified stream.
1279 * @param[in] metadata Metadata the module can use to identify the stream.
1280 * @param[out] confidence "Strength" of the detection.
1281 * Specialized handlers can take precedence over generic/basic support.
1283 * @retval SR_OK This module knows the format.
1284 * @retval SR_ERR_NA There wasn't enough data for this module to
1285 * positively identify the format.
1286 * @retval SR_ERR_DATA This module knows the format, but cannot handle
1287 * it. This means the stream is either corrupt, or indicates a
1288 * feature that the module does not support.
1289 * @retval SR_ERR This module does not know the format.
1291 * Lower numeric values of 'confidence' mean that the input module
1292 * stronger believes in its capability to handle this specific format.
1293 * This way, multiple input modules can claim support for a format,
1294 * and the application can pick the best match, or try fallbacks
1295 * in case of errors. This approach also copes with formats that
1296 * are unreliable to detect in the absence of magic signatures.
1298 int (*format_match) (GHashTable *metadata, unsigned int *confidence);
1301 * Initialize the input module.
1303 * @retval SR_OK Success
1304 * @retval other Negative error code.
1306 int (*init) (struct sr_input *in, GHashTable *options);
1309 * Send data to the specified input instance.
1311 * When an input module instance is created with sr_input_new(), this
1312 * function is used to feed data to the instance.
1314 * As enough data gets fed into this function to completely populate
1315 * the device instance associated with this input instance, this is
1316 * guaranteed to return the moment it's ready. This gives the caller
1317 * the chance to examine the device instance, attach session callbacks
1320 * @retval SR_OK Success
1321 * @retval other Negative error code.
1323 int (*receive) (struct sr_input *in, GString *buf);
1326 * Signal the input module no more data will come.
1328 * This will cause the module to process any data it may have buffered.
1329 * The SR_DF_END packet will also typically be sent at this time.
1331 int (*end) (struct sr_input *in);
1334 * Reset the input module's input handling structures.
1336 * Causes the input module to reset its internal state so that we can
1337 * re-send the input data from the beginning without having to
1338 * re-create the entire input module.
1340 * @retval SR_OK Success.
1341 * @retval other Negative error code.
1343 int (*reset) (struct sr_input *in);
1346 * This function is called after the caller is finished using
1347 * the input module, and can be used to free any internal
1348 * resources the module may keep.
1350 * This function is optional.
1352 * @retval SR_OK Success
1353 * @retval other Negative error code.
1355 void (*cleanup) (struct sr_input *in);
1358 /** Output module instance. */
1360 /** A pointer to this output's module. */
1361 const struct sr_output_module *module;
1364 * The device for which this output module is creating output. This
1365 * can be used by the module to find out channel names and numbers.
1367 const struct sr_dev_inst *sdi;
1370 * The name of the file that the data should be written to.
1372 const char *filename;
1375 * A generic pointer which can be used by the module to keep internal
1376 * state between calls into its callback functions.
1378 * For example, the module might store a pointer to a chunk of output
1379 * there, and only flush it when it reaches a certain size.
1384 /** Output module driver. */
1385 struct sr_output_module {
1387 * A unique ID for this output module, suitable for use in command-line
1388 * clients, [a-z0-9-]. Must not be NULL.
1393 * A unique name for this output module, suitable for use in GUI
1394 * clients, can contain UTF-8. Must not be NULL.
1399 * A short description of the output module. Must not be NULL.
1401 * This can be displayed by frontends, e.g. when selecting the output
1402 * module for saving a file.
1407 * A NULL terminated array of strings containing a list of file name
1408 * extensions typical for the input file format, or NULL if there is
1409 * no typical extension for this file format.
1411 const char *const *exts;
1414 * Bitfield containing flags that describe certain properties
1415 * this output module may or may not have.
1416 * @see sr_output_flags
1418 const uint64_t flags;
1421 * Returns a NULL-terminated list of options this module can take.
1422 * Can be NULL, if the module has no options.
1424 const struct sr_option *(*options) (void);
1427 * This function is called once, at the beginning of an output stream.
1429 * The device struct will be available in the output struct passed in,
1430 * as well as the param field -- which may be NULL or an empty string,
1431 * if no parameter was passed.
1433 * The module can use this to initialize itself, create a struct for
1434 * keeping state and storing it in the <code>internal</code> field.
1436 * @param o Pointer to the respective 'struct sr_output'.
1438 * @retval SR_OK Success
1439 * @retval other Negative error code.
1441 int (*init) (struct sr_output *o, GHashTable *options);
1444 * This function is passed a copy of every packet in the data feed.
1445 * Any output generated by the output module in response to the
1446 * packet should be returned in a newly allocated GString
1447 * <code>out</code>, which will be freed by the caller.
1449 * Packets not of interest to the output module can just be ignored,
1450 * and the <code>out</code> parameter set to NULL.
1452 * @param o Pointer to the respective 'struct sr_output'.
1453 * @param sdi The device instance that generated the packet.
1454 * @param packet The complete packet.
1455 * @param out A pointer where a GString * should be stored if
1456 * the module generates output, or NULL if not.
1458 * @retval SR_OK Success
1459 * @retval other Negative error code.
1461 int (*receive) (const struct sr_output *o,
1462 const struct sr_datafeed_packet *packet, GString **out);
1465 * This function is called after the caller is finished using
1466 * the output module, and can be used to free any internal
1467 * resources the module may keep.
1469 * @retval SR_OK Success
1470 * @retval other Negative error code.
1472 int (*cleanup) (struct sr_output *o);
1475 /** Transform module instance. */
1476 struct sr_transform {
1477 /** A pointer to this transform's module. */
1478 const struct sr_transform_module *module;
1481 * The device for which this transform module is used. This
1482 * can be used by the module to find out channel names and numbers.
1484 const struct sr_dev_inst *sdi;
1487 * A generic pointer which can be used by the module to keep internal
1488 * state between calls into its callback functions.
1493 struct sr_transform_module {
1495 * A unique ID for this transform module, suitable for use in
1496 * command-line clients, [a-z0-9-]. Must not be NULL.
1501 * A unique name for this transform module, suitable for use in GUI
1502 * clients, can contain UTF-8. Must not be NULL.
1507 * A short description of the transform module. Must not be NULL.
1509 * This can be displayed by frontends, e.g. when selecting
1510 * which transform module(s) to add.
1515 * Returns a NULL-terminated list of options this transform module
1516 * can take. Can be NULL, if the transform module has no options.
1518 const struct sr_option *(*options) (void);
1521 * This function is called once, at the beginning of a stream.
1523 * @param t Pointer to the respective 'struct sr_transform'.
1524 * @param options Hash table of options for this transform module.
1525 * Can be NULL if no options are to be used.
1527 * @retval SR_OK Success
1528 * @retval other Negative error code.
1530 int (*init) (struct sr_transform *t, GHashTable *options);
1533 * This function is passed a pointer to every packet in the data feed.
1535 * It can either return (in packet_out) a pointer to another packet
1536 * (possibly the exact same packet it got as input), or NULL.
1538 * @param t Pointer to the respective 'struct sr_transform'.
1539 * @param packet_in Pointer to a datafeed packet.
1540 * @param packet_out Pointer to the resulting datafeed packet after
1541 * this function was run. If NULL, the transform
1542 * module intentionally didn't output a new packet.
1544 * @retval SR_OK Success
1545 * @retval other Negative error code.
1547 int (*receive) (const struct sr_transform *t,
1548 struct sr_datafeed_packet *packet_in,
1549 struct sr_datafeed_packet **packet_out);
1552 * This function is called after the caller is finished using
1553 * the transform module, and can be used to free any internal
1554 * resources the module may keep.
1556 * @retval SR_OK Success
1557 * @retval other Negative error code.
1559 int (*cleanup) (struct sr_transform *t);
1562 #ifdef HAVE_LIBUSB_1_0
1563 /** USB device instance */
1564 struct sr_usb_dev_inst {
1567 /** Device address on USB bus */
1569 /** libusb device handle */
1570 struct libusb_device_handle *devhdl;
1574 struct sr_serial_dev_inst;
1575 #ifdef HAVE_SERIAL_COMM
1576 struct ser_lib_functions;
1577 struct ser_hid_chip_functions;
1579 typedef void (*serial_rx_chunk_callback)(struct sr_serial_dev_inst *serial,
1580 void *cb_data, const void *buf, size_t count);
1581 struct sr_serial_dev_inst {
1582 /** Port name, e.g. '/dev/tty42'. */
1584 /** Comm params for serial_set_paramstr(). */
1586 struct ser_lib_functions *lib_funcs;
1593 GString *rcv_buffer;
1594 serial_rx_chunk_callback rx_chunk_cb_func;
1595 void *rx_chunk_cb_data;
1596 #ifdef HAVE_LIBSERIALPORT
1597 /** libserialport port handle */
1598 struct sp_port *sp_data;
1600 #ifdef HAVE_LIBHIDAPI
1601 enum ser_hid_chip_t {
1602 SER_HID_CHIP_UNKNOWN, /**!< place holder */
1603 SER_HID_CHIP_BTC_BU86X, /**!< Brymen BU86x */
1604 SER_HID_CHIP_SIL_CP2110, /**!< SiLabs CP2110 */
1605 SER_HID_CHIP_VICTOR_DMM, /**!< Victor 70/86 DMM cable */
1606 SER_HID_CHIP_WCH_CH9325, /**!< WCH CH9325 */
1607 SER_HID_CHIP_LAST, /**!< sentinel */
1609 struct ser_hid_chip_functions *hid_chip_funcs;
1612 const char *hid_path;
1613 hid_device *hid_dev;
1614 GSList *hid_source_args;
1616 #ifdef HAVE_BLUETOOTH
1617 enum ser_bt_conn_t {
1618 SER_BT_CONN_UNKNOWN, /**!< place holder */
1619 SER_BT_CONN_RFCOMM, /**!< BT classic, RFCOMM channel */
1620 SER_BT_CONN_BLE122, /**!< BLE, BLE122 module, indications */
1621 SER_BT_CONN_NRF51, /**!< BLE, Nordic nRF51, notifications */
1622 SER_BT_CONN_CC254x, /**!< BLE, TI CC254x, notifications */
1623 SER_BT_CONN_AC6328, /**!< BLE, JL AC6328B, notifications */
1624 SER_BT_CONN_MAX, /**!< sentinel */
1626 char *bt_addr_local;
1627 char *bt_addr_remote;
1628 size_t bt_rfcomm_channel;
1629 uint16_t bt_notify_handle_read;
1630 uint16_t bt_notify_handle_write;
1631 uint16_t bt_notify_handle_cccd;
1632 uint16_t bt_notify_value_cccd;
1633 uint16_t bt_ble_mtu;
1634 struct sr_bt_desc *bt_desc;
1635 GSList *bt_source_args;
1640 struct sr_usbtmc_dev_inst {
1645 /* Private driver context. */
1646 struct drv_context {
1647 /** sigrok context */
1648 struct sr_context *sr_ctx;
1652 /*--- log.c -----------------------------------------------------------------*/
1654 #if defined(_WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
1656 * On MinGW, we need to specify the gnu_printf format flavor or GCC
1657 * will assume non-standard Microsoft printf syntax.
1659 SR_PRIV int sr_log(int loglevel, const char *format, ...)
1660 __attribute__((__format__ (__gnu_printf__, 2, 3)));
1662 SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
1665 /* Message logging helpers with subsystem-specific prefix string. */
1666 #define sr_spew(...) sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
1667 #define sr_dbg(...) sr_log(SR_LOG_DBG, LOG_PREFIX ": " __VA_ARGS__)
1668 #define sr_info(...) sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
1669 #define sr_warn(...) sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
1670 #define sr_err(...) sr_log(SR_LOG_ERR, LOG_PREFIX ": " __VA_ARGS__)
1672 /*--- device.c --------------------------------------------------------------*/
1674 /** Scan options supported by a driver. */
1675 #define SR_CONF_SCAN_OPTIONS 0x7FFF0000
1677 /** Device options for a particular device. */
1678 #define SR_CONF_DEVICE_OPTIONS 0x7FFF0001
1680 /** Mask for separating config keys from capabilities. */
1681 #define SR_CONF_MASK 0x1fffffff
1683 /** Values for the changes argument of sr_dev_driver.config_channel_set. */
1685 /** The enabled state of the channel has been changed. */
1686 SR_CHANNEL_SET_ENABLED = 1 << 0,
1689 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
1690 int index, int type, gboolean enabled, const char *name);
1691 SR_PRIV void sr_channel_free(struct sr_channel *ch);
1692 SR_PRIV void sr_channel_free_cb(void *p);
1693 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
1694 struct sr_channel *cur_channel);
1695 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2);
1696 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2);
1698 SR_PRIV struct sr_channel_group *sr_channel_group_new(struct sr_dev_inst *sdi,
1699 const char *name, void *priv);
1700 SR_PRIV void sr_channel_group_free(struct sr_channel_group *cg);
1701 SR_PRIV void sr_channel_group_free_cb(void *cg);
1703 /** Device instance data */
1704 struct sr_dev_inst {
1705 /** Device driver. */
1706 struct sr_dev_driver *driver;
1707 /** Device instance status. SR_ST_NOT_FOUND, etc. */
1709 /** Device instance type. SR_INST_USB, etc. */
1711 /** Device vendor. */
1713 /** Device model. */
1715 /** Device version. */
1717 /** Serial number. */
1719 /** Connection string to uniquely identify devices. */
1720 char *connection_id;
1721 /** List of channels. */
1723 /** List of sr_channel_group structs */
1724 GSList *channel_groups;
1725 /** Device instance connection data (used?) */
1727 /** Device instance private data (used?) */
1729 /** Session to which this device is currently assigned. */
1730 struct sr_session *session;
1733 /* Generic device instances */
1734 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
1736 #ifdef HAVE_LIBUSB_1_0
1737 /* USB-specific instances */
1738 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
1739 uint8_t address, struct libusb_device_handle *hdl);
1740 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
1741 SR_PRIV void sr_usb_dev_inst_free_cb(gpointer p); /* Glib wrapper. */
1744 #ifdef HAVE_SERIAL_COMM
1745 #ifndef HAVE_LIBSERIALPORT
1747 * Some identifiers which initially got provided by libserialport are
1748 * used internally within the libsigrok serial layer's implementation,
1749 * while libserialport no longer is the exclusive provider of serial
1750 * communication support. Declare the identifiers here so they remain
1751 * available across all build configurations.
1758 SP_PARITY_SPACE = 4,
1761 enum libsp_flowcontrol {
1762 SP_FLOWCONTROL_NONE = 0,
1763 SP_FLOWCONTROL_XONXOFF = 1,
1764 SP_FLOWCONTROL_RTSCTS = 2,
1765 SP_FLOWCONTROL_DTRDSR = 3,
1769 /* Serial-specific instances */
1770 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
1771 const char *serialcomm);
1772 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
1775 /* USBTMC-specific instances */
1776 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
1777 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
1779 /*--- hwdriver.c ------------------------------------------------------------*/
1781 SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
1782 SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
1783 SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
1784 SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
1785 SR_PRIV void sr_config_free(struct sr_config *src);
1786 SR_PRIV int sr_dev_acquisition_start(struct sr_dev_inst *sdi);
1787 SR_PRIV int sr_dev_acquisition_stop(struct sr_dev_inst *sdi);
1789 /*--- session.c -------------------------------------------------------------*/
1792 /** Context this session exists in. */
1793 struct sr_context *ctx;
1794 /** List of struct sr_dev_inst pointers. */
1796 /** List of struct sr_dev_inst pointers owned by this session. */
1798 /** List of struct datafeed_callback pointers. */
1799 GSList *datafeed_callbacks;
1801 struct sr_trigger *trigger;
1803 /** Callback to invoke on session stop. */
1804 sr_session_stopped_callback stopped_callback;
1805 /** User data to be passed to the session stop callback. */
1806 void *stopped_cb_data;
1808 /** Mutex protecting the main context pointer. */
1810 /** Context of the session main loop. */
1811 GMainContext *main_context;
1813 /** Registered event sources for this session. */
1814 GHashTable *event_sources;
1815 /** Session main loop. */
1816 GMainLoop *main_loop;
1817 /** ID of idle source for dispatching the session stop notification. */
1818 unsigned int stop_check_id;
1819 /** Whether the session has been started. */
1823 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
1824 void *key, GSource *source);
1825 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
1827 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1828 void *key, GSource *source);
1829 SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1830 void *key, gintptr fd, int events, int timeout,
1831 sr_receive_data_callback cb, void *cb_data);
1833 SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
1834 int events, int timeout, sr_receive_data_callback cb, void *cb_data);
1835 SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
1836 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1838 SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
1839 GIOChannel *channel, int events, int timeout,
1840 sr_receive_data_callback cb, void *cb_data);
1841 SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
1842 SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
1844 SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
1845 GIOChannel *channel);
1847 SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi,
1848 uint32_t key, GVariant *var);
1849 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
1850 const struct sr_datafeed_packet *packet);
1851 SR_PRIV int sr_sessionfile_check(const char *filename);
1852 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename,
1853 struct sr_session **session);
1855 /*--- session_file.c --------------------------------------------------------*/
1857 #if !HAVE_ZIP_DISCARD
1858 /* Replace zip_discard() if not available. */
1859 #define zip_discard(zip) sr_zip_discard(zip)
1860 SR_PRIV void sr_zip_discard(struct zip *archive);
1863 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
1864 const struct zip_stat *entry);
1866 /*--- analog.c --------------------------------------------------------------*/
1868 SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
1869 struct sr_analog_encoding *encoding,
1870 struct sr_analog_meaning *meaning,
1871 struct sr_analog_spec *spec,
1874 /*--- std.c -----------------------------------------------------------------*/
1876 typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
1877 typedef void (*std_dev_clear_callback)(void *priv);
1879 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx);
1880 SR_PRIV int std_cleanup(const struct sr_dev_driver *di);
1881 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
1882 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
1883 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
1884 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
1885 #ifdef HAVE_SERIAL_COMM
1886 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
1887 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
1889 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi);
1890 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi);
1891 SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi);
1892 SR_PRIV int std_session_send_df_frame_begin(const struct sr_dev_inst *sdi);
1893 SR_PRIV int std_session_send_df_frame_end(const struct sr_dev_inst *sdi);
1894 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
1895 std_dev_clear_callback clear_private);
1896 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver);
1897 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di);
1898 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
1899 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices);
1901 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
1902 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
1903 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
1904 size_t drvsize, const uint32_t devopts[], size_t devsize);
1906 extern SR_PRIV const uint32_t NO_OPTS[1];
1908 #define STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts) \
1909 std_opts_config_list(key, data, sdi, cg, ARRAY_AND_SIZE(scanopts), \
1910 ARRAY_AND_SIZE(drvopts), ARRAY_AND_SIZE(devopts))
1912 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n);
1913 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n);
1914 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n);
1915 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n);
1916 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step);
1917 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]);
1918 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double dmin, const double dmax, const double dstep);
1920 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high);
1921 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high);
1923 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n);
1924 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n);
1925 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n);
1926 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n);
1928 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n);
1930 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n);
1931 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n);
1932 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n);
1934 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n);
1935 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n);
1937 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n);
1938 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
1939 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
1941 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
1943 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
1944 int baudrate, int bits, int parity, int stopbits,
1945 int flowcontrol, int rts, int dtr);
1946 SR_PRIV int std_dummy_set_handshake(struct sr_serial_dev_inst *serial,
1949 /*--- resource.c ------------------------------------------------------------*/
1951 SR_PRIV int64_t sr_file_get_size(FILE *file);
1953 SR_PRIV int sr_resource_open(struct sr_context *ctx,
1954 struct sr_resource *res, int type, const char *name)
1955 G_GNUC_WARN_UNUSED_RESULT;
1956 SR_PRIV int sr_resource_close(struct sr_context *ctx,
1957 struct sr_resource *res);
1958 SR_PRIV gssize sr_resource_read(struct sr_context *ctx,
1959 const struct sr_resource *res, void *buf, size_t count)
1960 G_GNUC_WARN_UNUSED_RESULT;
1961 SR_PRIV void *sr_resource_load(struct sr_context *ctx, int type,
1962 const char *name, size_t *size, size_t max_size)
1963 G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
1965 /*--- strutil.c -------------------------------------------------------------*/
1967 SR_PRIV int sr_atol(const char *str, long *ret);
1968 SR_PRIV int sr_atol_base(const char *str, long *ret, char **end, int base);
1969 SR_PRIV int sr_atoul_base(const char *str, unsigned long *ret, char **end, int base);
1970 SR_PRIV int sr_atoi(const char *str, int *ret);
1971 SR_PRIV int sr_atod(const char *str, double *ret);
1972 SR_PRIV int sr_atof(const char *str, float *ret);
1973 SR_PRIV int sr_atod_ascii(const char *str, double *ret);
1974 SR_PRIV int sr_atod_ascii_digits(const char *str, double *ret, int *digits);
1975 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
1977 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
1978 SR_PRIV void sr_hexdump_free(GString *s);
1980 /*--- soft-trigger.c --------------------------------------------------------*/
1982 struct soft_trigger_logic {
1983 const struct sr_dev_inst *sdi;
1984 const struct sr_trigger *trigger;
1988 uint8_t *prev_sample;
1989 uint8_t *pre_trigger_buffer;
1990 uint8_t *pre_trigger_head;
1991 int pre_trigger_size;
1992 int pre_trigger_fill;
1995 SR_PRIV int logic_channel_unitsize(GSList *channels);
1996 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
1997 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
1998 int pre_trigger_samples);
1999 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
2000 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
2001 int len, int *pre_trigger_samples);
2003 /*--- serial.c --------------------------------------------------------------*/
2005 #ifdef HAVE_SERIAL_COMM
2011 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
2012 typedef int (*packet_valid_len_callback)(void *st,
2013 const uint8_t *p, size_t l, size_t *pl);
2015 typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
2017 typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
2019 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
2020 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
2021 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
2022 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
2023 SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
2024 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
2025 const void *buf, size_t count, unsigned int timeout_ms);
2026 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
2027 const void *buf, size_t count);
2028 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
2029 size_t count, unsigned int timeout_ms);
2030 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
2032 SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
2033 serial_rx_chunk_callback cb, void *cb_data);
2034 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
2035 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
2036 SR_PRIV int serial_set_handshake(struct sr_serial_dev_inst *serial,
2038 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
2039 const char *paramstr);
2040 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
2041 int *buflen, gint64 timeout_ms);
2042 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
2043 uint8_t *buf, size_t *buflen,
2044 size_t packet_size, packet_valid_callback is_valid,
2045 packet_valid_len_callback is_valid_len, size_t *return_size,
2046 uint64_t timeout_ms);
2047 SR_PRIV int serial_source_add(struct sr_session *session,
2048 struct sr_serial_dev_inst *serial, int events, int timeout,
2049 sr_receive_data_callback cb, void *cb_data);
2050 SR_PRIV int serial_source_remove(struct sr_session *session,
2051 struct sr_serial_dev_inst *serial);
2052 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
2053 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
2055 SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial);
2056 SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial);
2057 SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
2058 const uint8_t *data, size_t len);
2059 SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
2060 uint8_t *data, size_t len);
2062 struct ser_lib_functions {
2063 int (*open)(struct sr_serial_dev_inst *serial, int flags);
2064 int (*close)(struct sr_serial_dev_inst *serial);
2065 int (*flush)(struct sr_serial_dev_inst *serial);
2066 int (*drain)(struct sr_serial_dev_inst *serial);
2067 int (*write)(struct sr_serial_dev_inst *serial,
2068 const void *buf, size_t count,
2069 int nonblocking, unsigned int timeout_ms);
2070 int (*read)(struct sr_serial_dev_inst *serial,
2071 void *buf, size_t count,
2072 int nonblocking, unsigned int timeout_ms);
2073 int (*set_params)(struct sr_serial_dev_inst *serial,
2074 int baudrate, int bits, int parity, int stopbits,
2075 int flowcontrol, int rts, int dtr);
2076 int (*set_handshake)(struct sr_serial_dev_inst *serial,
2078 int (*setup_source_add)(struct sr_session *session,
2079 struct sr_serial_dev_inst *serial,
2080 int events, int timeout,
2081 sr_receive_data_callback cb, void *cb_data);
2082 int (*setup_source_remove)(struct sr_session *session,
2083 struct sr_serial_dev_inst *serial);
2084 GSList *(*list)(GSList *list, sr_ser_list_append_t append);
2085 GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
2086 uint16_t vendor_id, uint16_t product_id);
2087 int (*get_frame_format)(struct sr_serial_dev_inst *serial,
2088 int *baud, int *bits);
2089 size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
2091 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
2092 SR_PRIV int ser_name_is_hid(struct sr_serial_dev_inst *serial);
2093 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_hid;
2094 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial);
2095 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt;
2097 #ifdef HAVE_LIBHIDAPI
2098 struct vid_pid_item {
2102 struct ser_hid_chip_functions {
2103 const char *chipname;
2104 const char *chipdesc;
2105 const struct vid_pid_item *vid_pid_items;
2106 const int max_bytes_per_request;
2107 int (*set_params)(struct sr_serial_dev_inst *serial,
2108 int baudrate, int bits, int parity, int stopbits,
2109 int flowcontrol, int rts, int dtr);
2110 int (*read_bytes)(struct sr_serial_dev_inst *serial,
2111 uint8_t *data, int space, unsigned int timeout);
2112 int (*write_bytes)(struct sr_serial_dev_inst *serial,
2113 const uint8_t *data, int space);
2114 int (*flush)(struct sr_serial_dev_inst *serial);
2115 int (*drain)(struct sr_serial_dev_inst *serial);
2117 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x;
2118 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325;
2119 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110;
2120 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor;
2121 SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid);
2125 SR_PRIV int sr_serial_extract_options(GSList *options,
2126 const char **serial_device, const char **serial_options);
2128 /*--- bt/ API ---------------------------------------------------------------*/
2130 #ifdef HAVE_BLUETOOTH
2131 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx);
2134 typedef void (*sr_bt_scan_cb)(void *cb_data, const char *addr, const char *name);
2135 typedef int (*sr_bt_data_cb)(void *cb_data, uint8_t *data, size_t dlen);
2137 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void);
2138 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc);
2140 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
2141 sr_bt_scan_cb cb, void *cb_data);
2142 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
2143 sr_bt_data_cb cb, void *cb_data);
2144 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr);
2145 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr);
2146 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel);
2147 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
2148 uint16_t read_handle, uint16_t write_handle,
2149 uint16_t cccd_handle, uint16_t cccd_value,
2152 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration);
2153 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration);
2155 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc);
2156 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc);
2157 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc);
2159 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc,
2160 void *data, size_t len);
2161 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
2162 const void *data, size_t len);
2164 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc);
2165 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc);
2168 /*--- ezusb.c ---------------------------------------------------------------*/
2170 #ifdef HAVE_LIBUSB_1_0
2171 SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
2172 SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
2174 SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
2175 int configuration, const char *name);
2178 /*--- usb.c -----------------------------------------------------------------*/
2180 SR_PRIV int sr_usb_split_conn(const char *conn,
2181 uint16_t *vid, uint16_t *pid, uint8_t *bus, uint8_t *addr);
2182 #ifdef HAVE_LIBUSB_1_0
2183 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
2184 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
2185 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
2186 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
2187 int timeout, sr_receive_data_callback cb, void *cb_data);
2188 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
2189 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
2190 SR_PRIV gboolean usb_match_manuf_prod(libusb_device *dev,
2191 const char *manufacturer, const char *product);
2194 /*--- binary_helpers.c ------------------------------------------------------*/
2196 /** Binary value type */
2197 enum binary_value_type {
2201 BVT_BE_UINT8 = BVT_UINT8,
2202 BVT_LE_UINT8 = BVT_UINT8,
2215 /** Binary value specification */
2216 struct binary_value_spec {
2217 size_t offset; /**!< Offset into binary image */
2218 enum binary_value_type type; /**!< Data type to decode */
2219 float scale; /**!< Scale factor to native units */
2223 * Read extract a value from a binary data image.
2225 * @param[out] out Pointer to output buffer (conversion result)
2226 * @param[in] spec Binary value specification
2227 * @param[in] data Pointer to binary input data
2228 * @param[in] length Size of binary input data
2230 * @return SR_OK on success, SR_ERR_* error code on failure.
2232 SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec,
2233 const void *data, size_t length);
2235 /*--- crc.c -----------------------------------------------------------------*/
2237 #define SR_CRC16_DEFAULT_INIT 0xffffU
2240 * Calculate a CRC16 checksum using the 0x8005 polynomial.
2242 * This CRC16 flavor is also known as CRC16-ANSI or CRC16-MODBUS.
2244 * @param crc Initial value (typically 0xffff)
2245 * @param buffer Input buffer
2246 * @param len Buffer length
2249 SR_PRIV uint16_t sr_crc16(uint16_t crc, const uint8_t *buffer, int len);
2251 /*--- modbus/modbus.c -------------------------------------------------------*/
2253 struct sr_modbus_dev_inst {
2257 GSList *(*scan)(int modbusaddr);
2258 int (*dev_inst_new)(void *priv, const char *resource,
2259 char **params, const char *serialcomm, int modbusaddr);
2260 int (*open)(void *priv);
2261 int (*source_add)(struct sr_session *session, void *priv, int events,
2262 int timeout, sr_receive_data_callback cb, void *cb_data);
2263 int (*source_remove)(struct sr_session *session, void *priv);
2264 int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
2265 int (*read_begin)(void *priv, uint8_t *function_code);
2266 int (*read_data)(void *priv, uint8_t *buf, int maxlen);
2267 int (*read_end)(void *priv);
2268 int (*close)(void *priv);
2269 void (*free)(void *priv);
2270 unsigned int read_timeout_ms;
2274 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
2275 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
2276 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
2277 const char *serialcomm, int modbusaddr);
2278 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
2279 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
2280 struct sr_modbus_dev_inst *modbus, int events, int timeout,
2281 sr_receive_data_callback cb, void *cb_data);
2282 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
2283 struct sr_modbus_dev_inst *modbus);
2284 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
2285 uint8_t *request, int request_size);
2286 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
2287 uint8_t *reply, int reply_size);
2288 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
2289 uint8_t *request, int request_size,
2290 uint8_t *reply, int reply_size);
2291 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
2292 int address, int nb_coils, uint8_t *coils);
2293 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
2294 int address, int nb_registers,
2295 uint16_t *registers);
2296 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
2297 int address, int value);
2298 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
2299 int address, int nb_registers,
2300 uint16_t *registers);
2301 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
2302 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
2304 /*--- dmm/es519xx.c ---------------------------------------------------------*/
2307 * All 11-byte es519xx chips repeat each block twice for each conversion cycle
2308 * so always read 2 blocks at a time.
2310 #define ES519XX_11B_PACKET_SIZE (11 * 2)
2311 #define ES519XX_14B_PACKET_SIZE 14
2313 struct es519xx_info {
2314 gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
2315 gboolean is_milli, is_resistance, is_continuity, is_diode;
2316 gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
2317 gboolean is_temperature, is_celsius, is_fahrenheit;
2318 gboolean is_adp0, is_adp1, is_adp2, is_adp3;
2319 gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
2320 gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
2321 gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
2324 gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
2328 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
2329 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
2330 struct sr_datafeed_analog *analog, void *info);
2331 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
2332 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
2333 float *floatval, struct sr_datafeed_analog *analog, void *info);
2334 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
2335 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
2336 float *floatval, struct sr_datafeed_analog *analog, void *info);
2337 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
2338 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
2339 float *floatval, struct sr_datafeed_analog *analog, void *info);
2340 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
2341 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
2342 struct sr_datafeed_analog *analog, void *info);
2343 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
2344 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
2345 struct sr_datafeed_analog *analog, void *info);
2346 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
2347 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
2348 float *floatval, struct sr_datafeed_analog *analog, void *info);
2350 /*--- dmm/fs9922.c ----------------------------------------------------------*/
2352 #define FS9922_PACKET_SIZE 14
2354 struct fs9922_info {
2355 gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
2356 gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
2357 gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
2358 gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
2359 gboolean is_celsius, is_fahrenheit;
2360 int bargraph_sign, bargraph_value;
2363 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
2364 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
2365 struct sr_datafeed_analog *analog, void *info);
2366 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
2368 /*--- dmm/fs9721.c ----------------------------------------------------------*/
2370 #define FS9721_PACKET_SIZE 14
2372 struct fs9721_info {
2373 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2374 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2375 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2376 gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
2379 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
2380 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
2381 struct sr_datafeed_analog *analog, void *info);
2382 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
2383 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
2384 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
2385 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
2386 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
2388 /*--- dmm/mm38xr.c ---------------------------------------------------------*/
2390 #define METERMAN_38XR_PACKET_SIZE 15
2392 struct meterman_38xr_info { int dummy; };
2394 SR_PRIV gboolean meterman_38xr_packet_valid(const uint8_t *buf);
2395 SR_PRIV int meterman_38xr_parse(const uint8_t *buf, float *floatval,
2396 struct sr_datafeed_analog *analog, void *info);
2398 /*--- dmm/ms2115b.c ---------------------------------------------------------*/
2400 #define MS2115B_PACKET_SIZE 9
2402 enum ms2115b_display {
2403 MS2115B_DISPLAY_MAIN,
2404 MS2115B_DISPLAY_SUB,
2405 MS2115B_DISPLAY_COUNT,
2408 struct ms2115b_info {
2409 /* Selected channel. */
2411 gboolean is_ac, is_dc, is_auto;
2412 gboolean is_diode, is_beep, is_farad;
2413 gboolean is_ohm, is_ampere, is_volt, is_hz;
2414 gboolean is_duty_cycle, is_percent;
2417 extern SR_PRIV const char *ms2115b_channel_formats[];
2418 SR_PRIV gboolean sr_ms2115b_packet_valid(const uint8_t *buf);
2419 SR_PRIV int sr_ms2115b_parse(const uint8_t *buf, float *floatval,
2420 struct sr_datafeed_analog *analog, void *info);
2422 /*--- dmm/ms8250d.c ---------------------------------------------------------*/
2424 #define MS8250D_PACKET_SIZE 18
2426 struct ms8250d_info {
2427 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2428 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2429 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2430 gboolean is_ncv, is_min, is_max, is_sign, is_autotimer;
2433 SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf);
2434 SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
2435 struct sr_datafeed_analog *analog, void *info);
2437 /*--- dmm/dtm0660.c ---------------------------------------------------------*/
2439 #define DTM0660_PACKET_SIZE 15
2441 struct dtm0660_info {
2442 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2443 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2444 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2445 gboolean is_degf, is_degc, is_c2c1_01, is_c2c1_00, is_apo, is_min;
2446 gboolean is_minmax, is_max, is_sign;
2449 SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf);
2450 SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
2451 struct sr_datafeed_analog *analog, void *info);
2453 /*--- dmm/m2110.c -----------------------------------------------------------*/
2455 #define BBCGM_M2110_PACKET_SIZE 9
2457 /* Dummy info struct. The parser does not use it. */
2458 struct m2110_info { int dummy; };
2460 SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
2461 SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
2462 struct sr_datafeed_analog *analog, void *info);
2464 /*--- dmm/metex14.c ---------------------------------------------------------*/
2466 #define METEX14_PACKET_SIZE 14
2468 struct metex14_info {
2470 gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
2471 gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
2472 gboolean is_hertz, is_ohm, is_celsius, is_fahrenheit, is_watt;
2473 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2474 gboolean is_gain, is_decibel, is_power, is_decibel_mw, is_power_factor;
2475 gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
2478 #ifdef HAVE_SERIAL_COMM
2479 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
2481 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
2482 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
2483 struct sr_datafeed_analog *analog, void *info);
2484 SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf);
2485 SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
2486 struct sr_datafeed_analog *analog, void *info);
2488 /*--- dmm/rs9lcd.c ----------------------------------------------------------*/
2490 #define RS9LCD_PACKET_SIZE 9
2492 /* Dummy info struct. The parser does not use it. */
2493 struct rs9lcd_info { int dummy; };
2495 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
2496 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
2497 struct sr_datafeed_analog *analog, void *info);
2499 /*--- dmm/bm25x.c -----------------------------------------------------------*/
2501 #define BRYMEN_BM25X_PACKET_SIZE 15
2503 /* Dummy info struct. The parser does not use it. */
2504 struct bm25x_info { int dummy; };
2506 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
2507 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
2508 struct sr_datafeed_analog *analog, void *info);
2510 /*--- dmm/bm52x.c -----------------------------------------------------------*/
2512 #define BRYMEN_BM52X_PACKET_SIZE 24
2513 #define BRYMEN_BM52X_DISPLAY_COUNT 2
2515 struct brymen_bm52x_info { size_t ch_idx; };
2517 #ifdef HAVE_SERIAL_COMM
2518 SR_PRIV int sr_brymen_bm52x_packet_request(struct sr_serial_dev_inst *serial);
2519 SR_PRIV int sr_brymen_bm82x_packet_request(struct sr_serial_dev_inst *serial);
2521 SR_PRIV gboolean sr_brymen_bm52x_packet_valid(const uint8_t *buf);
2522 SR_PRIV gboolean sr_brymen_bm82x_packet_valid(const uint8_t *buf);
2523 /* BM520s and BM820s protocols are similar, the parse routine is shared. */
2524 SR_PRIV int sr_brymen_bm52x_parse(const uint8_t *buf, float *floatval,
2525 struct sr_datafeed_analog *analog, void *info);
2527 struct brymen_bm52x_state;
2529 SR_PRIV void *brymen_bm52x_state_init(void);
2530 SR_PRIV void brymen_bm52x_state_free(void *state);
2531 SR_PRIV int brymen_bm52x_config_get(void *state, uint32_t key, GVariant **data,
2532 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2533 SR_PRIV int brymen_bm52x_config_set(void *state, uint32_t key, GVariant *data,
2534 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2535 SR_PRIV int brymen_bm52x_config_list(void *state, uint32_t key, GVariant **data,
2536 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2537 SR_PRIV int brymen_bm52x_acquire_start(void *state,
2538 const struct sr_dev_inst *sdi,
2539 sr_receive_data_callback *cb, void **cb_data);
2541 /*--- dmm/bm85x.c -----------------------------------------------------------*/
2543 #define BRYMEN_BM85x_PACKET_SIZE_MIN 4
2545 struct brymen_bm85x_info { int dummy; };
2547 #ifdef HAVE_SERIAL_COMM
2548 SR_PRIV int brymen_bm85x_after_open(struct sr_serial_dev_inst *serial);
2549 SR_PRIV int brymen_bm85x_packet_request(struct sr_serial_dev_inst *serial);
2551 SR_PRIV gboolean brymen_bm85x_packet_valid(void *state,
2552 const uint8_t *buf, size_t len, size_t *pkt_len);
2553 SR_PRIV int brymen_bm85x_parse(void *state, const uint8_t *buf, size_t len,
2554 double *floatval, struct sr_datafeed_analog *analog, void *info);
2556 /*--- dmm/bm86x.c -----------------------------------------------------------*/
2558 #define BRYMEN_BM86X_PACKET_SIZE 24
2559 #define BRYMEN_BM86X_DISPLAY_COUNT 2
2561 struct brymen_bm86x_info { size_t ch_idx; };
2563 #ifdef HAVE_SERIAL_COMM
2564 SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial);
2566 SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf);
2567 SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *floatval,
2568 struct sr_datafeed_analog *analog, void *info);
2570 /*--- dmm/ut71x.c -----------------------------------------------------------*/
2572 #define UT71X_PACKET_SIZE 11
2575 gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
2576 gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
2577 gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
2578 gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
2581 SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
2582 SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
2583 struct sr_datafeed_analog *analog, void *info);
2585 /*--- dmm/vc870.c -----------------------------------------------------------*/
2587 #define VC870_PACKET_SIZE 23
2590 gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
2591 gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
2592 gboolean is_current, is_micro, is_milli, is_power;
2593 gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_rms_value;
2594 gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
2595 gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
2596 gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
2597 gboolean is_lo, is_hi, is_open2;
2599 gboolean is_frequency, is_dual_display, is_auto;
2602 SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
2603 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
2604 struct sr_datafeed_analog *analog, void *info);
2606 /*--- dmm/vc96.c ------------------------------------------------------------*/
2608 #define VC96_PACKET_SIZE 13
2612 gboolean is_ac, is_dc, is_resistance, is_diode, is_ampere, is_volt;
2613 gboolean is_ohm, is_micro, is_milli, is_kilo, is_mega, is_hfe;
2614 gboolean is_unitless;
2617 SR_PRIV gboolean sr_vc96_packet_valid(const uint8_t *buf);
2618 SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval,
2619 struct sr_datafeed_analog *analog, void *info);
2621 /*--- lcr/es51919.c ---------------------------------------------------------*/
2623 /* Acquisition details which apply to all supported serial-lcr devices. */
2624 struct lcr_parse_info {
2626 uint64_t output_freq;
2627 const char *circuit_model;
2630 #define ES51919_PACKET_SIZE 17
2631 #define ES51919_CHANNEL_COUNT 2
2632 #define ES51919_COMM_PARAM "9600/8n1/rts=1/dtr=1"
2634 SR_PRIV int es51919_config_get(uint32_t key, GVariant **data,
2635 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2636 SR_PRIV int es51919_config_set(uint32_t key, GVariant *data,
2637 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2638 SR_PRIV int es51919_config_list(uint32_t key, GVariant **data,
2639 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2640 SR_PRIV gboolean es51919_packet_valid(const uint8_t *pkt);
2641 SR_PRIV int es51919_packet_parse(const uint8_t *pkt, float *floatval,
2642 struct sr_datafeed_analog *analog, void *info);
2644 /*--- lcr/vc4080.c ----------------------------------------------------------*/
2646 /* Note: Also uses 'struct lcr_parse_info' from es51919 above. */
2648 #define VC4080_PACKET_SIZE 39
2649 #define VC4080_COMM_PARAM "1200/8n1"
2650 #define VC4080_WITH_DQ_CHANS 0 /* Enable separate D/Q channels? */
2652 enum vc4080_display {
2653 VC4080_DISPLAY_PRIMARY,
2654 VC4080_DISPLAY_SECONDARY,
2655 #if VC4080_WITH_DQ_CHANS
2656 VC4080_DISPLAY_D_VALUE,
2657 VC4080_DISPLAY_Q_VALUE,
2659 VC4080_CHANNEL_COUNT,
2662 extern SR_PRIV const char *vc4080_channel_formats[VC4080_CHANNEL_COUNT];
2664 SR_PRIV int vc4080_config_list(uint32_t key, GVariant **data,
2665 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2666 SR_PRIV int vc4080_packet_request(struct sr_serial_dev_inst *serial);
2667 SR_PRIV gboolean vc4080_packet_valid(const uint8_t *pkt);
2668 SR_PRIV int vc4080_packet_parse(const uint8_t *pkt, float *floatval,
2669 struct sr_datafeed_analog *analog, void *info);
2671 /*--- dmm/ut372.c -----------------------------------------------------------*/
2673 #define UT372_PACKET_SIZE 27
2679 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
2680 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
2681 struct sr_datafeed_analog *analog, void *info);
2683 /*--- dmm/asycii.c ----------------------------------------------------------*/
2685 #define ASYCII_PACKET_SIZE 16
2687 struct asycii_info {
2688 gboolean is_ac, is_dc, is_ac_and_dc;
2689 gboolean is_resistance, is_capacitance, is_diode, is_gain;
2690 gboolean is_frequency, is_duty_cycle, is_duty_pos, is_duty_neg;
2691 gboolean is_pulse_width, is_period_pos, is_period_neg;
2692 gboolean is_pulse_count, is_count_pos, is_count_neg;
2693 gboolean is_ampere, is_volt, is_volt_ampere, is_farad, is_ohm;
2694 gboolean is_hertz, is_percent, is_seconds, is_decibel;
2695 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2696 gboolean is_unitless;
2697 gboolean is_peak_min, is_peak_max;
2698 gboolean is_invalid;
2701 #ifdef HAVE_SERIAL_COMM
2702 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
2704 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);
2705 SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
2706 struct sr_datafeed_analog *analog, void *info);
2708 /*--- dmm/eev121gw.c --------------------------------------------------------*/
2710 #define EEV121GW_PACKET_SIZE 19
2712 enum eev121gw_display {
2713 EEV121GW_DISPLAY_MAIN,
2714 EEV121GW_DISPLAY_SUB,
2715 EEV121GW_DISPLAY_BAR,
2716 EEV121GW_DISPLAY_COUNT,
2719 struct eev121gw_info {
2720 /* Selected channel. */
2723 * Measured value, number and sign/overflow flags, scale factor
2724 * and significant digits.
2726 uint32_t uint_value;
2727 gboolean is_ofl, is_neg;
2729 /* Currently active mode (meter's function). */
2730 gboolean is_ac, is_dc, is_voltage, is_current, is_power, is_gain;
2731 gboolean is_resistance, is_capacitance, is_diode, is_temperature;
2732 gboolean is_continuity, is_frequency, is_period, is_duty_cycle;
2733 /* Quantities associated with mode/function. */
2734 gboolean is_ampere, is_volt, is_volt_ampere, is_dbm;
2735 gboolean is_ohm, is_farad, is_celsius, is_fahrenheit;
2736 gboolean is_hertz, is_seconds, is_percent, is_loop_current;
2737 gboolean is_unitless, is_logic;
2738 /* Other indicators. */
2739 gboolean is_min, is_max, is_avg, is_1ms_peak, is_rel, is_hold;
2740 gboolean is_low_pass, is_mem, is_bt, is_auto_range, is_test;
2741 gboolean is_auto_poweroff, is_low_batt;
2744 extern SR_PRIV const char *eev121gw_channel_formats[];
2745 SR_PRIV gboolean sr_eev121gw_packet_valid(const uint8_t *buf);
2746 SR_PRIV int sr_eev121gw_3displays_parse(const uint8_t *buf, float *floatval,
2747 struct sr_datafeed_analog *analog, void *info);
2749 /*--- scale/kern.c ----------------------------------------------------------*/
2752 gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
2753 gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
2754 gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
2758 SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
2759 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
2760 struct sr_datafeed_analog *analog, void *info);
2762 /*--- sw_limits.c -----------------------------------------------------------*/
2764 struct sr_sw_limits {
2765 uint64_t limit_samples;
2766 uint64_t limit_frames;
2767 uint64_t limit_msec;
2768 uint64_t samples_read;
2769 uint64_t frames_read;
2770 uint64_t start_time;
2773 SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits, uint32_t key,
2775 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
2777 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
2778 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
2779 SR_PRIV int sr_sw_limits_get_remain(const struct sr_sw_limits *limits,
2780 uint64_t *samples, uint64_t *frames, uint64_t *msecs,
2781 gboolean *exceeded);
2782 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
2783 uint64_t samples_read);
2784 SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
2785 uint64_t frames_read);
2786 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
2788 /*--- feed_queue.h ----------------------------------------------------------*/
2790 struct feed_queue_logic;
2791 struct feed_queue_analog;
2793 SR_API struct feed_queue_logic *feed_queue_logic_alloc(
2794 const struct sr_dev_inst *sdi,
2795 size_t sample_count, size_t unit_size);
2796 SR_API int feed_queue_logic_submit(struct feed_queue_logic *q,
2797 const uint8_t *data, size_t count);
2798 SR_API int feed_queue_logic_flush(struct feed_queue_logic *q);
2799 SR_API int feed_queue_logic_send_trigger(struct feed_queue_logic *q);
2800 SR_API void feed_queue_logic_free(struct feed_queue_logic *q);
2802 SR_API struct feed_queue_analog *feed_queue_analog_alloc(
2803 const struct sr_dev_inst *sdi,
2804 size_t sample_count, int digits, struct sr_channel *ch);
2805 SR_API int feed_queue_analog_mq_unit(struct feed_queue_analog *q,
2806 enum sr_mq mq, enum sr_mqflag mq_flag, enum sr_unit unit);
2807 SR_API int feed_queue_analog_scale_offset(struct feed_queue_analog *q,
2808 const struct sr_rational *scale, const struct sr_rational *offset);
2809 SR_API int feed_queue_analog_submit(struct feed_queue_analog *q,
2810 float data, size_t count);
2811 SR_API int feed_queue_analog_flush(struct feed_queue_analog *q);
2812 SR_API void feed_queue_analog_free(struct feed_queue_analog *q);