]> sigrok.org Git - libsigrokdecode.git/blob - type_decoder.c
controller.c: Add checks for srd_inst_option_set().
[libsigrokdecode.git] / type_decoder.c
1 /*
2  * This file is part of the libsigrokdecode project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21 #include "libsigrokdecode-internal.h"
22 #include "config.h"
23 #include <inttypes.h>
24
25 /* This is only used for nicer srd_dbg() output. */
26 static const char *OUTPUT_TYPES[] = {
27         "OUTPUT_ANN",
28         "OUTPUT_PROTO",
29         "OUTPUT_BINARY",
30 };
31
32 static int convert_pyobj(struct srd_decoder_inst *di, PyObject *obj,
33                          int *ann_format, char ***ann)
34 {
35         PyObject *py_tmp;
36         struct srd_pd_output *pdo;
37         int ann_id;
38
39         /* Should be a list of [annotation format, [string, ...]]. */
40         if (!PyList_Check(obj) && !PyTuple_Check(obj)) {
41                 srd_err("Protocol decoder %s submitted %s instead of list.",
42                         di->decoder->name, obj->ob_type->tp_name);
43                 return SRD_ERR_PYTHON;
44         }
45
46         /* Should have 2 elements. */
47         if (PyList_Size(obj) != 2) {
48                 srd_err("Protocol decoder %s submitted annotation list with "
49                         "%d elements instead of 2", di->decoder->name,
50                         PyList_Size(obj));
51                 return SRD_ERR_PYTHON;
52         }
53
54         /*
55          * The first element should be an integer matching a previously
56          * registered annotation format.
57          */
58         py_tmp = PyList_GetItem(obj, 0);
59         if (!PyLong_Check(py_tmp)) {
60                 srd_err("Protocol decoder %s submitted annotation list, but "
61                         "first element was not an integer.", di->decoder->name);
62                 return SRD_ERR_PYTHON;
63         }
64
65         ann_id = PyLong_AsLong(py_tmp);
66         if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_id))) {
67                 srd_err("Protocol decoder %s submitted data to unregistered "
68                         "annotation format %d.", di->decoder->name, ann_id);
69                 return SRD_ERR_PYTHON;
70         }
71         *ann_format = ann_id;
72
73         /* Second element must be a list. */
74         py_tmp = PyList_GetItem(obj, 1);
75         if (!PyList_Check(py_tmp)) {
76                 srd_err("Protocol decoder %s submitted annotation list, but "
77                         "second element was not a list.", di->decoder->name);
78                 return SRD_ERR_PYTHON;
79         }
80         if (py_strlist_to_char(py_tmp, ann) != SRD_OK) {
81                 srd_err("Protocol decoder %s submitted annotation list, but "
82                         "second element was malformed.", di->decoder->name);
83                 return SRD_ERR_PYTHON;
84         }
85
86         return SRD_OK;
87 }
88
89 static PyObject *Decoder_put(PyObject *self, PyObject *args)
90 {
91         GSList *l;
92         PyObject *data, *py_res;
93         struct srd_decoder_inst *di, *next_di;
94         struct srd_pd_output *pdo;
95         struct srd_proto_data *pdata;
96         uint64_t start_sample, end_sample;
97         int output_id;
98         struct srd_pd_callback *cb;
99
100         if (!(di = srd_inst_find_by_obj(NULL, self))) {
101                 /* Shouldn't happen. */
102                 srd_dbg("put(): self instance not found.");
103                 return NULL;
104         }
105
106         if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
107             &output_id, &data)) {
108                 /*
109                  * This throws an exception, but by returning NULL here we let
110                  * Python raise it. This results in a much better trace in
111                  * controller.c on the decode() method call.
112                  */
113                 return NULL;
114         }
115
116         if (!(l = g_slist_nth(di->pd_output, output_id))) {
117                 srd_err("Protocol decoder %s submitted invalid output ID %d.",
118                         di->decoder->name, output_id);
119                 return NULL;
120         }
121         pdo = l->data;
122
123         srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
124                  di->inst_id, start_sample, end_sample,
125                  OUTPUT_TYPES[pdo->output_type], output_id);
126
127         if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) {
128                 srd_err("Failed to g_malloc() struct srd_proto_data.");
129                 return NULL;
130         }
131         pdata->start_sample = start_sample;
132         pdata->end_sample = end_sample;
133         pdata->pdo = pdo;
134
135         switch (pdo->output_type) {
136         case SRD_OUTPUT_ANN:
137                 /* Annotations are only fed to callbacks. */
138                 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
139                         /* Annotations need converting from PyObject. */
140                         if (convert_pyobj(di, data, &pdata->ann_format,
141                                           (char ***)&pdata->data) != SRD_OK) {
142                                 /* An error was already logged. */
143                                 break;
144                         }
145                         cb->cb(pdata, cb->cb_data);
146                 }
147                 break;
148         case SRD_OUTPUT_PROTO:
149                 for (l = di->next_di; l; l = l->next) {
150                         next_di = l->data;
151                         /* TODO: Is this needed? */
152                         Py_XINCREF(next_di->py_inst);
153                         srd_spew("Sending %d-%d to instance %s",
154                                  start_sample, end_sample,
155                                  next_di->inst_id);
156                         if (!(py_res = PyObject_CallMethod(
157                             next_di->py_inst, "decode", "KKO", start_sample,
158                             end_sample, data))) {
159                                 srd_exception_catch("Calling %s decode(): ",
160                                                     next_di->inst_id);
161                         }
162                         Py_XDECREF(py_res);
163                 }
164                 break;
165         case SRD_OUTPUT_BINARY:
166                 srd_err("SRD_OUTPUT_BINARY not yet supported.");
167                 break;
168         default:
169                 srd_err("Protocol decoder %s submitted invalid output type %d.",
170                         di->decoder->name, pdo->output_type);
171                 break;
172         }
173
174         g_free(pdata);
175
176         Py_RETURN_NONE;
177 }
178
179 static PyObject *Decoder_add(PyObject *self, PyObject *args)
180 {
181         PyObject *ret;
182         struct srd_decoder_inst *di;
183         char *proto_id;
184         int output_type, pdo_id;
185
186         if (!(di = srd_inst_find_by_obj(NULL, self))) {
187                 PyErr_SetString(PyExc_Exception, "decoder instance not found");
188                 return NULL;
189         }
190
191         if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
192                 /* Let Python raise this exception. */
193                 return NULL;
194         }
195
196         pdo_id = srd_inst_pd_output_add(di, output_type, proto_id);
197         if (pdo_id < 0)
198                 Py_RETURN_NONE;
199         else
200                 ret = Py_BuildValue("i", pdo_id);
201
202         return ret;
203 }
204
205 static PyMethodDef Decoder_methods[] = {
206         {"put", Decoder_put, METH_VARARGS,
207          "Accepts a dictionary with the following keys: startsample, endsample, data"},
208         {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
209         {NULL, NULL, 0, NULL}
210 };
211
212 /** @cond PRIVATE */
213 SRD_PRIV PyTypeObject srd_Decoder_type = {
214         PyVarObject_HEAD_INIT(NULL, 0)
215         .tp_name = "sigrokdecode.Decoder",
216         .tp_basicsize = sizeof(srd_Decoder),
217         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
218         .tp_doc = "sigrok Decoder base class",
219         .tp_methods = Decoder_methods,
220 };
221 /** @endcond */