]> sigrok.org Git - libsigrok.git/blob - src/backend.c
sr_buildinfo_libs_get(): Show LIBUSB_API_VERSION.
[libsigrok.git] / src / backend.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2012 Peter Stuge <peter@stuge.se>
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
21 #include <config.h>
22 #include <glib.h>
23 #ifdef _WIN32
24 #include <winsock2.h>
25 #endif
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28
29 /** @cond PRIVATE */
30 #define LOG_PREFIX "backend"
31 /** @endcond */
32
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  *
50  * @section sec_api API reference
51  *
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.
58  *
59  * @section sec_mailinglists Mailing lists
60  *
61  * There is one mailing list for sigrok/libsigrok: <a href="https://lists.sourceforge.net/lists/listinfo/sigrok-devel">sigrok-devel</a>.
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
74 /**
75  * @file
76  *
77  * Initializing and shutting down libsigrok.
78  */
79
80 /**
81  * @defgroup grp_init Initialization
82  *
83  * Initializing and shutting down libsigrok.
84  *
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.
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) {
105  *              printf("Error initializing libsigrok (%s): %s.\n",
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) {
113  *              printf("Error shutting down libsigrok (%s): %s.\n",
114  *                     sr_strerror_name(ret), sr_strerror(ret));
115  *              return 1;
116  *      }
117  *
118  *      return 0;
119  *   }
120  * @endcode
121  *
122  * @{
123  */
124
125 SR_API GSList *sr_buildinfo_libs_get(void)
126 {
127         GSList *l = NULL, *m = NULL;
128 #if defined(HAVE_LIBUSB_1_0) && !defined(__FreeBSD__)
129         const struct libusb_version *lv;
130 #endif
131
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);
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
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);
149 #endif
150 #ifdef HAVE_LIBUSB_1_0
151         m = g_slist_append(NULL, g_strdup("libusb-1.0"));
152 #ifdef __FreeBSD__
153         m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBUSB_1_0_VERSION));
154 #else
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                 LIBUSB_API_VERSION));
159 #endif
160         l = g_slist_append(l, m);
161 #endif
162 #ifdef HAVE_LIBFTDI
163         m = g_slist_append(NULL, g_strdup("libftdi"));
164         m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBFTDI_VERSION));
165         l = g_slist_append(l, m);
166 #endif
167 #ifdef HAVE_LIBGPIB
168         m = g_slist_append(NULL, g_strdup("libgpib"));
169         m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBGPIB_VERSION));
170         l = g_slist_append(l, m);
171 #endif
172 #ifdef HAVE_LIBREVISA
173         m = g_slist_append(NULL, g_strdup("librevisa"));
174         m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBREVISA_VERSION));
175         l = g_slist_append(l, m);
176 #endif
177
178         return l;
179 }
180
181 SR_API char *sr_buildinfo_host_get(void)
182 {
183         return g_strdup_printf("%s, %s-endian", CONF_HOST,
184 #ifdef WORDS_BIGENDIAN
185         "big"
186 #else
187         "little"
188 #endif
189         );
190 }
191
192 SR_API char *sr_buildinfo_scpi_backends_get(void)
193 {
194         GString *s;
195         char *str;
196
197         s = g_string_sized_new(200);
198
199         g_string_append_printf(s, "TCP, ");
200 #if HAVE_RPC
201         g_string_append_printf(s, "RPC, ");
202 #endif
203 #ifdef HAVE_LIBSERIALPORT
204         g_string_append_printf(s, "serial, ");
205 #endif
206 #ifdef HAVE_LIBREVISA
207         g_string_append_printf(s, "VISA, ");
208 #endif
209 #ifdef HAVE_LIBGPIB
210         g_string_append_printf(s, "GPIB, ");
211 #endif
212 #ifdef HAVE_LIBUSB_1_0
213         g_string_append_printf(s, "USBTMC, ");
214 #endif
215         s->str[s->len - 2] = '\0';
216
217         str = g_strdup(s->str);
218         g_string_free(s, TRUE);
219
220         return str;
221 }
222
223 static void print_versions(void)
224 {
225         GString *s;
226         GSList *l, *l_orig, *m;
227         char *str;
228         const char *lib, *version;
229
230         sr_dbg("libsigrok %s/%s (rt: %s/%s).",
231                 SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
232                 sr_package_version_string_get(), sr_lib_version_string_get());
233
234         s = g_string_sized_new(200);
235         g_string_append(s, "Libs: ");
236         l_orig = sr_buildinfo_libs_get();
237         for (l = l_orig; l; l = l->next) {
238                 m = l->data;
239                 lib = m->data;
240                 version = m->next->data;
241                 g_string_append_printf(s, "%s %s, ", lib, version);
242                 g_slist_free_full(m, g_free);
243         }
244         g_slist_free(l_orig);
245         s->str[s->len - 2] = '.';
246         s->str[s->len - 1] = '\0';
247         sr_dbg("%s", s->str);
248         g_string_free(s, TRUE);
249
250         str = sr_buildinfo_host_get();
251         sr_dbg("Host: %s.", str);
252         g_free(str);
253
254         str = sr_buildinfo_scpi_backends_get();
255         sr_dbg("SCPI backends: %s.", str);
256         g_free(str);
257 }
258
259 static void print_resourcepaths(void)
260 {
261         GSList *l, *l_orig;
262
263         sr_dbg("Firmware search paths:");
264         l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
265         for (l = l_orig; l; l = l->next)
266                 sr_dbg(" - %s", (const char *)l->data);
267         g_slist_free_full(l_orig, g_free);
268 }
269
270 /**
271  * Sanity-check all libsigrok drivers.
272  *
273  * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
274  *
275  * @retval SR_OK All drivers are OK
276  * @retval SR_ERR One or more drivers have issues.
277  * @retval SR_ERR_ARG Invalid argument.
278  */
279 static int sanity_check_all_drivers(const struct sr_context *ctx)
280 {
281         int i, errors, ret = SR_OK;
282         struct sr_dev_driver **drivers;
283         const char *d;
284
285         if (!ctx)
286                 return SR_ERR_ARG;
287
288         sr_spew("Sanity-checking all drivers.");
289
290         drivers = sr_driver_list(ctx);
291         for (i = 0; drivers[i]; i++) {
292                 errors = 0;
293
294                 d = (drivers[i]->name) ? drivers[i]->name : "NULL";
295
296                 if (!drivers[i]->name) {
297                         sr_err("No name in driver %d ('%s').", i, d);
298                         errors++;
299                 }
300                 if (!drivers[i]->longname) {
301                         sr_err("No longname in driver %d ('%s').", i, d);
302                         errors++;
303                 }
304                 if (drivers[i]->api_version < 1) {
305                         sr_err("API version in driver %d ('%s') < 1.", i, d);
306                         errors++;
307                 }
308                 if (!drivers[i]->init) {
309                         sr_err("No init in driver %d ('%s').", i, d);
310                         errors++;
311                 }
312                 if (!drivers[i]->cleanup) {
313                         sr_err("No cleanup in driver %d ('%s').", i, d);
314                         errors++;
315                 }
316                 if (!drivers[i]->scan) {
317                         sr_err("No scan in driver %d ('%s').", i, d);
318                         errors++;
319                 }
320                 if (!drivers[i]->dev_list) {
321                         sr_err("No dev_list in driver %d ('%s').", i, d);
322                         errors++;
323                 }
324                 if (!drivers[i]->dev_clear) {
325                         sr_err("No dev_clear in driver %d ('%s').", i, d);
326                         errors++;
327                 }
328                 /* Note: config_get() is optional. */
329                 if (!drivers[i]->config_set) {
330                         sr_err("No config_set in driver %d ('%s').", i, d);
331                         errors++;
332                 }
333                 /* Note: config_channel_set() is optional. */
334                 /* Note: config_commit() is optional. */
335                 if (!drivers[i]->config_list) {
336                         sr_err("No config_list in driver %d ('%s').", i, d);
337                         errors++;
338                 }
339                 if (!drivers[i]->dev_open) {
340                         sr_err("No dev_open in driver %d ('%s').", i, d);
341                         errors++;
342                 }
343                 if (!drivers[i]->dev_close) {
344                         sr_err("No dev_close in driver %d ('%s').", i, d);
345                         errors++;
346                 }
347                 if (!drivers[i]->dev_acquisition_start) {
348                         sr_err("No dev_acquisition_start in driver %d ('%s').",
349                                i, d);
350                         errors++;
351                 }
352                 if (!drivers[i]->dev_acquisition_stop) {
353                         sr_err("No dev_acquisition_stop in driver %d ('%s').",
354                                i, d);
355                         errors++;
356                 }
357
358                 /* Note: 'priv' is allowed to be NULL. */
359
360                 if (errors == 0)
361                         continue;
362
363                 ret = SR_ERR;
364         }
365
366         return ret;
367 }
368
369 /**
370  * Sanity-check all libsigrok input modules.
371  *
372  * @retval SR_OK All modules are OK
373  * @retval SR_ERR One or more modules have issues.
374  */
375 static int sanity_check_all_input_modules(void)
376 {
377         int i, errors, ret = SR_OK;
378         const struct sr_input_module **inputs;
379         const char *d;
380
381         sr_spew("Sanity-checking all input modules.");
382
383         inputs = sr_input_list();
384         for (i = 0; inputs[i]; i++) {
385                 errors = 0;
386
387                 d = (inputs[i]->id) ? inputs[i]->id : "NULL";
388
389                 if (!inputs[i]->id) {
390                         sr_err("No ID in module %d ('%s').", i, d);
391                         errors++;
392                 }
393                 if (!inputs[i]->name) {
394                         sr_err("No name in module %d ('%s').", i, d);
395                         errors++;
396                 }
397                 if (!inputs[i]->desc) {
398                         sr_err("No description in module %d ('%s').", i, d);
399                         errors++;
400                 }
401                 if (!inputs[i]->init) {
402                         sr_err("No init in module %d ('%s').", i, d);
403                         errors++;
404                 }
405                 if (!inputs[i]->receive) {
406                         sr_err("No receive in module %d ('%s').", i, d);
407                         errors++;
408                 }
409                 if (!inputs[i]->end) {
410                         sr_err("No end in module %d ('%s').", i, d);
411                         errors++;
412                 }
413
414                 if (errors == 0)
415                         continue;
416
417                 ret = SR_ERR;
418         }
419
420         return ret;
421 }
422
423 /**
424  * Sanity-check all libsigrok output modules.
425  *
426  * @retval SR_OK All modules are OK
427  * @retval SR_ERR One or more modules have issues.
428  */
429 static int sanity_check_all_output_modules(void)
430 {
431         int i, errors, ret = SR_OK;
432         const struct sr_output_module **outputs;
433         const char *d;
434
435         sr_spew("Sanity-checking all output modules.");
436
437         outputs = sr_output_list();
438         for (i = 0; outputs[i]; i++) {
439                 errors = 0;
440
441                 d = (outputs[i]->id) ? outputs[i]->id : "NULL";
442
443                 if (!outputs[i]->id) {
444                         sr_err("No ID in module %d ('%s').", i, d);
445                         errors++;
446                 }
447                 if (!outputs[i]->name) {
448                         sr_err("No name in module %d ('%s').", i, d);
449                         errors++;
450                 }
451                 if (!outputs[i]->desc) {
452                         sr_err("No description in module '%s'.", d);
453                         errors++;
454                 }
455                 if (!outputs[i]->receive) {
456                         sr_err("No receive in module '%s'.", d);
457                         errors++;
458                 }
459
460                 if (errors == 0)
461                         continue;
462
463                 ret = SR_ERR;
464         }
465
466         return ret;
467 }
468
469 /**
470  * Sanity-check all libsigrok transform modules.
471  *
472  * @retval SR_OK All modules are OK
473  * @retval SR_ERR One or more modules have issues.
474  */
475 static int sanity_check_all_transform_modules(void)
476 {
477         int i, errors, ret = SR_OK;
478         const struct sr_transform_module **transforms;
479         const char *d;
480
481         sr_spew("Sanity-checking all transform modules.");
482
483         transforms = sr_transform_list();
484         for (i = 0; transforms[i]; i++) {
485                 errors = 0;
486
487                 d = (transforms[i]->id) ? transforms[i]->id : "NULL";
488
489                 if (!transforms[i]->id) {
490                         sr_err("No ID in module %d ('%s').", i, d);
491                         errors++;
492                 }
493                 if (!transforms[i]->name) {
494                         sr_err("No name in module %d ('%s').", i, d);
495                         errors++;
496                 }
497                 if (!transforms[i]->desc) {
498                         sr_err("No description in module '%s'.", d);
499                         errors++;
500                 }
501                 /* Note: options() is optional. */
502                 /* Note: init() is optional. */
503                 if (!transforms[i]->receive) {
504                         sr_err("No receive in module '%s'.", d);
505                         errors++;
506                 }
507                 /* Note: cleanup() is optional. */
508
509                 if (errors == 0)
510                         continue;
511
512                 ret = SR_ERR;
513         }
514
515         return ret;
516 }
517
518 /**
519  * Initialize libsigrok.
520  *
521  * This function must be called before any other libsigrok function.
522  *
523  * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
524  *            This will be a pointer to a newly allocated libsigrok context
525  *            object upon success, and is undefined upon errors.
526  *
527  * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
528  *         the 'ctx' pointer is undefined and should not be used. Upon success,
529  *         the context will be free'd by sr_exit() as part of the libsigrok
530  *         shutdown.
531  *
532  * @since 0.2.0
533  */
534 SR_API int sr_init(struct sr_context **ctx)
535 {
536         int ret = SR_ERR;
537         struct sr_context *context;
538 #ifdef _WIN32
539         WSADATA wsadata;
540 #endif
541
542         print_versions();
543
544         print_resourcepaths();
545
546         if (!ctx) {
547                 sr_err("%s(): libsigrok context was NULL.", __func__);
548                 return SR_ERR;
549         }
550
551         context = g_malloc0(sizeof(struct sr_context));
552
553         sr_drivers_init(context);
554
555         if (sanity_check_all_drivers(context) < 0) {
556                 sr_err("Internal driver error(s), aborting.");
557                 goto done;
558         }
559
560         if (sanity_check_all_input_modules() < 0) {
561                 sr_err("Internal input module error(s), aborting.");
562                 goto done;
563         }
564
565         if (sanity_check_all_output_modules() < 0) {
566                 sr_err("Internal output module error(s), aborting.");
567                 goto done;
568         }
569
570         if (sanity_check_all_transform_modules() < 0) {
571                 sr_err("Internal transform module error(s), aborting.");
572                 goto done;
573         }
574
575 #ifdef _WIN32
576         if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
577                 sr_err("WSAStartup failed with error code %d.", ret);
578                 ret = SR_ERR;
579                 goto done;
580         }
581 #endif
582
583 #ifdef HAVE_LIBUSB_1_0
584         ret = libusb_init(&context->libusb_ctx);
585         if (LIBUSB_SUCCESS != ret) {
586                 sr_err("libusb_init() returned %s.", libusb_error_name(ret));
587                 ret = SR_ERR;
588                 goto done;
589         }
590 #endif
591         sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
592
593         *ctx = context;
594         context = NULL;
595         ret = SR_OK;
596
597 done:
598         g_free(context);
599         return ret;
600 }
601
602 /**
603  * Shutdown libsigrok.
604  *
605  * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
606  *
607  * @retval SR_OK Success
608  * @retval other Error code SR_ERR, ...
609  *
610  * @since 0.2.0
611  */
612 SR_API int sr_exit(struct sr_context *ctx)
613 {
614         if (!ctx) {
615                 sr_err("%s(): libsigrok context was NULL.", __func__);
616                 return SR_ERR;
617         }
618
619         sr_hw_cleanup_all(ctx);
620
621 #ifdef _WIN32
622         WSACleanup();
623 #endif
624
625 #ifdef HAVE_LIBUSB_1_0
626         libusb_exit(ctx->libusb_ctx);
627 #endif
628
629         g_free(sr_driver_list(ctx));
630         g_free(ctx);
631
632         return SR_OK;
633 }
634
635 /** @} */