+ if (!(d = malloc(sizeof(struct srd_decoder))))
+ return SRD_ERR_MALLOC;
+
+ /* We'll just use the name of the module for the ID. */
+ d->id = strdup(name);
+
+ if ((r = h_str(py_res, py_mod, "name", &(d->name))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_mod, "longname",
+ &(d->longname))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_mod, "desc", &(d->desc))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_mod, "longdesc",
+ &(d->longdesc))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_mod, "author", &(d->author))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_mod, "email", &(d->email))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_mod, "license", &(d->license))) < 0)
+ return r;
+
+ d->py_mod = py_mod;
+ d->py_decobj = py_res;
+
+ /* TODO: Handle func, inputformats, outputformats. */
+ /* Note: They must at least be set to NULL, will segfault otherwise. */
+ d->func = NULL;
+ d->inputformats = NULL;
+ d->outputformats = NULL;
+
+ *dec = d;
+
+ return SRD_OK;
+}
+
+/** Create a new decoder instance and add to session. */
+struct srd_decoder_instance *srd_instance_new(const char *id)
+{
+ struct srd_decoder *dec;
+ struct srd_decoder_instance *di;
+ PyObject *py_args;
+
+ if (!(dec = srd_get_decoder_by_id(id)))
+ return NULL;
+
+ /* TODO: Error handling. Use g_try_malloc(). */
+ di = g_malloc(sizeof(*di));
+
+ /* Create an empty Python tuple. */
+ if (!(py_args = PyTuple_New(0))) { /* NEWREF */
+ if (PyErr_Occurred())
+ PyErr_Print(); /* Returns void. */
+
+ return NULL; /* TODO: More specific error? */
+ }
+
+ /* Create an instance of the 'Decoder' class. */
+ di->py_instance = PyObject_Call(dec->py_decobj, py_args, NULL);
+ if (!di->py_instance) {
+ if (PyErr_Occurred())
+ PyErr_Print(); /* Returns void. */
+ Py_XDECREF(py_args);
+ return NULL; /* TODO: More specific error? */
+ }
+
+ /* Append to list of PD instances */
+ decoders = g_slist_append(decoders, di);
+
+ Py_XDECREF(py_args);
+
+ return di;
+}
+
+int srd_instance_set_probe(struct srd_decoder_instance *di,
+ const char *probename, int num)
+{
+ PyObject *probedict, *probenum;
+
+ probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */
+ if (!probedict) {
+ if (PyErr_Occurred())
+ PyErr_Print(); /* Returns void. */
+
+ return SRD_ERR_PYTHON; /* TODO: More specific error? */
+ }
+
+ probenum = PyInt_FromLong(num);
+ PyMapping_SetItemString(probedict, (char *)probename, probenum);
+
+ Py_XDECREF(probenum);
+ Py_XDECREF(probedict);
+
+ return SRD_OK;
+}
+
+/** Start decoding session. Feed metadata to decoder instances. */
+int srd_session_start(const char *driver, int unitsize, uint64_t starttime,
+ uint64_t samplerate)
+{
+ PyObject *py_res;
+ GSList *d;
+ for (d = decoders; d; d = d->next) {
+ struct srd_decoder_instance *di = d->data;
+ /* TODO: Error handling. */
+ if (!(py_res = PyObject_CallMethod(di->py_instance, "start",
+ "{s:s,s:l,s:l,s:l}",
+ "driver", driver,
+ "unitsize", (long)unitsize,
+ "starttime", (long)starttime,
+ "samplerate", (long)samplerate))) {
+ if (PyErr_Occurred())
+ PyErr_Print(); /* Returns void. */
+
+ return SRD_ERR_PYTHON; /* TODO: More specific error? */
+ }
+ Py_XDECREF(py_res);
+ }
+
+ return SRD_OK;