]> sigrok.org Git - libsigrok.git/blob - src/libsigrok-internal.h
serial: prepare alternative transports for serial communication
[libsigrok.git] / src / libsigrok-internal.h
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @file
22  *
23  * @internal
24  */
25
26 #ifndef LIBSIGROK_LIBSIGROK_INTERNAL_H
27 #define LIBSIGROK_LIBSIGROK_INTERNAL_H
28
29 #include <glib.h>
30 #ifdef HAVE_LIBSERIALPORT
31 #include <libserialport.h>
32 #endif
33 #ifdef HAVE_LIBUSB_1_0
34 #include <libusb.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 struct zip;
41 struct zip_stat;
42
43 /**
44  * @file
45  *
46  * libsigrok private header file, only to be used internally.
47  */
48
49 /*--- Macros ----------------------------------------------------------------*/
50
51 #ifndef ARRAY_SIZE
52 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
53 #endif
54
55 #ifndef ARRAY_AND_SIZE
56 #define ARRAY_AND_SIZE(a) (a), ARRAY_SIZE(a)
57 #endif
58
59 /**
60  * Read a 8 bits unsigned integer out of memory.
61  * @param x a pointer to the input memory
62  * @return the corresponding unsigned integer
63  */
64 #define R8(x)     ((unsigned)((const uint8_t*)(x))[0])
65
66 /**
67  * Read a 16 bits big endian unsigned integer out of memory.
68  * @param x a pointer to the input memory
69  * @return the corresponding unsigned integer
70  */
71 #define RB16(x)  (((unsigned)((const uint8_t*)(x))[0] <<  8) | \
72                    (unsigned)((const uint8_t*)(x))[1])
73
74 /**
75  * Read a 16 bits little endian unsigned integer out of memory.
76  * @param x a pointer to the input memory
77  * @return the corresponding unsigned integer
78  */
79 #define RL16(x)  (((unsigned)((const uint8_t*)(x))[1] <<  8) | \
80                    (unsigned)((const uint8_t*)(x))[0])
81
82 /**
83  * Read a 16 bits big endian signed integer out of memory.
84  * @param x a pointer to the input memory
85  * @return the corresponding signed integer
86  */
87 #define RB16S(x)  ((int16_t) \
88                   (((unsigned)((const uint8_t*)(x))[0] <<  8) | \
89                     (unsigned)((const uint8_t*)(x))[1]))
90
91 /**
92  * Read a 16 bits little endian signed integer out of memory.
93  * @param x a pointer to the input memory
94  * @return the corresponding signed integer
95  */
96 #define RL16S(x)  ((int16_t) \
97                   (((unsigned)((const uint8_t*)(x))[1] <<  8) | \
98                     (unsigned)((const uint8_t*)(x))[0]))
99
100 /**
101  * Read a 32 bits big endian unsigned integer out of memory.
102  * @param x a pointer to the input memory
103  * @return the corresponding unsigned integer
104  */
105 #define RB32(x)  (((unsigned)((const uint8_t*)(x))[0] << 24) | \
106                   ((unsigned)((const uint8_t*)(x))[1] << 16) | \
107                   ((unsigned)((const uint8_t*)(x))[2] <<  8) | \
108                    (unsigned)((const uint8_t*)(x))[3])
109
110 /**
111  * Read a 32 bits little endian unsigned integer out of memory.
112  * @param x a pointer to the input memory
113  * @return the corresponding unsigned integer
114  */
115 #define RL32(x)  (((unsigned)((const uint8_t*)(x))[3] << 24) | \
116                   ((unsigned)((const uint8_t*)(x))[2] << 16) | \
117                   ((unsigned)((const uint8_t*)(x))[1] <<  8) | \
118                    (unsigned)((const uint8_t*)(x))[0])
119
120 /**
121  * Read a 32 bits big endian signed integer out of memory.
122  * @param x a pointer to the input memory
123  * @return the corresponding signed integer
124  */
125 #define RB32S(x)  ((int32_t) \
126                  (((unsigned)((const uint8_t*)(x))[0] << 24) | \
127                   ((unsigned)((const uint8_t*)(x))[1] << 16) | \
128                   ((unsigned)((const uint8_t*)(x))[2] <<  8) | \
129                    (unsigned)((const uint8_t*)(x))[3]))
130
131 /**
132  * Read a 32 bits little endian signed integer out of memory.
133  * @param x a pointer to the input memory
134  * @return the corresponding signed integer
135  */
136 #define RL32S(x)  ((int32_t) \
137                  (((unsigned)((const uint8_t*)(x))[3] << 24) | \
138                   ((unsigned)((const uint8_t*)(x))[2] << 16) | \
139                   ((unsigned)((const uint8_t*)(x))[1] <<  8) | \
140                    (unsigned)((const uint8_t*)(x))[0]))
141
142 /**
143  * Read a 64 bits big endian unsigned integer out of memory.
144  * @param x a pointer to the input memory
145  * @return the corresponding unsigned integer
146  */
147 #define RB64(x)  (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
148                   ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
149                   ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
150                   ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
151                   ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
152                   ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
153                   ((uint64_t)((const uint8_t*)(x))[6] <<  8) | \
154                    (uint64_t)((const uint8_t*)(x))[7])
155
156 /**
157  * Read a 64 bits little endian unsigned integer out of memory.
158  * @param x a pointer to the input memory
159  * @return the corresponding unsigned integer
160  */
161 #define RL64(x)  (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
162                   ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
163                   ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
164                   ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
165                   ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
166                   ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
167                   ((uint64_t)((const uint8_t*)(x))[1] <<  8) | \
168                    (uint64_t)((const uint8_t*)(x))[0])
169
170 /**
171  * Read a 64 bits little endian signed integer out of memory.
172  * @param x a pointer to the input memory
173  * @return the corresponding unsigned integer
174  */
175 #define RL64S(x)  ((int64_t) \
176                  (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
177                   ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
178                   ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
179                   ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
180                   ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
181                   ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
182                   ((uint64_t)((const uint8_t*)(x))[1] <<  8) | \
183                    (uint64_t)((const uint8_t*)(x))[0]))
184
185 /**
186  * Read a 32 bits big endian float out of memory.
187  * @param x a pointer to the input memory
188  * @return the corresponding float
189  */
190 #define RBFL(x)  ((union { uint32_t u; float f; }) { .u = RB32(x) }.f)
191
192 /**
193  * Read a 32 bits little endian float out of memory.
194  * @param x a pointer to the input memory
195  * @return the corresponding float
196  */
197 #define RLFL(x)  ((union { uint32_t u; float f; }) { .u = RL32(x) }.f)
198
199 /**
200  * Write a 8 bits unsigned integer to memory.
201  * @param p a pointer to the output memory
202  * @param x the input unsigned integer
203  */
204 #define W8(p, x)    do { ((uint8_t*)(p))[0] = (uint8_t) (x);      } while (0)
205
206 /**
207  * Write a 16 bits unsigned integer to memory stored as big endian.
208  * @param p a pointer to the output memory
209  * @param x the input unsigned integer
210  */
211 #define WB16(p, x)  do { ((uint8_t*)(p))[1] = (uint8_t) (x);      \
212                          ((uint8_t*)(p))[0] = (uint8_t)((x)>>8);  } while (0)
213
214 /**
215  * Write a 16 bits unsigned integer to memory stored as little endian.
216  * @param p a pointer to the output memory
217  * @param x the input unsigned integer
218  */
219 #define WL16(p, x)  do { ((uint8_t*)(p))[0] = (uint8_t) (x);      \
220                          ((uint8_t*)(p))[1] = (uint8_t)((x)>>8);  } while (0)
221
222 /**
223  * Write a 32 bits unsigned integer to memory stored as big endian.
224  * @param p a pointer to the output memory
225  * @param x the input unsigned integer
226  */
227 #define WB32(p, x)  do { ((uint8_t*)(p))[3] = (uint8_t) (x);      \
228                          ((uint8_t*)(p))[2] = (uint8_t)((x)>>8);  \
229                          ((uint8_t*)(p))[1] = (uint8_t)((x)>>16); \
230                          ((uint8_t*)(p))[0] = (uint8_t)((x)>>24); } while (0)
231
232 /**
233  * Write a 32 bits unsigned integer to memory stored as little endian.
234  * @param p a pointer to the output memory
235  * @param x the input unsigned integer
236  */
237 #define WL32(p, x)  do { ((uint8_t*)(p))[0] = (uint8_t) (x);      \
238                          ((uint8_t*)(p))[1] = (uint8_t)((x)>>8);  \
239                          ((uint8_t*)(p))[2] = (uint8_t)((x)>>16); \
240                          ((uint8_t*)(p))[3] = (uint8_t)((x)>>24); } while (0)
241
242 /**
243  * Write a 32 bits float to memory stored as big endian.
244  * @param p a pointer to the output memory
245  * @param x the input float
246  */
247 #define WBFL(p, x)  WB32(p, (union { uint32_t u; float f; }) { .f = x }.u)
248
249 /**
250  * Write a 32 bits float to memory stored as little endian.
251  * @param p a pointer to the output memory
252  * @param x the input float
253  */
254 #define WLFL(p, x)  WL32(p, (union { uint32_t u; float f; }) { .f = x }.u)
255
256 /* Portability fixes for FreeBSD. */
257 #ifdef __FreeBSD__
258 #define LIBUSB_CLASS_APPLICATION 0xfe
259 #define libusb_has_capability(x) 0
260 #define libusb_handle_events_timeout_completed(ctx, tv, c) \
261         libusb_handle_events_timeout(ctx, tv)
262 #endif
263
264 /* Static definitions of structs ending with an all-zero entry are a
265  * problem when compiling with -Wmissing-field-initializers: GCC
266  * suppresses the warning only with { 0 }, clang wants { } */
267 #ifdef __clang__
268 #define ALL_ZERO { }
269 #else
270 #define ALL_ZERO { 0 }
271 #endif
272
273 #ifdef __APPLE__
274 #define SR_DRIVER_LIST_SECTION "__DATA,__sr_driver_list"
275 #else
276 #define SR_DRIVER_LIST_SECTION "sr_driver_list"
277 #endif
278
279 /**
280  * Register a list of hardware drivers.
281  *
282  * This macro can be used to register multiple hardware drivers to the library.
283  * This is useful when a driver supports multiple similar but slightly
284  * different devices that require different sr_dev_driver struct definitions.
285  *
286  * For registering only a single driver see SR_REGISTER_DEV_DRIVER().
287  *
288  * Example:
289  * @code{c}
290  * #define MY_DRIVER(_name) \
291  *     &(struct sr_dev_driver){ \
292  *         .name = _name, \
293  *         ...
294  *     };
295  *
296  * SR_REGISTER_DEV_DRIVER_LIST(my_driver_infos,
297  *     MY_DRIVER("driver 1"),
298  *     MY_DRIVER("driver 2"),
299  *     ...
300  * );
301  * @endcode
302  *
303  * @param name Name to use for the driver list identifier.
304  * @param ... Comma separated list of pointers to sr_dev_driver structs.
305  */
306 #define SR_REGISTER_DEV_DRIVER_LIST(name, ...) \
307         static const struct sr_dev_driver *name[] \
308                 __attribute__((section (SR_DRIVER_LIST_SECTION), used, \
309                         aligned(sizeof(struct sr_dev_driver *)))) \
310                 = { \
311                         __VA_ARGS__ \
312                 };
313
314 /**
315  * Register a hardware driver.
316  *
317  * This macro is used to register a hardware driver with the library. It has
318  * to be used in order to make the driver accessible to applications using the
319  * library.
320  *
321  * The macro invocation should be placed directly under the struct
322  * sr_dev_driver definition.
323  *
324  * Example:
325  * @code{c}
326  * static struct sr_dev_driver driver_info = {
327  *     .name = "driver",
328  *     ....
329  * };
330  * SR_REGISTER_DEV_DRIVER(driver_info);
331  * @endcode
332  *
333  * @param name Identifier name of sr_dev_driver struct to register.
334  */
335 #define SR_REGISTER_DEV_DRIVER(name) \
336         SR_REGISTER_DEV_DRIVER_LIST(name##_list, &name);
337
338 SR_API void sr_drivers_init(struct sr_context *context);
339
340 struct sr_context {
341         struct sr_dev_driver **driver_list;
342 #ifdef HAVE_LIBUSB_1_0
343         libusb_context *libusb_ctx;
344 #endif
345         sr_resource_open_callback resource_open_cb;
346         sr_resource_close_callback resource_close_cb;
347         sr_resource_read_callback resource_read_cb;
348         void *resource_cb_data;
349 };
350
351 /** Input module metadata keys. */
352 enum sr_input_meta_keys {
353         /** The input filename, if there is one. */
354         SR_INPUT_META_FILENAME = 0x01,
355         /** The input file's size in bytes. */
356         SR_INPUT_META_FILESIZE = 0x02,
357         /** The first 128 bytes of the file, provided as a GString. */
358         SR_INPUT_META_HEADER = 0x04,
359
360         /** The module cannot identify a file without this metadata. */
361         SR_INPUT_META_REQUIRED = 0x80,
362 };
363
364 /** Input (file) module struct. */
365 struct sr_input {
366         /**
367          * A pointer to this input module's 'struct sr_input_module'.
368          */
369         const struct sr_input_module *module;
370         GString *buf;
371         struct sr_dev_inst *sdi;
372         gboolean sdi_ready;
373         void *priv;
374 };
375
376 /** Input (file) module driver. */
377 struct sr_input_module {
378         /**
379          * A unique ID for this input module, suitable for use in command-line
380          * clients, [a-z0-9-]. Must not be NULL.
381          */
382         const char *id;
383
384         /**
385          * A unique name for this input module, suitable for use in GUI
386          * clients, can contain UTF-8. Must not be NULL.
387          */
388         const char *name;
389
390         /**
391          * A short description of the input module. Must not be NULL.
392          *
393          * This can be displayed by frontends, e.g. when selecting the input
394          * module for saving a file.
395          */
396         const char *desc;
397
398         /**
399          * A NULL terminated array of strings containing a list of file name
400          * extensions typical for the input file format, or NULL if there is
401          * no typical extension for this file format.
402          */
403         const char *const *exts;
404
405         /**
406          * Zero-terminated list of metadata items the module needs to be able
407          * to identify an input stream. Can be all-zero, if the module cannot
408          * identify streams at all, i.e. has to be forced into use.
409          *
410          * Each item is one of:
411          *   SR_INPUT_META_FILENAME
412          *   SR_INPUT_META_FILESIZE
413          *   SR_INPUT_META_HEADER
414          *
415          * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
416          * identify a stream without the given metadata.
417          */
418         const uint8_t metadata[8];
419
420         /**
421          * Returns a NULL-terminated list of options this module can take.
422          * Can be NULL, if the module has no options.
423          */
424         const struct sr_option *(*options) (void);
425
426         /**
427          * Check if this input module can load and parse the specified stream.
428          *
429          * @param[in] metadata Metadata the module can use to identify the stream.
430          * @param[out] confidence "Strength" of the detection.
431          *   Specialized handlers can take precedence over generic/basic support.
432          *
433          * @retval SR_OK This module knows the format.
434          * @retval SR_ERR_NA There wasn't enough data for this module to
435          *   positively identify the format.
436          * @retval SR_ERR_DATA This module knows the format, but cannot handle
437          *   it. This means the stream is either corrupt, or indicates a
438          *   feature that the module does not support.
439          * @retval SR_ERR This module does not know the format.
440          *
441          * Lower numeric values of 'confidence' mean that the input module
442          * stronger believes in its capability to handle this specific format.
443          * This way, multiple input modules can claim support for a format,
444          * and the application can pick the best match, or try fallbacks
445          * in case of errors. This approach also copes with formats that
446          * are unreliable to detect in the absence of magic signatures.
447          */
448         int (*format_match) (GHashTable *metadata, unsigned int *confidence);
449
450         /**
451          * Initialize the input module.
452          *
453          * @retval SR_OK Success
454          * @retval other Negative error code.
455          */
456         int (*init) (struct sr_input *in, GHashTable *options);
457
458         /**
459          * Send data to the specified input instance.
460          *
461          * When an input module instance is created with sr_input_new(), this
462          * function is used to feed data to the instance.
463          *
464          * As enough data gets fed into this function to completely populate
465          * the device instance associated with this input instance, this is
466          * guaranteed to return the moment it's ready. This gives the caller
467          * the chance to examine the device instance, attach session callbacks
468          * and so on.
469          *
470          * @retval SR_OK Success
471          * @retval other Negative error code.
472          */
473         int (*receive) (struct sr_input *in, GString *buf);
474
475         /**
476          * Signal the input module no more data will come.
477          *
478          * This will cause the module to process any data it may have buffered.
479          * The SR_DF_END packet will also typically be sent at this time.
480          */
481         int (*end) (struct sr_input *in);
482
483         /**
484          * Reset the input module's input handling structures.
485          *
486          * Causes the input module to reset its internal state so that we can
487          * re-send the input data from the beginning without having to
488          * re-create the entire input module.
489          *
490          * @retval SR_OK Success.
491          * @retval other Negative error code.
492          */
493         int (*reset) (struct sr_input *in);
494
495         /**
496          * This function is called after the caller is finished using
497          * the input module, and can be used to free any internal
498          * resources the module may keep.
499          *
500          * This function is optional.
501          *
502          * @retval SR_OK Success
503          * @retval other Negative error code.
504          */
505         void (*cleanup) (struct sr_input *in);
506 };
507
508 /** Output module instance. */
509 struct sr_output {
510         /** A pointer to this output's module. */
511         const struct sr_output_module *module;
512
513         /**
514          * The device for which this output module is creating output. This
515          * can be used by the module to find out channel names and numbers.
516          */
517         const struct sr_dev_inst *sdi;
518
519         /**
520          * The name of the file that the data should be written to.
521          */
522         const char *filename;
523
524         /**
525          * A generic pointer which can be used by the module to keep internal
526          * state between calls into its callback functions.
527          *
528          * For example, the module might store a pointer to a chunk of output
529          * there, and only flush it when it reaches a certain size.
530          */
531         void *priv;
532 };
533
534 /** Output module driver. */
535 struct sr_output_module {
536         /**
537          * A unique ID for this output module, suitable for use in command-line
538          * clients, [a-z0-9-]. Must not be NULL.
539          */
540         const char *id;
541
542         /**
543          * A unique name for this output module, suitable for use in GUI
544          * clients, can contain UTF-8. Must not be NULL.
545          */
546         const char *name;
547
548         /**
549          * A short description of the output module. Must not be NULL.
550          *
551          * This can be displayed by frontends, e.g. when selecting the output
552          * module for saving a file.
553          */
554         const char *desc;
555
556         /**
557          * A NULL terminated array of strings containing a list of file name
558          * extensions typical for the input file format, or NULL if there is
559          * no typical extension for this file format.
560          */
561         const char *const *exts;
562
563         /**
564          * Bitfield containing flags that describe certain properties
565          * this output module may or may not have.
566          * @see sr_output_flags
567          */
568         const uint64_t flags;
569
570         /**
571          * Returns a NULL-terminated list of options this module can take.
572          * Can be NULL, if the module has no options.
573          */
574         const struct sr_option *(*options) (void);
575
576         /**
577          * This function is called once, at the beginning of an output stream.
578          *
579          * The device struct will be available in the output struct passed in,
580          * as well as the param field -- which may be NULL or an empty string,
581          * if no parameter was passed.
582          *
583          * The module can use this to initialize itself, create a struct for
584          * keeping state and storing it in the <code>internal</code> field.
585          *
586          * @param o Pointer to the respective 'struct sr_output'.
587          *
588          * @retval SR_OK Success
589          * @retval other Negative error code.
590          */
591         int (*init) (struct sr_output *o, GHashTable *options);
592
593         /**
594          * This function is passed a copy of every packet in the data feed.
595          * Any output generated by the output module in response to the
596          * packet should be returned in a newly allocated GString
597          * <code>out</code>, which will be freed by the caller.
598          *
599          * Packets not of interest to the output module can just be ignored,
600          * and the <code>out</code> parameter set to NULL.
601          *
602          * @param o Pointer to the respective 'struct sr_output'.
603          * @param sdi The device instance that generated the packet.
604          * @param packet The complete packet.
605          * @param out A pointer where a GString * should be stored if
606          * the module generates output, or NULL if not.
607          *
608          * @retval SR_OK Success
609          * @retval other Negative error code.
610          */
611         int (*receive) (const struct sr_output *o,
612                         const struct sr_datafeed_packet *packet, GString **out);
613
614         /**
615          * This function is called after the caller is finished using
616          * the output module, and can be used to free any internal
617          * resources the module may keep.
618          *
619          * @retval SR_OK Success
620          * @retval other Negative error code.
621          */
622         int (*cleanup) (struct sr_output *o);
623 };
624
625 /** Transform module instance. */
626 struct sr_transform {
627         /** A pointer to this transform's module. */
628         const struct sr_transform_module *module;
629
630         /**
631          * The device for which this transform module is used. This
632          * can be used by the module to find out channel names and numbers.
633          */
634         const struct sr_dev_inst *sdi;
635
636         /**
637          * A generic pointer which can be used by the module to keep internal
638          * state between calls into its callback functions.
639          */
640         void *priv;
641 };
642
643 struct sr_transform_module {
644         /**
645          * A unique ID for this transform module, suitable for use in
646          * command-line clients, [a-z0-9-]. Must not be NULL.
647          */
648         const char *id;
649
650         /**
651          * A unique name for this transform module, suitable for use in GUI
652          * clients, can contain UTF-8. Must not be NULL.
653          */
654         const char *name;
655
656         /**
657          * A short description of the transform module. Must not be NULL.
658          *
659          * This can be displayed by frontends, e.g. when selecting
660          * which transform module(s) to add.
661          */
662         const char *desc;
663
664         /**
665          * Returns a NULL-terminated list of options this transform module
666          * can take. Can be NULL, if the transform module has no options.
667          */
668         const struct sr_option *(*options) (void);
669
670         /**
671          * This function is called once, at the beginning of a stream.
672          *
673          * @param t Pointer to the respective 'struct sr_transform'.
674          * @param options Hash table of options for this transform module.
675          *                Can be NULL if no options are to be used.
676          *
677          * @retval SR_OK Success
678          * @retval other Negative error code.
679          */
680         int (*init) (struct sr_transform *t, GHashTable *options);
681
682         /**
683          * This function is passed a pointer to every packet in the data feed.
684          *
685          * It can either return (in packet_out) a pointer to another packet
686          * (possibly the exact same packet it got as input), or NULL.
687          *
688          * @param t Pointer to the respective 'struct sr_transform'.
689          * @param packet_in Pointer to a datafeed packet.
690          * @param packet_out Pointer to the resulting datafeed packet after
691          *                   this function was run. If NULL, the transform
692          *                   module intentionally didn't output a new packet.
693          *
694          * @retval SR_OK Success
695          * @retval other Negative error code.
696          */
697         int (*receive) (const struct sr_transform *t,
698                         struct sr_datafeed_packet *packet_in,
699                         struct sr_datafeed_packet **packet_out);
700
701         /**
702          * This function is called after the caller is finished using
703          * the transform module, and can be used to free any internal
704          * resources the module may keep.
705          *
706          * @retval SR_OK Success
707          * @retval other Negative error code.
708          */
709         int (*cleanup) (struct sr_transform *t);
710 };
711
712 #ifdef HAVE_LIBUSB_1_0
713 /** USB device instance */
714 struct sr_usb_dev_inst {
715         /** USB bus */
716         uint8_t bus;
717         /** Device address on USB bus */
718         uint8_t address;
719         /** libusb device handle */
720         struct libusb_device_handle *devhdl;
721 };
722 #endif
723
724 #ifdef HAVE_LIBSERIALPORT
725 struct ser_lib_functions;
726 struct sr_serial_dev_inst {
727         /** Port name, e.g. '/dev/tty42'. */
728         char *port;
729         /** Comm params for serial_set_paramstr(). */
730         char *serialcomm;
731         struct ser_lib_functions *lib_funcs;
732         struct {
733                 int bit_rate;
734                 int data_bits;
735                 int parity_bits;
736                 int stop_bits;
737         } comm_params;
738 #ifdef HAVE_LIBSERIALPORT
739         /** libserialport port handle */
740         struct sp_port *sp_data;
741 #endif
742 };
743 #endif
744
745 struct sr_usbtmc_dev_inst {
746         char *device;
747         int fd;
748 };
749
750 /* Private driver context. */
751 struct drv_context {
752         /** sigrok context */
753         struct sr_context *sr_ctx;
754         GSList *instances;
755 };
756
757 /*--- log.c -----------------------------------------------------------------*/
758
759 #if defined(_WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
760 /*
761  * On MinGW, we need to specify the gnu_printf format flavor or GCC
762  * will assume non-standard Microsoft printf syntax.
763  */
764 SR_PRIV int sr_log(int loglevel, const char *format, ...)
765                 __attribute__((__format__ (__gnu_printf__, 2, 3)));
766 #else
767 SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
768 #endif
769
770 /* Message logging helpers with subsystem-specific prefix string. */
771 #define sr_spew(...)    sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
772 #define sr_dbg(...)     sr_log(SR_LOG_DBG,  LOG_PREFIX ": " __VA_ARGS__)
773 #define sr_info(...)    sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
774 #define sr_warn(...)    sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
775 #define sr_err(...)     sr_log(SR_LOG_ERR,  LOG_PREFIX ": " __VA_ARGS__)
776
777 /*--- device.c --------------------------------------------------------------*/
778
779 /** Scan options supported by a driver. */
780 #define SR_CONF_SCAN_OPTIONS 0x7FFF0000
781
782 /** Device options for a particular device. */
783 #define SR_CONF_DEVICE_OPTIONS 0x7FFF0001
784
785 /** Mask for separating config keys from capabilities. */
786 #define SR_CONF_MASK 0x1fffffff
787
788 /** Values for the changes argument of sr_dev_driver.config_channel_set. */
789 enum {
790         /** The enabled state of the channel has been changed. */
791         SR_CHANNEL_SET_ENABLED = 1 << 0,
792 };
793
794 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
795                 int index, int type, gboolean enabled, const char *name);
796 SR_PRIV void sr_channel_free(struct sr_channel *ch);
797 SR_PRIV void sr_channel_free_cb(void *p);
798 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
799                 struct sr_channel *cur_channel);
800 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2);
801 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2);
802
803 /** Device instance data */
804 struct sr_dev_inst {
805         /** Device driver. */
806         struct sr_dev_driver *driver;
807         /** Device instance status. SR_ST_NOT_FOUND, etc. */
808         int status;
809         /** Device instance type. SR_INST_USB, etc. */
810         int inst_type;
811         /** Device vendor. */
812         char *vendor;
813         /** Device model. */
814         char *model;
815         /** Device version. */
816         char *version;
817         /** Serial number. */
818         char *serial_num;
819         /** Connection string to uniquely identify devices. */
820         char *connection_id;
821         /** List of channels. */
822         GSList *channels;
823         /** List of sr_channel_group structs */
824         GSList *channel_groups;
825         /** Device instance connection data (used?) */
826         void *conn;
827         /** Device instance private data (used?) */
828         void *priv;
829         /** Session to which this device is currently assigned. */
830         struct sr_session *session;
831 };
832
833 /* Generic device instances */
834 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
835
836 #ifdef HAVE_LIBUSB_1_0
837 /* USB-specific instances */
838 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
839                 uint8_t address, struct libusb_device_handle *hdl);
840 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
841 #endif
842
843 #ifdef HAVE_LIBSERIALPORT
844 /* Serial-specific instances */
845 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
846                 const char *serialcomm);
847 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
848 #endif
849
850 /* USBTMC-specific instances */
851 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
852 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
853
854 /*--- hwdriver.c ------------------------------------------------------------*/
855
856 SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
857 SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
858 SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
859 SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
860 SR_PRIV void sr_config_free(struct sr_config *src);
861 SR_PRIV int sr_dev_acquisition_start(struct sr_dev_inst *sdi);
862 SR_PRIV int sr_dev_acquisition_stop(struct sr_dev_inst *sdi);
863
864 /*--- session.c -------------------------------------------------------------*/
865
866 struct sr_session {
867         /** Context this session exists in. */
868         struct sr_context *ctx;
869         /** List of struct sr_dev_inst pointers. */
870         GSList *devs;
871         /** List of struct sr_dev_inst pointers owned by this session. */
872         GSList *owned_devs;
873         /** List of struct datafeed_callback pointers. */
874         GSList *datafeed_callbacks;
875         GSList *transforms;
876         struct sr_trigger *trigger;
877
878         /** Callback to invoke on session stop. */
879         sr_session_stopped_callback stopped_callback;
880         /** User data to be passed to the session stop callback. */
881         void *stopped_cb_data;
882
883         /** Mutex protecting the main context pointer. */
884         GMutex main_mutex;
885         /** Context of the session main loop. */
886         GMainContext *main_context;
887
888         /** Registered event sources for this session. */
889         GHashTable *event_sources;
890         /** Session main loop. */
891         GMainLoop *main_loop;
892         /** ID of idle source for dispatching the session stop notification. */
893         unsigned int stop_check_id;
894         /** Whether the session has been started. */
895         gboolean running;
896 };
897
898 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
899                 void *key, GSource *source);
900 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
901                 void *key);
902 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
903                 void *key, GSource *source);
904 SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
905                 void *key, gintptr fd, int events, int timeout,
906                 sr_receive_data_callback cb, void *cb_data);
907
908 SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
909                 int events, int timeout, sr_receive_data_callback cb, void *cb_data);
910 SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
911                 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
912                 void *cb_data);
913 SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
914                 GIOChannel *channel, int events, int timeout,
915                 sr_receive_data_callback cb, void *cb_data);
916 SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
917 SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
918                 GPollFD *pollfd);
919 SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
920                 GIOChannel *channel);
921
922 SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi,
923                 uint32_t key, GVariant *var);
924 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
925                 const struct sr_datafeed_packet *packet);
926 SR_PRIV int sr_sessionfile_check(const char *filename);
927 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename,
928                 struct sr_session **session);
929
930 /*--- session_file.c --------------------------------------------------------*/
931
932 #if !HAVE_ZIP_DISCARD
933 /* Replace zip_discard() if not available. */
934 #define zip_discard(zip) sr_zip_discard(zip)
935 SR_PRIV void sr_zip_discard(struct zip *archive);
936 #endif
937
938 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
939                         const struct zip_stat *entry);
940
941 /*--- analog.c --------------------------------------------------------------*/
942
943 SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
944                            struct sr_analog_encoding *encoding,
945                            struct sr_analog_meaning *meaning,
946                            struct sr_analog_spec *spec,
947                            int digits);
948
949 /*--- std.c -----------------------------------------------------------------*/
950
951 typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
952 typedef void (*std_dev_clear_callback)(void *priv);
953
954 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx);
955 SR_PRIV int std_cleanup(const struct sr_dev_driver *di);
956 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
957 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
958 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
959 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
960 #ifdef HAVE_LIBSERIALPORT
961 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
962 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
963 #endif
964 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi);
965 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi);
966 SR_PRIV int std_session_send_frame_begin(const struct sr_dev_inst *sdi);
967 SR_PRIV int std_session_send_frame_end(const struct sr_dev_inst *sdi);
968 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
969                 std_dev_clear_callback clear_private);
970 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver);
971 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di);
972 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
973 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices);
974
975 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
976         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
977         const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
978         size_t drvsize, const uint32_t devopts[], size_t devsize);
979
980 extern SR_PRIV const uint32_t NO_OPTS[1];
981
982 #define STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts) \
983         std_opts_config_list(key, data, sdi, cg, ARRAY_AND_SIZE(scanopts), \
984                 ARRAY_AND_SIZE(drvopts), ARRAY_AND_SIZE(devopts))
985
986 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n);
987 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n);
988 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n);
989 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n);
990 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step);
991 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]);
992 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double dmin, const double dmax, const double dstep);
993
994 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high);
995 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high);
996
997 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n);
998 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n);
999 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n);
1000 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n);
1001
1002 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n);
1003
1004 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n);
1005 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n);
1006 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n);
1007
1008 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n);
1009 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n);
1010
1011 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n);
1012 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
1013 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
1014
1015 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
1016
1017 /*--- resource.c ------------------------------------------------------------*/
1018
1019 SR_PRIV int64_t sr_file_get_size(FILE *file);
1020
1021 SR_PRIV int sr_resource_open(struct sr_context *ctx,
1022                 struct sr_resource *res, int type, const char *name)
1023                 G_GNUC_WARN_UNUSED_RESULT;
1024 SR_PRIV int sr_resource_close(struct sr_context *ctx,
1025                 struct sr_resource *res);
1026 SR_PRIV gssize sr_resource_read(struct sr_context *ctx,
1027                 const struct sr_resource *res, void *buf, size_t count)
1028                 G_GNUC_WARN_UNUSED_RESULT;
1029 SR_PRIV void *sr_resource_load(struct sr_context *ctx, int type,
1030                 const char *name, size_t *size, size_t max_size)
1031                 G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
1032
1033 /*--- strutil.c -------------------------------------------------------------*/
1034
1035 SR_PRIV int sr_atol(const char *str, long *ret);
1036 SR_PRIV int sr_atoi(const char *str, int *ret);
1037 SR_PRIV int sr_atod(const char *str, double *ret);
1038 SR_PRIV int sr_atof(const char *str, float *ret);
1039 SR_PRIV int sr_atod_ascii(const char *str, double *ret);
1040 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
1041
1042 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
1043 SR_PRIV void sr_hexdump_free(GString *s);
1044
1045 /*--- soft-trigger.c --------------------------------------------------------*/
1046
1047 struct soft_trigger_logic {
1048         const struct sr_dev_inst *sdi;
1049         const struct sr_trigger *trigger;
1050         int count;
1051         int unitsize;
1052         int cur_stage;
1053         uint8_t *prev_sample;
1054         uint8_t *pre_trigger_buffer;
1055         uint8_t *pre_trigger_head;
1056         int pre_trigger_size;
1057         int pre_trigger_fill;
1058 };
1059
1060 SR_PRIV int logic_channel_unitsize(GSList *channels);
1061 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
1062                 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
1063                 int pre_trigger_samples);
1064 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
1065 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
1066                 int len, int *pre_trigger_samples);
1067
1068 /*--- serial.c --------------------------------------------------------------*/
1069
1070 #ifdef HAVE_LIBSERIALPORT
1071 enum {
1072         SERIAL_RDWR = 1,
1073         SERIAL_RDONLY = 2,
1074 };
1075
1076 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
1077
1078 typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
1079                 const char *desc);
1080 typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
1081
1082 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
1083 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
1084 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
1085 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
1086 SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
1087 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
1088                 const void *buf, size_t count, unsigned int timeout_ms);
1089 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
1090                 const void *buf, size_t count);
1091 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
1092                 size_t count, unsigned int timeout_ms);
1093 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
1094                 size_t count);
1095 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
1096                 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
1097 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
1098                 const char *paramstr);
1099 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
1100                 int *buflen, gint64 timeout_ms);
1101 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
1102                                  uint8_t *buf, size_t *buflen,
1103                                  size_t packet_size,
1104                                  packet_valid_callback is_valid,
1105                                  uint64_t timeout_ms, int baudrate);
1106 SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
1107                                       const char **serial_options);
1108 SR_PRIV int serial_source_add(struct sr_session *session,
1109                 struct sr_serial_dev_inst *serial, int events, int timeout,
1110                 sr_receive_data_callback cb, void *cb_data);
1111 SR_PRIV int serial_source_remove(struct sr_session *session,
1112                 struct sr_serial_dev_inst *serial);
1113 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
1114 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
1115
1116 struct ser_lib_functions {
1117         int (*open)(struct sr_serial_dev_inst *serial, int flags);
1118         int (*close)(struct sr_serial_dev_inst *serial);
1119         int (*flush)(struct sr_serial_dev_inst *serial);
1120         int (*drain)(struct sr_serial_dev_inst *serial);
1121         int (*write)(struct sr_serial_dev_inst *serial,
1122                         const void *buf, size_t count,
1123                         int nonblocking, unsigned int timeout_ms);
1124         int (*read)(struct sr_serial_dev_inst *serial,
1125                         void *buf, size_t count,
1126                         int nonblocking, unsigned int timeout_ms);
1127         int (*set_params)(struct sr_serial_dev_inst *serial,
1128                         int baudrate, int bits, int parity, int stopbits,
1129                         int flowcontrol, int rts, int dtr);
1130         int (*setup_source_add)(struct sr_session *session,
1131                         struct sr_serial_dev_inst *serial,
1132                         int events, int timeout,
1133                         sr_receive_data_callback cb, void *cb_data);
1134         int (*setup_source_remove)(struct sr_session *session,
1135                         struct sr_serial_dev_inst *serial);
1136         GSList *(*list)(GSList *list, sr_ser_list_append_t append);
1137         GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
1138                         uint16_t vendor_id, uint16_t product_id);
1139         int (*get_frame_format)(struct sr_serial_dev_inst *serial,
1140                         int *baud, int *bits);
1141         size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
1142 };
1143 extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
1144 #endif
1145
1146 /*--- ezusb.c ---------------------------------------------------------------*/
1147
1148 #ifdef HAVE_LIBUSB_1_0
1149 SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
1150 SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
1151                                    const char *name);
1152 SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
1153                                   int configuration, const char *name);
1154 #endif
1155
1156 /*--- usb.c -----------------------------------------------------------------*/
1157
1158 #ifdef HAVE_LIBUSB_1_0
1159 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
1160 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
1161 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
1162 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
1163                 int timeout, sr_receive_data_callback cb, void *cb_data);
1164 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
1165 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
1166 SR_PRIV gboolean usb_match_manuf_prod(libusb_device *dev,
1167                 const char *manufacturer, const char *product);
1168 #endif
1169
1170
1171 /*--- modbus/modbus.c -------------------------------------------------------*/
1172
1173 struct sr_modbus_dev_inst {
1174         const char *name;
1175         const char *prefix;
1176         int priv_size;
1177         GSList *(*scan)(int modbusaddr);
1178         int (*dev_inst_new)(void *priv, const char *resource,
1179                 char **params, const char *serialcomm, int modbusaddr);
1180         int (*open)(void *priv);
1181         int (*source_add)(struct sr_session *session, void *priv, int events,
1182                 int timeout, sr_receive_data_callback cb, void *cb_data);
1183         int (*source_remove)(struct sr_session *session, void *priv);
1184         int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
1185         int (*read_begin)(void *priv, uint8_t *function_code);
1186         int (*read_data)(void *priv, uint8_t *buf, int maxlen);
1187         int (*read_end)(void *priv);
1188         int (*close)(void *priv);
1189         void (*free)(void *priv);
1190         unsigned int read_timeout_ms;
1191         void *priv;
1192 };
1193
1194 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
1195                 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
1196 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
1197                 const char *serialcomm, int modbusaddr);
1198 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
1199 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
1200                 struct sr_modbus_dev_inst *modbus, int events, int timeout,
1201                 sr_receive_data_callback cb, void *cb_data);
1202 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
1203                 struct sr_modbus_dev_inst *modbus);
1204 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
1205                               uint8_t *request, int request_size);
1206 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
1207                             uint8_t *reply, int reply_size);
1208 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
1209                                     uint8_t *request, int request_size,
1210                                     uint8_t *reply, int reply_size);
1211 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
1212                                  int address, int nb_coils, uint8_t *coils);
1213 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
1214                                              int address, int nb_registers,
1215                                              uint16_t *registers);
1216 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
1217                                  int address, int value);
1218 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
1219                                                int address, int nb_registers,
1220                                                uint16_t *registers);
1221 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
1222 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
1223
1224 /*--- dmm/es519xx.c ---------------------------------------------------------*/
1225
1226 /**
1227  * All 11-byte es519xx chips repeat each block twice for each conversion cycle
1228  * so always read 2 blocks at a time.
1229  */
1230 #define ES519XX_11B_PACKET_SIZE (11 * 2)
1231 #define ES519XX_14B_PACKET_SIZE 14
1232
1233 struct es519xx_info {
1234         gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
1235         gboolean is_milli, is_resistance, is_continuity, is_diode;
1236         gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
1237         gboolean is_temperature, is_celsius, is_fahrenheit;
1238         gboolean is_adp0, is_adp1, is_adp2, is_adp3;
1239         gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
1240         gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
1241         gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
1242         uint32_t baudrate;
1243         int packet_size;
1244         gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
1245         int digits;
1246 };
1247
1248 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
1249 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
1250                 struct sr_datafeed_analog *analog, void *info);
1251 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
1252 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
1253                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1254 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
1255 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
1256                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1257 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
1258 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
1259                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1260 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
1261 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
1262                 struct sr_datafeed_analog *analog, void *info);
1263 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
1264 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
1265                 struct sr_datafeed_analog *analog, void *info);
1266 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
1267 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
1268                 float *floatval, struct sr_datafeed_analog *analog, void *info);
1269
1270 /*--- dmm/fs9922.c ----------------------------------------------------------*/
1271
1272 #define FS9922_PACKET_SIZE 14
1273
1274 struct fs9922_info {
1275         gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
1276         gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
1277         gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
1278         gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
1279         gboolean is_celsius, is_fahrenheit;
1280         int bargraph_sign, bargraph_value;
1281 };
1282
1283 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
1284 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
1285                             struct sr_datafeed_analog *analog, void *info);
1286 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
1287
1288 /*--- dmm/fs9721.c ----------------------------------------------------------*/
1289
1290 #define FS9721_PACKET_SIZE 14
1291
1292 struct fs9721_info {
1293         gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
1294         gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
1295         gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
1296         gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
1297 };
1298
1299 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
1300 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
1301                             struct sr_datafeed_analog *analog, void *info);
1302 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
1303 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
1304 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
1305 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
1306 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
1307
1308 /*--- dmm/ms2115b.c ---------------------------------------------------------*/
1309
1310 #define MS2115B_PACKET_SIZE 9
1311
1312 enum ms2115b_display {
1313         MS2115B_DISPLAY_MAIN,
1314         MS2115B_DISPLAY_SUB,
1315         MS2115B_DISPLAY_COUNT,
1316 };
1317
1318 struct ms2115b_info {
1319         /* Selected channel. */
1320         size_t ch_idx;
1321         gboolean is_ac, is_dc, is_auto;
1322         gboolean is_diode, is_beep, is_farad;
1323         gboolean is_ohm, is_ampere, is_volt, is_hz;
1324         gboolean is_duty_cycle, is_percent;
1325 };
1326
1327 extern SR_PRIV const char *ms2115b_channel_formats[];
1328 SR_PRIV gboolean sr_ms2115b_packet_valid(const uint8_t *buf);
1329 SR_PRIV int sr_ms2115b_parse(const uint8_t *buf, float *floatval,
1330         struct sr_datafeed_analog *analog, void *info);
1331
1332 /*--- dmm/ms8250d.c ---------------------------------------------------------*/
1333
1334 #define MS8250D_PACKET_SIZE 18
1335
1336 struct ms8250d_info {
1337         gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
1338         gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
1339         gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
1340         gboolean is_ncv, is_min, is_max, is_sign, is_autotimer;
1341 };
1342
1343 SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf);
1344 SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
1345                              struct sr_datafeed_analog *analog, void *info);
1346
1347 /*--- dmm/dtm0660.c ---------------------------------------------------------*/
1348
1349 #define DTM0660_PACKET_SIZE 15
1350
1351 struct dtm0660_info {
1352         gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
1353         gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
1354         gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
1355         gboolean is_degf, is_degc, is_c2c1_01, is_c2c1_00, is_apo, is_min;
1356         gboolean is_minmax, is_max, is_sign;
1357 };
1358
1359 SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf);
1360 SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
1361                         struct sr_datafeed_analog *analog, void *info);
1362
1363 /*--- dmm/m2110.c -----------------------------------------------------------*/
1364
1365 #define BBCGM_M2110_PACKET_SIZE 9
1366
1367 /* Dummy info struct. The parser does not use it. */
1368 struct m2110_info { int dummy; };
1369
1370 SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
1371 SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
1372                              struct sr_datafeed_analog *analog, void *info);
1373
1374 /*--- dmm/metex14.c ---------------------------------------------------------*/
1375
1376 #define METEX14_PACKET_SIZE 14
1377
1378 struct metex14_info {
1379         size_t ch_idx;
1380         gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
1381         gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
1382         gboolean is_hertz, is_ohm, is_celsius, is_fahrenheit, is_watt;
1383         gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
1384         gboolean is_gain, is_decibel, is_power, is_decibel_mw, is_power_factor;
1385         gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
1386 };
1387
1388 #ifdef HAVE_LIBSERIALPORT
1389 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
1390 #endif
1391 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
1392 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
1393                              struct sr_datafeed_analog *analog, void *info);
1394 SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf);
1395 SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
1396                              struct sr_datafeed_analog *analog, void *info);
1397
1398 /*--- dmm/rs9lcd.c ----------------------------------------------------------*/
1399
1400 #define RS9LCD_PACKET_SIZE 9
1401
1402 /* Dummy info struct. The parser does not use it. */
1403 struct rs9lcd_info { int dummy; };
1404
1405 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
1406 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
1407                             struct sr_datafeed_analog *analog, void *info);
1408
1409 /*--- dmm/bm25x.c -----------------------------------------------------------*/
1410
1411 #define BRYMEN_BM25X_PACKET_SIZE 15
1412
1413 /* Dummy info struct. The parser does not use it. */
1414 struct bm25x_info { int dummy; };
1415
1416 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
1417 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
1418                              struct sr_datafeed_analog *analog, void *info);
1419
1420 /*--- dmm/ut71x.c -----------------------------------------------------------*/
1421
1422 #define UT71X_PACKET_SIZE 11
1423
1424 struct ut71x_info {
1425         gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
1426         gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
1427         gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
1428         gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
1429 };
1430
1431 SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
1432 SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
1433                 struct sr_datafeed_analog *analog, void *info);
1434
1435 /*--- dmm/vc870.c -----------------------------------------------------------*/
1436
1437 #define VC870_PACKET_SIZE 23
1438
1439 struct vc870_info {
1440         gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
1441         gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
1442         gboolean is_current, is_micro, is_milli, is_power;
1443         gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_rms_value;
1444         gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
1445         gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
1446         gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
1447         gboolean is_lo, is_hi, is_open2;
1448
1449         gboolean is_frequency, is_dual_display, is_auto;
1450 };
1451
1452 SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
1453 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
1454                 struct sr_datafeed_analog *analog, void *info);
1455
1456 /*--- dmm/vc96.c ------------------------------------------------------------*/
1457
1458 #define VC96_PACKET_SIZE 13
1459
1460 struct vc96_info {
1461         size_t ch_idx;
1462         gboolean is_ac, is_dc, is_resistance, is_diode, is_ampere, is_volt;
1463         gboolean is_ohm, is_micro, is_milli, is_kilo, is_mega, is_hfe;
1464         gboolean is_unitless;
1465 };
1466
1467 SR_PRIV gboolean sr_vc96_packet_valid(const uint8_t *buf);
1468 SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval,
1469                 struct sr_datafeed_analog *analog, void *info);
1470
1471 /*--- lcr/es51919.c ---------------------------------------------------------*/
1472
1473 SR_PRIV void es51919_serial_clean(void *priv);
1474 SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
1475                                                 const char *vendor,
1476                                                 const char *model);
1477 SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data,
1478                                       const struct sr_dev_inst *sdi,
1479                                       const struct sr_channel_group *cg);
1480 SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data,
1481                                       const struct sr_dev_inst *sdi,
1482                                       const struct sr_channel_group *cg);
1483 SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data,
1484                                        const struct sr_dev_inst *sdi,
1485                                        const struct sr_channel_group *cg);
1486 SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi);
1487 SR_PRIV int es51919_serial_acquisition_stop(struct sr_dev_inst *sdi);
1488
1489 /*--- dmm/ut372.c -----------------------------------------------------------*/
1490
1491 #define UT372_PACKET_SIZE 27
1492
1493 struct ut372_info {
1494         int dummy;
1495 };
1496
1497 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
1498 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
1499                 struct sr_datafeed_analog *analog, void *info);
1500
1501 /*--- dmm/asycii.c ----------------------------------------------------------*/
1502
1503 #define ASYCII_PACKET_SIZE 16
1504
1505 struct asycii_info {
1506         gboolean is_ac, is_dc, is_ac_and_dc;
1507         gboolean is_resistance, is_capacitance, is_diode, is_gain;
1508         gboolean is_frequency, is_duty_cycle, is_duty_pos, is_duty_neg;
1509         gboolean is_pulse_width, is_period_pos, is_period_neg;
1510         gboolean is_pulse_count, is_count_pos, is_count_neg;
1511         gboolean is_ampere, is_volt, is_volt_ampere, is_farad, is_ohm;
1512         gboolean is_hertz, is_percent, is_seconds, is_decibel;
1513         gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
1514         gboolean is_unitless;
1515         gboolean is_peak_min, is_peak_max;
1516         gboolean is_invalid;
1517 };
1518
1519 #ifdef HAVE_LIBSERIALPORT
1520 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
1521 #endif
1522 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);
1523 SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
1524                             struct sr_datafeed_analog *analog, void *info);
1525
1526 /*--- dmm/eev121gw.c --------------------------------------------------------*/
1527
1528 #define EEV121GW_PACKET_SIZE 19
1529
1530 enum eev121gw_display {
1531         EEV121GW_DISPLAY_MAIN,
1532         EEV121GW_DISPLAY_SUB,
1533         EEV121GW_DISPLAY_BAR,
1534         EEV121GW_DISPLAY_COUNT,
1535 };
1536
1537 struct eev121gw_info {
1538         /* Selected channel. */
1539         size_t ch_idx;
1540         /*
1541          * Measured value, number and sign/overflow flags, scale factor
1542          * and significant digits.
1543          */
1544         uint32_t uint_value;
1545         gboolean is_ofl, is_neg;
1546         int factor, digits;
1547         /* Currently active mode (meter's function). */
1548         gboolean is_ac, is_dc, is_voltage, is_current, is_power, is_gain;
1549         gboolean is_resistance, is_capacitance, is_diode, is_temperature;
1550         gboolean is_continuity, is_frequency, is_period, is_duty_cycle;
1551         /* Quantities associated with mode/function. */
1552         gboolean is_ampere, is_volt, is_volt_ampere, is_dbm;
1553         gboolean is_ohm, is_farad, is_celsius, is_fahrenheit;
1554         gboolean is_hertz, is_seconds, is_percent, is_loop_current;
1555         gboolean is_unitless, is_logic;
1556         /* Other indicators. */
1557         gboolean is_min, is_max, is_avg, is_1ms_peak, is_rel, is_hold;
1558         gboolean is_low_pass, is_mem, is_bt, is_auto_range, is_test;
1559         gboolean is_auto_poweroff, is_low_batt;
1560 };
1561
1562 extern SR_PRIV const char *eev121gw_channel_formats[];
1563 SR_PRIV gboolean sr_eev121gw_packet_valid(const uint8_t *buf);
1564 SR_PRIV int sr_eev121gw_parse(const uint8_t *buf, float *floatval,
1565                              struct sr_datafeed_analog *analog, void *info);
1566 SR_PRIV int sr_eev121gw_3displays_parse(const uint8_t *buf, float *floatval,
1567                              struct sr_datafeed_analog *analog, void *info);
1568
1569 /*--- scale/kern.c ----------------------------------------------------------*/
1570
1571 struct kern_info {
1572         gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
1573         gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
1574         gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
1575         int buflen;
1576 };
1577
1578 SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
1579 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
1580                 struct sr_datafeed_analog *analog, void *info);
1581
1582 /*--- sw_limits.c -----------------------------------------------------------*/
1583
1584 struct sr_sw_limits {
1585         uint64_t limit_samples;
1586         uint64_t limit_msec;
1587         uint64_t samples_read;
1588         uint64_t start_time;
1589 };
1590
1591 SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
1592         GVariant **data);
1593 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
1594         GVariant *data);
1595 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
1596 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
1597 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
1598         uint64_t samples_read);
1599 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
1600
1601 #endif