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