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