]>
Commit | Line | Data |
---|---|---|
d0a0ed03 | 1 | /* |
50bd5d25 | 2 | * This file is part of the libsigrokdecode project. |
d0a0ed03 BV |
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 | ||
c1f86f02 UH |
20 | #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
21 | #include "libsigrokdecode-internal.h" | |
d0a0ed03 | 22 | #include "config.h" |
dbeaab27 | 23 | #include <inttypes.h> |
d0a0ed03 | 24 | |
58572aed | 25 | /* This is only used for nicer srd_dbg() output. */ |
abeeed8b | 26 | static const char *OUTPUT_TYPES[] = { |
58572aed | 27 | "OUTPUT_ANN", |
f2a5df42 | 28 | "OUTPUT_PYTHON", |
58572aed BV |
29 | "OUTPUT_BINARY", |
30 | }; | |
31 | ||
4f75f1c1 | 32 | static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj, |
0b922460 | 33 | struct srd_proto_data *pdata) |
d0a0ed03 BV |
34 | { |
35 | PyObject *py_tmp; | |
36 | struct srd_pd_output *pdo; | |
0b922460 BV |
37 | struct srd_proto_data_annotation *pda; |
38 | int ann_format; | |
39 | char **ann_text; | |
d0a0ed03 | 40 | |
361fdcaa | 41 | /* Should be a list of [annotation format, [string, ...]]. */ |
d0a0ed03 | 42 | if (!PyList_Check(obj) && !PyTuple_Check(obj)) { |
d906d3f9 | 43 | srd_err("Protocol decoder %s submitted %s instead of list.", |
c9bfccc6 | 44 | di->decoder->name, obj->ob_type->tp_name); |
d0a0ed03 BV |
45 | return SRD_ERR_PYTHON; |
46 | } | |
47 | ||
361fdcaa | 48 | /* Should have 2 elements. */ |
d0a0ed03 | 49 | if (PyList_Size(obj) != 2) { |
c9bfccc6 UH |
50 | srd_err("Protocol decoder %s submitted annotation list with " |
51 | "%d elements instead of 2", di->decoder->name, | |
52 | PyList_Size(obj)); | |
d0a0ed03 BV |
53 | return SRD_ERR_PYTHON; |
54 | } | |
55 | ||
c9bfccc6 UH |
56 | /* |
57 | * The first element should be an integer matching a previously | |
58 | * registered annotation format. | |
59 | */ | |
d0a0ed03 BV |
60 | py_tmp = PyList_GetItem(obj, 0); |
61 | if (!PyLong_Check(py_tmp)) { | |
c9bfccc6 UH |
62 | srd_err("Protocol decoder %s submitted annotation list, but " |
63 | "first element was not an integer.", di->decoder->name); | |
d0a0ed03 BV |
64 | return SRD_ERR_PYTHON; |
65 | } | |
0b922460 BV |
66 | ann_format = PyLong_AsLong(py_tmp); |
67 | if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_format))) { | |
d906d3f9 | 68 | srd_err("Protocol decoder %s submitted data to unregistered " |
0b922460 | 69 | "annotation format %d.", di->decoder->name, ann_format); |
d0a0ed03 BV |
70 | return SRD_ERR_PYTHON; |
71 | } | |
d0a0ed03 | 72 | |
361fdcaa | 73 | /* Second element must be a list. */ |
d0a0ed03 BV |
74 | py_tmp = PyList_GetItem(obj, 1); |
75 | if (!PyList_Check(py_tmp)) { | |
d906d3f9 | 76 | srd_err("Protocol decoder %s submitted annotation list, but " |
c9bfccc6 | 77 | "second element was not a list.", di->decoder->name); |
d0a0ed03 BV |
78 | return SRD_ERR_PYTHON; |
79 | } | |
0b922460 | 80 | if (py_strlist_to_char(py_tmp, &ann_text) != SRD_OK) { |
d906d3f9 | 81 | srd_err("Protocol decoder %s submitted annotation list, but " |
c9bfccc6 | 82 | "second element was malformed.", di->decoder->name); |
d0a0ed03 BV |
83 | return SRD_ERR_PYTHON; |
84 | } | |
85 | ||
0b922460 BV |
86 | if (!(pda = g_try_malloc(sizeof(struct srd_proto_data_annotation)))) |
87 | return SRD_ERR_MALLOC; | |
88 | pda->ann_format = ann_format; | |
89 | pda->ann_text = ann_text; | |
90 | pdata->data = pda; | |
91 | ||
d0a0ed03 BV |
92 | return SRD_OK; |
93 | } | |
94 | ||
95 | static PyObject *Decoder_put(PyObject *self, PyObject *args) | |
96 | { | |
97 | GSList *l; | |
4f75f1c1 | 98 | PyObject *py_data, *py_res; |
a8b72b05 | 99 | struct srd_decoder_inst *di, *next_di; |
d0a0ed03 BV |
100 | struct srd_pd_output *pdo; |
101 | struct srd_proto_data *pdata; | |
102 | uint64_t start_sample, end_sample; | |
103 | int output_id; | |
2994587f | 104 | struct srd_pd_callback *cb; |
d0a0ed03 | 105 | |
a8b72b05 | 106 | if (!(di = srd_inst_find_by_obj(NULL, self))) { |
58572aed | 107 | /* Shouldn't happen. */ |
7a1712c4 | 108 | srd_dbg("put(): self instance not found."); |
d0a0ed03 | 109 | return NULL; |
58572aed | 110 | } |
d0a0ed03 | 111 | |
c9bfccc6 | 112 | if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, |
4f75f1c1 | 113 | &output_id, &py_data)) { |
c9bfccc6 UH |
114 | /* |
115 | * This throws an exception, but by returning NULL here we let | |
116 | * Python raise it. This results in a much better trace in | |
117 | * controller.c on the decode() method call. | |
118 | */ | |
d0a0ed03 | 119 | return NULL; |
c9bfccc6 | 120 | } |
d0a0ed03 BV |
121 | |
122 | if (!(l = g_slist_nth(di->pd_output, output_id))) { | |
d906d3f9 | 123 | srd_err("Protocol decoder %s submitted invalid output ID %d.", |
c9bfccc6 | 124 | di->decoder->name, output_id); |
d0a0ed03 BV |
125 | return NULL; |
126 | } | |
127 | pdo = l->data; | |
128 | ||
1c2b0d0b | 129 | srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.", |
a8b72b05 | 130 | di->inst_id, start_sample, end_sample, |
1c2b0d0b | 131 | OUTPUT_TYPES[pdo->output_type], output_id); |
58572aed | 132 | |
a61ece20 UH |
133 | if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) { |
134 | srd_err("Failed to g_malloc() struct srd_proto_data."); | |
d0a0ed03 | 135 | return NULL; |
a61ece20 | 136 | } |
d0a0ed03 BV |
137 | pdata->start_sample = start_sample; |
138 | pdata->end_sample = end_sample; | |
139 | pdata->pdo = pdo; | |
140 | ||
141 | switch (pdo->output_type) { | |
142 | case SRD_OUTPUT_ANN: | |
143 | /* Annotations are only fed to callbacks. */ | |
32cfb920 | 144 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { |
d0a0ed03 | 145 | /* Annotations need converting from PyObject. */ |
0b922460 | 146 | if (convert_annotation(di, py_data, pdata) != SRD_OK) { |
d0a0ed03 BV |
147 | /* An error was already logged. */ |
148 | break; | |
149 | } | |
2994587f | 150 | cb->cb(pdata, cb->cb_data); |
d0a0ed03 BV |
151 | } |
152 | break; | |
f2a5df42 | 153 | case SRD_OUTPUT_PYTHON: |
d0a0ed03 BV |
154 | for (l = di->next_di; l; l = l->next) { |
155 | next_di = l->data; | |
361fdcaa | 156 | /* TODO: Is this needed? */ |
a8b72b05 | 157 | Py_XINCREF(next_di->py_inst); |
7a1712c4 | 158 | srd_spew("Sending %d-%d to instance %s", |
c9bfccc6 | 159 | start_sample, end_sample, |
a8b72b05 | 160 | next_di->inst_id); |
c9bfccc6 | 161 | if (!(py_res = PyObject_CallMethod( |
a8b72b05 | 162 | next_di->py_inst, "decode", "KKO", start_sample, |
4f75f1c1 | 163 | end_sample, py_data))) { |
aafeeaea UH |
164 | srd_exception_catch("Calling %s decode(): ", |
165 | next_di->inst_id); | |
d0a0ed03 BV |
166 | } |
167 | Py_XDECREF(py_res); | |
168 | } | |
169 | break; | |
170 | case SRD_OUTPUT_BINARY: | |
d906d3f9 | 171 | srd_err("SRD_OUTPUT_BINARY not yet supported."); |
d0a0ed03 BV |
172 | break; |
173 | default: | |
d906d3f9 | 174 | srd_err("Protocol decoder %s submitted invalid output type %d.", |
c9bfccc6 | 175 | di->decoder->name, pdo->output_type); |
d0a0ed03 BV |
176 | break; |
177 | } | |
178 | ||
179 | g_free(pdata); | |
180 | ||
181 | Py_RETURN_NONE; | |
182 | } | |
183 | ||
d0a0ed03 BV |
184 | static PyObject *Decoder_add(PyObject *self, PyObject *args) |
185 | { | |
186 | PyObject *ret; | |
a8b72b05 | 187 | struct srd_decoder_inst *di; |
d0a0ed03 BV |
188 | char *proto_id; |
189 | int output_type, pdo_id; | |
190 | ||
a8b72b05 | 191 | if (!(di = srd_inst_find_by_obj(NULL, self))) { |
d0a0ed03 BV |
192 | PyErr_SetString(PyExc_Exception, "decoder instance not found"); |
193 | return NULL; | |
194 | } | |
195 | ||
196 | if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) { | |
511e2123 | 197 | /* Let Python raise this exception. */ |
d0a0ed03 BV |
198 | return NULL; |
199 | } | |
200 | ||
aafeeaea | 201 | pdo_id = srd_inst_pd_output_add(di, output_type, proto_id); |
d0a0ed03 BV |
202 | if (pdo_id < 0) |
203 | Py_RETURN_NONE; | |
204 | else | |
205 | ret = Py_BuildValue("i", pdo_id); | |
206 | ||
207 | return ret; | |
208 | } | |
209 | ||
210 | static PyMethodDef Decoder_methods[] = { | |
211 | {"put", Decoder_put, METH_VARARGS, | |
86528298 | 212 | "Accepts a dictionary with the following keys: startsample, endsample, data"}, |
d0a0ed03 BV |
213 | {"add", Decoder_add, METH_VARARGS, "Create a new output stream"}, |
214 | {NULL, NULL, 0, NULL} | |
215 | }; | |
216 | ||
57790bc8 | 217 | /** @cond PRIVATE */ |
55c3c5f4 | 218 | SRD_PRIV PyTypeObject srd_Decoder_type = { |
d0a0ed03 BV |
219 | PyVarObject_HEAD_INIT(NULL, 0) |
220 | .tp_name = "sigrokdecode.Decoder", | |
221 | .tp_basicsize = sizeof(srd_Decoder), | |
222 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | |
361fdcaa | 223 | .tp_doc = "sigrok Decoder base class", |
d0a0ed03 BV |
224 | .tp_methods = Decoder_methods, |
225 | }; | |
57790bc8 | 226 | /** @endcond */ |