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