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> |
| 5 | * Copyright (C) 2011 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 "config.h" |
| 22 | #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
| 23 | #include <glib.h> |
| 24 | |
| 25 | /* TODO: this should probably be in sigrokdecode.h */ |
| 26 | /* Re-define some string functions for Python >= 3.0. */ |
| 27 | #if PY_VERSION_HEX >= 0x03000000 |
| 28 | #define PyString_AsString PyBytes_AsString |
| 29 | #define PyString_FromString PyBytes_FromString |
| 30 | #define PyString_Check PyBytes_Check |
| 31 | #endif |
| 32 | |
| 33 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 34 | /* TODO |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 35 | static GSList *pipelines = NULL; |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 36 | */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 37 | |
| 38 | /* lives in decoder.c */ |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 39 | extern GSList *pd_list; |
| 40 | extern GSList *di_list; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 41 | |
| 42 | struct srd_pipeline { |
| 43 | int id; |
| 44 | GSList *decoders; |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | |
| 49 | static PyObject *Decoder_init(PyObject *self, PyObject *args) |
| 50 | { |
| 51 | (void)self; |
| 52 | (void)args; |
| 53 | // printf("init object %x\n", self); |
| 54 | |
| 55 | Py_RETURN_NONE; |
| 56 | } |
| 57 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 58 | struct srd_decoder_instance *get_di_by_decobject(void *decobject); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 59 | |
| 60 | static PyObject *Decoder_put(PyObject *self, PyObject *args) |
| 61 | { |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 62 | GSList *l; |
| 63 | PyObject *data; |
| 64 | struct srd_decoder_instance *di; |
| 65 | struct srd_pd_output *pdo; |
| 66 | int output_id; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 67 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 68 | if (!(di = get_di_by_decobject(self))) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 69 | return NULL; |
| 70 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 71 | printf("put: %s instance %x: ", di->decoder->name, (unsigned int) di); |
| 72 | |
| 73 | if (!PyArg_ParseTuple(args, "iO", &output_id, &data)) |
| 74 | return NULL; |
| 75 | |
| 76 | if (!(l = g_slist_nth(di->pd_output, output_id))) |
| 77 | /* PD supplied invalid output id */ |
| 78 | /* TODO: better error message */ |
| 79 | return NULL; |
| 80 | pdo = l->data; |
| 81 | |
| 82 | printf("output type %d: ", pdo->output_type); |
| 83 | PyObject_Print(data, stdout, Py_PRINT_RAW); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 84 | puts(""); |
| 85 | |
| 86 | Py_RETURN_NONE; |
| 87 | } |
| 88 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 89 | |
| 90 | static PyObject *Decoder_output_new(PyObject *self, PyObject *py_output_type) |
| 91 | { |
| 92 | PyObject *ret; |
| 93 | struct srd_decoder_instance *di; |
| 94 | char *protocol_id, *description; |
| 95 | int output_type, pdo_id; |
| 96 | |
| 97 | if (!(di = get_di_by_decobject(self))) |
| 98 | return NULL; |
| 99 | |
| 100 | printf("output_new di %s\n", di->decoder->name); |
| 101 | |
| 102 | // if (!PyArg_ParseTuple(args, "i:output_type,s:protocol_id,s:description", |
| 103 | // &output_type, &protocol_id, &description)) |
| 104 | if (!PyArg_ParseTuple(py_output_type, "i:output_type", &output_type)) |
| 105 | return NULL; |
| 106 | |
| 107 | protocol_id = "i2c"; |
| 108 | description = "blah"; |
| 109 | pdo_id = pd_output_new(di, output_type, protocol_id, description); |
| 110 | if (pdo_id < 0) |
| 111 | Py_RETURN_NONE; |
| 112 | else |
| 113 | ret = Py_BuildValue("i", pdo_id); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 118 | static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} }; |
| 119 | static PyMethodDef Decoder_methods[] = { |
| 120 | {"__init__", Decoder_init, METH_VARARGS, ""}, |
| 121 | {"put", Decoder_put, METH_VARARGS, |
| 122 | "Accepts a dictionary with the following keys: time, duration, data"}, |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 123 | {"output_new", Decoder_output_new, METH_VARARGS, |
| 124 | "Create a new output stream"}, |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 125 | {NULL, NULL, 0, NULL} |
| 126 | }; |
| 127 | |
| 128 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 129 | typedef struct { |
| 130 | PyObject_HEAD |
| 131 | } sigrok_Decoder_object; |
| 132 | |
| 133 | static PyTypeObject sigrok_Decoder_type = { |
| 134 | PyObject_HEAD_INIT(NULL) |
Gareth McMullin | 6547acf | 2011-12-05 20:31:32 +1300 | [diff] [blame] | 135 | .tp_name = "sigrok.Decoder", |
| 136 | .tp_basicsize = sizeof(sigrok_Decoder_object), |
| 137 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 138 | .tp_doc = "Sigrok Decoder object", |
| 139 | .tp_methods = Decoder_methods, |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | PyMODINIT_FUNC init_sigrok_Decoder(void) |
| 143 | { |
| 144 | PyObject *mod; |
| 145 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 146 | /* assign this here, for compiler portability */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 147 | sigrok_Decoder_type.tp_new = PyType_GenericNew; |
| 148 | if (PyType_Ready(&sigrok_Decoder_type) < 0) |
| 149 | return; |
| 150 | |
| 151 | mod = Py_InitModule3("sigrok", no_methods, "sigrok base classes"); |
| 152 | Py_INCREF(&sigrok_Decoder_type); |
| 153 | PyModule_AddObject(mod, "Decoder", (PyObject *)&sigrok_Decoder_type); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 158 | struct srd_decoder_instance *get_di_by_decobject(void *decobject) |
| 159 | { |
| 160 | GSList *l; |
| 161 | struct srd_decoder_instance *di; |
| 162 | |
| 163 | for (l = di_list; l; l = l->next) { |
| 164 | di = l->data; |
| 165 | if (decobject == di->py_instance) |
| 166 | return di; |
| 167 | } |
| 168 | |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 172 | /** |
| 173 | * Initialize libsigrokdecode. |
| 174 | * |
| 175 | * This initializes the Python interpreter, and creates and initializes |
| 176 | * a "sigrok" Python module with a single put() method. |
| 177 | * |
| 178 | * Then, it searches for sigrok protocol decoder files (*.py) in the |
| 179 | * "decoders" subdirectory of the the sigrok installation directory. |
| 180 | * All decoders that are found are loaded into memory and added to an |
| 181 | * internal list of decoders, which can be queried via srd_list_decoders(). |
| 182 | * |
| 183 | * The caller is responsible for calling the clean-up function srd_exit(), |
| 184 | * which will properly shut down libsigrokdecode and free its allocated memory. |
| 185 | * |
| 186 | * Multiple calls to srd_init(), without calling srd_exit() inbetween, |
| 187 | * are not allowed. |
| 188 | * |
| 189 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 190 | * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders |
| 191 | * directory cannot be accessed, return SRD_ERR_DECODERS_DIR. |
| 192 | * If not enough memory could be allocated, return SRD_ERR_MALLOC. |
| 193 | */ |
| 194 | int srd_init(void) |
| 195 | { |
| 196 | int ret; |
| 197 | |
| 198 | /* Py_Initialize() returns void and usually cannot fail. */ |
| 199 | Py_Initialize(); |
| 200 | |
| 201 | init_sigrok_Decoder(); |
| 202 | |
| 203 | PyRun_SimpleString("import sys;"); |
| 204 | if ((ret = set_modulepath()) != SRD_OK) { |
| 205 | Py_Finalize(); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | if ((ret = srd_load_all_decoders()) != SRD_OK) { |
| 210 | Py_Finalize(); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | return SRD_OK; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * Shutdown libsigrokdecode. |
| 220 | * |
| 221 | * This frees all the memory allocated for protocol decoders and shuts down |
| 222 | * the Python interpreter. |
| 223 | * |
| 224 | * This function should only be called if there was a (successful!) invocation |
| 225 | * of srd_init() before. Calling this function multiple times in a row, without |
| 226 | * any successful srd_init() calls inbetween, is not allowed. |
| 227 | * |
| 228 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 229 | */ |
| 230 | int srd_exit(void) |
| 231 | { |
| 232 | /* Unload/free all decoders, and then the list of decoders itself. */ |
| 233 | /* TODO: Error handling. */ |
| 234 | srd_unload_all_decoders(); |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 235 | g_slist_free(pd_list); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 236 | |
| 237 | /* Py_Finalize() returns void, any finalization errors are ignored. */ |
| 238 | Py_Finalize(); |
| 239 | |
| 240 | return SRD_OK; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * Add search directories for the protocol decoders. |
| 246 | * |
| 247 | * TODO: add path from env var SIGROKDECODE_PATH, config etc |
| 248 | */ |
| 249 | int set_modulepath(void) |
| 250 | { |
| 251 | int ret; |
| 252 | |
| 253 | ret = PyRun_SimpleString("sys.path.append(r'" DECODERS_DIR "');"); |
| 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | struct srd_decoder_instance *srd_instance_new(const char *id) |
| 260 | { |
| 261 | struct srd_decoder *dec; |
| 262 | struct srd_decoder_instance *di; |
| 263 | PyObject *py_args; |
| 264 | |
| 265 | fprintf(stdout, "%s: %s\n", __func__, id); |
| 266 | |
| 267 | if (!(dec = srd_get_decoder_by_id(id))) |
| 268 | return NULL; |
| 269 | |
| 270 | /* TODO: Error handling. Use g_try_malloc(). */ |
| 271 | di = g_malloc(sizeof(*di)); |
| 272 | di->decoder = dec; |
| 273 | di->pd_output = NULL; |
| 274 | |
| 275 | /* Create an empty Python tuple. */ |
| 276 | if (!(py_args = PyTuple_New(0))) { /* NEWREF */ |
| 277 | if (PyErr_Occurred()) |
| 278 | PyErr_Print(); /* Returns void. */ |
| 279 | |
| 280 | return NULL; /* TODO: More specific error? */ |
| 281 | } |
| 282 | |
| 283 | /* Create an instance of the 'Decoder' class. */ |
| 284 | di->py_instance = PyObject_Call(dec->py_decobj, py_args, NULL); |
| 285 | if (!di->py_instance) { |
| 286 | if (PyErr_Occurred()) |
| 287 | PyErr_Print(); /* Returns void. */ |
| 288 | Py_XDECREF(py_args); |
| 289 | return NULL; /* TODO: More specific error? */ |
| 290 | } |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 291 | di_list = g_slist_append(di_list, di); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 292 | |
| 293 | Py_XDECREF(py_args); |
| 294 | |
| 295 | return di; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | int srd_instance_set_probe(struct srd_decoder_instance *di, |
| 300 | const char *probename, int num) |
| 301 | { |
| 302 | PyObject *probedict, *probenum; |
| 303 | |
| 304 | probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */ |
| 305 | if (!probedict) { |
| 306 | if (PyErr_Occurred()) |
| 307 | PyErr_Print(); /* Returns void. */ |
| 308 | |
| 309 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
| 310 | } |
| 311 | |
| 312 | probenum = PyInt_FromLong(num); |
| 313 | PyMapping_SetItemString(probedict, (char *)probename, probenum); |
| 314 | |
| 315 | Py_XDECREF(probenum); |
| 316 | Py_XDECREF(probedict); |
| 317 | |
| 318 | return SRD_OK; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | int srd_session_start(const char *driver, int unitsize, uint64_t starttime, |
| 323 | uint64_t samplerate) |
| 324 | { |
| 325 | PyObject *py_res; |
| 326 | GSList *d; |
| 327 | struct srd_decoder_instance *di; |
| 328 | |
| 329 | fprintf(stdout, "%s: %s\n", __func__, driver); |
| 330 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 331 | for (d = di_list; d; d = d->next) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 332 | di = d->data; |
| 333 | if (!(py_res = PyObject_CallMethod(di->py_instance, "start", |
Gareth McMullin | 6547acf | 2011-12-05 20:31:32 +1300 | [diff] [blame] | 334 | "{s:s,s:l,s:l,s:l}", |
| 335 | "driver", driver, |
| 336 | "unitsize", (long)unitsize, |
| 337 | "starttime", (long)starttime, |
| 338 | "samplerate", (long)samplerate))) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 339 | if (PyErr_Occurred()) |
| 340 | PyErr_Print(); /* Returns void. */ |
| 341 | |
| 342 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
| 343 | } |
| 344 | Py_XDECREF(py_res); |
| 345 | } |
| 346 | |
| 347 | return SRD_OK; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | /** |
| 352 | * Run the specified decoder function. |
| 353 | * |
| 354 | * @param dec TODO |
| 355 | * @param inbuf TODO |
| 356 | * @param inbuflen TODO |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 357 | * |
| 358 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 359 | */ |
| 360 | int srd_run_decoder(struct srd_decoder_instance *dec, |
| 361 | uint8_t *inbuf, uint64_t inbuflen) |
| 362 | { |
| 363 | PyObject *py_instance, *py_res; |
| 364 | /* FIXME: Don't have a timebase available here. Make one up. */ |
| 365 | static int _timehack = 0; |
| 366 | |
| 367 | _timehack += inbuflen; |
| 368 | |
| 369 | // fprintf(stdout, "%s: %s\n", __func__, dec->decoder->name); |
| 370 | |
| 371 | /* Return an error upon unusable input. */ |
| 372 | if (dec == NULL) |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 373 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 374 | if (inbuf == NULL) |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 375 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 376 | if (inbuflen == 0) /* No point in working on empty buffers. */ |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 377 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 378 | |
| 379 | /* TODO: Error handling. */ |
| 380 | py_instance = dec->py_instance; |
| 381 | Py_XINCREF(py_instance); |
| 382 | |
| 383 | if (!(py_res = PyObject_CallMethod(py_instance, "decode", |
| 384 | "{s:i,s:i,s:s#}", |
| 385 | "time", _timehack, |
| 386 | "duration", 10, |
| 387 | "data", inbuf, inbuflen))) { /* NEWREF */ |
| 388 | if (PyErr_Occurred()) |
| 389 | PyErr_Print(); /* Returns void. */ |
| 390 | |
| 391 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
| 392 | } |
| 393 | |
| 394 | Py_XDECREF(py_res); |
| 395 | return SRD_OK; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /* Feed logic samples to decoder session. */ |
| 400 | int srd_session_feed(uint8_t *inbuf, uint64_t inbuflen) |
| 401 | { |
| 402 | GSList *d; |
| 403 | int ret; |
| 404 | |
| 405 | // fprintf(stdout, "%s: %d bytes\n", __func__, inbuflen); |
| 406 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 407 | for (d = di_list; d; d = d->next) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 408 | if ((ret = srd_run_decoder(d->data, inbuf, inbuflen)) != SRD_OK) |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | return SRD_OK; |
| 413 | } |
| 414 | |
| 415 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame^] | 416 | int pd_output_new(struct srd_decoder_instance *di, int output_type, |
| 417 | char *protocol_id, char *description) |
| 418 | { |
| 419 | GSList *l; |
| 420 | struct srd_pd_output *pdo; |
| 421 | int pdo_id; |
| 422 | |
| 423 | fprintf(stdout, "%s: output type %d, protocol_id %s, description %s\n", |
| 424 | __func__, output_type, protocol_id, description); |
| 425 | |
| 426 | pdo_id = -1; |
| 427 | for (l = di->pd_output; l; l = l->next) { |
| 428 | pdo = l->data; |
| 429 | if (pdo->pdo_id > pdo_id) |
| 430 | pdo_id = pdo->pdo_id; |
| 431 | } |
| 432 | pdo_id++; |
| 433 | |
| 434 | if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) |
| 435 | return -1; |
| 436 | |
| 437 | pdo->pdo_id = pdo_id; |
| 438 | pdo->output_type = output_type; |
| 439 | pdo->protocol_id = g_strdup(protocol_id); |
| 440 | pdo->description = g_strdup(description); |
| 441 | di->pd_output = g_slist_append(di->pd_output, pdo); |
| 442 | |
| 443 | return pdo_id; |
| 444 | } |
| 445 | |
| 446 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 447 | //int srd_pipeline_new(int plid) |
| 448 | //{ |
| 449 | // |
| 450 | // |
| 451 | //} |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 452 | |
| 453 | |
| 454 | |
| 455 | |