]> sigrok.org Git - libsigrok.git/blob - libsigrok.h
Doxygen: Add @file items for the relevant files.
[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 (e.g. a transistor's gain, or hFE). */
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, 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,
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 (e.g. voltage, current, temperature). */
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. Must be 0! */
341         SR_HWOPT_DUMMY = 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. 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).
356          */
357         SR_HWOPT_CONN,
358
359         /**
360          * Serial communication specification, in the form:
361          *   [baudrate]/[databits][parity][stopbits], e.g. 9600/8n1
362          *
363          * This is always an optional parameter, since a driver typically
364          * knows the speed at which the device wants to communicate.
365          */
366         SR_HWOPT_SERIALCOMM,
367 };
368
369 /** Hardware device capabilities. */
370 enum {
371         /** Used to terminate lists. Must be 0! */
372         SR_HWCAP_DUMMY = 0,
373
374         /*--- Device classes ------------------------------------------------*/
375
376         /** The device can act as logic analyzer. */
377         SR_HWCAP_LOGIC_ANALYZER,
378
379         /** The device can act as an oscilloscope. */
380         SR_HWCAP_OSCILLOSCOPE,
381
382         /** The device can act as a multimeter. */
383         SR_HWCAP_MULTIMETER,
384
385         /** The device is a demo device. */
386         SR_HWCAP_DEMO_DEV,
387
388
389         /*--- Device configuration ------------------------------------------*/
390
391         /** The device supports setting/changing its samplerate. */
392         SR_HWCAP_SAMPLERATE,
393
394         /** The device supports setting a pre/post-trigger capture ratio. */
395         SR_HWCAP_CAPTURE_RATIO,
396
397         /* TODO? */
398         /** The device supports setting a pattern (pattern generator mode). */
399         SR_HWCAP_PATTERN_MODE,
400
401         /** The device supports Run Length Encoding. */
402         SR_HWCAP_RLE,
403
404         /** The device supports setting trigger slope. */
405         SR_HWCAP_TRIGGER_SLOPE,
406
407         /** Trigger source. */
408         SR_HWCAP_TRIGGER_SOURCE,
409
410         /** Horizontal trigger position. */
411         SR_HWCAP_HORIZ_TRIGGERPOS,
412
413         /** Buffer size. */
414         SR_HWCAP_BUFFERSIZE,
415
416         /** Time base. */
417         SR_HWCAP_TIMEBASE,
418
419         /** Filter. */
420         SR_HWCAP_FILTER,
421
422         /** Volts/div. */
423         SR_HWCAP_VDIV,
424
425         /** Coupling. */
426         SR_HWCAP_COUPLING,
427
428
429         /*--- Special stuff -------------------------------------------------*/
430
431         /** Session filename. */
432         SR_HWCAP_SESSIONFILE,
433
434         /* TODO: Better description. */
435         /** The device supports specifying a capturefile to inject. */
436         SR_HWCAP_CAPTUREFILE,
437
438         /* TODO: Better description. */
439         /** The device supports specifying the capturefile unit size. */
440         SR_HWCAP_CAPTURE_UNITSIZE,
441
442         /* TODO: Better description. */
443         /** The device supports setting the number of probes. */
444         SR_HWCAP_CAPTURE_NUM_PROBES,
445
446
447         /*--- Acquisition modes ---------------------------------------------*/
448
449         /**
450          * The device supports setting a sample time limit, i.e. how long the
451          * sample acquisition should run (in ms).
452          */
453         SR_HWCAP_LIMIT_MSEC,
454
455         /**
456          * The device supports setting a sample number limit, i.e. how many
457          * samples should be acquired.
458          */
459         SR_HWCAP_LIMIT_SAMPLES,
460
461         /**
462          * The device supports setting a frame limit, i.e. how many
463          * frames should be acquired.
464          */
465         SR_HWCAP_LIMIT_FRAMES,
466
467         /**
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.
471          */
472         SR_HWCAP_CONTINUOUS,
473
474 };
475
476 struct sr_hwcap_option {
477         int hwcap;
478         int type;
479         char *description;
480         char *shortname;
481 };
482
483 struct sr_dev_inst {
484         struct sr_dev_driver *driver;
485         int index;
486         int status;
487         int inst_type;
488         char *vendor;
489         char *model;
490         char *version;
491         GSList *probes;
492         void *priv;
493 };
494
495 /** Types of device instances (sr_dev_inst). */
496 enum {
497         /** Device instance type for USB devices. */
498         SR_INST_USB,
499         /** Device instance type for serial port devices. */
500         SR_INST_SERIAL,
501 };
502
503 /** Device instance status. */
504 enum {
505         /** The device instance was not found. */
506         SR_ST_NOT_FOUND,
507         /** The device instance was found, but is still booting. */
508         SR_ST_INITIALIZING,
509         /** The device instance is live, but not in use. */
510         SR_ST_INACTIVE,
511         /** The device instance is actively in use in a session. */
512         SR_ST_ACTIVE,
513 };
514
515 /*
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.
518  */
519
520 /** Device info IDs. */
521 enum {
522         /** A list of options supported by the driver. */
523         SR_DI_HWOPTS,
524         /** A list of capabilities supported by the device. */
525         SR_DI_HWCAPS,
526         /** The number of probes connected to this device. */
527         SR_DI_NUM_PROBES,
528         /** The probe names on this device. */
529         SR_DI_PROBE_NAMES,
530         /** Samplerates supported by this device (struct sr_samplerates). */
531         SR_DI_SAMPLERATES,
532         /** Types of logic trigger supported, out of "01crf" (char *). */
533         SR_DI_TRIGGER_TYPES,
534         /** The currently set samplerate in Hz (uint64_t). */
535         SR_DI_CUR_SAMPLERATE,
536         /** Supported patterns (in pattern generator mode). */
537         SR_DI_PATTERNS,
538         /** Supported buffer sizes. */
539         SR_DI_BUFFERSIZES,
540         /** Supported time bases. */
541         SR_DI_TIMEBASES,
542         /** Supported trigger sources. */
543         SR_DI_TRIGGER_SOURCES,
544         /** Supported filter targets. */
545         SR_DI_FILTERS,
546         /** Valid volts/div values. */
547         SR_DI_VDIVS,
548         /** Coupling options. */
549         SR_DI_COUPLING,
550 };
551
552 /*
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.
556  */
557 struct sr_samplerates {
558         uint64_t low;
559         uint64_t high;
560         uint64_t step;
561         const uint64_t *list;
562 };
563
564 struct sr_dev_driver {
565         /* Driver-specific */
566         char *name;
567         char *longname;
568         int api_version;
569         int (*init) (void);
570         int (*cleanup) (void);
571         GSList *(*scan) (GSList *options);
572         GSList *(*dev_list) (void);
573         int (*dev_clear) (void);
574
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,
581                         const void *value);
582         int (*dev_acquisition_start) (const struct sr_dev_inst *sdi,
583                         void *cb_data);
584         int (*dev_acquisition_stop) (const struct sr_dev_inst *sdi,
585                         void *cb_data);
586
587         /* Dynamic */
588         void *priv;
589 };
590
591 struct sr_session {
592         /** List of struct sr_dev pointers. */
593         GSList *devs;
594         /** List of sr_receive_data_callback_t items. */
595         GSList *datafeed_callbacks;
596         GTimeVal starttime;
597
598         unsigned int num_sources;
599
600         /*
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().
605          */
606         struct source *sources;
607         GPollFD *pollfds;
608         int source_timeout;
609 };
610
611 #include "proto.h"
612 #include "version.h"
613
614 #ifdef __cplusplus
615 }
616 #endif
617
618 #endif