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