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 signed 8bit integer from raw memory, increment read position.
620 * @param[in, out] p Pointer into byte stream.
621 * @return Retrieved integer value, signed.
623 static inline int8_t read_i8_inc(const uint8_t **p)
636 * Read unsigned 16bit integer from raw memory (big endian format), increment read position.
637 * @param[in, out] p Pointer into byte stream.
638 * @return Retrieved integer value, unsigned.
640 static inline uint16_t read_u16be_inc(const uint8_t **p)
653 * Read unsigned 16bit integer from raw memory (little endian format), increment read position.
654 * @param[in, out] p Pointer into byte stream.
655 * @return Retrieved integer value, unsigned.
657 static inline uint16_t read_u16le_inc(const uint8_t **p)
670 * Read signed 16bit integer from raw memory (big endian format), increment read position.
671 * @param[in, out] p Pointer into byte stream.
672 * @return Retrieved integer value, signed.
674 static inline int16_t read_i16be_inc(const uint8_t **p)
687 * Read signed 16bit integer from raw memory (little endian format), increment read position.
688 * @param[in, out] p Pointer into byte stream.
689 * @return Retrieved integer value, signed.
691 static inline int16_t read_i16le_inc(const uint8_t **p)
704 * Read unsigned 24bit integer from raw memory (little endian format), increment read position.
705 * @param[in, out] p Pointer into byte stream.
706 * @return Retrieved integer value, unsigned.
708 static inline uint32_t read_u24le_inc(const uint8_t **p)
715 *p += 3 * sizeof(uint8_t);
721 * Read unsigned 32bit integer from raw memory (big endian format), increment read position.
722 * @param[in, out] p Pointer into byte stream.
723 * @return Retrieved integer value, unsigned.
725 static inline uint32_t read_u32be_inc(const uint8_t **p)
738 * Read unsigned 32bit integer from raw memory (little endian format), increment read position.
739 * @param[in, out] p Pointer into byte stream.
740 * @return Retrieved integer value, unsigned.
742 static inline uint32_t read_u32le_inc(const uint8_t **p)
755 * Read signed 32bit integer from raw memory (big endian format), increment read position.
756 * @param[in, out] p Pointer into byte stream.
757 * @return Retrieved integer value, signed.
759 static inline int32_t read_i32be_inc(const uint8_t **p)
772 * Read signed 32bit integer from raw memory (little endian format), increment read position.
773 * @param[in, out] p Pointer into byte stream.
774 * @return Retrieved integer value, signed.
776 static inline int32_t read_i32le_inc(const uint8_t **p)
789 * Read unsigned 64bit integer from raw memory (big endian format), increment read position.
790 * @param[in, out] p Pointer into byte stream.
791 * @return Retrieved integer value, unsigned.
793 static inline uint64_t read_u64be_inc(const uint8_t **p)
806 * Read unsigned 64bit integer from raw memory (little endian format), increment read position.
807 * @param[in, out] p Pointer into byte stream.
808 * @return Retrieved integer value, unsigned.
810 static inline uint64_t read_u64le_inc(const uint8_t **p)
823 * Read 32bit float from raw memory (big endian format), increment read position.
824 * @param[in, out] p Pointer into byte stream.
825 * @return Retrieved float value.
827 static inline float read_fltbe_inc(const uint8_t **p)
840 * Read 32bit float from raw memory (little endian format), increment read position.
841 * @param[in, out] p Pointer into byte stream.
842 * @return Retrieved float value.
844 static inline float read_fltle_inc(const uint8_t **p)
857 * Read 64bit float from raw memory (big endian format), increment read position.
858 * @param[in, out] p Pointer into byte stream.
859 * @return Retrieved float value.
861 static inline double read_dblbe_inc(const uint8_t **p)
874 * Read 64bit float from raw memory (little endian format), increment read position.
875 * @param[in, out] p Pointer into byte stream.
876 * @return Retrieved float value.
878 static inline double read_dblle_inc(const uint8_t **p)
891 * Write unsigned 8bit integer to raw memory, increment write position.
892 * @param[in, out] p Pointer into byte stream.
893 * @param[in] x Value to write.
895 static inline void write_u8_inc(uint8_t **p, uint8_t x)
904 * Write unsigned 16bit big endian integer to raw memory, increment write position.
905 * @param[in, out] p Pointer into byte stream.
906 * @param[in] x Value to write.
908 static inline void write_u16be_inc(uint8_t **p, uint16_t x)
917 * Write unsigned 16bit little endian integer to raw memory, increment write position.
918 * @param[in, out] p Pointer into byte stream.
919 * @param[in] x Value to write.
921 static inline void write_u16le_inc(uint8_t **p, uint16_t x)
930 * Write unsigned 24bit liggle endian integer to raw memory, increment write position.
931 * @param[in, out] p Pointer into byte stream.
932 * @param[in] x Value to write.
934 static inline void write_u24le_inc(uint8_t **p, uint32_t x)
939 *p += 3 * sizeof(uint8_t);
943 * Write unsigned 32bit big endian integer to raw memory, increment write position.
944 * @param[in, out] p Pointer into byte stream.
945 * @param[in] x Value to write.
947 static inline void write_u32be_inc(uint8_t **p, uint32_t x)
956 * Write unsigned 32bit little endian 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_u32le_inc(uint8_t **p, uint32_t x)
969 * Write unsigned 40bit little 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_u40le_inc(uint8_t **p, uint64_t x)
978 *p += 5 * sizeof(uint8_t);
982 * Write unsigned 48bit 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_u48le_inc(uint8_t **p, uint64_t x)
991 *p += 48 / 8 * sizeof(uint8_t);
995 * Write unsigned 64bit little 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_u64le_inc(uint8_t **p, uint64_t x)
1008 * Write single precision little endian float 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_fltle_inc(uint8_t **p, float x)
1021 * Write double precision little endian float 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_dblle_inc(uint8_t **p, double x)
1033 /* Portability fixes for FreeBSD. */
1035 #define LIBUSB_CLASS_APPLICATION 0xfe
1036 #define libusb_has_capability(x) 0
1037 #define libusb_handle_events_timeout_completed(ctx, tv, c) \
1038 libusb_handle_events_timeout(ctx, tv)
1042 * Convenience for FTDI library version dependency.
1043 * - Version 1.5 introduced ftdi_tciflush(), ftdi_tcoflush(), and
1044 * ftdi_tcioflush() all within the same commit, and deprecated
1045 * ftdi_usb_purge_buffers() which suffered from inverse semantics.
1046 * The API is drop-in compatible (arguments count and data types are
1047 * identical). The libsigrok source code always flushes RX and TX at
1048 * the same time, never individually.
1050 #if defined HAVE_FTDI_TCIOFLUSH && HAVE_FTDI_TCIOFLUSH
1051 # define PURGE_FTDI_BOTH ftdi_tcioflush
1053 # define PURGE_FTDI_BOTH ftdi_usb_purge_buffers
1056 /* Static definitions of structs ending with an all-zero entry are a
1057 * problem when compiling with -Wmissing-field-initializers: GCC
1058 * suppresses the warning only with { 0 }, clang wants { } */
1060 #define ALL_ZERO { }
1062 #define ALL_ZERO { 0 }
1066 #define SR_DRIVER_LIST_SECTION "__DATA,__sr_driver_list"
1068 #define SR_DRIVER_LIST_SECTION "__sr_driver_list"
1071 #if !defined SR_DRIVER_LIST_NOREORDER && defined __has_attribute
1072 #if __has_attribute(no_reorder)
1073 #define SR_DRIVER_LIST_NOREORDER __attribute__((no_reorder))
1076 #if !defined SR_DRIVER_LIST_NOREORDER
1077 #define SR_DRIVER_LIST_NOREORDER /* EMPTY */
1081 * Register a list of hardware drivers.
1083 * This macro can be used to register multiple hardware drivers to the library.
1084 * This is useful when a driver supports multiple similar but slightly
1085 * different devices that require different sr_dev_driver struct definitions.
1087 * For registering only a single driver see SR_REGISTER_DEV_DRIVER().
1091 * #define MY_DRIVER(_name) \
1092 * &(struct sr_dev_driver){ \
1097 * SR_REGISTER_DEV_DRIVER_LIST(my_driver_infos,
1098 * MY_DRIVER("driver 1"),
1099 * MY_DRIVER("driver 2"),
1104 * @param name Name to use for the driver list identifier.
1105 * @param ... Comma separated list of pointers to sr_dev_driver structs.
1107 #define SR_REGISTER_DEV_DRIVER_LIST(name, ...) \
1108 static const struct sr_dev_driver *name[] \
1109 SR_DRIVER_LIST_NOREORDER \
1110 __attribute__((section (SR_DRIVER_LIST_SECTION), used, \
1111 aligned(sizeof(struct sr_dev_driver *)))) \
1117 * Register a hardware driver.
1119 * This macro is used to register a hardware driver with the library. It has
1120 * to be used in order to make the driver accessible to applications using the
1123 * The macro invocation should be placed directly under the struct
1124 * sr_dev_driver definition.
1128 * static struct sr_dev_driver driver_info = {
1132 * SR_REGISTER_DEV_DRIVER(driver_info);
1135 * @param name Identifier name of sr_dev_driver struct to register.
1137 #define SR_REGISTER_DEV_DRIVER(name) \
1138 SR_REGISTER_DEV_DRIVER_LIST(name##_list, &name);
1140 SR_API void sr_drivers_init(struct sr_context *context);
1143 struct sr_dev_driver **driver_list;
1144 #ifdef HAVE_LIBUSB_1_0
1145 libusb_context *libusb_ctx;
1147 sr_resource_open_callback resource_open_cb;
1148 sr_resource_close_callback resource_close_cb;
1149 sr_resource_read_callback resource_read_cb;
1150 void *resource_cb_data;
1153 /** Input module metadata keys. */
1154 enum sr_input_meta_keys {
1155 /** The input filename, if there is one. */
1156 SR_INPUT_META_FILENAME = 0x01,
1157 /** The input file's size in bytes. */
1158 SR_INPUT_META_FILESIZE = 0x02,
1159 /** The first 128 bytes of the file, provided as a GString. */
1160 SR_INPUT_META_HEADER = 0x04,
1162 /** The module cannot identify a file without this metadata. */
1163 SR_INPUT_META_REQUIRED = 0x80,
1166 /** Input (file) module struct. */
1169 * A pointer to this input module's 'struct sr_input_module'.
1171 const struct sr_input_module *module;
1173 struct sr_dev_inst *sdi;
1178 /** Input (file) module driver. */
1179 struct sr_input_module {
1181 * A unique ID for this input module, suitable for use in command-line
1182 * clients, [a-z0-9-]. Must not be NULL.
1187 * A unique name for this input module, suitable for use in GUI
1188 * clients, can contain UTF-8. Must not be NULL.
1193 * A short description of the input module. Must not be NULL.
1195 * This can be displayed by frontends, e.g. when selecting the input
1196 * module for saving a file.
1201 * A NULL terminated array of strings containing a list of file name
1202 * extensions typical for the input file format, or NULL if there is
1203 * no typical extension for this file format.
1205 const char *const *exts;
1208 * Zero-terminated list of metadata items the module needs to be able
1209 * to identify an input stream. Can be all-zero, if the module cannot
1210 * identify streams at all, i.e. has to be forced into use.
1212 * Each item is one of:
1213 * SR_INPUT_META_FILENAME
1214 * SR_INPUT_META_FILESIZE
1215 * SR_INPUT_META_HEADER
1217 * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
1218 * identify a stream without the given metadata.
1220 const uint8_t metadata[8];
1223 * Returns a NULL-terminated list of options this module can take.
1224 * Can be NULL, if the module has no options.
1226 const struct sr_option *(*options) (void);
1229 * Check if this input module can load and parse the specified stream.
1231 * @param[in] metadata Metadata the module can use to identify the stream.
1232 * @param[out] confidence "Strength" of the detection.
1233 * Specialized handlers can take precedence over generic/basic support.
1235 * @retval SR_OK This module knows the format.
1236 * @retval SR_ERR_NA There wasn't enough data for this module to
1237 * positively identify the format.
1238 * @retval SR_ERR_DATA This module knows the format, but cannot handle
1239 * it. This means the stream is either corrupt, or indicates a
1240 * feature that the module does not support.
1241 * @retval SR_ERR This module does not know the format.
1243 * Lower numeric values of 'confidence' mean that the input module
1244 * stronger believes in its capability to handle this specific format.
1245 * This way, multiple input modules can claim support for a format,
1246 * and the application can pick the best match, or try fallbacks
1247 * in case of errors. This approach also copes with formats that
1248 * are unreliable to detect in the absence of magic signatures.
1250 int (*format_match) (GHashTable *metadata, unsigned int *confidence);
1253 * Initialize the input module.
1255 * @retval SR_OK Success
1256 * @retval other Negative error code.
1258 int (*init) (struct sr_input *in, GHashTable *options);
1261 * Send data to the specified input instance.
1263 * When an input module instance is created with sr_input_new(), this
1264 * function is used to feed data to the instance.
1266 * As enough data gets fed into this function to completely populate
1267 * the device instance associated with this input instance, this is
1268 * guaranteed to return the moment it's ready. This gives the caller
1269 * the chance to examine the device instance, attach session callbacks
1272 * @retval SR_OK Success
1273 * @retval other Negative error code.
1275 int (*receive) (struct sr_input *in, GString *buf);
1278 * Signal the input module no more data will come.
1280 * This will cause the module to process any data it may have buffered.
1281 * The SR_DF_END packet will also typically be sent at this time.
1283 int (*end) (struct sr_input *in);
1286 * Reset the input module's input handling structures.
1288 * Causes the input module to reset its internal state so that we can
1289 * re-send the input data from the beginning without having to
1290 * re-create the entire input module.
1292 * @retval SR_OK Success.
1293 * @retval other Negative error code.
1295 int (*reset) (struct sr_input *in);
1298 * This function is called after the caller is finished using
1299 * the input module, and can be used to free any internal
1300 * resources the module may keep.
1302 * This function is optional.
1304 * @retval SR_OK Success
1305 * @retval other Negative error code.
1307 void (*cleanup) (struct sr_input *in);
1310 /** Output module instance. */
1312 /** A pointer to this output's module. */
1313 const struct sr_output_module *module;
1316 * The device for which this output module is creating output. This
1317 * can be used by the module to find out channel names and numbers.
1319 const struct sr_dev_inst *sdi;
1322 * The name of the file that the data should be written to.
1324 const char *filename;
1327 * A generic pointer which can be used by the module to keep internal
1328 * state between calls into its callback functions.
1330 * For example, the module might store a pointer to a chunk of output
1331 * there, and only flush it when it reaches a certain size.
1336 /** Output module driver. */
1337 struct sr_output_module {
1339 * A unique ID for this output module, suitable for use in command-line
1340 * clients, [a-z0-9-]. Must not be NULL.
1345 * A unique name for this output module, suitable for use in GUI
1346 * clients, can contain UTF-8. Must not be NULL.
1351 * A short description of the output module. Must not be NULL.
1353 * This can be displayed by frontends, e.g. when selecting the output
1354 * module for saving a file.
1359 * A NULL terminated array of strings containing a list of file name
1360 * extensions typical for the input file format, or NULL if there is
1361 * no typical extension for this file format.
1363 const char *const *exts;
1366 * Bitfield containing flags that describe certain properties
1367 * this output module may or may not have.
1368 * @see sr_output_flags
1370 const uint64_t flags;
1373 * Returns a NULL-terminated list of options this module can take.
1374 * Can be NULL, if the module has no options.
1376 const struct sr_option *(*options) (void);
1379 * This function is called once, at the beginning of an output stream.
1381 * The device struct will be available in the output struct passed in,
1382 * as well as the param field -- which may be NULL or an empty string,
1383 * if no parameter was passed.
1385 * The module can use this to initialize itself, create a struct for
1386 * keeping state and storing it in the <code>internal</code> field.
1388 * @param o Pointer to the respective 'struct sr_output'.
1390 * @retval SR_OK Success
1391 * @retval other Negative error code.
1393 int (*init) (struct sr_output *o, GHashTable *options);
1396 * This function is passed a copy of every packet in the data feed.
1397 * Any output generated by the output module in response to the
1398 * packet should be returned in a newly allocated GString
1399 * <code>out</code>, which will be freed by the caller.
1401 * Packets not of interest to the output module can just be ignored,
1402 * and the <code>out</code> parameter set to NULL.
1404 * @param o Pointer to the respective 'struct sr_output'.
1405 * @param sdi The device instance that generated the packet.
1406 * @param packet The complete packet.
1407 * @param out A pointer where a GString * should be stored if
1408 * the module generates output, or NULL if not.
1410 * @retval SR_OK Success
1411 * @retval other Negative error code.
1413 int (*receive) (const struct sr_output *o,
1414 const struct sr_datafeed_packet *packet, GString **out);
1417 * This function is called after the caller is finished using
1418 * the output module, and can be used to free any internal
1419 * resources the module may keep.
1421 * @retval SR_OK Success
1422 * @retval other Negative error code.
1424 int (*cleanup) (struct sr_output *o);
1427 /** Transform module instance. */
1428 struct sr_transform {
1429 /** A pointer to this transform's module. */
1430 const struct sr_transform_module *module;
1433 * The device for which this transform module is used. This
1434 * can be used by the module to find out channel names and numbers.
1436 const struct sr_dev_inst *sdi;
1439 * A generic pointer which can be used by the module to keep internal
1440 * state between calls into its callback functions.
1445 struct sr_transform_module {
1447 * A unique ID for this transform module, suitable for use in
1448 * command-line clients, [a-z0-9-]. Must not be NULL.
1453 * A unique name for this transform module, suitable for use in GUI
1454 * clients, can contain UTF-8. Must not be NULL.
1459 * A short description of the transform module. Must not be NULL.
1461 * This can be displayed by frontends, e.g. when selecting
1462 * which transform module(s) to add.
1467 * Returns a NULL-terminated list of options this transform module
1468 * can take. Can be NULL, if the transform module has no options.
1470 const struct sr_option *(*options) (void);
1473 * This function is called once, at the beginning of a stream.
1475 * @param t Pointer to the respective 'struct sr_transform'.
1476 * @param options Hash table of options for this transform module.
1477 * Can be NULL if no options are to be used.
1479 * @retval SR_OK Success
1480 * @retval other Negative error code.
1482 int (*init) (struct sr_transform *t, GHashTable *options);
1485 * This function is passed a pointer to every packet in the data feed.
1487 * It can either return (in packet_out) a pointer to another packet
1488 * (possibly the exact same packet it got as input), or NULL.
1490 * @param t Pointer to the respective 'struct sr_transform'.
1491 * @param packet_in Pointer to a datafeed packet.
1492 * @param packet_out Pointer to the resulting datafeed packet after
1493 * this function was run. If NULL, the transform
1494 * module intentionally didn't output a new packet.
1496 * @retval SR_OK Success
1497 * @retval other Negative error code.
1499 int (*receive) (const struct sr_transform *t,
1500 struct sr_datafeed_packet *packet_in,
1501 struct sr_datafeed_packet **packet_out);
1504 * This function is called after the caller is finished using
1505 * the transform module, and can be used to free any internal
1506 * resources the module may keep.
1508 * @retval SR_OK Success
1509 * @retval other Negative error code.
1511 int (*cleanup) (struct sr_transform *t);
1514 #ifdef HAVE_LIBUSB_1_0
1515 /** USB device instance */
1516 struct sr_usb_dev_inst {
1519 /** Device address on USB bus */
1521 /** libusb device handle */
1522 struct libusb_device_handle *devhdl;
1526 struct sr_serial_dev_inst;
1527 #ifdef HAVE_SERIAL_COMM
1528 struct ser_lib_functions;
1529 struct ser_hid_chip_functions;
1531 typedef void (*serial_rx_chunk_callback)(struct sr_serial_dev_inst *serial,
1532 void *cb_data, const void *buf, size_t count);
1533 struct sr_serial_dev_inst {
1534 /** Port name, e.g. '/dev/tty42'. */
1536 /** Comm params for serial_set_paramstr(). */
1538 struct ser_lib_functions *lib_funcs;
1545 GString *rcv_buffer;
1546 serial_rx_chunk_callback rx_chunk_cb_func;
1547 void *rx_chunk_cb_data;
1548 #ifdef HAVE_LIBSERIALPORT
1549 /** libserialport port handle */
1550 struct sp_port *sp_data;
1552 #ifdef HAVE_LIBHIDAPI
1553 enum ser_hid_chip_t {
1554 SER_HID_CHIP_UNKNOWN, /**!< place holder */
1555 SER_HID_CHIP_BTC_BU86X, /**!< Brymen BU86x */
1556 SER_HID_CHIP_SIL_CP2110, /**!< SiLabs CP2110 */
1557 SER_HID_CHIP_VICTOR_DMM, /**!< Victor 70/86 DMM cable */
1558 SER_HID_CHIP_WCH_CH9325, /**!< WCH CH9325 */
1559 SER_HID_CHIP_LAST, /**!< sentinel */
1561 struct ser_hid_chip_functions *hid_chip_funcs;
1564 const char *hid_path;
1565 hid_device *hid_dev;
1566 GSList *hid_source_args;
1568 #ifdef HAVE_BLUETOOTH
1569 enum ser_bt_conn_t {
1570 SER_BT_CONN_UNKNOWN, /**!< place holder */
1571 SER_BT_CONN_RFCOMM, /**!< BT classic, RFCOMM channel */
1572 SER_BT_CONN_BLE122, /**!< BLE, BLE122 module, indications */
1573 SER_BT_CONN_NRF51, /**!< BLE, Nordic nRF51, notifications */
1574 SER_BT_CONN_CC254x, /**!< BLE, TI CC254x, notifications */
1575 SER_BT_CONN_MAX, /**!< sentinel */
1577 char *bt_addr_local;
1578 char *bt_addr_remote;
1579 size_t bt_rfcomm_channel;
1580 uint16_t bt_notify_handle_read;
1581 uint16_t bt_notify_handle_write;
1582 uint16_t bt_notify_handle_cccd;
1583 uint16_t bt_notify_value_cccd;
1584 struct sr_bt_desc *bt_desc;
1585 GSList *bt_source_args;
1590 struct sr_usbtmc_dev_inst {
1595 /* Private driver context. */
1596 struct drv_context {
1597 /** sigrok context */
1598 struct sr_context *sr_ctx;
1602 /*--- log.c -----------------------------------------------------------------*/
1604 #if defined(_WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
1606 * On MinGW, we need to specify the gnu_printf format flavor or GCC
1607 * will assume non-standard Microsoft printf syntax.
1609 SR_PRIV int sr_log(int loglevel, const char *format, ...)
1610 __attribute__((__format__ (__gnu_printf__, 2, 3)));
1612 SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
1615 /* Message logging helpers with subsystem-specific prefix string. */
1616 #define sr_spew(...) sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
1617 #define sr_dbg(...) sr_log(SR_LOG_DBG, LOG_PREFIX ": " __VA_ARGS__)
1618 #define sr_info(...) sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
1619 #define sr_warn(...) sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
1620 #define sr_err(...) sr_log(SR_LOG_ERR, LOG_PREFIX ": " __VA_ARGS__)
1622 /*--- device.c --------------------------------------------------------------*/
1624 /** Scan options supported by a driver. */
1625 #define SR_CONF_SCAN_OPTIONS 0x7FFF0000
1627 /** Device options for a particular device. */
1628 #define SR_CONF_DEVICE_OPTIONS 0x7FFF0001
1630 /** Mask for separating config keys from capabilities. */
1631 #define SR_CONF_MASK 0x1fffffff
1633 /** Values for the changes argument of sr_dev_driver.config_channel_set. */
1635 /** The enabled state of the channel has been changed. */
1636 SR_CHANNEL_SET_ENABLED = 1 << 0,
1639 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
1640 int index, int type, gboolean enabled, const char *name);
1641 SR_PRIV void sr_channel_free(struct sr_channel *ch);
1642 SR_PRIV void sr_channel_free_cb(void *p);
1643 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
1644 struct sr_channel *cur_channel);
1645 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2);
1646 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2);
1648 SR_PRIV struct sr_channel_group *sr_channel_group_new(struct sr_dev_inst *sdi,
1649 const char *name, void *priv);
1650 SR_PRIV void sr_channel_group_free(struct sr_channel_group *cg);
1651 SR_PRIV void sr_channel_group_free_cb(void *cg);
1653 /** Device instance data */
1654 struct sr_dev_inst {
1655 /** Device driver. */
1656 struct sr_dev_driver *driver;
1657 /** Device instance status. SR_ST_NOT_FOUND, etc. */
1659 /** Device instance type. SR_INST_USB, etc. */
1661 /** Device vendor. */
1663 /** Device model. */
1665 /** Device version. */
1667 /** Serial number. */
1669 /** Connection string to uniquely identify devices. */
1670 char *connection_id;
1671 /** List of channels. */
1673 /** List of sr_channel_group structs */
1674 GSList *channel_groups;
1675 /** Device instance connection data (used?) */
1677 /** Device instance private data (used?) */
1679 /** Session to which this device is currently assigned. */
1680 struct sr_session *session;
1683 /* Generic device instances */
1684 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
1686 #ifdef HAVE_LIBUSB_1_0
1687 /* USB-specific instances */
1688 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
1689 uint8_t address, struct libusb_device_handle *hdl);
1690 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
1691 SR_PRIV void sr_usb_dev_inst_free_cb(gpointer p); /* Glib wrapper. */
1694 #ifdef HAVE_SERIAL_COMM
1695 #ifndef HAVE_LIBSERIALPORT
1697 * Some identifiers which initially got provided by libserialport are
1698 * used internally within the libsigrok serial layer's implementation,
1699 * while libserialport no longer is the exclusive provider of serial
1700 * communication support. Declare the identifiers here so they remain
1701 * available across all build configurations.
1708 SP_PARITY_SPACE = 4,
1711 enum libsp_flowcontrol {
1712 SP_FLOWCONTROL_NONE = 0,
1713 SP_FLOWCONTROL_XONXOFF = 1,
1714 SP_FLOWCONTROL_RTSCTS = 2,
1715 SP_FLOWCONTROL_DTRDSR = 3,
1719 /* Serial-specific instances */
1720 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
1721 const char *serialcomm);
1722 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
1725 /* USBTMC-specific instances */
1726 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
1727 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
1729 /*--- hwdriver.c ------------------------------------------------------------*/
1731 SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
1732 SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
1733 SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
1734 SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
1735 SR_PRIV void sr_config_free(struct sr_config *src);
1736 SR_PRIV int sr_dev_acquisition_start(struct sr_dev_inst *sdi);
1737 SR_PRIV int sr_dev_acquisition_stop(struct sr_dev_inst *sdi);
1739 /*--- session.c -------------------------------------------------------------*/
1742 /** Context this session exists in. */
1743 struct sr_context *ctx;
1744 /** List of struct sr_dev_inst pointers. */
1746 /** List of struct sr_dev_inst pointers owned by this session. */
1748 /** List of struct datafeed_callback pointers. */
1749 GSList *datafeed_callbacks;
1751 struct sr_trigger *trigger;
1753 /** Callback to invoke on session stop. */
1754 sr_session_stopped_callback stopped_callback;
1755 /** User data to be passed to the session stop callback. */
1756 void *stopped_cb_data;
1758 /** Mutex protecting the main context pointer. */
1760 /** Context of the session main loop. */
1761 GMainContext *main_context;
1763 /** Registered event sources for this session. */
1764 GHashTable *event_sources;
1765 /** Session main loop. */
1766 GMainLoop *main_loop;
1767 /** ID of idle source for dispatching the session stop notification. */
1768 unsigned int stop_check_id;
1769 /** Whether the session has been started. */
1773 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
1774 void *key, GSource *source);
1775 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
1777 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1778 void *key, GSource *source);
1779 SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1780 void *key, gintptr fd, int events, int timeout,
1781 sr_receive_data_callback cb, void *cb_data);
1783 SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
1784 int events, int timeout, sr_receive_data_callback cb, void *cb_data);
1785 SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
1786 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1788 SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
1789 GIOChannel *channel, int events, int timeout,
1790 sr_receive_data_callback cb, void *cb_data);
1791 SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
1792 SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
1794 SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
1795 GIOChannel *channel);
1797 SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi,
1798 uint32_t key, GVariant *var);
1799 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
1800 const struct sr_datafeed_packet *packet);
1801 SR_PRIV int sr_sessionfile_check(const char *filename);
1802 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename,
1803 struct sr_session **session);
1805 /*--- session_file.c --------------------------------------------------------*/
1807 #if !HAVE_ZIP_DISCARD
1808 /* Replace zip_discard() if not available. */
1809 #define zip_discard(zip) sr_zip_discard(zip)
1810 SR_PRIV void sr_zip_discard(struct zip *archive);
1813 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
1814 const struct zip_stat *entry);
1816 /*--- analog.c --------------------------------------------------------------*/
1818 SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
1819 struct sr_analog_encoding *encoding,
1820 struct sr_analog_meaning *meaning,
1821 struct sr_analog_spec *spec,
1824 /*--- std.c -----------------------------------------------------------------*/
1826 typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
1827 typedef void (*std_dev_clear_callback)(void *priv);
1829 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx);
1830 SR_PRIV int std_cleanup(const struct sr_dev_driver *di);
1831 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
1832 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
1833 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
1834 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
1835 #ifdef HAVE_SERIAL_COMM
1836 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
1837 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
1839 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi);
1840 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi);
1841 SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi);
1842 SR_PRIV int std_session_send_df_frame_begin(const struct sr_dev_inst *sdi);
1843 SR_PRIV int std_session_send_df_frame_end(const struct sr_dev_inst *sdi);
1844 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
1845 std_dev_clear_callback clear_private);
1846 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver);
1847 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di);
1848 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
1849 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices);
1851 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
1852 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
1853 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
1854 size_t drvsize, const uint32_t devopts[], size_t devsize);
1856 extern SR_PRIV const uint32_t NO_OPTS[1];
1858 #define STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts) \
1859 std_opts_config_list(key, data, sdi, cg, ARRAY_AND_SIZE(scanopts), \
1860 ARRAY_AND_SIZE(drvopts), ARRAY_AND_SIZE(devopts))
1862 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n);
1863 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n);
1864 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n);
1865 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n);
1866 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step);
1867 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]);
1868 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double dmin, const double dmax, const double dstep);
1870 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high);
1871 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high);
1873 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n);
1874 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n);
1875 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n);
1876 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n);
1878 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n);
1880 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n);
1881 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n);
1882 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n);
1884 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n);
1885 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n);
1887 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n);
1888 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
1889 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
1891 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
1893 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
1894 int baudrate, int bits, int parity, int stopbits,
1895 int flowcontrol, int rts, int dtr);
1896 SR_PRIV int std_dummy_set_handshake(struct sr_serial_dev_inst *serial,
1899 /*--- resource.c ------------------------------------------------------------*/
1901 SR_PRIV int64_t sr_file_get_size(FILE *file);
1903 SR_PRIV int sr_resource_open(struct sr_context *ctx,
1904 struct sr_resource *res, int type, const char *name)
1905 G_GNUC_WARN_UNUSED_RESULT;
1906 SR_PRIV int sr_resource_close(struct sr_context *ctx,
1907 struct sr_resource *res);
1908 SR_PRIV gssize sr_resource_read(struct sr_context *ctx,
1909 const struct sr_resource *res, void *buf, size_t count)
1910 G_GNUC_WARN_UNUSED_RESULT;
1911 SR_PRIV void *sr_resource_load(struct sr_context *ctx, int type,
1912 const char *name, size_t *size, size_t max_size)
1913 G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
1915 /*--- strutil.c -------------------------------------------------------------*/
1917 SR_PRIV int sr_atol(const char *str, long *ret);
1918 SR_PRIV int sr_atol_base(const char *str, long *ret, char **end, int base);
1919 SR_PRIV int sr_atoul_base(const char *str, unsigned long *ret, char **end, int base);
1920 SR_PRIV int sr_atoi(const char *str, int *ret);
1921 SR_PRIV int sr_atod(const char *str, double *ret);
1922 SR_PRIV int sr_atof(const char *str, float *ret);
1923 SR_PRIV int sr_atod_ascii(const char *str, double *ret);
1924 SR_PRIV int sr_atod_ascii_digits(const char *str, double *ret, int *digits);
1925 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
1927 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
1928 SR_PRIV void sr_hexdump_free(GString *s);
1930 /*--- soft-trigger.c --------------------------------------------------------*/
1932 struct soft_trigger_logic {
1933 const struct sr_dev_inst *sdi;
1934 const struct sr_trigger *trigger;
1938 uint8_t *prev_sample;
1939 uint8_t *pre_trigger_buffer;
1940 uint8_t *pre_trigger_head;
1941 int pre_trigger_size;
1942 int pre_trigger_fill;
1945 SR_PRIV int logic_channel_unitsize(GSList *channels);
1946 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
1947 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
1948 int pre_trigger_samples);
1949 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
1950 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
1951 int len, int *pre_trigger_samples);
1953 /*--- serial.c --------------------------------------------------------------*/
1955 #ifdef HAVE_SERIAL_COMM
1961 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
1962 typedef int (*packet_valid_len_callback)(void *st,
1963 const uint8_t *p, size_t l, size_t *pl);
1965 typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
1967 typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
1969 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
1970 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
1971 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
1972 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
1973 SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
1974 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
1975 const void *buf, size_t count, unsigned int timeout_ms);
1976 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
1977 const void *buf, size_t count);
1978 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
1979 size_t count, unsigned int timeout_ms);
1980 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
1982 SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
1983 serial_rx_chunk_callback cb, void *cb_data);
1984 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
1985 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
1986 SR_PRIV int serial_set_handshake(struct sr_serial_dev_inst *serial,
1988 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
1989 const char *paramstr);
1990 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
1991 int *buflen, gint64 timeout_ms);
1992 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
1993 uint8_t *buf, size_t *buflen,
1994 size_t packet_size, packet_valid_callback is_valid,
1995 packet_valid_len_callback is_valid_len, size_t *return_size,
1996 uint64_t timeout_ms);
1997 SR_PRIV int serial_source_add(struct sr_session *session,
1998 struct sr_serial_dev_inst *serial, int events, int timeout,
1999 sr_receive_data_callback cb, void *cb_data);
2000 SR_PRIV int serial_source_remove(struct sr_session *session,
2001 struct sr_serial_dev_inst *serial);
2002 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
2003 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
2005 SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial);
2006 SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial);
2007 SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
2008 const uint8_t *data, size_t len);
2009 SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
2010 uint8_t *data, size_t len);
2012 struct ser_lib_functions {
2013 int (*open)(struct sr_serial_dev_inst *serial, int flags);
2014 int (*close)(struct sr_serial_dev_inst *serial);
2015 int (*flush)(struct sr_serial_dev_inst *serial);
2016 int (*drain)(struct sr_serial_dev_inst *serial);
2017 int (*write)(struct sr_serial_dev_inst *serial,
2018 const void *buf, size_t count,
2019 int nonblocking, unsigned int timeout_ms);
2020 int (*read)(struct sr_serial_dev_inst *serial,
2021 void *buf, size_t count,
2022 int nonblocking, unsigned int timeout_ms);
2023 int (*set_params)(struct sr_serial_dev_inst *serial,
2024 int baudrate, int bits, int parity, int stopbits,
2025 int flowcontrol, int rts, int dtr);
2026 int (*set_handshake)(struct sr_serial_dev_inst *serial,
2028 int (*setup_source_add)(struct sr_session *session,
2029 struct sr_serial_dev_inst *serial,
2030 int events, int timeout,
2031 sr_receive_data_callback cb, void *cb_data);
2032 int (*setup_source_remove)(struct sr_session *session,
2033 struct sr_serial_dev_inst *serial);
2034 GSList *(*list)(GSList *list, sr_ser_list_append_t append);
2035 GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
2036 uint16_t vendor_id, uint16_t product_id);
2037 int (*get_frame_format)(struct sr_serial_dev_inst *serial,
2038 int *baud, int *bits);
2039 size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
2041 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
2042 SR_PRIV int ser_name_is_hid(struct sr_serial_dev_inst *serial);
2043 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_hid;
2044 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial);
2045 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt;
2047 #ifdef HAVE_LIBHIDAPI
2048 struct vid_pid_item {
2052 struct ser_hid_chip_functions {
2053 const char *chipname;
2054 const char *chipdesc;
2055 const struct vid_pid_item *vid_pid_items;
2056 const int max_bytes_per_request;
2057 int (*set_params)(struct sr_serial_dev_inst *serial,
2058 int baudrate, int bits, int parity, int stopbits,
2059 int flowcontrol, int rts, int dtr);
2060 int (*read_bytes)(struct sr_serial_dev_inst *serial,
2061 uint8_t *data, int space, unsigned int timeout);
2062 int (*write_bytes)(struct sr_serial_dev_inst *serial,
2063 const uint8_t *data, int space);
2064 int (*flush)(struct sr_serial_dev_inst *serial);
2065 int (*drain)(struct sr_serial_dev_inst *serial);
2067 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x;
2068 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325;
2069 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110;
2070 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor;
2071 SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid);
2075 SR_PRIV int sr_serial_extract_options(GSList *options,
2076 const char **serial_device, const char **serial_options);
2078 /*--- bt/ API ---------------------------------------------------------------*/
2080 #ifdef HAVE_BLUETOOTH
2081 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx);
2084 typedef void (*sr_bt_scan_cb)(void *cb_data, const char *addr, const char *name);
2085 typedef int (*sr_bt_data_cb)(void *cb_data, uint8_t *data, size_t dlen);
2087 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void);
2088 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc);
2090 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
2091 sr_bt_scan_cb cb, void *cb_data);
2092 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
2093 sr_bt_data_cb cb, void *cb_data);
2094 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr);
2095 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr);
2096 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel);
2097 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
2098 uint16_t read_handle, uint16_t write_handle,
2099 uint16_t cccd_handle, uint16_t cccd_value);
2101 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration);
2102 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration);
2104 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc);
2105 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc);
2106 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc);
2108 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc,
2109 void *data, size_t len);
2110 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
2111 const void *data, size_t len);
2113 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc);
2114 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc);
2117 /*--- ezusb.c ---------------------------------------------------------------*/
2119 #ifdef HAVE_LIBUSB_1_0
2120 SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
2121 SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
2123 SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
2124 int configuration, const char *name);
2127 /*--- usb.c -----------------------------------------------------------------*/
2129 SR_PRIV int sr_usb_split_conn(const char *conn,
2130 uint16_t *vid, uint16_t *pid, uint8_t *bus, uint8_t *addr);
2131 #ifdef HAVE_LIBUSB_1_0
2132 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
2133 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
2134 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
2135 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
2136 int timeout, sr_receive_data_callback cb, void *cb_data);
2137 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
2138 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
2139 SR_PRIV gboolean usb_match_manuf_prod(libusb_device *dev,
2140 const char *manufacturer, const char *product);
2143 /*--- binary_helpers.c ------------------------------------------------------*/
2145 /** Binary value type */
2146 enum binary_value_type {
2150 BVT_BE_UINT8 = BVT_UINT8,
2151 BVT_LE_UINT8 = BVT_UINT8,
2164 /** Binary value specification */
2165 struct binary_value_spec {
2166 /** Offset into binary blob */
2168 /** Data type to decode */
2169 enum binary_value_type type;
2170 /** Scale factor to get native units */
2174 /** Binary channel definition */
2175 struct binary_analog_channel {
2178 /** Binary value in data stream */
2179 struct binary_value_spec spec;
2180 /** Significant digits */
2182 /** Measured quantity */
2184 /** Measured unit */
2189 * Read extract a value from a binary blob.
2191 * @param out Pointer to output buffer.
2192 * @param spec Binary value specification
2193 * @param data Pointer to binary blob
2194 * @param length Size of binary blob
2195 * @return SR_OK on success, SR_ERR_* error code on failure.
2197 SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, const void *data, size_t length);
2200 * Send an analog channel packet based on a binary analog channel
2203 * @param sdi Device instance
2204 * @param ch Sigrok channel
2205 * @param spec Channel specification
2206 * @param data Pointer to binary blob
2207 * @param length Size of binary blob
2208 * @return SR_OK on success, SR_ERR_* error code on failure.
2210 SR_PRIV int bv_send_analog_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch,
2211 const struct binary_analog_channel *spec, const void *data, size_t length);
2213 /*--- crc.c -----------------------------------------------------------------*/
2215 #define SR_CRC16_DEFAULT_INIT 0xffffU
2218 * Calculate a CRC16 checksum using the 0x8005 polynomial.
2220 * This CRC16 flavor is also known as CRC16-ANSI or CRC16-MODBUS.
2222 * @param crc Initial value (typically 0xffff)
2223 * @param buffer Input buffer
2224 * @param len Buffer length
2227 SR_PRIV uint16_t sr_crc16(uint16_t crc, const uint8_t *buffer, int len);
2229 /*--- modbus/modbus.c -------------------------------------------------------*/
2231 struct sr_modbus_dev_inst {
2235 GSList *(*scan)(int modbusaddr);
2236 int (*dev_inst_new)(void *priv, const char *resource,
2237 char **params, const char *serialcomm, int modbusaddr);
2238 int (*open)(void *priv);
2239 int (*source_add)(struct sr_session *session, void *priv, int events,
2240 int timeout, sr_receive_data_callback cb, void *cb_data);
2241 int (*source_remove)(struct sr_session *session, void *priv);
2242 int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
2243 int (*read_begin)(void *priv, uint8_t *function_code);
2244 int (*read_data)(void *priv, uint8_t *buf, int maxlen);
2245 int (*read_end)(void *priv);
2246 int (*close)(void *priv);
2247 void (*free)(void *priv);
2248 unsigned int read_timeout_ms;
2252 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
2253 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
2254 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
2255 const char *serialcomm, int modbusaddr);
2256 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
2257 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
2258 struct sr_modbus_dev_inst *modbus, int events, int timeout,
2259 sr_receive_data_callback cb, void *cb_data);
2260 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
2261 struct sr_modbus_dev_inst *modbus);
2262 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
2263 uint8_t *request, int request_size);
2264 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
2265 uint8_t *reply, int reply_size);
2266 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
2267 uint8_t *request, int request_size,
2268 uint8_t *reply, int reply_size);
2269 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
2270 int address, int nb_coils, uint8_t *coils);
2271 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
2272 int address, int nb_registers,
2273 uint16_t *registers);
2274 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
2275 int address, int value);
2276 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
2277 int address, int nb_registers,
2278 uint16_t *registers);
2279 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
2280 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
2282 /*--- dmm/es519xx.c ---------------------------------------------------------*/
2285 * All 11-byte es519xx chips repeat each block twice for each conversion cycle
2286 * so always read 2 blocks at a time.
2288 #define ES519XX_11B_PACKET_SIZE (11 * 2)
2289 #define ES519XX_14B_PACKET_SIZE 14
2291 struct es519xx_info {
2292 gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
2293 gboolean is_milli, is_resistance, is_continuity, is_diode;
2294 gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
2295 gboolean is_temperature, is_celsius, is_fahrenheit;
2296 gboolean is_adp0, is_adp1, is_adp2, is_adp3;
2297 gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
2298 gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
2299 gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
2302 gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
2306 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
2307 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
2308 struct sr_datafeed_analog *analog, void *info);
2309 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
2310 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
2311 float *floatval, struct sr_datafeed_analog *analog, void *info);
2312 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
2313 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
2314 float *floatval, struct sr_datafeed_analog *analog, void *info);
2315 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
2316 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
2317 float *floatval, struct sr_datafeed_analog *analog, void *info);
2318 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
2319 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
2320 struct sr_datafeed_analog *analog, void *info);
2321 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
2322 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
2323 struct sr_datafeed_analog *analog, void *info);
2324 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
2325 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
2326 float *floatval, struct sr_datafeed_analog *analog, void *info);
2328 /*--- dmm/fs9922.c ----------------------------------------------------------*/
2330 #define FS9922_PACKET_SIZE 14
2332 struct fs9922_info {
2333 gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
2334 gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
2335 gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
2336 gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
2337 gboolean is_celsius, is_fahrenheit;
2338 int bargraph_sign, bargraph_value;
2341 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
2342 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
2343 struct sr_datafeed_analog *analog, void *info);
2344 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
2346 /*--- dmm/fs9721.c ----------------------------------------------------------*/
2348 #define FS9721_PACKET_SIZE 14
2350 struct fs9721_info {
2351 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2352 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2353 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2354 gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
2357 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
2358 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
2359 struct sr_datafeed_analog *analog, void *info);
2360 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
2361 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
2362 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
2363 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
2364 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
2366 /*--- dmm/mm38xr.c ---------------------------------------------------------*/
2368 #define METERMAN_38XR_PACKET_SIZE 15
2370 struct meterman_38xr_info { int dummy; };
2372 SR_PRIV gboolean meterman_38xr_packet_valid(const uint8_t *buf);
2373 SR_PRIV int meterman_38xr_parse(const uint8_t *buf, float *floatval,
2374 struct sr_datafeed_analog *analog, void *info);
2376 /*--- dmm/ms2115b.c ---------------------------------------------------------*/
2378 #define MS2115B_PACKET_SIZE 9
2380 enum ms2115b_display {
2381 MS2115B_DISPLAY_MAIN,
2382 MS2115B_DISPLAY_SUB,
2383 MS2115B_DISPLAY_COUNT,
2386 struct ms2115b_info {
2387 /* Selected channel. */
2389 gboolean is_ac, is_dc, is_auto;
2390 gboolean is_diode, is_beep, is_farad;
2391 gboolean is_ohm, is_ampere, is_volt, is_hz;
2392 gboolean is_duty_cycle, is_percent;
2395 extern SR_PRIV const char *ms2115b_channel_formats[];
2396 SR_PRIV gboolean sr_ms2115b_packet_valid(const uint8_t *buf);
2397 SR_PRIV int sr_ms2115b_parse(const uint8_t *buf, float *floatval,
2398 struct sr_datafeed_analog *analog, void *info);
2400 /*--- dmm/ms8250d.c ---------------------------------------------------------*/
2402 #define MS8250D_PACKET_SIZE 18
2404 struct ms8250d_info {
2405 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2406 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2407 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2408 gboolean is_ncv, is_min, is_max, is_sign, is_autotimer;
2411 SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf);
2412 SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
2413 struct sr_datafeed_analog *analog, void *info);
2415 /*--- dmm/dtm0660.c ---------------------------------------------------------*/
2417 #define DTM0660_PACKET_SIZE 15
2419 struct dtm0660_info {
2420 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2421 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2422 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2423 gboolean is_degf, is_degc, is_c2c1_01, is_c2c1_00, is_apo, is_min;
2424 gboolean is_minmax, is_max, is_sign;
2427 SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf);
2428 SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
2429 struct sr_datafeed_analog *analog, void *info);
2431 /*--- dmm/m2110.c -----------------------------------------------------------*/
2433 #define BBCGM_M2110_PACKET_SIZE 9
2435 /* Dummy info struct. The parser does not use it. */
2436 struct m2110_info { int dummy; };
2438 SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
2439 SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
2440 struct sr_datafeed_analog *analog, void *info);
2442 /*--- dmm/metex14.c ---------------------------------------------------------*/
2444 #define METEX14_PACKET_SIZE 14
2446 struct metex14_info {
2448 gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
2449 gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
2450 gboolean is_hertz, is_ohm, is_celsius, is_fahrenheit, is_watt;
2451 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2452 gboolean is_gain, is_decibel, is_power, is_decibel_mw, is_power_factor;
2453 gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
2456 #ifdef HAVE_SERIAL_COMM
2457 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
2459 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
2460 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
2461 struct sr_datafeed_analog *analog, void *info);
2462 SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf);
2463 SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
2464 struct sr_datafeed_analog *analog, void *info);
2466 /*--- dmm/rs9lcd.c ----------------------------------------------------------*/
2468 #define RS9LCD_PACKET_SIZE 9
2470 /* Dummy info struct. The parser does not use it. */
2471 struct rs9lcd_info { int dummy; };
2473 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
2474 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
2475 struct sr_datafeed_analog *analog, void *info);
2477 /*--- dmm/bm25x.c -----------------------------------------------------------*/
2479 #define BRYMEN_BM25X_PACKET_SIZE 15
2481 /* Dummy info struct. The parser does not use it. */
2482 struct bm25x_info { int dummy; };
2484 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
2485 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
2486 struct sr_datafeed_analog *analog, void *info);
2488 /*--- dmm/bm52x.c -----------------------------------------------------------*/
2490 #define BRYMEN_BM52X_PACKET_SIZE 24
2491 #define BRYMEN_BM52X_DISPLAY_COUNT 2
2493 struct brymen_bm52x_info { size_t ch_idx; };
2495 #ifdef HAVE_SERIAL_COMM
2496 SR_PRIV int sr_brymen_bm52x_packet_request(struct sr_serial_dev_inst *serial);
2497 SR_PRIV int sr_brymen_bm82x_packet_request(struct sr_serial_dev_inst *serial);
2499 SR_PRIV gboolean sr_brymen_bm52x_packet_valid(const uint8_t *buf);
2500 SR_PRIV gboolean sr_brymen_bm82x_packet_valid(const uint8_t *buf);
2501 /* BM520s and BM820s protocols are similar, the parse routine is shared. */
2502 SR_PRIV int sr_brymen_bm52x_parse(const uint8_t *buf, float *floatval,
2503 struct sr_datafeed_analog *analog, void *info);
2505 struct brymen_bm52x_state;
2507 SR_PRIV void *brymen_bm52x_state_init(void);
2508 SR_PRIV void brymen_bm52x_state_free(void *state);
2509 SR_PRIV int brymen_bm52x_config_get(void *state, uint32_t key, GVariant **data,
2510 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2511 SR_PRIV int brymen_bm52x_config_set(void *state, uint32_t key, GVariant *data,
2512 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2513 SR_PRIV int brymen_bm52x_config_list(void *state, uint32_t key, GVariant **data,
2514 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2515 SR_PRIV int brymen_bm52x_acquire_start(void *state,
2516 const struct sr_dev_inst *sdi,
2517 sr_receive_data_callback *cb, void **cb_data);
2519 /*--- dmm/bm85x.c -----------------------------------------------------------*/
2521 #define BRYMEN_BM85x_PACKET_SIZE_MIN 4
2523 struct brymen_bm85x_info { int dummy; };
2525 #ifdef HAVE_SERIAL_COMM
2526 SR_PRIV int brymen_bm85x_after_open(struct sr_serial_dev_inst *serial);
2527 SR_PRIV int brymen_bm85x_packet_request(struct sr_serial_dev_inst *serial);
2529 SR_PRIV gboolean brymen_bm85x_packet_valid(void *state,
2530 const uint8_t *buf, size_t len, size_t *pkt_len);
2531 SR_PRIV int brymen_bm85x_parse(void *state, const uint8_t *buf, size_t len,
2532 double *floatval, struct sr_datafeed_analog *analog, void *info);
2534 /*--- dmm/bm86x.c -----------------------------------------------------------*/
2536 #define BRYMEN_BM86X_PACKET_SIZE 24
2537 #define BRYMEN_BM86X_DISPLAY_COUNT 2
2539 struct brymen_bm86x_info { size_t ch_idx; };
2541 #ifdef HAVE_SERIAL_COMM
2542 SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial);
2544 SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf);
2545 SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *floatval,
2546 struct sr_datafeed_analog *analog, void *info);
2548 /*--- dmm/ut71x.c -----------------------------------------------------------*/
2550 #define UT71X_PACKET_SIZE 11
2553 gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
2554 gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
2555 gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
2556 gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
2559 SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
2560 SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
2561 struct sr_datafeed_analog *analog, void *info);
2563 /*--- dmm/vc870.c -----------------------------------------------------------*/
2565 #define VC870_PACKET_SIZE 23
2568 gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
2569 gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
2570 gboolean is_current, is_micro, is_milli, is_power;
2571 gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_rms_value;
2572 gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
2573 gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
2574 gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
2575 gboolean is_lo, is_hi, is_open2;
2577 gboolean is_frequency, is_dual_display, is_auto;
2580 SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
2581 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
2582 struct sr_datafeed_analog *analog, void *info);
2584 /*--- dmm/vc96.c ------------------------------------------------------------*/
2586 #define VC96_PACKET_SIZE 13
2590 gboolean is_ac, is_dc, is_resistance, is_diode, is_ampere, is_volt;
2591 gboolean is_ohm, is_micro, is_milli, is_kilo, is_mega, is_hfe;
2592 gboolean is_unitless;
2595 SR_PRIV gboolean sr_vc96_packet_valid(const uint8_t *buf);
2596 SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval,
2597 struct sr_datafeed_analog *analog, void *info);
2599 /*--- lcr/es51919.c ---------------------------------------------------------*/
2601 /* Acquisition details which apply to all supported serial-lcr devices. */
2602 struct lcr_parse_info {
2604 uint64_t output_freq;
2605 const char *circuit_model;
2608 #define ES51919_PACKET_SIZE 17
2609 #define ES51919_CHANNEL_COUNT 2
2610 #define ES51919_COMM_PARAM "9600/8n1/rts=1/dtr=1"
2612 SR_PRIV int es51919_config_get(uint32_t key, GVariant **data,
2613 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2614 SR_PRIV int es51919_config_set(uint32_t key, GVariant *data,
2615 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2616 SR_PRIV int es51919_config_list(uint32_t key, GVariant **data,
2617 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2618 SR_PRIV gboolean es51919_packet_valid(const uint8_t *pkt);
2619 SR_PRIV int es51919_packet_parse(const uint8_t *pkt, float *floatval,
2620 struct sr_datafeed_analog *analog, void *info);
2622 /*--- lcr/vc4080.c ----------------------------------------------------------*/
2624 /* Note: Also uses 'struct lcr_parse_info' from es51919 above. */
2626 #define VC4080_PACKET_SIZE 39
2627 #define VC4080_COMM_PARAM "1200/8n1"
2628 #define VC4080_WITH_DQ_CHANS 0 /* Enable separate D/Q channels? */
2630 enum vc4080_display {
2631 VC4080_DISPLAY_PRIMARY,
2632 VC4080_DISPLAY_SECONDARY,
2633 #if VC4080_WITH_DQ_CHANS
2634 VC4080_DISPLAY_D_VALUE,
2635 VC4080_DISPLAY_Q_VALUE,
2637 VC4080_CHANNEL_COUNT,
2640 extern SR_PRIV const char *vc4080_channel_formats[VC4080_CHANNEL_COUNT];
2642 SR_PRIV int vc4080_config_list(uint32_t key, GVariant **data,
2643 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2644 SR_PRIV int vc4080_packet_request(struct sr_serial_dev_inst *serial);
2645 SR_PRIV gboolean vc4080_packet_valid(const uint8_t *pkt);
2646 SR_PRIV int vc4080_packet_parse(const uint8_t *pkt, float *floatval,
2647 struct sr_datafeed_analog *analog, void *info);
2649 /*--- dmm/ut372.c -----------------------------------------------------------*/
2651 #define UT372_PACKET_SIZE 27
2657 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
2658 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
2659 struct sr_datafeed_analog *analog, void *info);
2661 /*--- dmm/asycii.c ----------------------------------------------------------*/
2663 #define ASYCII_PACKET_SIZE 16
2665 struct asycii_info {
2666 gboolean is_ac, is_dc, is_ac_and_dc;
2667 gboolean is_resistance, is_capacitance, is_diode, is_gain;
2668 gboolean is_frequency, is_duty_cycle, is_duty_pos, is_duty_neg;
2669 gboolean is_pulse_width, is_period_pos, is_period_neg;
2670 gboolean is_pulse_count, is_count_pos, is_count_neg;
2671 gboolean is_ampere, is_volt, is_volt_ampere, is_farad, is_ohm;
2672 gboolean is_hertz, is_percent, is_seconds, is_decibel;
2673 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2674 gboolean is_unitless;
2675 gboolean is_peak_min, is_peak_max;
2676 gboolean is_invalid;
2679 #ifdef HAVE_SERIAL_COMM
2680 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
2682 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);
2683 SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
2684 struct sr_datafeed_analog *analog, void *info);
2686 /*--- dmm/eev121gw.c --------------------------------------------------------*/
2688 #define EEV121GW_PACKET_SIZE 19
2690 enum eev121gw_display {
2691 EEV121GW_DISPLAY_MAIN,
2692 EEV121GW_DISPLAY_SUB,
2693 EEV121GW_DISPLAY_BAR,
2694 EEV121GW_DISPLAY_COUNT,
2697 struct eev121gw_info {
2698 /* Selected channel. */
2701 * Measured value, number and sign/overflow flags, scale factor
2702 * and significant digits.
2704 uint32_t uint_value;
2705 gboolean is_ofl, is_neg;
2707 /* Currently active mode (meter's function). */
2708 gboolean is_ac, is_dc, is_voltage, is_current, is_power, is_gain;
2709 gboolean is_resistance, is_capacitance, is_diode, is_temperature;
2710 gboolean is_continuity, is_frequency, is_period, is_duty_cycle;
2711 /* Quantities associated with mode/function. */
2712 gboolean is_ampere, is_volt, is_volt_ampere, is_dbm;
2713 gboolean is_ohm, is_farad, is_celsius, is_fahrenheit;
2714 gboolean is_hertz, is_seconds, is_percent, is_loop_current;
2715 gboolean is_unitless, is_logic;
2716 /* Other indicators. */
2717 gboolean is_min, is_max, is_avg, is_1ms_peak, is_rel, is_hold;
2718 gboolean is_low_pass, is_mem, is_bt, is_auto_range, is_test;
2719 gboolean is_auto_poweroff, is_low_batt;
2722 extern SR_PRIV const char *eev121gw_channel_formats[];
2723 SR_PRIV gboolean sr_eev121gw_packet_valid(const uint8_t *buf);
2724 SR_PRIV int sr_eev121gw_3displays_parse(const uint8_t *buf, float *floatval,
2725 struct sr_datafeed_analog *analog, void *info);
2727 /*--- scale/kern.c ----------------------------------------------------------*/
2730 gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
2731 gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
2732 gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
2736 SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
2737 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
2738 struct sr_datafeed_analog *analog, void *info);
2740 /*--- sw_limits.c -----------------------------------------------------------*/
2742 struct sr_sw_limits {
2743 uint64_t limit_samples;
2744 uint64_t limit_frames;
2745 uint64_t limit_msec;
2746 uint64_t samples_read;
2747 uint64_t frames_read;
2748 uint64_t start_time;
2751 SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits, uint32_t key,
2753 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
2755 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
2756 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
2757 SR_PRIV int sr_sw_limits_get_remain(const struct sr_sw_limits *limits,
2758 uint64_t *samples, uint64_t *frames, uint64_t *msecs,
2759 gboolean *exceeded);
2760 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
2761 uint64_t samples_read);
2762 SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
2763 uint64_t frames_read);
2764 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
2766 /*--- feed_queue.h ----------------------------------------------------------*/
2768 struct feed_queue_logic;
2769 struct feed_queue_analog;
2771 SR_API struct feed_queue_logic *feed_queue_logic_alloc(
2772 const struct sr_dev_inst *sdi,
2773 size_t sample_count, size_t unit_size);
2774 SR_API int feed_queue_logic_submit(struct feed_queue_logic *q,
2775 const uint8_t *data, size_t count);
2776 SR_API int feed_queue_logic_flush(struct feed_queue_logic *q);
2777 SR_API int feed_queue_logic_send_trigger(struct feed_queue_logic *q);
2778 SR_API void feed_queue_logic_free(struct feed_queue_logic *q);
2780 SR_API struct feed_queue_analog *feed_queue_analog_alloc(
2781 const struct sr_dev_inst *sdi,
2782 size_t sample_count, int digits, struct sr_channel *ch);
2783 SR_API int feed_queue_analog_submit(struct feed_queue_analog *q,
2784 float data, size_t count);
2785 SR_API int feed_queue_analog_flush(struct feed_queue_analog *q);
2786 SR_API void feed_queue_analog_free(struct feed_queue_analog *q);