]> sigrok.org Git - libsigrok.git/blob - sigrok.h
Start unification of libsigrok return codes.
[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 #include <libusb.h>
29
30 /*
31  * Status/error codes returned by libsigrok functions.
32  *
33  * All possible return codes of libsigrok functions must be listed here.
34  * Functions should never return hardcoded numbers as status, but rather
35  * use these #defines instead. All error codes are negative numbers.
36  *
37  * The error codes are globally unique in libsigrok, i.e. if one of the
38  * libsigrok function returns a "malloc error" it must be exactly the same
39  * return value as used for all other functions to indicate "malloc error".
40  * There must be no functions which indicate two different errors via the
41  * same return code.
42  *
43  * Also, for compatibility reasons, no defined return codes are ever removed
44  * or reused for different #defines later. You can only add new #defines and
45  * return codes, but never remove or redefine existing ones.
46  */
47 #define SIGROK_OK                       0  /* No error */
48 #define SIGROK_ERR                      -1 /* Generic/unspecified error */
49 #define SIGROK_ERR_MALLOC               -2 /* Malloc/calloc/realloc error */
50 #define SIGROK_ERR_SAMPLERATE           -3 /* Incorrect samplerate */
51
52 /* Handy little macros */
53 #define KHZ(n) (n * 1000)
54 #define MHZ(n) (n * 1000000)
55 #define GHZ(n) (n * 1000000000)
56
57 /* Data types, used by hardware plugins for set_configuration() */
58 enum {
59         T_UINT64,
60         T_CHAR,
61 };
62
63 enum {
64         PROTO_RAW,
65 };
66
67 /* (Unused) protocol decoder stack entry */
68 struct protocol {
69         char *name;
70         int id;
71         int stackindex;
72 };
73
74 /*
75  * datafeed
76  */
77
78 /* datafeed_packet.type values */
79 enum {
80         DF_HEADER,
81         DF_END,
82         DF_TRIGGER,
83         DF_LOGIC8,
84         DF_LOGIC16,
85         DF_LOGIC24,
86         DF_LOGIC32,
87         DF_LOGIC48,
88         DF_LOGIC64,
89 };
90
91 struct datafeed_packet {
92         uint16_t type;
93         uint16_t length;
94         void *payload;
95 };
96
97 struct datafeed_header {
98         int feed_version;
99         struct timeval starttime;
100         uint64_t samplerate;
101         int protocol_id;
102         int num_probes;
103 };
104
105 /*
106  * output
107  */
108 struct output {
109         struct output_format *format;
110         struct device *device;
111         char *param;
112         void *internal;
113 };
114
115 struct output_format {
116         char *extension;
117         char *description;
118         int (*init) (struct output *o);
119         int (*data) (struct output *o, char *data_in, uint64_t length_in,
120                      char **data_out, uint64_t *length_out);
121         int (*event) (struct output *o, int event_type, char **data_out,
122                       uint64_t *length_out);
123 };
124
125 struct output_format **output_list(void);
126 int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
127                   char *data_in, uint64_t length_in, char **data_out,
128                   uint64_t *length_out);
129
130 /*--- analyzer.c ------------------------------------------------------------*/
131
132 struct analyzer {
133         char *name;
134         char *filename;
135         /*
136          * TODO: Parameters? If so, configured plugins need another struct.
137          * TODO: Input and output format?
138          */
139 };
140
141 /*--- backend.c -------------------------------------------------------------*/
142
143 int sigrok_init(void);
144 void sigrok_cleanup(void);
145
146 /*--- datastore.c -----------------------------------------------------------*/
147
148 /* Size of a chunk in units */
149 #define DATASTORE_CHUNKSIZE 512000
150
151 struct datastore {
152         /* Size in bytes of the number of units stored in this datastore */
153         int ds_unitsize;
154         unsigned int num_units;
155         GSList *chunklist;
156 };
157
158 struct datastore *datastore_new(int unitsize);
159 void datastore_destroy(struct datastore *ds);
160 void datastore_put(struct datastore *ds, void *data, unsigned int length,
161                    int in_unitsize, int *probelist);
162
163 /*--- debug.c ---------------------------------------------------------------*/
164
165 void hexdump(unsigned char *address, int length);
166
167 /*--- device.c --------------------------------------------------------------*/
168
169 /*
170  * This represents a generic device connected to the system.
171  * For device-specific information, ask the plugin. The plugin_index refers
172  * to the device index within that plugin; it may be handling more than one
173  * device. All relevant plugin calls take a device_index parameter for this.
174  */
175 struct device {
176         /* Which plugin handles this device */
177         struct device_plugin *plugin;
178         /* A plugin may handle multiple devices of the same type */
179         int plugin_index;
180         /* List of struct probe* */
181         GSList *probes;
182         /* Data acquired by this device, if any */
183         struct datastore *datastore;
184 };
185
186 struct probe {
187         int index;
188         gboolean enabled;
189         char *name;
190         char *trigger;
191 };
192
193 extern GSList *devices;
194
195 void device_scan(void);
196 void device_close_all(void);
197 GSList *device_list(void);
198 struct device *device_new(struct device_plugin *plugin, int plugin_index);
199 void device_clear(struct device *device);
200 void device_destroy(struct device *dev);
201
202 void device_probe_clear(struct device *device, int probenum);
203 void device_probe_add(struct device *device, char *name);
204 struct probe *probe_find(struct device *device, int probenum);
205 void device_probe_name(struct device *device, int probenum, char *name);
206
207 void device_trigger_clear(struct device *device);
208 void device_trigger_set(struct device *device, int probenum, char *trigger);
209
210 /*--- hwplugin.c ------------------------------------------------------------*/
211
212 /* Hardware plugin capabilities */
213 enum {
214         HWCAP_DUMMY,            // used to terminate lists
215         HWCAP_LOGIC_ANALYZER,
216         HWCAP_SAMPLERATE,       // change samplerate
217         HWCAP_PROBECONFIG,      // configure probe mask
218         HWCAP_CAPTURE_RATIO,    // set pre-trigger / post-trigger ratio
219         HWCAP_LIMIT_MSEC,       // set a time limit for sample acquisition
220         HWCAP_LIMIT_SAMPLES,    // set a limit on number of samples
221 };
222
223 struct hwcap_option {
224         int capability;
225         int type;
226         char *description;
227         char *shortname;
228 };
229
230 struct sigrok_device_instance {
231         int index;
232         int status;
233         int instance_type;
234         char *vendor;
235         char *model;
236         char *version;
237         union {
238                 struct usb_device_instance *usb;
239                 struct serial_device_instance *serial;
240         };
241 };
242
243 /* sigrok_device_instance types */
244 enum {
245         USB_INSTANCE,
246         SERIAL_INSTANCE,
247 };
248
249 struct usb_device_instance {
250         uint8_t bus;
251         uint8_t address;
252         struct libusb_device_handle *devhdl;
253 };
254
255 struct serial_device_instance {
256         char *port;
257         int fd;
258 };
259
260 /* Device instance status */
261 enum {
262         ST_NOT_FOUND,
263         /* Found, but still booting */
264         ST_INITIALIZING,
265         /* Live, but not in use */
266         ST_INACTIVE,
267         /* Actively in use in a session */
268         ST_ACTIVE,
269 };
270
271 /*
272  * TODO: This sucks, you just kinda have to "know" the returned type.
273  * TODO: Need a DI to return the number of trigger stages supported.
274  */
275
276 /* Device info IDs */
277 enum {
278         /* struct sigrok_device_instance for this specific device */
279         DI_INSTANCE,
280         /* The number of probes connected to this device */
281         DI_NUM_PROBES,
282         /* Samplerates supported by this device, (struct samplerates) */
283         DI_SAMPLERATES,
284         /* Types of trigger supported, out of "01crf" (char *) */
285         DI_TRIGGER_TYPES,
286         /* The currently set samplerate in Hz (uint64_t) */
287         DI_CUR_SAMPLERATE,
288 };
289
290 /* a device supports either a range of samplerates with steps of a given
291  * granularity, or is limited to a set of defined samplerates. use either
292  * step or list, but not both.
293  */
294 struct samplerates {
295         uint64_t low;
296         uint64_t high;
297         uint64_t step;
298         uint64_t *list;
299 };
300
301 struct device_plugin {
302         /* plugin-specific */
303         char *name;
304         int api_version;
305         int (*init) (char *deviceinfo);
306         void (*cleanup) (void);
307
308         /* device-specific */
309         int (*open) (int device_index);
310         void (*close) (int device_index);
311         void *(*get_device_info) (int device_index, int device_info_id);
312         int (*get_status) (int device_index);
313         int *(*get_capabilities) (void);
314         int (*set_configuration) (int device_index, int capability, void *value);
315         int (*start_acquisition) (int device_index, gpointer session_device_id);
316         void (*stop_acquisition) (int device_index, gpointer session_device_id);
317 };
318
319 struct gsource_fd {
320         GSource source;
321         GPollFD gpfd;
322         /* Not really using this */
323         GSource *timeout_source;
324 };
325
326 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
327
328 int load_hwplugins(void);
329 GSList *list_hwplugins(void);
330
331 /* Generic device instances */
332 struct sigrok_device_instance *sigrok_device_instance_new(int index,
333         int status, char *vendor, char *model, char *version);
334 struct sigrok_device_instance *get_sigrok_device_instance(GSList *device_instances, int device_index);
335 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
336
337 /* USB-specific instances */
338 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
339                 uint8_t address, struct libusb_device_handle *hdl);
340 void usb_device_instance_free(struct usb_device_instance *usb);
341
342 /* Serial-specific instances */
343 struct serial_device_instance *serial_device_instance_new(char *port, int fd);
344 void serial_device_instance_free(struct serial_device_instance *serial);
345
346 int find_hwcap(int *capabilities, int hwcap);
347 struct hwcap_option *find_hwcap_option(int hwcap);
348 void source_remove(int fd);
349 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb, void *user_data);
350
351 /*--- session.c -------------------------------------------------------------*/
352
353 typedef void (*source_callback_remove) (int fd);
354 typedef void (*source_callback_add) (int fd, int events, int timeout,
355                 receive_data_callback callback, void *user_data);
356 typedef void (*datafeed_callback) (struct device *device,
357                                  struct datafeed_packet *packet);
358
359 struct session {
360         /* List of struct device* */
361         GSList *devices;
362         /* List of struct analyzer* */
363         GSList *analyzers;
364         /* datafeed callbacks */
365         GSList *datafeed_callbacks;
366         GTimeVal starttime;
367 };
368
369 /* Session setup */
370 struct session *session_load(char *filename);
371 struct session *session_new(void);
372 void session_destroy(void);
373 void session_device_clear(void);
374 int session_device_add(struct device *device);
375
376 /* Protocol analyzers setup */
377 void session_pa_clear(void);
378 void session_pa_add(struct analyzer *pa);
379
380 /* Datafeed setup */
381 void session_datafeed_callback_clear(void);
382 void session_datafeed_callback_add(datafeed_callback callback);
383
384 /* Session control */
385 int session_start(void);
386 void session_stop(void);
387 void session_bus(struct device *device, struct datafeed_packet *packet);
388 void make_metadata(char *filename);
389 int session_save(char *filename);
390
391 /*--- hwcommon.c ------------------------------------------------------------*/
392
393 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
394 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
395
396 GSList *list_serial_ports(void);
397
398 #endif