2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef LIBSIGROK_SIGROK_H
21 #define LIBSIGROK_SIGROK_H
36 * The public libsigrok header file to be used by frontends.
40 * All possible return codes of libsigrok functions must be listed here.
41 * Functions should never return hardcoded numbers as status, but rather
42 * use these enum values. All error codes are negative numbers.
44 * The error codes are globally unique in libsigrok, i.e. if one of the
45 * libsigrok functions returns a "malloc error" it must be exactly the same
46 * return value as used by all other functions to indicate "malloc error".
47 * There must be no functions which indicate two different errors via the
50 * Also, for compatibility reasons, no defined return codes are ever removed
51 * or reused for different errors later. You can only add new entries and
52 * return codes, but never remove or redefine existing ones.
55 /** Status/error codes returned by libsigrok functions. */
57 SR_OK = 0, /**< No error. */
58 SR_ERR = -1, /**< Generic/unspecified error. */
59 SR_ERR_MALLOC = -2, /**< Malloc/calloc/realloc error. */
60 SR_ERR_ARG = -3, /**< Function argument error. */
61 SR_ERR_BUG = -4, /**< Errors hinting at internal bugs. */
62 SR_ERR_SAMPLERATE = -5, /**< Incorrect samplerate. */
65 #define SR_MAX_NUM_PROBES 64 /* Limited by uint64_t. */
66 #define SR_MAX_PROBENAME_LEN 32
68 /* Handy little macros */
70 #define SR_KHZ(n) ((n) * 1000)
71 #define SR_MHZ(n) ((n) * 1000000)
72 #define SR_GHZ(n) ((n) * 1000000000)
74 #define SR_HZ_TO_NS(n) (1000000000 / (n))
76 /** libsigrok loglevels. */
78 SR_LOG_NONE = 0, /**< Output no messages at all. */
79 SR_LOG_ERR = 1, /**< Output error messages. */
80 SR_LOG_WARN = 2, /**< Output warnings. */
81 SR_LOG_INFO = 3, /**< Output informational messages. */
82 SR_LOG_DBG = 4, /**< Output debug messages. */
83 SR_LOG_SPEW = 5, /**< Output very noisy debug messages. */
87 * Use SR_API to mark public API symbols, and SR_PRIV for private symbols.
89 * Variables and functions marked 'static' are private already and don't
90 * need SR_PRIV. However, functions which are not static (because they need
91 * to be used in other libsigrok-internal files) but are also not meant to
92 * be part of the public libsigrok API, must use SR_PRIV.
94 * This uses the 'visibility' feature of gcc (requires gcc >= 4.0).
96 * This feature is not available on MinGW/Windows, as it is a feature of
97 * ELF files and MinGW/Windows uses PE files.
99 * Details: http://gcc.gnu.org/wiki/Visibility
102 /* Marks public libsigrok API symbols. */
104 #define SR_API __attribute__((visibility("default")))
109 /* Marks private, non-public libsigrok symbols (not part of the API). */
111 #define SR_PRIV __attribute__((visibility("hidden")))
116 typedef int (*sr_receive_data_callback_t)(int fd, int revents, void *cb_data);
118 /** Data types used by hardware drivers for dev_config_set(). */
124 SR_T_RATIONAL_PERIOD,
129 /** Rational number data type, containing numerator and denominator values. */
131 /** Numerator of the rational number. */
133 /** Denominator of the rational number. */
137 /** Value for sr_datafeed_packet.type. */
150 /** Values for sr_datafeed_analog.mq. */
162 /** Electrical power, usually in W, or dBm. */
164 /** Gain (e.g. a transistor's gain, or hFE). */
168 /** Values for sr_datafeed_analog.unit. */
181 /** Unit of conductance, the inverse of resistance. */
184 * An absolute measurement of power, in decibels, referenced to
188 /** Voltage in decibel, referenced to 1 volt (dBV). */
189 SR_UNIT_DECIBEL_VOLT,
191 * Measurements that intrinsically do not have units attached, such
192 * as ratios, gains, etc. Specifically, a transistor's gain (hFE) is
193 * a unitless quantity, for example.
198 /** Values for sr_datafeed_analog.flags. */
200 /** Voltage measurement is alternating current (AC). */
202 /** Voltage measurement is direct current (DC). */
204 /** This is a true RMS measurement. */
205 SR_MQFLAG_RMS = 0x04,
206 /** Value is voltage drop across a diode, or NAN. */
207 SR_MQFLAG_DIODE = 0x08,
208 /** Device is in "hold" mode, i.e. repeating the last measurement. */
209 SR_MQFLAG_HOLD = 0x10,
210 /** Device is in "max" mode, only updating upon a new max value. */
211 SR_MQFLAG_MAX = 0x20,
212 /** Device is in "min" mode, only updating upon a new min value. */
213 SR_MQFLAG_MIN = 0x40,
214 /** Device is in autoranging mode. */
215 SR_MQFLAG_AUTORANGE = 0x80,
216 /** Device is in relative mode. */
217 SR_MQFLAG_RELATIVE = 0x100,
222 struct sr_datafeed_packet {
227 struct sr_datafeed_header {
229 struct timeval starttime;
232 struct sr_datafeed_meta_logic {
237 struct sr_datafeed_logic {
243 struct sr_datafeed_meta_analog {
247 struct sr_datafeed_analog {
249 /** Measured quantity (e.g. voltage, current, temperature). */
251 /** Unit in which the MQ is measured. */
253 /** Bitmap with extra information about the MQ. */
255 /** The analog value. */
260 struct sr_input_format *format;
262 struct sr_dev_inst *sdi;
266 struct sr_input_format {
269 int (*format_match) (const char *filename);
270 int (*init) (struct sr_input *in);
271 int (*loadfile) (struct sr_input *in, const char *filename);
275 struct sr_output_format *format;
276 struct sr_dev_inst *sdi;
281 struct sr_output_format {
285 int (*init) (struct sr_output *o);
286 int (*data) (struct sr_output *o, const uint8_t *data_in,
287 uint64_t length_in, uint8_t **data_out,
288 uint64_t *length_out);
289 int (*event) (struct sr_output *o, int event_type, uint8_t **data_out,
290 uint64_t *length_out);
291 GString *(*recv) (struct sr_output *o, const struct sr_dev_inst *sdi,
292 struct sr_datafeed_packet *packet);
293 int (*cleanup) (struct sr_output *o);
296 struct sr_datastore {
297 /** Size in bytes of the number of units stored in this datastore. */
299 unsigned int num_units; /* TODO: uint64_t */
304 * This represents a generic device connected to the system.
305 * For device-specific information, ask the driver. The driver_index refers
306 * to the device index within that driver; it may be handling more than one
307 * device. All relevant driver calls take a dev_index parameter for this.
310 /** Which driver handles this device. */
311 struct sr_dev_driver *driver;
312 /** A driver may handle multiple devices of the same type. */
314 /** List of struct sr_probe pointers. */
316 /** Data acquired by this device, if any. */
317 struct sr_datastore *datastore;
338 /** Hardware driver options. */
340 /** Used to terminate lists. Must be 0! */
344 * Some drivers cannot detect the exact model they're talking to
345 * (may be phased out).
350 * Specification on how to connect to a device. In combination
351 * with SR_HWOPT_SERIALCOMM, this is a serial port in the form
352 * which makes sense to the operating system (e.g., /dev/ttyS0).
353 * Otherwise this specifies a USB device, either in the form of
354 * [bus].[address] (decimal, e.g. 1.65) or [vendorid].[productid]
355 * (hexadecimal, e.g. 1d6b.0001).
360 * Serial communication specification, in the form:
361 * [baudrate]/[databits][parity][stopbits], e.g. 9600/8n1
363 * This is always an optional parameter, since a driver typically
364 * knows the speed at which the device wants to communicate.
369 /** Hardware device capabilities. */
371 /** Used to terminate lists. Must be 0! */
374 /*--- Device classes ------------------------------------------------*/
376 /** The device can act as logic analyzer. */
377 SR_HWCAP_LOGIC_ANALYZER,
379 /** The device can act as an oscilloscope. */
380 SR_HWCAP_OSCILLOSCOPE,
382 /** The device can act as a multimeter. */
385 /** The device is a demo device. */
389 /*--- Device configuration ------------------------------------------*/
391 /** The device supports setting/changing its samplerate. */
394 /** The device supports setting a pre/post-trigger capture ratio. */
395 SR_HWCAP_CAPTURE_RATIO,
398 /** The device supports setting a pattern (pattern generator mode). */
399 SR_HWCAP_PATTERN_MODE,
401 /** The device supports Run Length Encoding. */
404 /** The device supports setting trigger slope. */
405 SR_HWCAP_TRIGGER_SLOPE,
407 /** Trigger source. */
408 SR_HWCAP_TRIGGER_SOURCE,
410 /** Horizontal trigger position. */
411 SR_HWCAP_HORIZ_TRIGGERPOS,
429 /*--- Special stuff -------------------------------------------------*/
431 /** Session filename. */
432 SR_HWCAP_SESSIONFILE,
434 /* TODO: Better description. */
435 /** The device supports specifying a capturefile to inject. */
436 SR_HWCAP_CAPTUREFILE,
438 /* TODO: Better description. */
439 /** The device supports specifying the capturefile unit size. */
440 SR_HWCAP_CAPTURE_UNITSIZE,
442 /* TODO: Better description. */
443 /** The device supports setting the number of probes. */
444 SR_HWCAP_CAPTURE_NUM_PROBES,
447 /*--- Acquisition modes ---------------------------------------------*/
450 * The device supports setting a sample time limit, i.e. how long the
451 * sample acquisition should run (in ms).
456 * The device supports setting a sample number limit, i.e. how many
457 * samples should be acquired.
459 SR_HWCAP_LIMIT_SAMPLES,
462 * The device supports setting a frame limit, i.e. how many
463 * frames should be acquired.
465 SR_HWCAP_LIMIT_FRAMES,
468 * The device supports continuous sampling, i.e. neither a time limit
469 * nor a sample number limit has to be supplied, it will just acquire
470 * samples continuously, until explicitly stopped by a certain command.
476 struct sr_hwcap_option {
484 struct sr_dev_driver *driver;
495 /** Types of device instances (sr_dev_inst). */
497 /** Device instance type for USB devices. */
499 /** Device instance type for serial port devices. */
503 /** Device instance status. */
505 /** The device instance was not found. */
507 /** The device instance was found, but is still booting. */
509 /** The device instance is live, but not in use. */
511 /** The device instance is actively in use in a session. */
516 * TODO: This sucks, you just kinda have to "know" the returned type.
517 * TODO: Need a DI to return the number of trigger stages supported.
520 /** Device info IDs. */
522 /** A list of options supported by the driver. */
524 /** A list of capabilities supported by the device. */
526 /** The number of probes connected to this device. */
528 /** The probe names on this device. */
530 /** Samplerates supported by this device (struct sr_samplerates). */
532 /** Types of logic trigger supported, out of "01crf" (char *). */
534 /** The currently set samplerate in Hz (uint64_t). */
535 SR_DI_CUR_SAMPLERATE,
536 /** Supported patterns (in pattern generator mode). */
538 /** Supported buffer sizes. */
540 /** Supported time bases. */
542 /** Supported trigger sources. */
543 SR_DI_TRIGGER_SOURCES,
544 /** Supported filter targets. */
546 /** Valid volts/div values. */
548 /** Coupling options. */
553 * A device supports either a range of samplerates with steps of a given
554 * granularity, or is limited to a set of defined samplerates. Use either
555 * step or list, but not both.
557 struct sr_samplerates {
561 const uint64_t *list;
564 struct sr_dev_driver {
565 /* Driver-specific */
570 int (*cleanup) (void);
571 GSList *(*scan) (GSList *options);
572 GSList *(*dev_list) (void);
573 int (*dev_clear) (void);
575 /* Device-specific */
576 int (*dev_open) (struct sr_dev_inst *sdi);
577 int (*dev_close) (struct sr_dev_inst *sdi);
578 int (*info_get) (int info_id, const void **data,
579 const struct sr_dev_inst *sdi);
580 int (*dev_config_set) (const struct sr_dev_inst *sdi, int hwcap,
582 int (*dev_acquisition_start) (const struct sr_dev_inst *sdi,
584 int (*dev_acquisition_stop) (const struct sr_dev_inst *sdi,
592 /** List of struct sr_dev pointers. */
594 /** List of sr_receive_data_callback_t items. */
595 GSList *datafeed_callbacks;
598 unsigned int num_sources;
601 * Both "sources" and "pollfds" are of the same size and contain pairs
602 * of descriptor and callback function. We can not embed the GPollFD
603 * into the source struct since we want to be able to pass the array
604 * of all poll descriptors to g_poll().
606 struct source *sources;