]> sigrok.org Git - libsigrok.git/blob - libsigrok.h
Doxygen: Various fixes in libsigrok.h.
[libsigrok.git] / libsigrok.h
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 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
39 /*
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.
43  *
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
48  * same return code.
49  *
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.
53  */
54
55 /** Status/error codes returned by libsigrok functions. */
56 enum {
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. */
63 };
64
65 #define SR_MAX_NUM_PROBES    64 /* Limited by uint64_t. */
66 #define SR_MAX_PROBENAME_LEN 32
67
68 /* Handy little macros */
69 #define SR_HZ(n)  (n)
70 #define SR_KHZ(n) ((n) * 1000)
71 #define SR_MHZ(n) ((n) * 1000000)
72 #define SR_GHZ(n) ((n) * 1000000000)
73
74 #define SR_HZ_TO_NS(n) (1000000000 / (n))
75
76 /** libsigrok loglevels. */
77 enum {
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. */
84 };
85
86 /*
87  * Use SR_API to mark public API symbols, and SR_PRIV for private symbols.
88  *
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.
93  *
94  * This uses the 'visibility' feature of gcc (requires gcc >= 4.0).
95  *
96  * This feature is not available on MinGW/Windows, as it is a feature of
97  * ELF files and MinGW/Windows uses PE files.
98  *
99  * Details: http://gcc.gnu.org/wiki/Visibility
100  */
101
102 /* Marks public libsigrok API symbols. */
103 #ifndef _WIN32
104 #define SR_API __attribute__((visibility("default")))
105 #else
106 #define SR_API
107 #endif
108
109 /* Marks private, non-public libsigrok symbols (not part of the API). */
110 #ifndef _WIN32
111 #define SR_PRIV __attribute__((visibility("hidden")))
112 #else
113 #define SR_PRIV
114 #endif
115
116 typedef int (*sr_receive_data_callback_t)(int fd, int revents, void *cb_data);
117
118 /** Data types used by hardware drivers for dev_config_set(). */
119 enum {
120         SR_T_UINT64,
121         SR_T_CHAR,
122         SR_T_BOOL,
123         SR_T_FLOAT,
124         SR_T_RATIONAL_PERIOD,
125         SR_T_RATIONAL_VOLT,
126         SR_T_KEYVALUE,
127 };
128
129 /** Rational number data type, containing numerator and denominator values. */
130 struct sr_rational {
131         /** Numerator of the rational number. */
132         uint64_t p;
133         /** Denominator of the rational number. */
134         uint64_t q;
135 };
136
137 /** Value for sr_datafeed_packet.type. */
138 enum {
139         SR_DF_HEADER,
140         SR_DF_END,
141         SR_DF_TRIGGER,
142         SR_DF_LOGIC,
143         SR_DF_META_LOGIC,
144         SR_DF_ANALOG,
145         SR_DF_META_ANALOG,
146         SR_DF_FRAME_BEGIN,
147         SR_DF_FRAME_END,
148 };
149
150 /** Values for sr_datafeed_analog.mq. */
151 enum {
152         SR_MQ_VOLTAGE,
153         SR_MQ_CURRENT,
154         SR_MQ_RESISTANCE,
155         SR_MQ_CAPACITANCE,
156         SR_MQ_TEMPERATURE,
157         SR_MQ_FREQUENCY,
158         SR_MQ_DUTY_CYCLE,
159         SR_MQ_CONTINUITY,
160         SR_MQ_PULSE_WIDTH,
161         SR_MQ_CONDUCTANCE,
162         /** Electrical power, usually in W, or dBm. */
163         SR_MQ_POWER,
164         /** Gain (a transistor's gain, or hFE, for example). */
165         SR_MQ_GAIN,
166 };
167
168 /** Values for sr_datafeed_analog.unit. */
169 enum {
170         SR_UNIT_VOLT,
171         SR_UNIT_AMPERE,
172         SR_UNIT_OHM,
173         SR_UNIT_FARAD,
174         SR_UNIT_KELVIN,
175         SR_UNIT_CELSIUS,
176         SR_UNIT_FAHRENHEIT,
177         SR_UNIT_HERTZ,
178         SR_UNIT_PERCENTAGE,
179         SR_UNIT_BOOLEAN,
180         SR_UNIT_SECOND,
181         /** Unit of conductance, the inverse of resistance. */
182         SR_UNIT_SIEMENS,
183         /**
184          * An absolute measurement of power, in decibels, referenced to
185          * 1 milliwatt (dBu).
186          */
187         SR_UNIT_DECIBEL_MW,
188         /** Voltage in decibel, referenced to 1 volt (dBV). */
189         SR_UNIT_DECIBEL_VOLT,
190         /**
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.
194          */
195         SR_UNIT_UNITLESS,
196 };
197
198 /** Values for sr_datafeed_analog.flags. */
199 enum {
200         /** Voltage measurement is alternating current (AC). */
201         SR_MQFLAG_AC = 0x01,
202         /** Voltage measurement is direct current (DC). */
203         SR_MQFLAG_DC = 0x02,
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 (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,
218 };
219
220 struct sr_context;
221
222 struct sr_datafeed_packet {
223         uint16_t type;
224         void *payload;
225 };
226
227 struct sr_datafeed_header {
228         int feed_version;
229         struct timeval starttime;
230 };
231
232 struct sr_datafeed_meta_logic {
233         int num_probes;
234         uint64_t samplerate;
235 };
236
237 struct sr_datafeed_logic {
238         uint64_t length;
239         uint16_t unitsize;
240         void *data;
241 };
242
243 struct sr_datafeed_meta_analog {
244         int num_probes;
245 };
246
247 struct sr_datafeed_analog {
248         int num_samples;
249         /** Measured quantity (voltage, current, temperature, and so on). */
250         int mq;
251         /** Unit in which the MQ is measured. */
252         int unit;
253         /** Bitmap with extra information about the MQ. */
254         uint64_t mqflags;
255         /** The analog value. */
256         float *data;
257 };
258
259 struct sr_input {
260         struct sr_input_format *format;
261         GHashTable *param;
262         struct sr_dev_inst *sdi;
263         void *internal;
264 };
265
266 struct sr_input_format {
267         char *id;
268         char *description;
269         int (*format_match) (const char *filename);
270         int (*init) (struct sr_input *in);
271         int (*loadfile) (struct sr_input *in, const char *filename);
272 };
273
274 struct sr_output {
275         struct sr_output_format *format;
276         struct sr_dev_inst *sdi;
277         char *param;
278         void *internal;
279 };
280
281 struct sr_output_format {
282         char *id;
283         char *description;
284         int df_type;
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);
294 };
295
296 struct sr_datastore {
297         /** Size in bytes of the number of units stored in this datastore. */
298         int ds_unitsize;
299         unsigned int num_units; /* TODO: uint64_t */
300         GSList *chunklist;
301 };
302
303 /*
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.
308  */
309 struct sr_dev {
310         /** Which driver handles this device. */
311         struct sr_dev_driver *driver;
312         /** A driver may handle multiple devices of the same type. */
313         int driver_index;
314         /** List of struct sr_probe pointers. */
315         GSList *probes;
316         /** Data acquired by this device, if any. */
317         struct sr_datastore *datastore;
318 };
319
320 enum {
321         SR_PROBE_LOGIC,
322         SR_PROBE_ANALOG,
323 };
324
325 struct sr_probe {
326         int index;
327         int type;
328         gboolean enabled;
329         char *name;
330         char *trigger;
331 };
332
333 struct sr_hwopt {
334         int hwopt;
335         const void *value;
336 };
337
338 /** Hardware driver options. */
339 enum {
340         /** Used to terminate lists. */
341         SR_HWOPT_DUMMY = 0, /* Must be 0! */
342
343         /**
344          * Some drivers cannot detect the exact model they're talking to
345          * (may be phased out).
346          */
347         SR_HWOPT_MODEL,
348
349         /**
350          * Specification on how to connect to a device.
351          *
352          * In combination with SR_HWOPT_SERIALCOMM, this is a serial port in
353          * the form which makes sense to the OS (e.g., /dev/ttyS0).
354          * Otherwise this specifies a USB device, either in the form of
355          * @verbatim <bus>.<address> @endverbatim (decimal, e.g. 1.65) or
356          * @verbatim <vendorid>.<productid> @endverbatim
357          * (hexadecimal, e.g. 1d6b.0001).
358          */
359         SR_HWOPT_CONN,
360
361         /**
362          * Serial communication specification, in the form:
363          *
364          *   @verbatim <baudrate>/<databits><parity><stopbits> @endverbatim
365          *
366          * Example: 9600/8n1
367          *
368          * This is always an optional parameter, since a driver typically
369          * knows the speed at which the device wants to communicate.
370          */
371         SR_HWOPT_SERIALCOMM,
372 };
373
374 /** Hardware device capabilities. */
375 enum {
376         /** Used to terminate lists. */
377         SR_HWCAP_DUMMY = 0, /* Must be 0! */
378
379         /*--- Device classes ------------------------------------------------*/
380
381         /** The device can act as logic analyzer. */
382         SR_HWCAP_LOGIC_ANALYZER,
383
384         /** The device can act as an oscilloscope. */
385         SR_HWCAP_OSCILLOSCOPE,
386
387         /** The device can act as a multimeter. */
388         SR_HWCAP_MULTIMETER,
389
390         /** The device is a demo device. */
391         SR_HWCAP_DEMO_DEV,
392
393
394         /*--- Device configuration ------------------------------------------*/
395
396         /** The device supports setting/changing its samplerate. */
397         SR_HWCAP_SAMPLERATE,
398
399         /** The device supports setting a pre/post-trigger capture ratio. */
400         SR_HWCAP_CAPTURE_RATIO,
401
402         /* TODO? */
403         /** The device supports setting a pattern (pattern generator mode). */
404         SR_HWCAP_PATTERN_MODE,
405
406         /** The device supports Run Length Encoding. */
407         SR_HWCAP_RLE,
408
409         /** The device supports setting trigger slope. */
410         SR_HWCAP_TRIGGER_SLOPE,
411
412         /** Trigger source. */
413         SR_HWCAP_TRIGGER_SOURCE,
414
415         /** Horizontal trigger position. */
416         SR_HWCAP_HORIZ_TRIGGERPOS,
417
418         /** Buffer size. */
419         SR_HWCAP_BUFFERSIZE,
420
421         /** Time base. */
422         SR_HWCAP_TIMEBASE,
423
424         /** Filter. */
425         SR_HWCAP_FILTER,
426
427         /** Volts/div. */
428         SR_HWCAP_VDIV,
429
430         /** Coupling. */
431         SR_HWCAP_COUPLING,
432
433
434         /*--- Special stuff -------------------------------------------------*/
435
436         /** Session filename. */
437         SR_HWCAP_SESSIONFILE,
438
439         /* TODO: Better description. */
440         /** The device supports specifying a capturefile to inject. */
441         SR_HWCAP_CAPTUREFILE,
442
443         /* TODO: Better description. */
444         /** The device supports specifying the capturefile unit size. */
445         SR_HWCAP_CAPTURE_UNITSIZE,
446
447         /* TODO: Better description. */
448         /** The device supports setting the number of probes. */
449         SR_HWCAP_CAPTURE_NUM_PROBES,
450
451
452         /*--- Acquisition modes ---------------------------------------------*/
453
454         /**
455          * The device supports setting a sample time limit (how long
456          * the sample acquisition should run, in ms).
457          */
458         SR_HWCAP_LIMIT_MSEC,
459
460         /**
461          * The device supports setting a sample number limit (how many
462          * samples should be acquired).
463          */
464         SR_HWCAP_LIMIT_SAMPLES,
465
466         /**
467          * The device supports setting a frame limit (how many
468          * frames should be acquired).
469          */
470         SR_HWCAP_LIMIT_FRAMES,
471
472         /**
473          * The device supports continuous sampling. Neither a time limit
474          * nor a sample number limit has to be supplied, it will just acquire
475          * samples continuously, until explicitly stopped by a certain command.
476          */
477         SR_HWCAP_CONTINUOUS,
478
479 };
480
481 struct sr_hwcap_option {
482         int hwcap;
483         int type;
484         char *description;
485         char *shortname;
486 };
487
488 struct sr_dev_inst {
489         struct sr_dev_driver *driver;
490         int index;
491         int status;
492         int inst_type;
493         char *vendor;
494         char *model;
495         char *version;
496         GSList *probes;
497         void *priv;
498 };
499
500 /** Types of device instances (sr_dev_inst). */
501 enum {
502         /** Device instance type for USB devices. */
503         SR_INST_USB,
504         /** Device instance type for serial port devices. */
505         SR_INST_SERIAL,
506 };
507
508 /** Device instance status. */
509 enum {
510         /** The device instance was not found. */
511         SR_ST_NOT_FOUND,
512         /** The device instance was found, but is still booting. */
513         SR_ST_INITIALIZING,
514         /** The device instance is live, but not in use. */
515         SR_ST_INACTIVE,
516         /** The device instance is actively in use in a session. */
517         SR_ST_ACTIVE,
518 };
519
520 /*
521  * TODO: This sucks, you just kinda have to "know" the returned type.
522  * TODO: Need a DI to return the number of trigger stages supported.
523  */
524
525 /** Device info IDs. */
526 enum {
527         /** A list of options supported by the driver. */
528         SR_DI_HWOPTS,
529         /** A list of capabilities supported by the device. */
530         SR_DI_HWCAPS,
531         /** The number of probes connected to this device. */
532         SR_DI_NUM_PROBES,
533         /** The probe names on this device. */
534         SR_DI_PROBE_NAMES,
535         /** Samplerates supported by this device (struct sr_samplerates). */
536         SR_DI_SAMPLERATES,
537         /** Types of logic trigger supported, out of "01crf" (char *). */
538         SR_DI_TRIGGER_TYPES,
539         /** The currently set samplerate in Hz (uint64_t). */
540         SR_DI_CUR_SAMPLERATE,
541         /** Supported patterns (in pattern generator mode). */
542         SR_DI_PATTERNS,
543         /** Supported buffer sizes. */
544         SR_DI_BUFFERSIZES,
545         /** Supported time bases. */
546         SR_DI_TIMEBASES,
547         /** Supported trigger sources. */
548         SR_DI_TRIGGER_SOURCES,
549         /** Supported filter targets. */
550         SR_DI_FILTERS,
551         /** Valid volts/div values. */
552         SR_DI_VDIVS,
553         /** Coupling options. */
554         SR_DI_COUPLING,
555 };
556
557 /*
558  * A device supports either a range of samplerates with steps of a given
559  * granularity, or is limited to a set of defined samplerates. Use either
560  * step or list, but not both.
561  */
562 struct sr_samplerates {
563         uint64_t low;
564         uint64_t high;
565         uint64_t step;
566         const uint64_t *list;
567 };
568
569 struct sr_dev_driver {
570         /* Driver-specific */
571         char *name;
572         char *longname;
573         int api_version;
574         int (*init) (void);
575         int (*cleanup) (void);
576         GSList *(*scan) (GSList *options);
577         GSList *(*dev_list) (void);
578         int (*dev_clear) (void);
579
580         /* Device-specific */
581         int (*dev_open) (struct sr_dev_inst *sdi);
582         int (*dev_close) (struct sr_dev_inst *sdi);
583         int (*info_get) (int info_id, const void **data,
584                         const struct sr_dev_inst *sdi);
585         int (*dev_config_set) (const struct sr_dev_inst *sdi, int hwcap,
586                         const void *value);
587         int (*dev_acquisition_start) (const struct sr_dev_inst *sdi,
588                         void *cb_data);
589         int (*dev_acquisition_stop) (const struct sr_dev_inst *sdi,
590                         void *cb_data);
591
592         /* Dynamic */
593         void *priv;
594 };
595
596 struct sr_session {
597         /** List of struct sr_dev pointers. */
598         GSList *devs;
599         /** List of sr_receive_data_callback_t items. */
600         GSList *datafeed_callbacks;
601         GTimeVal starttime;
602
603         unsigned int num_sources;
604
605         /*
606          * Both "sources" and "pollfds" are of the same size and contain pairs
607          * of descriptor and callback function. We can not embed the GPollFD
608          * into the source struct since we want to be able to pass the array
609          * of all poll descriptors to g_poll().
610          */
611         struct source *sources;
612         GPollFD *pollfds;
613         int source_timeout;
614 };
615
616 #include "proto.h"
617 #include "version.h"
618
619 #ifdef __cplusplus
620 }
621 #endif
622
623 #endif