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 static void print_versions(void)
128 #if defined(HAVE_LIBUSB_1_0) && !defined(__FreeBSD__)
129 const struct libusb_version *lv;
132 s = g_string_sized_new(200);
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());
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);
143 g_string_append_printf(s, "libzip %s, ", CONF_LIBZIP_VERSION);
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());
149 #ifdef HAVE_LIBUSB_1_0
151 g_string_append_printf(s, "libusb-1.0 %s, ", CONF_LIBUSB_1_0_VERSION);
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);
159 g_string_append_printf(s, "libftdi %s, ", CONF_LIBFTDI_VERSION);
162 g_string_append_printf(s, "libgpib %s, ", CONF_LIBGPIB_VERSION);
164 #ifdef HAVE_LIBREVISA
165 g_string_append_printf(s, "librevisa %s, ", CONF_LIBREVISA_VERSION);
167 s->str[s->len - 2] = '.';
168 s->str[s->len - 1] = '\0';
169 sr_dbg("%s", s->str);
171 s = g_string_truncate(s, 0);
172 g_string_append_printf(s, "Host: %s, ", CONF_HOST);
173 #ifdef WORDS_BIGENDIAN
174 g_string_append_printf(s, "big-endian.");
176 g_string_append_printf(s, "little-endian.");
178 sr_dbg("%s", s->str);
180 s = g_string_truncate(s, 0);
181 g_string_append_printf(s, "SCPI backends: ");
183 g_string_append_printf(s, "TCP, ");
185 g_string_append_printf(s, "RPC, ");
187 #ifdef HAVE_LIBSERIALPORT
188 g_string_append_printf(s, "serial, ");
190 #ifdef HAVE_LIBREVISA
191 g_string_append_printf(s, "VISA, ");
194 g_string_append_printf(s, "GPIB, ");
196 #ifdef HAVE_LIBUSB_1_0
197 g_string_append_printf(s, "USBTMC, ");
199 s->str[s->len - 2] = '.';
200 s->str[s->len - 1] = '\0';
201 sr_dbg("%s", s->str);
203 g_string_free(s, TRUE);
207 * Sanity-check all libsigrok drivers.
209 * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
211 * @retval SR_OK All drivers are OK
212 * @retval SR_ERR One or more drivers have issues.
213 * @retval SR_ERR_ARG Invalid argument.
215 static int sanity_check_all_drivers(const struct sr_context *ctx)
217 int i, errors, ret = SR_OK;
218 struct sr_dev_driver **drivers;
224 sr_spew("Sanity-checking all drivers.");
226 drivers = sr_driver_list(ctx);
227 for (i = 0; drivers[i]; i++) {
230 d = (drivers[i]->name) ? drivers[i]->name : "NULL";
232 if (!drivers[i]->name) {
233 sr_err("No name in driver %d ('%s').", i, d);
236 if (!drivers[i]->longname) {
237 sr_err("No longname in driver %d ('%s').", i, d);
240 if (drivers[i]->api_version < 1) {
241 sr_err("API version in driver %d ('%s') < 1.", i, d);
244 if (!drivers[i]->init) {
245 sr_err("No init in driver %d ('%s').", i, d);
248 if (!drivers[i]->cleanup) {
249 sr_err("No cleanup in driver %d ('%s').", i, d);
252 if (!drivers[i]->scan) {
253 sr_err("No scan in driver %d ('%s').", i, d);
256 if (!drivers[i]->dev_list) {
257 sr_err("No dev_list in driver %d ('%s').", i, d);
260 /* Note: dev_clear() is optional. */
261 /* Note: config_get() is optional. */
262 if (!drivers[i]->config_set) {
263 sr_err("No config_set in driver %d ('%s').", i, d);
266 /* Note: config_channel_set() is optional. */
267 /* Note: config_commit() is optional. */
268 if (!drivers[i]->config_list) {
269 sr_err("No config_list in driver %d ('%s').", i, d);
272 if (!drivers[i]->dev_open) {
273 sr_err("No dev_open in driver %d ('%s').", i, d);
276 if (!drivers[i]->dev_close) {
277 sr_err("No dev_close in driver %d ('%s').", i, d);
280 if (!drivers[i]->dev_acquisition_start) {
281 sr_err("No dev_acquisition_start in driver %d ('%s').",
285 if (!drivers[i]->dev_acquisition_stop) {
286 sr_err("No dev_acquisition_stop in driver %d ('%s').",
291 /* Note: 'priv' is allowed to be NULL. */
303 * Sanity-check all libsigrok input modules.
305 * @retval SR_OK All modules are OK
306 * @retval SR_ERR One or more modules have issues.
308 static int sanity_check_all_input_modules(void)
310 int i, errors, ret = SR_OK;
311 const struct sr_input_module **inputs;
314 sr_spew("Sanity-checking all input modules.");
316 inputs = sr_input_list();
317 for (i = 0; inputs[i]; i++) {
320 d = (inputs[i]->id) ? inputs[i]->id : "NULL";
322 if (!inputs[i]->id) {
323 sr_err("No ID in module %d ('%s').", i, d);
326 if (!inputs[i]->name) {
327 sr_err("No name in module %d ('%s').", i, d);
330 if (!inputs[i]->desc) {
331 sr_err("No description in module %d ('%s').", i, d);
334 if (!inputs[i]->init) {
335 sr_err("No init in module %d ('%s').", i, d);
338 if (!inputs[i]->receive) {
339 sr_err("No receive in module %d ('%s').", i, d);
342 if (!inputs[i]->end) {
343 sr_err("No end in module %d ('%s').", i, d);
357 * Sanity-check all libsigrok output modules.
359 * @retval SR_OK All modules are OK
360 * @retval SR_ERR One or more modules have issues.
362 static int sanity_check_all_output_modules(void)
364 int i, errors, ret = SR_OK;
365 const struct sr_output_module **outputs;
368 sr_spew("Sanity-checking all output modules.");
370 outputs = sr_output_list();
371 for (i = 0; outputs[i]; i++) {
374 d = (outputs[i]->id) ? outputs[i]->id : "NULL";
376 if (!outputs[i]->id) {
377 sr_err("No ID in module %d ('%s').", i, d);
380 if (!outputs[i]->name) {
381 sr_err("No name in module %d ('%s').", i, d);
384 if (!outputs[i]->desc) {
385 sr_err("No description in module '%s'.", d);
388 if (!outputs[i]->receive) {
389 sr_err("No receive in module '%s'.", d);
403 * Sanity-check all libsigrok transform modules.
405 * @retval SR_OK All modules are OK
406 * @retval SR_ERR One or more modules have issues.
408 static int sanity_check_all_transform_modules(void)
410 int i, errors, ret = SR_OK;
411 const struct sr_transform_module **transforms;
414 sr_spew("Sanity-checking all transform modules.");
416 transforms = sr_transform_list();
417 for (i = 0; transforms[i]; i++) {
420 d = (transforms[i]->id) ? transforms[i]->id : "NULL";
422 if (!transforms[i]->id) {
423 sr_err("No ID in module %d ('%s').", i, d);
426 if (!transforms[i]->name) {
427 sr_err("No name in module %d ('%s').", i, d);
430 if (!transforms[i]->desc) {
431 sr_err("No description in module '%s'.", d);
434 /* Note: options() is optional. */
435 /* Note: init() is optional. */
436 if (!transforms[i]->receive) {
437 sr_err("No receive in module '%s'.", d);
440 /* Note: cleanup() is optional. */
452 * Initialize libsigrok.
454 * This function must be called before any other libsigrok function.
456 * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
457 * This will be a pointer to a newly allocated libsigrok context
458 * object upon success, and is undefined upon errors.
460 * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
461 * the 'ctx' pointer is undefined and should not be used. Upon success,
462 * the context will be free'd by sr_exit() as part of the libsigrok
467 SR_API int sr_init(struct sr_context **ctx)
470 struct sr_context *context;
471 struct sr_dev_driver ***lists, **drivers;
480 sr_err("%s(): libsigrok context was NULL.", __func__);
484 context = g_malloc0(sizeof(struct sr_context));
486 /* Generate ctx->driver_list at runtime. */
487 array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
488 for (lists = drivers_lists; *lists; lists++)
489 for (drivers = *lists; *drivers; drivers++)
490 g_array_append_val(array, *drivers);
491 context->driver_list = (struct sr_dev_driver **)array->data;
492 g_array_free(array, FALSE);
494 if (sanity_check_all_drivers(context) < 0) {
495 sr_err("Internal driver error(s), aborting.");
499 if (sanity_check_all_input_modules() < 0) {
500 sr_err("Internal input module error(s), aborting.");
504 if (sanity_check_all_output_modules() < 0) {
505 sr_err("Internal output module error(s), aborting.");
509 if (sanity_check_all_transform_modules() < 0) {
510 sr_err("Internal transform module error(s), aborting.");
515 if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
516 sr_err("WSAStartup failed with error code %d.", ret);
522 #ifdef HAVE_LIBUSB_1_0
523 ret = libusb_init(&context->libusb_ctx);
524 if (LIBUSB_SUCCESS != ret) {
525 sr_err("libusb_init() returned %s.", libusb_error_name(ret));
530 sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
536 #if defined(HAVE_LIBUSB_1_0) || defined(_WIN32)
544 * Shutdown libsigrok.
546 * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
548 * @retval SR_OK Success
549 * @retval other Error code SR_ERR, ...
553 SR_API int sr_exit(struct sr_context *ctx)
556 sr_err("%s(): libsigrok context was NULL.", __func__);
560 sr_hw_cleanup_all(ctx);
566 #ifdef HAVE_LIBUSB_1_0
567 libusb_exit(ctx->libusb_ctx);
570 g_free(sr_driver_list(ctx));