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 functions returns a "malloc error" it must be exactly the same
39 * return value as used by 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 char *sigrok_samplerate_string(uint64_t samplerate);
132 /*--- analyzer.c ------------------------------------------------------------*/
138 * TODO: Parameters? If so, configured plugins need another struct.
139 * TODO: Input and output format?
143 /*--- backend.c -------------------------------------------------------------*/
145 int sigrok_init(void);
146 void sigrok_cleanup(void);
148 /*--- datastore.c -----------------------------------------------------------*/
150 /* Size of a chunk in units */
151 #define DATASTORE_CHUNKSIZE 512000
154 /* Size in bytes of the number of units stored in this datastore */
156 unsigned int num_units;
160 struct datastore *datastore_new(int unitsize);
161 void datastore_destroy(struct datastore *ds);
162 void datastore_put(struct datastore *ds, void *data, unsigned int length,
163 int in_unitsize, int *probelist);
165 /*--- debug.c ---------------------------------------------------------------*/
167 void hexdump(unsigned char *address, int length);
169 /*--- device.c --------------------------------------------------------------*/
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.
178 /* Which plugin handles this device */
179 struct device_plugin *plugin;
180 /* A plugin may handle multiple devices of the same type */
182 /* List of struct probe* */
184 /* Data acquired by this device, if any */
185 struct datastore *datastore;
195 extern GSList *devices;
197 void device_scan(void);
198 void device_close_all(void);
199 GSList *device_list(void);
200 struct device *device_new(struct device_plugin *plugin, int plugin_index);
201 void device_clear(struct device *device);
202 void device_destroy(struct device *dev);
204 void device_probe_clear(struct device *device, int probenum);
205 void device_probe_add(struct device *device, char *name);
206 struct probe *probe_find(struct device *device, int probenum);
207 void device_probe_name(struct device *device, int probenum, char *name);
209 void device_trigger_clear(struct device *device);
210 void device_trigger_set(struct device *device, int probenum, char *trigger);
212 /*--- hwplugin.c ------------------------------------------------------------*/
214 /* Hardware plugin capabilities */
216 HWCAP_DUMMY, // used to terminate lists
217 HWCAP_LOGIC_ANALYZER,
218 HWCAP_SAMPLERATE, // change samplerate
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
225 struct hwcap_option {
232 struct sigrok_device_instance {
240 struct usb_device_instance *usb;
241 struct serial_device_instance *serial;
245 /* sigrok_device_instance types */
251 struct usb_device_instance {
254 struct libusb_device_handle *devhdl;
257 struct serial_device_instance {
262 /* Device instance status */
265 /* Found, but still booting */
267 /* Live, but not in use */
269 /* Actively in use in a session */
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.
278 /* Device info IDs */
280 /* struct sigrok_device_instance for this specific device */
282 /* The number of probes connected to this device */
284 /* Samplerates supported by this device, (struct samplerates) */
286 /* Types of trigger supported, out of "01crf" (char *) */
288 /* The currently set samplerate in Hz (uint64_t) */
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.
303 struct device_plugin {
304 /* plugin-specific */
307 int (*init) (char *deviceinfo);
308 void (*cleanup) (void);
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);
324 /* Not really using this */
325 GSource *timeout_source;
328 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
330 int load_hwplugins(void);
331 GSList *list_hwplugins(void);
333 /* Generic device instances */
334 struct sigrok_device_instance *sigrok_device_instance_new(int index,
335 int status, char *vendor, char *model, char *version);
336 struct sigrok_device_instance *get_sigrok_device_instance(GSList *device_instances, int device_index);
337 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
339 /* USB-specific instances */
340 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
341 uint8_t address, struct libusb_device_handle *hdl);
342 void usb_device_instance_free(struct usb_device_instance *usb);
344 /* Serial-specific instances */
345 struct serial_device_instance *serial_device_instance_new(char *port, int fd);
346 void serial_device_instance_free(struct serial_device_instance *serial);
348 int find_hwcap(int *capabilities, int hwcap);
349 struct hwcap_option *find_hwcap_option(int hwcap);
350 void source_remove(int fd);
351 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb, void *user_data);
353 /*--- session.c -------------------------------------------------------------*/
355 typedef void (*source_callback_remove) (int fd);
356 typedef void (*source_callback_add) (int fd, int events, int timeout,
357 receive_data_callback callback, void *user_data);
358 typedef void (*datafeed_callback) (struct device *device,
359 struct datafeed_packet *packet);
362 /* List of struct device* */
364 /* List of struct analyzer* */
366 /* datafeed callbacks */
367 GSList *datafeed_callbacks;
372 struct session *session_load(char *filename);
373 struct session *session_new(void);
374 void session_destroy(void);
375 void session_device_clear(void);
376 int session_device_add(struct device *device);
378 /* Protocol analyzers setup */
379 void session_pa_clear(void);
380 void session_pa_add(struct analyzer *pa);
383 void session_datafeed_callback_clear(void);
384 void session_datafeed_callback_add(datafeed_callback callback);
386 /* Session control */
387 int session_start(void);
388 void session_stop(void);
389 void session_bus(struct device *device, struct datafeed_packet *packet);
390 void make_metadata(char *filename);
391 int session_save(char *filename);
393 /*--- hwcommon.c ------------------------------------------------------------*/
395 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
396 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
398 GSList *list_serial_ports(void);
399 int serial_open(const char *pathname, int flags);
400 int serial_close(int fd);
401 void *serial_backup_params(int fd);
402 void serial_restore_params(int fd, void *backup);
403 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, int flowcontrol);