]> sigrok.org Git - libsigrokdecode.git/blob - type_decoder.c
accept only supported types in decoder class default option values
[libsigrokdecode.git] / type_decoder.c
1 /*
2  * This file is part of the sigrok 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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21 #include "sigrokdecode-internal.h"
22 #include "config.h"
23
24
25 static int convert_pyobj(struct srd_decoder_instance *di, PyObject *obj,
26                 int *ann_format, char ***ann)
27 {
28         PyObject *py_tmp;
29         struct srd_pd_output *pdo;
30         int ann_id;
31
32         /* Should be a list of [annotation format, [string, ...]] */
33         if (!PyList_Check(obj) && !PyTuple_Check(obj)) {
34                 srd_err("Protocol decoder %s submitted %s instead of list",
35                                 di->decoder->name, obj->ob_type->tp_name);
36                 return SRD_ERR_PYTHON;
37         }
38
39         /* Should have 2 elements... */
40         if (PyList_Size(obj) != 2) {
41                 srd_err("Protocol decoder %s submitted annotation list with %d elements instead of 2",
42                                 di->decoder->name, PyList_Size(obj));
43                 return SRD_ERR_PYTHON;
44         }
45
46         /* First element should be an integer matching a previously
47          * registered annotation format. */
48         py_tmp = PyList_GetItem(obj, 0);
49         if (!PyLong_Check(py_tmp)) {
50                 srd_err("Protocol decoder %s submitted annotation list, but first element was not an integer",
51                                 di->decoder->name);
52                 return SRD_ERR_PYTHON;
53         }
54
55         ann_id = PyLong_AsLong(py_tmp);
56         if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_id))) {
57                 srd_err("Protocol decoder %s submitted data to non-existent annotation format %d",
58                                 di->decoder->name, ann_id);
59                 return SRD_ERR_PYTHON;
60         }
61         *ann_format = ann_id;
62
63         /* Second element must be a list */
64         py_tmp = PyList_GetItem(obj, 1);
65         if (!PyList_Check(py_tmp)) {
66                 srd_err("Protocol decoder %s submitted annotation list, but second element was not a list",
67                                 di->decoder->name);
68                 return SRD_ERR_PYTHON;
69         }
70         if (py_strlist_to_char(py_tmp, ann) != SRD_OK) {
71                 srd_err("Protocol decoder %s submitted annotation list, but second element was malformed",
72                                 di->decoder->name);
73                 return SRD_ERR_PYTHON;
74         }
75
76         return SRD_OK;
77 }
78
79 static PyObject *Decoder_put(PyObject *self, PyObject *args)
80 {
81         GSList *l;
82         PyObject *data, *py_res;
83         struct srd_decoder_instance *di, *next_di;
84         struct srd_pd_output *pdo;
85         struct srd_proto_data *pdata;
86         uint64_t start_sample, end_sample;
87         int output_id;
88         void (*cb)();
89
90         if (!(di = get_di_by_decobject(self)))
91                 return NULL;
92
93         if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, &output_id, &data))
94                 return NULL;
95
96         if (!(l = g_slist_nth(di->pd_output, output_id))) {
97                 srd_err("Protocol decoder %s submitted invalid output ID %d",
98                                 di->decoder->name, output_id);
99                 return NULL;
100         }
101         pdo = l->data;
102
103         if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data))))
104                 return NULL;
105         pdata->start_sample = start_sample;
106         pdata->end_sample = end_sample;
107         pdata->pdo = pdo;
108
109         switch (pdo->output_type) {
110         case SRD_OUTPUT_ANN:
111                 /* Annotations are only fed to callbacks. */
112                 if ((cb = srd_find_callback(pdo->output_type))) {
113                         /* Annotations need converting from PyObject. */
114                         if (convert_pyobj(di, data, &pdata->ann_format,
115                                         (char ***)&pdata->data) != SRD_OK) {
116                                 /* An error was already logged. */
117                                 break;
118                         }
119                         cb(pdata);
120                 }
121                 break;
122         case SRD_OUTPUT_PROTO:
123                 for (l = di->next_di; l; l = l->next) {
124                         next_di = l->data;
125                         /* TODO: is this needed? */
126                         Py_XINCREF(next_di->py_instance);
127                         if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode",
128                                         "KKO", start_sample, end_sample, data))) {
129                                 if (PyErr_Occurred())
130                                         PyErr_Print();
131                         }
132                         Py_XDECREF(py_res);
133                 }
134                 break;
135         case SRD_OUTPUT_BINARY:
136                 srd_err("SRD_OUTPUT_BINARY not yet supported");
137                 break;
138         default:
139                 srd_err("Protocol decoder %s submitted invalid output type %d",
140                                 di->decoder->name, pdo->output_type);
141                 break;
142         }
143
144         g_free(pdata);
145
146         Py_RETURN_NONE;
147 }
148
149
150 static PyObject *Decoder_add(PyObject *self, PyObject *args)
151 {
152         PyObject *ret;
153         struct srd_decoder_instance *di;
154         char *proto_id;
155         int output_type, pdo_id;
156
157         if (!(di = get_di_by_decobject(self))) {
158                 srd_err("%s():%d decoder instance not found", __func__, __LINE__);
159                 PyErr_SetString(PyExc_Exception, "decoder instance not found");
160                 return NULL;
161         }
162
163         if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
164                 if (PyErr_Occurred())
165                         PyErr_Print();
166                 return NULL;
167         }
168
169         pdo_id = pd_add(di, output_type, proto_id);
170         if (pdo_id < 0)
171                 Py_RETURN_NONE;
172         else
173                 ret = Py_BuildValue("i", pdo_id);
174
175         return ret;
176 }
177
178 static PyMethodDef Decoder_methods[] = {
179         {"put", Decoder_put, METH_VARARGS,
180          "Accepts a dictionary with the following keys: startsample, endsample, data"},
181         {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
182         {NULL, NULL, 0, NULL}
183 };
184
185
186 PyTypeObject srd_Decoder_type = {
187         PyVarObject_HEAD_INIT(NULL, 0)
188         .tp_name = "sigrokdecode.Decoder",
189         .tp_basicsize = sizeof(srd_Decoder),
190         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
191         .tp_doc = "Sigrok Decoder base class",
192         .tp_methods = Decoder_methods,
193 };
194