Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the sigrok project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 5 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 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 | |
Uwe Hermann | 7319141 | 2012-01-03 19:56:01 +0100 | [diff] [blame] | 21 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 22 | #include "sigrokdecode-internal.h" |
| 23 | #include "config.h" |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 24 | #include <glib.h> |
Bert Vermeulen | 1aef2f9 | 2011-12-15 03:31:31 +0100 | [diff] [blame] | 25 | #include <inttypes.h> |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 26 | #include <stdlib.h> |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 27 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 28 | |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 29 | static GSList *di_list = NULL; |
Bert Vermeulen | 983cb0f | 2012-01-08 03:20:12 +0100 | [diff] [blame] | 30 | static GSList *callbacks = NULL; |
| 31 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 32 | /* lives in decoder.c */ |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 33 | extern GSList *pd_list; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 34 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 35 | /* lives in module_sigrokdecode.c */ |
| 36 | extern PyMODINIT_FUNC PyInit_sigrokdecode(void); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 37 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 38 | /* lives in type_logic.c */ |
| 39 | extern PyTypeObject srd_logic_type; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 40 | |
| 41 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 42 | /** |
| 43 | * Initialize libsigrokdecode. |
| 44 | * |
| 45 | * This initializes the Python interpreter, and creates and initializes |
| 46 | * a "sigrok" Python module with a single put() method. |
| 47 | * |
| 48 | * Then, it searches for sigrok protocol decoder files (*.py) in the |
| 49 | * "decoders" subdirectory of the the sigrok installation directory. |
| 50 | * All decoders that are found are loaded into memory and added to an |
| 51 | * internal list of decoders, which can be queried via srd_list_decoders(). |
| 52 | * |
| 53 | * The caller is responsible for calling the clean-up function srd_exit(), |
| 54 | * which will properly shut down libsigrokdecode and free its allocated memory. |
| 55 | * |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 56 | * Multiple calls to srd_init(), without calling srd_exit() in between, |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 57 | * are not allowed. |
| 58 | * |
| 59 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 60 | * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders |
| 61 | * directory cannot be accessed, return SRD_ERR_DECODERS_DIR. |
| 62 | * If not enough memory could be allocated, return SRD_ERR_MALLOC. |
| 63 | */ |
| 64 | int srd_init(void) |
| 65 | { |
| 66 | int ret; |
| 67 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 68 | PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode); |
Bert Vermeulen | 5f802ec | 2011-12-27 22:15:53 +0100 | [diff] [blame] | 69 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 70 | /* Py_Initialize() returns void and usually cannot fail. */ |
| 71 | Py_Initialize(); |
| 72 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 73 | if ((ret = set_modulepath()) != SRD_OK) { |
| 74 | Py_Finalize(); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | if ((ret = srd_load_all_decoders()) != SRD_OK) { |
| 79 | Py_Finalize(); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | return SRD_OK; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Shutdown libsigrokdecode. |
| 89 | * |
| 90 | * This frees all the memory allocated for protocol decoders and shuts down |
| 91 | * the Python interpreter. |
| 92 | * |
| 93 | * This function should only be called if there was a (successful!) invocation |
| 94 | * of srd_init() before. Calling this function multiple times in a row, without |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 95 | * any successful srd_init() calls in between, is not allowed. |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 96 | * |
| 97 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 98 | */ |
| 99 | int srd_exit(void) |
| 100 | { |
| 101 | /* Unload/free all decoders, and then the list of decoders itself. */ |
| 102 | /* TODO: Error handling. */ |
| 103 | srd_unload_all_decoders(); |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 104 | g_slist_free(pd_list); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 105 | |
| 106 | /* Py_Finalize() returns void, any finalization errors are ignored. */ |
| 107 | Py_Finalize(); |
| 108 | |
| 109 | return SRD_OK; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Add search directories for the protocol decoders. |
| 115 | * |
| 116 | * TODO: add path from env var SIGROKDECODE_PATH, config etc |
| 117 | */ |
| 118 | int set_modulepath(void) |
| 119 | { |
| 120 | int ret; |
| 121 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 122 | PyRun_SimpleString("import sys"); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 123 | ret = PyRun_SimpleString("sys.path.append(r'" DECODERS_DIR "');"); |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 129 | /** |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 130 | * Set options in a decoder instance. |
| 131 | * |
| 132 | * @param di Decoder instance. |
| 133 | * @param options A GHashTable of options to set. |
| 134 | * |
| 135 | * Handled options are removed from the hash. |
| 136 | * |
| 137 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 138 | */ |
| 139 | int srd_instance_set_options(struct srd_decoder_instance *di, |
| 140 | GHashTable *options) |
| 141 | { |
| 142 | PyObject *py_dec_options, *py_dec_optkeys, *py_di_options, *py_optval; |
| 143 | PyObject *py_optlist, *py_classval; |
| 144 | Py_UNICODE *py_ustr; |
| 145 | unsigned long long int val_ull; |
| 146 | int num_optkeys, ret, size, i; |
| 147 | char *key, *value; |
| 148 | |
Bert Vermeulen | e431d9c | 2012-01-18 22:59:14 +0100 | [diff] [blame^] | 149 | if(!PyObject_HasAttrString(di->decoder->py_dec, "options")) { |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 150 | /* Decoder has no options. */ |
Bert Vermeulen | e431d9c | 2012-01-18 22:59:14 +0100 | [diff] [blame^] | 151 | if (g_hash_table_size(options) == 0) { |
| 152 | /* No options provided. */ |
| 153 | return SRD_OK; |
| 154 | } else { |
| 155 | srd_err("Protocol decoder has no options."); |
| 156 | return SRD_ERR_ARG; |
| 157 | } |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 158 | return SRD_OK; |
Bert Vermeulen | e431d9c | 2012-01-18 22:59:14 +0100 | [diff] [blame^] | 159 | } |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 160 | |
| 161 | ret = SRD_ERR_PYTHON; |
| 162 | key = NULL; |
| 163 | py_dec_options = py_dec_optkeys = py_di_options = py_optval = NULL; |
| 164 | py_optlist = py_classval = NULL; |
| 165 | py_dec_options = PyObject_GetAttrString(di->decoder->py_dec, "options"); |
| 166 | if (!PyDict_Check(py_dec_options)) { |
| 167 | srd_err("Protocol decoder %s options is not a dictionary.", |
| 168 | di->decoder->name); |
| 169 | goto err_out; |
| 170 | } |
| 171 | |
| 172 | /* All of these are synthesized objects, so they're good. */ |
| 173 | py_dec_optkeys = PyDict_Keys(py_dec_options); |
| 174 | num_optkeys = PyList_Size(py_dec_optkeys); |
| 175 | if (!(py_di_options = PyObject_GetAttrString(di->py_instance, "options"))) |
| 176 | goto err_out; |
| 177 | for (i = 0; i < num_optkeys; i++) { |
| 178 | /* Get the default class value for this option. */ |
| 179 | py_str_as_str(PyList_GetItem(py_dec_optkeys, i), &key); |
| 180 | if (!(py_optlist = PyDict_GetItemString(py_dec_options, key))) |
| 181 | goto err_out; |
| 182 | if (!(py_classval = PyList_GetItem(py_optlist, 1))) |
| 183 | goto err_out; |
| 184 | |
| 185 | if ((value = g_hash_table_lookup(options, key))) { |
| 186 | /* An override for this option was provided. */ |
| 187 | if (PyUnicode_Check(py_classval)) { |
| 188 | if (!(py_optval = PyUnicode_FromString(value))) { |
| 189 | /* Some UTF-8 encoding error. */ |
| 190 | PyErr_Clear(); |
| 191 | goto err_out; |
| 192 | } |
| 193 | } else if (PyLong_Check(py_classval)) { |
| 194 | if (!(py_optval = PyLong_FromString(value, NULL, 0))) { |
| 195 | /* ValueError Exception */ |
| 196 | PyErr_Clear(); |
| 197 | srd_err("Option %s has invalid value %s: expected integer", |
| 198 | key, value); |
| 199 | goto err_out; |
| 200 | } |
| 201 | } |
| 202 | g_hash_table_remove(options, key); |
| 203 | } else { |
| 204 | /* Use the class default for this option. */ |
| 205 | if (PyUnicode_Check(py_classval)) { |
| 206 | /* Make a brand new copy of the string. */ |
| 207 | py_ustr = PyUnicode_AS_UNICODE(py_classval); |
| 208 | size = PyUnicode_GET_SIZE(py_classval); |
| 209 | py_optval = PyUnicode_FromUnicode(py_ustr, size); |
| 210 | } else if (PyLong_Check(py_classval)) { |
| 211 | /* Make a brand new copy of the integer. */ |
| 212 | val_ull = PyLong_AsUnsignedLongLong(py_classval); |
| 213 | if (val_ull == (unsigned long long)-1) { |
| 214 | /* OverFlowError exception */ |
| 215 | PyErr_Clear(); |
| 216 | srd_err("Invalid integer value for %s: expected integer", key); |
| 217 | goto err_out; |
| 218 | } |
| 219 | if (!(py_optval = PyLong_FromUnsignedLongLong(val_ull))) |
| 220 | goto err_out; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* If we got here, py_optval holds a known good new reference |
| 225 | * to the instance option to set. |
| 226 | */ |
| 227 | if (PyDict_SetItemString(py_di_options, key, py_optval) == -1) |
| 228 | goto err_out; |
| 229 | } |
| 230 | |
| 231 | ret = SRD_OK; |
| 232 | |
| 233 | err_out: |
| 234 | Py_XDECREF(py_optlist); |
| 235 | Py_XDECREF(py_di_options); |
| 236 | Py_XDECREF(py_dec_optkeys); |
| 237 | Py_XDECREF(py_dec_options); |
| 238 | if (key) |
| 239 | g_free(key); |
| 240 | if (PyErr_Occurred()) { |
| 241 | srd_dbg("stray exception!"); |
| 242 | PyErr_Print(); |
| 243 | PyErr_Clear(); |
| 244 | } |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Set probes in a decoder instance. |
| 251 | * |
| 252 | * @param di Decoder instance. |
| 253 | * @param probes A GHashTable of probes to set. Key is probe name, value is |
| 254 | * the probe number. Samples passed to this instance will be arranged in this |
| 255 | * order. |
| 256 | * |
| 257 | * Handled probes are removed from the hash. |
| 258 | * |
| 259 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 260 | */ |
| 261 | int srd_instance_set_probes(struct srd_decoder_instance *di, |
| 262 | GHashTable *probes) |
| 263 | { |
| 264 | int ret; |
| 265 | |
| 266 | if (g_hash_table_size(probes) == 0) |
| 267 | /* No probes provided. */ |
| 268 | return SRD_OK; |
| 269 | |
| 270 | if(!PyObject_HasAttrString(di->decoder->py_dec, "probes")) |
| 271 | /* Decoder has no probes. */ |
| 272 | return SRD_OK; |
| 273 | |
| 274 | ret = SRD_ERR_PYTHON; |
| 275 | |
| 276 | /* TODO */ |
| 277 | if (g_hash_table_size(probes) > 0) { |
| 278 | srd_err("Setting probes is not yet supported."); |
| 279 | return SRD_ERR_PYTHON; |
| 280 | } |
| 281 | |
| 282 | ret = SRD_OK; |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | /** |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 288 | * Create a new protocol decoder instance. |
| 289 | * |
Uwe Hermann | 42a85ed | 2012-01-10 20:00:25 +0100 | [diff] [blame] | 290 | * @param id Decoder 'id' field. |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 291 | * @param options GHashtable of options which override the defaults set in |
| 292 | * the decoder class. |
Uwe Hermann | 42a85ed | 2012-01-10 20:00:25 +0100 | [diff] [blame] | 293 | * @return Pointer to a newly allocated struct srd_decoder_instance, or |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 294 | * NULL in case of failure. |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 295 | */ |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 296 | struct srd_decoder_instance *srd_instance_new(const char *decoder_id, |
| 297 | GHashTable *options) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 298 | { |
| 299 | struct srd_decoder *dec; |
| 300 | struct srd_decoder_instance *di; |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 301 | char *instance_id; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 302 | |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 303 | srd_dbg("%s: creating new %s instance", __func__, decoder_id); |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 304 | |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 305 | if (!(dec = srd_get_decoder_by_id(decoder_id))) { |
| 306 | srd_err("Protocol decoder %s not found.", decoder_id); |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 309 | |
| 310 | if (!(di = g_try_malloc0(sizeof(*di)))) { |
| 311 | srd_err("Failed to malloc instance."); |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | instance_id = g_hash_table_lookup(options, "id"); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 316 | di->decoder = dec; |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 317 | di->instance_id = g_strdup(instance_id ? instance_id : decoder_id); |
| 318 | g_hash_table_remove(options, "id"); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 319 | |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 320 | /* Create a new instance of this decoder class. */ |
| 321 | if (!(di->py_instance = PyObject_CallObject(dec->py_dec, NULL))) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 322 | if (PyErr_Occurred()) |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 323 | PyErr_Print(); |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 324 | g_free(di); |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 325 | return NULL; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 326 | } |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 327 | |
| 328 | /* Instance takes input from a frontend by default. */ |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 329 | di_list = g_slist_append(di_list, di); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 330 | |
Bert Vermeulen | 0bdadba | 2012-01-17 03:37:34 +0100 | [diff] [blame] | 331 | if (srd_instance_set_options(di, options) != SRD_OK) { |
| 332 | di_list = g_slist_remove(di_list, di); |
| 333 | g_free(di); |
| 334 | return NULL; |
| 335 | } |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 336 | |
| 337 | return di; |
| 338 | } |
| 339 | |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 340 | int srd_instance_stack(struct srd_decoder_instance *di_from, |
| 341 | struct srd_decoder_instance *di_to) |
| 342 | { |
| 343 | |
| 344 | if (!di_from || !di_to) { |
| 345 | srd_err("invalid from/to instance pair"); |
| 346 | return SRD_ERR_ARG; |
| 347 | } |
| 348 | |
| 349 | if (!g_slist_find(di_list, di_from)) { |
| 350 | srd_err("unstacked instance not found"); |
| 351 | return SRD_ERR_ARG; |
| 352 | } |
| 353 | |
| 354 | /* Remove from the unstacked list. */ |
| 355 | di_list = g_slist_remove(di_list, di_to); |
| 356 | |
| 357 | /* Stack on top of source di. */ |
| 358 | di_from->next_di = g_slist_append(di_from->next_di, di_to); |
| 359 | |
| 360 | return SRD_OK; |
| 361 | } |
| 362 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 363 | |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 364 | /* TODO: this should go into the PD stack */ |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 365 | struct srd_decoder_instance *srd_instance_find(char *instance_id) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 366 | { |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 367 | GSList *l; |
| 368 | struct srd_decoder_instance *tmp, *di; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 369 | |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 370 | di = NULL; |
| 371 | for (l = di_list; l; l = l->next) { |
| 372 | tmp = l->data; |
| 373 | if (!strcmp(tmp->instance_id, instance_id)) { |
| 374 | di = tmp; |
| 375 | break; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 376 | } |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 379 | return di; |
| 380 | } |
| 381 | |
| 382 | int srd_instance_start(struct srd_decoder_instance *di, PyObject *args) |
| 383 | { |
| 384 | PyObject *py_name, *py_res; |
| 385 | |
| 386 | srd_dbg("calling start() method on protocol decoder instance %s", di->instance_id); |
| 387 | |
| 388 | if (!(py_name = PyUnicode_FromString("start"))) { |
| 389 | srd_err("unable to build python object for 'start'"); |
| 390 | if (PyErr_Occurred()) |
| 391 | PyErr_Print(); |
| 392 | return SRD_ERR_PYTHON; |
| 393 | } |
| 394 | |
| 395 | if (!(py_res = PyObject_CallMethodObjArgs(di->py_instance, |
| 396 | py_name, args, NULL))) { |
| 397 | if (PyErr_Occurred()) |
| 398 | PyErr_Print(); |
| 399 | return SRD_ERR_PYTHON; |
| 400 | } |
| 401 | |
| 402 | Py_XDECREF(py_res); |
| 403 | Py_DECREF(py_name); |
| 404 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 405 | return SRD_OK; |
| 406 | } |
| 407 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 408 | /** |
| 409 | * Run the specified decoder function. |
| 410 | * |
| 411 | * @param dec TODO |
| 412 | * @param inbuf TODO |
| 413 | * @param inbuflen TODO |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 414 | * |
| 415 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 416 | */ |
Bert Vermeulen | 8652829 | 2012-01-15 23:20:39 +0100 | [diff] [blame] | 417 | int srd_instance_decode(uint64_t start_samplenum, |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 418 | struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 419 | { |
| 420 | PyObject *py_instance, *py_res; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 421 | srd_logic *logic; |
Bert Vermeulen | 8652829 | 2012-01-15 23:20:39 +0100 | [diff] [blame] | 422 | uint64_t end_samplenum; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 423 | |
| 424 | /* Return an error upon unusable input. */ |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 425 | if (di == NULL) |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 426 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 427 | if (inbuf == NULL) |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 428 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 429 | if (inbuflen == 0) /* No point in working on empty buffers. */ |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 430 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 431 | |
| 432 | /* TODO: Error handling. */ |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 433 | py_instance = di->py_instance; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 434 | Py_XINCREF(py_instance); |
| 435 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 436 | logic = PyObject_New(srd_logic, &srd_logic_type); |
| 437 | Py_INCREF(logic); |
| 438 | logic->di = di; |
Bert Vermeulen | 8652829 | 2012-01-15 23:20:39 +0100 | [diff] [blame] | 439 | logic->start_samplenum = start_samplenum; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 440 | logic->itercnt = 0; |
| 441 | logic->inbuf = inbuf; |
| 442 | logic->inbuflen = inbuflen; |
| 443 | logic->sample = PyList_New(2); |
| 444 | Py_INCREF(logic->sample); |
| 445 | |
Bert Vermeulen | 8652829 | 2012-01-15 23:20:39 +0100 | [diff] [blame] | 446 | end_samplenum = start_samplenum + inbuflen / di->unitsize; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 447 | if (!(py_res = PyObject_CallMethod(py_instance, "decode", |
Bert Vermeulen | 8652829 | 2012-01-15 23:20:39 +0100 | [diff] [blame] | 448 | "KKO", logic->start_samplenum, end_samplenum, logic))) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 449 | if (PyErr_Occurred()) |
| 450 | PyErr_Print(); /* Returns void. */ |
| 451 | |
| 452 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
| 453 | } |
| 454 | |
| 455 | Py_XDECREF(py_res); |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 456 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 457 | return SRD_OK; |
| 458 | } |
| 459 | |
| 460 | |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 461 | int srd_session_start(int num_probes, int unitsize, uint64_t samplerate) |
| 462 | { |
| 463 | PyObject *args; |
| 464 | GSList *d, *s; |
| 465 | struct srd_decoder_instance *di; |
| 466 | int ret; |
| 467 | |
| 468 | if (!(args = Py_BuildValue("{s:l}", "samplerate", (long)samplerate))) { |
| 469 | srd_err("unable to build python object for metadata"); |
| 470 | return SRD_ERR_PYTHON; |
| 471 | } |
| 472 | |
| 473 | /* Run the start() method on all decoders receiving frontend data. */ |
| 474 | for (d = di_list; d; d = d->next) { |
| 475 | di = d->data; |
| 476 | di->num_probes = num_probes; |
| 477 | di->unitsize = unitsize; |
| 478 | di->samplerate = samplerate; |
| 479 | if ((ret = srd_instance_start(di, args) != SRD_OK)) |
| 480 | return ret; |
| 481 | |
| 482 | /* Run the start() method on all decoders up the stack from this one. */ |
| 483 | for (s = di->next_di; s; s = s->next) { |
| 484 | /* These don't need probes, unitsize and samplerate. */ |
| 485 | di = s->data; |
| 486 | if ((ret = srd_instance_start(di, args) != SRD_OK)) |
| 487 | return ret; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | Py_DECREF(args); |
| 492 | |
| 493 | return SRD_OK; |
| 494 | } |
| 495 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 496 | /* Feed logic samples to decoder session. */ |
Bert Vermeulen | 8652829 | 2012-01-15 23:20:39 +0100 | [diff] [blame] | 497 | int srd_session_feed(uint64_t start_samplenum, uint8_t *inbuf, uint64_t inbuflen) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 498 | { |
| 499 | GSList *d; |
| 500 | int ret; |
| 501 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 502 | for (d = di_list; d; d = d->next) { |
Bert Vermeulen | 8652829 | 2012-01-15 23:20:39 +0100 | [diff] [blame] | 503 | if ((ret = srd_instance_decode(start_samplenum, d->data, inbuf, |
Bert Vermeulen | 1aef2f9 | 2011-12-15 03:31:31 +0100 | [diff] [blame] | 504 | inbuflen)) != SRD_OK) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | return SRD_OK; |
| 509 | } |
| 510 | |
| 511 | |
Bert Vermeulen | f9a3947 | 2012-01-09 00:12:19 +0100 | [diff] [blame] | 512 | int pd_add(struct srd_decoder_instance *di, int output_type, |
Uwe Hermann | 94d43b3 | 2012-01-10 20:44:22 +0100 | [diff] [blame] | 513 | char *proto_id) |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 514 | { |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 515 | struct srd_pd_output *pdo; |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 516 | |
| 517 | if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) |
| 518 | return -1; |
| 519 | |
Bert Vermeulen | b231546 | 2012-01-09 00:13:03 +0100 | [diff] [blame] | 520 | /* pdo_id is just a simple index, nothing is deleted from this list anyway. */ |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 521 | pdo->pdo_id = g_slist_length(di->pd_output); |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 522 | pdo->output_type = output_type; |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 523 | pdo->decoder = di->decoder; |
Uwe Hermann | 94d43b3 | 2012-01-10 20:44:22 +0100 | [diff] [blame] | 524 | pdo->proto_id = g_strdup(proto_id); |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 525 | di->pd_output = g_slist_append(di->pd_output, pdo); |
| 526 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 527 | return pdo->pdo_id; |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 528 | } |
| 529 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 530 | struct srd_decoder_instance *get_di_by_decobject(void *decobject) |
| 531 | { |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 532 | GSList *l, *s; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 533 | struct srd_decoder_instance *di; |
| 534 | |
| 535 | for (l = di_list; l; l = l->next) { |
| 536 | di = l->data; |
| 537 | if (decobject == di->py_instance) |
| 538 | return di; |
Bert Vermeulen | 7ce7775 | 2012-01-10 00:25:16 +0100 | [diff] [blame] | 539 | /* Check decoders stacked on top of this one. */ |
| 540 | for (s = di->next_di; s; s = s->next) { |
| 541 | di = s->data; |
| 542 | if (decobject == di->py_instance) |
| 543 | return di; |
| 544 | } |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return NULL; |
| 548 | } |
| 549 | |
Bert Vermeulen | 983cb0f | 2012-01-08 03:20:12 +0100 | [diff] [blame] | 550 | int srd_register_callback(int output_type, void *cb) |
| 551 | { |
| 552 | struct srd_pd_callback *pd_cb; |
| 553 | |
| 554 | if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback)))) |
| 555 | return SRD_ERR_MALLOC; |
| 556 | |
| 557 | pd_cb->output_type = output_type; |
| 558 | pd_cb->callback = cb; |
| 559 | callbacks = g_slist_append(callbacks, pd_cb); |
| 560 | |
Bert Vermeulen | 983cb0f | 2012-01-08 03:20:12 +0100 | [diff] [blame] | 561 | return SRD_OK; |
| 562 | } |
| 563 | |
| 564 | void *srd_find_callback(int output_type) |
| 565 | { |
| 566 | GSList *l; |
| 567 | struct srd_pd_callback *pd_cb; |
| 568 | void *(cb); |
| 569 | |
| 570 | cb = NULL; |
| 571 | for (l = callbacks; l; l = l->next) { |
| 572 | pd_cb = l->data; |
| 573 | if (pd_cb->output_type == output_type) { |
| 574 | cb = pd_cb->callback; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return cb; |
| 580 | } |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 581 | |