]> sigrok.org Git - libsigrok.git/blob - sigrok.h
Binary output: Add more error checks.
[libsigrok.git] / sigrok.h
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 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 SIGROK_SIGROK_H
21 #define SIGROK_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 #ifdef HAVE_LIBUSB_1_0
29 #include <libusb.h>
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /*
37  * Status/error codes returned by libsigrok functions.
38  *
39  * All possible return codes of libsigrok functions must be listed here.
40  * Functions should never return hardcoded numbers as status, but rather
41  * use these #defines instead. All error codes are negative numbers.
42  *
43  * The error codes are globally unique in libsigrok, i.e. if one of the
44  * libsigrok functions returns a "malloc error" it must be exactly the same
45  * return value as used by all other functions to indicate "malloc error".
46  * There must be no functions which indicate two different errors via the
47  * same return code.
48  *
49  * Also, for compatibility reasons, no defined return codes are ever removed
50  * or reused for different #defines later. You can only add new #defines and
51  * return codes, but never remove or redefine existing ones.
52  */
53 #define SR_OK                 0 /* No error */
54 #define SR_ERR               -1 /* Generic/unspecified error */
55 #define SR_ERR_MALLOC        -2 /* Malloc/calloc/realloc error */
56 #define SR_ERR_SAMPLERATE    -3 /* Incorrect samplerate */
57
58 #define SR_MAX_NUM_PROBES       64 /* Limited by uint64_t. */
59 #define SR_MAX_PROBENAME_LEN    32
60
61 /* Handy little macros */
62 #define SR_HZ(n)  (n)
63 #define SR_KHZ(n) ((n) * 1000)
64 #define SR_MHZ(n) ((n) * 1000000)
65 #define SR_GHZ(n) ((n) * 1000000000)
66
67 #define SR_HZ_TO_NS(n) (1000000000 / (n))
68
69 typedef int (*sr_receive_data_callback) (int fd, int revents, void *user_data);
70
71 /* Data types used by hardware plugins for set_configuration() */
72 enum {
73         SR_T_UINT64,
74         SR_T_CHAR,
75         SR_T_NULL,
76 };
77
78 enum {
79         SR_PROTO_RAW,
80 };
81
82 #if 0
83 /* (Unused) protocol decoder stack entry */
84 struct sr_protocol {
85         char *name;
86         int id;
87         int stackindex;
88 };
89 #endif
90
91 /* sr_datafeed_packet.type values */
92 enum {
93         SR_DF_HEADER,
94         SR_DF_END,
95         SR_DF_TRIGGER,
96         SR_DF_LOGIC,
97         SR_DF_ANALOG,
98         SR_DF_PD,
99 };
100
101 struct sr_datafeed_packet {
102         uint16_t type;
103         uint64_t length;
104         uint16_t unitsize;
105         void *payload;
106 };
107
108 struct sr_datafeed_header {
109         int feed_version;
110         struct timeval starttime;
111         uint64_t samplerate;
112         int protocol_id;
113         int num_analog_probes;
114         int num_logic_probes;
115 };
116
117 #if 0
118 struct sr_analog_probe {
119         uint8_t att;
120         uint8_t res;    /* Needs to be a power of 2, FIXME */
121         uint16_t val;   /* Max hardware ADC width is 16bits */
122 };
123
124 struct sr_analog_sample {
125         uint8_t num_probes; /* Max hardware probes is 256 */
126         struct sr_analog_probe probes[];
127 };
128 #endif
129
130 struct sr_input {
131         struct sr_input_format *format;
132         char *param;
133         struct sr_device *vdevice;
134 };
135
136 struct sr_input_format {
137         char *extension;
138         char *description;
139         int (*format_match) (const char *filename);
140         int (*init) (struct sr_input *in);
141         int (*loadfile) (struct sr_input *in, const char *filename);
142 };
143
144 struct sr_output {
145         struct sr_output_format *format;
146         struct sr_device *device;
147         char *param;
148         void *internal;
149 };
150
151 struct sr_output_format {
152         char *extension;
153         char *description;
154         int df_type;
155         int (*init) (struct sr_output *o);
156         int (*data) (struct sr_output *o, const char *data_in,
157                      uint64_t length_in, char **data_out, uint64_t *length_out);
158         int (*event) (struct sr_output *o, int event_type, char **data_out,
159                       uint64_t *length_out);
160 };
161
162 #if 0
163 struct sr_analyzer {
164         char *name;
165         char *filename;
166         /*
167          * TODO: Parameters? If so, configured plugins need another struct.
168          * TODO: Input and output format?
169          */
170 };
171 #endif
172
173 struct sr_datastore {
174         /* Size in bytes of the number of units stored in this datastore */
175         int ds_unitsize;
176         unsigned int num_units; /* TODO: uint64_t */
177         GSList *chunklist;
178 };
179
180 /*
181  * This represents a generic device connected to the system.
182  * For device-specific information, ask the plugin. The plugin_index refers
183  * to the device index within that plugin; it may be handling more than one
184  * device. All relevant plugin calls take a device_index parameter for this.
185  */
186 struct sr_device {
187         /* Which plugin handles this device */
188         struct sr_device_plugin *plugin;
189         /* A plugin may handle multiple devices of the same type */
190         int plugin_index;
191         /* List of struct sr_probe* */
192         GSList *probes;
193         /* Data acquired by this device, if any */
194         struct sr_datastore *datastore;
195 };
196
197 enum {
198         SR_PROBE_TYPE_LOGIC,
199         SR_PROBE_TYPE_ANALOG,
200 };
201
202 struct sr_probe {
203         int index;
204         int type;
205         gboolean enabled;
206         char *name;
207         char *trigger;
208 };
209
210 /* TODO: Get rid of this global variable. */
211 extern GSList *devices;
212
213 /* Hardware plugin capabilities */
214 enum {
215         SR_HWCAP_DUMMY,             /* Used to terminate lists */
216         /* device classes */
217         SR_HWCAP_LOGIC_ANALYZER,
218
219         /* device options */
220         SR_HWCAP_SAMPLERATE,        /* Change samplerate */
221         SR_HWCAP_PROBECONFIG,       /* Configure probe mask */
222         SR_HWCAP_CAPTURE_RATIO,     /* Set pre/post-trigger capture ratio */
223         SR_HWCAP_PATTERN_MODE,      /* Pattern generator mode */
224
225         /* special stuff */
226         SR_HWCAP_CAPTUREFILE,       /* capturefile to inject */
227         SR_HWCAP_CAPTURE_UNITSIZE,  /* unitsize of capturefile data */
228         SR_HWCAP_CAPTURE_NUM_PROBES,/* set number of probes */
229
230         /* acquisition modes */
231         SR_HWCAP_LIMIT_MSEC,        /* Set a time limit for sample acquisition */
232         SR_HWCAP_LIMIT_SAMPLES,     /* Set a limit on number of samples */
233         SR_HWCAP_CONTINUOUS,
234 };
235
236 struct sr_hwcap_option {
237         int capability;
238         int type;
239         char *description;
240         char *shortname;
241 };
242
243 struct sr_device_instance {
244         int index;
245         int status;
246         int instance_type;
247         char *vendor;
248         char *model;
249         char *version;
250         void *priv;
251         union {
252                 struct sr_usb_device_instance *usb;
253                 struct sr_serial_device_instance *serial;
254         };
255 };
256
257 /* sr_device_instance types */
258 enum {
259         SR_USB_INSTANCE,
260         SR_SERIAL_INSTANCE,
261 };
262
263 #ifdef HAVE_LIBUSB_1_0
264 struct sr_usb_device_instance {
265         uint8_t bus;
266         uint8_t address;
267         struct libusb_device_handle *devhdl;
268 };
269 #endif
270
271 struct sr_serial_device_instance {
272         char *port;
273         int fd;
274 };
275
276 /* Device instance status */
277 enum {
278         SR_ST_NOT_FOUND,
279         /* Found, but still booting */
280         SR_ST_INITIALIZING,
281         /* Live, but not in use */
282         SR_ST_INACTIVE,
283         /* Actively in use in a session */
284         SR_ST_ACTIVE,
285 };
286
287 /*
288  * TODO: This sucks, you just kinda have to "know" the returned type.
289  * TODO: Need a DI to return the number of trigger stages supported.
290  */
291
292 /* Device info IDs */
293 enum {
294         /* struct sr_device_instance for this specific device */
295         SR_DI_INSTANCE,
296         /* The number of probes connected to this device */
297         SR_DI_NUM_PROBES,
298         /* Samplerates supported by this device, (struct sr_samplerates) */
299         SR_DI_SAMPLERATES,
300         /* Types of trigger supported, out of "01crf" (char *) */
301         SR_DI_TRIGGER_TYPES,
302         /* The currently set samplerate in Hz (uint64_t) */
303         SR_DI_CUR_SAMPLERATE,
304         /* Supported pattern generator modes */
305         SR_DI_PATTERNMODES,
306 };
307
308 /*
309  * A device supports either a range of samplerates with steps of a given
310  * granularity, or is limited to a set of defined samplerates. Use either
311  * step or list, but not both.
312  */
313 struct sr_samplerates {
314         uint64_t low;
315         uint64_t high;
316         uint64_t step;
317         uint64_t *list;
318 };
319
320 struct sr_device_plugin {
321         /* Plugin-specific */
322         char *name;
323         char *longname;
324         int api_version;
325         int (*init) (const char *deviceinfo);
326         void (*cleanup) (void);
327
328         /* Device-specific */
329         int (*open) (int device_index);
330         void (*close) (int device_index);
331         void *(*get_device_info) (int device_index, int device_info_id);
332         int (*get_status) (int device_index);
333         int *(*get_capabilities) (void);
334         int (*set_configuration) (int device_index, int capability, void *value);
335         int (*start_acquisition) (int device_index, gpointer session_device_id);
336         void (*stop_acquisition) (int device_index, gpointer session_device_id);
337 };
338
339 struct sr_session {
340         /* List of struct sr_device* */
341         GSList *devices;
342         /* List of struct analyzer* */
343         GSList *analyzers;
344         /* list of sr_receive_data_callback */
345         GSList *datafeed_callbacks;
346         GTimeVal starttime;
347         gboolean running;
348 };
349
350 #include "sigrok-proto.h"
351
352 #ifdef __cplusplus
353 }
354 #endif
355
356 #endif