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