]> sigrok.org Git - libsigrokdecode.git/blame - type_decoder.c
srd: All PDs: Various fixes, cosmetics.
[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"
23
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
32
d0a0ed03
BV
33static 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
87static 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
BV
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
2072ae0c
BV
117 srd_spew("srd: instance %s put %d-%d %s on oid %d", di->instance_id,
118 start_sample, end_sample, OUTPUT_TYPES[pdo->output_type], output_id);
58572aed 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);
2072ae0c
BV
144 srd_spew("srd: sending %d-%d to instance %s",
145 start_sample, end_sample, next_di->instance_id);
d0a0ed03
BV
146 if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode",
147 "KKO", start_sample, end_sample, data))) {
2086c684 148 catch_exception("calling %s decode(): ", next_di->instance_id);
d0a0ed03
BV
149 }
150 Py_XDECREF(py_res);
151 }
152 break;
153 case SRD_OUTPUT_BINARY:
d906d3f9 154 srd_err("SRD_OUTPUT_BINARY not yet supported.");
d0a0ed03
BV
155 break;
156 default:
d906d3f9 157 srd_err("Protocol decoder %s submitted invalid output type %d.",
d0a0ed03
BV
158 di->decoder->name, pdo->output_type);
159 break;
160 }
161
162 g_free(pdata);
163
164 Py_RETURN_NONE;
165}
166
167
168static PyObject *Decoder_add(PyObject *self, PyObject *args)
169{
170 PyObject *ret;
171 struct srd_decoder_instance *di;
172 char *proto_id;
173 int output_type, pdo_id;
174
2072ae0c 175 if (!(di = srd_instance_find_by_obj(NULL, self))) {
d0a0ed03
BV
176 PyErr_SetString(PyExc_Exception, "decoder instance not found");
177 return NULL;
178 }
179
180 if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
58572aed 181 /* Let python raise this exception. */
d0a0ed03
BV
182 return NULL;
183 }
184
185 pdo_id = pd_add(di, output_type, proto_id);
186 if (pdo_id < 0)
187 Py_RETURN_NONE;
188 else
189 ret = Py_BuildValue("i", pdo_id);
190
191 return ret;
192}
193
194static PyMethodDef Decoder_methods[] = {
195 {"put", Decoder_put, METH_VARARGS,
86528298 196 "Accepts a dictionary with the following keys: startsample, endsample, data"},
d0a0ed03
BV
197 {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
198 {NULL, NULL, 0, NULL}
199};
200
201
202PyTypeObject srd_Decoder_type = {
203 PyVarObject_HEAD_INIT(NULL, 0)
204 .tp_name = "sigrokdecode.Decoder",
205 .tp_basicsize = sizeof(srd_Decoder),
206 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
207 .tp_doc = "Sigrok Decoder base class",
208 .tp_methods = Decoder_methods,
209};
210