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 input_format *format;
118 struct input_format {
121 int (*format_match) (char *filename);
122 int (*in_loadfile) (char *filename);
125 struct input_format **input_list(void);
132 struct output_format *format;
133 struct device *device;
138 struct output_format {
141 int (*init) (struct output *o);
142 int (*data) (struct output *o, char *data_in, uint64_t length_in,
143 char **data_out, uint64_t *length_out);
144 int (*event) (struct output *o, int event_type, char **data_out,
145 uint64_t *length_out);
148 struct output_format **output_list(void);
151 int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
152 char *data_in, uint64_t length_in, char **data_out,
153 uint64_t *length_out);
155 char *sigrok_samplerate_string(uint64_t samplerate);
157 /*--- analyzer.c ------------------------------------------------------------*/
163 * TODO: Parameters? If so, configured plugins need another struct.
164 * TODO: Input and output format?
168 /*--- backend.c -------------------------------------------------------------*/
170 int sigrok_init(void);
171 void sigrok_cleanup(void);
173 /*--- datastore.c -----------------------------------------------------------*/
175 /* Size of a chunk in units */
176 #define DATASTORE_CHUNKSIZE 512000
179 /* Size in bytes of the number of units stored in this datastore */
181 unsigned int num_units; /* TODO: uint64_t */
185 int datastore_new(int unitsize, struct datastore **ds);
186 int datastore_destroy(struct datastore *ds);
187 void datastore_put(struct datastore *ds, void *data, unsigned int length,
188 int in_unitsize, int *probelist);
190 /*--- debug.c ---------------------------------------------------------------*/
192 void hexdump(unsigned char *address, int length);
194 /*--- device.c --------------------------------------------------------------*/
197 * This represents a generic device connected to the system.
198 * For device-specific information, ask the plugin. The plugin_index refers
199 * to the device index within that plugin; it may be handling more than one
200 * device. All relevant plugin calls take a device_index parameter for this.
203 /* Which plugin handles this device */
204 struct device_plugin *plugin;
205 /* A plugin may handle multiple devices of the same type */
207 /* List of struct probe* */
209 /* Data acquired by this device, if any */
210 struct datastore *datastore;
220 extern GSList *devices;
222 void device_scan(void);
223 void device_close_all(void);
224 GSList *device_list(void);
225 struct device *device_new(struct device_plugin *plugin, int plugin_index, int num_probes);
226 void device_clear(struct device *device);
227 void device_destroy(struct device *dev);
229 void device_probe_clear(struct device *device, int probenum);
230 void device_probe_add(struct device *device, char *name);
231 struct probe *probe_find(struct device *device, int probenum);
232 void device_probe_name(struct device *device, int probenum, char *name);
234 void device_trigger_clear(struct device *device);
235 void device_trigger_set(struct device *device, int probenum, char *trigger);
237 /*--- hwplugin.c ------------------------------------------------------------*/
239 /* Hardware plugin capabilities */
241 HWCAP_DUMMY, /* Used to terminate lists */
242 HWCAP_LOGIC_ANALYZER,
243 HWCAP_SAMPLERATE, /* Change samplerate */
244 HWCAP_PROBECONFIG, /* Configure probe mask */
245 HWCAP_CAPTURE_RATIO, /* Set pre-trigger / post-trigger ratio */
246 HWCAP_LIMIT_MSEC, /* Set a time limit for sample acquisition */
247 HWCAP_LIMIT_SAMPLES, /* Set a limit on number of samples */
250 struct hwcap_option {
257 struct sigrok_device_instance {
265 struct usb_device_instance *usb;
266 struct serial_device_instance *serial;
270 /* sigrok_device_instance types */
276 struct usb_device_instance {
279 struct libusb_device_handle *devhdl;
282 struct serial_device_instance {
287 /* Device instance status */
290 /* Found, but still booting */
292 /* Live, but not in use */
294 /* Actively in use in a session */
299 * TODO: This sucks, you just kinda have to "know" the returned type.
300 * TODO: Need a DI to return the number of trigger stages supported.
303 /* Device info IDs */
305 /* struct sigrok_device_instance for this specific device */
307 /* The number of probes connected to this device */
309 /* Samplerates supported by this device, (struct samplerates) */
311 /* Types of trigger supported, out of "01crf" (char *) */
313 /* The currently set samplerate in Hz (uint64_t) */
318 * A device supports either a range of samplerates with steps of a given
319 * granularity, or is limited to a set of defined samplerates. Use either
320 * step or list, but not both.
329 struct device_plugin {
330 /* Plugin-specific */
333 int (*init) (char *deviceinfo);
334 void (*cleanup) (void);
336 /* Device-specific */
337 int (*open) (int device_index);
338 void (*close) (int device_index);
339 void *(*get_device_info) (int device_index, int device_info_id);
340 int (*get_status) (int device_index);
341 int *(*get_capabilities) (void);
342 int (*set_configuration) (int device_index, int capability, void *value);
343 int (*start_acquisition) (int device_index, gpointer session_device_id);
344 void (*stop_acquisition) (int device_index, gpointer session_device_id);
350 /* Not really using this */
351 GSource *timeout_source;
354 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
356 int load_hwplugins(void);
357 GSList *list_hwplugins(void);
359 /* Generic device instances */
360 struct sigrok_device_instance *sigrok_device_instance_new(int index,
361 int status, const char *vendor, const char *model, const char *version);
362 struct sigrok_device_instance *get_sigrok_device_instance(
363 GSList *device_instances, int device_index);
364 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
366 /* USB-specific instances */
367 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
368 uint8_t address, struct libusb_device_handle *hdl);
369 void usb_device_instance_free(struct usb_device_instance *usb);
371 /* Serial-specific instances */
372 struct serial_device_instance *serial_device_instance_new(
373 const char *port, int fd);
374 void serial_device_instance_free(struct serial_device_instance *serial);
376 int find_hwcap(int *capabilities, int hwcap);
377 struct hwcap_option *find_hwcap_option(int hwcap);
378 void source_remove(int fd);
379 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
382 /*--- session.c -------------------------------------------------------------*/
384 typedef void (*source_callback_remove) (int fd);
385 typedef void (*source_callback_add) (int fd, int events, int timeout,
386 receive_data_callback callback, void *user_data);
387 typedef void (*datafeed_callback) (struct device *device,
388 struct datafeed_packet *packet);
391 /* List of struct device* */
393 /* List of struct analyzer* */
395 /* Datafeed callbacks */
396 GSList *datafeed_callbacks;
401 struct session *session_load(const char *filename);
402 struct session *session_new(void);
403 void session_destroy(void);
404 void session_device_clear(void);
405 int session_device_add(struct device *device);
407 /* Protocol analyzers setup */
408 void session_pa_clear(void);
409 void session_pa_add(struct analyzer *pa);
412 void session_datafeed_callback_clear(void);
413 void session_datafeed_callback_add(datafeed_callback callback);
415 /* Session control */
416 int session_start(void);
417 void session_stop(void);
418 void session_bus(struct device *device, struct datafeed_packet *packet);
419 void make_metadata(char *filename);
420 int session_save(char *filename);
422 /*--- hwcommon.c ------------------------------------------------------------*/
424 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
425 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
426 int ezusb_upload_firmware(libusb_device *dev, int configuration,
427 const char *filename);
429 GSList *list_serial_ports(void);
430 int serial_open(const char *pathname, int flags);
431 int serial_close(int fd);
432 void *serial_backup_params(int fd);
433 void serial_restore_params(int fd, void *backup);
434 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
437 /* libsigrok/hardware/common/misc.c */
438 /* TODO: Should not be public. */
439 int opendev2(int device_index, struct sigrok_device_instance **sdi,
440 libusb_device *dev, struct libusb_device_descriptor *des,
441 int *skip, uint16_t vid, uint16_t pid, int interface);
442 int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
443 struct libusb_device_descriptor *des,
444 uint16_t vid, uint16_t pid, int interface);