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