]> sigrok.org Git - libsigrokdecode.git/blob - type_decoder.c
srd: use new exception system everywhere
[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 "
42                                 "instead of 2", 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 "
51                                 "element was not an integer.", 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 unregistered "
58                                 "annotation format %d.", 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 "
67                                 "second element was not a list.", 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 "
72                                 "second element was malformed.", 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                                 catch_exception("calling %s decode(): ", next_di->instance_id);
130                         }
131                         Py_XDECREF(py_res);
132                 }
133                 break;
134         case SRD_OUTPUT_BINARY:
135                 srd_err("SRD_OUTPUT_BINARY not yet supported.");
136                 break;
137         default:
138                 srd_err("Protocol decoder %s submitted invalid output type %d.",
139                                 di->decoder->name, pdo->output_type);
140                 break;
141         }
142
143         g_free(pdata);
144
145         Py_RETURN_NONE;
146 }
147
148
149 static PyObject *Decoder_add(PyObject *self, PyObject *args)
150 {
151         PyObject *ret;
152         struct srd_decoder_instance *di;
153         char *proto_id;
154         int output_type, pdo_id;
155
156         if (!(di = get_di_by_decobject(self))) {
157                 srd_dbg("srd: decoder instance not found");
158                 PyErr_SetString(PyExc_Exception, "decoder instance not found");
159                 return NULL;
160         }
161
162         if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
163                 catch_exception("");
164                 return NULL;
165         }
166
167         pdo_id = pd_add(di, output_type, proto_id);
168         if (pdo_id < 0)
169                 Py_RETURN_NONE;
170         else
171                 ret = Py_BuildValue("i", pdo_id);
172
173         return ret;
174 }
175
176 static PyMethodDef Decoder_methods[] = {
177         {"put", Decoder_put, METH_VARARGS,
178          "Accepts a dictionary with the following keys: startsample, endsample, data"},
179         {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
180         {NULL, NULL, 0, NULL}
181 };
182
183
184 PyTypeObject srd_Decoder_type = {
185         PyVarObject_HEAD_INIT(NULL, 0)
186         .tp_name = "sigrokdecode.Decoder",
187         .tp_basicsize = sizeof(srd_Decoder),
188         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
189         .tp_doc = "Sigrok Decoder base class",
190         .tp_methods = Decoder_methods,
191 };
192