]>
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 |
bd7b83cf | 167 | #ifdef HAVE_LIBFTDI |
42be2adb UH |
168 | m = g_slist_append(NULL, g_strdup("libftdi")); |
169 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBFTDI_VERSION)); | |
170 | l = g_slist_append(l, m); | |
bd7b83cf UH |
171 | #endif |
172 | #ifdef HAVE_LIBGPIB | |
42be2adb UH |
173 | m = g_slist_append(NULL, g_strdup("libgpib")); |
174 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBGPIB_VERSION)); | |
175 | l = g_slist_append(l, m); | |
bd7b83cf UH |
176 | #endif |
177 | #ifdef HAVE_LIBREVISA | |
42be2adb UH |
178 | m = g_slist_append(NULL, g_strdup("librevisa")); |
179 | m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBREVISA_VERSION)); | |
180 | l = g_slist_append(l, m); | |
bd7b83cf | 181 | #endif |
bd7b83cf | 182 | |
42be2adb UH |
183 | return l; |
184 | } | |
185 | ||
186 | SR_API char *sr_buildinfo_host_get(void) | |
187 | { | |
188 | return g_strdup_printf("%s, %s-endian", CONF_HOST, | |
bd7b83cf | 189 | #ifdef WORDS_BIGENDIAN |
42be2adb | 190 | "big" |
bd7b83cf | 191 | #else |
42be2adb | 192 | "little" |
bd7b83cf | 193 | #endif |
42be2adb UH |
194 | ); |
195 | } | |
bd7b83cf | 196 | |
42be2adb UH |
197 | SR_API char *sr_buildinfo_scpi_backends_get(void) |
198 | { | |
199 | GString *s; | |
200 | char *str; | |
201 | ||
202 | s = g_string_sized_new(200); | |
bd7b83cf UH |
203 | |
204 | g_string_append_printf(s, "TCP, "); | |
205 | #if HAVE_RPC | |
206 | g_string_append_printf(s, "RPC, "); | |
207 | #endif | |
208 | #ifdef HAVE_LIBSERIALPORT | |
209 | g_string_append_printf(s, "serial, "); | |
210 | #endif | |
211 | #ifdef HAVE_LIBREVISA | |
212 | g_string_append_printf(s, "VISA, "); | |
213 | #endif | |
214 | #ifdef HAVE_LIBGPIB | |
215 | g_string_append_printf(s, "GPIB, "); | |
216 | #endif | |
217 | #ifdef HAVE_LIBUSB_1_0 | |
218 | g_string_append_printf(s, "USBTMC, "); | |
219 | #endif | |
42be2adb UH |
220 | s->str[s->len - 2] = '\0'; |
221 | ||
222 | str = g_strdup(s->str); | |
223 | g_string_free(s, TRUE); | |
224 | ||
225 | return str; | |
226 | } | |
227 | ||
228 | static void print_versions(void) | |
229 | { | |
230 | GString *s; | |
231 | GSList *l, *l_orig, *m; | |
232 | char *str; | |
233 | const char *lib, *version; | |
234 | ||
235 | sr_dbg("libsigrok %s/%s (rt: %s/%s).", | |
236 | SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING, | |
237 | sr_package_version_string_get(), sr_lib_version_string_get()); | |
238 | ||
239 | s = g_string_sized_new(200); | |
240 | g_string_append(s, "Libs: "); | |
241 | l_orig = sr_buildinfo_libs_get(); | |
242 | for (l = l_orig; l; l = l->next) { | |
243 | m = l->data; | |
244 | lib = m->data; | |
245 | version = m->next->data; | |
246 | g_string_append_printf(s, "%s %s, ", lib, version); | |
247 | g_slist_free_full(m, g_free); | |
248 | } | |
249 | g_slist_free(l_orig); | |
bd7b83cf UH |
250 | s->str[s->len - 2] = '.'; |
251 | s->str[s->len - 1] = '\0'; | |
252 | sr_dbg("%s", s->str); | |
bd7b83cf | 253 | g_string_free(s, TRUE); |
42be2adb UH |
254 | |
255 | str = sr_buildinfo_host_get(); | |
256 | sr_dbg("Host: %s.", str); | |
257 | g_free(str); | |
258 | ||
259 | str = sr_buildinfo_scpi_backends_get(); | |
260 | sr_dbg("SCPI backends: %s.", str); | |
261 | g_free(str); | |
bd7b83cf UH |
262 | } |
263 | ||
410883ba UH |
264 | static void print_resourcepaths(void) |
265 | { | |
266 | GSList *l, *l_orig; | |
267 | ||
268 | sr_dbg("Firmware search paths:"); | |
269 | l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE); | |
270 | for (l = l_orig; l; l = l->next) | |
271 | sr_dbg(" - %s", (const char *)l->data); | |
272 | g_slist_free_full(l_orig, g_free); | |
273 | } | |
274 | ||
55a6daf5 UH |
275 | /** |
276 | * Sanity-check all libsigrok drivers. | |
277 | * | |
032da34b UH |
278 | * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL. |
279 | * | |
04cb9157 MH |
280 | * @retval SR_OK All drivers are OK |
281 | * @retval SR_ERR One or more drivers have issues. | |
032da34b | 282 | * @retval SR_ERR_ARG Invalid argument. |
55a6daf5 | 283 | */ |
032da34b | 284 | static int sanity_check_all_drivers(const struct sr_context *ctx) |
55a6daf5 UH |
285 | { |
286 | int i, errors, ret = SR_OK; | |
287 | struct sr_dev_driver **drivers; | |
288 | const char *d; | |
289 | ||
032da34b UH |
290 | if (!ctx) |
291 | return SR_ERR_ARG; | |
292 | ||
55a6daf5 UH |
293 | sr_spew("Sanity-checking all drivers."); |
294 | ||
032da34b | 295 | drivers = sr_driver_list(ctx); |
55a6daf5 UH |
296 | for (i = 0; drivers[i]; i++) { |
297 | errors = 0; | |
298 | ||
299 | d = (drivers[i]->name) ? drivers[i]->name : "NULL"; | |
300 | ||
301 | if (!drivers[i]->name) { | |
302 | sr_err("No name in driver %d ('%s').", i, d); | |
303 | errors++; | |
304 | } | |
305 | if (!drivers[i]->longname) { | |
306 | sr_err("No longname in driver %d ('%s').", i, d); | |
307 | errors++; | |
308 | } | |
309 | if (drivers[i]->api_version < 1) { | |
310 | sr_err("API version in driver %d ('%s') < 1.", i, d); | |
311 | errors++; | |
312 | } | |
313 | if (!drivers[i]->init) { | |
314 | sr_err("No init in driver %d ('%s').", i, d); | |
315 | errors++; | |
316 | } | |
317 | if (!drivers[i]->cleanup) { | |
318 | sr_err("No cleanup in driver %d ('%s').", i, d); | |
319 | errors++; | |
320 | } | |
321 | if (!drivers[i]->scan) { | |
322 | sr_err("No scan in driver %d ('%s').", i, d); | |
323 | errors++; | |
324 | } | |
325 | if (!drivers[i]->dev_list) { | |
326 | sr_err("No dev_list in driver %d ('%s').", i, d); | |
327 | errors++; | |
328 | } | |
f778bf02 UH |
329 | if (!drivers[i]->dev_clear) { |
330 | sr_err("No dev_clear in driver %d ('%s').", i, d); | |
331 | errors++; | |
332 | } | |
6fab7b8f UH |
333 | /* Note: config_get() is optional. */ |
334 | if (!drivers[i]->config_set) { | |
335 | sr_err("No config_set in driver %d ('%s').", i, d); | |
336 | errors++; | |
337 | } | |
797f1a0c UH |
338 | /* Note: config_channel_set() is optional. */ |
339 | /* Note: config_commit() is optional. */ | |
6fab7b8f UH |
340 | if (!drivers[i]->config_list) { |
341 | sr_err("No config_list in driver %d ('%s').", i, d); | |
342 | errors++; | |
343 | } | |
55a6daf5 UH |
344 | if (!drivers[i]->dev_open) { |
345 | sr_err("No dev_open in driver %d ('%s').", i, d); | |
346 | errors++; | |
347 | } | |
348 | if (!drivers[i]->dev_close) { | |
349 | sr_err("No dev_close in driver %d ('%s').", i, d); | |
350 | errors++; | |
351 | } | |
55a6daf5 UH |
352 | if (!drivers[i]->dev_acquisition_start) { |
353 | sr_err("No dev_acquisition_start in driver %d ('%s').", | |
354 | i, d); | |
355 | errors++; | |
356 | } | |
357 | if (!drivers[i]->dev_acquisition_stop) { | |
358 | sr_err("No dev_acquisition_stop in driver %d ('%s').", | |
359 | i, d); | |
360 | errors++; | |
361 | } | |
362 | ||
363 | /* Note: 'priv' is allowed to be NULL. */ | |
364 | ||
365 | if (errors == 0) | |
366 | continue; | |
367 | ||
368 | ret = SR_ERR; | |
369 | } | |
370 | ||
371 | return ret; | |
372 | } | |
373 | ||
218e629f UH |
374 | /** |
375 | * Sanity-check all libsigrok input modules. | |
376 | * | |
04cb9157 MH |
377 | * @retval SR_OK All modules are OK |
378 | * @retval SR_ERR One or more modules have issues. | |
218e629f UH |
379 | */ |
380 | static int sanity_check_all_input_modules(void) | |
381 | { | |
382 | int i, errors, ret = SR_OK; | |
17bfaca6 | 383 | const struct sr_input_module **inputs; |
218e629f UH |
384 | const char *d; |
385 | ||
386 | sr_spew("Sanity-checking all input modules."); | |
387 | ||
388 | inputs = sr_input_list(); | |
389 | for (i = 0; inputs[i]; i++) { | |
390 | errors = 0; | |
391 | ||
392 | d = (inputs[i]->id) ? inputs[i]->id : "NULL"; | |
393 | ||
394 | if (!inputs[i]->id) { | |
395 | sr_err("No ID in module %d ('%s').", i, d); | |
396 | errors++; | |
397 | } | |
17bfaca6 BV |
398 | if (!inputs[i]->name) { |
399 | sr_err("No name in module %d ('%s').", i, d); | |
400 | errors++; | |
401 | } | |
402 | if (!inputs[i]->desc) { | |
218e629f UH |
403 | sr_err("No description in module %d ('%s').", i, d); |
404 | errors++; | |
405 | } | |
218e629f UH |
406 | if (!inputs[i]->init) { |
407 | sr_err("No init in module %d ('%s').", i, d); | |
408 | errors++; | |
409 | } | |
17bfaca6 BV |
410 | if (!inputs[i]->receive) { |
411 | sr_err("No receive in module %d ('%s').", i, d); | |
7066fd46 BV |
412 | errors++; |
413 | } | |
414 | if (!inputs[i]->end) { | |
415 | sr_err("No end in module %d ('%s').", i, d); | |
218e629f UH |
416 | errors++; |
417 | } | |
418 | ||
419 | if (errors == 0) | |
420 | continue; | |
421 | ||
422 | ret = SR_ERR; | |
423 | } | |
424 | ||
425 | return ret; | |
426 | } | |
427 | ||
428 | /** | |
429 | * Sanity-check all libsigrok output modules. | |
430 | * | |
04cb9157 MH |
431 | * @retval SR_OK All modules are OK |
432 | * @retval SR_ERR One or more modules have issues. | |
218e629f UH |
433 | */ |
434 | static int sanity_check_all_output_modules(void) | |
435 | { | |
436 | int i, errors, ret = SR_OK; | |
a755b0e1 | 437 | const struct sr_output_module **outputs; |
218e629f UH |
438 | const char *d; |
439 | ||
440 | sr_spew("Sanity-checking all output modules."); | |
441 | ||
442 | outputs = sr_output_list(); | |
443 | for (i = 0; outputs[i]; i++) { | |
444 | errors = 0; | |
445 | ||
446 | d = (outputs[i]->id) ? outputs[i]->id : "NULL"; | |
447 | ||
448 | if (!outputs[i]->id) { | |
449 | sr_err("No ID in module %d ('%s').", i, d); | |
450 | errors++; | |
451 | } | |
a755b0e1 BV |
452 | if (!outputs[i]->name) { |
453 | sr_err("No name in module %d ('%s').", i, d); | |
454 | errors++; | |
455 | } | |
456 | if (!outputs[i]->desc) { | |
44559b2c | 457 | sr_err("No description in module '%s'.", d); |
218e629f UH |
458 | errors++; |
459 | } | |
44559b2c BV |
460 | if (!outputs[i]->receive) { |
461 | sr_err("No receive in module '%s'.", d); | |
218e629f UH |
462 | errors++; |
463 | } | |
464 | ||
218e629f UH |
465 | if (errors == 0) |
466 | continue; | |
467 | ||
468 | ret = SR_ERR; | |
469 | } | |
470 | ||
471 | return ret; | |
472 | } | |
473 | ||
187cfc60 UH |
474 | /** |
475 | * Sanity-check all libsigrok transform modules. | |
476 | * | |
477 | * @retval SR_OK All modules are OK | |
478 | * @retval SR_ERR One or more modules have issues. | |
479 | */ | |
480 | static int sanity_check_all_transform_modules(void) | |
481 | { | |
482 | int i, errors, ret = SR_OK; | |
483 | const struct sr_transform_module **transforms; | |
484 | const char *d; | |
485 | ||
486 | sr_spew("Sanity-checking all transform modules."); | |
487 | ||
488 | transforms = sr_transform_list(); | |
489 | for (i = 0; transforms[i]; i++) { | |
490 | errors = 0; | |
491 | ||
492 | d = (transforms[i]->id) ? transforms[i]->id : "NULL"; | |
493 | ||
494 | if (!transforms[i]->id) { | |
495 | sr_err("No ID in module %d ('%s').", i, d); | |
496 | errors++; | |
497 | } | |
498 | if (!transforms[i]->name) { | |
499 | sr_err("No name in module %d ('%s').", i, d); | |
500 | errors++; | |
501 | } | |
502 | if (!transforms[i]->desc) { | |
503 | sr_err("No description in module '%s'.", d); | |
504 | errors++; | |
505 | } | |
506 | /* Note: options() is optional. */ | |
507 | /* Note: init() is optional. */ | |
508 | if (!transforms[i]->receive) { | |
509 | sr_err("No receive in module '%s'.", d); | |
510 | errors++; | |
511 | } | |
512 | /* Note: cleanup() is optional. */ | |
513 | ||
514 | if (errors == 0) | |
515 | continue; | |
516 | ||
517 | ret = SR_ERR; | |
518 | } | |
519 | ||
520 | return ret; | |
521 | } | |
522 | ||
cd009d55 UH |
523 | /** |
524 | * Initialize libsigrok. | |
525 | * | |
c46762a2 UH |
526 | * This function must be called before any other libsigrok function. |
527 | * | |
528 | * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL. | |
529 | * This will be a pointer to a newly allocated libsigrok context | |
530 | * object upon success, and is undefined upon errors. | |
531 | * | |
532 | * @return SR_OK upon success, a (negative) error code otherwise. Upon errors | |
533 | * the 'ctx' pointer is undefined and should not be used. Upon success, | |
534 | * the context will be free'd by sr_exit() as part of the libsigrok | |
535 | * shutdown. | |
9fb5f2df | 536 | * |
53f05fa8 | 537 | * @since 0.2.0 |
cd009d55 | 538 | */ |
b8072700 | 539 | SR_API int sr_init(struct sr_context **ctx) |
a1bb33af | 540 | { |
b8072700 PS |
541 | int ret = SR_ERR; |
542 | struct sr_context *context; | |
fa69e191 ML |
543 | #ifdef _WIN32 |
544 | WSADATA wsadata; | |
545 | #endif | |
b8072700 | 546 | |
bd7b83cf UH |
547 | print_versions(); |
548 | ||
410883ba UH |
549 | print_resourcepaths(); |
550 | ||
c46762a2 UH |
551 | if (!ctx) { |
552 | sr_err("%s(): libsigrok context was NULL.", __func__); | |
553 | return SR_ERR; | |
554 | } | |
555 | ||
032da34b UH |
556 | context = g_malloc0(sizeof(struct sr_context)); |
557 | ||
5d8b3913 | 558 | sr_drivers_init(context); |
032da34b UH |
559 | |
560 | if (sanity_check_all_drivers(context) < 0) { | |
55a6daf5 | 561 | sr_err("Internal driver error(s), aborting."); |
928560e6 | 562 | goto done; |
55a6daf5 UH |
563 | } |
564 | ||
218e629f UH |
565 | if (sanity_check_all_input_modules() < 0) { |
566 | sr_err("Internal input module error(s), aborting."); | |
928560e6 | 567 | goto done; |
218e629f UH |
568 | } |
569 | ||
570 | if (sanity_check_all_output_modules() < 0) { | |
571 | sr_err("Internal output module error(s), aborting."); | |
928560e6 | 572 | goto done; |
218e629f UH |
573 | } |
574 | ||
187cfc60 UH |
575 | if (sanity_check_all_transform_modules() < 0) { |
576 | sr_err("Internal transform module error(s), aborting."); | |
928560e6 | 577 | goto done; |
187cfc60 UH |
578 | } |
579 | ||
fa69e191 ML |
580 | #ifdef _WIN32 |
581 | if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) { | |
582 | sr_err("WSAStartup failed with error code %d.", ret); | |
583 | ret = SR_ERR; | |
584 | goto done; | |
585 | } | |
586 | #endif | |
587 | ||
785b9ff2 PS |
588 | #ifdef HAVE_LIBUSB_1_0 |
589 | ret = libusb_init(&context->libusb_ctx); | |
8e2d43cc | 590 | if (LIBUSB_SUCCESS != ret) { |
9d122af8 | 591 | sr_err("libusb_init() returned %s.", libusb_error_name(ret)); |
63c07e48 | 592 | ret = SR_ERR; |
785b9ff2 PS |
593 | goto done; |
594 | } | |
595 | #endif | |
bee24666 | 596 | sr_resource_set_hooks(context, NULL, NULL, NULL, NULL); |
785b9ff2 | 597 | |
b8072700 | 598 | *ctx = context; |
123d97b1 | 599 | context = NULL; |
b8072700 PS |
600 | ret = SR_OK; |
601 | ||
602 | done: | |
b1f83103 | 603 | g_free(context); |
b8072700 | 604 | return ret; |
a1bb33af UH |
605 | } |
606 | ||
cd009d55 UH |
607 | /** |
608 | * Shutdown libsigrok. | |
609 | * | |
c46762a2 UH |
610 | * @param ctx Pointer to a libsigrok context struct. Must not be NULL. |
611 | * | |
04cb9157 MH |
612 | * @retval SR_OK Success |
613 | * @retval other Error code SR_ERR, ... | |
9fb5f2df | 614 | * |
53f05fa8 | 615 | * @since 0.2.0 |
cd009d55 | 616 | */ |
b8072700 | 617 | SR_API int sr_exit(struct sr_context *ctx) |
a1bb33af | 618 | { |
c46762a2 UH |
619 | if (!ctx) { |
620 | sr_err("%s(): libsigrok context was NULL.", __func__); | |
621 | return SR_ERR; | |
622 | } | |
623 | ||
032da34b | 624 | sr_hw_cleanup_all(ctx); |
cd009d55 | 625 | |
fa69e191 ML |
626 | #ifdef _WIN32 |
627 | WSACleanup(); | |
628 | #endif | |
629 | ||
785b9ff2 PS |
630 | #ifdef HAVE_LIBUSB_1_0 |
631 | libusb_exit(ctx->libusb_ctx); | |
632 | #endif | |
633 | ||
032da34b | 634 | g_free(sr_driver_list(ctx)); |
b8072700 PS |
635 | g_free(ctx); |
636 | ||
cd009d55 | 637 | return SR_OK; |
a1bb33af | 638 | } |
7b870c38 UH |
639 | |
640 | /** @} */ |