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