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