2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
27 static GSList *callbacks = NULL;
30 static GSList *pipelines = NULL;
37 /* lives in decoder.c */
38 extern GSList *pd_list;
39 extern GSList *di_list;
41 /* lives in module_sigrokdecode.c */
42 extern PyMODINIT_FUNC PyInit_sigrokdecode(void);
44 /* lives in type_logic.c */
45 extern PyTypeObject srd_logic_type;
49 * Initialize libsigrokdecode.
51 * This initializes the Python interpreter, and creates and initializes
52 * a "sigrok" Python module with a single put() method.
54 * Then, it searches for sigrok protocol decoder files (*.py) in the
55 * "decoders" subdirectory of the the sigrok installation directory.
56 * All decoders that are found are loaded into memory and added to an
57 * internal list of decoders, which can be queried via srd_list_decoders().
59 * The caller is responsible for calling the clean-up function srd_exit(),
60 * which will properly shut down libsigrokdecode and free its allocated memory.
62 * Multiple calls to srd_init(), without calling srd_exit() inbetween,
65 * @return SRD_OK upon success, a (negative) error code otherwise.
66 * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders
67 * directory cannot be accessed, return SRD_ERR_DECODERS_DIR.
68 * If not enough memory could be allocated, return SRD_ERR_MALLOC.
74 PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode);
76 /* Py_Initialize() returns void and usually cannot fail. */
79 if ((ret = set_modulepath()) != SRD_OK) {
84 if ((ret = srd_load_all_decoders()) != SRD_OK) {
94 * Shutdown libsigrokdecode.
96 * This frees all the memory allocated for protocol decoders and shuts down
97 * the Python interpreter.
99 * This function should only be called if there was a (successful!) invocation
100 * of srd_init() before. Calling this function multiple times in a row, without
101 * any successful srd_init() calls inbetween, is not allowed.
103 * @return SRD_OK upon success, a (negative) error code otherwise.
107 /* Unload/free all decoders, and then the list of decoders itself. */
108 /* TODO: Error handling. */
109 srd_unload_all_decoders();
110 g_slist_free(pd_list);
112 /* Py_Finalize() returns void, any finalization errors are ignored. */
120 * Add search directories for the protocol decoders.
122 * TODO: add path from env var SIGROKDECODE_PATH, config etc
124 int set_modulepath(void)
128 PyRun_SimpleString("import sys");
129 ret = PyRun_SimpleString("sys.path.append(r'" DECODERS_DIR "');");
135 struct srd_decoder_instance *srd_instance_new(const char *id)
137 struct srd_decoder *dec;
138 struct srd_decoder_instance *di;
141 fprintf(stdout, "%s: %s\n", __func__, id);
143 if (!(dec = srd_get_decoder_by_id(id)))
146 /* TODO: Error handling. Use g_try_malloc(). */
147 di = g_malloc(sizeof(*di));
149 di->pd_output = NULL;
152 /* Create an empty Python tuple. */
153 if (!(py_args = PyTuple_New(0))) { /* NEWREF */
154 if (PyErr_Occurred())
155 PyErr_Print(); /* Returns void. */
157 return NULL; /* TODO: More specific error? */
160 /* Create an instance of the 'Decoder' class. */
161 di->py_instance = PyObject_Call(dec->py_decobj, py_args, NULL);
162 if (!di->py_instance) {
163 if (PyErr_Occurred())
164 PyErr_Print(); /* Returns void. */
166 return NULL; /* TODO: More specific error? */
168 di_list = g_slist_append(di_list, di);
176 int srd_instance_set_probe(struct srd_decoder_instance *di,
177 const char *probename, int num)
179 PyObject *probedict, *probenum;
181 probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */
183 if (PyErr_Occurred())
184 PyErr_Print(); /* Returns void. */
186 return SRD_ERR_PYTHON; /* TODO: More specific error? */
189 probenum = PyLong_FromLong(num);
190 PyMapping_SetItemString(probedict, (char *)probename, probenum);
192 Py_XDECREF(probenum);
193 Py_XDECREF(probedict);
199 int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
203 struct srd_decoder_instance *di;
205 fprintf(stdout, "%s\n", __func__);
207 for (d = di_list; d; d = d->next) {
209 di->num_probes = num_probes;
210 di->unitsize = unitsize;
211 di->samplerate = samplerate;
212 if (!(py_res = PyObject_CallMethod(di->py_instance, "start",
214 "samplerate", (long)samplerate))) {
215 if (PyErr_Occurred())
216 PyErr_Print(); /* Returns void. */
218 return SRD_ERR_PYTHON; /* TODO: More specific error? */
228 * Run the specified decoder function.
232 * @param inbuflen TODO
234 * @return SRD_OK upon success, a (negative) error code otherwise.
236 int srd_run_decoder(uint64_t timeoffset, uint64_t duration,
237 struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen)
239 PyObject *py_instance, *py_res;
242 /* Return an error upon unusable input. */
244 return SRD_ERR_ARG; /* TODO: More specific error? */
246 return SRD_ERR_ARG; /* TODO: More specific error? */
247 if (inbuflen == 0) /* No point in working on empty buffers. */
248 return SRD_ERR_ARG; /* TODO: More specific error? */
250 /* TODO: Error handling. */
251 py_instance = di->py_instance;
252 Py_XINCREF(py_instance);
254 logic = PyObject_New(srd_logic, &srd_logic_type);
258 logic->inbuf = inbuf;
259 logic->inbuflen = inbuflen;
260 logic->sample = PyList_New(2);
261 Py_INCREF(logic->sample);
263 if (!(py_res = PyObject_CallMethod(py_instance, "decode",
264 "KKO", timeoffset, duration, logic))) {
265 if (PyErr_Occurred())
266 PyErr_Print(); /* Returns void. */
268 return SRD_ERR_PYTHON; /* TODO: More specific error? */
277 /* Feed logic samples to decoder session. */
278 int srd_session_feed(uint64_t timeoffset, uint64_t duration, uint8_t *inbuf,
284 // fprintf(stdout, "%s: %d bytes\n", __func__, inbuflen);
286 for (d = di_list; d; d = d->next) {
287 if ((ret = srd_run_decoder(timeoffset, duration, d->data, inbuf,
288 inbuflen)) != SRD_OK)
296 int pd_add(struct srd_decoder_instance *di, int output_type,
299 struct srd_pd_output *pdo;
301 if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output))))
304 /* pdo_id is just a simple index, nothing is deleted from this list anway */
305 pdo->pdo_id = g_slist_length(di->pd_output);
306 pdo->output_type = output_type;
307 pdo->decoder = di->decoder;
308 pdo->protocol_id = g_strdup(protocol_id);
309 di->pd_output = g_slist_append(di->pd_output, pdo);
311 fprintf(stdout, "%s: output type %d, protocol_id %s, id %d\n",
312 __func__, output_type, protocol_id, pdo->pdo_id);
317 struct srd_decoder_instance *get_di_by_decobject(void *decobject)
320 struct srd_decoder_instance *di;
322 for (l = di_list; l; l = l->next) {
324 if (decobject == di->py_instance)
331 int srd_register_callback(int output_type, void *cb)
333 struct srd_pd_callback *pd_cb;
335 if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback))))
336 return SRD_ERR_MALLOC;
338 pd_cb->output_type = output_type;
339 pd_cb->callback = cb;
340 callbacks = g_slist_append(callbacks, pd_cb);
342 printf("got cb for %d: %p\n", output_type, cb);
346 void *srd_find_callback(int output_type)
349 struct srd_pd_callback *pd_cb;
353 for (l = callbacks; l; l = l->next) {
355 if (pd_cb->output_type == output_type) {
356 cb = pd_cb->callback;
364 //int srd_pipeline_new(int plid)