]> sigrok.org Git - libsigrok.git/blob - libsigrok.h
We now require libusb >= 1.0.9.
[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  * Status/error codes returned by libsigrok functions.
35  *
36  * All possible return codes of libsigrok functions must be listed here.
37  * Functions should never return hardcoded numbers as status, but rather
38  * use these #defines instead. All error codes are negative numbers.
39  *
40  * The error codes are globally unique in libsigrok, i.e. if one of the
41  * libsigrok functions returns a "malloc error" it must be exactly the same
42  * return value as used by all other functions to indicate "malloc error".
43  * There must be no functions which indicate two different errors via the
44  * same return code.
45  *
46  * Also, for compatibility reasons, no defined return codes are ever removed
47  * or reused for different #defines later. You can only add new #defines and
48  * return codes, but never remove or redefine existing ones.
49  */
50 #define SR_OK                 0 /* No error */
51 #define SR_ERR               -1 /* Generic/unspecified error */
52 #define SR_ERR_MALLOC        -2 /* Malloc/calloc/realloc error */
53 #define SR_ERR_ARG           -3 /* Function argument error */
54 #define SR_ERR_BUG           -4 /* Errors hinting at internal bugs */
55 #define SR_ERR_SAMPLERATE    -5 /* Incorrect samplerate */
56
57 #define SR_MAX_NUM_PROBES    64 /* Limited by uint64_t. */
58 #define SR_MAX_PROBENAME_LEN 32
59
60 /* Handy little macros */
61 #define SR_HZ(n)  (n)
62 #define SR_KHZ(n) ((n) * 1000)
63 #define SR_MHZ(n) ((n) * 1000000)
64 #define SR_GHZ(n) ((n) * 1000000000)
65
66 #define SR_HZ_TO_NS(n) (1000000000 / (n))
67
68 /* libsigrok loglevels. */
69 #define SR_LOG_NONE           0 /**< Output no messages at all. */
70 #define SR_LOG_ERR            1 /**< Output error messages. */
71 #define SR_LOG_WARN           2 /**< Output warnings. */
72 #define SR_LOG_INFO           3 /**< Output informational messages. */
73 #define SR_LOG_DBG            4 /**< Output debug messages. */
74 #define SR_LOG_SPEW           5 /**< Output very noisy debug messages. */
75
76 /*
77  * Use SR_API to mark public API symbols, and SR_PRIV for private symbols.
78  *
79  * Variables and functions marked 'static' are private already and don't
80  * need SR_PRIV. However, functions which are not static (because they need
81  * to be used in other libsigrok-internal files) but are also not meant to
82  * be part of the public libsigrok API, must use SR_PRIV.
83  *
84  * This uses the 'visibility' feature of gcc (requires gcc >= 4.0).
85  *
86  * This feature is not available on MinGW/Windows, as it is a feature of
87  * ELF files and MinGW/Windows uses PE files.
88  *
89  * Details: http://gcc.gnu.org/wiki/Visibility
90  */
91
92 /* Marks public libsigrok API symbols. */
93 #ifndef _WIN32
94 #define SR_API __attribute__((visibility("default")))
95 #else
96 #define SR_API
97 #endif
98
99 /* Marks private, non-public libsigrok symbols (not part of the API). */
100 #ifndef _WIN32
101 #define SR_PRIV __attribute__((visibility("hidden")))
102 #else
103 #define SR_PRIV
104 #endif
105
106 typedef int (*sr_receive_data_callback_t)(int fd, int revents, void *cb_data);
107
108 /* Data types used by hardware drivers for dev_config_set() */
109 enum {
110         SR_T_UINT64,
111         SR_T_CHAR,
112         SR_T_BOOL,
113         SR_T_FLOAT,
114         SR_T_RATIONAL_PERIOD,
115         SR_T_RATIONAL_VOLT,
116         SR_T_KEYVALUE,
117 };
118
119 struct sr_rational {
120         /* numerator */
121         uint64_t p;
122         /* denominator */
123         uint64_t q;
124 };
125
126 /* sr_datafeed_packet.type values */
127 enum {
128         SR_DF_HEADER,
129         SR_DF_END,
130         SR_DF_TRIGGER,
131         SR_DF_LOGIC,
132         SR_DF_META_LOGIC,
133         SR_DF_ANALOG,
134         SR_DF_META_ANALOG,
135         SR_DF_FRAME_BEGIN,
136         SR_DF_FRAME_END,
137 };
138
139 /* sr_datafeed_analog.mq values */
140 enum {
141         SR_MQ_VOLTAGE,
142         SR_MQ_CURRENT,
143         SR_MQ_RESISTANCE,
144         SR_MQ_CAPACITANCE,
145         SR_MQ_TEMPERATURE,
146         SR_MQ_FREQUENCY,
147         SR_MQ_DUTY_CYCLE,
148         SR_MQ_CONTINUITY,
149         SR_MQ_PULSE_WIDTH,
150         SR_MQ_CONDUCTANCE,
151         /** For a measurement of electrical power, usually in W, or dBm */
152         SR_MQ_POWER,
153         /** Usually for measuring a transistor's gain, or h_FE*/
154         SR_MQ_GAIN,
155 };
156
157 /* sr_datafeed_analog.unit values */
158 enum {
159         SR_UNIT_VOLT,
160         SR_UNIT_AMPERE,
161         SR_UNIT_OHM,
162         SR_UNIT_FARAD,
163         SR_UNIT_KELVIN,
164         SR_UNIT_CELSIUS,
165         SR_UNIT_FAHRENHEIT,
166         SR_UNIT_HERTZ,
167         SR_UNIT_PERCENTAGE,
168         SR_UNIT_BOOLEAN,
169         SR_UNIT_SECOND,
170         /** Unit of conductance, the inverse of resistance.  */
171         SR_UNIT_SIEMENS,
172         /** An absolute measurement of power, in decibels, referenced to
173          * 1 milliwatt (dBu). */
174         SR_UNIT_DECIBEL_MW,
175         /** Voltage in decibel, referenced to 1 volt (dBV). */
176         SR_UNIT_DECIBEL_VOLT,
177         /** Measurements that intrinsically do not have units attached, such
178          * as ratios, gains, etc. Specifically, a transistor's gain (hFE) is
179          * a unitless quantity. */
180         SR_UNIT_UNITLESS,
181 };
182
183 /** sr_datafeed_analog.flags values */
184 enum {
185         /** Voltage measurement is alternating current. */
186         SR_MQFLAG_AC = 0x01,
187         /** Voltage measurement is direct current. */
188         SR_MQFLAG_DC = 0x02,
189         /** This is a true RMS measurement. */
190         SR_MQFLAG_RMS = 0x04,
191         /** Value is voltage drop across a diode, or NAN. */
192         SR_MQFLAG_DIODE = 0x08,
193         /** Device is in "hold" mode, i.e. repeating the last measurement. */
194         SR_MQFLAG_HOLD = 0x10,
195         /** Device is in "max" mode, only updating when a new max value is found. */
196         SR_MQFLAG_MAX = 0x20,
197         /** Device is in "min" mode, only updating when a new min value is found. */
198         SR_MQFLAG_MIN = 0x40,
199         /** Device is in autoranging mode. */
200         SR_MQFLAG_AUTORANGE = 0x80,
201         /** Device is in relative mode. */
202         SR_MQFLAG_RELATIVE = 0x100,
203 };
204
205 struct sr_context;
206
207 struct sr_datafeed_packet {
208         uint16_t type;
209         void *payload;
210 };
211
212 struct sr_datafeed_header {
213         int feed_version;
214         struct timeval starttime;
215 };
216
217 struct sr_datafeed_meta_logic {
218         int num_probes;
219         uint64_t samplerate;
220 };
221
222 struct sr_datafeed_logic {
223         uint64_t length;
224         uint16_t unitsize;
225         void *data;
226 };
227
228 struct sr_datafeed_meta_analog {
229         int num_probes;
230 };
231
232 struct sr_datafeed_analog {
233         int num_samples;
234         /** Measured quantity (e.g. voltage, current, temperature) */
235         int mq;
236         /** Unit in which the MQ is measured. */
237         int unit;
238         /** Bitmap with extra information about the MQ. */
239         uint64_t mqflags;
240         float *data;
241 };
242
243 struct sr_input {
244         struct sr_input_format *format;
245         GHashTable *param;
246         struct sr_dev_inst *sdi;
247         void *internal;
248 };
249
250 struct sr_input_format {
251         char *id;
252         char *description;
253         int (*format_match) (const char *filename);
254         int (*init) (struct sr_input *in);
255         int (*loadfile) (struct sr_input *in, const char *filename);
256 };
257
258 struct sr_output {
259         struct sr_output_format *format;
260         struct sr_dev_inst *sdi;
261         char *param;
262         void *internal;
263 };
264
265 struct sr_output_format {
266         char *id;
267         char *description;
268         int df_type;
269         int (*init) (struct sr_output *o);
270         int (*data) (struct sr_output *o, const uint8_t *data_in,
271                      uint64_t length_in, uint8_t **data_out,
272                      uint64_t *length_out);
273         int (*event) (struct sr_output *o, int event_type, uint8_t **data_out,
274                       uint64_t *length_out);
275         GString *(*recv) (struct sr_output *o, const struct sr_dev_inst *sdi,
276                         struct sr_datafeed_packet *packet);
277         int (*cleanup) (struct sr_output *o);
278 };
279
280 struct sr_datastore {
281         /* Size in bytes of the number of units stored in this datastore */
282         int ds_unitsize;
283         unsigned int num_units; /* TODO: uint64_t */
284         GSList *chunklist;
285 };
286
287 /*
288  * This represents a generic device connected to the system.
289  * For device-specific information, ask the driver. The driver_index refers
290  * to the device index within that driver; it may be handling more than one
291  * device. All relevant driver calls take a dev_index parameter for this.
292  */
293 struct sr_dev {
294         /* Which driver handles this device */
295         struct sr_dev_driver *driver;
296         /* A driver may handle multiple devices of the same type */
297         int driver_index;
298         /* List of struct sr_probe* */
299         GSList *probes;
300         /* Data acquired by this device, if any */
301         struct sr_datastore *datastore;
302 };
303
304 enum {
305         SR_PROBE_LOGIC,
306         SR_PROBE_ANALOG,
307 };
308
309 struct sr_probe {
310         int index;
311         int type;
312         gboolean enabled;
313         char *name;
314         char *trigger;
315 };
316
317 struct sr_hwopt {
318         int hwopt;
319         const void *value;
320 };
321
322 /* Hardware driver options */
323 enum {
324         SR_HWOPT_DUMMY = 0, /* Used to terminate lists. Must be 0! */
325
326         /** Some drivers cannot detect the exact model they're talking to
327          * (may be phased out). */
328         SR_HWOPT_MODEL,
329
330         /** Specification on how to connect to a device. In combination
331          * with SR_HWOPT_SERIALCOMM, this is a serial port in the form
332          * which makes sense to the operating system (/dev/ttyS0).
333          * Otherwise this specifies a USB device, either in the form of
334          * <bus>.<address> (decimal, e.g. 1.65) or <vendorid>.<productid>
335          * (hexadecimal, e.g. 1d6b.0001). */
336         SR_HWOPT_CONN,
337
338         /** Serial communication specification, in the form:
339          *   <speed>/<data bits><parity><stop bit> e.g.9600/ 8n1
340          * This is always an optional parameter, since a driver typically
341          * knows the speed at which the device wants to communicate */
342         SR_HWOPT_SERIALCOMM,
343 };
344
345 /* Hardware device capabilities */
346 enum {
347         SR_HWCAP_DUMMY = 0, /* Used to terminate lists. Must be 0! */
348
349         /*--- Device classes ------------------------------------------------*/
350
351         /** The device can act as logic analyzer. */
352         SR_HWCAP_LOGIC_ANALYZER,
353
354         /** The device can act as an oscilloscope. */
355         SR_HWCAP_OSCILLOSCOPE,
356
357         /** The device can act as a multimeter. */
358         SR_HWCAP_MULTIMETER,
359
360         /** The device is a demo device. */
361         SR_HWCAP_DEMO_DEV,
362
363
364         /*--- Device configuration ------------------------------------------*/
365
366         /** The device supports setting/changing its samplerate. */
367         SR_HWCAP_SAMPLERATE,
368
369         /** The device supports setting a pre/post-trigger capture ratio. */
370         SR_HWCAP_CAPTURE_RATIO,
371
372         /* TODO? */
373         /** The device supports setting a pattern (pattern generator mode). */
374         SR_HWCAP_PATTERN_MODE,
375
376         /** The device supports Run Length Encoding. */
377         SR_HWCAP_RLE,
378
379         /** The device supports setting trigger slope. */
380         SR_HWCAP_TRIGGER_SLOPE,
381
382         /** Trigger source. */
383         SR_HWCAP_TRIGGER_SOURCE,
384
385         /** Horizontal trigger position */
386         SR_HWCAP_HORIZ_TRIGGERPOS,
387
388         /** Buffer size. */
389         SR_HWCAP_BUFFERSIZE,
390
391         /** Time base. */
392         SR_HWCAP_TIMEBASE,
393
394         /** Filter. */
395         SR_HWCAP_FILTER,
396
397         /** Volts/div. */
398         SR_HWCAP_VDIV,
399
400         /** Coupling. */
401         SR_HWCAP_COUPLING,
402
403
404         /*--- Special stuff -------------------------------------------------*/
405
406         /** Session filename */
407         SR_HWCAP_SESSIONFILE,
408
409         /* TODO: Better description. */
410         /** The device supports specifying a capturefile to inject. */
411         SR_HWCAP_CAPTUREFILE,
412
413         /* TODO: Better description. */
414         /** The device supports specifying the capturefile unit size. */
415         SR_HWCAP_CAPTURE_UNITSIZE,
416
417         /* TODO: Better description. */
418         /** The device supports setting the number of probes. */
419         SR_HWCAP_CAPTURE_NUM_PROBES,
420
421
422         /*--- Acquisition modes ---------------------------------------------*/
423
424         /**
425          * The device supports setting a sample time limit, i.e. how long the
426          * sample acquisition should run (in ms).
427          */
428         SR_HWCAP_LIMIT_MSEC,
429
430         /**
431          * The device supports setting a sample number limit, i.e. how many
432          * samples should be acquired.
433          */
434         SR_HWCAP_LIMIT_SAMPLES,
435
436         /**
437          * The device supports setting a frame limit, i.e. how many
438          * frames should be acquired.
439          */
440         SR_HWCAP_LIMIT_FRAMES,
441
442         /**
443          * The device supports continuous sampling, i.e. neither a time limit
444          * nor a sample number limit has to be supplied, it will just acquire
445          * samples continuously, until explicitly stopped by a certain command.
446          */
447         SR_HWCAP_CONTINUOUS,
448
449 };
450
451 struct sr_hwcap_option {
452         int hwcap;
453         int type;
454         char *description;
455         char *shortname;
456 };
457
458 struct sr_dev_inst {
459         struct sr_dev_driver *driver;
460         int index;
461         int status;
462         int inst_type;
463         char *vendor;
464         char *model;
465         char *version;
466         GSList *probes;
467         void *priv;
468 };
469
470 /* sr_dev_inst types */
471 enum {
472         /** Device instance type for USB devices. */
473         SR_INST_USB,
474         /** Device instance type for serial port devices. */
475         SR_INST_SERIAL,
476 };
477
478 /* Device instance status */
479 enum {
480         SR_ST_NOT_FOUND,
481         /* Found, but still booting */
482         SR_ST_INITIALIZING,
483         /* Live, but not in use */
484         SR_ST_INACTIVE,
485         /* Actively in use in a session */
486         SR_ST_ACTIVE,
487 };
488
489 /*
490  * TODO: This sucks, you just kinda have to "know" the returned type.
491  * TODO: Need a DI to return the number of trigger stages supported.
492  */
493
494 /* Device info IDs */
495 enum {
496         /* A list of options supported by the driver. */
497         SR_DI_HWOPTS,
498         /* A list of capabilities supported by the device. */
499         SR_DI_HWCAPS,
500         /* The number of probes connected to this device */
501         SR_DI_NUM_PROBES,
502         /* The probe names on this device */
503         SR_DI_PROBE_NAMES,
504         /* Samplerates supported by this device, (struct sr_samplerates) */
505         SR_DI_SAMPLERATES,
506         /* Types of logic trigger supported, out of "01crf" (char *) */
507         SR_DI_TRIGGER_TYPES,
508         /* The currently set samplerate in Hz (uint64_t) */
509         SR_DI_CUR_SAMPLERATE,
510         /* Supported patterns (in pattern generator mode) */
511         SR_DI_PATTERNS,
512         /* Supported buffer sizes */
513         SR_DI_BUFFERSIZES,
514         /* Supported time bases */
515         SR_DI_TIMEBASES,
516         /* Supported trigger sources */
517         SR_DI_TRIGGER_SOURCES,
518         /* Supported filter targets */
519         SR_DI_FILTERS,
520         /* Valid volts/div values */
521         SR_DI_VDIVS,
522         /* Coupling options */
523         SR_DI_COUPLING,
524 };
525
526 /*
527  * A device supports either a range of samplerates with steps of a given
528  * granularity, or is limited to a set of defined samplerates. Use either
529  * step or list, but not both.
530  */
531 struct sr_samplerates {
532         uint64_t low;
533         uint64_t high;
534         uint64_t step;
535         const uint64_t *list;
536 };
537
538 struct sr_dev_driver {
539         /* Driver-specific */
540         char *name;
541         char *longname;
542         int api_version;
543         int (*init) (void);
544         int (*cleanup) (void);
545         GSList *(*scan) (GSList *options);
546         GSList *(*dev_list) (void);
547         int (*dev_clear) (void);
548
549         /* Device-specific */
550         int (*dev_open) (struct sr_dev_inst *sdi);
551         int (*dev_close) (struct sr_dev_inst *sdi);
552         int (*info_get) (int info_id, const void **data,
553                         const struct sr_dev_inst *sdi);
554         int (*dev_config_set) (const struct sr_dev_inst *sdi, int hwcap,
555                         const void *value);
556         int (*dev_acquisition_start) (const struct sr_dev_inst *sdi,
557                         void *cb_data);
558         int (*dev_acquisition_stop) (const struct sr_dev_inst *sdi,
559                         void *cb_data);
560
561         /* Dynamic */
562         void *priv;
563 };
564
565 struct sr_session {
566         /* List of struct sr_dev* */
567         GSList *devs;
568         /* list of sr_receive_data_callback_t */
569         GSList *datafeed_callbacks;
570         GTimeVal starttime;
571
572         unsigned int num_sources;
573
574         /* Both "sources" and "pollfds" are of the same size and contain pairs of
575          * descriptor and callback function. We can not embed the GPollFD into the
576          * source struct since we want to be able to pass the array of all poll
577          * descriptors to g_poll.
578          */
579         struct source *sources;
580         GPollFD *pollfds;
581         int source_timeout;
582 };
583
584 #include "proto.h"
585 #include "version.h"
586
587 #ifdef __cplusplus
588 }
589 #endif
590
591 #endif