]> sigrok.org Git - libsigrok.git/blob - src/libsigrok-internal.h
log: Use generalized sr_log() to implement logging helpers
[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 /** @file
21   * @internal
22   */
23
24 #ifndef LIBSIGROK_LIBSIGROK_INTERNAL_H
25 #define LIBSIGROK_LIBSIGROK_INTERNAL_H
26
27 #include <stdarg.h>
28 #include <glib.h>
29 #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
30 #ifdef HAVE_LIBUSB_1_0
31 #include <libusb.h>
32 #endif
33 #ifdef HAVE_LIBSERIALPORT
34 #include <libserialport.h>
35 #endif
36
37 /**
38  * @file
39  *
40  * libsigrok private header file, only to be used internally.
41  */
42
43 /*--- Macros ----------------------------------------------------------------*/
44
45 #ifndef ARRAY_SIZE
46 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
47 #endif
48
49 #ifndef ARRAY_AND_SIZE
50 #define ARRAY_AND_SIZE(a) (a), ARRAY_SIZE(a)
51 #endif
52
53 /**
54  * Read a 8 bits unsigned integer out of memory.
55  * @param x a pointer to the input memory
56  * @return the corresponding unsigned integer
57  */
58 #define R8(x)     ((unsigned)((const uint8_t*)(x))[0])
59
60 /**
61  * Read a 16 bits big endian unsigned integer out of memory.
62  * @param x a pointer to the input memory
63  * @return the corresponding unsigned integer
64  */
65 #define RB16(x)  (((unsigned)((const uint8_t*)(x))[0] <<  8) |  \
66                    (unsigned)((const uint8_t*)(x))[1])
67
68 /**
69  * Read a 16 bits little endian unsigned integer out of memory.
70  * @param x a pointer to the input memory
71  * @return the corresponding unsigned integer
72  */
73 #define RL16(x)  (((unsigned)((const uint8_t*)(x))[1] <<  8) | \
74                    (unsigned)((const uint8_t*)(x))[0])
75
76 /**
77  * Read a 16 bits little endian signed integer out of memory.
78  * @param x a pointer to the input memory
79  * @return the corresponding signed integer
80  */
81 #define RL16S(x)  ((int16_t) \
82                   (((unsigned)((const uint8_t*)(x))[1] <<  8) | \
83                     (unsigned)((const uint8_t*)(x))[0]))
84
85 /**
86  * Read a 32 bits big endian unsigned integer out of memory.
87  * @param x a pointer to the input memory
88  * @return the corresponding unsigned integer
89  */
90 #define RB32(x)  (((unsigned)((const uint8_t*)(x))[0] << 24) | \
91                   ((unsigned)((const uint8_t*)(x))[1] << 16) |  \
92                   ((unsigned)((const uint8_t*)(x))[2] <<  8) |  \
93                    (unsigned)((const uint8_t*)(x))[3])
94
95 /**
96  * Read a 32 bits little endian unsigned integer out of memory.
97  * @param x a pointer to the input memory
98  * @return the corresponding unsigned integer
99  */
100 #define RL32(x)  (((unsigned)((const uint8_t*)(x))[3] << 24) | \
101                   ((unsigned)((const uint8_t*)(x))[2] << 16) |  \
102                   ((unsigned)((const uint8_t*)(x))[1] <<  8) |  \
103                    (unsigned)((const uint8_t*)(x))[0])
104
105 /**
106  * Read a 32 bits little endian signed integer out of memory.
107  * @param x a pointer to the input memory
108  * @return the corresponding signed integer
109  */
110 #define RL32S(x)  ((int32_t) \
111                  (((unsigned)((const uint8_t*)(x))[3] << 24) | \
112                   ((unsigned)((const uint8_t*)(x))[2] << 16) |  \
113                   ((unsigned)((const uint8_t*)(x))[1] <<  8) |  \
114                    (unsigned)((const uint8_t*)(x))[0]))
115
116 /**
117  * Read a 32 bits big endian float out of memory.
118  * @param x a pointer to the input memory
119  * @return the corresponding float
120  */
121 #define RBFL(x)  ((union { uint32_t u; float f; }) { .u = RB32(x) }.f)
122
123 /**
124  * Read a 32 bits little endian float out of memory.
125  * @param x a pointer to the input memory
126  * @return the corresponding float
127  */
128 #define RLFL(x)  ((union { uint32_t u; float f; }) { .u = RL32(x) }.f)
129
130 /**
131  * Write a 8 bits unsigned integer to memory.
132  * @param p a pointer to the output memory
133  * @param x the input unsigned integer
134  */
135 #define W8(p, x)    do { ((uint8_t*)(p))[0] = (uint8_t) (x);      } while (0)
136
137 /**
138  * Write a 16 bits unsigned integer to memory stored as big endian.
139  * @param p a pointer to the output memory
140  * @param x the input unsigned integer
141  */
142 #define WB16(p, x)  do { ((uint8_t*)(p))[1] = (uint8_t) (x);      \
143                          ((uint8_t*)(p))[0] = (uint8_t)((x)>>8);  } while (0)
144
145 /**
146  * Write a 16 bits unsigned integer to memory stored as little endian.
147  * @param p a pointer to the output memory
148  * @param x the input unsigned integer
149  */
150 #define WL16(p, x)  do { ((uint8_t*)(p))[0] = (uint8_t) (x);      \
151                          ((uint8_t*)(p))[1] = (uint8_t)((x)>>8);  } while (0)
152
153 /**
154  * Write a 32 bits unsigned integer to memory stored as big endian.
155  * @param p a pointer to the output memory
156  * @param x the input unsigned integer
157  */
158 #define WB32(p, x)  do { ((uint8_t*)(p))[3] = (uint8_t) (x);      \
159                          ((uint8_t*)(p))[2] = (uint8_t)((x)>>8);  \
160                          ((uint8_t*)(p))[1] = (uint8_t)((x)>>16); \
161                          ((uint8_t*)(p))[0] = (uint8_t)((x)>>24); } while (0)
162
163 /**
164  * Write a 32 bits unsigned integer to memory stored as little endian.
165  * @param p a pointer to the output memory
166  * @param x the input unsigned integer
167  */
168 #define WL32(p, x)  do { ((uint8_t*)(p))[0] = (uint8_t) (x);      \
169                          ((uint8_t*)(p))[1] = (uint8_t)((x)>>8);  \
170                          ((uint8_t*)(p))[2] = (uint8_t)((x)>>16); \
171                          ((uint8_t*)(p))[3] = (uint8_t)((x)>>24); } while (0)
172
173 /**
174  * Write a 32 bits float to memory stored as big endian.
175  * @param p a pointer to the output memory
176  * @param x the input float
177  */
178 #define WBFL(p, x)  WB32(p, (union { uint32_t u; float f; }) { .f = x }.u)
179
180 /**
181  * Write a 32 bits float to memory stored as little endian.
182  * @param p a pointer to the output memory
183  * @param x the input float
184  */
185 #define WLFL(p, x)  WL32(p, (union { uint32_t u; float f; }) { .f = x }.u)
186
187 /* Portability fixes for FreeBSD. */
188 #ifdef __FreeBSD__
189 #define LIBUSB_CLASS_APPLICATION 0xfe
190 #define libusb_has_capability(x) 0
191 #define libusb_handle_events_timeout_completed(ctx, tv, c) \
192         libusb_handle_events_timeout(ctx, tv)
193 #endif
194
195 /* Static definitions of structs ending with an all-zero entry are a
196  * problem when compiling with -Wmissing-field-initializers: GCC
197  * suppresses the warning only with { 0 }, clang wants { } */
198 #ifdef __clang__
199 #define ALL_ZERO { }
200 #else
201 #define ALL_ZERO { 0 }
202 #endif
203
204 struct sr_context {
205         struct sr_dev_driver **driver_list;
206 #ifdef HAVE_LIBUSB_1_0
207         libusb_context *libusb_ctx;
208 #endif
209 };
210
211 /** Input module metadata keys. */
212 enum sr_input_meta_keys {
213         /** The input filename, if there is one. */
214         SR_INPUT_META_FILENAME = 0x01,
215         /** The input file's size in bytes. */
216         SR_INPUT_META_FILESIZE = 0x02,
217         /** The first 128 bytes of the file, provided as a GString. */
218         SR_INPUT_META_HEADER = 0x04,
219         /** The file's MIME type. */
220         SR_INPUT_META_MIMETYPE = 0x08,
221
222         /** The module cannot identify a file without this metadata. */
223         SR_INPUT_META_REQUIRED = 0x80,
224 };
225
226 /** Input (file) module struct. */
227 struct sr_input {
228         /**
229          * A pointer to this input module's 'struct sr_input_module'.
230          */
231         const struct sr_input_module *module;
232         GString *buf;
233         struct sr_dev_inst *sdi;
234         gboolean sdi_ready;
235         void *priv;
236 };
237
238 /** Input (file) module driver. */
239 struct sr_input_module {
240         /**
241          * A unique ID for this input module, suitable for use in command-line
242          * clients, [a-z0-9-]. Must not be NULL.
243          */
244         const char *id;
245
246         /**
247          * A unique name for this input module, suitable for use in GUI
248          * clients, can contain UTF-8. Must not be NULL.
249          */
250         const char *name;
251
252         /**
253          * A short description of the input module. Must not be NULL.
254          *
255          * This can be displayed by frontends, e.g. when selecting the input
256          * module for saving a file.
257          */
258         const char *desc;
259
260         /**
261          * A NULL terminated array of strings containing a list of file name
262          * extensions typical for the input file format, or NULL if there is
263          * no typical extension for this file format.
264          */
265         const char *const *exts;
266
267         /**
268          * Zero-terminated list of metadata items the module needs to be able
269          * to identify an input stream. Can be all-zero, if the module cannot
270          * identify streams at all, i.e. has to be forced into use.
271          *
272          * Each item is one of:
273          *   SR_INPUT_META_FILENAME
274          *   SR_INPUT_META_FILESIZE
275          *   SR_INPUT_META_HEADER
276          *   SR_INPUT_META_MIMETYPE
277          *
278          * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
279          * identify a stream without the given metadata.
280          */
281         const uint8_t metadata[8];
282
283         /**
284          * Returns a NULL-terminated list of options this module can take.
285          * Can be NULL, if the module has no options.
286          */
287         struct sr_option *(*options) (void);
288
289         /**
290          * Check if this input module can load and parse the specified stream.
291          *
292          * @param[in] metadata Metadata the module can use to identify the stream.
293          *
294          * @retval SR_OK This module knows the format.
295          * @retval SR_ERR_NA There wasn't enough data for this module to
296          *   positively identify the format.
297          * @retval SR_ERR_DATA This module knows the format, but cannot handle it.
298          *   This means the stream is either corrupt, or indicates a feature
299          *   that the module does not support.
300          * @retval SR_ERR This module does not know the format.
301          */
302         int (*format_match) (GHashTable *metadata);
303
304         /**
305          * Initialize the input module.
306          *
307          * @retval SR_OK Success
308          * @retval other Negative error code.
309          */
310         int (*init) (struct sr_input *in, GHashTable *options);
311
312         /**
313          * Send data to the specified input instance.
314          *
315          * When an input module instance is created with sr_input_new(), this
316          * function is used to feed data to the instance.
317          *
318          * As enough data gets fed into this function to completely populate
319          * the device instance associated with this input instance, this is
320          * guaranteed to return the moment it's ready. This gives the caller
321          * the chance to examine the device instance, attach session callbacks
322          * and so on.
323          *
324          * @retval SR_OK Success
325          * @retval other Negative error code.
326          */
327         int (*receive) (struct sr_input *in, GString *buf);
328
329         /**
330          * Signal the input module no more data will come.
331          *
332          * This will cause the module to process any data it may have buffered.
333          * The SR_DF_END packet will also typically be sent at this time.
334          */
335         int (*end) (struct sr_input *in);
336
337         /**
338          * This function is called after the caller is finished using
339          * the input module, and can be used to free any internal
340          * resources the module may keep.
341          *
342          * This function is optional.
343          *
344          * @retval SR_OK Success
345          * @retval other Negative error code.
346          */
347         void (*cleanup) (struct sr_input *in);
348 };
349
350 /** Output module instance. */
351 struct sr_output {
352         /** A pointer to this output's module.  */
353         const struct sr_output_module *module;
354
355         /**
356          * The device for which this output module is creating output. This
357          * can be used by the module to find out channel names and numbers.
358          */
359         const struct sr_dev_inst *sdi;
360
361         /**
362          * The name of the file that the data should be written to.
363          */
364         const char *filename;
365
366         /**
367          * A generic pointer which can be used by the module to keep internal
368          * state between calls into its callback functions.
369          *
370          * For example, the module might store a pointer to a chunk of output
371          * there, and only flush it when it reaches a certain size.
372          */
373         void *priv;
374 };
375
376 /** Output module driver. */
377 struct sr_output_module {
378         /**
379          * A unique ID for this output module, suitable for use in command-line
380          * clients, [a-z0-9-]. Must not be NULL.
381          */
382         char *id;
383
384         /**
385          * A unique name for this output 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 output module. Must not be NULL.
392          *
393          * This can be displayed by frontends, e.g. when selecting the output
394          * module for saving a file.
395          */
396         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          * Bitfield containing flags that describe certain properties
407          * this output module may or may not have.
408          * @see sr_output_flags
409          */
410         const uint64_t flags;
411
412         /**
413          * Returns a NULL-terminated list of options this module can take.
414          * Can be NULL, if the module has no options.
415          */
416         const struct sr_option *(*options) (void);
417
418         /**
419          * This function is called once, at the beginning of an output stream.
420          *
421          * The device struct will be available in the output struct passed in,
422          * as well as the param field -- which may be NULL or an empty string,
423          * if no parameter was passed.
424          *
425          * The module can use this to initialize itself, create a struct for
426          * keeping state and storing it in the <code>internal</code> field.
427          *
428          * @param o Pointer to the respective 'struct sr_output'.
429          *
430          * @retval SR_OK Success
431          * @retval other Negative error code.
432          */
433         int (*init) (struct sr_output *o, GHashTable *options);
434
435         /**
436          * This function is passed a copy of every packet in the data feed.
437          * Any output generated by the output module in response to the
438          * packet should be returned in a newly allocated GString
439          * <code>out</code>, which will be freed by the caller.
440          *
441          * Packets not of interest to the output module can just be ignored,
442          * and the <code>out</code> parameter set to NULL.
443          *
444          * @param o Pointer to the respective 'struct sr_output'.
445          * @param sdi The device instance that generated the packet.
446          * @param packet The complete packet.
447          * @param out A pointer where a GString * should be stored if
448          * the module generates output, or NULL if not.
449          *
450          * @retval SR_OK Success
451          * @retval other Negative error code.
452          */
453         int (*receive) (const struct sr_output *o,
454                         const struct sr_datafeed_packet *packet, GString **out);
455
456         /**
457          * This function is called after the caller is finished using
458          * the output module, and can be used to free any internal
459          * resources the module may keep.
460          *
461          * @retval SR_OK Success
462          * @retval other Negative error code.
463          */
464         int (*cleanup) (struct sr_output *o);
465 };
466
467 /** Transform module instance. */
468 struct sr_transform {
469         /** A pointer to this transform's module.  */
470         const struct sr_transform_module *module;
471
472         /**
473          * The device for which this transform module is used. This
474          * can be used by the module to find out channel names and numbers.
475          */
476         const struct sr_dev_inst *sdi;
477
478         /**
479          * A generic pointer which can be used by the module to keep internal
480          * state between calls into its callback functions.
481          */
482         void *priv;
483 };
484
485 struct sr_transform_module {
486         /**
487          * A unique ID for this transform module, suitable for use in
488          * command-line clients, [a-z0-9-]. Must not be NULL.
489          */
490         char *id;
491
492         /**
493          * A unique name for this transform module, suitable for use in GUI
494          * clients, can contain UTF-8. Must not be NULL.
495          */
496         const char *name;
497
498         /**
499          * A short description of the transform module. Must not be NULL.
500          *
501          * This can be displayed by frontends, e.g. when selecting
502          * which transform module(s) to add.
503          */
504         char *desc;
505
506         /**
507          * Returns a NULL-terminated list of options this transform module
508          * can take. Can be NULL, if the transform module has no options.
509          */
510         const struct sr_option *(*options) (void);
511
512         /**
513          * This function is called once, at the beginning of a stream.
514          *
515          * @param t Pointer to the respective 'struct sr_transform'.
516          * @param options Hash table of options for this transform module.
517          *                Can be NULL if no options are to be used.
518          *
519          * @retval SR_OK Success
520          * @retval other Negative error code.
521          */
522         int (*init) (struct sr_transform *t, GHashTable *options);
523
524         /**
525          * This function is passed a pointer to every packet in the data feed.
526          *
527          * It can either return (in packet_out) a pointer to another packet
528          * (possibly the exact same packet it got as input), or NULL.
529          *
530          * @param t Pointer to the respective 'struct sr_transform'.
531          * @param packet_in Pointer to a datafeed packet.
532          * @param packet_out Pointer to the resulting datafeed packet after
533          *                   this function was run. If NULL, the transform
534          *                   module intentionally didn't output a new packet.
535          *
536          * @retval SR_OK Success
537          * @retval other Negative error code.
538          */
539         int (*receive) (const struct sr_transform *t,
540                         struct sr_datafeed_packet *packet_in,
541                         struct sr_datafeed_packet **packet_out);
542
543         /**
544          * This function is called after the caller is finished using
545          * the transform module, and can be used to free any internal
546          * resources the module may keep.
547          *
548          * @retval SR_OK Success
549          * @retval other Negative error code.
550          */
551         int (*cleanup) (struct sr_transform *t);
552 };
553
554 #ifdef HAVE_LIBUSB_1_0
555 /** USB device instance */
556 struct sr_usb_dev_inst {
557         /** USB bus */
558         uint8_t bus;
559         /** Device address on USB bus */
560         uint8_t address;
561         /** libusb device handle */
562         struct libusb_device_handle *devhdl;
563 };
564 #endif
565
566 #ifdef HAVE_LIBSERIALPORT
567 #define SERIAL_PARITY_NONE SP_PARITY_NONE
568 #define SERIAL_PARITY_EVEN SP_PARITY_EVEN
569 #define SERIAL_PARITY_ODD  SP_PARITY_ODD
570 struct sr_serial_dev_inst {
571         /** Port name, e.g. '/dev/tty42'. */
572         char *port;
573         /** Comm params for serial_set_paramstr(). */
574         char *serialcomm;
575         /** libserialport port handle */
576         struct sp_port *data;
577         /** libserialport event set */
578         struct sp_event_set *event_set;
579         /** GPollFDs for event polling */
580         GPollFD *pollfds;
581 };
582 #endif
583
584 struct sr_usbtmc_dev_inst {
585         char *device;
586         int fd;
587 };
588
589 /* Private driver context. */
590 struct drv_context {
591         /** sigrok context */
592         struct sr_context *sr_ctx;
593         GSList *instances;
594 };
595
596 /*--- log.c -----------------------------------------------------------------*/
597
598 SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
599
600 /* Message logging helpers with subsystem-specific prefix string. */
601 #define sr_spew(...)    sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
602 #define sr_dbg(...)     sr_log(SR_LOG_DBG,  LOG_PREFIX ": " __VA_ARGS__)
603 #define sr_info(...)    sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
604 #define sr_warn(...)    sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
605 #define sr_err(...)     sr_log(SR_LOG_ERR,  LOG_PREFIX ": " __VA_ARGS__)
606
607 /*--- device.c --------------------------------------------------------------*/
608
609 /** Values for the changes argument of sr_dev_driver.config_channel_set. */
610 enum {
611         /** The enabled state of the channel has been changed. */
612         SR_CHANNEL_SET_ENABLED = 1 << 0,
613 };
614
615 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
616                 int index, int type, gboolean enabled, const char *name);
617 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
618                 struct sr_channel *cur_channel);
619
620 /** Device instance data */
621 struct sr_dev_inst {
622         /** Device driver. */
623         struct sr_dev_driver *driver;
624         /** Device instance status. SR_ST_NOT_FOUND, etc. */
625         int status;
626         /** Device instance type. SR_INST_USB, etc. */
627         int inst_type;
628         /** Device vendor. */
629         char *vendor;
630         /** Device model. */
631         char *model;
632         /** Device version. */
633         char *version;
634         /** Serial number. */
635         char *serial_num;
636         /** Connection string to uniquely identify devices. */
637         char *connection_id;
638         /** List of channels. */
639         GSList *channels;
640         /** List of sr_channel_group structs */
641         GSList *channel_groups;
642         /** Device instance connection data (used?) */
643         void *conn;
644         /** Device instance private data (used?) */
645         void *priv;
646         /** Session to which this device is currently assigned. */
647         struct sr_session *session;
648 };
649
650 /* Generic device instances */
651 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
652
653 #ifdef HAVE_LIBUSB_1_0
654 /* USB-specific instances */
655 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
656                 uint8_t address, struct libusb_device_handle *hdl);
657 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
658 #endif
659
660 #ifdef HAVE_LIBSERIALPORT
661 /* Serial-specific instances */
662 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
663                 const char *serialcomm);
664 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
665 #endif
666
667 /* USBTMC-specific instances */
668 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
669 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
670
671 /*--- hwdriver.c ------------------------------------------------------------*/
672
673 extern SR_PRIV struct sr_dev_driver **drivers_lists[];
674
675 SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
676 SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
677 SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
678 SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
679 SR_PRIV void sr_config_free(struct sr_config *src);
680 SR_PRIV int sr_source_remove(int fd);
681 SR_PRIV int sr_source_remove_pollfd(GPollFD *pollfd);
682 SR_PRIV int sr_source_remove_channel(GIOChannel *channel);
683 SR_PRIV int sr_source_add(int fd, int events, int timeout,
684                 sr_receive_data_callback cb, void *cb_data);
685 SR_PRIV int sr_source_add_pollfd(GPollFD *pollfd, int timeout,
686                 sr_receive_data_callback cb, void *cb_data);
687 SR_PRIV int sr_source_add_channel(GIOChannel *channel, int events, int timeout,
688                 sr_receive_data_callback cb, void *cb_data);
689
690 /*--- session.c -------------------------------------------------------------*/
691
692 struct sr_session {
693         /** Context this session exists in. */
694         struct sr_context *ctx;
695         /** List of struct sr_dev_inst pointers. */
696         GSList *devs;
697         /** List of struct sr_dev_inst pointers owned by this session. */
698         GSList *owned_devs;
699         /** List of struct datafeed_callback pointers. */
700         GSList *datafeed_callbacks;
701         GSList *transforms;
702         struct sr_trigger *trigger;
703
704         /** Mutex protecting the main context pointer and ownership flag. */
705         GMutex main_mutex;
706         /** Context of the session main loop. */
707         GMainContext *main_context;
708         /** Whether we are using the thread's default context. */
709         gboolean main_context_is_default;
710
711         /** Whether the session has been started. */
712         gboolean running;
713
714         /** Registered event sources for this session. */
715         GHashTable *event_sources;
716         /** Session main loop. */
717         GMainLoop *main_loop;
718 };
719
720 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
721                 void *key, GSource *source);
722 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
723                 void *key);
724 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
725                 void *key, GSource *source);
726 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
727                 const struct sr_datafeed_packet *packet);
728 SR_PRIV int sr_sessionfile_check(const char *filename);
729 SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
730                 struct sr_datafeed_packet **copy);
731 SR_PRIV void sr_packet_free(struct sr_datafeed_packet *packet);
732
733 /*--- analog.c --------------------------------------------------------------*/
734
735 SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog,
736                            struct sr_analog_encoding *encoding,
737                            struct sr_analog_meaning *meaning,
738                            struct sr_analog_spec *spec,
739                            int digits);
740
741 /*--- std.c -----------------------------------------------------------------*/
742
743 typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
744 typedef void (*std_dev_clear_callback)(void *priv);
745
746 SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
747                 const char *prefix);
748 #ifdef HAVE_LIBSERIALPORT
749 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
750 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi,
751                 void *cb_data, dev_close_callback dev_close_fn,
752                 struct sr_serial_dev_inst *serial, const char *prefix);
753 #endif
754 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi,
755                 const char *prefix);
756 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver,
757                 std_dev_clear_callback clear_private);
758 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
759
760 /*--- strutil.c -------------------------------------------------------------*/
761
762 SR_PRIV int sr_atol(const char *str, long *ret);
763 SR_PRIV int sr_atoi(const char *str, int *ret);
764 SR_PRIV int sr_atod(const char *str, double *ret);
765 SR_PRIV int sr_atof(const char *str, float *ret);
766 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
767
768 /*--- soft-trigger.c --------------------------------------------------------*/
769
770 struct soft_trigger_logic {
771         const struct sr_dev_inst *sdi;
772         const struct sr_trigger *trigger;
773         int count;
774         int unitsize;
775         int cur_stage;
776         uint8_t *prev_sample;
777         uint8_t *pre_trigger_buffer;
778         uint8_t *pre_trigger_head;
779         int pre_trigger_size;
780         int pre_trigger_fill;
781 };
782
783 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
784                 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
785                 int pre_trigger_samples);
786 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
787 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
788                 int len, int *pre_trigger_samples);
789
790 /*--- hardware/serial.c -----------------------------------------------------*/
791
792 #ifdef HAVE_LIBSERIALPORT
793 enum {
794         SERIAL_RDWR = 1,
795         SERIAL_RDONLY = 2,
796 };
797
798 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
799
800 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
801 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
802 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
803 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
804 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
805                 const void *buf, size_t count, unsigned int timeout_ms);
806 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
807                 const void *buf, size_t count);
808 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
809                 size_t count, unsigned int timeout_ms);
810 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
811                 size_t count);
812 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
813                 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
814 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
815                 const char *paramstr);
816 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
817                 int *buflen, gint64 timeout_ms);
818 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
819                                  uint8_t *buf, size_t *buflen,
820                                  size_t packet_size,
821                                  packet_valid_callback is_valid,
822                                  uint64_t timeout_ms, int baudrate);
823 SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
824                                       const char **serial_options);
825 SR_PRIV int serial_source_add(struct sr_session *session,
826                 struct sr_serial_dev_inst *serial, int events, int timeout,
827                 sr_receive_data_callback cb, void *cb_data);
828 SR_PRIV int serial_source_remove(struct sr_session *session,
829                 struct sr_serial_dev_inst *serial);
830 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
831 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
832 #endif
833
834 /*--- hardware/ezusb.c ------------------------------------------------------*/
835
836 #ifdef HAVE_LIBUSB_1_0
837 SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
838 SR_PRIV int ezusb_install_firmware(libusb_device_handle *hdl,
839                                    const char *filename);
840 SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration,
841                                   const char *filename);
842 #endif
843
844 /*--- hardware/usb.c --------------------------------------------------------*/
845
846 #ifdef HAVE_LIBUSB_1_0
847 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
848 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
849 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
850 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
851                 int timeout, sr_receive_data_callback cb, void *cb_data);
852 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
853 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
854 #endif
855
856
857 /*--- modbus/modbus.c -------------------------------------------------------*/
858
859 struct sr_modbus_dev_inst {
860         const char *name;
861         const char *prefix;
862         int priv_size;
863         GSList *(*scan)(int modbusaddr);
864         int (*dev_inst_new)(void *priv, const char *resource,
865                 char **params, const char *serialcomm, int modbusaddr);
866         int (*open)(void *priv);
867         int (*source_add)(struct sr_session *session, void *priv, int events,
868                 int timeout, sr_receive_data_callback cb, void *cb_data);
869         int (*source_remove)(struct sr_session *session, void *priv);
870         int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
871         int (*read_begin)(void *priv, uint8_t *function_code);
872         int (*read_data)(void *priv, uint8_t *buf, int maxlen);
873         int (*read_end)(void *priv);
874         int (*close)(void *priv);
875         void (*free)(void *priv);
876         unsigned int read_timeout_ms;
877         void *priv;
878 };
879
880 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
881                 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
882 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
883                 const char *serialcomm, int modbusaddr);
884 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
885 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
886                 struct sr_modbus_dev_inst *modbus, int events, int timeout,
887                 sr_receive_data_callback cb, void *cb_data);
888 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
889                 struct sr_modbus_dev_inst *modbus);
890 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
891                               uint8_t *request, int request_size);
892 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
893                             uint8_t *reply, int reply_size);
894 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
895                                     uint8_t *request, int request_size,
896                                     uint8_t *reply, int reply_size);
897 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
898                                  int address, int nb_coils, uint8_t *coils);
899 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
900                                              int address, int nb_registers,
901                                              uint16_t *registers);
902 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
903                                  int address, int value);
904 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
905                                                int address, int nb_registers,
906                                                uint16_t *registers);
907 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
908 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
909
910 /*--- hardware/dmm/es519xx.c ------------------------------------------------*/
911
912 /**
913  * All 11-byte es519xx chips repeat each block twice for each conversion cycle
914  * so always read 2 blocks at a time.
915  */
916 #define ES519XX_11B_PACKET_SIZE (11 * 2)
917 #define ES519XX_14B_PACKET_SIZE 14
918
919 struct es519xx_info {
920         gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
921         gboolean is_milli, is_resistance, is_continuity, is_diode;
922         gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
923         gboolean is_temperature, is_celsius, is_fahrenheit;
924         gboolean is_adp0, is_adp1, is_adp2, is_adp3;
925         gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
926         gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
927         gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
928         uint32_t baudrate;
929         int packet_size;
930         gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
931 };
932
933 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
934 SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
935                 struct sr_datafeed_analog *analog, void *info);
936 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
937 SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
938                 float *floatval, struct sr_datafeed_analog *analog, void *info);
939 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
940 SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
941                 float *floatval, struct sr_datafeed_analog *analog, void *info);
942 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
943 SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
944                 float *floatval, struct sr_datafeed_analog *analog, void *info);
945 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
946 SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
947                 struct sr_datafeed_analog *analog, void *info);
948 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
949 SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
950                 struct sr_datafeed_analog *analog, void *info);
951 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
952 SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
953                 float *floatval, struct sr_datafeed_analog *analog, void *info);
954
955 /*--- hardware/dmm/fs9922.c -------------------------------------------------*/
956
957 #define FS9922_PACKET_SIZE 14
958
959 struct fs9922_info {
960         gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
961         gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
962         gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
963         gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
964         gboolean is_celsius, is_fahrenheit;
965         int bargraph_sign, bargraph_value;
966 };
967
968 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
969 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
970                             struct sr_datafeed_analog *analog, void *info);
971 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
972
973 /*--- hardware/dmm/fs9721.c -------------------------------------------------*/
974
975 #define FS9721_PACKET_SIZE 14
976
977 struct fs9721_info {
978         gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
979         gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
980         gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
981         gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
982 };
983
984 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
985 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
986                             struct sr_datafeed_analog *analog, void *info);
987 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
988 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
989 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
990 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
991 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
992
993 /*--- hardware/dmm/m2110.c --------------------------------------------------*/
994
995 #define BBCGM_M2110_PACKET_SIZE 9
996
997 SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
998 SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
999                              struct sr_datafeed_analog *analog, void *info);
1000
1001 /*--- hardware/dmm/metex14.c ------------------------------------------------*/
1002
1003 #define METEX14_PACKET_SIZE 14
1004
1005 struct metex14_info {
1006         gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
1007         gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
1008         gboolean is_hertz, is_ohm, is_celsius, is_pico, is_nano, is_micro;
1009         gboolean is_milli, is_kilo, is_mega, is_gain, is_decibel, is_hfe;
1010         gboolean is_unitless, is_logic;
1011 };
1012
1013 #ifdef HAVE_LIBSERIALPORT
1014 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
1015 #endif
1016 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
1017 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
1018                              struct sr_datafeed_analog *analog, void *info);
1019
1020 /*--- hardware/dmm/rs9lcd.c -------------------------------------------------*/
1021
1022 #define RS9LCD_PACKET_SIZE 9
1023
1024 /* Dummy info struct. The parser does not use it. */
1025 struct rs9lcd_info { int dummy; };
1026
1027 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
1028 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
1029                             struct sr_datafeed_analog *analog, void *info);
1030
1031 /*--- hardware/dmm/bm25x.c --------------------------------------------------*/
1032
1033 #define BRYMEN_BM25X_PACKET_SIZE 15
1034
1035 /* Dummy info struct. The parser does not use it. */
1036 struct bm25x_info { int dummy; };
1037
1038 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
1039 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
1040                              struct sr_datafeed_analog *analog, void *info);
1041
1042 /*--- hardware/dmm/ut71x.c --------------------------------------------------*/
1043
1044 #define UT71X_PACKET_SIZE 11
1045
1046 struct ut71x_info {
1047         gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
1048         gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
1049         gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
1050         gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
1051 };
1052
1053 SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
1054 SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
1055                 struct sr_datafeed_analog *analog, void *info);
1056
1057 /*--- hardware/dmm/vc870.c --------------------------------------------------*/
1058
1059 #define VC870_PACKET_SIZE 23
1060
1061 struct vc870_info {
1062         gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
1063         gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
1064         gboolean is_current, is_micro, is_milli, is_power;
1065         gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_eff_value;
1066         gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
1067         gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
1068         gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
1069         gboolean is_lo, is_hi, is_open2;
1070
1071         gboolean is_frequency, is_dual_display, is_auto, is_rms;
1072 };
1073
1074 SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
1075 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
1076                 struct sr_datafeed_analog *analog, void *info);
1077
1078 /*--- hardware/lcr/es51919.c ------------------------------------------------*/
1079
1080 SR_PRIV void es51919_serial_clean(void *priv);
1081 SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
1082                                                 const char *vendor,
1083                                                 const char *model);
1084 SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data,
1085                                       const struct sr_dev_inst *sdi,
1086                                       const struct sr_channel_group *cg);
1087 SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data,
1088                                       const struct sr_dev_inst *sdi,
1089                                       const struct sr_channel_group *cg);
1090 SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data,
1091                                        const struct sr_dev_inst *sdi,
1092                                        const struct sr_channel_group *cg);
1093 SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi,
1094                                              void *cb_data);
1095 SR_PRIV int es51919_serial_acquisition_stop(struct sr_dev_inst *sdi,
1096                                             void *cb_data);
1097
1098 /*--- hardware/dmm/ut372.c --------------------------------------------------*/
1099
1100 #define UT372_PACKET_SIZE 27
1101
1102 struct ut372_info {
1103         int dummy;
1104 };
1105
1106 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
1107 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
1108                 struct sr_datafeed_analog *analog, void *info);
1109
1110 /*--- hardware/scale/kern.c -------------------------------------------------*/
1111
1112 struct kern_info {
1113         gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
1114         gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
1115         gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
1116         int buflen;
1117 };
1118
1119 SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
1120 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
1121                 struct sr_datafeed_analog *analog, void *info);
1122
1123 #endif