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 | |
| 21 | #include "config.h" |
Uwe Hermann | 7319141 | 2012-01-03 19:56:01 +0100 | [diff] [blame] | 22 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 23 | #include <glib.h> |
Bert Vermeulen | 1aef2f9 | 2011-12-15 03:31:31 +0100 | [diff] [blame] | 24 | #include <inttypes.h> |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 25 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 26 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 27 | /* TODO |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 28 | static GSList *pipelines = NULL; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 29 | struct srd_pipeline { |
| 30 | int id; |
| 31 | GSList *decoders; |
| 32 | }; |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 33 | */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 34 | |
| 35 | /* lives in decoder.c */ |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 36 | extern GSList *pd_list; |
| 37 | extern GSList *di_list; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 38 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 39 | /* lives in module_sigrokdecode.c */ |
| 40 | extern PyMODINIT_FUNC PyInit_sigrokdecode(void); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 41 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 42 | /* lives in type_logic.c */ |
| 43 | extern PyTypeObject srd_logic_type; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 44 | |
| 45 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 46 | /** |
| 47 | * Initialize libsigrokdecode. |
| 48 | * |
| 49 | * This initializes the Python interpreter, and creates and initializes |
| 50 | * a "sigrok" Python module with a single put() method. |
| 51 | * |
| 52 | * Then, it searches for sigrok protocol decoder files (*.py) in the |
| 53 | * "decoders" subdirectory of the the sigrok installation directory. |
| 54 | * All decoders that are found are loaded into memory and added to an |
| 55 | * internal list of decoders, which can be queried via srd_list_decoders(). |
| 56 | * |
| 57 | * The caller is responsible for calling the clean-up function srd_exit(), |
| 58 | * which will properly shut down libsigrokdecode and free its allocated memory. |
| 59 | * |
| 60 | * Multiple calls to srd_init(), without calling srd_exit() inbetween, |
| 61 | * are not allowed. |
| 62 | * |
| 63 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 64 | * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders |
| 65 | * directory cannot be accessed, return SRD_ERR_DECODERS_DIR. |
| 66 | * If not enough memory could be allocated, return SRD_ERR_MALLOC. |
| 67 | */ |
| 68 | int srd_init(void) |
| 69 | { |
| 70 | int ret; |
| 71 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 72 | PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode); |
Bert Vermeulen | 5f802ec | 2011-12-27 22:15:53 +0100 | [diff] [blame] | 73 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 74 | /* Py_Initialize() returns void and usually cannot fail. */ |
| 75 | Py_Initialize(); |
| 76 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 77 | if ((ret = set_modulepath()) != SRD_OK) { |
| 78 | Py_Finalize(); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | if ((ret = srd_load_all_decoders()) != SRD_OK) { |
| 83 | Py_Finalize(); |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | return SRD_OK; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Shutdown libsigrokdecode. |
| 93 | * |
| 94 | * This frees all the memory allocated for protocol decoders and shuts down |
| 95 | * the Python interpreter. |
| 96 | * |
| 97 | * This function should only be called if there was a (successful!) invocation |
| 98 | * of srd_init() before. Calling this function multiple times in a row, without |
| 99 | * any successful srd_init() calls inbetween, is not allowed. |
| 100 | * |
| 101 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 102 | */ |
| 103 | int srd_exit(void) |
| 104 | { |
| 105 | /* Unload/free all decoders, and then the list of decoders itself. */ |
| 106 | /* TODO: Error handling. */ |
| 107 | srd_unload_all_decoders(); |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 108 | g_slist_free(pd_list); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 109 | |
| 110 | /* Py_Finalize() returns void, any finalization errors are ignored. */ |
| 111 | Py_Finalize(); |
| 112 | |
| 113 | return SRD_OK; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Add search directories for the protocol decoders. |
| 119 | * |
| 120 | * TODO: add path from env var SIGROKDECODE_PATH, config etc |
| 121 | */ |
| 122 | int set_modulepath(void) |
| 123 | { |
| 124 | int ret; |
| 125 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 126 | PyRun_SimpleString("import sys"); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 127 | ret = PyRun_SimpleString("sys.path.append(r'" DECODERS_DIR "');"); |
| 128 | |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | struct srd_decoder_instance *srd_instance_new(const char *id) |
| 134 | { |
| 135 | struct srd_decoder *dec; |
| 136 | struct srd_decoder_instance *di; |
| 137 | PyObject *py_args; |
| 138 | |
| 139 | fprintf(stdout, "%s: %s\n", __func__, id); |
| 140 | |
| 141 | if (!(dec = srd_get_decoder_by_id(id))) |
| 142 | return NULL; |
| 143 | |
| 144 | /* TODO: Error handling. Use g_try_malloc(). */ |
| 145 | di = g_malloc(sizeof(*di)); |
| 146 | di->decoder = dec; |
| 147 | di->pd_output = NULL; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 148 | di->unitsize = 0; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 149 | |
| 150 | /* Create an empty Python tuple. */ |
| 151 | if (!(py_args = PyTuple_New(0))) { /* NEWREF */ |
| 152 | if (PyErr_Occurred()) |
| 153 | PyErr_Print(); /* Returns void. */ |
| 154 | |
| 155 | return NULL; /* TODO: More specific error? */ |
| 156 | } |
| 157 | |
| 158 | /* Create an instance of the 'Decoder' class. */ |
| 159 | di->py_instance = PyObject_Call(dec->py_decobj, py_args, NULL); |
| 160 | if (!di->py_instance) { |
| 161 | if (PyErr_Occurred()) |
| 162 | PyErr_Print(); /* Returns void. */ |
| 163 | Py_XDECREF(py_args); |
| 164 | return NULL; /* TODO: More specific error? */ |
| 165 | } |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 166 | di_list = g_slist_append(di_list, di); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 167 | |
| 168 | Py_XDECREF(py_args); |
| 169 | |
| 170 | return di; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | int srd_instance_set_probe(struct srd_decoder_instance *di, |
| 175 | const char *probename, int num) |
| 176 | { |
| 177 | PyObject *probedict, *probenum; |
| 178 | |
| 179 | probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */ |
| 180 | if (!probedict) { |
| 181 | if (PyErr_Occurred()) |
| 182 | PyErr_Print(); /* Returns void. */ |
| 183 | |
| 184 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
| 185 | } |
| 186 | |
Bert Vermeulen | 5f802ec | 2011-12-27 22:15:53 +0100 | [diff] [blame] | 187 | probenum = PyLong_FromLong(num); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 188 | PyMapping_SetItemString(probedict, (char *)probename, probenum); |
| 189 | |
| 190 | Py_XDECREF(probenum); |
| 191 | Py_XDECREF(probedict); |
| 192 | |
| 193 | return SRD_OK; |
| 194 | } |
| 195 | |
| 196 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 197 | int srd_session_start(int num_probes, int unitsize, uint64_t samplerate) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 198 | { |
| 199 | PyObject *py_res; |
| 200 | GSList *d; |
| 201 | struct srd_decoder_instance *di; |
| 202 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 203 | fprintf(stdout, "%s\n", __func__); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 204 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 205 | for (d = di_list; d; d = d->next) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 206 | di = d->data; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 207 | di->num_probes = num_probes; |
| 208 | di->unitsize = unitsize; |
| 209 | di->samplerate = samplerate; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 210 | if (!(py_res = PyObject_CallMethod(di->py_instance, "start", |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 211 | "{s:l}", |
Gareth McMullin | 6547acf | 2011-12-05 20:31:32 +1300 | [diff] [blame] | 212 | "samplerate", (long)samplerate))) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 213 | if (PyErr_Occurred()) |
| 214 | PyErr_Print(); /* Returns void. */ |
| 215 | |
| 216 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
| 217 | } |
| 218 | Py_XDECREF(py_res); |
| 219 | } |
| 220 | |
| 221 | return SRD_OK; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Run the specified decoder function. |
| 227 | * |
| 228 | * @param dec TODO |
| 229 | * @param inbuf TODO |
| 230 | * @param inbuflen TODO |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 231 | * |
| 232 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 233 | */ |
Bert Vermeulen | 1aef2f9 | 2011-12-15 03:31:31 +0100 | [diff] [blame] | 234 | int srd_run_decoder(uint64_t timeoffset, uint64_t duration, |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 235 | struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 236 | { |
| 237 | PyObject *py_instance, *py_res; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 238 | srd_logic *logic; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 239 | |
| 240 | /* Return an error upon unusable input. */ |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 241 | if (di == NULL) |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 242 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 243 | if (inbuf == NULL) |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 244 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 245 | if (inbuflen == 0) /* No point in working on empty buffers. */ |
Uwe Hermann | a62186e | 2011-12-21 19:09:46 +0100 | [diff] [blame] | 246 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 247 | |
| 248 | /* TODO: Error handling. */ |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 249 | py_instance = di->py_instance; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 250 | Py_XINCREF(py_instance); |
| 251 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 252 | logic = PyObject_New(srd_logic, &srd_logic_type); |
| 253 | Py_INCREF(logic); |
| 254 | logic->di = di; |
| 255 | logic->itercnt = 0; |
| 256 | logic->inbuf = inbuf; |
| 257 | logic->inbuflen = inbuflen; |
| 258 | logic->sample = PyList_New(2); |
| 259 | Py_INCREF(logic->sample); |
| 260 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 261 | if (!(py_res = PyObject_CallMethod(py_instance, "decode", |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 262 | "KKO", timeoffset, duration, logic))) { |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 263 | if (PyErr_Occurred()) |
| 264 | PyErr_Print(); /* Returns void. */ |
| 265 | |
| 266 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
| 267 | } |
| 268 | |
| 269 | Py_XDECREF(py_res); |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 270 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 271 | return SRD_OK; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /* Feed logic samples to decoder session. */ |
Bert Vermeulen | 1aef2f9 | 2011-12-15 03:31:31 +0100 | [diff] [blame] | 276 | int srd_session_feed(uint64_t timeoffset, uint64_t duration, uint8_t *inbuf, |
| 277 | uint64_t inbuflen) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 278 | { |
| 279 | GSList *d; |
| 280 | int ret; |
| 281 | |
| 282 | // fprintf(stdout, "%s: %d bytes\n", __func__, inbuflen); |
| 283 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 284 | for (d = di_list; d; d = d->next) { |
Bert Vermeulen | 1aef2f9 | 2011-12-15 03:31:31 +0100 | [diff] [blame] | 285 | if ((ret = srd_run_decoder(timeoffset, duration, d->data, inbuf, |
| 286 | inbuflen)) != SRD_OK) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | return SRD_OK; |
| 291 | } |
| 292 | |
| 293 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 294 | int pd_output_new(struct srd_decoder_instance *di, int output_type, |
| 295 | char *protocol_id, char *description) |
| 296 | { |
| 297 | GSList *l; |
| 298 | struct srd_pd_output *pdo; |
| 299 | int pdo_id; |
| 300 | |
| 301 | fprintf(stdout, "%s: output type %d, protocol_id %s, description %s\n", |
| 302 | __func__, output_type, protocol_id, description); |
| 303 | |
| 304 | pdo_id = -1; |
| 305 | for (l = di->pd_output; l; l = l->next) { |
| 306 | pdo = l->data; |
| 307 | if (pdo->pdo_id > pdo_id) |
| 308 | pdo_id = pdo->pdo_id; |
| 309 | } |
| 310 | pdo_id++; |
| 311 | |
| 312 | if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) |
| 313 | return -1; |
| 314 | |
| 315 | pdo->pdo_id = pdo_id; |
| 316 | pdo->output_type = output_type; |
| 317 | pdo->protocol_id = g_strdup(protocol_id); |
| 318 | pdo->description = g_strdup(description); |
| 319 | di->pd_output = g_slist_append(di->pd_output, pdo); |
| 320 | |
| 321 | return pdo_id; |
| 322 | } |
| 323 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 324 | struct srd_decoder_instance *get_di_by_decobject(void *decobject) |
| 325 | { |
| 326 | GSList *l; |
| 327 | struct srd_decoder_instance *di; |
| 328 | |
| 329 | for (l = di_list; l; l = l->next) { |
| 330 | di = l->data; |
| 331 | if (decobject == di->py_instance) |
| 332 | return di; |
| 333 | } |
| 334 | |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 338 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 339 | //int srd_pipeline_new(int plid) |
| 340 | //{ |
| 341 | // |
| 342 | // |
| 343 | //} |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 344 | |
| 345 | |
| 346 | |
| 347 | |