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 /* limited by uint64_t */
53 #define MAX_NUM_PROBES 64
54 #define MAX_PROBENAME_LEN 32
57 /* Handy little macros */
58 #define KHZ(n) ((n) * 1000)
59 #define MHZ(n) ((n) * 1000000)
60 #define GHZ(n) ((n) * 1000000000)
63 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
66 /* Data types, used by hardware plugins for set_configuration() */
76 /* (Unused) protocol decoder stack entry */
87 /* datafeed_packet.type values */
97 struct datafeed_packet {
104 struct datafeed_header {
106 struct timeval starttime;
116 struct input_format *format;
121 struct input_format {
124 int (*format_match) (const char *filename);
125 int (*in_loadfile) (const char *filename);
128 struct input_format **input_list(void);
135 struct output_format *format;
136 struct device *device;
141 struct output_format {
144 int (*init) (struct output *o);
145 int (*data) (struct output *o, char *data_in, uint64_t length_in,
146 char **data_out, uint64_t *length_out);
147 int (*event) (struct output *o, int event_type, char **data_out,
148 uint64_t *length_out);
151 struct output_format **output_list(void);
154 int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
155 char *data_in, uint64_t length_in, char **data_out,
156 uint64_t *length_out);
158 char *sigrok_samplerate_string(uint64_t samplerate);
159 char *sigrok_period_string(uint64_t frequency);
161 /*--- analyzer.c ------------------------------------------------------------*/
167 * TODO: Parameters? If so, configured plugins need another struct.
168 * TODO: Input and output format?
172 /*--- backend.c -------------------------------------------------------------*/
174 int sigrok_init(void);
175 void sigrok_cleanup(void);
177 /*--- datastore.c -----------------------------------------------------------*/
179 /* Size of a chunk in units */
180 #define DATASTORE_CHUNKSIZE 512000
183 /* Size in bytes of the number of units stored in this datastore */
185 unsigned int num_units; /* TODO: uint64_t */
189 int datastore_new(int unitsize, struct datastore **ds);
190 int datastore_destroy(struct datastore *ds);
191 void datastore_put(struct datastore *ds, void *data, unsigned int length,
192 int in_unitsize, int *probelist);
194 /*--- debug.c ---------------------------------------------------------------*/
196 void hexdump(unsigned char *address, int length);
198 /*--- device.c --------------------------------------------------------------*/
201 * This represents a generic device connected to the system.
202 * For device-specific information, ask the plugin. The plugin_index refers
203 * to the device index within that plugin; it may be handling more than one
204 * device. All relevant plugin calls take a device_index parameter for this.
207 /* Which plugin handles this device */
208 struct device_plugin *plugin;
209 /* A plugin may handle multiple devices of the same type */
211 /* List of struct probe* */
213 /* Data acquired by this device, if any */
214 struct datastore *datastore;
224 extern GSList *devices;
226 void device_scan(void);
227 void device_close_all(void);
228 GSList *device_list(void);
229 struct device *device_new(struct device_plugin *plugin, int plugin_index, int num_probes);
230 void device_clear(struct device *device);
231 void device_destroy(struct device *dev);
233 void device_probe_clear(struct device *device, int probenum);
234 void device_probe_add(struct device *device, char *name);
235 struct probe *probe_find(struct device *device, int probenum);
236 void device_probe_name(struct device *device, int probenum, char *name);
238 void device_trigger_clear(struct device *device);
239 void device_trigger_set(struct device *device, int probenum, char *trigger);
241 /*--- hwplugin.c ------------------------------------------------------------*/
243 /* Hardware plugin capabilities */
245 HWCAP_DUMMY, /* Used to terminate lists */
246 HWCAP_LOGIC_ANALYZER,
247 HWCAP_SAMPLERATE, /* Change samplerate */
248 HWCAP_PROBECONFIG, /* Configure probe mask */
249 HWCAP_CAPTURE_RATIO, /* Set pre/post-trigger capture ratio */
250 HWCAP_LIMIT_MSEC, /* Set a time limit for sample acquisition */
251 HWCAP_LIMIT_SAMPLES, /* Set a limit on number of samples */
254 struct hwcap_option {
261 struct sigrok_device_instance {
269 struct usb_device_instance *usb;
270 struct serial_device_instance *serial;
274 /* sigrok_device_instance types */
280 struct usb_device_instance {
283 struct libusb_device_handle *devhdl;
286 struct serial_device_instance {
291 /* Device instance status */
294 /* Found, but still booting */
296 /* Live, but not in use */
298 /* Actively in use in a session */
303 * TODO: This sucks, you just kinda have to "know" the returned type.
304 * TODO: Need a DI to return the number of trigger stages supported.
307 /* Device info IDs */
309 /* struct sigrok_device_instance for this specific device */
311 /* The number of probes connected to this device */
313 /* Samplerates supported by this device, (struct samplerates) */
315 /* Types of trigger supported, out of "01crf" (char *) */
317 /* The currently set samplerate in Hz (uint64_t) */
322 * A device supports either a range of samplerates with steps of a given
323 * granularity, or is limited to a set of defined samplerates. Use either
324 * step or list, but not both.
333 struct device_plugin {
334 /* Plugin-specific */
337 int (*init) (char *deviceinfo);
338 void (*cleanup) (void);
340 /* Device-specific */
341 int (*open) (int device_index);
342 void (*close) (int device_index);
343 void *(*get_device_info) (int device_index, int device_info_id);
344 int (*get_status) (int device_index);
345 int *(*get_capabilities) (void);
346 int (*set_configuration) (int device_index, int capability, void *value);
347 int (*start_acquisition) (int device_index, gpointer session_device_id);
348 void (*stop_acquisition) (int device_index, gpointer session_device_id);
354 /* Not really using this */
355 GSource *timeout_source;
358 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
360 int load_hwplugins(void);
361 GSList *list_hwplugins(void);
363 /* Generic device instances */
364 struct sigrok_device_instance *sigrok_device_instance_new(int index,
365 int status, const char *vendor, const char *model, const char *version);
366 struct sigrok_device_instance *get_sigrok_device_instance(
367 GSList *device_instances, int device_index);
368 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
370 /* USB-specific instances */
371 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
372 uint8_t address, struct libusb_device_handle *hdl);
373 void usb_device_instance_free(struct usb_device_instance *usb);
375 /* Serial-specific instances */
376 struct serial_device_instance *serial_device_instance_new(
377 const char *port, int fd);
378 void serial_device_instance_free(struct serial_device_instance *serial);
380 int find_hwcap(int *capabilities, int hwcap);
381 struct hwcap_option *find_hwcap_option(int hwcap);
382 void source_remove(int fd);
383 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
386 /*--- session.c -------------------------------------------------------------*/
388 typedef void (*source_callback_remove) (int fd);
389 typedef void (*source_callback_add) (int fd, int events, int timeout,
390 receive_data_callback callback, void *user_data);
391 typedef void (*datafeed_callback) (struct device *device,
392 struct datafeed_packet *packet);
395 /* List of struct device* */
397 /* List of struct analyzer* */
399 /* Datafeed callbacks */
400 GSList *datafeed_callbacks;
405 struct session *session_load(const char *filename);
406 struct session *session_new(void);
407 void session_destroy(void);
408 void session_device_clear(void);
409 int session_device_add(struct device *device);
411 /* Protocol analyzers setup */
412 void session_pa_clear(void);
413 void session_pa_add(struct analyzer *pa);
416 void session_datafeed_callback_clear(void);
417 void session_datafeed_callback_add(datafeed_callback callback);
419 /* Session control */
420 int session_start(void);
421 void session_stop(void);
422 void session_bus(struct device *device, struct datafeed_packet *packet);
423 void make_metadata(char *filename);
424 int session_save(char *filename);
426 /*--- hwcommon.c ------------------------------------------------------------*/
428 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
429 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
430 int ezusb_upload_firmware(libusb_device *dev, int configuration,
431 const char *filename);
433 GSList *list_serial_ports(void);
434 int serial_open(const char *pathname, int flags);
435 int serial_close(int fd);
436 int serial_flush(int fd);
437 void *serial_backup_params(int fd);
438 void serial_restore_params(int fd, void *backup);
439 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
442 /* libsigrok/hardware/common/misc.c */
443 /* TODO: Should not be public. */
444 int opendev2(int device_index, struct sigrok_device_instance **sdi,
445 libusb_device *dev, struct libusb_device_descriptor *des,
446 int *skip, uint16_t vid, uint16_t pid, int interface);
447 int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
448 struct libusb_device_descriptor *des,
449 uint16_t vid, uint16_t pid, int interface);