libsigrok  0.2.0
sigrok hardware access and backend library
 All Data Structures Files Functions Variables Typedefs Enumerator Macros Groups Pages
libsigrok.h
Go to the documentation of this file.
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 #ifndef LIBSIGROK_SIGROK_H
21 #define LIBSIGROK_SIGROK_H
22 
23 #include <stdio.h>
24 #include <sys/time.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <glib.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34  * @file
35  *
36  * The public libsigrok header file to be used by frontends.
37  *
38  * This is the only file that libsigrok users (frontends) are supposed to
39  * use and \#include. There are other header files which get installed with
40  * libsigrok, but those are not meant to be used directly by frontends.
41  *
42  * The correct way to get/use the libsigrok API functions is:
43  *
44  * @code{.c}
45  * #include <libsigrok/libsigrok.h>
46  * @endcode
47  */
48 
49 /*
50  * All possible return codes of libsigrok functions must be listed here.
51  * Functions should never return hardcoded numbers as status, but rather
52  * use these enum values. All error codes are negative numbers.
53  *
54  * The error codes are globally unique in libsigrok, i.e. if one of the
55  * libsigrok functions returns a "malloc error" it must be exactly the same
56  * return value as used by all other functions to indicate "malloc error".
57  * There must be no functions which indicate two different errors via the
58  * same return code.
59  *
60  * Also, for compatibility reasons, no defined return codes are ever removed
61  * or reused for different errors later. You can only add new entries and
62  * return codes, but never remove or redefine existing ones.
63  */
64 
65 /** Status/error codes returned by libsigrok functions. */
66 enum {
67  SR_OK = 0, /**< No error. */
68  SR_ERR = -1, /**< Generic/unspecified error. */
69  SR_ERR_MALLOC = -2, /**< Malloc/calloc/realloc error. */
70  SR_ERR_ARG = -3, /**< Function argument error. */
71  SR_ERR_BUG = -4, /**< Errors hinting at internal bugs. */
72  SR_ERR_SAMPLERATE = -5, /**< Incorrect samplerate. */
73  SR_ERR_NA = -6, /**< Not applicable. */
74  SR_ERR_DEV_CLOSED = -7, /**< Device is closed, but needs to be open. */
75 
76  /*
77  * Note: When adding entries here, don't forget to also update the
78  * sr_strerror() and sr_strerror_name() functions in error.c.
79  */
80 };
81 
82 #define SR_MAX_PROBENAME_LEN 32
83 
84 /* Handy little macros */
85 #define SR_HZ(n) (n)
86 #define SR_KHZ(n) ((n) * (uint64_t)(1000ULL))
87 #define SR_MHZ(n) ((n) * (uint64_t)(1000000ULL))
88 #define SR_GHZ(n) ((n) * (uint64_t)(1000000000ULL))
89 
90 #define SR_HZ_TO_NS(n) ((uint64_t)(1000000000ULL) / (n))
91 
92 /** libsigrok loglevels. */
93 enum {
94  SR_LOG_NONE = 0, /**< Output no messages at all. */
95  SR_LOG_ERR = 1, /**< Output error messages. */
96  SR_LOG_WARN = 2, /**< Output warnings. */
97  SR_LOG_INFO = 3, /**< Output informational messages. */
98  SR_LOG_DBG = 4, /**< Output debug messages. */
99  SR_LOG_SPEW = 5, /**< Output very noisy debug messages. */
100 };
101 
102 /*
103  * Use SR_API to mark public API symbols, and SR_PRIV for private symbols.
104  *
105  * Variables and functions marked 'static' are private already and don't
106  * need SR_PRIV. However, functions which are not static (because they need
107  * to be used in other libsigrok-internal files) but are also not meant to
108  * be part of the public libsigrok API, must use SR_PRIV.
109  *
110  * This uses the 'visibility' feature of gcc (requires gcc >= 4.0).
111  *
112  * This feature is not available on MinGW/Windows, as it is a feature of
113  * ELF files and MinGW/Windows uses PE files.
114  *
115  * Details: http://gcc.gnu.org/wiki/Visibility
116  */
117 
118 /* Marks public libsigrok API symbols. */
119 #ifndef _WIN32
120 #define SR_API __attribute__((visibility("default")))
121 #else
122 #define SR_API
123 #endif
124 
125 /* Marks private, non-public libsigrok symbols (not part of the API). */
126 #ifndef _WIN32
127 #define SR_PRIV __attribute__((visibility("hidden")))
128 #else
129 #define SR_PRIV
130 #endif
131 
132 typedef int (*sr_receive_data_callback_t)(int fd, int revents, void *cb_data);
133 
134 /** Data types used by sr_config_info(). */
135 enum {
136  SR_T_UINT64 = 10000,
143 };
144 
145 /** Value for sr_datafeed_packet.type. */
146 enum {
147  SR_DF_HEADER = 10000,
155 };
156 
157 /** Values for sr_datafeed_analog.mq. */
158 enum {
159  SR_MQ_VOLTAGE = 10000,
169  /** Electrical power, usually in W, or dBm. */
171  /** Gain (a transistor's gain, or hFE, for example). */
173  /** Logarithmic representation of sound pressure relative to a
174  * reference value. */
178 };
179 
180 /** Values for sr_datafeed_analog.unit. */
181 enum {
182  SR_UNIT_VOLT = 10000,
193  /** Unit of conductance, the inverse of resistance. */
195  /**
196  * An absolute measurement of power, in decibels, referenced to
197  * 1 milliwatt (dBu).
198  */
200  /** Voltage in decibel, referenced to 1 volt (dBV). */
202  /**
203  * Measurements that intrinsically do not have units attached, such
204  * as ratios, gains, etc. Specifically, a transistor's gain (hFE) is
205  * a unitless quantity, for example.
206  */
208  /** Sound pressure level relative so 20 micropascals. */
210  /**
211  * Normalized (0 to 1) concentration of a substance or compound with 0
212  * representing a concentration of 0%, and 1 being 100%. This is
213  * represented as the fraction of number of particles of the substance.
214  */
216 };
217 
218 /** Values for sr_datafeed_analog.flags. */
219 enum {
220  /** Voltage measurement is alternating current (AC). */
221  SR_MQFLAG_AC = 0x01,
222  /** Voltage measurement is direct current (DC). */
223  SR_MQFLAG_DC = 0x02,
224  /** This is a true RMS measurement. */
226  /** Value is voltage drop across a diode, or NAN. */
228  /** Device is in "hold" mode (repeating the last measurement). */
230  /** Device is in "max" mode, only updating upon a new max value. */
232  /** Device is in "min" mode, only updating upon a new min value. */
234  /** Device is in autoranging mode. */
236  /** Device is in relative mode. */
238  /** Sound pressure level is A-weighted in the frequency domain,
239  * according to IEC 61672:2003. */
241  /** Sound pressure level is C-weighted in the frequency domain,
242  * according to IEC 61672:2003. */
244  /** Sound pressure level is Z-weighted (i.e. not at all) in the
245  * frequency domain, according to IEC 61672:2003. */
247  /** Sound pressure level is not weighted in the frequency domain,
248  * albeit without standards-defined low and high frequency limits. */
250  /** Sound pressure level measurement is S-weighted (1s) in the
251  * time domain. */
253  /** Sound pressure level measurement is F-weighted (125ms) in the
254  * time domain. */
256  /** Sound pressure level is time-averaged (LAT), also known as
257  * Equivalent Continuous A-weighted Sound Level (LEQ). */
259  /** Sound pressure level represented as a percentage of measurements
260  * that were over a preset alarm level. */
262 };
263 
264 struct sr_context;
265 
267  uint16_t type;
268  const void *payload;
269 };
270 
273  struct timeval starttime;
274 };
275 
277  GSList *config;
278 };
279 
281  uint64_t length;
282  uint16_t unitsize;
283  void *data;
284 };
285 
287  /** The probes for which data is included in this packet. */
288  GSList *probes;
290  /** Measured quantity (voltage, current, temperature, and so on). */
291  int mq;
292  /** Unit in which the MQ is measured. */
293  int unit;
294  /** Bitmap with extra information about the MQ. */
295  uint64_t mqflags;
296  /** The analog value(s). The data is interleaved according to
297  * the probes list. */
298  float *data;
299 };
300 
301 /** Input (file) format struct. */
302 struct sr_input {
303  /**
304  * A pointer to this input format's 'struct sr_input_format'.
305  * The frontend can use this to call the module's callbacks.
306  */
308 
309  GHashTable *param;
310 
311  struct sr_dev_inst *sdi;
312 
313  void *internal;
314 };
315 
317  /** The unique ID for this input format. Must not be NULL. */
318  char *id;
319 
320  /**
321  * A short description of the input format, which can (for example)
322  * be displayed to the user by frontends. Must not be NULL.
323  */
324  char *description;
325 
326  /**
327  * Check if this input module can load and parse the specified file.
328  *
329  * @param filename The name (and path) of the file to check.
330  *
331  * @return TRUE if this module knows the format, FALSE if it doesn't.
332  */
333  int (*format_match) (const char *filename);
334 
335  /**
336  * Initialize the input module.
337  *
338  * @param in A pointer to a valid 'struct sr_input' that the caller
339  * has to allocate and provide to this function. It is also
340  * the responsibility of the caller to free it later.
341  * @param filename The name (and path) of the file to use.
342  *
343  * @return SR_OK upon success, a negative error code upon failure.
344  */
345  int (*init) (struct sr_input *in, const char *filename);
346 
347  /**
348  * Load a file, parsing the input according to the file's format.
349  *
350  * This function will send datafeed packets to the session bus, so
351  * the calling frontend must have registered its session callbacks
352  * beforehand.
353  *
354  * The packet types sent across the session bus by this function must
355  * include at least SR_DF_HEADER, SR_DF_END, and an appropriate data
356  * type such as SR_DF_LOGIC. It may also send a SR_DF_TRIGGER packet
357  * if appropriate.
358  *
359  * @param in A pointer to a valid 'struct sr_input' that the caller
360  * has to allocate and provide to this function. It is also
361  * the responsibility of the caller to free it later.
362  * @param filename The name (and path) of the file to use.
363  *
364  * @return SR_OK upon success, a negative error code upon failure.
365  */
366  int (*loadfile) (struct sr_input *in, const char *filename);
367 };
368 
369 /** Output (file) format struct. */
370 struct sr_output {
371  /**
372  * A pointer to this output format's 'struct sr_output_format'.
373  * The frontend can use this to call the module's callbacks.
374  */
376 
377  /**
378  * The device for which this output module is creating output. This
379  * can be used by the module to find out probe names and numbers.
380  */
381  struct sr_dev_inst *sdi;
382 
383  /**
384  * An optional parameter which the frontend can pass in to the
385  * output module. How the string is interpreted is entirely up to
386  * the module.
387  */
388  char *param;
389 
390  /**
391  * A generic pointer which can be used by the module to keep internal
392  * state between calls into its callback functions.
393  *
394  * For example, the module might store a pointer to a chunk of output
395  * there, and only flush it when it reaches a certain size.
396  */
397  void *internal;
398 };
399 
401  /**
402  * A unique ID for this output format. Must not be NULL.
403  *
404  * It can be used by frontends to select this output format for use.
405  *
406  * For example, calling sigrok-cli with <code>-O hex</code> will
407  * select the hexadecimal text output format.
408  */
409  char *id;
410 
411  /**
412  * A short description of the output format. Must not be NULL.
413  *
414  * This can be displayed by frontends, e.g. when selecting the output
415  * format for saving a file.
416  */
417  char *description;
418 
419  int df_type;
420 
421  /**
422  * This function is called once, at the beginning of an output stream.
423  *
424  * The device struct will be available in the output struct passed in,
425  * as well as the param field -- which may be NULL or an empty string,
426  * if no parameter was passed.
427  *
428  * The module can use this to initialize itself, create a struct for
429  * keeping state and storing it in the <code>internal</code> field.
430  *
431  * @param o Pointer to the respective 'struct sr_output'.
432  *
433  * @return SR_OK upon success, a negative error code otherwise.
434  */
435  int (*init) (struct sr_output *o);
436 
437  /**
438  * Whenever a chunk of data comes in, it will be passed to the
439  * output module via this function. The <code>data_in</code> and
440  * <code>length_in</code> values refers to this data; the module
441  * must not alter or g_free() this buffer.
442  *
443  * The function must allocate a buffer for storing its output, and
444  * pass along a pointer to this buffer in the <code>data_out</code>
445  * parameter, as well as storing the length of the buffer in
446  * <code>length_out</code>. The calling frontend will g_free()
447  * this buffer when it's done with it.
448  *
449  * IMPORTANT: The memory allocation much happen using a glib memory
450  * allocation call (not a "normal" malloc) since g_free() will be
451  * used to free the memory!
452  *
453  * If there is no output, this function MUST store NULL in the
454  * <code>data_out</code> parameter, so the caller knows not to try
455  * and g_free() it.
456  *
457  * Note: This API call is obsolete, use receive() instead.
458  *
459  * @param o Pointer to the respective 'struct sr_output'.
460  * @param data_in Pointer to the input data buffer.
461  * @param length_in Length of the input.
462  * @param data_out Pointer to the allocated output buffer.
463  * @param length_out Length (in bytes) of the output.
464  *
465  * @return SR_OK upon success, a negative error code otherwise.
466  */
467  int (*data) (struct sr_output *o, const uint8_t *data_in,
468  uint64_t length_in, uint8_t **data_out,
469  uint64_t *length_out);
470 
471  /**
472  * This function is called when an event occurs in the datafeed
473  * which the output module may need to be aware of. No data is
474  * passed in, only the fact that the event occurs. The following
475  * events can currently be passed in:
476  *
477  * - SR_DF_TRIGGER: At this point in the datafeed, the trigger
478  * matched. The output module may mark this in some way, e.g. by
479  * plotting a red line on a graph.
480  *
481  * - SR_DF_END: This marks the end of the datafeed. No more calls
482  * into the output module will be done, so this is a good time to
483  * free up any memory used to keep state, for example.
484  *
485  * Any output generated by this function must have a reference to
486  * it stored in the <code>data_out</code> and <code>length_out</code>
487  * parameters, or NULL if no output was generated.
488  *
489  * Note: This API call is obsolete, use receive() instead.
490  *
491  * @param o Pointer to the respective 'struct sr_output'.
492  * @param event_type Type of event that occured.
493  * @param data_out Pointer to the allocated output buffer.
494  * @param length_out Length (in bytes) of the output.
495  *
496  * @return SR_OK upon success, a negative error code otherwise.
497  */
498  int (*event) (struct sr_output *o, int event_type, uint8_t **data_out,
499  uint64_t *length_out);
500 
501  /**
502  * This function is passed a copy of every packed in the data feed.
503  * Any output generated by the output module in response to the
504  * packet should be returned in a newly allocated GString
505  * <code>out</code>, which will be freed by the caller.
506  *
507  * Packets not of interest to the output module can just be ignored,
508  * and the <code>out</code> parameter set to NULL.
509  *
510  * @param o Pointer to the respective 'struct sr_output'.
511  * @param sdi The device instance that generated the packet.
512  * @param packet The complete packet.
513  * @param out A pointer where a GString * should be stored if
514  * the module generates output, or NULL if not.
515  *
516  * @return SR_OK upon success, a negative error code otherwise.
517  */
518  int (*receive) (struct sr_output *o, const struct sr_dev_inst *sdi,
519  const struct sr_datafeed_packet *packet, GString **out);
520 
521  /**
522  * This function is called after the caller is finished using
523  * the output module, and can be used to free any internal
524  * resources the module may keep.
525  *
526  * @return SR_OK upon success, a negative error code otherwise.
527  */
528  int (*cleanup) (struct sr_output *o);
529 };
530 
531 enum {
532  SR_PROBE_LOGIC = 10000,
534 };
535 
536 struct sr_probe {
537  /* The index field will go: use g_slist_length(sdi->probes) instead. */
538  int index;
539  int type;
540  gboolean enabled;
541  char *name;
542  char *trigger;
543 };
544 
545 struct sr_config {
546  int key;
547  GVariant *data;
548 };
549 
551  int key;
552  int datatype;
553  char *id;
554  char *name;
555  char *description;
556 };
557 
558 enum {
559  /*--- Device classes ------------------------------------------------*/
560 
561  /** The device can act as logic analyzer. */
563 
564  /** The device can act as an oscilloscope. */
566 
567  /** The device can act as a multimeter. */
569 
570  /** The device is a demo device. */
572 
573  /** The device can act as a sound level meter. */
575 
576  /** The device can measure temperature. */
578 
579  /** The device can measure humidity. */
581 
582  /*--- Driver scan options -------------------------------------------*/
583 
584  /**
585  * Specification on how to connect to a device.
586  *
587  * In combination with SR_CONF_SERIALCOMM, this is a serial port in
588  * the form which makes sense to the OS (e.g., /dev/ttyS0).
589  * Otherwise this specifies a USB device, either in the form of
590  * @verbatim <bus>.<address> @endverbatim (decimal, e.g. 1.65) or
591  * @verbatim <vendorid>.<productid> @endverbatim
592  * (hexadecimal, e.g. 1d6b.0001).
593  */
594  SR_CONF_CONN = 20000,
595 
596  /**
597  * Serial communication specification, in the form:
598  *
599  * @verbatim <baudrate>/<databits><parity><stopbits> @endverbatim
600  *
601  * Example: 9600/8n1
602  *
603  * The string may also be followed by one or more special settings,
604  * in the form "/key=value". Supported keys and their values are:
605  *
606  * rts 0,1 set the port's RTS pin to low or high
607  * dtr 0,1 set the port's DTR pin to low or high
608  * flow 0 no flow control
609  * 1 hardware-based (RTS/CTS) flow control
610  * 2 software-based (XON/XOFF) flow control
611  *
612  * This is always an optional parameter, since a driver typically
613  * knows the speed at which the device wants to communicate.
614  */
616 
617  /*--- Device configuration ------------------------------------------*/
618 
619  /** The device supports setting its samplerate, in Hz. */
621 
622  /** The device supports setting a pre/post-trigger capture ratio. */
624 
625  /** The device supports setting a pattern (pattern generator mode). */
627 
628  /** The device supports Run Length Encoding. */
630 
631  /** The device supports setting trigger slope. */
633 
634  /** Trigger source. */
636 
637  /** Horizontal trigger position. */
639 
640  /** Buffer size. */
642 
643  /** Time base. */
645 
646  /** Filter. */
648 
649  /** Volts/div. */
651 
652  /** Coupling. */
654 
655  /** Trigger types. */
657 
658  /** The device supports setting its sample interval, in ms. */
660 
661  /** Number of timebases, as related to SR_CONF_TIMEBASE. */
663 
664  /** Number of vertical divisions, as related to SR_CONF_VDIV. */
666 
667  /*--- Special stuff -------------------------------------------------*/
668 
669  /** Scan options supported by the driver. */
671 
672  /** Device options for a particular device. */
674 
675  /** Session filename. */
677 
678  /** The device supports specifying a capturefile to inject. */
680 
681  /** The device supports specifying the capturefile unit size. */
683 
684  /** The device supports setting the number of probes. */
686 
687  /*--- Acquisition modes ---------------------------------------------*/
688 
689  /**
690  * The device supports setting a sample time limit (how long
691  * the sample acquisition should run, in ms).
692  */
694 
695  /**
696  * The device supports setting a sample number limit (how many
697  * samples should be acquired).
698  */
700 
701  /**
702  * The device supports setting a frame limit (how many
703  * frames should be acquired).
704  */
706 
707  /**
708  * The device supports continuous sampling. Neither a time limit
709  * nor a sample number limit has to be supplied, it will just acquire
710  * samples continuously, until explicitly stopped by a certain command.
711  */
713 
714  /** The device has internal storage, into which data is logged. This
715  * starts or stops the internal logging. */
717 };
718 
719 struct sr_dev_inst {
721  int index;
722  int status;
724  char *vendor;
725  char *model;
726  char *version;
727  GSList *probes;
728  void *conn;
729  void *priv;
730 };
731 
732 /** Types of device instances (sr_dev_inst). */
733 enum {
734  /** Device instance type for USB devices. */
735  SR_INST_USB = 10000,
736  /** Device instance type for serial port devices. */
738 };
739 
740 /** Device instance status. */
741 enum {
742  /** The device instance was not found. */
744  /** The device instance was found, but is still booting. */
746  /** The device instance is live, but not in use. */
748  /** The device instance is actively in use in a session. */
750  /** The device is winding down its session. */
752 };
753 
755  /* Driver-specific */
756  char *name;
757  char *longname;
759  int (*init) (struct sr_context *sr_ctx);
760  int (*cleanup) (void);
761  GSList *(*scan) (GSList *options);
762  GSList *(*dev_list) (void);
763  int (*dev_clear) (void);
764  int (*config_get) (int id, GVariant **data,
765  const struct sr_dev_inst *sdi);
766  int (*config_set) (int id, GVariant *data,
767  const struct sr_dev_inst *sdi);
768  int (*config_list) (int info_id, GVariant **data,
769  const struct sr_dev_inst *sdi);
770 
771  /* Device-specific */
772  int (*dev_open) (struct sr_dev_inst *sdi);
773  int (*dev_close) (struct sr_dev_inst *sdi);
774  int (*dev_acquisition_start) (const struct sr_dev_inst *sdi,
775  void *cb_data);
776  int (*dev_acquisition_stop) (struct sr_dev_inst *sdi,
777  void *cb_data);
778 
779  /* Dynamic */
780  void *priv;
781 };
782 
783 struct sr_session {
784  /** List of struct sr_dev pointers. */
785  GSList *devs;
786  /** List of struct datafeed_callback pointers. */
788  GTimeVal starttime;
789 
790  unsigned int num_sources;
791 
792  /*
793  * Both "sources" and "pollfds" are of the same size and contain pairs
794  * of descriptor and callback function. We can not embed the GPollFD
795  * into the source struct since we want to be able to pass the array
796  * of all poll descriptors to g_poll().
797  */
798  struct source *sources;
799  GPollFD *pollfds;
801 
802  /*
803  * These are our synchronization primitives for stopping the session in
804  * an async fashion. We need to make sure the session is stopped from
805  * within the session thread itself.
806  */
807  GMutex stop_mutex;
808  gboolean abort_session;
809 };
810 
811 #include "proto.h"
812 #include "version.h"
813 
814 #ifdef __cplusplus
815 }
816 #endif
817 
818 #endif