]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
b8072700 | 5 | * Copyright (C) 2012 Peter Stuge <peter@stuge.se> |
a1bb33af UH |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
a1bb33af | 22 | #include <glib.h> |
6954fdbc UH |
23 | #ifdef _WIN32 |
24 | #include <winsock2.h> | |
25 | #endif | |
c1aae900 | 26 | #include <libsigrok/libsigrok.h> |
45c59c8b | 27 | #include "libsigrok-internal.h" |
a1bb33af | 28 | |
2ad1deb8 | 29 | /** @cond PRIVATE */ |
3544f848 | 30 | #define LOG_PREFIX "backend" |
2ad1deb8 | 31 | /** @endcond */ |
3544f848 | 32 | |
5b30cca7 UH |
33 | /** |
34 | * @mainpage libsigrok API | |
35 | * | |
36 | * @section sec_intro Introduction | |
37 | * | |
38 | * The <a href="http://sigrok.org">sigrok</a> project aims at creating a | |
39 | * portable, cross-platform, Free/Libre/Open-Source signal analysis software | |
40 | * suite that supports various device types (such as logic analyzers, | |
41 | * oscilloscopes, multimeters, and more). | |
42 | * | |
43 | * <a href="http://sigrok.org/wiki/Libsigrok">libsigrok</a> is a shared | |
44 | * library written in C which provides the basic API for talking to | |
45 | * <a href="http://sigrok.org/wiki/Supported_hardware">supported hardware</a> | |
46 | * and reading/writing the acquired data into various | |
47 | * <a href="http://sigrok.org/wiki/Input_output_formats">input/output | |
48 | * file formats</a>. | |
49 | * | |
f21193fa | 50 | * @section sec_api API reference |
5b30cca7 | 51 | * |
f21193fa UH |
52 | * See the "Modules" page for an introduction to various libsigrok |
53 | * related topics and the detailed API documentation of the respective | |
54 | * functions. | |
55 | * | |
56 | * You can also browse the API documentation by file, or review all | |
57 | * data structures. | |
5b30cca7 UH |
58 | * |
59 | * @section sec_mailinglists Mailing lists | |
60 | * | |
b88c3e49 | 61 | * There is one mailing list for sigrok/libsigrok: <a href="https://lists.sourceforge.net/lists/listinfo/sigrok-devel">sigrok-devel</a>. |
5b30cca7 UH |
62 | * |
63 | * @section sec_irc IRC | |
64 | * | |
65 | * You can find the sigrok developers in the | |
66 | * <a href="irc://chat.freenode.net/sigrok">\#sigrok</a> | |
67 | * IRC channel on Freenode. | |
68 | * | |
69 | * @section sec_website Website | |
70 | * | |
71 | * <a href="http://sigrok.org/wiki/Libsigrok">sigrok.org/wiki/Libsigrok</a> | |
72 | */ | |
73 | ||
393fb9cb UH |
74 | /** |
75 | * @file | |
76 | * | |
77 | * Initializing and shutting down libsigrok. | |
78 | */ | |
79 | ||
7b870c38 UH |
80 | /** |
81 | * @defgroup grp_init Initialization | |
82 | * | |
83 | * Initializing and shutting down libsigrok. | |
84 | * | |
ad0293f1 UH |
85 | * Before using any of the libsigrok functionality (except for |
86 | * sr_log_loglevel_set()), sr_init() must be called to initialize the | |
87 | * library, which will return a struct sr_context when the initialization | |
88 | * was successful. | |
afe2f28e UH |
89 | * |
90 | * When libsigrok functionality is no longer needed, sr_exit() should be | |
91 | * called, which will (among other things) free the struct sr_context. | |
92 | * | |
93 | * Example for a minimal program using libsigrok: | |
94 | * | |
95 | * @code{.c} | |
96 | * #include <stdio.h> | |
97 | * #include <libsigrok/libsigrok.h> | |
98 | * | |
99 | * int main(int argc, char **argv) | |
100 | * { | |
101 | * int ret; | |
102 | * struct sr_context *sr_ctx; | |
103 | * | |
104 | * if ((ret = sr_init(&sr_ctx)) != SR_OK) { | |
df823ac4 | 105 | * printf("Error initializing libsigrok (%s): %s.\n", |
afe2f28e UH |
106 | * sr_strerror_name(ret), sr_strerror(ret)); |
107 | * return 1; | |
108 | * } | |
109 | * | |
110 | * // Use libsigrok functions here... | |
111 | * | |
112 | * if ((ret = sr_exit(sr_ctx)) != SR_OK) { | |
df823ac4 | 113 | * printf("Error shutting down libsigrok (%s): %s.\n", |
afe2f28e UH |
114 | * sr_strerror_name(ret), sr_strerror(ret)); |
115 | * return 1; | |
116 | * } | |
117 | * | |
118 | * return 0; | |
119 | * } | |
120 | * @endcode | |
121 | * | |
7b870c38 UH |
122 | * @{ |
123 | */ | |
124 | ||
42be2adb | 125 | SR_API GSList *sr_buildinfo_libs_get(void) |
bd7b83cf | 126 | { |
42be2adb | 127 | GSList *l = NULL, *m = NULL; |
731c01f2 | 128 | #if defined(HAVE_LIBUSB_1_0) && !defined(__FreeBSD__) |
bd7b83cf UH |
129 | const struct libusb_version *lv; |
130 | #endif | |
131 | ||
42be2adb UH |
132 | m = g_slist_append(NULL, g_strdup("glib")); |
133 | m = g_slist_append(m, g_strdup_printf("%d.%d.%d (rt: %d.%d.%d/%d:%d)", | |
bd7b83cf UH |
134 | GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION, |
135 | glib_major_version, glib_minor_version, glib_micro_version, | |
42be2adb UH |
136 | glib_binary_age, glib_interface_age)); |
137 | l = g_slist_append(l, m); | |
138 | ||
139 | m = g_slist_append(NULL, g_strdup("libzip")); | |
140 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBZIP_VERSION)); | |
141 | l = g_slist_append(l, m); | |
142 | ||
bd7b83cf | 143 | #ifdef HAVE_LIBSERIALPORT |
42be2adb UH |
144 | m = g_slist_append(NULL, g_strdup("libserialport")); |
145 | m = g_slist_append(m, g_strdup_printf("%s/%s (rt: %s/%s)", | |
bd7b83cf | 146 | SP_PACKAGE_VERSION_STRING, SP_LIB_VERSION_STRING, |
42be2adb UH |
147 | sp_get_package_version_string(), sp_get_lib_version_string())); |
148 | l = g_slist_append(l, m); | |
bd7b83cf UH |
149 | #endif |
150 | #ifdef HAVE_LIBUSB_1_0 | |
42be2adb | 151 | m = g_slist_append(NULL, g_strdup("libusb-1.0")); |
731c01f2 | 152 | #ifdef __FreeBSD__ |
42be2adb | 153 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBUSB_1_0_VERSION)); |
731c01f2 | 154 | #else |
bd7b83cf | 155 | lv = libusb_get_version(); |
e6104296 UH |
156 | m = g_slist_append(m, g_strdup_printf("%d.%d.%d.%d%s API 0x%08x", |
157 | lv->major, lv->minor, lv->micro, lv->nano, lv->rc, | |
17e0251d UH |
158 | #if defined(LIBUSB_API_VERSION) |
159 | LIBUSB_API_VERSION | |
160 | #elif defined(LIBUSBX_API_VERSION) | |
161 | LIBUSBX_API_VERSION | |
162 | #endif | |
163 | )); | |
bd7b83cf | 164 | #endif |
42be2adb | 165 | l = g_slist_append(l, m); |
731c01f2 | 166 | #endif |
4417074c GS |
167 | #ifdef HAVE_LIBHIDAPI |
168 | m = g_slist_append(NULL, g_strdup("hidapi")); | |
169 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBHIDAPI_VERSION)); | |
170 | l = g_slist_append(l, m); | |
171 | #endif | |
f085705f GS |
172 | #ifdef HAVE_LIBBLUEZ |
173 | m = g_slist_append(NULL, g_strdup("bluez")); | |
174 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBBLUEZ_VERSION)); | |
175 | l = g_slist_append(l, m); | |
176 | #endif | |
bd7b83cf | 177 | #ifdef HAVE_LIBFTDI |
42be2adb UH |
178 | m = g_slist_append(NULL, g_strdup("libftdi")); |
179 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBFTDI_VERSION)); | |
180 | l = g_slist_append(l, m); | |
bd7b83cf UH |
181 | #endif |
182 | #ifdef HAVE_LIBGPIB | |
42be2adb UH |
183 | m = g_slist_append(NULL, g_strdup("libgpib")); |
184 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBGPIB_VERSION)); | |
185 | l = g_slist_append(l, m); | |
bd7b83cf UH |
186 | #endif |
187 | #ifdef HAVE_LIBREVISA | |
42be2adb UH |
188 | m = g_slist_append(NULL, g_strdup("librevisa")); |
189 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBREVISA_VERSION)); | |
190 | l = g_slist_append(l, m); | |
bd7b83cf | 191 | #endif |
bd7b83cf | 192 | |
42be2adb UH |
193 | return l; |
194 | } | |
195 | ||
196 | SR_API char *sr_buildinfo_host_get(void) | |
197 | { | |
198 | return g_strdup_printf("%s, %s-endian", CONF_HOST, | |
bd7b83cf | 199 | #ifdef WORDS_BIGENDIAN |
42be2adb | 200 | "big" |
bd7b83cf | 201 | #else |
42be2adb | 202 | "little" |
bd7b83cf | 203 | #endif |
42be2adb UH |
204 | ); |
205 | } | |
bd7b83cf | 206 | |
42be2adb UH |
207 | SR_API char *sr_buildinfo_scpi_backends_get(void) |
208 | { | |
209 | GString *s; | |
210 | char *str; | |
211 | ||
212 | s = g_string_sized_new(200); | |
bd7b83cf UH |
213 | |
214 | g_string_append_printf(s, "TCP, "); | |
215 | #if HAVE_RPC | |
216 | g_string_append_printf(s, "RPC, "); | |
217 | #endif | |
1df81f4b | 218 | #ifdef HAVE_SERIAL_COMM |
bd7b83cf UH |
219 | g_string_append_printf(s, "serial, "); |
220 | #endif | |
221 | #ifdef HAVE_LIBREVISA | |
222 | g_string_append_printf(s, "VISA, "); | |
223 | #endif | |
224 | #ifdef HAVE_LIBGPIB | |
225 | g_string_append_printf(s, "GPIB, "); | |
226 | #endif | |
227 | #ifdef HAVE_LIBUSB_1_0 | |
228 | g_string_append_printf(s, "USBTMC, "); | |
229 | #endif | |
42be2adb UH |
230 | s->str[s->len - 2] = '\0'; |
231 | ||
232 | str = g_strdup(s->str); | |
233 | g_string_free(s, TRUE); | |
234 | ||
235 | return str; | |
236 | } | |
237 | ||
238 | static void print_versions(void) | |
239 | { | |
240 | GString *s; | |
241 | GSList *l, *l_orig, *m; | |
242 | char *str; | |
243 | const char *lib, *version; | |
244 | ||
942719b4 | 245 | sr_dbg("libsigrok %s/%s.", |
42be2adb UH |
246 | sr_package_version_string_get(), sr_lib_version_string_get()); |
247 | ||
248 | s = g_string_sized_new(200); | |
249 | g_string_append(s, "Libs: "); | |
250 | l_orig = sr_buildinfo_libs_get(); | |
251 | for (l = l_orig; l; l = l->next) { | |
252 | m = l->data; | |
253 | lib = m->data; | |
254 | version = m->next->data; | |
255 | g_string_append_printf(s, "%s %s, ", lib, version); | |
256 | g_slist_free_full(m, g_free); | |
257 | } | |
258 | g_slist_free(l_orig); | |
bd7b83cf UH |
259 | s->str[s->len - 2] = '.'; |
260 | s->str[s->len - 1] = '\0'; | |
261 | sr_dbg("%s", s->str); | |
bd7b83cf | 262 | g_string_free(s, TRUE); |
42be2adb UH |
263 | |
264 | str = sr_buildinfo_host_get(); | |
265 | sr_dbg("Host: %s.", str); | |
266 | g_free(str); | |
267 | ||
268 | str = sr_buildinfo_scpi_backends_get(); | |
269 | sr_dbg("SCPI backends: %s.", str); | |
270 | g_free(str); | |
bd7b83cf UH |
271 | } |
272 | ||
410883ba UH |
273 | static void print_resourcepaths(void) |
274 | { | |
275 | GSList *l, *l_orig; | |
276 | ||
277 | sr_dbg("Firmware search paths:"); | |
278 | l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE); | |
279 | for (l = l_orig; l; l = l->next) | |
280 | sr_dbg(" - %s", (const char *)l->data); | |
281 | g_slist_free_full(l_orig, g_free); | |
282 | } | |
283 | ||
55a6daf5 UH |
284 | /** |
285 | * Sanity-check all libsigrok drivers. | |
286 | * | |
032da34b UH |
287 | * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL. |
288 | * | |
04cb9157 MH |
289 | * @retval SR_OK All drivers are OK |
290 | * @retval SR_ERR One or more drivers have issues. | |
032da34b | 291 | * @retval SR_ERR_ARG Invalid argument. |
55a6daf5 | 292 | */ |
032da34b | 293 | static int sanity_check_all_drivers(const struct sr_context *ctx) |
55a6daf5 UH |
294 | { |
295 | int i, errors, ret = SR_OK; | |
296 | struct sr_dev_driver **drivers; | |
297 | const char *d; | |
298 | ||
032da34b UH |
299 | if (!ctx) |
300 | return SR_ERR_ARG; | |
301 | ||
55a6daf5 UH |
302 | sr_spew("Sanity-checking all drivers."); |
303 | ||
032da34b | 304 | drivers = sr_driver_list(ctx); |
55a6daf5 UH |
305 | for (i = 0; drivers[i]; i++) { |
306 | errors = 0; | |
307 | ||
308 | d = (drivers[i]->name) ? drivers[i]->name : "NULL"; | |
309 | ||
310 | if (!drivers[i]->name) { | |
311 | sr_err("No name in driver %d ('%s').", i, d); | |
312 | errors++; | |
313 | } | |
314 | if (!drivers[i]->longname) { | |
315 | sr_err("No longname in driver %d ('%s').", i, d); | |
316 | errors++; | |
317 | } | |
318 | if (drivers[i]->api_version < 1) { | |
319 | sr_err("API version in driver %d ('%s') < 1.", i, d); | |
320 | errors++; | |
321 | } | |
322 | if (!drivers[i]->init) { | |
323 | sr_err("No init in driver %d ('%s').", i, d); | |
324 | errors++; | |
325 | } | |
326 | if (!drivers[i]->cleanup) { | |
327 | sr_err("No cleanup in driver %d ('%s').", i, d); | |
328 | errors++; | |
329 | } | |
330 | if (!drivers[i]->scan) { | |
331 | sr_err("No scan in driver %d ('%s').", i, d); | |
332 | errors++; | |
333 | } | |
334 | if (!drivers[i]->dev_list) { | |
335 | sr_err("No dev_list in driver %d ('%s').", i, d); | |
336 | errors++; | |
337 | } | |
f778bf02 UH |
338 | if (!drivers[i]->dev_clear) { |
339 | sr_err("No dev_clear in driver %d ('%s').", i, d); | |
340 | errors++; | |
341 | } | |
6fab7b8f UH |
342 | /* Note: config_get() is optional. */ |
343 | if (!drivers[i]->config_set) { | |
344 | sr_err("No config_set in driver %d ('%s').", i, d); | |
345 | errors++; | |
346 | } | |
797f1a0c UH |
347 | /* Note: config_channel_set() is optional. */ |
348 | /* Note: config_commit() is optional. */ | |
6fab7b8f UH |
349 | if (!drivers[i]->config_list) { |
350 | sr_err("No config_list in driver %d ('%s').", i, d); | |
351 | errors++; | |
352 | } | |
55a6daf5 UH |
353 | if (!drivers[i]->dev_open) { |
354 | sr_err("No dev_open in driver %d ('%s').", i, d); | |
355 | errors++; | |
356 | } | |
357 | if (!drivers[i]->dev_close) { | |
358 | sr_err("No dev_close in driver %d ('%s').", i, d); | |
359 | errors++; | |
360 | } | |
55a6daf5 UH |
361 | if (!drivers[i]->dev_acquisition_start) { |
362 | sr_err("No dev_acquisition_start in driver %d ('%s').", | |
363 | i, d); | |
364 | errors++; | |
365 | } | |
366 | if (!drivers[i]->dev_acquisition_stop) { | |
367 | sr_err("No dev_acquisition_stop in driver %d ('%s').", | |
368 | i, d); | |
369 | errors++; | |
370 | } | |
371 | ||
372 | /* Note: 'priv' is allowed to be NULL. */ | |
373 | ||
374 | if (errors == 0) | |
375 | continue; | |
376 | ||
377 | ret = SR_ERR; | |
378 | } | |
379 | ||
380 | return ret; | |
381 | } | |
382 | ||
218e629f UH |
383 | /** |
384 | * Sanity-check all libsigrok input modules. | |
385 | * | |
04cb9157 MH |
386 | * @retval SR_OK All modules are OK |
387 | * @retval SR_ERR One or more modules have issues. | |
218e629f UH |
388 | */ |
389 | static int sanity_check_all_input_modules(void) | |
390 | { | |
391 | int i, errors, ret = SR_OK; | |
17bfaca6 | 392 | const struct sr_input_module **inputs; |
218e629f UH |
393 | const char *d; |
394 | ||
395 | sr_spew("Sanity-checking all input modules."); | |
396 | ||
397 | inputs = sr_input_list(); | |
398 | for (i = 0; inputs[i]; i++) { | |
399 | errors = 0; | |
400 | ||
401 | d = (inputs[i]->id) ? inputs[i]->id : "NULL"; | |
402 | ||
403 | if (!inputs[i]->id) { | |
404 | sr_err("No ID in module %d ('%s').", i, d); | |
405 | errors++; | |
406 | } | |
17bfaca6 BV |
407 | if (!inputs[i]->name) { |
408 | sr_err("No name in module %d ('%s').", i, d); | |
409 | errors++; | |
410 | } | |
411 | if (!inputs[i]->desc) { | |
218e629f UH |
412 | sr_err("No description in module %d ('%s').", i, d); |
413 | errors++; | |
414 | } | |
218e629f UH |
415 | if (!inputs[i]->init) { |
416 | sr_err("No init in module %d ('%s').", i, d); | |
417 | errors++; | |
418 | } | |
17bfaca6 BV |
419 | if (!inputs[i]->receive) { |
420 | sr_err("No receive in module %d ('%s').", i, d); | |
7066fd46 BV |
421 | errors++; |
422 | } | |
423 | if (!inputs[i]->end) { | |
424 | sr_err("No end in module %d ('%s').", i, d); | |
218e629f UH |
425 | errors++; |
426 | } | |
427 | ||
428 | if (errors == 0) | |
429 | continue; | |
430 | ||
431 | ret = SR_ERR; | |
432 | } | |
433 | ||
434 | return ret; | |
435 | } | |
436 | ||
437 | /** | |
438 | * Sanity-check all libsigrok output modules. | |
439 | * | |
04cb9157 MH |
440 | * @retval SR_OK All modules are OK |
441 | * @retval SR_ERR One or more modules have issues. | |
218e629f UH |
442 | */ |
443 | static int sanity_check_all_output_modules(void) | |
444 | { | |
445 | int i, errors, ret = SR_OK; | |
a755b0e1 | 446 | const struct sr_output_module **outputs; |
218e629f UH |
447 | const char *d; |
448 | ||
449 | sr_spew("Sanity-checking all output modules."); | |
450 | ||
451 | outputs = sr_output_list(); | |
452 | for (i = 0; outputs[i]; i++) { | |
453 | errors = 0; | |
454 | ||
455 | d = (outputs[i]->id) ? outputs[i]->id : "NULL"; | |
456 | ||
457 | if (!outputs[i]->id) { | |
458 | sr_err("No ID in module %d ('%s').", i, d); | |
459 | errors++; | |
460 | } | |
a755b0e1 BV |
461 | if (!outputs[i]->name) { |
462 | sr_err("No name in module %d ('%s').", i, d); | |
463 | errors++; | |
464 | } | |
465 | if (!outputs[i]->desc) { | |
44559b2c | 466 | sr_err("No description in module '%s'.", d); |
218e629f UH |
467 | errors++; |
468 | } | |
44559b2c BV |
469 | if (!outputs[i]->receive) { |
470 | sr_err("No receive in module '%s'.", d); | |
218e629f UH |
471 | errors++; |
472 | } | |
473 | ||
218e629f UH |
474 | if (errors == 0) |
475 | continue; | |
476 | ||
477 | ret = SR_ERR; | |
478 | } | |
479 | ||
480 | return ret; | |
481 | } | |
482 | ||
187cfc60 UH |
483 | /** |
484 | * Sanity-check all libsigrok transform modules. | |
485 | * | |
486 | * @retval SR_OK All modules are OK | |
487 | * @retval SR_ERR One or more modules have issues. | |
488 | */ | |
489 | static int sanity_check_all_transform_modules(void) | |
490 | { | |
491 | int i, errors, ret = SR_OK; | |
492 | const struct sr_transform_module **transforms; | |
493 | const char *d; | |
494 | ||
495 | sr_spew("Sanity-checking all transform modules."); | |
496 | ||
497 | transforms = sr_transform_list(); | |
498 | for (i = 0; transforms[i]; i++) { | |
499 | errors = 0; | |
500 | ||
501 | d = (transforms[i]->id) ? transforms[i]->id : "NULL"; | |
502 | ||
503 | if (!transforms[i]->id) { | |
504 | sr_err("No ID in module %d ('%s').", i, d); | |
505 | errors++; | |
506 | } | |
507 | if (!transforms[i]->name) { | |
508 | sr_err("No name in module %d ('%s').", i, d); | |
509 | errors++; | |
510 | } | |
511 | if (!transforms[i]->desc) { | |
512 | sr_err("No description in module '%s'.", d); | |
513 | errors++; | |
514 | } | |
515 | /* Note: options() is optional. */ | |
516 | /* Note: init() is optional. */ | |
517 | if (!transforms[i]->receive) { | |
518 | sr_err("No receive in module '%s'.", d); | |
519 | errors++; | |
520 | } | |
521 | /* Note: cleanup() is optional. */ | |
522 | ||
523 | if (errors == 0) | |
524 | continue; | |
525 | ||
526 | ret = SR_ERR; | |
527 | } | |
528 | ||
529 | return ret; | |
530 | } | |
531 | ||
cd009d55 UH |
532 | /** |
533 | * Initialize libsigrok. | |
534 | * | |
c46762a2 UH |
535 | * This function must be called before any other libsigrok function. |
536 | * | |
537 | * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL. | |
538 | * This will be a pointer to a newly allocated libsigrok context | |
539 | * object upon success, and is undefined upon errors. | |
540 | * | |
541 | * @return SR_OK upon success, a (negative) error code otherwise. Upon errors | |
542 | * the 'ctx' pointer is undefined and should not be used. Upon success, | |
543 | * the context will be free'd by sr_exit() as part of the libsigrok | |
544 | * shutdown. | |
9fb5f2df | 545 | * |
53f05fa8 | 546 | * @since 0.2.0 |
cd009d55 | 547 | */ |
b8072700 | 548 | SR_API int sr_init(struct sr_context **ctx) |
a1bb33af | 549 | { |
b8072700 PS |
550 | int ret = SR_ERR; |
551 | struct sr_context *context; | |
fa69e191 ML |
552 | #ifdef _WIN32 |
553 | WSADATA wsadata; | |
554 | #endif | |
b8072700 | 555 | |
bd7b83cf UH |
556 | print_versions(); |
557 | ||
410883ba UH |
558 | print_resourcepaths(); |
559 | ||
c46762a2 UH |
560 | if (!ctx) { |
561 | sr_err("%s(): libsigrok context was NULL.", __func__); | |
562 | return SR_ERR; | |
563 | } | |
564 | ||
032da34b UH |
565 | context = g_malloc0(sizeof(struct sr_context)); |
566 | ||
5d8b3913 | 567 | sr_drivers_init(context); |
032da34b UH |
568 | |
569 | if (sanity_check_all_drivers(context) < 0) { | |
55a6daf5 | 570 | sr_err("Internal driver error(s), aborting."); |
928560e6 | 571 | goto done; |
55a6daf5 UH |
572 | } |
573 | ||
218e629f UH |
574 | if (sanity_check_all_input_modules() < 0) { |
575 | sr_err("Internal input module error(s), aborting."); | |
928560e6 | 576 | goto done; |
218e629f UH |
577 | } |
578 | ||
579 | if (sanity_check_all_output_modules() < 0) { | |
580 | sr_err("Internal output module error(s), aborting."); | |
928560e6 | 581 | goto done; |
218e629f UH |
582 | } |
583 | ||
187cfc60 UH |
584 | if (sanity_check_all_transform_modules() < 0) { |
585 | sr_err("Internal transform module error(s), aborting."); | |
928560e6 | 586 | goto done; |
187cfc60 UH |
587 | } |
588 | ||
fa69e191 ML |
589 | #ifdef _WIN32 |
590 | if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) { | |
591 | sr_err("WSAStartup failed with error code %d.", ret); | |
592 | ret = SR_ERR; | |
593 | goto done; | |
594 | } | |
595 | #endif | |
596 | ||
785b9ff2 PS |
597 | #ifdef HAVE_LIBUSB_1_0 |
598 | ret = libusb_init(&context->libusb_ctx); | |
8e2d43cc | 599 | if (LIBUSB_SUCCESS != ret) { |
9d122af8 | 600 | sr_err("libusb_init() returned %s.", libusb_error_name(ret)); |
63c07e48 | 601 | ret = SR_ERR; |
785b9ff2 PS |
602 | goto done; |
603 | } | |
4417074c GS |
604 | #endif |
605 | #ifdef HAVE_LIBHIDAPI | |
606 | /* | |
607 | * According to <hidapi.h>, the hid_init() routine just returns | |
608 | * zero or non-zero, and hid_error() appears to relate to calls | |
609 | * for a specific device after hid_open(). Which means that there | |
610 | * is no more detailled information available beyond success/fail | |
611 | * at this point in time. | |
612 | */ | |
613 | if (hid_init() != 0) { | |
614 | sr_err("HIDAPI hid_init() failed."); | |
615 | ret = SR_ERR; | |
616 | goto done; | |
617 | } | |
785b9ff2 | 618 | #endif |
bee24666 | 619 | sr_resource_set_hooks(context, NULL, NULL, NULL, NULL); |
785b9ff2 | 620 | |
b8072700 | 621 | *ctx = context; |
123d97b1 | 622 | context = NULL; |
b8072700 PS |
623 | ret = SR_OK; |
624 | ||
625 | done: | |
b1f83103 | 626 | g_free(context); |
b8072700 | 627 | return ret; |
a1bb33af UH |
628 | } |
629 | ||
cd009d55 UH |
630 | /** |
631 | * Shutdown libsigrok. | |
632 | * | |
c46762a2 UH |
633 | * @param ctx Pointer to a libsigrok context struct. Must not be NULL. |
634 | * | |
04cb9157 MH |
635 | * @retval SR_OK Success |
636 | * @retval other Error code SR_ERR, ... | |
9fb5f2df | 637 | * |
53f05fa8 | 638 | * @since 0.2.0 |
cd009d55 | 639 | */ |
b8072700 | 640 | SR_API int sr_exit(struct sr_context *ctx) |
a1bb33af | 641 | { |
c46762a2 UH |
642 | if (!ctx) { |
643 | sr_err("%s(): libsigrok context was NULL.", __func__); | |
644 | return SR_ERR; | |
645 | } | |
646 | ||
032da34b | 647 | sr_hw_cleanup_all(ctx); |
cd009d55 | 648 | |
fa69e191 ML |
649 | #ifdef _WIN32 |
650 | WSACleanup(); | |
651 | #endif | |
652 | ||
4417074c GS |
653 | #ifdef HAVE_LIBHIDAPI |
654 | hid_exit(); | |
655 | #endif | |
785b9ff2 PS |
656 | #ifdef HAVE_LIBUSB_1_0 |
657 | libusb_exit(ctx->libusb_ctx); | |
658 | #endif | |
659 | ||
032da34b | 660 | g_free(sr_driver_list(ctx)); |
b8072700 PS |
661 | g_free(ctx); |
662 | ||
cd009d55 | 663 | return SR_OK; |
a1bb33af | 664 | } |
7b870c38 UH |
665 | |
666 | /** @} */ |