]> sigrok.org Git - libsigrok.git/blob - sigrok.h
add DF_ANALOG, and an analog output module
[libsigrok.git] / sigrok.h
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifndef SIGROK_SIGROK_H
21 #define SIGROK_SIGROK_H
22
23 #include <stdio.h>
24 #include <sys/time.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <glib.h>
28 #include <libusb.h>
29
30 /*
31  * Status/error codes returned by libsigrok functions.
32  *
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.
36  *
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
41  * same return code.
42  *
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.
46  */
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 */
51
52 /* limited by uint64_t */
53 #define MAX_NUM_PROBES 64
54 #define MAX_PROBENAME_LEN 32
55
56
57 /* Handy little macros */
58 #define KHZ(n) ((n) * 1000)
59 #define MHZ(n) ((n) * 1000000)
60 #define GHZ(n) ((n) * 1000000000)
61
62 #ifndef ARRAY_SIZE
63 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64 #endif
65
66 #ifndef ARRAY_AND_SIZE
67 #define ARRAY_AND_SIZE(a) (a), ARRAY_SIZE(a)
68 #endif
69
70 /* Data types, used by hardware plugins for set_configuration() */
71 enum {
72         T_UINT64,
73         T_CHAR,
74 };
75
76 enum {
77         PROTO_RAW,
78 };
79
80 /* (Unused) protocol decoder stack entry */
81 struct protocol {
82         char *name;
83         int id;
84         int stackindex;
85 };
86
87 /*
88  * Datafeed
89  */
90
91 /* datafeed_packet.type values */
92 enum {
93         DF_HEADER,
94         DF_END,
95         DF_TRIGGER,
96         DF_LOGIC,
97         DF_ANALOG,
98         DF_PD,
99 };
100
101 struct datafeed_packet {
102         uint16_t type;
103         uint64_t length;
104         uint16_t unitsize;
105         void *payload;
106 };
107
108 struct datafeed_header {
109         int feed_version;
110         struct timeval starttime;
111         uint64_t samplerate;
112         int protocol_id;
113         int num_probes;
114 };
115
116 /*
117  * Input
118  */
119 struct input {
120         struct input_format *format;
121         void *param;
122         void *internal;
123 };
124
125 struct input_format {
126         char *extension;
127         char *description;
128         int (*format_match) (const char *filename);
129         int (*in_loadfile) (const char *filename);
130 };
131
132 struct input_format **input_list(void);
133
134
135 /*
136  * Output
137  */
138 struct output {
139         struct output_format *format;
140         struct device *device;
141         char *param;
142         void *internal;
143 };
144
145 struct output_format {
146         char *extension;
147         char *description;
148         int df_type;
149         int (*init) (struct output *o);
150         int (*data) (struct output *o, char *data_in, uint64_t length_in,
151                      char **data_out, uint64_t *length_out);
152         int (*event) (struct output *o, int event_type, char **data_out,
153                       uint64_t *length_out);
154 };
155
156 struct output_format **output_list(void);
157
158
159 int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
160                   char *data_in, uint64_t length_in, char **data_out,
161                   uint64_t *length_out);
162
163 char *sigrok_samplerate_string(uint64_t samplerate);
164 char *sigrok_period_string(uint64_t frequency);
165
166 /*--- analyzer.c ------------------------------------------------------------*/
167
168 struct analyzer {
169         char *name;
170         char *filename;
171         /*
172          * TODO: Parameters? If so, configured plugins need another struct.
173          * TODO: Input and output format?
174          */
175 };
176
177 /*--- backend.c -------------------------------------------------------------*/
178
179 int sigrok_init(void);
180 void sigrok_cleanup(void);
181
182 /*--- datastore.c -----------------------------------------------------------*/
183
184 /* Size of a chunk in units */
185 #define DATASTORE_CHUNKSIZE 512000
186
187 struct datastore {
188         /* Size in bytes of the number of units stored in this datastore */
189         int ds_unitsize;
190         unsigned int num_units; /* TODO: uint64_t */
191         GSList *chunklist;
192 };
193
194 int datastore_new(int unitsize, struct datastore **ds);
195 int datastore_destroy(struct datastore *ds);
196 void datastore_put(struct datastore *ds, void *data, unsigned int length,
197                    int in_unitsize, int *probelist);
198
199 /*--- debug.c ---------------------------------------------------------------*/
200
201 void hexdump(unsigned char *address, int length);
202
203 /*--- device.c --------------------------------------------------------------*/
204
205 /*
206  * This represents a generic device connected to the system.
207  * For device-specific information, ask the plugin. The plugin_index refers
208  * to the device index within that plugin; it may be handling more than one
209  * device. All relevant plugin calls take a device_index parameter for this.
210  */
211 struct device {
212         /* Which plugin handles this device */
213         struct device_plugin *plugin;
214         /* A plugin may handle multiple devices of the same type */
215         int plugin_index;
216         /* List of struct probe* */
217         GSList *probes;
218         /* Data acquired by this device, if any */
219         struct datastore *datastore;
220 };
221
222 struct probe {
223         int index;
224         gboolean enabled;
225         char *name;
226         char *trigger;
227 };
228
229 extern GSList *devices;
230
231 void device_scan(void);
232 void device_close_all(void);
233 GSList *device_list(void);
234 struct device *device_new(struct device_plugin *plugin, int plugin_index, int num_probes);
235 void device_clear(struct device *device);
236 void device_destroy(struct device *dev);
237
238 void device_probe_clear(struct device *device, int probenum);
239 void device_probe_add(struct device *device, char *name);
240 struct probe *probe_find(struct device *device, int probenum);
241 void device_probe_name(struct device *device, int probenum, char *name);
242
243 void device_trigger_clear(struct device *device);
244 void device_trigger_set(struct device *device, int probenum, char *trigger);
245
246 /*--- hwplugin.c ------------------------------------------------------------*/
247
248 /* Hardware plugin capabilities */
249 enum {
250         HWCAP_DUMMY,            /* Used to terminate lists */
251         HWCAP_LOGIC_ANALYZER,
252         HWCAP_SAMPLERATE,       /* Change samplerate */
253         HWCAP_PROBECONFIG,      /* Configure probe mask */
254         HWCAP_CAPTURE_RATIO,    /* Set pre/post-trigger capture ratio */
255         HWCAP_LIMIT_MSEC,       /* Set a time limit for sample acquisition */
256         HWCAP_LIMIT_SAMPLES,    /* Set a limit on number of samples */
257 };
258
259 struct hwcap_option {
260         int capability;
261         int type;
262         char *description;
263         char *shortname;
264 };
265
266 struct sigrok_device_instance {
267         int index;
268         int status;
269         int instance_type;
270         char *vendor;
271         char *model;
272         char *version;
273         void *priv;
274         union {
275                 struct usb_device_instance *usb;
276                 struct serial_device_instance *serial;
277         };
278 };
279
280 /* sigrok_device_instance types */
281 enum {
282         USB_INSTANCE,
283         SERIAL_INSTANCE,
284 };
285
286 struct usb_device_instance {
287         uint8_t bus;
288         uint8_t address;
289         struct libusb_device_handle *devhdl;
290 };
291
292 struct serial_device_instance {
293         char *port;
294         int fd;
295 };
296
297 /* Device instance status */
298 enum {
299         ST_NOT_FOUND,
300         /* Found, but still booting */
301         ST_INITIALIZING,
302         /* Live, but not in use */
303         ST_INACTIVE,
304         /* Actively in use in a session */
305         ST_ACTIVE,
306 };
307
308 /*
309  * TODO: This sucks, you just kinda have to "know" the returned type.
310  * TODO: Need a DI to return the number of trigger stages supported.
311  */
312
313 /* Device info IDs */
314 enum {
315         /* struct sigrok_device_instance for this specific device */
316         DI_INSTANCE,
317         /* The number of probes connected to this device */
318         DI_NUM_PROBES,
319         /* Samplerates supported by this device, (struct samplerates) */
320         DI_SAMPLERATES,
321         /* Types of trigger supported, out of "01crf" (char *) */
322         DI_TRIGGER_TYPES,
323         /* The currently set samplerate in Hz (uint64_t) */
324         DI_CUR_SAMPLERATE,
325 };
326
327 /*
328  * A device supports either a range of samplerates with steps of a given
329  * granularity, or is limited to a set of defined samplerates. Use either
330  * step or list, but not both.
331  */
332 struct samplerates {
333         uint64_t low;
334         uint64_t high;
335         uint64_t step;
336         uint64_t *list;
337 };
338
339 struct device_plugin {
340         /* Plugin-specific */
341         char *name;
342         int api_version;
343         int (*init) (char *deviceinfo);
344         void (*cleanup) (void);
345
346         /* Device-specific */
347         int (*open) (int device_index);
348         void (*close) (int device_index);
349         void *(*get_device_info) (int device_index, int device_info_id);
350         int (*get_status) (int device_index);
351         int *(*get_capabilities) (void);
352         int (*set_configuration) (int device_index, int capability, void *value);
353         int (*start_acquisition) (int device_index, gpointer session_device_id);
354         void (*stop_acquisition) (int device_index, gpointer session_device_id);
355 };
356
357 struct gsource_fd {
358         GSource source;
359         GPollFD gpfd;
360         /* Not really using this */
361         GSource *timeout_source;
362 };
363
364 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
365
366 int load_hwplugins(void);
367 GSList *list_hwplugins(void);
368
369 /* Generic device instances */
370 struct sigrok_device_instance *sigrok_device_instance_new(int index,
371        int status, const char *vendor, const char *model, const char *version);
372 struct sigrok_device_instance *get_sigrok_device_instance(
373                         GSList *device_instances, int device_index);
374 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
375
376 /* USB-specific instances */
377 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
378                 uint8_t address, struct libusb_device_handle *hdl);
379 void usb_device_instance_free(struct usb_device_instance *usb);
380
381 /* Serial-specific instances */
382 struct serial_device_instance *serial_device_instance_new(
383                                         const char *port, int fd);
384 void serial_device_instance_free(struct serial_device_instance *serial);
385
386 int find_hwcap(int *capabilities, int hwcap);
387 struct hwcap_option *find_hwcap_option(int hwcap);
388 void source_remove(int fd);
389 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
390                 void *user_data);
391
392 /*--- session.c -------------------------------------------------------------*/
393
394 typedef void (*source_callback_remove) (int fd);
395 typedef void (*source_callback_add) (int fd, int events, int timeout,
396                 receive_data_callback callback, void *user_data);
397 typedef void (*datafeed_callback) (struct device *device,
398                                  struct datafeed_packet *packet);
399
400 struct session {
401         /* List of struct device* */
402         GSList *devices;
403         /* List of struct analyzer* */
404         GSList *analyzers;
405         /* Datafeed callbacks */
406         GSList *datafeed_callbacks;
407         GTimeVal starttime;
408 };
409
410 /* Session setup */
411 struct session *session_load(const char *filename);
412 struct session *session_new(void);
413 void session_destroy(void);
414 void session_device_clear(void);
415 int session_device_add(struct device *device);
416
417 /* Protocol analyzers setup */
418 void session_pa_clear(void);
419 void session_pa_add(struct analyzer *pa);
420
421 /* Datafeed setup */
422 void session_datafeed_callback_clear(void);
423 void session_datafeed_callback_add(datafeed_callback callback);
424
425 /* Session control */
426 int session_start(void);
427 void session_stop(void);
428 void session_bus(struct device *device, struct datafeed_packet *packet);
429 void make_metadata(char *filename);
430 int session_save(char *filename);
431
432 /*--- hwcommon.c ------------------------------------------------------------*/
433
434 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
435 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
436 int ezusb_upload_firmware(libusb_device *dev, int configuration,
437                           const char *filename);
438
439 GSList *list_serial_ports(void);
440 int serial_open(const char *pathname, int flags);
441 int serial_close(int fd);
442 int serial_flush(int fd);
443 void *serial_backup_params(int fd);
444 void serial_restore_params(int fd, void *backup);
445 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
446                       int flowcontrol);
447
448 /* libsigrok/hardware/common/misc.c */
449 /* TODO: Should not be public. */
450 int opendev2(int device_index, struct sigrok_device_instance **sdi,
451              libusb_device *dev, struct libusb_device_descriptor *des,
452              int *skip, uint16_t vid, uint16_t pid, int interface);
453 int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
454              struct libusb_device_descriptor *des,
455              uint16_t vid, uint16_t pid, int interface);
456
457 #endif