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