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