]> sigrok.org Git - libsigrok.git/blame - src/backend.c
gmc-mh-1x-2x: Make two functions static.
[libsigrok.git] / src / backend.c
CommitLineData
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
bd7b83cf
UH
125static void print_versions(void)
126{
127 GString *s;
731c01f2 128#if defined(HAVE_LIBUSB_1_0) && !defined(__FreeBSD__)
bd7b83cf
UH
129 const struct libusb_version *lv;
130#endif
131
132 s = g_string_sized_new(200);
133
134 sr_dbg("libsigrok %s/%s (rt: %s/%s).",
135 SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
136 sr_package_version_string_get(), sr_lib_version_string_get());
137
138 g_string_append(s, "Libs: ");
139 g_string_append_printf(s, "glib %d.%d.%d (rt: %d.%d.%d/%d:%d), ",
140 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
141 glib_major_version, glib_minor_version, glib_micro_version,
142 glib_binary_age, glib_interface_age);
2cbde151 143 g_string_append_printf(s, "libzip %s, ", CONF_LIBZIP_VERSION);
bd7b83cf
UH
144#ifdef HAVE_LIBSERIALPORT
145 g_string_append_printf(s, "libserialport %s/%s (rt: %s/%s), ",
146 SP_PACKAGE_VERSION_STRING, SP_LIB_VERSION_STRING,
147 sp_get_package_version_string(), sp_get_lib_version_string());
148#endif
149#ifdef HAVE_LIBUSB_1_0
731c01f2
UH
150#ifdef __FreeBSD__
151 g_string_append_printf(s, "libusb-1.0 %s, ", CONF_LIBUSB_1_0_VERSION);
152#else
bd7b83cf
UH
153 lv = libusb_get_version();
154 g_string_append_printf(s, "libusb-1.0 %d.%d.%d.%d%s, ",
155 lv->major, lv->minor, lv->micro, lv->nano, lv->rc);
156#endif
731c01f2 157#endif
bd7b83cf 158#ifdef HAVE_LIBFTDI
2cbde151 159 g_string_append_printf(s, "libftdi %s, ", CONF_LIBFTDI_VERSION);
bd7b83cf
UH
160#endif
161#ifdef HAVE_LIBGPIB
2cbde151 162 g_string_append_printf(s, "libgpib %s, ", CONF_LIBGPIB_VERSION);
bd7b83cf
UH
163#endif
164#ifdef HAVE_LIBREVISA
2cbde151 165 g_string_append_printf(s, "librevisa %s, ", CONF_LIBREVISA_VERSION);
bd7b83cf
UH
166#endif
167 s->str[s->len - 2] = '.';
168 s->str[s->len - 1] = '\0';
169 sr_dbg("%s", s->str);
170
171 s = g_string_truncate(s, 0);
2cbde151 172 g_string_append_printf(s, "Host: %s, ", CONF_HOST);
bd7b83cf
UH
173#ifdef WORDS_BIGENDIAN
174 g_string_append_printf(s, "big-endian.");
175#else
176 g_string_append_printf(s, "little-endian.");
177#endif
178 sr_dbg("%s", s->str);
179
180 s = g_string_truncate(s, 0);
181 g_string_append_printf(s, "SCPI backends: ");
182
183 g_string_append_printf(s, "TCP, ");
184#if HAVE_RPC
185 g_string_append_printf(s, "RPC, ");
186#endif
187#ifdef HAVE_LIBSERIALPORT
188 g_string_append_printf(s, "serial, ");
189#endif
190#ifdef HAVE_LIBREVISA
191 g_string_append_printf(s, "VISA, ");
192#endif
193#ifdef HAVE_LIBGPIB
194 g_string_append_printf(s, "GPIB, ");
195#endif
196#ifdef HAVE_LIBUSB_1_0
197 g_string_append_printf(s, "USBTMC, ");
198#endif
199 s->str[s->len - 2] = '.';
200 s->str[s->len - 1] = '\0';
201 sr_dbg("%s", s->str);
202
203 g_string_free(s, TRUE);
204}
205
55a6daf5
UH
206/**
207 * Sanity-check all libsigrok drivers.
208 *
032da34b
UH
209 * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
210 *
04cb9157
MH
211 * @retval SR_OK All drivers are OK
212 * @retval SR_ERR One or more drivers have issues.
032da34b 213 * @retval SR_ERR_ARG Invalid argument.
55a6daf5 214 */
032da34b 215static int sanity_check_all_drivers(const struct sr_context *ctx)
55a6daf5
UH
216{
217 int i, errors, ret = SR_OK;
218 struct sr_dev_driver **drivers;
219 const char *d;
220
032da34b
UH
221 if (!ctx)
222 return SR_ERR_ARG;
223
55a6daf5
UH
224 sr_spew("Sanity-checking all drivers.");
225
032da34b 226 drivers = sr_driver_list(ctx);
55a6daf5
UH
227 for (i = 0; drivers[i]; i++) {
228 errors = 0;
229
230 d = (drivers[i]->name) ? drivers[i]->name : "NULL";
231
232 if (!drivers[i]->name) {
233 sr_err("No name in driver %d ('%s').", i, d);
234 errors++;
235 }
236 if (!drivers[i]->longname) {
237 sr_err("No longname in driver %d ('%s').", i, d);
238 errors++;
239 }
240 if (drivers[i]->api_version < 1) {
241 sr_err("API version in driver %d ('%s') < 1.", i, d);
242 errors++;
243 }
244 if (!drivers[i]->init) {
245 sr_err("No init in driver %d ('%s').", i, d);
246 errors++;
247 }
248 if (!drivers[i]->cleanup) {
249 sr_err("No cleanup in driver %d ('%s').", i, d);
250 errors++;
251 }
252 if (!drivers[i]->scan) {
253 sr_err("No scan in driver %d ('%s').", i, d);
254 errors++;
255 }
256 if (!drivers[i]->dev_list) {
257 sr_err("No dev_list in driver %d ('%s').", i, d);
258 errors++;
259 }
6fab7b8f
UH
260 /* Note: config_get() is optional. */
261 if (!drivers[i]->config_set) {
262 sr_err("No config_set in driver %d ('%s').", i, d);
263 errors++;
264 }
265 if (!drivers[i]->config_list) {
266 sr_err("No config_list in driver %d ('%s').", i, d);
267 errors++;
268 }
55a6daf5
UH
269 if (!drivers[i]->dev_open) {
270 sr_err("No dev_open in driver %d ('%s').", i, d);
271 errors++;
272 }
273 if (!drivers[i]->dev_close) {
274 sr_err("No dev_close in driver %d ('%s').", i, d);
275 errors++;
276 }
55a6daf5
UH
277 if (!drivers[i]->dev_acquisition_start) {
278 sr_err("No dev_acquisition_start in driver %d ('%s').",
279 i, d);
280 errors++;
281 }
282 if (!drivers[i]->dev_acquisition_stop) {
283 sr_err("No dev_acquisition_stop in driver %d ('%s').",
284 i, d);
285 errors++;
286 }
287
288 /* Note: 'priv' is allowed to be NULL. */
289
290 if (errors == 0)
291 continue;
292
293 ret = SR_ERR;
294 }
295
296 return ret;
297}
298
218e629f
UH
299/**
300 * Sanity-check all libsigrok input modules.
301 *
04cb9157
MH
302 * @retval SR_OK All modules are OK
303 * @retval SR_ERR One or more modules have issues.
218e629f
UH
304 */
305static int sanity_check_all_input_modules(void)
306{
307 int i, errors, ret = SR_OK;
17bfaca6 308 const struct sr_input_module **inputs;
218e629f
UH
309 const char *d;
310
311 sr_spew("Sanity-checking all input modules.");
312
313 inputs = sr_input_list();
314 for (i = 0; inputs[i]; i++) {
315 errors = 0;
316
317 d = (inputs[i]->id) ? inputs[i]->id : "NULL";
318
319 if (!inputs[i]->id) {
320 sr_err("No ID in module %d ('%s').", i, d);
321 errors++;
322 }
17bfaca6
BV
323 if (!inputs[i]->name) {
324 sr_err("No name in module %d ('%s').", i, d);
325 errors++;
326 }
327 if (!inputs[i]->desc) {
218e629f
UH
328 sr_err("No description in module %d ('%s').", i, d);
329 errors++;
330 }
218e629f
UH
331 if (!inputs[i]->init) {
332 sr_err("No init in module %d ('%s').", i, d);
333 errors++;
334 }
17bfaca6
BV
335 if (!inputs[i]->receive) {
336 sr_err("No receive in module %d ('%s').", i, d);
7066fd46
BV
337 errors++;
338 }
339 if (!inputs[i]->end) {
340 sr_err("No end in module %d ('%s').", i, d);
218e629f
UH
341 errors++;
342 }
343
344 if (errors == 0)
345 continue;
346
347 ret = SR_ERR;
348 }
349
350 return ret;
351}
352
353/**
354 * Sanity-check all libsigrok output modules.
355 *
04cb9157
MH
356 * @retval SR_OK All modules are OK
357 * @retval SR_ERR One or more modules have issues.
218e629f
UH
358 */
359static int sanity_check_all_output_modules(void)
360{
361 int i, errors, ret = SR_OK;
a755b0e1 362 const struct sr_output_module **outputs;
218e629f
UH
363 const char *d;
364
365 sr_spew("Sanity-checking all output modules.");
366
367 outputs = sr_output_list();
368 for (i = 0; outputs[i]; i++) {
369 errors = 0;
370
371 d = (outputs[i]->id) ? outputs[i]->id : "NULL";
372
373 if (!outputs[i]->id) {
374 sr_err("No ID in module %d ('%s').", i, d);
375 errors++;
376 }
a755b0e1
BV
377 if (!outputs[i]->name) {
378 sr_err("No name in module %d ('%s').", i, d);
379 errors++;
380 }
381 if (!outputs[i]->desc) {
44559b2c 382 sr_err("No description in module '%s'.", d);
218e629f
UH
383 errors++;
384 }
44559b2c
BV
385 if (!outputs[i]->receive) {
386 sr_err("No receive in module '%s'.", d);
218e629f
UH
387 errors++;
388 }
389
218e629f
UH
390 if (errors == 0)
391 continue;
392
393 ret = SR_ERR;
394 }
395
396 return ret;
397}
398
187cfc60
UH
399/**
400 * Sanity-check all libsigrok transform modules.
401 *
402 * @retval SR_OK All modules are OK
403 * @retval SR_ERR One or more modules have issues.
404 */
405static int sanity_check_all_transform_modules(void)
406{
407 int i, errors, ret = SR_OK;
408 const struct sr_transform_module **transforms;
409 const char *d;
410
411 sr_spew("Sanity-checking all transform modules.");
412
413 transforms = sr_transform_list();
414 for (i = 0; transforms[i]; i++) {
415 errors = 0;
416
417 d = (transforms[i]->id) ? transforms[i]->id : "NULL";
418
419 if (!transforms[i]->id) {
420 sr_err("No ID in module %d ('%s').", i, d);
421 errors++;
422 }
423 if (!transforms[i]->name) {
424 sr_err("No name in module %d ('%s').", i, d);
425 errors++;
426 }
427 if (!transforms[i]->desc) {
428 sr_err("No description in module '%s'.", d);
429 errors++;
430 }
431 /* Note: options() is optional. */
432 /* Note: init() is optional. */
433 if (!transforms[i]->receive) {
434 sr_err("No receive in module '%s'.", d);
435 errors++;
436 }
437 /* Note: cleanup() is optional. */
438
439 if (errors == 0)
440 continue;
441
442 ret = SR_ERR;
443 }
444
445 return ret;
446}
447
cd009d55
UH
448/**
449 * Initialize libsigrok.
450 *
c46762a2
UH
451 * This function must be called before any other libsigrok function.
452 *
453 * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
454 * This will be a pointer to a newly allocated libsigrok context
455 * object upon success, and is undefined upon errors.
456 *
457 * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
458 * the 'ctx' pointer is undefined and should not be used. Upon success,
459 * the context will be free'd by sr_exit() as part of the libsigrok
460 * shutdown.
9fb5f2df 461 *
53f05fa8 462 * @since 0.2.0
cd009d55 463 */
b8072700 464SR_API int sr_init(struct sr_context **ctx)
a1bb33af 465{
b8072700
PS
466 int ret = SR_ERR;
467 struct sr_context *context;
032da34b
UH
468 struct sr_dev_driver ***lists, **drivers;
469 GArray *array;
fa69e191
ML
470#ifdef _WIN32
471 WSADATA wsadata;
472#endif
b8072700 473
bd7b83cf
UH
474 print_versions();
475
c46762a2
UH
476 if (!ctx) {
477 sr_err("%s(): libsigrok context was NULL.", __func__);
478 return SR_ERR;
479 }
480
032da34b
UH
481 context = g_malloc0(sizeof(struct sr_context));
482
483 /* Generate ctx->driver_list at runtime. */
484 array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
485 for (lists = drivers_lists; *lists; lists++)
486 for (drivers = *lists; *drivers; drivers++)
487 g_array_append_val(array, *drivers);
488 context->driver_list = (struct sr_dev_driver **)array->data;
489 g_array_free(array, FALSE);
490
491 if (sanity_check_all_drivers(context) < 0) {
55a6daf5
UH
492 sr_err("Internal driver error(s), aborting.");
493 return ret;
494 }
495
218e629f
UH
496 if (sanity_check_all_input_modules() < 0) {
497 sr_err("Internal input module error(s), aborting.");
498 return ret;
499 }
500
501 if (sanity_check_all_output_modules() < 0) {
502 sr_err("Internal output module error(s), aborting.");
503 return ret;
504 }
505
187cfc60
UH
506 if (sanity_check_all_transform_modules() < 0) {
507 sr_err("Internal transform module error(s), aborting.");
508 return ret;
509 }
510
fa69e191
ML
511#ifdef _WIN32
512 if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
513 sr_err("WSAStartup failed with error code %d.", ret);
514 ret = SR_ERR;
515 goto done;
516 }
517#endif
518
785b9ff2
PS
519#ifdef HAVE_LIBUSB_1_0
520 ret = libusb_init(&context->libusb_ctx);
8e2d43cc 521 if (LIBUSB_SUCCESS != ret) {
9d122af8 522 sr_err("libusb_init() returned %s.", libusb_error_name(ret));
63c07e48 523 ret = SR_ERR;
785b9ff2
PS
524 goto done;
525 }
526#endif
bee24666 527 sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
785b9ff2 528
b8072700 529 *ctx = context;
123d97b1 530 context = NULL;
b8072700
PS
531 ret = SR_OK;
532
fa69e191 533#if defined(HAVE_LIBUSB_1_0) || defined(_WIN32)
b8072700 534done:
468665df 535#endif
b1f83103 536 g_free(context);
b8072700 537 return ret;
a1bb33af
UH
538}
539
cd009d55
UH
540/**
541 * Shutdown libsigrok.
542 *
c46762a2
UH
543 * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
544 *
04cb9157
MH
545 * @retval SR_OK Success
546 * @retval other Error code SR_ERR, ...
9fb5f2df 547 *
53f05fa8 548 * @since 0.2.0
cd009d55 549 */
b8072700 550SR_API int sr_exit(struct sr_context *ctx)
a1bb33af 551{
c46762a2
UH
552 if (!ctx) {
553 sr_err("%s(): libsigrok context was NULL.", __func__);
554 return SR_ERR;
555 }
556
032da34b 557 sr_hw_cleanup_all(ctx);
cd009d55 558
fa69e191
ML
559#ifdef _WIN32
560 WSACleanup();
561#endif
562
785b9ff2
PS
563#ifdef HAVE_LIBUSB_1_0
564 libusb_exit(ctx->libusb_ctx);
565#endif
566
032da34b 567 g_free(sr_driver_list(ctx));
b8072700
PS
568 g_free(ctx);
569
cd009d55 570 return SR_OK;
a1bb33af 571}
7b870c38
UH
572
573/** @} */