]> sigrok.org Git - libsigrok.git/blob - sigrok.h
Start of code base layout restructuring.
[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 /* Returned status/error codes */
31 #define SIGROK_STATUS_DISABLED          0
32 #define SIGROK_OK                       1
33 #define SIGROK_NOK                      2
34 #define SIGROK_ERR_BADVALUE             20
35
36 /* Handy little macros */
37 #define KHZ(n) (n * 1000)
38 #define MHZ(n) (n * 1000000)
39 #define GHZ(n) (n * 1000000000)
40
41 /* Data types, used by hardware plugins for set_configuration() */
42 enum {
43         T_UINT64,
44         T_CHAR,
45 };
46
47 enum {
48         PROTO_RAW,
49 };
50
51 /* (Unused) protocol decoder stack entry */
52 struct protocol {
53         char *name;
54         int id;
55         int stackindex;
56 };
57
58 /*
59  * datafeed
60  */
61
62 /* datafeed_packet.type values */
63 enum {
64         DF_HEADER,
65         DF_END,
66         DF_TRIGGER,
67         DF_LOGIC8,
68         DF_LOGIC16,
69         DF_LOGIC24,
70         DF_LOGIC32,
71         DF_LOGIC48,
72         DF_LOGIC64,
73 };
74
75 struct datafeed_packet {
76         uint16_t type;
77         uint16_t length;
78         void *payload;
79 };
80
81 struct datafeed_header {
82         int feed_version;
83         struct timeval starttime;
84         uint64_t rate;
85         int protocol_id;
86         int num_probes;
87 };
88
89 /*
90  * output
91  */
92 struct output {
93         struct output_format *format;
94         struct device *device;
95         char *param;
96         void *internal;
97 };
98
99 struct output_format {
100         char *extension;
101         char *description;
102         void (*init) (struct output *o);
103         int (*data) (struct output *o, char *data_in, uint64_t length_in,
104                      char **data_out, uint64_t *length_out);
105         int (*event) (struct output *o, int event_type, char **data_out,
106                       uint64_t *length_out);
107 };
108
109 struct output_format **output_list(void);
110 int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
111                   char *data_in, uint64_t length_in, char **data_out,
112                   uint64_t *length_out);
113
114 /*--- analyzer.c ------------------------------------------------------------*/
115
116 struct analyzer {
117         char *name;
118         char *filename;
119         /*
120          * TODO: Parameters? If so, configured plugins need another struct.
121          * TODO: Input and output format?
122          */
123 };
124
125 /*--- backend.c -------------------------------------------------------------*/
126
127 int sigrok_init(void);
128 void sigrok_cleanup(void);
129
130 /*--- datastore.c -----------------------------------------------------------*/
131
132 /* Size of a chunk in units */
133 #define DATASTORE_CHUNKSIZE 512000
134
135 struct datastore {
136         /* Size in bytes of the number of units stored in this datastore */
137         int ds_unitsize;
138         unsigned int num_units;
139         GSList *chunklist;
140 };
141
142 struct datastore *datastore_new(int unitsize);
143 void datastore_destroy(struct datastore *ds);
144 void datastore_put(struct datastore *ds, void *data, unsigned int length,
145                    int in_unitsize, int *probelist);
146
147 /*--- debug.c ---------------------------------------------------------------*/
148
149 void hexdump(unsigned char *address, int length);
150
151 /*--- device.c --------------------------------------------------------------*/
152
153 /*
154  * This represents a generic device connected to the system.
155  * For device-specific information, ask the plugin. The plugin_index refers
156  * to the device index within that plugin; it may be handling more than one
157  * device. All relevant plugin calls take a device_index parameter for this.
158  */
159 struct device {
160         /* Which plugin handles this device */
161         struct device_plugin *plugin;
162         /* A plugin may handle multiple devices of the same type */
163         int plugin_index;
164         /* List of struct probe* */
165         GSList *probes;
166         /* Data acquired by this device, if any */
167         struct datastore *datastore;
168 };
169
170 struct probe {
171         int index;
172         gboolean enabled;
173         char *name;
174         char *trigger;
175 };
176
177 extern GSList *devices;
178
179 void device_scan(void);
180 void device_close_all(void);
181 GSList *device_list(void);
182 struct device *device_new(struct device_plugin *plugin, int plugin_index);
183 void device_clear(struct device *device);
184 void device_destroy(struct device *dev);
185
186 void device_probe_clear(struct device *device, int probenum);
187 void device_probe_add(struct device *device, char *name);
188 struct probe *probe_find(struct device *device, int probenum);
189 void device_probe_name(struct device *device, int probenum, char *name);
190
191 void device_trigger_clear(struct device *device);
192 void device_trigger_set(struct device *device, int probenum, char *trigger);
193
194 /*--- hwplugin.c ------------------------------------------------------------*/
195
196 /* Hardware plugin capabilities */
197 enum {
198         HWCAP_DUMMY,            // used to terminate lists
199         HWCAP_LOGIC_ANALYZER,
200         HWCAP_SAMPLERATE,       // change sample rate
201         HWCAP_PROBECONFIG,      // configure probe mask
202         HWCAP_CAPTURE_RATIO,    // set pre-trigger / post-trigger ratio
203         HWCAP_LIMIT_MSEC,       // set a time limit for sample acquisition
204         HWCAP_LIMIT_SAMPLES,    // set a limit on number of samples
205 };
206
207 struct hwcap_option {
208         int capability;
209         int type;
210         char *description;
211         char *shortname;
212 };
213
214 struct sigrok_device_instance {
215         int index;
216         int status;
217         int instance_type;
218         char *vendor;
219         char *model;
220         char *version;
221         union {
222                 struct usb_device_instance *usb;
223                 struct serial_device_instance *serial;
224         };
225 };
226
227 /* sigrok_device_instance types */
228 enum {
229         USB_INSTANCE,
230         SERIAL_INSTANCE,
231 };
232
233 struct usb_device_instance {
234         uint8_t bus;
235         uint8_t address;
236         struct libusb_device_handle *devhdl;
237 };
238
239 struct serial_device_instance {
240         char *port;
241         int fd;
242 };
243
244 /* Device instance status */
245 enum {
246         ST_NOT_FOUND,
247         /* Found, but still booting */
248         ST_INITIALIZING,
249         /* Live, but not in use */
250         ST_INACTIVE,
251         /* Actively in use in a session */
252         ST_ACTIVE,
253 };
254
255 /*
256  * TODO: This sucks, you just kinda have to "know" the returned type.
257  * TODO: Need a DI to return the number of trigger stages supported.
258  */
259
260 /* Device info IDs */
261 enum {
262         /* struct sigrok_device_instance for this specific device */
263         DI_INSTANCE,
264         /* The number of probes connected to this device */
265         DI_NUM_PROBES,
266         /* Sample rates supported by this device, (struct samplerates) */
267         DI_SAMPLERATES,
268         /* Types of trigger supported, out of "01crf" (char *) */
269         DI_TRIGGER_TYPES,
270         /* The currently set sample rate in Hz (uint64_t) */
271         DI_CUR_SAMPLE_RATE,
272 };
273
274 /* a device supports either a range of samplerates with steps of a given
275  * granularity, or is limited to a set of defined samplerates. use either
276  * step or list, but not both.
277  */
278 struct samplerates {
279         uint64_t low;
280         uint64_t high;
281         uint64_t step;
282         uint64_t *list;
283 };
284
285 struct device_plugin {
286         /* plugin-specific */
287         char *name;
288         int api_version;
289         int (*init) (char *deviceinfo);
290         void (*cleanup) (void);
291
292         /* device-specific */
293         int (*open) (int device_index);
294         void (*close) (int device_index);
295         void *(*get_device_info) (int device_index, int device_info_id);
296         int (*get_status) (int device_index);
297         int *(*get_capabilities) (void);
298         int (*set_configuration) (int device_index, int capability, void *value);
299         int (*start_acquisition) (int device_index, gpointer session_device_id);
300         void (*stop_acquisition) (int device_index, gpointer session_device_id);
301 };
302
303 struct gsource_fd {
304         GSource source;
305         GPollFD gpfd;
306         /* Not really using this */
307         GSource *timeout_source;
308 };
309
310 typedef int (*receive_data_callback) (int fd, int revents, void *user_data);
311
312 int load_hwplugins(void);
313 GSList *list_hwplugins(void);
314
315 /* Generic device instances */
316 struct sigrok_device_instance *sigrok_device_instance_new(int index,
317         int status, char *vendor, char *model, char *version);
318 struct sigrok_device_instance *get_sigrok_device_instance(GSList *device_instances, int device_index);
319 void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
320
321 /* USB-specific instances */
322 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
323                 uint8_t address, struct libusb_device_handle *hdl);
324 void usb_device_instance_free(struct usb_device_instance *usb);
325
326 /* Serial-specific instances */
327 struct serial_device_instance *serial_device_instance_new(char *port, int fd);
328 void serial_device_instance_free(struct serial_device_instance *serial);
329
330 int find_hwcap(int *capabilities, int hwcap);
331 struct hwcap_option *find_hwcap_option(int hwcap);
332 void source_remove(int fd);
333 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb, void *user_data);
334
335 /*--- session.c -------------------------------------------------------------*/
336
337 typedef void (*source_callback_remove) (int fd);
338 typedef void (*source_callback_add) (int fd, int events, int timeout,
339                 receive_data_callback callback, void *user_data);
340 typedef void (*datafeed_callback) (struct device *device,
341                                  struct datafeed_packet *packet);
342
343 struct session {
344         /* List of struct device* */
345         GSList *devices;
346         /* List of struct analyzer* */
347         GSList *analyzers;
348         /* datafeed callbacks */
349         GSList *datafeed_callbacks;
350         GTimeVal starttime;
351 };
352
353 /* Session setup */
354 struct session *session_load(char *filename);
355 struct session *session_new(void);
356 void session_destroy(void);
357 void session_device_clear(void);
358 int session_device_add(struct device *device);
359
360 /* Protocol analyzers setup */
361 void session_pa_clear(void);
362 void session_pa_add(struct analyzer *pa);
363
364 /* Datafeed setup */
365 void session_datafeed_callback_clear(void);
366 void session_datafeed_callback_add(datafeed_callback callback);
367
368 /* Session control */
369 int session_start(void);
370 void session_stop(void);
371 void session_bus(struct device *device, struct datafeed_packet *packet);
372 void make_metadata(char *filename);
373 int session_save(char *filename);
374
375 /*--- hwcommon.c ------------------------------------------------------------*/
376
377 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
378 int ezusb_install_firmware(libusb_device_handle *hdl, char *filename);
379
380 GSList *list_serial_ports(void);
381
382 #endif