2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #ifndef SIGROK_SIGROK_H
21 #define SIGROK_SIGROK_H
31 * Status/error codes returned by libsigrok functions.
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.
37 * The error codes are globally unique in libsigrok, i.e. if one of the
38 * libsigrok function returns a "malloc error" it must be exactly the same
39 * return value as used for all other functions to indicate "malloc error".
40 * There must be no functions which indicate two different errors via the
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.
47 #define SIGROK_OK 0 /* No error */
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 */
52 /* Handy little macros */
53 #define KHZ(n) (n * 1000)
54 #define MHZ(n) (n * 1000000)
55 #define GHZ(n) (n * 1000000000)
57 /* Data types, used by hardware plugins for set_configuration() */
67 /* (Unused) protocol decoder stack entry */
78 /* datafeed_packet.type values */
91 struct datafeed_packet {
97 struct datafeed_header {
99 struct timeval starttime;
109 struct output_format *format;
110 struct device *device;
115 struct output_format {
118 int (*init) (struct output *o);
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);
125 struct output_format **output_list(void);
126 int 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);
130 /*--- analyzer.c ------------------------------------------------------------*/
136 * TODO: Parameters? If so, configured plugins need another struct.
137 * TODO: Input and output format?
141 /*--- backend.c -------------------------------------------------------------*/
143 int sigrok_init(void);
144 void sigrok_cleanup(void);
146 /*--- datastore.c -----------------------------------------------------------*/
148 /* Size of a chunk in units */
149 #define DATASTORE_CHUNKSIZE 512000
152 /* Size in bytes of the number of units stored in this datastore */
154 unsigned int num_units;
158 struct datastore *datastore_new(int unitsize);
159 void datastore_destroy(struct datastore *ds);
160 void datastore_put(struct datastore *ds, void *data, unsigned int length,
161 int in_unitsize, int *probelist);
163 /*--- debug.c ---------------------------------------------------------------*/
165 void hexdump(unsigned char *address, int length);
167 /*--- device.c --------------------------------------------------------------*/
170 * This represents a generic device connected to the system.
171 * For device-specific information, ask the plugin. The plugin_index refers
172 * to the device index within that plugin; it may be handling more than one
173 * device. All relevant plugin calls take a device_index parameter for this.
176 /* Which plugin handles this device */
177 struct device_plugin *plugin;
178 /* A plugin may handle multiple devices of the same type */
180 /* List of struct probe* */
182 /* Data acquired by this device, if any */
183 struct datastore *datastore;
193 extern GSList *devices;
195 void device_scan(void);
196 void device_close_all(void);
197 GSList *device_list(void);
198 struct device *device_new(struct device_plugin *plugin, int plugin_index);
199 void device_clear(struct device *device);
200 void device_destroy(struct device *dev);
202 void device_probe_clear(struct device *device, int probenum);
203 void device_probe_add(struct device *device, char *name);
204 struct probe *probe_find(struct device *device, int probenum);
205 void device_probe_name(struct device *device, int probenum, char *name);
207 void device_trigger_clear(struct device *device);
208 void device_trigger_set(struct device *device, int probenum, char *trigger);
210 /*--- hwplugin.c ------------------------------------------------------------*/
212 /* Hardware plugin capabilities */
214 HWCAP_DUMMY, // used to terminate lists
215 HWCAP_LOGIC_ANALYZER,
216 HWCAP_SAMPLERATE, // change samplerate
217 HWCAP_PROBECONFIG, // configure probe mask
218 HWCAP_CAPTURE_RATIO, // set pre-trigger / post-trigger ratio
219 HWCAP_LIMIT_MSEC, // set a time limit for sample acquisition
220 HWCAP_LIMIT_SAMPLES, // set a limit on number of samples
223 struct hwcap_option {
230 struct sigrok_device_instance {
238 struct usb_device_instance *usb;
239 struct serial_device_instance *serial;
243 /* sigrok_device_instance types */
249 struct usb_device_instance {
252 struct libusb_device_handle *devhdl;
255 struct serial_device_instance {
260 /* Device instance status */
263 /* Found, but still booting */
265 /* Live, but not in use */
267 /* Actively in use in a session */
272 * TODO: This sucks, you just kinda have to "know" the returned type.
273 * TODO: Need a DI to return the number of trigger stages supported.
276 /* Device info IDs */
278 /* struct sigrok_device_instance for this specific device */
280 /* The number of probes connected to this device */
282 /* Samplerates supported by this device, (struct samplerates) */
284 /* Types of trigger supported, out of "01crf" (char *) */
286 /* The currently set samplerate in Hz (uint64_t) */
290 /* a device supports either a range of samplerates with steps of a given
291 * granularity, or is limited to a set of defined samplerates. use either
292 * step or list, but not both.
301 struct device_plugin {
302 /* plugin-specific */
305 int (*init) (char *deviceinfo);
306 void (*cleanup) (void);
308 /* device-specific */
309 int (*open) (int device_index);
310 void (*close) (int device_index);
311 void *(*get_device_info) (int device_index, int device_info_id);
312 int (*get_status) (int device_index);
313 int *(*get_capabilities) (void);
314 int (*set_configuration) (int device_index, int capability, void *value);
315 int (*start_acquisition) (int device_index, gpointer session_device_id);
316 void (*stop_acquisition) (int device_index, gpointer session_device_id);
322 /* Not really using this */
323 GSource *timeout_source;
326 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
328 int load_hwplugins(void);
329 GSList *list_hwplugins(void);
331 /* Generic device instances */
332 struct sigrok_device_instance *sigrok_device_instance_new(int index,
333 int status, char *vendor, char *model, char *version);
334 struct sigrok_device_instance *get_sigrok_device_instance(GSList *device_instances, int device_index);
335 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
337 /* USB-specific instances */
338 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
339 uint8_t address, struct libusb_device_handle *hdl);
340 void usb_device_instance_free(struct usb_device_instance *usb);
342 /* Serial-specific instances */
343 struct serial_device_instance *serial_device_instance_new(char *port, int fd);
344 void serial_device_instance_free(struct serial_device_instance *serial);
346 int find_hwcap(int *capabilities, int hwcap);
347 struct hwcap_option *find_hwcap_option(int hwcap);
348 void source_remove(int fd);
349 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb, void *user_data);
351 /*--- session.c -------------------------------------------------------------*/
353 typedef void (*source_callback_remove) (int fd);
354 typedef void (*source_callback_add) (int fd, int events, int timeout,
355 receive_data_callback callback, void *user_data);
356 typedef void (*datafeed_callback) (struct device *device,
357 struct datafeed_packet *packet);
360 /* List of struct device* */
362 /* List of struct analyzer* */
364 /* datafeed callbacks */
365 GSList *datafeed_callbacks;
370 struct session *session_load(char *filename);
371 struct session *session_new(void);
372 void session_destroy(void);
373 void session_device_clear(void);
374 int session_device_add(struct device *device);
376 /* Protocol analyzers setup */
377 void session_pa_clear(void);
378 void session_pa_add(struct analyzer *pa);
381 void session_datafeed_callback_clear(void);
382 void session_datafeed_callback_add(datafeed_callback callback);
384 /* Session control */
385 int session_start(void);
386 void session_stop(void);
387 void session_bus(struct device *device, struct datafeed_packet *packet);
388 void make_metadata(char *filename);
389 int session_save(char *filename);
391 /*--- hwcommon.c ------------------------------------------------------------*/
393 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
394 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
396 GSList *list_serial_ports(void);