blob: ab668efa800576dacfe8379e64c868b83772dc7c [file] [log] [blame]
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02001/*
Uwe Hermann50985c22013-04-23 22:24:30 +02002 * This file is part of the libsigrok project.
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02003 *
Bert Vermeulenc73d2ea2012-02-13 14:31:51 +01004 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
Peter Stugeb8072702012-10-21 20:23:14 +02005 * Copyright (C) 2012 Peter Stuge <peter@stuge.se>
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02006 *
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 <glib.h>
Uwe Hermann545f9782012-10-24 00:41:21 +020022#include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
Bert Vermeulen45c59c82012-07-05 00:55:07 +020023#include "libsigrok.h"
24#include "libsigrok-internal.h"
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020025
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020026/** @cond PRIVATE */
Martin Ling3544f842013-12-23 03:38:35 +000027#define LOG_PREFIX "backend"
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020028/** @endcond */
Martin Ling3544f842013-12-23 03:38:35 +000029
Bert Vermeulenfa931542013-09-02 14:23:42 +020030extern struct sr_session *session;
31
Uwe Hermanncd009d52011-02-22 18:13:32 +010032/**
Uwe Hermann5b30cca2012-10-19 10:06:45 +020033 * @mainpage libsigrok API
34 *
35 * @section sec_intro Introduction
36 *
37 * The <a href="http://sigrok.org">sigrok</a> project aims at creating a
38 * portable, cross-platform, Free/Libre/Open-Source signal analysis software
39 * suite that supports various device types (such as logic analyzers,
40 * oscilloscopes, multimeters, and more).
41 *
42 * <a href="http://sigrok.org/wiki/Libsigrok">libsigrok</a> is a shared
43 * library written in C which provides the basic API for talking to
44 * <a href="http://sigrok.org/wiki/Supported_hardware">supported hardware</a>
45 * and reading/writing the acquired data into various
46 * <a href="http://sigrok.org/wiki/Input_output_formats">input/output
47 * file formats</a>.
48 *
Uwe Hermannf21193f2012-10-21 16:36:23 +020049 * @section sec_api API reference
Uwe Hermann5b30cca2012-10-19 10:06:45 +020050 *
Uwe Hermannf21193f2012-10-21 16:36:23 +020051 * See the "Modules" page for an introduction to various libsigrok
52 * related topics and the detailed API documentation of the respective
53 * functions.
54 *
55 * You can also browse the API documentation by file, or review all
56 * data structures.
Uwe Hermann5b30cca2012-10-19 10:06:45 +020057 *
58 * @section sec_mailinglists Mailing lists
59 *
60 * There are two mailing lists for sigrok/libsigrok: <a href="https://lists.sourceforge.net/lists/listinfo/sigrok-devel">sigrok-devel</a> and <a href="https://lists.sourceforge.net/lists/listinfo/sigrok-commits">sigrok-commits</a>.
61 *
62 * @section sec_irc IRC
63 *
64 * You can find the sigrok developers in the
65 * <a href="irc://chat.freenode.net/sigrok">\#sigrok</a>
66 * IRC channel on Freenode.
67 *
68 * @section sec_website Website
69 *
70 * <a href="http://sigrok.org/wiki/Libsigrok">sigrok.org/wiki/Libsigrok</a>
71 */
72
73/**
Uwe Hermann393fb9c2012-10-22 00:30:12 +020074 * @file
75 *
76 * Initializing and shutting down libsigrok.
77 */
78
79/**
Uwe Hermann7b870c32012-10-21 16:13:36 +020080 * @defgroup grp_init Initialization
81 *
82 * Initializing and shutting down libsigrok.
83 *
Matthias Heidbrinkd0a92ab2014-05-19 19:44:04 +020084 * Before using any of the libsigrok functionality (except
85 * sr_log_loglevel_set() and sr_log_opts_set()), sr_init() must
Uwe Hermannafe2f282012-10-22 02:32:53 +020086 * be called to initialize the library, which will return a struct sr_context
87 * when the initialization was successful.
88 *
89 * When libsigrok functionality is no longer needed, sr_exit() should be
90 * called, which will (among other things) free the struct sr_context.
91 *
92 * Example for a minimal program using libsigrok:
93 *
94 * @code{.c}
95 * #include <stdio.h>
96 * #include <libsigrok/libsigrok.h>
97 *
98 * int main(int argc, char **argv)
99 * {
100 * int ret;
101 * struct sr_context *sr_ctx;
102 *
103 * if ((ret = sr_init(&sr_ctx)) != SR_OK) {
Uwe Hermanndf823ac2013-12-27 16:18:28 +0100104 * printf("Error initializing libsigrok (%s): %s.\n",
Uwe Hermannafe2f282012-10-22 02:32:53 +0200105 * sr_strerror_name(ret), sr_strerror(ret));
106 * return 1;
107 * }
108 *
109 * // Use libsigrok functions here...
110 *
111 * if ((ret = sr_exit(sr_ctx)) != SR_OK) {
Uwe Hermanndf823ac2013-12-27 16:18:28 +0100112 * printf("Error shutting down libsigrok (%s): %s.\n",
Uwe Hermannafe2f282012-10-22 02:32:53 +0200113 * sr_strerror_name(ret), sr_strerror(ret));
114 * return 1;
115 * }
116 *
117 * return 0;
118 * }
119 * @endcode
120 *
Uwe Hermann7b870c32012-10-21 16:13:36 +0200121 * @{
122 */
123
124/**
Uwe Hermann55a6daf2012-11-11 23:54:49 +0100125 * Sanity-check all libsigrok drivers.
126 *
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100127 * @retval SR_OK All drivers are OK
128 * @retval SR_ERR One or more drivers have issues.
Uwe Hermann55a6daf2012-11-11 23:54:49 +0100129 */
130static int sanity_check_all_drivers(void)
131{
132 int i, errors, ret = SR_OK;
133 struct sr_dev_driver **drivers;
134 const char *d;
135
136 sr_spew("Sanity-checking all drivers.");
137
138 drivers = sr_driver_list();
139 for (i = 0; drivers[i]; i++) {
140 errors = 0;
141
142 d = (drivers[i]->name) ? drivers[i]->name : "NULL";
143
144 if (!drivers[i]->name) {
145 sr_err("No name in driver %d ('%s').", i, d);
146 errors++;
147 }
148 if (!drivers[i]->longname) {
149 sr_err("No longname in driver %d ('%s').", i, d);
150 errors++;
151 }
152 if (drivers[i]->api_version < 1) {
153 sr_err("API version in driver %d ('%s') < 1.", i, d);
154 errors++;
155 }
156 if (!drivers[i]->init) {
157 sr_err("No init in driver %d ('%s').", i, d);
158 errors++;
159 }
160 if (!drivers[i]->cleanup) {
161 sr_err("No cleanup in driver %d ('%s').", i, d);
162 errors++;
163 }
164 if (!drivers[i]->scan) {
165 sr_err("No scan in driver %d ('%s').", i, d);
166 errors++;
167 }
168 if (!drivers[i]->dev_list) {
169 sr_err("No dev_list in driver %d ('%s').", i, d);
170 errors++;
171 }
Uwe Hermann6fab7b82013-02-06 22:35:24 +0100172 /* Note: config_get() is optional. */
173 if (!drivers[i]->config_set) {
174 sr_err("No config_set in driver %d ('%s').", i, d);
175 errors++;
176 }
177 if (!drivers[i]->config_list) {
178 sr_err("No config_list in driver %d ('%s').", i, d);
179 errors++;
180 }
Uwe Hermann55a6daf2012-11-11 23:54:49 +0100181 if (!drivers[i]->dev_open) {
182 sr_err("No dev_open in driver %d ('%s').", i, d);
183 errors++;
184 }
185 if (!drivers[i]->dev_close) {
186 sr_err("No dev_close in driver %d ('%s').", i, d);
187 errors++;
188 }
Uwe Hermann55a6daf2012-11-11 23:54:49 +0100189 if (!drivers[i]->dev_acquisition_start) {
190 sr_err("No dev_acquisition_start in driver %d ('%s').",
191 i, d);
192 errors++;
193 }
194 if (!drivers[i]->dev_acquisition_stop) {
195 sr_err("No dev_acquisition_stop in driver %d ('%s').",
196 i, d);
197 errors++;
198 }
199
200 /* Note: 'priv' is allowed to be NULL. */
201
202 if (errors == 0)
203 continue;
204
205 ret = SR_ERR;
206 }
207
208 return ret;
209}
210
211/**
Uwe Hermann218e6292013-03-10 17:13:15 +0100212 * Sanity-check all libsigrok input modules.
213 *
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100214 * @retval SR_OK All modules are OK
215 * @retval SR_ERR One or more modules have issues.
Uwe Hermann218e6292013-03-10 17:13:15 +0100216 */
217static int sanity_check_all_input_modules(void)
218{
219 int i, errors, ret = SR_OK;
220 struct sr_input_format **inputs;
221 const char *d;
222
223 sr_spew("Sanity-checking all input modules.");
224
225 inputs = sr_input_list();
226 for (i = 0; inputs[i]; i++) {
227 errors = 0;
228
229 d = (inputs[i]->id) ? inputs[i]->id : "NULL";
230
231 if (!inputs[i]->id) {
232 sr_err("No ID in module %d ('%s').", i, d);
233 errors++;
234 }
235 if (!inputs[i]->description) {
236 sr_err("No description in module %d ('%s').", i, d);
237 errors++;
238 }
239 if (!inputs[i]->format_match) {
240 sr_err("No format_match in module %d ('%s').", i, d);
241 errors++;
242 }
243 if (!inputs[i]->init) {
244 sr_err("No init in module %d ('%s').", i, d);
245 errors++;
246 }
247 if (!inputs[i]->loadfile) {
248 sr_err("No loadfile in module %d ('%s').", i, d);
249 errors++;
250 }
251
252 if (errors == 0)
253 continue;
254
255 ret = SR_ERR;
256 }
257
258 return ret;
259}
260
261/**
262 * Sanity-check all libsigrok output modules.
263 *
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100264 * @retval SR_OK All modules are OK
265 * @retval SR_ERR One or more modules have issues.
Uwe Hermann218e6292013-03-10 17:13:15 +0100266 */
267static int sanity_check_all_output_modules(void)
268{
269 int i, errors, ret = SR_OK;
270 struct sr_output_format **outputs;
271 const char *d;
272
273 sr_spew("Sanity-checking all output modules.");
274
275 outputs = sr_output_list();
276 for (i = 0; outputs[i]; i++) {
277 errors = 0;
278
279 d = (outputs[i]->id) ? outputs[i]->id : "NULL";
280
281 if (!outputs[i]->id) {
282 sr_err("No ID in module %d ('%s').", i, d);
283 errors++;
284 }
285 if (!outputs[i]->description) {
Bert Vermeulen44559b22014-04-19 13:28:17 +0200286 sr_err("No description in module '%s'.", d);
Uwe Hermann218e6292013-03-10 17:13:15 +0100287 errors++;
288 }
Bert Vermeulen44559b22014-04-19 13:28:17 +0200289 if (!outputs[i]->receive) {
290 sr_err("No receive in module '%s'.", d);
Uwe Hermann218e6292013-03-10 17:13:15 +0100291 errors++;
292 }
293
Uwe Hermann218e6292013-03-10 17:13:15 +0100294 if (errors == 0)
295 continue;
296
297 ret = SR_ERR;
298 }
299
300 return ret;
301}
302
303/**
Uwe Hermanncd009d52011-02-22 18:13:32 +0100304 * Initialize libsigrok.
305 *
Uwe Hermannc46762a2012-10-21 22:40:43 +0200306 * This function must be called before any other libsigrok function.
307 *
308 * @param ctx Pointer to a libsigrok context struct pointer. Must not be NULL.
309 * This will be a pointer to a newly allocated libsigrok context
310 * object upon success, and is undefined upon errors.
311 *
312 * @return SR_OK upon success, a (negative) error code otherwise. Upon errors
313 * the 'ctx' pointer is undefined and should not be used. Upon success,
314 * the context will be free'd by sr_exit() as part of the libsigrok
315 * shutdown.
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200316 *
Uwe Hermann53f05fa2013-11-03 16:08:38 +0100317 * @since 0.2.0
Uwe Hermanncd009d52011-02-22 18:13:32 +0100318 */
Peter Stugeb8072702012-10-21 20:23:14 +0200319SR_API int sr_init(struct sr_context **ctx)
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200320{
Peter Stugeb8072702012-10-21 20:23:14 +0200321 int ret = SR_ERR;
322 struct sr_context *context;
323
Uwe Hermannc46762a2012-10-21 22:40:43 +0200324 if (!ctx) {
325 sr_err("%s(): libsigrok context was NULL.", __func__);
326 return SR_ERR;
327 }
328
Uwe Hermann55a6daf2012-11-11 23:54:49 +0100329 if (sanity_check_all_drivers() < 0) {
330 sr_err("Internal driver error(s), aborting.");
331 return ret;
332 }
333
Uwe Hermann218e6292013-03-10 17:13:15 +0100334 if (sanity_check_all_input_modules() < 0) {
335 sr_err("Internal input module error(s), aborting.");
336 return ret;
337 }
338
339 if (sanity_check_all_output_modules() < 0) {
340 sr_err("Internal output module error(s), aborting.");
341 return ret;
342 }
343
Peter Stugeb8072702012-10-21 20:23:14 +0200344 /* + 1 to handle when struct sr_context has no members. */
345 context = g_try_malloc0(sizeof(struct sr_context) + 1);
346
347 if (!context) {
348 ret = SR_ERR_MALLOC;
349 goto done;
350 }
351
Peter Stuge785b9ff2012-10-21 20:23:36 +0200352#ifdef HAVE_LIBUSB_1_0
353 ret = libusb_init(&context->libusb_ctx);
Uwe Hermann8e2d43c2012-10-22 10:17:38 +0200354 if (LIBUSB_SUCCESS != ret) {
Uwe Hermann9d122af2013-12-10 18:18:58 +0100355 sr_err("libusb_init() returned %s.", libusb_error_name(ret));
Peter Stuge63c07e42012-11-03 08:29:26 +0100356 ret = SR_ERR;
Peter Stuge785b9ff2012-10-21 20:23:36 +0200357 goto done;
358 }
359#endif
360
Peter Stugeb8072702012-10-21 20:23:14 +0200361 *ctx = context;
Peter Stuge123d97b2012-11-03 08:27:48 +0100362 context = NULL;
Bert Vermeulenfa931542013-09-02 14:23:42 +0200363 session = NULL;
Peter Stugeb8072702012-10-21 20:23:14 +0200364 ret = SR_OK;
365
366done:
Peter Stuge123d97b2012-11-03 08:27:48 +0100367 if (context)
368 g_free(context);
Peter Stugeb8072702012-10-21 20:23:14 +0200369 return ret;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200370}
371
Uwe Hermanncd009d52011-02-22 18:13:32 +0100372/**
373 * Shutdown libsigrok.
374 *
Uwe Hermannc46762a2012-10-21 22:40:43 +0200375 * @param ctx Pointer to a libsigrok context struct. Must not be NULL.
376 *
Matthias Heidbrink04cb9152013-11-22 20:40:52 +0100377 * @retval SR_OK Success
378 * @retval other Error code SR_ERR, ...
Uwe Hermann9fb5f2d2013-04-13 18:58:11 +0200379 *
Uwe Hermann53f05fa2013-11-03 16:08:38 +0100380 * @since 0.2.0
Uwe Hermanncd009d52011-02-22 18:13:32 +0100381 */
Peter Stugeb8072702012-10-21 20:23:14 +0200382SR_API int sr_exit(struct sr_context *ctx)
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200383{
Uwe Hermannc46762a2012-10-21 22:40:43 +0200384 if (!ctx) {
385 sr_err("%s(): libsigrok context was NULL.", __func__);
386 return SR_ERR;
387 }
388
Bert Vermeulen93a04e32012-02-15 03:18:48 +0100389 sr_hw_cleanup_all();
Uwe Hermanncd009d52011-02-22 18:13:32 +0100390
Peter Stuge785b9ff2012-10-21 20:23:36 +0200391#ifdef HAVE_LIBUSB_1_0
392 libusb_exit(ctx->libusb_ctx);
393#endif
394
Peter Stugeb8072702012-10-21 20:23:14 +0200395 g_free(ctx);
396
Uwe Hermanncd009d52011-02-22 18:13:32 +0100397 return SR_OK;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200398}
Uwe Hermann7b870c32012-10-21 16:13:36 +0200399
400/** @} */