]>
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 | ||
2072ae0c | 98 | if (!(di = srd_instance_find_by_obj(NULL, self))) { |
58572aed | 99 | /* Shouldn't happen. */ |
7a1712c4 | 100 | srd_dbg("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 | ||
1c2b0d0b UH |
117 | srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.", |
118 | di->instance_id, start_sample, end_sample, | |
119 | OUTPUT_TYPES[pdo->output_type], output_id); | |
58572aed | 120 | |
d0a0ed03 BV |
121 | if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) |
122 | return NULL; | |
123 | pdata->start_sample = start_sample; | |
124 | pdata->end_sample = end_sample; | |
125 | pdata->pdo = pdo; | |
126 | ||
127 | switch (pdo->output_type) { | |
128 | case SRD_OUTPUT_ANN: | |
129 | /* Annotations are only fed to callbacks. */ | |
130 | if ((cb = srd_find_callback(pdo->output_type))) { | |
131 | /* Annotations need converting from PyObject. */ | |
132 | if (convert_pyobj(di, data, &pdata->ann_format, | |
133 | (char ***)&pdata->data) != SRD_OK) { | |
134 | /* An error was already logged. */ | |
135 | break; | |
136 | } | |
137 | cb(pdata); | |
138 | } | |
139 | break; | |
140 | case SRD_OUTPUT_PROTO: | |
141 | for (l = di->next_di; l; l = l->next) { | |
142 | next_di = l->data; | |
143 | /* TODO: is this needed? */ | |
144 | Py_XINCREF(next_di->py_instance); | |
7a1712c4 UH |
145 | srd_spew("Sending %d-%d to instance %s", |
146 | start_sample, end_sample, next_di->instance_id); | |
d0a0ed03 BV |
147 | if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode", |
148 | "KKO", start_sample, end_sample, data))) { | |
2086c684 | 149 | catch_exception("calling %s decode(): ", next_di->instance_id); |
d0a0ed03 BV |
150 | } |
151 | Py_XDECREF(py_res); | |
152 | } | |
153 | break; | |
154 | case SRD_OUTPUT_BINARY: | |
d906d3f9 | 155 | srd_err("SRD_OUTPUT_BINARY not yet supported."); |
d0a0ed03 BV |
156 | break; |
157 | default: | |
d906d3f9 | 158 | srd_err("Protocol decoder %s submitted invalid output type %d.", |
d0a0ed03 BV |
159 | di->decoder->name, pdo->output_type); |
160 | break; | |
161 | } | |
162 | ||
163 | g_free(pdata); | |
164 | ||
165 | Py_RETURN_NONE; | |
166 | } | |
167 | ||
168 | ||
169 | static PyObject *Decoder_add(PyObject *self, PyObject *args) | |
170 | { | |
171 | PyObject *ret; | |
172 | struct srd_decoder_instance *di; | |
173 | char *proto_id; | |
174 | int output_type, pdo_id; | |
175 | ||
2072ae0c | 176 | if (!(di = srd_instance_find_by_obj(NULL, self))) { |
d0a0ed03 BV |
177 | PyErr_SetString(PyExc_Exception, "decoder instance not found"); |
178 | return NULL; | |
179 | } | |
180 | ||
181 | if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) { | |
58572aed | 182 | /* Let python raise this exception. */ |
d0a0ed03 BV |
183 | return NULL; |
184 | } | |
185 | ||
186 | pdo_id = pd_add(di, output_type, proto_id); | |
187 | if (pdo_id < 0) | |
188 | Py_RETURN_NONE; | |
189 | else | |
190 | ret = Py_BuildValue("i", pdo_id); | |
191 | ||
192 | return ret; | |
193 | } | |
194 | ||
195 | static PyMethodDef Decoder_methods[] = { | |
196 | {"put", Decoder_put, METH_VARARGS, | |
86528298 | 197 | "Accepts a dictionary with the following keys: startsample, endsample, data"}, |
d0a0ed03 BV |
198 | {"add", Decoder_add, METH_VARARGS, "Create a new output stream"}, |
199 | {NULL, NULL, 0, NULL} | |
200 | }; | |
201 | ||
202 | ||
203 | PyTypeObject srd_Decoder_type = { | |
204 | PyVarObject_HEAD_INIT(NULL, 0) | |
205 | .tp_name = "sigrokdecode.Decoder", | |
206 | .tp_basicsize = sizeof(srd_Decoder), | |
207 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | |
208 | .tp_doc = "Sigrok Decoder base class", | |
209 | .tp_methods = Decoder_methods, | |
210 | }; | |
211 |