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