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