]> sigrok.org Git - libsigrok.git/blame - src/backend.c
README.devices: the PRINT button must get pressed for Metrix MX56C
[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
42be2adb 125SR_API GSList *sr_buildinfo_libs_get(void)
bd7b83cf 126{
42be2adb 127 GSList *l = NULL, *m = NULL;
731c01f2 128#if defined(HAVE_LIBUSB_1_0) && !defined(__FreeBSD__)
bd7b83cf
UH
129 const struct libusb_version *lv;
130#endif
131
42be2adb
UH
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)",
bd7b83cf
UH
134 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
135 glib_major_version, glib_minor_version, glib_micro_version,
42be2adb
UH
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
bd7b83cf 143#ifdef HAVE_LIBSERIALPORT
42be2adb
UH
144 m = g_slist_append(NULL, g_strdup("libserialport"));
145 m = g_slist_append(m, g_strdup_printf("%s/%s (rt: %s/%s)",
bd7b83cf 146 SP_PACKAGE_VERSION_STRING, SP_LIB_VERSION_STRING,
42be2adb
UH
147 sp_get_package_version_string(), sp_get_lib_version_string()));
148 l = g_slist_append(l, m);
bd7b83cf
UH
149#endif
150#ifdef HAVE_LIBUSB_1_0
42be2adb 151 m = g_slist_append(NULL, g_strdup("libusb-1.0"));
731c01f2 152#ifdef __FreeBSD__
42be2adb 153 m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBUSB_1_0_VERSION));
731c01f2 154#else
bd7b83cf 155 lv = libusb_get_version();
42be2adb
UH
156 m = g_slist_append(m, g_strdup_printf("%d.%d.%d.%d%s",
157 lv->major, lv->minor, lv->micro, lv->nano, lv->rc));
bd7b83cf 158#endif
42be2adb 159 l = g_slist_append(l, m);
731c01f2 160#endif
bd7b83cf 161#ifdef HAVE_LIBFTDI
42be2adb
UH
162 m = g_slist_append(NULL, g_strdup("libftdi"));
163 m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBFTDI_VERSION));
164 l = g_slist_append(l, m);
bd7b83cf
UH
165#endif
166#ifdef HAVE_LIBGPIB
42be2adb
UH
167 m = g_slist_append(NULL, g_strdup("libgpib"));
168 m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBGPIB_VERSION));
169 l = g_slist_append(l, m);
bd7b83cf
UH
170#endif
171#ifdef HAVE_LIBREVISA
42be2adb
UH
172 m = g_slist_append(NULL, g_strdup("librevisa"));
173 m = g_slist_append(m, g_strdup_printf("%s", CONF_LIBREVISA_VERSION));
174 l = g_slist_append(l, m);
bd7b83cf 175#endif
bd7b83cf 176
42be2adb
UH
177 return l;
178}
179
180SR_API char *sr_buildinfo_host_get(void)
181{
182 return g_strdup_printf("%s, %s-endian", CONF_HOST,
bd7b83cf 183#ifdef WORDS_BIGENDIAN
42be2adb 184 "big"
bd7b83cf 185#else
42be2adb 186 "little"
bd7b83cf 187#endif
42be2adb
UH
188 );
189}
bd7b83cf 190
42be2adb
UH
191SR_API char *sr_buildinfo_scpi_backends_get(void)
192{
193 GString *s;
194 char *str;
195
196 s = g_string_sized_new(200);
bd7b83cf
UH
197
198 g_string_append_printf(s, "TCP, ");
199#if HAVE_RPC
200 g_string_append_printf(s, "RPC, ");
201#endif
202#ifdef HAVE_LIBSERIALPORT
203 g_string_append_printf(s, "serial, ");
204#endif
205#ifdef HAVE_LIBREVISA
206 g_string_append_printf(s, "VISA, ");
207#endif
208#ifdef HAVE_LIBGPIB
209 g_string_append_printf(s, "GPIB, ");
210#endif
211#ifdef HAVE_LIBUSB_1_0
212 g_string_append_printf(s, "USBTMC, ");
213#endif
42be2adb
UH
214 s->str[s->len - 2] = '\0';
215
216 str = g_strdup(s->str);
217 g_string_free(s, TRUE);
218
219 return str;
220}
221
222static void print_versions(void)
223{
224 GString *s;
225 GSList *l, *l_orig, *m;
226 char *str;
227 const char *lib, *version;
228
229 sr_dbg("libsigrok %s/%s (rt: %s/%s).",
230 SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
231 sr_package_version_string_get(), sr_lib_version_string_get());
232
233 s = g_string_sized_new(200);
234 g_string_append(s, "Libs: ");
235 l_orig = sr_buildinfo_libs_get();
236 for (l = l_orig; l; l = l->next) {
237 m = l->data;
238 lib = m->data;
239 version = m->next->data;
240 g_string_append_printf(s, "%s %s, ", lib, version);
241 g_slist_free_full(m, g_free);
242 }
243 g_slist_free(l_orig);
bd7b83cf
UH
244 s->str[s->len - 2] = '.';
245 s->str[s->len - 1] = '\0';
246 sr_dbg("%s", s->str);
bd7b83cf 247 g_string_free(s, TRUE);
42be2adb
UH
248
249 str = sr_buildinfo_host_get();
250 sr_dbg("Host: %s.", str);
251 g_free(str);
252
253 str = sr_buildinfo_scpi_backends_get();
254 sr_dbg("SCPI backends: %s.", str);
255 g_free(str);
bd7b83cf
UH
256}
257
55a6daf5
UH
258/**
259 * Sanity-check all libsigrok drivers.
260 *
032da34b
UH
261 * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
262 *
04cb9157
MH
263 * @retval SR_OK All drivers are OK
264 * @retval SR_ERR One or more drivers have issues.
032da34b 265 * @retval SR_ERR_ARG Invalid argument.
55a6daf5 266 */
032da34b 267static int sanity_check_all_drivers(const struct sr_context *ctx)
55a6daf5
UH
268{
269 int i, errors, ret = SR_OK;
270 struct sr_dev_driver **drivers;
271 const char *d;
272
032da34b
UH
273 if (!ctx)
274 return SR_ERR_ARG;
275
55a6daf5
UH
276 sr_spew("Sanity-checking all drivers.");
277
032da34b 278 drivers = sr_driver_list(ctx);
55a6daf5
UH
279 for (i = 0; drivers[i]; i++) {
280 errors = 0;
281
282 d = (drivers[i]->name) ? drivers[i]->name : "NULL";
283
284 if (!drivers[i]->name) {
285 sr_err("No name in driver %d ('%s').", i, d);
286 errors++;
287 }
288 if (!drivers[i]->longname) {
289 sr_err("No longname in driver %d ('%s').", i, d);
290 errors++;
291 }
292 if (drivers[i]->api_version < 1) {
293 sr_err("API version in driver %d ('%s') < 1.", i, d);
294 errors++;
295 }
296 if (!drivers[i]->init) {
297 sr_err("No init in driver %d ('%s').", i, d);
298 errors++;
299 }
300 if (!drivers[i]->cleanup) {
301 sr_err("No cleanup in driver %d ('%s').", i, d);
302 errors++;
303 }
304 if (!drivers[i]->scan) {
305 sr_err("No scan in driver %d ('%s').", i, d);
306 errors++;
307 }
308 if (!drivers[i]->dev_list) {
309 sr_err("No dev_list in driver %d ('%s').", i, d);
310 errors++;
311 }
f778bf02
UH
312 if (!drivers[i]->dev_clear) {
313 sr_err("No dev_clear in driver %d ('%s').", i, d);
314 errors++;
315 }
6fab7b8f
UH
316 /* Note: config_get() is optional. */
317 if (!drivers[i]->config_set) {
318 sr_err("No config_set in driver %d ('%s').", i, d);
319 errors++;
320 }
797f1a0c
UH
321 /* Note: config_channel_set() is optional. */
322 /* Note: config_commit() is optional. */
6fab7b8f
UH
323 if (!drivers[i]->config_list) {
324 sr_err("No config_list in driver %d ('%s').", i, d);
325 errors++;
326 }
55a6daf5
UH
327 if (!drivers[i]->dev_open) {
328 sr_err("No dev_open in driver %d ('%s').", i, d);
329 errors++;
330 }
331 if (!drivers[i]->dev_close) {
332 sr_err("No dev_close in driver %d ('%s').", i, d);
333 errors++;
334 }
55a6daf5
UH
335 if (!drivers[i]->dev_acquisition_start) {
336 sr_err("No dev_acquisition_start in driver %d ('%s').",
337 i, d);
338 errors++;
339 }
340 if (!drivers[i]->dev_acquisition_stop) {
341 sr_err("No dev_acquisition_stop in driver %d ('%s').",
342 i, d);
343 errors++;
344 }
345
346 /* Note: 'priv' is allowed to be NULL. */
347
348 if (errors == 0)
349 continue;
350
351 ret = SR_ERR;
352 }
353
354 return ret;
355}
356
218e629f
UH
357/**
358 * Sanity-check all libsigrok input modules.
359 *
04cb9157
MH
360 * @retval SR_OK All modules are OK
361 * @retval SR_ERR One or more modules have issues.
218e629f
UH
362 */
363static int sanity_check_all_input_modules(void)
364{
365 int i, errors, ret = SR_OK;
17bfaca6 366 const struct sr_input_module **inputs;
218e629f
UH
367 const char *d;
368
369 sr_spew("Sanity-checking all input modules.");
370
371 inputs = sr_input_list();
372 for (i = 0; inputs[i]; i++) {
373 errors = 0;
374
375 d = (inputs[i]->id) ? inputs[i]->id : "NULL";
376
377 if (!inputs[i]->id) {
378 sr_err("No ID in module %d ('%s').", i, d);
379 errors++;
380 }
17bfaca6
BV
381 if (!inputs[i]->name) {
382 sr_err("No name in module %d ('%s').", i, d);
383 errors++;
384 }
385 if (!inputs[i]->desc) {
218e629f
UH
386 sr_err("No description in module %d ('%s').", i, d);
387 errors++;
388 }
218e629f
UH
389 if (!inputs[i]->init) {
390 sr_err("No init in module %d ('%s').", i, d);
391 errors++;
392 }
17bfaca6
BV
393 if (!inputs[i]->receive) {
394 sr_err("No receive in module %d ('%s').", i, d);
7066fd46
BV
395 errors++;
396 }
397 if (!inputs[i]->end) {
398 sr_err("No end in module %d ('%s').", i, d);
218e629f
UH
399 errors++;
400 }
401
402 if (errors == 0)
403 continue;
404
405 ret = SR_ERR;
406 }
407
408 return ret;
409}
410
411/**
412 * Sanity-check all libsigrok output modules.
413 *
04cb9157
MH
414 * @retval SR_OK All modules are OK
415 * @retval SR_ERR One or more modules have issues.
218e629f
UH
416 */
417static int sanity_check_all_output_modules(void)
418{
419 int i, errors, ret = SR_OK;
a755b0e1 420 const struct sr_output_module **outputs;
218e629f
UH
421 const char *d;
422
423 sr_spew("Sanity-checking all output modules.");
424
425 outputs = sr_output_list();
426 for (i = 0; outputs[i]; i++) {
427 errors = 0;
428
429 d = (outputs[i]->id) ? outputs[i]->id : "NULL";
430
431 if (!outputs[i]->id) {
432 sr_err("No ID in module %d ('%s').", i, d);
433 errors++;
434 }
a755b0e1
BV
435 if (!outputs[i]->name) {
436 sr_err("No name in module %d ('%s').", i, d);
437 errors++;
438 }
439 if (!outputs[i]->desc) {
44559b2c 440 sr_err("No description in module '%s'.", d);
218e629f
UH
441 errors++;
442 }
44559b2c
BV
443 if (!outputs[i]->receive) {
444 sr_err("No receive in module '%s'.", d);
218e629f
UH
445 errors++;
446 }
447
218e629f
UH
448 if (errors == 0)
449 continue;
450
451 ret = SR_ERR;
452 }
453
454 return ret;
455}
456
187cfc60
UH
457/**
458 * Sanity-check all libsigrok transform modules.
459 *
460 * @retval SR_OK All modules are OK
461 * @retval SR_ERR One or more modules have issues.
462 */
463static int sanity_check_all_transform_modules(void)
464{
465 int i, errors, ret = SR_OK;
466 const struct sr_transform_module **transforms;
467 const char *d;
468
469 sr_spew("Sanity-checking all transform modules.");
470
471 transforms = sr_transform_list();
472 for (i = 0; transforms[i]; i++) {
473 errors = 0;
474
475 d = (transforms[i]->id) ? transforms[i]->id : "NULL";
476
477 if (!transforms[i]->id) {
478 sr_err("No ID in module %d ('%s').", i, d);
479 errors++;
480 }
481 if (!transforms[i]->name) {
482 sr_err("No name in module %d ('%s').", i, d);
483 errors++;
484 }
485 if (!transforms[i]->desc) {
486 sr_err("No description in module '%s'.", d);
487 errors++;
488 }
489 /* Note: options() is optional. */
490 /* Note: init() is optional. */
491 if (!transforms[i]->receive) {
492 sr_err("No receive in module '%s'.", d);
493 errors++;
494 }
495 /* Note: cleanup() is optional. */
496
497 if (errors == 0)
498 continue;
499
500 ret = SR_ERR;
501 }
502
503 return ret;
504}
505
cd009d55
UH
506/**
507 * Initialize libsigrok.
508 *
c46762a2
UH
509 * This function must be called before any other libsigrok function.
510 *
511 * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
512 * This will be a pointer to a newly allocated libsigrok context
513 * object upon success, and is undefined upon errors.
514 *
515 * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
516 * the 'ctx' pointer is undefined and should not be used. Upon success,
517 * the context will be free'd by sr_exit() as part of the libsigrok
518 * shutdown.
9fb5f2df 519 *
53f05fa8 520 * @since 0.2.0
cd009d55 521 */
b8072700 522SR_API int sr_init(struct sr_context **ctx)
a1bb33af 523{
b8072700
PS
524 int ret = SR_ERR;
525 struct sr_context *context;
fa69e191
ML
526#ifdef _WIN32
527 WSADATA wsadata;
528#endif
b8072700 529
bd7b83cf
UH
530 print_versions();
531
c46762a2
UH
532 if (!ctx) {
533 sr_err("%s(): libsigrok context was NULL.", __func__);
534 return SR_ERR;
535 }
536
032da34b
UH
537 context = g_malloc0(sizeof(struct sr_context));
538
5d8b3913 539 sr_drivers_init(context);
032da34b
UH
540
541 if (sanity_check_all_drivers(context) < 0) {
55a6daf5
UH
542 sr_err("Internal driver error(s), aborting.");
543 return ret;
544 }
545
218e629f
UH
546 if (sanity_check_all_input_modules() < 0) {
547 sr_err("Internal input module error(s), aborting.");
548 return ret;
549 }
550
551 if (sanity_check_all_output_modules() < 0) {
552 sr_err("Internal output module error(s), aborting.");
553 return ret;
554 }
555
187cfc60
UH
556 if (sanity_check_all_transform_modules() < 0) {
557 sr_err("Internal transform module error(s), aborting.");
558 return ret;
559 }
560
fa69e191
ML
561#ifdef _WIN32
562 if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
563 sr_err("WSAStartup failed with error code %d.", ret);
564 ret = SR_ERR;
565 goto done;
566 }
567#endif
568
785b9ff2
PS
569#ifdef HAVE_LIBUSB_1_0
570 ret = libusb_init(&context->libusb_ctx);
8e2d43cc 571 if (LIBUSB_SUCCESS != ret) {
9d122af8 572 sr_err("libusb_init() returned %s.", libusb_error_name(ret));
63c07e48 573 ret = SR_ERR;
785b9ff2
PS
574 goto done;
575 }
576#endif
bee24666 577 sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
785b9ff2 578
b8072700 579 *ctx = context;
123d97b1 580 context = NULL;
b8072700
PS
581 ret = SR_OK;
582
fa69e191 583#if defined(HAVE_LIBUSB_1_0) || defined(_WIN32)
b8072700 584done:
468665df 585#endif
b1f83103 586 g_free(context);
b8072700 587 return ret;
a1bb33af
UH
588}
589
cd009d55
UH
590/**
591 * Shutdown libsigrok.
592 *
c46762a2
UH
593 * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
594 *
04cb9157
MH
595 * @retval SR_OK Success
596 * @retval other Error code SR_ERR, ...
9fb5f2df 597 *
53f05fa8 598 * @since 0.2.0
cd009d55 599 */
b8072700 600SR_API int sr_exit(struct sr_context *ctx)
a1bb33af 601{
c46762a2
UH
602 if (!ctx) {
603 sr_err("%s(): libsigrok context was NULL.", __func__);
604 return SR_ERR;
605 }
606
032da34b 607 sr_hw_cleanup_all(ctx);
cd009d55 608
fa69e191
ML
609#ifdef _WIN32
610 WSACleanup();
611#endif
612
785b9ff2
PS
613#ifdef HAVE_LIBUSB_1_0
614 libusb_exit(ctx->libusb_ctx);
615#endif
616
032da34b 617 g_free(sr_driver_list(ctx));
b8072700
PS
618 g_free(ctx);
619
cd009d55 620 return SR_OK;
a1bb33af 621}
7b870c38
UH
622
623/** @} */