X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=module_sigrokdecode.c;h=69bd17e396dc8d740a431e27090c19821cfc69ba;hp=d4af5c344a4f40cba32b54156d759372bfe1fde8;hb=f9a3947a7a8d884de6c55693b216f89b1d27d979;hpb=159699490ea4bf2495e99dcd5fb18b240d7499df diff --git a/module_sigrokdecode.c b/module_sigrokdecode.c index d4af5c3..69bd17e 100644 --- a/module_sigrokdecode.c +++ b/module_sigrokdecode.c @@ -95,14 +95,15 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args) PyObject *data; struct srd_decoder_instance *di; struct srd_pd_output *pdo; - uint64_t timeoffset, duration; - int output_id, annotation_format, i; - char **annotation, **ann_info; + struct srd_protocol_data *pdata; + uint64_t start_sample, end_sample; + int output_id; + void (*cb)(); if (!(di = get_di_by_decobject(self))) return NULL; - if (!PyArg_ParseTuple(args, "KKiO", &timeoffset, &duration, &output_id, &data)) + if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, &output_id, &data)) return NULL; if (!(l = g_slist_nth(di->pd_output, output_id))) { @@ -114,36 +115,40 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args) switch (pdo->output_type) { case SRD_OUTPUT_ANNOTATION: - if (convert_pyobj(di, data, &annotation_format, &annotation) != SRD_OK) - return NULL; - - /* TODO: SRD_OUTPUT_ANNOTATION should go back up to the caller */ - ann_info = g_slist_nth_data(pdo->decoder->annotation, annotation_format); - printf("annotation format %d (%s): ", annotation_format, ann_info[0]); - for (i = 0; annotation[i]; i++) - printf("\"%s\" ", annotation[i]); - printf("\n"); - break; - case SRD_OUTPUT_PROTOCOL: - - /* TODO: SRD_OUTPUT_PROTOCOL should go up the PD stack. */ - printf("%s protocol data: ", pdo->decoder->name); - PyObject_Print(data, stdout, Py_PRINT_RAW); - puts(""); + case SRD_OUTPUT_BINARY: break; - default: srd_err("Protocol decoder %s submitted invalid output type %d", di->decoder->name, pdo->output_type); + return NULL; break; } + if ((cb = srd_find_callback(pdo->output_type))) { + /* Something registered an interest in this output type. */ + if (!(pdata = g_try_malloc0(sizeof(struct srd_protocol_data)))) + return NULL; + pdata->start_sample = start_sample; + pdata->end_sample = end_sample; + pdata->pdo = pdo; + if (pdo->output_type == SRD_OUTPUT_ANNOTATION) { + /* annotations need converting from PyObject */ + if (convert_pyobj(di, data, &pdata->annotation_format, + (char ***)&pdata->data) != SRD_OK) + return NULL; + } else { + /* annotation_format is unused, data is an opaque blob. */ + pdata->data = data; + } + cb(pdata); + } + Py_RETURN_NONE; } -static PyObject *Decoder_output_new(PyObject *self, PyObject *py_output_type) +static PyObject *Decoder_add(PyObject *self, PyObject *args) { PyObject *ret; struct srd_decoder_instance *di; @@ -155,12 +160,10 @@ static PyObject *Decoder_output_new(PyObject *self, PyObject *py_output_type) printf("output_new di %s\n", di->decoder->name); - if (!PyArg_ParseTuple(py_output_type, "i:output_type", &output_type)) + if (!PyArg_ParseTuple(args, "is", &output_type, &protocol_id)) return NULL; - /* TODO: take protocol_id from python */ - protocol_id = "i2c"; - pdo_id = pd_output_new(di, output_type, protocol_id); + pdo_id = pd_add(di, output_type, protocol_id); if (pdo_id < 0) Py_RETURN_NONE; else @@ -173,8 +176,7 @@ static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} }; static PyMethodDef Decoder_methods[] = { {"put", Decoder_put, METH_VARARGS, "Accepts a dictionary with the following keys: time, duration, data"}, - {"output_new", Decoder_output_new, METH_VARARGS, - "Create a new output stream"}, + {"add", Decoder_add, METH_VARARGS, "Create a new output stream"}, {NULL, NULL, 0, NULL} }; @@ -222,6 +224,17 @@ PyMODINIT_FUNC PyInit_sigrokdecode(void) if (PyModule_AddObject(mod, "srd_logic", (PyObject *)&srd_logic_type) == -1) return NULL; + /* expose output types as symbols in the sigrokdecode module */ + if(PyModule_AddObject(mod, "SRD_OUTPUT_ANNOTATION", + PyLong_FromLong(SRD_OUTPUT_ANNOTATION)) == -1) + return NULL; + if(PyModule_AddObject(mod, "SRD_OUTPUT_PROTOCOL", + PyLong_FromLong(SRD_OUTPUT_PROTOCOL)) == -1) + return NULL; + if(PyModule_AddObject(mod, "SRD_OUTPUT_BINARY", + PyLong_FromLong(SRD_OUTPUT_BINARY)) == -1) + return NULL; + return mod; }