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)
58 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
61 /* Data types, used by hardware plugins for set_configuration() */
71 /* (Unused) protocol decoder stack entry */
82 /* datafeed_packet.type values */
95 struct datafeed_packet {
101 struct datafeed_header {
103 struct timeval starttime;
113 struct output_format *format;
114 struct device *device;
119 struct output_format {
122 int (*init) (struct output *o);
123 int (*data) (struct output *o, char *data_in, uint64_t length_in,
124 char **data_out, uint64_t *length_out);
125 int (*event) (struct output *o, int event_type, char **data_out,
126 uint64_t *length_out);
129 struct output_format **output_list(void);
130 int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
131 char *data_in, uint64_t length_in, char **data_out,
132 uint64_t *length_out);
134 char *sigrok_samplerate_string(uint64_t samplerate);
136 /*--- analyzer.c ------------------------------------------------------------*/
142 * TODO: Parameters? If so, configured plugins need another struct.
143 * TODO: Input and output format?
147 /*--- backend.c -------------------------------------------------------------*/
149 int sigrok_init(void);
150 void sigrok_cleanup(void);
152 /*--- datastore.c -----------------------------------------------------------*/
154 /* Size of a chunk in units */
155 #define DATASTORE_CHUNKSIZE 512000
158 /* Size in bytes of the number of units stored in this datastore */
160 unsigned int num_units;
164 struct datastore *datastore_new(int unitsize);
165 void datastore_destroy(struct datastore *ds);
166 void datastore_put(struct datastore *ds, void *data, unsigned int length,
167 int in_unitsize, int *probelist);
169 /*--- debug.c ---------------------------------------------------------------*/
171 void hexdump(unsigned char *address, int length);
173 /*--- device.c --------------------------------------------------------------*/
176 * This represents a generic device connected to the system.
177 * For device-specific information, ask the plugin. The plugin_index refers
178 * to the device index within that plugin; it may be handling more than one
179 * device. All relevant plugin calls take a device_index parameter for this.
182 /* Which plugin handles this device */
183 struct device_plugin *plugin;
184 /* A plugin may handle multiple devices of the same type */
186 /* List of struct probe* */
188 /* Data acquired by this device, if any */
189 struct datastore *datastore;
199 extern GSList *devices;
201 void device_scan(void);
202 void device_close_all(void);
203 GSList *device_list(void);
204 struct device *device_new(struct device_plugin *plugin, int plugin_index);
205 void device_clear(struct device *device);
206 void device_destroy(struct device *dev);
208 void device_probe_clear(struct device *device, int probenum);
209 void device_probe_add(struct device *device, char *name);
210 struct probe *probe_find(struct device *device, int probenum);
211 void device_probe_name(struct device *device, int probenum, char *name);
213 void device_trigger_clear(struct device *device);
214 void device_trigger_set(struct device *device, int probenum, char *trigger);
216 /*--- hwplugin.c ------------------------------------------------------------*/
218 /* Hardware plugin capabilities */
220 HWCAP_DUMMY, /* Used to terminate lists */
221 HWCAP_LOGIC_ANALYZER,
222 HWCAP_SAMPLERATE, /* Change samplerate */
223 HWCAP_PROBECONFIG, /* Configure probe mask */
224 HWCAP_CAPTURE_RATIO, /* Set pre-trigger / post-trigger ratio */
225 HWCAP_LIMIT_MSEC, /* Set a time limit for sample acquisition */
226 HWCAP_LIMIT_SAMPLES, /* Set a limit on number of samples */
229 struct hwcap_option {
236 struct sigrok_device_instance {
244 struct usb_device_instance *usb;
245 struct serial_device_instance *serial;
249 /* sigrok_device_instance types */
255 struct usb_device_instance {
258 struct libusb_device_handle *devhdl;
261 struct serial_device_instance {
266 /* Device instance status */
269 /* Found, but still booting */
271 /* Live, but not in use */
273 /* Actively in use in a session */
278 * TODO: This sucks, you just kinda have to "know" the returned type.
279 * TODO: Need a DI to return the number of trigger stages supported.
282 /* Device info IDs */
284 /* struct sigrok_device_instance for this specific device */
286 /* The number of probes connected to this device */
288 /* Samplerates supported by this device, (struct samplerates) */
290 /* Types of trigger supported, out of "01crf" (char *) */
292 /* The currently set samplerate in Hz (uint64_t) */
297 * A device supports either a range of samplerates with steps of a given
298 * granularity, or is limited to a set of defined samplerates. Use either
299 * step or list, but not both.
308 struct device_plugin {
309 /* Plugin-specific */
312 int (*init) (char *deviceinfo);
313 void (*cleanup) (void);
315 /* Device-specific */
316 int (*open) (int device_index);
317 void (*close) (int device_index);
318 void *(*get_device_info) (int device_index, int device_info_id);
319 int (*get_status) (int device_index);
320 int *(*get_capabilities) (void);
321 int (*set_configuration) (int device_index, int capability, void *value);
322 int (*start_acquisition) (int device_index, gpointer session_device_id);
323 void (*stop_acquisition) (int device_index, gpointer session_device_id);
329 /* Not really using this */
330 GSource *timeout_source;
333 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
335 int load_hwplugins(void);
336 GSList *list_hwplugins(void);
338 /* Generic device instances */
339 struct sigrok_device_instance *sigrok_device_instance_new(int index,
340 int status, char *vendor, char *model, char *version);
341 struct sigrok_device_instance *get_sigrok_device_instance(
342 GSList *device_instances, int device_index);
343 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
345 /* USB-specific instances */
346 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
347 uint8_t address, struct libusb_device_handle *hdl);
348 void usb_device_instance_free(struct usb_device_instance *usb);
350 /* Serial-specific instances */
351 struct serial_device_instance *serial_device_instance_new(char *port, int fd);
352 void serial_device_instance_free(struct serial_device_instance *serial);
354 int find_hwcap(int *capabilities, int hwcap);
355 struct hwcap_option *find_hwcap_option(int hwcap);
356 void source_remove(int fd);
357 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
360 /*--- session.c -------------------------------------------------------------*/
362 typedef void (*source_callback_remove) (int fd);
363 typedef void (*source_callback_add) (int fd, int events, int timeout,
364 receive_data_callback callback, void *user_data);
365 typedef void (*datafeed_callback) (struct device *device,
366 struct datafeed_packet *packet);
369 /* List of struct device* */
371 /* List of struct analyzer* */
373 /* Datafeed callbacks */
374 GSList *datafeed_callbacks;
379 struct session *session_load(const char *filename);
380 struct session *session_new(void);
381 void session_destroy(void);
382 void session_device_clear(void);
383 int session_device_add(struct device *device);
385 /* Protocol analyzers setup */
386 void session_pa_clear(void);
387 void session_pa_add(struct analyzer *pa);
390 void session_datafeed_callback_clear(void);
391 void session_datafeed_callback_add(datafeed_callback callback);
393 /* Session control */
394 int session_start(void);
395 void session_stop(void);
396 void session_bus(struct device *device, struct datafeed_packet *packet);
397 void make_metadata(char *filename);
398 int session_save(char *filename);
400 /*--- hwcommon.c ------------------------------------------------------------*/
402 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
403 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
404 int ezusb_upload_firmware(libusb_device *dev, int configuration,
405 const char *filename);
407 GSList *list_serial_ports(void);
408 int serial_open(const char *pathname, int flags);
409 int serial_close(int fd);
410 void *serial_backup_params(int fd);
411 void serial_restore_params(int fd, void *backup);
412 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,