2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Peter Stuge <peter@stuge.se>
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.
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.
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/>.
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "backend"
34 * @mainpage libsigrok API
36 * @section sec_intro Introduction
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).
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
50 * @section sec_api API reference
52 * See the "Modules" page for an introduction to various libsigrok
53 * related topics and the detailed API documentation of the respective
56 * You can also browse the API documentation by file, or review all
59 * @section sec_mailinglists Mailing lists
61 * There is one mailing list for sigrok/libsigrok: <a href="https://lists.sourceforge.net/lists/listinfo/sigrok-devel">sigrok-devel</a>.
63 * @section sec_irc IRC
65 * You can find the sigrok developers in the
66 * <a href="irc://chat.freenode.net/sigrok">\#sigrok</a>
67 * IRC channel on Freenode.
69 * @section sec_website Website
71 * <a href="http://sigrok.org/wiki/Libsigrok">sigrok.org/wiki/Libsigrok</a>
77 * Initializing and shutting down libsigrok.
81 * @defgroup grp_init Initialization
83 * Initializing and shutting down libsigrok.
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
90 * When libsigrok functionality is no longer needed, sr_exit() should be
91 * called, which will (among other things) free the struct sr_context.
93 * Example for a minimal program using libsigrok:
97 * #include <libsigrok/libsigrok.h>
99 * int main(int argc, char **argv)
102 * struct sr_context *sr_ctx;
104 * if ((ret = sr_init(&sr_ctx)) != SR_OK) {
105 * printf("Error initializing libsigrok (%s): %s.\n",
106 * sr_strerror_name(ret), sr_strerror(ret));
110 * // Use libsigrok functions here...
112 * if ((ret = sr_exit(sr_ctx)) != SR_OK) {
113 * printf("Error shutting down libsigrok (%s): %s.\n",
114 * sr_strerror_name(ret), sr_strerror(ret));
125 SR_API GSList *sr_buildinfo_libs_get(void)
127 GSList *l = NULL, *m = NULL;
128 #if defined(HAVE_LIBUSB_1_0) && !defined(__FreeBSD__)
129 const struct libusb_version *lv;
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)",
134 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
135 glib_major_version, glib_minor_version, glib_micro_version,
136 glib_binary_age, glib_interface_age));
137 l = g_slist_append(l, m);
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);
143 #ifdef HAVE_LIBSERIALPORT
144 m = g_slist_append(NULL, g_strdup("libserialport"));
145 m = g_slist_append(m, g_strdup_printf("%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 l = g_slist_append(l, m);
150 #ifdef HAVE_LIBUSB_1_0
151 m = g_slist_append(NULL, g_strdup("libusb-1.0"));
153 m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBUSB_1_0_VERSION));
155 lv = libusb_get_version();
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,
158 #if defined(LIBUSB_API_VERSION)
160 #elif defined(LIBUSBX_API_VERSION)
165 l = g_slist_append(l, m);
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);
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);
177 #ifdef HAVE_LIBREVISA
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);
186 SR_API char *sr_buildinfo_host_get(void)
188 return g_strdup_printf("%s, %s-endian", CONF_HOST,
189 #ifdef WORDS_BIGENDIAN
197 SR_API char *sr_buildinfo_scpi_backends_get(void)
202 s = g_string_sized_new(200);
204 g_string_append_printf(s, "TCP, ");
206 g_string_append_printf(s, "RPC, ");
208 #ifdef HAVE_LIBSERIALPORT
209 g_string_append_printf(s, "serial, ");
211 #ifdef HAVE_LIBREVISA
212 g_string_append_printf(s, "VISA, ");
215 g_string_append_printf(s, "GPIB, ");
217 #ifdef HAVE_LIBUSB_1_0
218 g_string_append_printf(s, "USBTMC, ");
220 s->str[s->len - 2] = '\0';
222 str = g_strdup(s->str);
223 g_string_free(s, TRUE);
228 static void print_versions(void)
231 GSList *l, *l_orig, *m;
233 const char *lib, *version;
235 sr_dbg("libsigrok %s/%s.",
236 sr_package_version_string_get(), sr_lib_version_string_get());
238 s = g_string_sized_new(200);
239 g_string_append(s, "Libs: ");
240 l_orig = sr_buildinfo_libs_get();
241 for (l = l_orig; l; l = l->next) {
244 version = m->next->data;
245 g_string_append_printf(s, "%s %s, ", lib, version);
246 g_slist_free_full(m, g_free);
248 g_slist_free(l_orig);
249 s->str[s->len - 2] = '.';
250 s->str[s->len - 1] = '\0';
251 sr_dbg("%s", s->str);
252 g_string_free(s, TRUE);
254 str = sr_buildinfo_host_get();
255 sr_dbg("Host: %s.", str);
258 str = sr_buildinfo_scpi_backends_get();
259 sr_dbg("SCPI backends: %s.", str);
263 static void print_resourcepaths(void)
267 sr_dbg("Firmware search paths:");
268 l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
269 for (l = l_orig; l; l = l->next)
270 sr_dbg(" - %s", (const char *)l->data);
271 g_slist_free_full(l_orig, g_free);
275 * Sanity-check all libsigrok drivers.
277 * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
279 * @retval SR_OK All drivers are OK
280 * @retval SR_ERR One or more drivers have issues.
281 * @retval SR_ERR_ARG Invalid argument.
283 static int sanity_check_all_drivers(const struct sr_context *ctx)
285 int i, errors, ret = SR_OK;
286 struct sr_dev_driver **drivers;
292 sr_spew("Sanity-checking all drivers.");
294 drivers = sr_driver_list(ctx);
295 for (i = 0; drivers[i]; i++) {
298 d = (drivers[i]->name) ? drivers[i]->name : "NULL";
300 if (!drivers[i]->name) {
301 sr_err("No name in driver %d ('%s').", i, d);
304 if (!drivers[i]->longname) {
305 sr_err("No longname in driver %d ('%s').", i, d);
308 if (drivers[i]->api_version < 1) {
309 sr_err("API version in driver %d ('%s') < 1.", i, d);
312 if (!drivers[i]->init) {
313 sr_err("No init in driver %d ('%s').", i, d);
316 if (!drivers[i]->cleanup) {
317 sr_err("No cleanup in driver %d ('%s').", i, d);
320 if (!drivers[i]->scan) {
321 sr_err("No scan in driver %d ('%s').", i, d);
324 if (!drivers[i]->dev_list) {
325 sr_err("No dev_list in driver %d ('%s').", i, d);
328 if (!drivers[i]->dev_clear) {
329 sr_err("No dev_clear in driver %d ('%s').", i, d);
332 /* Note: config_get() is optional. */
333 if (!drivers[i]->config_set) {
334 sr_err("No config_set in driver %d ('%s').", i, d);
337 /* Note: config_channel_set() is optional. */
338 /* Note: config_commit() is optional. */
339 if (!drivers[i]->config_list) {
340 sr_err("No config_list in driver %d ('%s').", i, d);
343 if (!drivers[i]->dev_open) {
344 sr_err("No dev_open in driver %d ('%s').", i, d);
347 if (!drivers[i]->dev_close) {
348 sr_err("No dev_close in driver %d ('%s').", i, d);
351 if (!drivers[i]->dev_acquisition_start) {
352 sr_err("No dev_acquisition_start in driver %d ('%s').",
356 if (!drivers[i]->dev_acquisition_stop) {
357 sr_err("No dev_acquisition_stop in driver %d ('%s').",
362 /* Note: 'priv' is allowed to be NULL. */
374 * Sanity-check all libsigrok input modules.
376 * @retval SR_OK All modules are OK
377 * @retval SR_ERR One or more modules have issues.
379 static int sanity_check_all_input_modules(void)
381 int i, errors, ret = SR_OK;
382 const struct sr_input_module **inputs;
385 sr_spew("Sanity-checking all input modules.");
387 inputs = sr_input_list();
388 for (i = 0; inputs[i]; i++) {
391 d = (inputs[i]->id) ? inputs[i]->id : "NULL";
393 if (!inputs[i]->id) {
394 sr_err("No ID in module %d ('%s').", i, d);
397 if (!inputs[i]->name) {
398 sr_err("No name in module %d ('%s').", i, d);
401 if (!inputs[i]->desc) {
402 sr_err("No description in module %d ('%s').", i, d);
405 if (!inputs[i]->init) {
406 sr_err("No init in module %d ('%s').", i, d);
409 if (!inputs[i]->receive) {
410 sr_err("No receive in module %d ('%s').", i, d);
413 if (!inputs[i]->end) {
414 sr_err("No end in module %d ('%s').", i, d);
428 * Sanity-check all libsigrok output modules.
430 * @retval SR_OK All modules are OK
431 * @retval SR_ERR One or more modules have issues.
433 static int sanity_check_all_output_modules(void)
435 int i, errors, ret = SR_OK;
436 const struct sr_output_module **outputs;
439 sr_spew("Sanity-checking all output modules.");
441 outputs = sr_output_list();
442 for (i = 0; outputs[i]; i++) {
445 d = (outputs[i]->id) ? outputs[i]->id : "NULL";
447 if (!outputs[i]->id) {
448 sr_err("No ID in module %d ('%s').", i, d);
451 if (!outputs[i]->name) {
452 sr_err("No name in module %d ('%s').", i, d);
455 if (!outputs[i]->desc) {
456 sr_err("No description in module '%s'.", d);
459 if (!outputs[i]->receive) {
460 sr_err("No receive in module '%s'.", d);
474 * Sanity-check all libsigrok transform modules.
476 * @retval SR_OK All modules are OK
477 * @retval SR_ERR One or more modules have issues.
479 static int sanity_check_all_transform_modules(void)
481 int i, errors, ret = SR_OK;
482 const struct sr_transform_module **transforms;
485 sr_spew("Sanity-checking all transform modules.");
487 transforms = sr_transform_list();
488 for (i = 0; transforms[i]; i++) {
491 d = (transforms[i]->id) ? transforms[i]->id : "NULL";
493 if (!transforms[i]->id) {
494 sr_err("No ID in module %d ('%s').", i, d);
497 if (!transforms[i]->name) {
498 sr_err("No name in module %d ('%s').", i, d);
501 if (!transforms[i]->desc) {
502 sr_err("No description in module '%s'.", d);
505 /* Note: options() is optional. */
506 /* Note: init() is optional. */
507 if (!transforms[i]->receive) {
508 sr_err("No receive in module '%s'.", d);
511 /* Note: cleanup() is optional. */
523 * Initialize libsigrok.
525 * This function must be called before any other libsigrok function.
527 * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
528 * This will be a pointer to a newly allocated libsigrok context
529 * object upon success, and is undefined upon errors.
531 * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
532 * the 'ctx' pointer is undefined and should not be used. Upon success,
533 * the context will be free'd by sr_exit() as part of the libsigrok
538 SR_API int sr_init(struct sr_context **ctx)
541 struct sr_context *context;
548 print_resourcepaths();
551 sr_err("%s(): libsigrok context was NULL.", __func__);
555 context = g_malloc0(sizeof(struct sr_context));
557 sr_drivers_init(context);
559 if (sanity_check_all_drivers(context) < 0) {
560 sr_err("Internal driver error(s), aborting.");
564 if (sanity_check_all_input_modules() < 0) {
565 sr_err("Internal input module error(s), aborting.");
569 if (sanity_check_all_output_modules() < 0) {
570 sr_err("Internal output module error(s), aborting.");
574 if (sanity_check_all_transform_modules() < 0) {
575 sr_err("Internal transform module error(s), aborting.");
580 if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
581 sr_err("WSAStartup failed with error code %d.", ret);
587 #ifdef HAVE_LIBUSB_1_0
588 ret = libusb_init(&context->libusb_ctx);
589 if (LIBUSB_SUCCESS != ret) {
590 sr_err("libusb_init() returned %s.", libusb_error_name(ret));
595 sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
607 * Shutdown libsigrok.
609 * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
611 * @retval SR_OK Success
612 * @retval other Error code SR_ERR, ...
616 SR_API int sr_exit(struct sr_context *ctx)
619 sr_err("%s(): libsigrok context was NULL.", __func__);
623 sr_hw_cleanup_all(ctx);
629 #ifdef HAVE_LIBUSB_1_0
630 libusb_exit(ctx->libusb_ctx);
633 g_free(sr_driver_list(ctx));