]> sigrok.org Git - libsigrok.git/blame - sigrok.h
Only build hardware drivers if they're enabled.
[libsigrok.git] / sigrok.h
CommitLineData
a1bb33af
UH
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
e31b636d
UH
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
2b3414a4
UH
38 * libsigrok functions returns a "malloc error" it must be exactly the same
39 * return value as used by all other functions to indicate "malloc error".
e31b636d
UH
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 */
2b3414a4 47#define SIGROK_OK 0 /* No error */
e31b636d
UH
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 */
a1bb33af 51
2a3f9541
BV
52/* limited by uint64_t */
53#define MAX_NUM_PROBES 64
54#define MAX_PROBENAME_LEN 32
55
56
a1bb33af 57/* Handy little macros */
d86dc674
UH
58#define KHZ(n) ((n) * 1000)
59#define MHZ(n) ((n) * 1000000)
60#define GHZ(n) ((n) * 1000000000)
a1bb33af 61
fdd20b52
UH
62#ifndef ARRAY_SIZE
63#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64#endif
65
a1bb33af
UH
66/* Data types, used by hardware plugins for set_configuration() */
67enum {
68 T_UINT64,
69 T_CHAR,
70};
71
72enum {
73 PROTO_RAW,
74};
75
76/* (Unused) protocol decoder stack entry */
77struct protocol {
78 char *name;
79 int id;
80 int stackindex;
81};
82
83/*
1b452b85 84 * Datafeed
a1bb33af
UH
85 */
86
87/* datafeed_packet.type values */
88enum {
89 DF_HEADER,
90 DF_END,
91 DF_TRIGGER,
92 DF_LOGIC8,
93 DF_LOGIC16,
94 DF_LOGIC24,
95 DF_LOGIC32,
96 DF_LOGIC48,
97 DF_LOGIC64,
98};
99
100struct datafeed_packet {
101 uint16_t type;
e1aac231 102 uint64_t length;
a1bb33af
UH
103 void *payload;
104};
105
106struct datafeed_header {
107 int feed_version;
108 struct timeval starttime;
4c100f32 109 uint64_t samplerate;
a1bb33af
UH
110 int protocol_id;
111 int num_probes;
112};
113
34e4813f
BV
114/*
115 * Input
116 */
117struct input {
118 struct input_format *format;
119 void *param;
120 void *internal;
121};
122
123struct input_format {
124 char *extension;
125 char *description;
757b8c62
UH
126 int (*format_match) (const char *filename);
127 int (*in_loadfile) (const char *filename);
34e4813f
BV
128};
129
130struct input_format **input_list(void);
131
132
a1bb33af 133/*
1b452b85 134 * Output
a1bb33af
UH
135 */
136struct output {
137 struct output_format *format;
138 struct device *device;
139 char *param;
140 void *internal;
141};
142
143struct output_format {
144 char *extension;
145 char *description;
5a8fda15 146 int (*init) (struct output *o);
a1bb33af
UH
147 int (*data) (struct output *o, char *data_in, uint64_t length_in,
148 char **data_out, uint64_t *length_out);
149 int (*event) (struct output *o, int event_type, char **data_out,
150 uint64_t *length_out);
151};
152
153struct output_format **output_list(void);
34e4813f
BV
154
155
a1bb33af
UH
156int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
157 char *data_in, uint64_t length_in, char **data_out,
158 uint64_t *length_out);
159
25e7d9b1 160char *sigrok_samplerate_string(uint64_t samplerate);
2a3f9541 161char *sigrok_period_string(uint64_t frequency);
25e7d9b1 162
a1bb33af
UH
163/*--- analyzer.c ------------------------------------------------------------*/
164
165struct analyzer {
166 char *name;
167 char *filename;
168 /*
169 * TODO: Parameters? If so, configured plugins need another struct.
170 * TODO: Input and output format?
171 */
172};
173
174/*--- backend.c -------------------------------------------------------------*/
175
176int sigrok_init(void);
177void sigrok_cleanup(void);
178
179/*--- datastore.c -----------------------------------------------------------*/
180
181/* Size of a chunk in units */
182#define DATASTORE_CHUNKSIZE 512000
183
184struct datastore {
185 /* Size in bytes of the number of units stored in this datastore */
186 int ds_unitsize;
33247d6a 187 unsigned int num_units; /* TODO: uint64_t */
a1bb33af
UH
188 GSList *chunklist;
189};
190
2aebf78d 191int datastore_new(int unitsize, struct datastore **ds);
33247d6a 192int datastore_destroy(struct datastore *ds);
a1bb33af
UH
193void datastore_put(struct datastore *ds, void *data, unsigned int length,
194 int in_unitsize, int *probelist);
195
196/*--- debug.c ---------------------------------------------------------------*/
197
198void hexdump(unsigned char *address, int length);
199
200/*--- device.c --------------------------------------------------------------*/
201
202/*
203 * This represents a generic device connected to the system.
204 * For device-specific information, ask the plugin. The plugin_index refers
205 * to the device index within that plugin; it may be handling more than one
206 * device. All relevant plugin calls take a device_index parameter for this.
207 */
208struct device {
209 /* Which plugin handles this device */
210 struct device_plugin *plugin;
211 /* A plugin may handle multiple devices of the same type */
212 int plugin_index;
213 /* List of struct probe* */
214 GSList *probes;
215 /* Data acquired by this device, if any */
216 struct datastore *datastore;
217};
218
219struct probe {
220 int index;
221 gboolean enabled;
222 char *name;
223 char *trigger;
224};
225
226extern GSList *devices;
227
228void device_scan(void);
229void device_close_all(void);
230GSList *device_list(void);
873080cc 231struct device *device_new(struct device_plugin *plugin, int plugin_index, int num_probes);
a1bb33af
UH
232void device_clear(struct device *device);
233void device_destroy(struct device *dev);
234
235void device_probe_clear(struct device *device, int probenum);
236void device_probe_add(struct device *device, char *name);
237struct probe *probe_find(struct device *device, int probenum);
238void device_probe_name(struct device *device, int probenum, char *name);
239
240void device_trigger_clear(struct device *device);
241void device_trigger_set(struct device *device, int probenum, char *trigger);
242
243/*--- hwplugin.c ------------------------------------------------------------*/
244
245/* Hardware plugin capabilities */
246enum {
1b452b85 247 HWCAP_DUMMY, /* Used to terminate lists */
a1bb33af 248 HWCAP_LOGIC_ANALYZER,
1b452b85
UH
249 HWCAP_SAMPLERATE, /* Change samplerate */
250 HWCAP_PROBECONFIG, /* Configure probe mask */
a803c0db 251 HWCAP_CAPTURE_RATIO, /* Set pre/post-trigger capture ratio */
1b452b85
UH
252 HWCAP_LIMIT_MSEC, /* Set a time limit for sample acquisition */
253 HWCAP_LIMIT_SAMPLES, /* Set a limit on number of samples */
a1bb33af
UH
254};
255
256struct hwcap_option {
257 int capability;
258 int type;
259 char *description;
260 char *shortname;
261};
262
263struct sigrok_device_instance {
264 int index;
265 int status;
266 int instance_type;
267 char *vendor;
268 char *model;
269 char *version;
270 union {
271 struct usb_device_instance *usb;
272 struct serial_device_instance *serial;
273 };
274};
275
276/* sigrok_device_instance types */
277enum {
278 USB_INSTANCE,
279 SERIAL_INSTANCE,
280};
281
282struct usb_device_instance {
283 uint8_t bus;
284 uint8_t address;
285 struct libusb_device_handle *devhdl;
286};
287
288struct serial_device_instance {
289 char *port;
290 int fd;
291};
292
293/* Device instance status */
294enum {
295 ST_NOT_FOUND,
296 /* Found, but still booting */
297 ST_INITIALIZING,
298 /* Live, but not in use */
299 ST_INACTIVE,
300 /* Actively in use in a session */
301 ST_ACTIVE,
302};
303
304/*
305 * TODO: This sucks, you just kinda have to "know" the returned type.
306 * TODO: Need a DI to return the number of trigger stages supported.
307 */
308
309/* Device info IDs */
310enum {
311 /* struct sigrok_device_instance for this specific device */
312 DI_INSTANCE,
313 /* The number of probes connected to this device */
314 DI_NUM_PROBES,
4c100f32 315 /* Samplerates supported by this device, (struct samplerates) */
a1bb33af
UH
316 DI_SAMPLERATES,
317 /* Types of trigger supported, out of "01crf" (char *) */
318 DI_TRIGGER_TYPES,
4c100f32
UH
319 /* The currently set samplerate in Hz (uint64_t) */
320 DI_CUR_SAMPLERATE,
a1bb33af
UH
321};
322
1b452b85
UH
323/*
324 * A device supports either a range of samplerates with steps of a given
325 * granularity, or is limited to a set of defined samplerates. Use either
a1bb33af
UH
326 * step or list, but not both.
327 */
328struct samplerates {
329 uint64_t low;
330 uint64_t high;
331 uint64_t step;
332 uint64_t *list;
333};
334
335struct device_plugin {
1b452b85 336 /* Plugin-specific */
a1bb33af
UH
337 char *name;
338 int api_version;
339 int (*init) (char *deviceinfo);
340 void (*cleanup) (void);
341
1b452b85 342 /* Device-specific */
a1bb33af
UH
343 int (*open) (int device_index);
344 void (*close) (int device_index);
345 void *(*get_device_info) (int device_index, int device_info_id);
346 int (*get_status) (int device_index);
347 int *(*get_capabilities) (void);
348 int (*set_configuration) (int device_index, int capability, void *value);
349 int (*start_acquisition) (int device_index, gpointer session_device_id);
350 void (*stop_acquisition) (int device_index, gpointer session_device_id);
351};
352
353struct gsource_fd {
354 GSource source;
355 GPollFD gpfd;
356 /* Not really using this */
357 GSource *timeout_source;
358};
359
360typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
361
362int load_hwplugins(void);
363GSList *list_hwplugins(void);
364
365/* Generic device instances */
366struct sigrok_device_instance *sigrok_device_instance_new(int index,
49d0ce50 367 int status, const char *vendor, const char *model, const char *version);
1b452b85
UH
368struct sigrok_device_instance *get_sigrok_device_instance(
369 GSList *device_instances, int device_index);
a1bb33af
UH
370void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
371
372/* USB-specific instances */
373struct usb_device_instance *usb_device_instance_new(uint8_t bus,
374 uint8_t address, struct libusb_device_handle *hdl);
375void usb_device_instance_free(struct usb_device_instance *usb);
376
377/* Serial-specific instances */
49d0ce50
UH
378struct serial_device_instance *serial_device_instance_new(
379 const char *port, int fd);
a1bb33af
UH
380void serial_device_instance_free(struct serial_device_instance *serial);
381
382int find_hwcap(int *capabilities, int hwcap);
383struct hwcap_option *find_hwcap_option(int hwcap);
384void source_remove(int fd);
1b452b85
UH
385void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
386 void *user_data);
a1bb33af
UH
387
388/*--- session.c -------------------------------------------------------------*/
389
390typedef void (*source_callback_remove) (int fd);
391typedef void (*source_callback_add) (int fd, int events, int timeout,
392 receive_data_callback callback, void *user_data);
393typedef void (*datafeed_callback) (struct device *device,
394 struct datafeed_packet *packet);
395
396struct session {
397 /* List of struct device* */
398 GSList *devices;
399 /* List of struct analyzer* */
400 GSList *analyzers;
1b452b85 401 /* Datafeed callbacks */
a1bb33af
UH
402 GSList *datafeed_callbacks;
403 GTimeVal starttime;
404};
405
406/* Session setup */
afc8e4de 407struct session *session_load(const char *filename);
a1bb33af
UH
408struct session *session_new(void);
409void session_destroy(void);
410void session_device_clear(void);
411int session_device_add(struct device *device);
412
413/* Protocol analyzers setup */
414void session_pa_clear(void);
415void session_pa_add(struct analyzer *pa);
416
417/* Datafeed setup */
418void session_datafeed_callback_clear(void);
419void session_datafeed_callback_add(datafeed_callback callback);
420
421/* Session control */
422int session_start(void);
423void session_stop(void);
424void session_bus(struct device *device, struct datafeed_packet *packet);
425void make_metadata(char *filename);
426int session_save(char *filename);
427
428/*--- hwcommon.c ------------------------------------------------------------*/
429
430int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
431int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
edf60d05
UH
432int ezusb_upload_firmware(libusb_device *dev, int configuration,
433 const char *filename);
a1bb33af
UH
434
435GSList *list_serial_ports(void);
d02a535e
BV
436int serial_open(const char *pathname, int flags);
437int serial_close(int fd);
f0551a65 438int serial_flush(int fd);
d02a535e
BV
439void *serial_backup_params(int fd);
440void serial_restore_params(int fd, void *backup);
1b452b85
UH
441int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
442 int flowcontrol);
a1bb33af 443
9a5c6dcf
UH
444/* libsigrok/hardware/common/misc.c */
445/* TODO: Should not be public. */
446int opendev2(int device_index, struct sigrok_device_instance **sdi,
447 libusb_device *dev, struct libusb_device_descriptor *des,
448 int *skip, uint16_t vid, uint16_t pid, int interface);
449int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
450 struct libusb_device_descriptor *des,
451 uint16_t vid, uint16_t pid, int interface);
452
a1bb33af 453#endif