Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 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 | |
Marcus Comstedt | f6c7ead | 2014-07-01 00:07:40 +0200 | [diff] [blame] | 21 | #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
| 22 | #include "libsigrokdecode.h" |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 23 | #include "config.h" |
| 24 | #include <glib.h> |
| 25 | |
| 26 | /** @cond PRIVATE */ |
| 27 | |
Bert Vermeulen | ea81b49 | 2013-12-11 18:31:56 +0100 | [diff] [blame] | 28 | /* Python module search paths */ |
| 29 | SRD_PRIV GSList *searchpaths = NULL; |
| 30 | |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 31 | /* session.c */ |
Uwe Hermann | 23a29d2 | 2014-05-04 21:00:24 +0200 | [diff] [blame] | 32 | extern SRD_PRIV GSList *sessions; |
| 33 | extern SRD_PRIV int max_session_id; |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 34 | |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 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 | * |
Uwe Hermann | 3b884e8 | 2014-07-23 00:00:33 +0200 | [diff] [blame] | 65 | * There is one mailing list for sigrok/libsigrokdecode: <a href="https://lists.sourceforge.net/lists/listinfo/sigrok-devel">sigrok-devel</a>. |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 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 | |
| 98 | /** |
| 99 | * Initialize libsigrokdecode. |
| 100 | * |
| 101 | * This initializes the Python interpreter, and creates and initializes |
| 102 | * a "sigrokdecode" Python module. |
| 103 | * |
| 104 | * Then, it searches for sigrok protocol decoders in the "decoders" |
| 105 | * subdirectory of the the libsigrokdecode installation directory. |
| 106 | * All decoders that are found are loaded into memory and added to an |
| 107 | * internal list of decoders, which can be queried via srd_decoder_list(). |
| 108 | * |
| 109 | * The caller is responsible for calling the clean-up function srd_exit(), |
| 110 | * which will properly shut down libsigrokdecode and free its allocated memory. |
| 111 | * |
| 112 | * Multiple calls to srd_init(), without calling srd_exit() in between, |
| 113 | * are not allowed. |
| 114 | * |
| 115 | * @param path Path to an extra directory containing protocol decoders |
| 116 | * which will be added to the Python sys.path. May be NULL. |
| 117 | * |
| 118 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 119 | * Upon Python errors, SRD_ERR_PYTHON is returned. If the decoders |
| 120 | * directory cannot be accessed, SRD_ERR_DECODERS_DIR is returned. |
| 121 | * If not enough memory could be allocated, SRD_ERR_MALLOC is returned. |
| 122 | * |
| 123 | * @since 0.1.0 |
| 124 | */ |
| 125 | SRD_API int srd_init(const char *path) |
| 126 | { |
| 127 | int ret; |
| 128 | char *env_path; |
| 129 | |
| 130 | if (max_session_id != -1) { |
| 131 | srd_err("libsigrokdecode is already initialized."); |
| 132 | return SRD_ERR; |
| 133 | } |
| 134 | |
| 135 | srd_dbg("Initializing libsigrokdecode."); |
| 136 | |
| 137 | /* Add our own module to the list of built-in modules. */ |
| 138 | PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode); |
| 139 | |
| 140 | /* Initialize the Python interpreter. */ |
| 141 | Py_Initialize(); |
| 142 | |
| 143 | /* Installed decoders. */ |
| 144 | if ((ret = srd_decoder_searchpath_add(DECODERS_DIR)) != SRD_OK) { |
| 145 | Py_Finalize(); |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | /* Path specified by the user. */ |
| 150 | if (path) { |
| 151 | if ((ret = srd_decoder_searchpath_add(path)) != SRD_OK) { |
| 152 | Py_Finalize(); |
| 153 | return ret; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* Environment variable overrides everything, for debugging. */ |
| 158 | if ((env_path = getenv("SIGROKDECODE_DIR"))) { |
| 159 | if ((ret = srd_decoder_searchpath_add(env_path)) != SRD_OK) { |
| 160 | Py_Finalize(); |
| 161 | return ret; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | max_session_id = 0; |
| 166 | |
| 167 | return SRD_OK; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Shutdown libsigrokdecode. |
| 172 | * |
| 173 | * This frees all the memory allocated for protocol decoders and shuts down |
| 174 | * the Python interpreter. |
| 175 | * |
| 176 | * This function should only be called if there was a (successful!) invocation |
| 177 | * of srd_init() before. Calling this function multiple times in a row, without |
| 178 | * any successful srd_init() calls in between, is not allowed. |
| 179 | * |
| 180 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 181 | * |
| 182 | * @since 0.1.0 |
| 183 | */ |
| 184 | SRD_API int srd_exit(void) |
| 185 | { |
| 186 | GSList *l; |
| 187 | |
| 188 | srd_dbg("Exiting libsigrokdecode."); |
| 189 | |
| 190 | for (l = sessions; l; l = l->next) |
| 191 | srd_session_destroy((struct srd_session *)l->data); |
| 192 | |
| 193 | srd_decoder_unload_all(); |
Bert Vermeulen | ea81b49 | 2013-12-11 18:31:56 +0100 | [diff] [blame] | 194 | g_slist_free_full(searchpaths, g_free); |
| 195 | searchpaths = NULL; |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 196 | |
| 197 | /* Py_Finalize() returns void, any finalization errors are ignored. */ |
| 198 | Py_Finalize(); |
| 199 | |
| 200 | max_session_id = -1; |
| 201 | |
| 202 | return SRD_OK; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add an additional search directory for the protocol decoders. |
| 207 | * |
| 208 | * The specified directory is prepended (not appended!) to Python's sys.path, |
| 209 | * in order to search for sigrok protocol decoders in the specified |
| 210 | * directories first, and in the generic Python module directories (and in |
| 211 | * the current working directory) last. This avoids conflicts if there are |
| 212 | * Python modules which have the same name as a sigrok protocol decoder in |
| 213 | * sys.path or in the current working directory. |
| 214 | * |
| 215 | * @param path Path to the directory containing protocol decoders which shall |
| 216 | * be added to the Python sys.path, or NULL. |
| 217 | * |
| 218 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 219 | * |
| 220 | * @private |
| 221 | * |
| 222 | * @since 0.1.0 |
| 223 | */ |
| 224 | SRD_PRIV int srd_decoder_searchpath_add(const char *path) |
| 225 | { |
| 226 | PyObject *py_cur_path, *py_item; |
| 227 | GString *new_path; |
| 228 | int wc_len, i; |
| 229 | wchar_t *wc_new_path; |
| 230 | char *item; |
| 231 | |
| 232 | srd_dbg("Adding '%s' to module path.", path); |
| 233 | |
| 234 | new_path = g_string_sized_new(256); |
| 235 | g_string_assign(new_path, path); |
| 236 | py_cur_path = PySys_GetObject("path"); |
| 237 | for (i = 0; i < PyList_Size(py_cur_path); i++) { |
| 238 | g_string_append(new_path, G_SEARCHPATH_SEPARATOR_S); |
| 239 | py_item = PyList_GetItem(py_cur_path, i); |
| 240 | if (!PyUnicode_Check(py_item)) |
| 241 | /* Shouldn't happen. */ |
| 242 | continue; |
| 243 | if (py_str_as_str(py_item, &item) != SRD_OK) |
| 244 | continue; |
| 245 | g_string_append(new_path, item); |
| 246 | g_free(item); |
| 247 | } |
| 248 | |
| 249 | /* Convert to wide chars. */ |
| 250 | wc_len = sizeof(wchar_t) * (new_path->len + 1); |
| 251 | if (!(wc_new_path = g_try_malloc(wc_len))) { |
| 252 | srd_dbg("malloc failed"); |
| 253 | return SRD_ERR_MALLOC; |
| 254 | } |
| 255 | mbstowcs(wc_new_path, new_path->str, wc_len); |
| 256 | PySys_SetPath(wc_new_path); |
| 257 | g_string_free(new_path, TRUE); |
| 258 | g_free(wc_new_path); |
Bert Vermeulen | ea81b49 | 2013-12-11 18:31:56 +0100 | [diff] [blame] | 259 | searchpaths = g_slist_append(searchpaths, g_strdup(path)); |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 260 | |
| 261 | return SRD_OK; |
| 262 | } |
| 263 | |
Bert Vermeulen | 190b71c | 2013-11-16 22:58:28 +0100 | [diff] [blame] | 264 | /** @} */ |