]> sigrok.org Git - libsigrok.git/blame - src/backend.c
Put driver pointers into special section
[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 }
797f1a0c 260 /* Note: dev_clear() is optional. */
6fab7b8f
UH
261 /* Note: config_get() is optional. */
262 if (!drivers[i]->config_set) {
263 sr_err("No config_set in driver %d ('%s').", i, d);
264 errors++;
265 }
797f1a0c
UH
266 /* Note: config_channel_set() is optional. */
267 /* Note: config_commit() is optional. */
6fab7b8f
UH
268 if (!drivers[i]->config_list) {
269 sr_err("No config_list in driver %d ('%s').", i, d);
270 errors++;
271 }
55a6daf5
UH
272 if (!drivers[i]->dev_open) {
273 sr_err("No dev_open in driver %d ('%s').", i, d);
274 errors++;
275 }
276 if (!drivers[i]->dev_close) {
277 sr_err("No dev_close in driver %d ('%s').", i, d);
278 errors++;
279 }
55a6daf5
UH
280 if (!drivers[i]->dev_acquisition_start) {
281 sr_err("No dev_acquisition_start in driver %d ('%s').",
282 i, d);
283 errors++;
284 }
285 if (!drivers[i]->dev_acquisition_stop) {
286 sr_err("No dev_acquisition_stop in driver %d ('%s').",
287 i, d);
288 errors++;
289 }
290
291 /* Note: 'priv' is allowed to be NULL. */
292
293 if (errors == 0)
294 continue;
295
296 ret = SR_ERR;
297 }
298
299 return ret;
300}
301
218e629f
UH
302/**
303 * Sanity-check all libsigrok input modules.
304 *
04cb9157
MH
305 * @retval SR_OK All modules are OK
306 * @retval SR_ERR One or more modules have issues.
218e629f
UH
307 */
308static int sanity_check_all_input_modules(void)
309{
310 int i, errors, ret = SR_OK;
17bfaca6 311 const struct sr_input_module **inputs;
218e629f
UH
312 const char *d;
313
314 sr_spew("Sanity-checking all input modules.");
315
316 inputs = sr_input_list();
317 for (i = 0; inputs[i]; i++) {
318 errors = 0;
319
320 d = (inputs[i]->id) ? inputs[i]->id : "NULL";
321
322 if (!inputs[i]->id) {
323 sr_err("No ID in module %d ('%s').", i, d);
324 errors++;
325 }
17bfaca6
BV
326 if (!inputs[i]->name) {
327 sr_err("No name in module %d ('%s').", i, d);
328 errors++;
329 }
330 if (!inputs[i]->desc) {
218e629f
UH
331 sr_err("No description in module %d ('%s').", i, d);
332 errors++;
333 }
218e629f
UH
334 if (!inputs[i]->init) {
335 sr_err("No init in module %d ('%s').", i, d);
336 errors++;
337 }
17bfaca6
BV
338 if (!inputs[i]->receive) {
339 sr_err("No receive in module %d ('%s').", i, d);
7066fd46
BV
340 errors++;
341 }
342 if (!inputs[i]->end) {
343 sr_err("No end in module %d ('%s').", i, d);
218e629f
UH
344 errors++;
345 }
346
347 if (errors == 0)
348 continue;
349
350 ret = SR_ERR;
351 }
352
353 return ret;
354}
355
356/**
357 * Sanity-check all libsigrok output modules.
358 *
04cb9157
MH
359 * @retval SR_OK All modules are OK
360 * @retval SR_ERR One or more modules have issues.
218e629f
UH
361 */
362static int sanity_check_all_output_modules(void)
363{
364 int i, errors, ret = SR_OK;
a755b0e1 365 const struct sr_output_module **outputs;
218e629f
UH
366 const char *d;
367
368 sr_spew("Sanity-checking all output modules.");
369
370 outputs = sr_output_list();
371 for (i = 0; outputs[i]; i++) {
372 errors = 0;
373
374 d = (outputs[i]->id) ? outputs[i]->id : "NULL";
375
376 if (!outputs[i]->id) {
377 sr_err("No ID in module %d ('%s').", i, d);
378 errors++;
379 }
a755b0e1
BV
380 if (!outputs[i]->name) {
381 sr_err("No name in module %d ('%s').", i, d);
382 errors++;
383 }
384 if (!outputs[i]->desc) {
44559b2c 385 sr_err("No description in module '%s'.", d);
218e629f
UH
386 errors++;
387 }
44559b2c
BV
388 if (!outputs[i]->receive) {
389 sr_err("No receive in module '%s'.", d);
218e629f
UH
390 errors++;
391 }
392
218e629f
UH
393 if (errors == 0)
394 continue;
395
396 ret = SR_ERR;
397 }
398
399 return ret;
400}
401
187cfc60
UH
402/**
403 * Sanity-check all libsigrok transform modules.
404 *
405 * @retval SR_OK All modules are OK
406 * @retval SR_ERR One or more modules have issues.
407 */
408static int sanity_check_all_transform_modules(void)
409{
410 int i, errors, ret = SR_OK;
411 const struct sr_transform_module **transforms;
412 const char *d;
413
414 sr_spew("Sanity-checking all transform modules.");
415
416 transforms = sr_transform_list();
417 for (i = 0; transforms[i]; i++) {
418 errors = 0;
419
420 d = (transforms[i]->id) ? transforms[i]->id : "NULL";
421
422 if (!transforms[i]->id) {
423 sr_err("No ID in module %d ('%s').", i, d);
424 errors++;
425 }
426 if (!transforms[i]->name) {
427 sr_err("No name in module %d ('%s').", i, d);
428 errors++;
429 }
430 if (!transforms[i]->desc) {
431 sr_err("No description in module '%s'.", d);
432 errors++;
433 }
434 /* Note: options() is optional. */
435 /* Note: init() is optional. */
436 if (!transforms[i]->receive) {
437 sr_err("No receive in module '%s'.", d);
438 errors++;
439 }
440 /* Note: cleanup() is optional. */
441
442 if (errors == 0)
443 continue;
444
445 ret = SR_ERR;
446 }
447
448 return ret;
449}
450
dd5c48a6
LPC
451extern struct sr_dev_driver *sr_driver_list_start;
452
cd009d55
UH
453/**
454 * Initialize libsigrok.
455 *
c46762a2
UH
456 * This function must be called before any other libsigrok function.
457 *
458 * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
459 * This will be a pointer to a newly allocated libsigrok context
460 * object upon success, and is undefined upon errors.
461 *
462 * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
463 * the 'ctx' pointer is undefined and should not be used. Upon success,
464 * the context will be free'd by sr_exit() as part of the libsigrok
465 * shutdown.
9fb5f2df 466 *
53f05fa8 467 * @since 0.2.0
cd009d55 468 */
b8072700 469SR_API int sr_init(struct sr_context **ctx)
a1bb33af 470{
b8072700
PS
471 int ret = SR_ERR;
472 struct sr_context *context;
dd5c48a6 473 struct sr_dev_driver **drivers;
032da34b 474 GArray *array;
fa69e191
ML
475#ifdef _WIN32
476 WSADATA wsadata;
477#endif
b8072700 478
bd7b83cf
UH
479 print_versions();
480
c46762a2
UH
481 if (!ctx) {
482 sr_err("%s(): libsigrok context was NULL.", __func__);
483 return SR_ERR;
484 }
485
032da34b
UH
486 context = g_malloc0(sizeof(struct sr_context));
487
488 /* Generate ctx->driver_list at runtime. */
489 array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
dd5c48a6
LPC
490 for (drivers = (&sr_driver_list_start) + 1; *drivers; drivers++)
491 g_array_append_val(array, *drivers);
032da34b
UH
492 context->driver_list = (struct sr_dev_driver **)array->data;
493 g_array_free(array, FALSE);
494
495 if (sanity_check_all_drivers(context) < 0) {
55a6daf5
UH
496 sr_err("Internal driver error(s), aborting.");
497 return ret;
498 }
499
218e629f
UH
500 if (sanity_check_all_input_modules() < 0) {
501 sr_err("Internal input module error(s), aborting.");
502 return ret;
503 }
504
505 if (sanity_check_all_output_modules() < 0) {
506 sr_err("Internal output module error(s), aborting.");
507 return ret;
508 }
509
187cfc60
UH
510 if (sanity_check_all_transform_modules() < 0) {
511 sr_err("Internal transform module error(s), aborting.");
512 return ret;
513 }
514
fa69e191
ML
515#ifdef _WIN32
516 if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
517 sr_err("WSAStartup failed with error code %d.", ret);
518 ret = SR_ERR;
519 goto done;
520 }
521#endif
522
785b9ff2
PS
523#ifdef HAVE_LIBUSB_1_0
524 ret = libusb_init(&context->libusb_ctx);
8e2d43cc 525 if (LIBUSB_SUCCESS != ret) {
9d122af8 526 sr_err("libusb_init() returned %s.", libusb_error_name(ret));
63c07e48 527 ret = SR_ERR;
785b9ff2
PS
528 goto done;
529 }
530#endif
bee24666 531 sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
785b9ff2 532
b8072700 533 *ctx = context;
123d97b1 534 context = NULL;
b8072700
PS
535 ret = SR_OK;
536
fa69e191 537#if defined(HAVE_LIBUSB_1_0) || defined(_WIN32)
b8072700 538done:
468665df 539#endif
b1f83103 540 g_free(context);
b8072700 541 return ret;
a1bb33af
UH
542}
543
cd009d55
UH
544/**
545 * Shutdown libsigrok.
546 *
c46762a2
UH
547 * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
548 *
04cb9157
MH
549 * @retval SR_OK Success
550 * @retval other Error code SR_ERR, ...
9fb5f2df 551 *
53f05fa8 552 * @since 0.2.0
cd009d55 553 */
b8072700 554SR_API int sr_exit(struct sr_context *ctx)
a1bb33af 555{
c46762a2
UH
556 if (!ctx) {
557 sr_err("%s(): libsigrok context was NULL.", __func__);
558 return SR_ERR;
559 }
560
032da34b 561 sr_hw_cleanup_all(ctx);
cd009d55 562
fa69e191
ML
563#ifdef _WIN32
564 WSACleanup();
565#endif
566
785b9ff2
PS
567#ifdef HAVE_LIBUSB_1_0
568 libusb_exit(ctx->libusb_ctx);
569#endif
570
032da34b 571 g_free(sr_driver_list(ctx));
b8072700
PS
572 g_free(ctx);
573
cd009d55 574 return SR_OK;
a1bb33af 575}
7b870c38
UH
576
577/** @} */