]> sigrok.org Git - libsigrok.git/blob - sigrok.h
4c3af637779d5fbfd5089631d712c63593449ea9
[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 /* Handy little macros */
53 #define KHZ(n) (n * 1000)
54 #define MHZ(n) (n * 1000000)
55 #define GHZ(n) (n * 1000000000)
56
57 /* Data types, used by hardware plugins for set_configuration() */
58 enum {
59         T_UINT64,
60         T_CHAR,
61 };
62
63 enum {
64         PROTO_RAW,
65 };
66
67 /* (Unused) protocol decoder stack entry */
68 struct protocol {
69         char *name;
70         int id;
71         int stackindex;
72 };
73
74 /*
75  * datafeed
76  */
77
78 /* datafeed_packet.type values */
79 enum {
80         DF_HEADER,
81         DF_END,
82         DF_TRIGGER,
83         DF_LOGIC8,
84         DF_LOGIC16,
85         DF_LOGIC24,
86         DF_LOGIC32,
87         DF_LOGIC48,
88         DF_LOGIC64,
89 };
90
91 struct datafeed_packet {
92         uint16_t type;
93         uint16_t length;
94         void *payload;
95 };
96
97 struct datafeed_header {
98         int feed_version;
99         struct timeval starttime;
100         uint64_t samplerate;
101         int protocol_id;
102         int num_probes;
103 };
104
105 /*
106  * output
107  */
108 struct output {
109         struct output_format *format;
110         struct device *device;
111         char *param;
112         void *internal;
113 };
114
115 struct output_format {
116         char *extension;
117         char *description;
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);
123 };
124
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);
129
130 char *sigrok_samplerate_string(uint64_t samplerate);
131
132 /*--- analyzer.c ------------------------------------------------------------*/
133
134 struct analyzer {
135         char *name;
136         char *filename;
137         /*
138          * TODO: Parameters? If so, configured plugins need another struct.
139          * TODO: Input and output format?
140          */
141 };
142
143 /*--- backend.c -------------------------------------------------------------*/
144
145 int sigrok_init(void);
146 void sigrok_cleanup(void);
147
148 /*--- datastore.c -----------------------------------------------------------*/
149
150 /* Size of a chunk in units */
151 #define DATASTORE_CHUNKSIZE 512000
152
153 struct datastore {
154         /* Size in bytes of the number of units stored in this datastore */
155         int ds_unitsize;
156         unsigned int num_units;
157         GSList *chunklist;
158 };
159
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);
164
165 /*--- debug.c ---------------------------------------------------------------*/
166
167 void hexdump(unsigned char *address, int length);
168
169 /*--- device.c --------------------------------------------------------------*/
170
171 /*
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.
176  */
177 struct device {
178         /* Which plugin handles this device */
179         struct device_plugin *plugin;
180         /* A plugin may handle multiple devices of the same type */
181         int plugin_index;
182         /* List of struct probe* */
183         GSList *probes;
184         /* Data acquired by this device, if any */
185         struct datastore *datastore;
186 };
187
188 struct probe {
189         int index;
190         gboolean enabled;
191         char *name;
192         char *trigger;
193 };
194
195 extern GSList *devices;
196
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);
203
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);
208
209 void device_trigger_clear(struct device *device);
210 void device_trigger_set(struct device *device, int probenum, char *trigger);
211
212 /*--- hwplugin.c ------------------------------------------------------------*/
213
214 /* Hardware plugin capabilities */
215 enum {
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
223 };
224
225 struct hwcap_option {
226         int capability;
227         int type;
228         char *description;
229         char *shortname;
230 };
231
232 struct sigrok_device_instance {
233         int index;
234         int status;
235         int instance_type;
236         char *vendor;
237         char *model;
238         char *version;
239         union {
240                 struct usb_device_instance *usb;
241                 struct serial_device_instance *serial;
242         };
243 };
244
245 /* sigrok_device_instance types */
246 enum {
247         USB_INSTANCE,
248         SERIAL_INSTANCE,
249 };
250
251 struct usb_device_instance {
252         uint8_t bus;
253         uint8_t address;
254         struct libusb_device_handle *devhdl;
255 };
256
257 struct serial_device_instance {
258         char *port;
259         int fd;
260 };
261
262 /* Device instance status */
263 enum {
264         ST_NOT_FOUND,
265         /* Found, but still booting */
266         ST_INITIALIZING,
267         /* Live, but not in use */
268         ST_INACTIVE,
269         /* Actively in use in a session */
270         ST_ACTIVE,
271 };
272
273 /*
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.
276  */
277
278 /* Device info IDs */
279 enum {
280         /* struct sigrok_device_instance for this specific device */
281         DI_INSTANCE,
282         /* The number of probes connected to this device */
283         DI_NUM_PROBES,
284         /* Samplerates supported by this device, (struct samplerates) */
285         DI_SAMPLERATES,
286         /* Types of trigger supported, out of "01crf" (char *) */
287         DI_TRIGGER_TYPES,
288         /* The currently set samplerate in Hz (uint64_t) */
289         DI_CUR_SAMPLERATE,
290 };
291
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.
295  */
296 struct samplerates {
297         uint64_t low;
298         uint64_t high;
299         uint64_t step;
300         uint64_t *list;
301 };
302
303 struct device_plugin {
304         /* plugin-specific */
305         char *name;
306         int api_version;
307         int (*init) (char *deviceinfo);
308         void (*cleanup) (void);
309
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);
319 };
320
321 struct gsource_fd {
322         GSource source;
323         GPollFD gpfd;
324         /* Not really using this */
325         GSource *timeout_source;
326 };
327
328 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
329
330 int load_hwplugins(void);
331 GSList *list_hwplugins(void);
332
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);
338
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);
343
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);
347
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);
352
353 /*--- session.c -------------------------------------------------------------*/
354
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);
360
361 struct session {
362         /* List of struct device* */
363         GSList *devices;
364         /* List of struct analyzer* */
365         GSList *analyzers;
366         /* datafeed callbacks */
367         GSList *datafeed_callbacks;
368         GTimeVal starttime;
369 };
370
371 /* Session setup */
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);
377
378 /* Protocol analyzers setup */
379 void session_pa_clear(void);
380 void session_pa_add(struct analyzer *pa);
381
382 /* Datafeed setup */
383 void session_datafeed_callback_clear(void);
384 void session_datafeed_callback_add(datafeed_callback callback);
385
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);
392
393 /*--- hwcommon.c ------------------------------------------------------------*/
394
395 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
396 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
397 int ezusb_upload_firmware(libusb_device *dev, int configuration,
398                           const char *filename);
399
400 GSList *list_serial_ports(void);
401 int serial_open(const char *pathname, int flags);
402 int serial_close(int fd);
403 void *serial_backup_params(int fd);
404 void serial_restore_params(int fd, void *backup);
405 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, int flowcontrol);
406
407 #endif