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 32 bits unsigned integer to memory stored as big endian.
455 * @param p a pointer to the output memory
456 * @param x the input unsigned integer
458 static inline void write_u32be(uint8_t *p, uint32_t x)
460 p[3] = x & 0xff; x >>= 8;
461 p[2] = x & 0xff; x >>= 8;
462 p[1] = x & 0xff; x >>= 8;
463 p[0] = x & 0xff; x >>= 8;
465 #define WB32(p, x) write_u32be((uint8_t *)(p), (uint32_t)(x))
468 * Write a 32 bits unsigned integer to memory stored as little endian.
469 * @param p a pointer to the output memory
470 * @param x the input unsigned integer
472 static inline void write_u32le(uint8_t *p, uint32_t x)
474 p[0] = x & 0xff; x >>= 8;
475 p[1] = x & 0xff; x >>= 8;
476 p[2] = x & 0xff; x >>= 8;
477 p[3] = x & 0xff; x >>= 8;
479 #define WL32(p, x) write_u32le((uint8_t *)(p), (uint32_t)(x))
482 * Write a 48 bits unsigned integer to memory stored as little endian.
483 * @param p a pointer to the output memory
484 * @param x the input unsigned integer
486 static inline void write_u48le(uint8_t *p, uint64_t x)
488 p[0] = x & 0xff; x >>= 8;
489 p[1] = x & 0xff; x >>= 8;
490 p[2] = x & 0xff; x >>= 8;
491 p[3] = x & 0xff; x >>= 8;
492 p[4] = x & 0xff; x >>= 8;
493 p[5] = x & 0xff; x >>= 8;
495 #define WL48(p, x) write_u48le((uint8_t *)(p), (uint64_t)(x))
498 * Write a 64 bits unsigned integer to memory stored as big endian.
499 * @param p a pointer to the output memory
500 * @param x the input unsigned integer
502 static inline void write_u64be(uint8_t *p, uint64_t x)
504 p[7] = x & 0xff; x >>= 8;
505 p[6] = x & 0xff; x >>= 8;
506 p[5] = x & 0xff; x >>= 8;
507 p[4] = x & 0xff; x >>= 8;
508 p[3] = x & 0xff; x >>= 8;
509 p[2] = x & 0xff; x >>= 8;
510 p[1] = x & 0xff; x >>= 8;
511 p[0] = x & 0xff; x >>= 8;
515 * Write a 64 bits unsigned integer to memory stored as little endian.
516 * @param p a pointer to the output memory
517 * @param x the input unsigned integer
519 static inline void write_u64le(uint8_t *p, uint64_t x)
521 p[0] = x & 0xff; x >>= 8;
522 p[1] = x & 0xff; x >>= 8;
523 p[2] = x & 0xff; x >>= 8;
524 p[3] = x & 0xff; x >>= 8;
525 p[4] = x & 0xff; x >>= 8;
526 p[5] = x & 0xff; x >>= 8;
527 p[6] = x & 0xff; x >>= 8;
528 p[7] = x & 0xff; x >>= 8;
530 #define WL64(p, x) write_u64le((uint8_t *)(p), (uint64_t)(x))
533 * Write a 32 bits float to memory stored as big endian.
534 * @param p a pointer to the output memory
535 * @param x the input float
537 static inline void write_fltbe(uint8_t *p, float x)
539 union { uint32_t u; float f; } u;
543 #define WBFL(p, x) write_fltbe((uint8_t *)(p), (x))
546 * Write a 32 bits float to memory stored as little endian.
547 * @param p a pointer to the output memory
548 * @param x the input float
550 static inline void write_fltle(uint8_t *p, float x)
552 union { uint32_t u; float f; } u;
556 #define WLFL(p, x) write_fltle((uint8_t *)(p), float (x))
559 * Write a 64 bits float to memory stored as little endian.
560 * @param p a pointer to the output memory
561 * @param x the input floating point value
563 static inline void write_dblle(uint8_t *p, double x)
565 union { uint64_t u; double f; } u;
569 #define WLDB(p, x) write_dblle((uint8_t *)(p), float (x))
571 /* Endianess conversion helpers with read/write position increment. */
574 * Read unsigned 8bit integer from raw memory, increment read position.
575 * @param[in, out] p Pointer into byte stream.
576 * @return Retrieved integer value, unsigned.
578 static inline uint8_t read_u8_inc(const uint8_t **p)
591 * Read signed 8bit integer from raw memory, increment read position.
592 * @param[in, out] p Pointer into byte stream.
593 * @return Retrieved integer value, signed.
595 static inline int8_t read_i8_inc(const uint8_t **p)
608 * Read unsigned 16bit integer from raw memory (big endian format), increment read position.
609 * @param[in, out] p Pointer into byte stream.
610 * @return Retrieved integer value, unsigned.
612 static inline uint16_t read_u16be_inc(const uint8_t **p)
625 * Read unsigned 16bit integer from raw memory (little endian format), increment read position.
626 * @param[in, out] p Pointer into byte stream.
627 * @return Retrieved integer value, unsigned.
629 static inline uint16_t read_u16le_inc(const uint8_t **p)
642 * Read signed 16bit integer from raw memory (big endian format), increment read position.
643 * @param[in, out] p Pointer into byte stream.
644 * @return Retrieved integer value, signed.
646 static inline int16_t read_i16be_inc(const uint8_t **p)
659 * Read signed 16bit integer from raw memory (little endian format), increment read position.
660 * @param[in, out] p Pointer into byte stream.
661 * @return Retrieved integer value, signed.
663 static inline int16_t read_i16le_inc(const uint8_t **p)
676 * Read unsigned 24bit integer from raw memory (little endian format), increment read position.
677 * @param[in, out] p Pointer into byte stream.
678 * @return Retrieved integer value, unsigned.
680 static inline uint32_t read_u24le_inc(const uint8_t **p)
687 *p += 3 * sizeof(uint8_t);
693 * Read unsigned 32bit integer from raw memory (big endian format), increment read position.
694 * @param[in, out] p Pointer into byte stream.
695 * @return Retrieved integer value, unsigned.
697 static inline uint32_t read_u32be_inc(const uint8_t **p)
710 * Read unsigned 32bit integer from raw memory (little endian format), increment read position.
711 * @param[in, out] p Pointer into byte stream.
712 * @return Retrieved integer value, unsigned.
714 static inline uint32_t read_u32le_inc(const uint8_t **p)
727 * Read signed 32bit integer from raw memory (big endian format), increment read position.
728 * @param[in, out] p Pointer into byte stream.
729 * @return Retrieved integer value, signed.
731 static inline int32_t read_i32be_inc(const uint8_t **p)
744 * Read signed 32bit integer from raw memory (little endian format), increment read position.
745 * @param[in, out] p Pointer into byte stream.
746 * @return Retrieved integer value, signed.
748 static inline int32_t read_i32le_inc(const uint8_t **p)
761 * Read unsigned 64bit integer from raw memory (big endian format), increment read position.
762 * @param[in, out] p Pointer into byte stream.
763 * @return Retrieved integer value, unsigned.
765 static inline uint64_t read_u64be_inc(const uint8_t **p)
778 * Read unsigned 64bit integer from raw memory (little endian format), increment read position.
779 * @param[in, out] p Pointer into byte stream.
780 * @return Retrieved integer value, unsigned.
782 static inline uint64_t read_u64le_inc(const uint8_t **p)
795 * Read 32bit float from raw memory (big endian format), increment read position.
796 * @param[in, out] p Pointer into byte stream.
797 * @return Retrieved float value.
799 static inline float read_fltbe_inc(const uint8_t **p)
812 * Read 32bit float from raw memory (little endian format), increment read position.
813 * @param[in, out] p Pointer into byte stream.
814 * @return Retrieved float value.
816 static inline float read_fltle_inc(const uint8_t **p)
829 * Read 64bit float from raw memory (big endian format), increment read position.
830 * @param[in, out] p Pointer into byte stream.
831 * @return Retrieved float value.
833 static inline double read_dblbe_inc(const uint8_t **p)
846 * Read 64bit float from raw memory (little endian format), increment read position.
847 * @param[in, out] p Pointer into byte stream.
848 * @return Retrieved float value.
850 static inline double read_dblle_inc(const uint8_t **p)
863 * Write unsigned 8bit integer to raw memory, increment write position.
864 * @param[in, out] p Pointer into byte stream.
865 * @param[in] x Value to write.
867 static inline void write_u8_inc(uint8_t **p, uint8_t x)
876 * Write unsigned 16bit big endian integer to raw memory, increment write position.
877 * @param[in, out] p Pointer into byte stream.
878 * @param[in] x Value to write.
880 static inline void write_u16be_inc(uint8_t **p, uint16_t x)
889 * Write unsigned 16bit little endian integer to raw memory, increment write position.
890 * @param[in, out] p Pointer into byte stream.
891 * @param[in] x Value to write.
893 static inline void write_u16le_inc(uint8_t **p, uint16_t x)
902 * Write unsigned 32bit big endian integer to raw memory, increment write position.
903 * @param[in, out] p Pointer into byte stream.
904 * @param[in] x Value to write.
906 static inline void write_u32be_inc(uint8_t **p, uint32_t x)
915 * Write unsigned 32bit little endian integer to raw memory, increment write position.
916 * @param[in, out] p Pointer into byte stream.
917 * @param[in] x Value to write.
919 static inline void write_u32le_inc(uint8_t **p, uint32_t x)
928 * Write unsigned 48bit little endian integer to raw memory, increment write position.
929 * @param[in, out] p Pointer into byte stream.
930 * @param[in] x Value to write.
932 static inline void write_u48le_inc(uint8_t **p, uint64_t x)
937 *p += 48 / 8 * sizeof(uint8_t);
941 * Write unsigned 64bit little endian integer to raw memory, increment write position.
942 * @param[in, out] p Pointer into byte stream.
943 * @param[in] x Value to write.
945 static inline void write_u64le_inc(uint8_t **p, uint64_t x)
954 * Write single precision little endian float to raw memory, increment write position.
955 * @param[in, out] p Pointer into byte stream.
956 * @param[in] x Value to write.
958 static inline void write_fltle_inc(uint8_t **p, float x)
967 * Write double precision little endian float to raw memory, increment write position.
968 * @param[in, out] p Pointer into byte stream.
969 * @param[in] x Value to write.
971 static inline void write_dblle_inc(uint8_t **p, double x)
979 /* Portability fixes for FreeBSD. */
981 #define LIBUSB_CLASS_APPLICATION 0xfe
982 #define libusb_has_capability(x) 0
983 #define libusb_handle_events_timeout_completed(ctx, tv, c) \
984 libusb_handle_events_timeout(ctx, tv)
988 * Convenience for FTDI library version dependency.
989 * - Version 1.5 introduced ftdi_tciflush(), ftdi_tcoflush(), and
990 * ftdi_tcioflush() all within the same commit, and deprecated
991 * ftdi_usb_purge_buffers() which suffered from inverse semantics.
992 * The API is drop-in compatible (arguments count and data types are
993 * identical). The libsigrok source code always flushes RX and TX at
994 * the same time, never individually.
996 #if defined HAVE_FTDI_TCIOFLUSH && HAVE_FTDI_TCIOFLUSH
997 # define PURGE_FTDI_BOTH ftdi_tcioflush
999 # define PURGE_FTDI_BOTH ftdi_usb_purge_buffers
1002 /* Static definitions of structs ending with an all-zero entry are a
1003 * problem when compiling with -Wmissing-field-initializers: GCC
1004 * suppresses the warning only with { 0 }, clang wants { } */
1006 #define ALL_ZERO { }
1008 #define ALL_ZERO { 0 }
1012 #define SR_DRIVER_LIST_SECTION "__DATA,__sr_driver_list"
1014 #define SR_DRIVER_LIST_SECTION "__sr_driver_list"
1017 #if !defined SR_DRIVER_LIST_NOREORDER && defined __has_attribute
1018 #if __has_attribute(no_reorder)
1019 #define SR_DRIVER_LIST_NOREORDER __attribute__((no_reorder))
1022 #if !defined SR_DRIVER_LIST_NOREORDER
1023 #define SR_DRIVER_LIST_NOREORDER /* EMPTY */
1027 * Register a list of hardware drivers.
1029 * This macro can be used to register multiple hardware drivers to the library.
1030 * This is useful when a driver supports multiple similar but slightly
1031 * different devices that require different sr_dev_driver struct definitions.
1033 * For registering only a single driver see SR_REGISTER_DEV_DRIVER().
1037 * #define MY_DRIVER(_name) \
1038 * &(struct sr_dev_driver){ \
1043 * SR_REGISTER_DEV_DRIVER_LIST(my_driver_infos,
1044 * MY_DRIVER("driver 1"),
1045 * MY_DRIVER("driver 2"),
1050 * @param name Name to use for the driver list identifier.
1051 * @param ... Comma separated list of pointers to sr_dev_driver structs.
1053 #define SR_REGISTER_DEV_DRIVER_LIST(name, ...) \
1054 static const struct sr_dev_driver *name[] \
1055 SR_DRIVER_LIST_NOREORDER \
1056 __attribute__((section (SR_DRIVER_LIST_SECTION), used, \
1057 aligned(sizeof(struct sr_dev_driver *)))) \
1063 * Register a hardware driver.
1065 * This macro is used to register a hardware driver with the library. It has
1066 * to be used in order to make the driver accessible to applications using the
1069 * The macro invocation should be placed directly under the struct
1070 * sr_dev_driver definition.
1074 * static struct sr_dev_driver driver_info = {
1078 * SR_REGISTER_DEV_DRIVER(driver_info);
1081 * @param name Identifier name of sr_dev_driver struct to register.
1083 #define SR_REGISTER_DEV_DRIVER(name) \
1084 SR_REGISTER_DEV_DRIVER_LIST(name##_list, &name);
1086 SR_API void sr_drivers_init(struct sr_context *context);
1089 struct sr_dev_driver **driver_list;
1090 #ifdef HAVE_LIBUSB_1_0
1091 libusb_context *libusb_ctx;
1093 sr_resource_open_callback resource_open_cb;
1094 sr_resource_close_callback resource_close_cb;
1095 sr_resource_read_callback resource_read_cb;
1096 void *resource_cb_data;
1099 /** Input module metadata keys. */
1100 enum sr_input_meta_keys {
1101 /** The input filename, if there is one. */
1102 SR_INPUT_META_FILENAME = 0x01,
1103 /** The input file's size in bytes. */
1104 SR_INPUT_META_FILESIZE = 0x02,
1105 /** The first 128 bytes of the file, provided as a GString. */
1106 SR_INPUT_META_HEADER = 0x04,
1108 /** The module cannot identify a file without this metadata. */
1109 SR_INPUT_META_REQUIRED = 0x80,
1112 /** Input (file) module struct. */
1115 * A pointer to this input module's 'struct sr_input_module'.
1117 const struct sr_input_module *module;
1119 struct sr_dev_inst *sdi;
1124 /** Input (file) module driver. */
1125 struct sr_input_module {
1127 * A unique ID for this input module, suitable for use in command-line
1128 * clients, [a-z0-9-]. Must not be NULL.
1133 * A unique name for this input module, suitable for use in GUI
1134 * clients, can contain UTF-8. Must not be NULL.
1139 * A short description of the input module. Must not be NULL.
1141 * This can be displayed by frontends, e.g. when selecting the input
1142 * module for saving a file.
1147 * A NULL terminated array of strings containing a list of file name
1148 * extensions typical for the input file format, or NULL if there is
1149 * no typical extension for this file format.
1151 const char *const *exts;
1154 * Zero-terminated list of metadata items the module needs to be able
1155 * to identify an input stream. Can be all-zero, if the module cannot
1156 * identify streams at all, i.e. has to be forced into use.
1158 * Each item is one of:
1159 * SR_INPUT_META_FILENAME
1160 * SR_INPUT_META_FILESIZE
1161 * SR_INPUT_META_HEADER
1163 * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
1164 * identify a stream without the given metadata.
1166 const uint8_t metadata[8];
1169 * Returns a NULL-terminated list of options this module can take.
1170 * Can be NULL, if the module has no options.
1172 const struct sr_option *(*options) (void);
1175 * Check if this input module can load and parse the specified stream.
1177 * @param[in] metadata Metadata the module can use to identify the stream.
1178 * @param[out] confidence "Strength" of the detection.
1179 * Specialized handlers can take precedence over generic/basic support.
1181 * @retval SR_OK This module knows the format.
1182 * @retval SR_ERR_NA There wasn't enough data for this module to
1183 * positively identify the format.
1184 * @retval SR_ERR_DATA This module knows the format, but cannot handle
1185 * it. This means the stream is either corrupt, or indicates a
1186 * feature that the module does not support.
1187 * @retval SR_ERR This module does not know the format.
1189 * Lower numeric values of 'confidence' mean that the input module
1190 * stronger believes in its capability to handle this specific format.
1191 * This way, multiple input modules can claim support for a format,
1192 * and the application can pick the best match, or try fallbacks
1193 * in case of errors. This approach also copes with formats that
1194 * are unreliable to detect in the absence of magic signatures.
1196 int (*format_match) (GHashTable *metadata, unsigned int *confidence);
1199 * Initialize the input module.
1201 * @retval SR_OK Success
1202 * @retval other Negative error code.
1204 int (*init) (struct sr_input *in, GHashTable *options);
1207 * Send data to the specified input instance.
1209 * When an input module instance is created with sr_input_new(), this
1210 * function is used to feed data to the instance.
1212 * As enough data gets fed into this function to completely populate
1213 * the device instance associated with this input instance, this is
1214 * guaranteed to return the moment it's ready. This gives the caller
1215 * the chance to examine the device instance, attach session callbacks
1218 * @retval SR_OK Success
1219 * @retval other Negative error code.
1221 int (*receive) (struct sr_input *in, GString *buf);
1224 * Signal the input module no more data will come.
1226 * This will cause the module to process any data it may have buffered.
1227 * The SR_DF_END packet will also typically be sent at this time.
1229 int (*end) (struct sr_input *in);
1232 * Reset the input module's input handling structures.
1234 * Causes the input module to reset its internal state so that we can
1235 * re-send the input data from the beginning without having to
1236 * re-create the entire input module.
1238 * @retval SR_OK Success.
1239 * @retval other Negative error code.
1241 int (*reset) (struct sr_input *in);
1244 * This function is called after the caller is finished using
1245 * the input module, and can be used to free any internal
1246 * resources the module may keep.
1248 * This function is optional.
1250 * @retval SR_OK Success
1251 * @retval other Negative error code.
1253 void (*cleanup) (struct sr_input *in);
1256 /** Output module instance. */
1258 /** A pointer to this output's module. */
1259 const struct sr_output_module *module;
1262 * The device for which this output module is creating output. This
1263 * can be used by the module to find out channel names and numbers.
1265 const struct sr_dev_inst *sdi;
1268 * The name of the file that the data should be written to.
1270 const char *filename;
1273 * A generic pointer which can be used by the module to keep internal
1274 * state between calls into its callback functions.
1276 * For example, the module might store a pointer to a chunk of output
1277 * there, and only flush it when it reaches a certain size.
1282 /** Output module driver. */
1283 struct sr_output_module {
1285 * A unique ID for this output module, suitable for use in command-line
1286 * clients, [a-z0-9-]. Must not be NULL.
1291 * A unique name for this output module, suitable for use in GUI
1292 * clients, can contain UTF-8. Must not be NULL.
1297 * A short description of the output module. Must not be NULL.
1299 * This can be displayed by frontends, e.g. when selecting the output
1300 * module for saving a file.
1305 * A NULL terminated array of strings containing a list of file name
1306 * extensions typical for the input file format, or NULL if there is
1307 * no typical extension for this file format.
1309 const char *const *exts;
1312 * Bitfield containing flags that describe certain properties
1313 * this output module may or may not have.
1314 * @see sr_output_flags
1316 const uint64_t flags;
1319 * Returns a NULL-terminated list of options this module can take.
1320 * Can be NULL, if the module has no options.
1322 const struct sr_option *(*options) (void);
1325 * This function is called once, at the beginning of an output stream.
1327 * The device struct will be available in the output struct passed in,
1328 * as well as the param field -- which may be NULL or an empty string,
1329 * if no parameter was passed.
1331 * The module can use this to initialize itself, create a struct for
1332 * keeping state and storing it in the <code>internal</code> field.
1334 * @param o Pointer to the respective 'struct sr_output'.
1336 * @retval SR_OK Success
1337 * @retval other Negative error code.
1339 int (*init) (struct sr_output *o, GHashTable *options);
1342 * This function is passed a copy of every packet in the data feed.
1343 * Any output generated by the output module in response to the
1344 * packet should be returned in a newly allocated GString
1345 * <code>out</code>, which will be freed by the caller.
1347 * Packets not of interest to the output module can just be ignored,
1348 * and the <code>out</code> parameter set to NULL.
1350 * @param o Pointer to the respective 'struct sr_output'.
1351 * @param sdi The device instance that generated the packet.
1352 * @param packet The complete packet.
1353 * @param out A pointer where a GString * should be stored if
1354 * the module generates output, or NULL if not.
1356 * @retval SR_OK Success
1357 * @retval other Negative error code.
1359 int (*receive) (const struct sr_output *o,
1360 const struct sr_datafeed_packet *packet, GString **out);
1363 * This function is called after the caller is finished using
1364 * the output module, and can be used to free any internal
1365 * resources the module may keep.
1367 * @retval SR_OK Success
1368 * @retval other Negative error code.
1370 int (*cleanup) (struct sr_output *o);
1373 /** Transform module instance. */
1374 struct sr_transform {
1375 /** A pointer to this transform's module. */
1376 const struct sr_transform_module *module;
1379 * The device for which this transform module is used. This
1380 * can be used by the module to find out channel names and numbers.
1382 const struct sr_dev_inst *sdi;
1385 * A generic pointer which can be used by the module to keep internal
1386 * state between calls into its callback functions.
1391 struct sr_transform_module {
1393 * A unique ID for this transform module, suitable for use in
1394 * command-line clients, [a-z0-9-]. Must not be NULL.
1399 * A unique name for this transform module, suitable for use in GUI
1400 * clients, can contain UTF-8. Must not be NULL.
1405 * A short description of the transform module. Must not be NULL.
1407 * This can be displayed by frontends, e.g. when selecting
1408 * which transform module(s) to add.
1413 * Returns a NULL-terminated list of options this transform module
1414 * can take. Can be NULL, if the transform module has no options.
1416 const struct sr_option *(*options) (void);
1419 * This function is called once, at the beginning of a stream.
1421 * @param t Pointer to the respective 'struct sr_transform'.
1422 * @param options Hash table of options for this transform module.
1423 * Can be NULL if no options are to be used.
1425 * @retval SR_OK Success
1426 * @retval other Negative error code.
1428 int (*init) (struct sr_transform *t, GHashTable *options);
1431 * This function is passed a pointer to every packet in the data feed.
1433 * It can either return (in packet_out) a pointer to another packet
1434 * (possibly the exact same packet it got as input), or NULL.
1436 * @param t Pointer to the respective 'struct sr_transform'.
1437 * @param packet_in Pointer to a datafeed packet.
1438 * @param packet_out Pointer to the resulting datafeed packet after
1439 * this function was run. If NULL, the transform
1440 * module intentionally didn't output a new packet.
1442 * @retval SR_OK Success
1443 * @retval other Negative error code.
1445 int (*receive) (const struct sr_transform *t,
1446 struct sr_datafeed_packet *packet_in,
1447 struct sr_datafeed_packet **packet_out);
1450 * This function is called after the caller is finished using
1451 * the transform module, and can be used to free any internal
1452 * resources the module may keep.
1454 * @retval SR_OK Success
1455 * @retval other Negative error code.
1457 int (*cleanup) (struct sr_transform *t);
1460 #ifdef HAVE_LIBUSB_1_0
1461 /** USB device instance */
1462 struct sr_usb_dev_inst {
1465 /** Device address on USB bus */
1467 /** libusb device handle */
1468 struct libusb_device_handle *devhdl;
1472 struct sr_serial_dev_inst;
1473 #ifdef HAVE_SERIAL_COMM
1474 struct ser_lib_functions;
1475 struct ser_hid_chip_functions;
1477 typedef void (*serial_rx_chunk_callback)(struct sr_serial_dev_inst *serial,
1478 void *cb_data, const void *buf, size_t count);
1479 struct sr_serial_dev_inst {
1480 /** Port name, e.g. '/dev/tty42'. */
1482 /** Comm params for serial_set_paramstr(). */
1484 struct ser_lib_functions *lib_funcs;
1491 GString *rcv_buffer;
1492 serial_rx_chunk_callback rx_chunk_cb_func;
1493 void *rx_chunk_cb_data;
1494 #ifdef HAVE_LIBSERIALPORT
1495 /** libserialport port handle */
1496 struct sp_port *sp_data;
1498 #ifdef HAVE_LIBHIDAPI
1499 enum ser_hid_chip_t {
1500 SER_HID_CHIP_UNKNOWN, /**!< place holder */
1501 SER_HID_CHIP_BTC_BU86X, /**!< Brymen BU86x */
1502 SER_HID_CHIP_SIL_CP2110, /**!< SiLabs CP2110 */
1503 SER_HID_CHIP_VICTOR_DMM, /**!< Victor 70/86 DMM cable */
1504 SER_HID_CHIP_WCH_CH9325, /**!< WCH CH9325 */
1505 SER_HID_CHIP_LAST, /**!< sentinel */
1507 struct ser_hid_chip_functions *hid_chip_funcs;
1510 const char *hid_path;
1511 hid_device *hid_dev;
1512 GSList *hid_source_args;
1514 #ifdef HAVE_BLUETOOTH
1515 enum ser_bt_conn_t {
1516 SER_BT_CONN_UNKNOWN, /**!< place holder */
1517 SER_BT_CONN_RFCOMM, /**!< BT classic, RFCOMM channel */
1518 SER_BT_CONN_BLE122, /**!< BLE, BLE122 module, indications */
1519 SER_BT_CONN_NRF51, /**!< BLE, Nordic nRF51, notifications */
1520 SER_BT_CONN_CC254x, /**!< BLE, TI CC254x, notifications */
1521 SER_BT_CONN_MAX, /**!< sentinel */
1523 char *bt_addr_local;
1524 char *bt_addr_remote;
1525 size_t bt_rfcomm_channel;
1526 uint16_t bt_notify_handle_read;
1527 uint16_t bt_notify_handle_write;
1528 uint16_t bt_notify_handle_cccd;
1529 uint16_t bt_notify_value_cccd;
1530 struct sr_bt_desc *bt_desc;
1531 GSList *bt_source_args;
1536 struct sr_usbtmc_dev_inst {
1541 /* Private driver context. */
1542 struct drv_context {
1543 /** sigrok context */
1544 struct sr_context *sr_ctx;
1548 /*--- log.c -----------------------------------------------------------------*/
1550 #if defined(_WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
1552 * On MinGW, we need to specify the gnu_printf format flavor or GCC
1553 * will assume non-standard Microsoft printf syntax.
1555 SR_PRIV int sr_log(int loglevel, const char *format, ...)
1556 __attribute__((__format__ (__gnu_printf__, 2, 3)));
1558 SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
1561 /* Message logging helpers with subsystem-specific prefix string. */
1562 #define sr_spew(...) sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
1563 #define sr_dbg(...) sr_log(SR_LOG_DBG, LOG_PREFIX ": " __VA_ARGS__)
1564 #define sr_info(...) sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
1565 #define sr_warn(...) sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
1566 #define sr_err(...) sr_log(SR_LOG_ERR, LOG_PREFIX ": " __VA_ARGS__)
1568 /*--- device.c --------------------------------------------------------------*/
1570 /** Scan options supported by a driver. */
1571 #define SR_CONF_SCAN_OPTIONS 0x7FFF0000
1573 /** Device options for a particular device. */
1574 #define SR_CONF_DEVICE_OPTIONS 0x7FFF0001
1576 /** Mask for separating config keys from capabilities. */
1577 #define SR_CONF_MASK 0x1fffffff
1579 /** Values for the changes argument of sr_dev_driver.config_channel_set. */
1581 /** The enabled state of the channel has been changed. */
1582 SR_CHANNEL_SET_ENABLED = 1 << 0,
1585 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
1586 int index, int type, gboolean enabled, const char *name);
1587 SR_PRIV void sr_channel_free(struct sr_channel *ch);
1588 SR_PRIV void sr_channel_free_cb(void *p);
1589 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
1590 struct sr_channel *cur_channel);
1591 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2);
1592 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2);
1594 /** Device instance data */
1595 struct sr_dev_inst {
1596 /** Device driver. */
1597 struct sr_dev_driver *driver;
1598 /** Device instance status. SR_ST_NOT_FOUND, etc. */
1600 /** Device instance type. SR_INST_USB, etc. */
1602 /** Device vendor. */
1604 /** Device model. */
1606 /** Device version. */
1608 /** Serial number. */
1610 /** Connection string to uniquely identify devices. */
1611 char *connection_id;
1612 /** List of channels. */
1614 /** List of sr_channel_group structs */
1615 GSList *channel_groups;
1616 /** Device instance connection data (used?) */
1618 /** Device instance private data (used?) */
1620 /** Session to which this device is currently assigned. */
1621 struct sr_session *session;
1624 /* Generic device instances */
1625 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
1627 #ifdef HAVE_LIBUSB_1_0
1628 /* USB-specific instances */
1629 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
1630 uint8_t address, struct libusb_device_handle *hdl);
1631 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
1634 #ifdef HAVE_SERIAL_COMM
1635 #ifndef HAVE_LIBSERIALPORT
1637 * Some identifiers which initially got provided by libserialport are
1638 * used internally within the libsigrok serial layer's implementation,
1639 * while libserialport no longer is the exclusive provider of serial
1640 * communication support. Declare the identifiers here so they remain
1641 * available across all build configurations.
1648 SP_PARITY_SPACE = 4,
1651 enum libsp_flowcontrol {
1652 SP_FLOWCONTROL_NONE = 0,
1653 SP_FLOWCONTROL_XONXOFF = 1,
1654 SP_FLOWCONTROL_RTSCTS = 2,
1655 SP_FLOWCONTROL_DTRDSR = 3,
1659 /* Serial-specific instances */
1660 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
1661 const char *serialcomm);
1662 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
1665 /* USBTMC-specific instances */
1666 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
1667 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
1669 /*--- hwdriver.c ------------------------------------------------------------*/
1671 SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
1672 SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
1673 SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
1674 SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
1675 SR_PRIV void sr_config_free(struct sr_config *src);
1676 SR_PRIV int sr_dev_acquisition_start(struct sr_dev_inst *sdi);
1677 SR_PRIV int sr_dev_acquisition_stop(struct sr_dev_inst *sdi);
1679 /*--- session.c -------------------------------------------------------------*/
1682 /** Context this session exists in. */
1683 struct sr_context *ctx;
1684 /** List of struct sr_dev_inst pointers. */
1686 /** List of struct sr_dev_inst pointers owned by this session. */
1688 /** List of struct datafeed_callback pointers. */
1689 GSList *datafeed_callbacks;
1691 struct sr_trigger *trigger;
1693 /** Callback to invoke on session stop. */
1694 sr_session_stopped_callback stopped_callback;
1695 /** User data to be passed to the session stop callback. */
1696 void *stopped_cb_data;
1698 /** Mutex protecting the main context pointer. */
1700 /** Context of the session main loop. */
1701 GMainContext *main_context;
1703 /** Registered event sources for this session. */
1704 GHashTable *event_sources;
1705 /** Session main loop. */
1706 GMainLoop *main_loop;
1707 /** ID of idle source for dispatching the session stop notification. */
1708 unsigned int stop_check_id;
1709 /** Whether the session has been started. */
1713 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
1714 void *key, GSource *source);
1715 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
1717 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1718 void *key, GSource *source);
1719 SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1720 void *key, gintptr fd, int events, int timeout,
1721 sr_receive_data_callback cb, void *cb_data);
1723 SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
1724 int events, int timeout, sr_receive_data_callback cb, void *cb_data);
1725 SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
1726 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1728 SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
1729 GIOChannel *channel, int events, int timeout,
1730 sr_receive_data_callback cb, void *cb_data);
1731 SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
1732 SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
1734 SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
1735 GIOChannel *channel);
1737 SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi,
1738 uint32_t key, GVariant *var);
1739 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
1740 const struct sr_datafeed_packet *packet);
1741 SR_PRIV int sr_sessionfile_check(const char *filename);
1742 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename,
1743 struct sr_session **session);
1745 /*--- session_file.c --------------------------------------------------------*/
1747 #if !HAVE_ZIP_DISCARD
1748 /* Replace zip_discard() if not available. */
1749 #define zip_discard(zip) sr_zip_discard(zip)
1750 SR_PRIV void sr_zip_discard(struct zip *archive);
1753 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
1754 const struct zip_stat *entry);
1756 /*--- analog.c --------------------------------------------------------------*/
1758 SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
1759 struct sr_analog_encoding *encoding,
1760 struct sr_analog_meaning *meaning,
1761 struct sr_analog_spec *spec,
1764 /*--- std.c -----------------------------------------------------------------*/
1766 typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
1767 typedef void (*std_dev_clear_callback)(void *priv);
1769 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx);
1770 SR_PRIV int std_cleanup(const struct sr_dev_driver *di);
1771 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
1772 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
1773 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
1774 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
1775 #ifdef HAVE_SERIAL_COMM
1776 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
1777 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
1779 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi);
1780 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi);
1781 SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi);
1782 SR_PRIV int std_session_send_df_frame_begin(const struct sr_dev_inst *sdi);
1783 SR_PRIV int std_session_send_df_frame_end(const struct sr_dev_inst *sdi);
1784 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
1785 std_dev_clear_callback clear_private);
1786 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver);
1787 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di);
1788 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
1789 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices);
1791 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
1792 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
1793 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
1794 size_t drvsize, const uint32_t devopts[], size_t devsize);
1796 extern SR_PRIV const uint32_t NO_OPTS[1];
1798 #define STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts) \
1799 std_opts_config_list(key, data, sdi, cg, ARRAY_AND_SIZE(scanopts), \
1800 ARRAY_AND_SIZE(drvopts), ARRAY_AND_SIZE(devopts))
1802 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n);
1803 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n);
1804 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n);
1805 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n);
1806 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step);
1807 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]);
1808 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double dmin, const double dmax, const double dstep);
1810 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high);
1811 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high);
1813 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n);
1814 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n);
1815 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n);
1816 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n);
1818 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n);
1820 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n);
1821 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n);
1822 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n);
1824 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n);
1825 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n);
1827 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n);
1828 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
1829 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
1831 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
1833 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
1834 int baudrate, int bits, int parity, int stopbits,
1835 int flowcontrol, int rts, int dtr);
1836 SR_PRIV int std_dummy_set_handshake(struct sr_serial_dev_inst *serial,
1839 /*--- resource.c ------------------------------------------------------------*/
1841 SR_PRIV int64_t sr_file_get_size(FILE *file);
1843 SR_PRIV int sr_resource_open(struct sr_context *ctx,
1844 struct sr_resource *res, int type, const char *name)
1845 G_GNUC_WARN_UNUSED_RESULT;
1846 SR_PRIV int sr_resource_close(struct sr_context *ctx,
1847 struct sr_resource *res);
1848 SR_PRIV gssize sr_resource_read(struct sr_context *ctx,
1849 const struct sr_resource *res, void *buf, size_t count)
1850 G_GNUC_WARN_UNUSED_RESULT;
1851 SR_PRIV void *sr_resource_load(struct sr_context *ctx, int type,
1852 const char *name, size_t *size, size_t max_size)
1853 G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
1855 /*--- strutil.c -------------------------------------------------------------*/
1857 SR_PRIV int sr_atol(const char *str, long *ret);
1858 SR_PRIV int sr_atol_base(const char *str, long *ret, char **end, int base);
1859 SR_PRIV int sr_atoul_base(const char *str, unsigned long *ret, char **end, int base);
1860 SR_PRIV int sr_atoi(const char *str, int *ret);
1861 SR_PRIV int sr_atod(const char *str, double *ret);
1862 SR_PRIV int sr_atof(const char *str, float *ret);
1863 SR_PRIV int sr_atod_ascii(const char *str, double *ret);
1864 SR_PRIV int sr_atod_ascii_digits(const char *str, double *ret, int *digits);
1865 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
1867 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
1868 SR_PRIV void sr_hexdump_free(GString *s);
1870 /*--- soft-trigger.c --------------------------------------------------------*/
1872 struct soft_trigger_logic {
1873 const struct sr_dev_inst *sdi;
1874 const struct sr_trigger *trigger;
1878 uint8_t *prev_sample;
1879 uint8_t *pre_trigger_buffer;
1880 uint8_t *pre_trigger_head;
1881 int pre_trigger_size;
1882 int pre_trigger_fill;
1885 SR_PRIV int logic_channel_unitsize(GSList *channels);
1886 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
1887 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
1888 int pre_trigger_samples);
1889 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
1890 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
1891 int len, int *pre_trigger_samples);
1893 /*--- serial.c --------------------------------------------------------------*/
1895 #ifdef HAVE_SERIAL_COMM
1901 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
1902 typedef int (*packet_valid_len_callback)(void *st,
1903 const uint8_t *p, size_t l, size_t *pl);
1905 typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
1907 typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
1909 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
1910 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
1911 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
1912 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
1913 SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
1914 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
1915 const void *buf, size_t count, unsigned int timeout_ms);
1916 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
1917 const void *buf, size_t count);
1918 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
1919 size_t count, unsigned int timeout_ms);
1920 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
1922 SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
1923 serial_rx_chunk_callback cb, void *cb_data);
1924 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
1925 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
1926 SR_PRIV int serial_set_handshake(struct sr_serial_dev_inst *serial,
1928 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
1929 const char *paramstr);
1930 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
1931 int *buflen, gint64 timeout_ms);
1932 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
1933 uint8_t *buf, size_t *buflen,
1934 size_t packet_size, packet_valid_callback is_valid,
1935 packet_valid_len_callback is_valid_len, size_t *return_size,
1936 uint64_t timeout_ms);
1937 SR_PRIV int serial_source_add(struct sr_session *session,
1938 struct sr_serial_dev_inst *serial, int events, int timeout,
1939 sr_receive_data_callback cb, void *cb_data);
1940 SR_PRIV int serial_source_remove(struct sr_session *session,
1941 struct sr_serial_dev_inst *serial);
1942 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
1943 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
1945 SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial);
1946 SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial);
1947 SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
1948 const uint8_t *data, size_t len);
1949 SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
1950 uint8_t *data, size_t len);
1952 struct ser_lib_functions {
1953 int (*open)(struct sr_serial_dev_inst *serial, int flags);
1954 int (*close)(struct sr_serial_dev_inst *serial);
1955 int (*flush)(struct sr_serial_dev_inst *serial);
1956 int (*drain)(struct sr_serial_dev_inst *serial);
1957 int (*write)(struct sr_serial_dev_inst *serial,
1958 const void *buf, size_t count,
1959 int nonblocking, unsigned int timeout_ms);
1960 int (*read)(struct sr_serial_dev_inst *serial,
1961 void *buf, size_t count,
1962 int nonblocking, unsigned int timeout_ms);
1963 int (*set_params)(struct sr_serial_dev_inst *serial,
1964 int baudrate, int bits, int parity, int stopbits,
1965 int flowcontrol, int rts, int dtr);
1966 int (*set_handshake)(struct sr_serial_dev_inst *serial,
1968 int (*setup_source_add)(struct sr_session *session,
1969 struct sr_serial_dev_inst *serial,
1970 int events, int timeout,
1971 sr_receive_data_callback cb, void *cb_data);
1972 int (*setup_source_remove)(struct sr_session *session,
1973 struct sr_serial_dev_inst *serial);
1974 GSList *(*list)(GSList *list, sr_ser_list_append_t append);
1975 GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
1976 uint16_t vendor_id, uint16_t product_id);
1977 int (*get_frame_format)(struct sr_serial_dev_inst *serial,
1978 int *baud, int *bits);
1979 size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
1981 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
1982 SR_PRIV int ser_name_is_hid(struct sr_serial_dev_inst *serial);
1983 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_hid;
1984 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial);
1985 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt;
1987 #ifdef HAVE_LIBHIDAPI
1988 struct vid_pid_item {
1992 struct ser_hid_chip_functions {
1993 const char *chipname;
1994 const char *chipdesc;
1995 const struct vid_pid_item *vid_pid_items;
1996 const int max_bytes_per_request;
1997 int (*set_params)(struct sr_serial_dev_inst *serial,
1998 int baudrate, int bits, int parity, int stopbits,
1999 int flowcontrol, int rts, int dtr);
2000 int (*read_bytes)(struct sr_serial_dev_inst *serial,
2001 uint8_t *data, int space, unsigned int timeout);
2002 int (*write_bytes)(struct sr_serial_dev_inst *serial,
2003 const uint8_t *data, int space);
2004 int (*flush)(struct sr_serial_dev_inst *serial);
2005 int (*drain)(struct sr_serial_dev_inst *serial);
2007 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x;
2008 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325;
2009 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110;
2010 extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor;
2011 SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid);
2015 SR_PRIV int sr_serial_extract_options(GSList *options,
2016 const char **serial_device, const char **serial_options);
2018 /*--- bt/ API ---------------------------------------------------------------*/
2020 #ifdef HAVE_BLUETOOTH
2021 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx);
2024 typedef void (*sr_bt_scan_cb)(void *cb_data, const char *addr, const char *name);
2025 typedef int (*sr_bt_data_cb)(void *cb_data, uint8_t *data, size_t dlen);
2027 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void);
2028 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc);
2030 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
2031 sr_bt_scan_cb cb, void *cb_data);
2032 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
2033 sr_bt_data_cb cb, void *cb_data);
2034 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr);
2035 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr);
2036 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel);
2037 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
2038 uint16_t read_handle, uint16_t write_handle,
2039 uint16_t cccd_handle, uint16_t cccd_value);
2041 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration);
2042 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration);
2044 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc);
2045 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc);
2046 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc);
2048 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc,
2049 void *data, size_t len);
2050 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
2051 const void *data, size_t len);
2053 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc);
2054 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc);
2057 /*--- ezusb.c ---------------------------------------------------------------*/
2059 #ifdef HAVE_LIBUSB_1_0
2060 SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
2061 SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
2063 SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
2064 int configuration, const char *name);
2067 /*--- usb.c -----------------------------------------------------------------*/
2069 #ifdef HAVE_LIBUSB_1_0
2070 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
2071 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
2072 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
2073 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
2074 int timeout, sr_receive_data_callback cb, void *cb_data);
2075 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
2076 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
2077 SR_PRIV gboolean usb_match_manuf_prod(libusb_device *dev,
2078 const char *manufacturer, const char *product);
2081 /*--- binary_helpers.c ------------------------------------------------------*/
2083 /** Binary value type */
2084 enum binary_value_type {
2088 BVT_BE_UINT8 = BVT_UINT8,
2089 BVT_LE_UINT8 = BVT_UINT8,
2102 /** Binary value specification */
2103 struct binary_value_spec {
2104 /** Offset into binary blob */
2106 /** Data type to decode */
2107 enum binary_value_type type;
2108 /** Scale factor to get native units */
2112 /** Binary channel definition */
2113 struct binary_analog_channel {
2116 /** Binary value in data stream */
2117 struct binary_value_spec spec;
2118 /** Significant digits */
2120 /** Measured quantity */
2122 /** Measured unit */
2127 * Read extract a value from a binary blob.
2129 * @param out Pointer to output buffer.
2130 * @param spec Binary value specification
2131 * @param data Pointer to binary blob
2132 * @param length Size of binary blob
2133 * @return SR_OK on success, SR_ERR_* error code on failure.
2135 SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, const void *data, size_t length);
2138 * Send an analog channel packet based on a binary analog channel
2141 * @param sdi Device instance
2142 * @param ch Sigrok channel
2143 * @param spec Channel specification
2144 * @param data Pointer to binary blob
2145 * @param length Size of binary blob
2146 * @return SR_OK on success, SR_ERR_* error code on failure.
2148 SR_PRIV int bv_send_analog_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch,
2149 const struct binary_analog_channel *spec, const void *data, size_t length);
2151 /*--- crc.c -----------------------------------------------------------------*/
2153 #define SR_CRC16_DEFAULT_INIT 0xffffU
2156 * Calculate a CRC16 checksum using the 0x8005 polynomial.
2158 * This CRC16 flavor is also known as CRC16-ANSI or CRC16-MODBUS.
2160 * @param crc Initial value (typically 0xffff)
2161 * @param buffer Input buffer
2162 * @param len Buffer length
2165 SR_PRIV uint16_t sr_crc16(uint16_t crc, const uint8_t *buffer, int len);
2167 /*--- modbus/modbus.c -------------------------------------------------------*/
2169 struct sr_modbus_dev_inst {
2173 GSList *(*scan)(int modbusaddr);
2174 int (*dev_inst_new)(void *priv, const char *resource,
2175 char **params, const char *serialcomm, int modbusaddr);
2176 int (*open)(void *priv);
2177 int (*source_add)(struct sr_session *session, void *priv, int events,
2178 int timeout, sr_receive_data_callback cb, void *cb_data);
2179 int (*source_remove)(struct sr_session *session, void *priv);
2180 int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
2181 int (*read_begin)(void *priv, uint8_t *function_code);
2182 int (*read_data)(void *priv, uint8_t *buf, int maxlen);
2183 int (*read_end)(void *priv);
2184 int (*close)(void *priv);
2185 void (*free)(void *priv);
2186 unsigned int read_timeout_ms;
2190 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
2191 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
2192 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
2193 const char *serialcomm, int modbusaddr);
2194 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
2195 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
2196 struct sr_modbus_dev_inst *modbus, int events, int timeout,
2197 sr_receive_data_callback cb, void *cb_data);
2198 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
2199 struct sr_modbus_dev_inst *modbus);
2200 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
2201 uint8_t *request, int request_size);
2202 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
2203 uint8_t *reply, int reply_size);
2204 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
2205 uint8_t *request, int request_size,
2206 uint8_t *reply, int reply_size);
2207 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
2208 int address, int nb_coils, uint8_t *coils);
2209 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
2210 int address, int nb_registers,
2211 uint16_t *registers);
2212 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
2213 int address, int value);
2214 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
2215 int address, int nb_registers,
2216 uint16_t *registers);
2217 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
2218 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
2220 /*--- dmm/es519xx.c ---------------------------------------------------------*/
2223 * All 11-byte es519xx chips repeat each block twice for each conversion cycle
2224 * so always read 2 blocks at a time.
2226 #define ES519XX_11B_PACKET_SIZE (11 * 2)
2227 #define ES519XX_14B_PACKET_SIZE 14
2229 struct es519xx_info {
2230 gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
2231 gboolean is_milli, is_resistance, is_continuity, is_diode;
2232 gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
2233 gboolean is_temperature, is_celsius, is_fahrenheit;
2234 gboolean is_adp0, is_adp1, is_adp2, is_adp3;
2235 gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
2236 gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
2237 gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
2240 gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
2244 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
2245 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
2246 struct sr_datafeed_analog *analog, void *info);
2247 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
2248 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
2249 float *floatval, struct sr_datafeed_analog *analog, void *info);
2250 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
2251 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
2252 float *floatval, struct sr_datafeed_analog *analog, void *info);
2253 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
2254 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
2255 float *floatval, struct sr_datafeed_analog *analog, void *info);
2256 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
2257 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
2258 struct sr_datafeed_analog *analog, void *info);
2259 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
2260 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
2261 struct sr_datafeed_analog *analog, void *info);
2262 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
2263 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
2264 float *floatval, struct sr_datafeed_analog *analog, void *info);
2266 /*--- dmm/fs9922.c ----------------------------------------------------------*/
2268 #define FS9922_PACKET_SIZE 14
2270 struct fs9922_info {
2271 gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
2272 gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
2273 gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
2274 gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
2275 gboolean is_celsius, is_fahrenheit;
2276 int bargraph_sign, bargraph_value;
2279 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
2280 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
2281 struct sr_datafeed_analog *analog, void *info);
2282 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
2284 /*--- dmm/fs9721.c ----------------------------------------------------------*/
2286 #define FS9721_PACKET_SIZE 14
2288 struct fs9721_info {
2289 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2290 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2291 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2292 gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
2295 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
2296 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
2297 struct sr_datafeed_analog *analog, void *info);
2298 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
2299 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
2300 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
2301 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
2302 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
2304 /*--- dmm/mm38xr.c ---------------------------------------------------------*/
2306 #define METERMAN_38XR_PACKET_SIZE 15
2308 struct meterman_38xr_info { int dummy; };
2310 SR_PRIV gboolean meterman_38xr_packet_valid(const uint8_t *buf);
2311 SR_PRIV int meterman_38xr_parse(const uint8_t *buf, float *floatval,
2312 struct sr_datafeed_analog *analog, void *info);
2314 /*--- dmm/ms2115b.c ---------------------------------------------------------*/
2316 #define MS2115B_PACKET_SIZE 9
2318 enum ms2115b_display {
2319 MS2115B_DISPLAY_MAIN,
2320 MS2115B_DISPLAY_SUB,
2321 MS2115B_DISPLAY_COUNT,
2324 struct ms2115b_info {
2325 /* Selected channel. */
2327 gboolean is_ac, is_dc, is_auto;
2328 gboolean is_diode, is_beep, is_farad;
2329 gboolean is_ohm, is_ampere, is_volt, is_hz;
2330 gboolean is_duty_cycle, is_percent;
2333 extern SR_PRIV const char *ms2115b_channel_formats[];
2334 SR_PRIV gboolean sr_ms2115b_packet_valid(const uint8_t *buf);
2335 SR_PRIV int sr_ms2115b_parse(const uint8_t *buf, float *floatval,
2336 struct sr_datafeed_analog *analog, void *info);
2338 /*--- dmm/ms8250d.c ---------------------------------------------------------*/
2340 #define MS8250D_PACKET_SIZE 18
2342 struct ms8250d_info {
2343 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2344 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2345 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2346 gboolean is_ncv, is_min, is_max, is_sign, is_autotimer;
2349 SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf);
2350 SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
2351 struct sr_datafeed_analog *analog, void *info);
2353 /*--- dmm/dtm0660.c ---------------------------------------------------------*/
2355 #define DTM0660_PACKET_SIZE 15
2357 struct dtm0660_info {
2358 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2359 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2360 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2361 gboolean is_degf, is_degc, is_c2c1_01, is_c2c1_00, is_apo, is_min;
2362 gboolean is_minmax, is_max, is_sign;
2365 SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf);
2366 SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
2367 struct sr_datafeed_analog *analog, void *info);
2369 /*--- dmm/m2110.c -----------------------------------------------------------*/
2371 #define BBCGM_M2110_PACKET_SIZE 9
2373 /* Dummy info struct. The parser does not use it. */
2374 struct m2110_info { int dummy; };
2376 SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
2377 SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
2378 struct sr_datafeed_analog *analog, void *info);
2380 /*--- dmm/metex14.c ---------------------------------------------------------*/
2382 #define METEX14_PACKET_SIZE 14
2384 struct metex14_info {
2386 gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
2387 gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
2388 gboolean is_hertz, is_ohm, is_celsius, is_fahrenheit, is_watt;
2389 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2390 gboolean is_gain, is_decibel, is_power, is_decibel_mw, is_power_factor;
2391 gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
2394 #ifdef HAVE_SERIAL_COMM
2395 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
2397 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
2398 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
2399 struct sr_datafeed_analog *analog, void *info);
2400 SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf);
2401 SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
2402 struct sr_datafeed_analog *analog, void *info);
2404 /*--- dmm/rs9lcd.c ----------------------------------------------------------*/
2406 #define RS9LCD_PACKET_SIZE 9
2408 /* Dummy info struct. The parser does not use it. */
2409 struct rs9lcd_info { int dummy; };
2411 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
2412 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
2413 struct sr_datafeed_analog *analog, void *info);
2415 /*--- dmm/bm25x.c -----------------------------------------------------------*/
2417 #define BRYMEN_BM25X_PACKET_SIZE 15
2419 /* Dummy info struct. The parser does not use it. */
2420 struct bm25x_info { int dummy; };
2422 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
2423 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
2424 struct sr_datafeed_analog *analog, void *info);
2426 /*--- dmm/bm52x.c -----------------------------------------------------------*/
2428 #define BRYMEN_BM52X_PACKET_SIZE 24
2429 #define BRYMEN_BM52X_DISPLAY_COUNT 2
2431 struct brymen_bm52x_info { size_t ch_idx; };
2433 #ifdef HAVE_SERIAL_COMM
2434 SR_PRIV int sr_brymen_bm52x_packet_request(struct sr_serial_dev_inst *serial);
2435 SR_PRIV int sr_brymen_bm82x_packet_request(struct sr_serial_dev_inst *serial);
2437 SR_PRIV gboolean sr_brymen_bm52x_packet_valid(const uint8_t *buf);
2438 SR_PRIV gboolean sr_brymen_bm82x_packet_valid(const uint8_t *buf);
2439 /* BM520s and BM820s protocols are similar, the parse routine is shared. */
2440 SR_PRIV int sr_brymen_bm52x_parse(const uint8_t *buf, float *floatval,
2441 struct sr_datafeed_analog *analog, void *info);
2443 struct brymen_bm52x_state;
2445 SR_PRIV void *brymen_bm52x_state_init(void);
2446 SR_PRIV void brymen_bm52x_state_free(void *state);
2447 SR_PRIV int brymen_bm52x_config_get(void *state, uint32_t key, GVariant **data,
2448 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2449 SR_PRIV int brymen_bm52x_config_set(void *state, uint32_t key, GVariant *data,
2450 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2451 SR_PRIV int brymen_bm52x_config_list(void *state, uint32_t key, GVariant **data,
2452 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2453 SR_PRIV int brymen_bm52x_acquire_start(void *state,
2454 const struct sr_dev_inst *sdi,
2455 sr_receive_data_callback *cb, void **cb_data);
2457 /*--- dmm/bm85x.c -----------------------------------------------------------*/
2459 #define BRYMEN_BM85x_PACKET_SIZE_MIN 4
2461 struct brymen_bm85x_info { int dummy; };
2463 #ifdef HAVE_SERIAL_COMM
2464 SR_PRIV int brymen_bm85x_after_open(struct sr_serial_dev_inst *serial);
2465 SR_PRIV int brymen_bm85x_packet_request(struct sr_serial_dev_inst *serial);
2467 SR_PRIV gboolean brymen_bm85x_packet_valid(void *state,
2468 const uint8_t *buf, size_t len, size_t *pkt_len);
2469 SR_PRIV int brymen_bm85x_parse(void *state, const uint8_t *buf, size_t len,
2470 double *floatval, struct sr_datafeed_analog *analog, void *info);
2472 /*--- dmm/bm86x.c -----------------------------------------------------------*/
2474 #define BRYMEN_BM86X_PACKET_SIZE 24
2475 #define BRYMEN_BM86X_DISPLAY_COUNT 2
2477 struct brymen_bm86x_info { size_t ch_idx; };
2479 #ifdef HAVE_SERIAL_COMM
2480 SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial);
2482 SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf);
2483 SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *floatval,
2484 struct sr_datafeed_analog *analog, void *info);
2486 /*--- dmm/ut71x.c -----------------------------------------------------------*/
2488 #define UT71X_PACKET_SIZE 11
2491 gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
2492 gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
2493 gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
2494 gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
2497 SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
2498 SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
2499 struct sr_datafeed_analog *analog, void *info);
2501 /*--- dmm/vc870.c -----------------------------------------------------------*/
2503 #define VC870_PACKET_SIZE 23
2506 gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
2507 gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
2508 gboolean is_current, is_micro, is_milli, is_power;
2509 gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_rms_value;
2510 gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
2511 gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
2512 gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
2513 gboolean is_lo, is_hi, is_open2;
2515 gboolean is_frequency, is_dual_display, is_auto;
2518 SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
2519 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
2520 struct sr_datafeed_analog *analog, void *info);
2522 /*--- dmm/vc96.c ------------------------------------------------------------*/
2524 #define VC96_PACKET_SIZE 13
2528 gboolean is_ac, is_dc, is_resistance, is_diode, is_ampere, is_volt;
2529 gboolean is_ohm, is_micro, is_milli, is_kilo, is_mega, is_hfe;
2530 gboolean is_unitless;
2533 SR_PRIV gboolean sr_vc96_packet_valid(const uint8_t *buf);
2534 SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval,
2535 struct sr_datafeed_analog *analog, void *info);
2537 /*--- lcr/es51919.c ---------------------------------------------------------*/
2539 /* Acquisition details which apply to all supported serial-lcr devices. */
2540 struct lcr_parse_info {
2542 uint64_t output_freq;
2543 const char *circuit_model;
2546 #define ES51919_PACKET_SIZE 17
2547 #define ES51919_CHANNEL_COUNT 2
2548 #define ES51919_COMM_PARAM "9600/8n1/rts=1/dtr=1"
2550 SR_PRIV int es51919_config_get(uint32_t key, GVariant **data,
2551 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2552 SR_PRIV int es51919_config_set(uint32_t key, GVariant *data,
2553 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2554 SR_PRIV int es51919_config_list(uint32_t key, GVariant **data,
2555 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2556 SR_PRIV gboolean es51919_packet_valid(const uint8_t *pkt);
2557 SR_PRIV int es51919_packet_parse(const uint8_t *pkt, float *floatval,
2558 struct sr_datafeed_analog *analog, void *info);
2560 /*--- lcr/vc4080.c ----------------------------------------------------------*/
2562 /* Note: Also uses 'struct lcr_parse_info' from es51919 above. */
2564 #define VC4080_PACKET_SIZE 39
2565 #define VC4080_COMM_PARAM "1200/8n1"
2566 #define VC4080_WITH_DQ_CHANS 0 /* Enable separate D/Q channels? */
2568 enum vc4080_display {
2569 VC4080_DISPLAY_PRIMARY,
2570 VC4080_DISPLAY_SECONDARY,
2571 #if VC4080_WITH_DQ_CHANS
2572 VC4080_DISPLAY_D_VALUE,
2573 VC4080_DISPLAY_Q_VALUE,
2575 VC4080_CHANNEL_COUNT,
2578 extern SR_PRIV const char *vc4080_channel_formats[VC4080_CHANNEL_COUNT];
2580 SR_PRIV int vc4080_config_list(uint32_t key, GVariant **data,
2581 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2582 SR_PRIV int vc4080_packet_request(struct sr_serial_dev_inst *serial);
2583 SR_PRIV gboolean vc4080_packet_valid(const uint8_t *pkt);
2584 SR_PRIV int vc4080_packet_parse(const uint8_t *pkt, float *floatval,
2585 struct sr_datafeed_analog *analog, void *info);
2587 /*--- dmm/ut372.c -----------------------------------------------------------*/
2589 #define UT372_PACKET_SIZE 27
2595 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
2596 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
2597 struct sr_datafeed_analog *analog, void *info);
2599 /*--- dmm/asycii.c ----------------------------------------------------------*/
2601 #define ASYCII_PACKET_SIZE 16
2603 struct asycii_info {
2604 gboolean is_ac, is_dc, is_ac_and_dc;
2605 gboolean is_resistance, is_capacitance, is_diode, is_gain;
2606 gboolean is_frequency, is_duty_cycle, is_duty_pos, is_duty_neg;
2607 gboolean is_pulse_width, is_period_pos, is_period_neg;
2608 gboolean is_pulse_count, is_count_pos, is_count_neg;
2609 gboolean is_ampere, is_volt, is_volt_ampere, is_farad, is_ohm;
2610 gboolean is_hertz, is_percent, is_seconds, is_decibel;
2611 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2612 gboolean is_unitless;
2613 gboolean is_peak_min, is_peak_max;
2614 gboolean is_invalid;
2617 #ifdef HAVE_SERIAL_COMM
2618 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
2620 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);
2621 SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
2622 struct sr_datafeed_analog *analog, void *info);
2624 /*--- dmm/eev121gw.c --------------------------------------------------------*/
2626 #define EEV121GW_PACKET_SIZE 19
2628 enum eev121gw_display {
2629 EEV121GW_DISPLAY_MAIN,
2630 EEV121GW_DISPLAY_SUB,
2631 EEV121GW_DISPLAY_BAR,
2632 EEV121GW_DISPLAY_COUNT,
2635 struct eev121gw_info {
2636 /* Selected channel. */
2639 * Measured value, number and sign/overflow flags, scale factor
2640 * and significant digits.
2642 uint32_t uint_value;
2643 gboolean is_ofl, is_neg;
2645 /* Currently active mode (meter's function). */
2646 gboolean is_ac, is_dc, is_voltage, is_current, is_power, is_gain;
2647 gboolean is_resistance, is_capacitance, is_diode, is_temperature;
2648 gboolean is_continuity, is_frequency, is_period, is_duty_cycle;
2649 /* Quantities associated with mode/function. */
2650 gboolean is_ampere, is_volt, is_volt_ampere, is_dbm;
2651 gboolean is_ohm, is_farad, is_celsius, is_fahrenheit;
2652 gboolean is_hertz, is_seconds, is_percent, is_loop_current;
2653 gboolean is_unitless, is_logic;
2654 /* Other indicators. */
2655 gboolean is_min, is_max, is_avg, is_1ms_peak, is_rel, is_hold;
2656 gboolean is_low_pass, is_mem, is_bt, is_auto_range, is_test;
2657 gboolean is_auto_poweroff, is_low_batt;
2660 extern SR_PRIV const char *eev121gw_channel_formats[];
2661 SR_PRIV gboolean sr_eev121gw_packet_valid(const uint8_t *buf);
2662 SR_PRIV int sr_eev121gw_3displays_parse(const uint8_t *buf, float *floatval,
2663 struct sr_datafeed_analog *analog, void *info);
2665 /*--- scale/kern.c ----------------------------------------------------------*/
2668 gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
2669 gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
2670 gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
2674 SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
2675 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
2676 struct sr_datafeed_analog *analog, void *info);
2678 /*--- sw_limits.c -----------------------------------------------------------*/
2680 struct sr_sw_limits {
2681 uint64_t limit_samples;
2682 uint64_t limit_frames;
2683 uint64_t limit_msec;
2684 uint64_t samples_read;
2685 uint64_t frames_read;
2686 uint64_t start_time;
2689 SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits, uint32_t key,
2691 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
2693 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
2694 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
2695 SR_PRIV int sr_sw_limits_get_remain(const struct sr_sw_limits *limits,
2696 uint64_t *samples, uint64_t *frames, uint64_t *msecs,
2697 gboolean *exceeded);
2698 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
2699 uint64_t samples_read);
2700 SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
2701 uint64_t frames_read);
2702 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
2704 /*--- feed_queue.h ----------------------------------------------------------*/
2706 struct feed_queue_logic;
2707 struct feed_queue_analog;
2709 SR_API struct feed_queue_logic *feed_queue_logic_alloc(
2710 const struct sr_dev_inst *sdi,
2711 size_t sample_count, size_t unit_size);
2712 SR_API int feed_queue_logic_submit(struct feed_queue_logic *q,
2713 const uint8_t *data, size_t count);
2714 SR_API int feed_queue_logic_flush(struct feed_queue_logic *q);
2715 SR_API int feed_queue_logic_send_trigger(struct feed_queue_logic *q);
2716 SR_API void feed_queue_logic_free(struct feed_queue_logic *q);
2718 SR_API struct feed_queue_analog *feed_queue_analog_alloc(
2719 const struct sr_dev_inst *sdi,
2720 size_t sample_count, int digits, struct sr_channel *ch);
2721 SR_API int feed_queue_analog_submit(struct feed_queue_analog *q,
2722 float data, size_t count);
2723 SR_API int feed_queue_analog_flush(struct feed_queue_analog *q);
2724 SR_API void feed_queue_analog_free(struct feed_queue_analog *q);