libsigrok  0.4.0
sigrok hardware access and backend library
backend.c
Go to the documentation of this file.
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).",
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: config_get() is optional. */
261  if (!drivers[i]->config_set) {
262  sr_err("No config_set in driver %d ('%s').", i, d);
263  errors++;
264  }
265  if (!drivers[i]->config_list) {
266  sr_err("No config_list in driver %d ('%s').", i, d);
267  errors++;
268  }
269  if (!drivers[i]->dev_open) {
270  sr_err("No dev_open in driver %d ('%s').", i, d);
271  errors++;
272  }
273  if (!drivers[i]->dev_close) {
274  sr_err("No dev_close in driver %d ('%s').", i, d);
275  errors++;
276  }
277  if (!drivers[i]->dev_acquisition_start) {
278  sr_err("No dev_acquisition_start in driver %d ('%s').",
279  i, d);
280  errors++;
281  }
282  if (!drivers[i]->dev_acquisition_stop) {
283  sr_err("No dev_acquisition_stop in driver %d ('%s').",
284  i, d);
285  errors++;
286  }
287 
288  /* Note: 'priv' is allowed to be NULL. */
289 
290  if (errors == 0)
291  continue;
292 
293  ret = SR_ERR;
294  }
295 
296  return ret;
297 }
298 
299 /**
300  * Sanity-check all libsigrok input modules.
301  *
302  * @retval SR_OK All modules are OK
303  * @retval SR_ERR One or more modules have issues.
304  */
305 static int sanity_check_all_input_modules(void)
306 {
307  int i, errors, ret = SR_OK;
308  const struct sr_input_module **inputs;
309  const char *d;
310 
311  sr_spew("Sanity-checking all input modules.");
312 
313  inputs = sr_input_list();
314  for (i = 0; inputs[i]; i++) {
315  errors = 0;
316 
317  d = (inputs[i]->id) ? inputs[i]->id : "NULL";
318 
319  if (!inputs[i]->id) {
320  sr_err("No ID in module %d ('%s').", i, d);
321  errors++;
322  }
323  if (!inputs[i]->name) {
324  sr_err("No name in module %d ('%s').", i, d);
325  errors++;
326  }
327  if (!inputs[i]->desc) {
328  sr_err("No description in module %d ('%s').", i, d);
329  errors++;
330  }
331  if (!inputs[i]->init) {
332  sr_err("No init in module %d ('%s').", i, d);
333  errors++;
334  }
335  if (!inputs[i]->receive) {
336  sr_err("No receive in module %d ('%s').", i, d);
337  errors++;
338  }
339  if (!inputs[i]->end) {
340  sr_err("No end in module %d ('%s').", i, d);
341  errors++;
342  }
343 
344  if (errors == 0)
345  continue;
346 
347  ret = SR_ERR;
348  }
349 
350  return ret;
351 }
352 
353 /**
354  * Sanity-check all libsigrok output modules.
355  *
356  * @retval SR_OK All modules are OK
357  * @retval SR_ERR One or more modules have issues.
358  */
359 static int sanity_check_all_output_modules(void)
360 {
361  int i, errors, ret = SR_OK;
362  const struct sr_output_module **outputs;
363  const char *d;
364 
365  sr_spew("Sanity-checking all output modules.");
366 
367  outputs = sr_output_list();
368  for (i = 0; outputs[i]; i++) {
369  errors = 0;
370 
371  d = (outputs[i]->id) ? outputs[i]->id : "NULL";
372 
373  if (!outputs[i]->id) {
374  sr_err("No ID in module %d ('%s').", i, d);
375  errors++;
376  }
377  if (!outputs[i]->name) {
378  sr_err("No name in module %d ('%s').", i, d);
379  errors++;
380  }
381  if (!outputs[i]->desc) {
382  sr_err("No description in module '%s'.", d);
383  errors++;
384  }
385  if (!outputs[i]->receive) {
386  sr_err("No receive in module '%s'.", d);
387  errors++;
388  }
389 
390  if (errors == 0)
391  continue;
392 
393  ret = SR_ERR;
394  }
395 
396  return ret;
397 }
398 
399 /**
400  * Sanity-check all libsigrok transform modules.
401  *
402  * @retval SR_OK All modules are OK
403  * @retval SR_ERR One or more modules have issues.
404  */
405 static int sanity_check_all_transform_modules(void)
406 {
407  int i, errors, ret = SR_OK;
408  const struct sr_transform_module **transforms;
409  const char *d;
410 
411  sr_spew("Sanity-checking all transform modules.");
412 
413  transforms = sr_transform_list();
414  for (i = 0; transforms[i]; i++) {
415  errors = 0;
416 
417  d = (transforms[i]->id) ? transforms[i]->id : "NULL";
418 
419  if (!transforms[i]->id) {
420  sr_err("No ID in module %d ('%s').", i, d);
421  errors++;
422  }
423  if (!transforms[i]->name) {
424  sr_err("No name in module %d ('%s').", i, d);
425  errors++;
426  }
427  if (!transforms[i]->desc) {
428  sr_err("No description in module '%s'.", d);
429  errors++;
430  }
431  /* Note: options() is optional. */
432  /* Note: init() is optional. */
433  if (!transforms[i]->receive) {
434  sr_err("No receive in module '%s'.", d);
435  errors++;
436  }
437  /* Note: cleanup() is optional. */
438 
439  if (errors == 0)
440  continue;
441 
442  ret = SR_ERR;
443  }
444 
445  return ret;
446 }
447 
448 /**
449  * Initialize libsigrok.
450  *
451  * This function must be called before any other libsigrok function.
452  *
453  * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
454  * This will be a pointer to a newly allocated libsigrok context
455  * object upon success, and is undefined upon errors.
456  *
457  * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
458  * the 'ctx' pointer is undefined and should not be used. Upon success,
459  * the context will be free'd by sr_exit() as part of the libsigrok
460  * shutdown.
461  *
462  * @since 0.2.0
463  */
464 SR_API int sr_init(struct sr_context **ctx)
465 {
466  int ret = SR_ERR;
467  struct sr_context *context;
468  struct sr_dev_driver ***lists, **drivers;
469  GArray *array;
470 #ifdef _WIN32
471  WSADATA wsadata;
472 #endif
473 
474  print_versions();
475 
476  if (!ctx) {
477  sr_err("%s(): libsigrok context was NULL.", __func__);
478  return SR_ERR;
479  }
480 
481  context = g_malloc0(sizeof(struct sr_context));
482 
483  /* Generate ctx->driver_list at runtime. */
484  array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
485  for (lists = drivers_lists; *lists; lists++)
486  for (drivers = *lists; *drivers; drivers++)
487  g_array_append_val(array, *drivers);
488  context->driver_list = (struct sr_dev_driver **)array->data;
489  g_array_free(array, FALSE);
490 
491  if (sanity_check_all_drivers(context) < 0) {
492  sr_err("Internal driver error(s), aborting.");
493  return ret;
494  }
495 
496  if (sanity_check_all_input_modules() < 0) {
497  sr_err("Internal input module error(s), aborting.");
498  return ret;
499  }
500 
501  if (sanity_check_all_output_modules() < 0) {
502  sr_err("Internal output module error(s), aborting.");
503  return ret;
504  }
505 
506  if (sanity_check_all_transform_modules() < 0) {
507  sr_err("Internal transform module error(s), aborting.");
508  return ret;
509  }
510 
511 #ifdef _WIN32
512  if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
513  sr_err("WSAStartup failed with error code %d.", ret);
514  ret = SR_ERR;
515  goto done;
516  }
517 #endif
518 
519 #ifdef HAVE_LIBUSB_1_0
520  ret = libusb_init(&context->libusb_ctx);
521  if (LIBUSB_SUCCESS != ret) {
522  sr_err("libusb_init() returned %s.", libusb_error_name(ret));
523  ret = SR_ERR;
524  goto done;
525  }
526 #endif
527  sr_resource_set_hooks(context, NULL, NULL, NULL, NULL);
528 
529  *ctx = context;
530  context = NULL;
531  ret = SR_OK;
532 
533 #if defined(HAVE_LIBUSB_1_0) || defined(_WIN32)
534 done:
535 #endif
536  g_free(context);
537  return ret;
538 }
539 
540 /**
541  * Shutdown libsigrok.
542  *
543  * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
544  *
545  * @retval SR_OK Success
546  * @retval other Error code SR_ERR, ...
547  *
548  * @since 0.2.0
549  */
550 SR_API int sr_exit(struct sr_context *ctx)
551 {
552  if (!ctx) {
553  sr_err("%s(): libsigrok context was NULL.", __func__);
554  return SR_ERR;
555  }
556 
557  sr_hw_cleanup_all(ctx);
558 
559 #ifdef _WIN32
560  WSACleanup();
561 #endif
562 
563 #ifdef HAVE_LIBUSB_1_0
564  libusb_exit(ctx->libusb_ctx);
565 #endif
566 
567  g_free(sr_driver_list(ctx));
568  g_free(ctx);
569 
570  return SR_OK;
571 }
572 
573 /** @} */
int sr_resource_set_hooks(struct sr_context *ctx, sr_resource_open_callback open_cb, sr_resource_close_callback close_cb, sr_resource_read_callback read_cb, void *cb_data)
Install resource access hooks.
Definition: resource.c:217
The public libsigrok header file to be used by frontends.
const struct sr_transform_module ** sr_transform_list(void)
Returns a NULL-terminated list of all available transform modules.
Definition: transform.c:62
int sr_exit(struct sr_context *ctx)
Shutdown libsigrok.
Definition: backend.c:550
Device driver data.
Definition: libsigrok.h:1090
GSList *(* scan)(struct sr_dev_driver *driver, GSList *options)
Scan for devices.
Definition: libsigrok.h:1109
Generic/unspecified error.
Definition: libsigrok.h:68
int(* dev_acquisition_stop)(struct sr_dev_inst *sdi, void *cb_data)
End data acquisition on the specified device.
Definition: libsigrok.h:1151
#define SR_PACKAGE_VERSION_STRING
The libsigrok package version ("major.minor.micro") as string.
Definition: version.h:50
int api_version
API version (currently 1).
Definition: libsigrok.h:1097
const char * sr_package_version_string_get(void)
Get the libsigrok package version number as a string.
Definition: version.c:94
int(* dev_acquisition_start)(const struct sr_dev_inst *sdi, void *cb_data)
Begin data acquisition on the specified device.
Definition: libsigrok.h:1148
#define SR_API
Definition: libsigrok.h:121
No error.
Definition: libsigrok.h:67
#define SR_LIB_VERSION_STRING
The libsigrok libtool version ("current:revision:age") as string.
Definition: version.h:66
const char * name
Driver name.
Definition: libsigrok.h:1093
const char * longname
Long name.
Definition: libsigrok.h:1095
const char * sr_lib_version_string_get(void)
Get the libsigrok library version number as a string.
Definition: version.c:143
GSList *(* dev_list)(const struct sr_dev_driver *driver)
Get list of device instances the driver knows about.
Definition: libsigrok.h:1114
int(* cleanup)(const struct sr_dev_driver *driver)
Called before driver is unloaded.
Definition: libsigrok.h:1102
const struct sr_output_module ** sr_output_list(void)
Returns a NULL-terminated list of all available output modules.
Definition: output.c:91
int(* config_set)(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
Set value of a configuration key in driver or a given device instance.
Definition: libsigrok.h:1125
Opaque structure representing a libsigrok context.
int(* init)(struct sr_dev_driver *driver, struct sr_context *sr_ctx)
Called when driver is loaded, e.g.
Definition: libsigrok.h:1099
void * context
Device driver context, considered private.
Definition: libsigrok.h:1156
int(* dev_close)(struct sr_dev_inst *sdi)
Close device.
Definition: libsigrok.h:1146
const struct sr_input_module ** sr_input_list(void)
Returns a NULL-terminated list of all available input modules.
Definition: input.c:87
struct sr_dev_driver ** sr_driver_list(const struct sr_context *ctx)
Return the list of supported hardware drivers.
Definition: hwdriver.c:343
int(* config_list)(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
List all possible values for a configuration key in a device instance.
Definition: libsigrok.h:1138
int(* dev_open)(struct sr_dev_inst *sdi)
Open device.
Definition: libsigrok.h:1144
int sr_init(struct sr_context **ctx)
Initialize libsigrok.
Definition: backend.c:464
Function argument error.
Definition: libsigrok.h:70