]> sigrok.org Git - libsigrok.git/blob - src/backend.c
e829912e914d071dcebf3aa148b01f0bc6c52f1b
[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 static void print_versions(void)
126 {
127         GString *s;
128 #if defined(HAVE_LIBUSB_1_0) && !defined(__FreeBSD__)
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);
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());
148 #endif
149 #ifdef HAVE_LIBUSB_1_0
150 #ifdef __FreeBSD__
151         g_string_append_printf(s, "libusb-1.0 %s, ", CONF_LIBUSB_1_0_VERSION);
152 #else
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
157 #endif
158 #ifdef HAVE_LIBFTDI
159         g_string_append_printf(s, "libftdi %s, ", CONF_LIBFTDI_VERSION);
160 #endif
161 #ifdef HAVE_LIBGPIB
162         g_string_append_printf(s, "libgpib %s, ", CONF_LIBGPIB_VERSION);
163 #endif
164 #ifdef HAVE_LIBREVISA
165         g_string_append_printf(s, "librevisa %s, ", CONF_LIBREVISA_VERSION);
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);
172         g_string_append_printf(s, "Host: %s, ", CONF_HOST);
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
206 /**
207  * Sanity-check all libsigrok drivers.
208  *
209  * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
210  *
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.
214  */
215 static int sanity_check_all_drivers(const struct sr_context *ctx)
216 {
217         int i, errors, ret = SR_OK;
218         struct sr_dev_driver **drivers;
219         const char *d;
220
221         if (!ctx)
222                 return SR_ERR_ARG;
223
224         sr_spew("Sanity-checking all drivers.");
225
226         drivers = sr_driver_list(ctx);
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                 }
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);
264                         errors++;
265                 }
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);
270                         errors++;
271                 }
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                 }
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
302 /**
303  * Sanity-check all libsigrok input modules.
304  *
305  * @retval SR_OK All modules are OK
306  * @retval SR_ERR One or more modules have issues.
307  */
308 static int sanity_check_all_input_modules(void)
309 {
310         int i, errors, ret = SR_OK;
311         const struct sr_input_module **inputs;
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                 }
326                 if (!inputs[i]->name) {
327                         sr_err("No name in module %d ('%s').", i, d);
328                         errors++;
329                 }
330                 if (!inputs[i]->desc) {
331                         sr_err("No description in module %d ('%s').", i, d);
332                         errors++;
333                 }
334                 if (!inputs[i]->init) {
335                         sr_err("No init in module %d ('%s').", i, d);
336                         errors++;
337                 }
338                 if (!inputs[i]->receive) {
339                         sr_err("No receive in module %d ('%s').", i, d);
340                         errors++;
341                 }
342                 if (!inputs[i]->end) {
343                         sr_err("No end in module %d ('%s').", i, d);
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  *
359  * @retval SR_OK All modules are OK
360  * @retval SR_ERR One or more modules have issues.
361  */
362 static int sanity_check_all_output_modules(void)
363 {
364         int i, errors, ret = SR_OK;
365         const struct sr_output_module **outputs;
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                 }
380                 if (!outputs[i]->name) {
381                         sr_err("No name in module %d ('%s').", i, d);
382                         errors++;
383                 }
384                 if (!outputs[i]->desc) {
385                         sr_err("No description in module '%s'.", d);
386                         errors++;
387                 }
388                 if (!outputs[i]->receive) {
389                         sr_err("No receive in module '%s'.", d);
390                         errors++;
391                 }
392
393                 if (errors == 0)
394                         continue;
395
396                 ret = SR_ERR;
397         }
398
399         return ret;
400 }
401
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  */
408 static 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
451 /**
452  * Initialize libsigrok.
453  *
454  * This function must be called before any other libsigrok function.
455  *
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.
459  *
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
463  *         shutdown.
464  *
465  * @since 0.2.0
466  */
467 SR_API int sr_init(struct sr_context **ctx)
468 {
469         int ret = SR_ERR;
470         struct sr_context *context;
471         struct sr_dev_driver ***lists, **drivers;
472         GArray *array;
473 #ifdef _WIN32
474         WSADATA wsadata;
475 #endif
476
477         print_versions();
478
479         if (!ctx) {
480                 sr_err("%s(): libsigrok context was NULL.", __func__);
481                 return SR_ERR;
482         }
483
484         context = g_malloc0(sizeof(struct sr_context));
485
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);
493
494         if (sanity_check_all_drivers(context) < 0) {
495                 sr_err("Internal driver error(s), aborting.");
496                 return ret;
497         }
498
499         if (sanity_check_all_input_modules() < 0) {
500                 sr_err("Internal input module error(s), aborting.");
501                 return ret;
502         }
503
504         if (sanity_check_all_output_modules() < 0) {
505                 sr_err("Internal output module error(s), aborting.");
506                 return ret;
507         }
508
509         if (sanity_check_all_transform_modules() < 0) {
510                 sr_err("Internal transform module error(s), aborting.");
511                 return ret;
512         }
513
514 #ifdef _WIN32
515         if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
516                 sr_err("WSAStartup failed with error code %d.", ret);
517                 ret = SR_ERR;
518                 goto done;
519         }
520 #endif
521
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));
526                 ret = SR_ERR;
527                 goto done;
528         }
529 #endif
530         sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
531
532         *ctx = context;
533         context = NULL;
534         ret = SR_OK;
535
536 #if defined(HAVE_LIBUSB_1_0) || defined(_WIN32)
537 done:
538 #endif
539         g_free(context);
540         return ret;
541 }
542
543 /**
544  * Shutdown libsigrok.
545  *
546  * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
547  *
548  * @retval SR_OK Success
549  * @retval other Error code SR_ERR, ...
550  *
551  * @since 0.2.0
552  */
553 SR_API int sr_exit(struct sr_context *ctx)
554 {
555         if (!ctx) {
556                 sr_err("%s(): libsigrok context was NULL.", __func__);
557                 return SR_ERR;
558         }
559
560         sr_hw_cleanup_all(ctx);
561
562 #ifdef _WIN32
563         WSACleanup();
564 #endif
565
566 #ifdef HAVE_LIBUSB_1_0
567         libusb_exit(ctx->libusb_ctx);
568 #endif
569
570         g_free(sr_driver_list(ctx));
571         g_free(ctx);
572
573         return SR_OK;
574 }
575
576 /** @} */