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