]> sigrok.org Git - libsigrokdecode.git/blame - srd.c
runtc: Make sure to compile against this library
[libsigrokdecode.git] / srd.c
CommitLineData
190b71cf
BV
1/*
2 * This file is part of the libsigrokdecode project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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 "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "libsigrokdecode-internal.h"
23#include "config.h"
24#include <glib.h>
25
26/** @cond PRIVATE */
27
ea81b49a
BV
28/* Python module search paths */
29SRD_PRIV GSList *searchpaths = NULL;
30
190b71cf
BV
31/* session.c */
32extern GSList *sessions;
33extern int max_session_id;
34
35/* decoder.c */
36extern SRD_PRIV GSList *pd_list;
37
38/* module_sigrokdecode.c */
39extern PyMODINIT_FUNC PyInit_sigrokdecode(void);
40
41/** @endcond */
42
43/**
44 * @mainpage libsigrokdecode API
45 *
46 * @section sec_intro Introduction
47 *
48 * The <a href="http://sigrok.org">sigrok</a> project aims at creating a
49 * portable, cross-platform, Free/Libre/Open-Source signal analysis software
50 * suite that supports various device types (such as logic analyzers,
51 * oscilloscopes, multimeters, and more).
52 *
53 * <a href="http://sigrok.org/wiki/Libsigrokdecode">libsigrokdecode</a> is a
54 * shared library written in C which provides the basic API for (streaming)
55 * protocol decoding functionality.
56 *
57 * The <a href="http://sigrok.org/wiki/Protocol_decoders">protocol decoders</a>
58 * are written in Python (>= 3.0).
59 *
60 * @section sec_api API reference
61 *
62 * See the "Modules" page for an introduction to various libsigrokdecode
63 * related topics and the detailed API documentation of the respective
64 * functions.
65 *
66 * You can also browse the API documentation by file, or review all
67 * data structures.
68 *
69 * @section sec_mailinglists Mailing lists
70 *
71 * There are two mailing lists for sigrok/libsigrokdecode: <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>.
72 *
73 * @section sec_irc IRC
74 *
75 * You can find the sigrok developers in the
76 * <a href="irc://chat.freenode.net/sigrok">\#sigrok</a>
77 * IRC channel on Freenode.
78 *
79 * @section sec_website Website
80 *
81 * <a href="http://sigrok.org/wiki/Libsigrokdecode">sigrok.org/wiki/Libsigrokdecode</a>
82 */
83
84/**
85 * @file
86 *
87 * Initializing and shutting down libsigrokdecode.
88 */
89
90/**
91 * @defgroup grp_init Initialization
92 *
93 * Initializing and shutting down libsigrokdecode.
94 *
95 * Before using any of the libsigrokdecode functionality, srd_init() must
96 * be called to initialize the library.
97 *
98 * When libsigrokdecode functionality is no longer needed, srd_exit() should
99 * be called.
100 *
101 * @{
102 */
103
104/**
105 * Initialize libsigrokdecode.
106 *
107 * This initializes the Python interpreter, and creates and initializes
108 * a "sigrokdecode" Python module.
109 *
110 * Then, it searches for sigrok protocol decoders in the "decoders"
111 * subdirectory of the the libsigrokdecode installation directory.
112 * All decoders that are found are loaded into memory and added to an
113 * internal list of decoders, which can be queried via srd_decoder_list().
114 *
115 * The caller is responsible for calling the clean-up function srd_exit(),
116 * which will properly shut down libsigrokdecode and free its allocated memory.
117 *
118 * Multiple calls to srd_init(), without calling srd_exit() in between,
119 * are not allowed.
120 *
121 * @param path Path to an extra directory containing protocol decoders
122 * which will be added to the Python sys.path. May be NULL.
123 *
124 * @return SRD_OK upon success, a (negative) error code otherwise.
125 * Upon Python errors, SRD_ERR_PYTHON is returned. If the decoders
126 * directory cannot be accessed, SRD_ERR_DECODERS_DIR is returned.
127 * If not enough memory could be allocated, SRD_ERR_MALLOC is returned.
128 *
129 * @since 0.1.0
130 */
131SRD_API int srd_init(const char *path)
132{
133 int ret;
134 char *env_path;
135
136 if (max_session_id != -1) {
137 srd_err("libsigrokdecode is already initialized.");
138 return SRD_ERR;
139 }
140
141 srd_dbg("Initializing libsigrokdecode.");
142
143 /* Add our own module to the list of built-in modules. */
144 PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode);
145
146 /* Initialize the Python interpreter. */
147 Py_Initialize();
148
149 /* Installed decoders. */
150 if ((ret = srd_decoder_searchpath_add(DECODERS_DIR)) != SRD_OK) {
151 Py_Finalize();
152 return ret;
153 }
154
155 /* Path specified by the user. */
156 if (path) {
157 if ((ret = srd_decoder_searchpath_add(path)) != SRD_OK) {
158 Py_Finalize();
159 return ret;
160 }
161 }
162
163 /* Environment variable overrides everything, for debugging. */
164 if ((env_path = getenv("SIGROKDECODE_DIR"))) {
165 if ((ret = srd_decoder_searchpath_add(env_path)) != SRD_OK) {
166 Py_Finalize();
167 return ret;
168 }
169 }
170
171 max_session_id = 0;
172
173 return SRD_OK;
174}
175
176/**
177 * Shutdown libsigrokdecode.
178 *
179 * This frees all the memory allocated for protocol decoders and shuts down
180 * the Python interpreter.
181 *
182 * This function should only be called if there was a (successful!) invocation
183 * of srd_init() before. Calling this function multiple times in a row, without
184 * any successful srd_init() calls in between, is not allowed.
185 *
186 * @return SRD_OK upon success, a (negative) error code otherwise.
187 *
188 * @since 0.1.0
189 */
190SRD_API int srd_exit(void)
191{
192 GSList *l;
193
194 srd_dbg("Exiting libsigrokdecode.");
195
196 for (l = sessions; l; l = l->next)
197 srd_session_destroy((struct srd_session *)l->data);
198
199 srd_decoder_unload_all();
ea81b49a
BV
200 g_slist_free_full(searchpaths, g_free);
201 searchpaths = NULL;
190b71cf
BV
202
203 /* Py_Finalize() returns void, any finalization errors are ignored. */
204 Py_Finalize();
205
206 max_session_id = -1;
207
208 return SRD_OK;
209}
210
211/**
212 * Add an additional search directory for the protocol decoders.
213 *
214 * The specified directory is prepended (not appended!) to Python's sys.path,
215 * in order to search for sigrok protocol decoders in the specified
216 * directories first, and in the generic Python module directories (and in
217 * the current working directory) last. This avoids conflicts if there are
218 * Python modules which have the same name as a sigrok protocol decoder in
219 * sys.path or in the current working directory.
220 *
221 * @param path Path to the directory containing protocol decoders which shall
222 * be added to the Python sys.path, or NULL.
223 *
224 * @return SRD_OK upon success, a (negative) error code otherwise.
225 *
226 * @private
227 *
228 * @since 0.1.0
229 */
230SRD_PRIV int srd_decoder_searchpath_add(const char *path)
231{
232 PyObject *py_cur_path, *py_item;
233 GString *new_path;
234 int wc_len, i;
235 wchar_t *wc_new_path;
236 char *item;
237
238 srd_dbg("Adding '%s' to module path.", path);
239
240 new_path = g_string_sized_new(256);
241 g_string_assign(new_path, path);
242 py_cur_path = PySys_GetObject("path");
243 for (i = 0; i < PyList_Size(py_cur_path); i++) {
244 g_string_append(new_path, G_SEARCHPATH_SEPARATOR_S);
245 py_item = PyList_GetItem(py_cur_path, i);
246 if (!PyUnicode_Check(py_item))
247 /* Shouldn't happen. */
248 continue;
249 if (py_str_as_str(py_item, &item) != SRD_OK)
250 continue;
251 g_string_append(new_path, item);
252 g_free(item);
253 }
254
255 /* Convert to wide chars. */
256 wc_len = sizeof(wchar_t) * (new_path->len + 1);
257 if (!(wc_new_path = g_try_malloc(wc_len))) {
258 srd_dbg("malloc failed");
259 return SRD_ERR_MALLOC;
260 }
261 mbstowcs(wc_new_path, new_path->str, wc_len);
262 PySys_SetPath(wc_new_path);
263 g_string_free(new_path, TRUE);
264 g_free(wc_new_path);
ea81b49a 265 searchpaths = g_slist_append(searchpaths, g_strdup(path));
190b71cf
BV
266
267 return SRD_OK;
268}
269
e195c025
BV
270/* @private */
271SRD_PRIV gboolean srd_check_init(void)
272{
273 if (max_session_id < 0) {
274 srd_err("Library is not initialized.");
275 return FALSE;
276 } else
277 return TRUE;
278}
279
190b71cf 280/** @} */