Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 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. */ |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 21 | #include "sigrokdecode-internal.h" |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
| 24 | /* lives in type_logic.c */ |
| 25 | extern PyTypeObject srd_logic_type; |
| 26 | |
| 27 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 28 | static int convert_pyobj(struct srd_decoder_instance *di, PyObject *obj, |
| 29 | int *annotation_format, char ***annotation) |
| 30 | { |
| 31 | PyObject *py_tmp; |
| 32 | struct srd_pd_output *pdo; |
| 33 | int ann_id; |
| 34 | |
| 35 | /* Should be a list of [annotation format, [string, ...]] */ |
| 36 | if (!PyList_Check(obj) && !PyTuple_Check(obj)) { |
| 37 | srd_err("Protocol decoder %s submitted %s instead of list", |
| 38 | di->decoder->name, obj->ob_type->tp_name); |
| 39 | return SRD_ERR_PYTHON; |
| 40 | } |
| 41 | |
| 42 | /* Should have 2 elements... */ |
| 43 | if (PyList_Size(obj) != 2) { |
| 44 | srd_err("Protocol decoder %s submitted annotation list with %d elements instead of 2", |
| 45 | di->decoder->name, PyList_Size(obj)); |
| 46 | return SRD_ERR_PYTHON; |
| 47 | } |
| 48 | |
| 49 | /* First element should be an integer matching a previously |
| 50 | * registered annotation format. */ |
| 51 | py_tmp = PyList_GetItem(obj, 0); |
| 52 | if (!PyLong_Check(py_tmp)) { |
| 53 | srd_err("Protocol decoder %s submitted annotation list, but first element was not an integer", |
| 54 | di->decoder->name); |
| 55 | return SRD_ERR_PYTHON; |
| 56 | } |
| 57 | |
| 58 | ann_id = PyLong_AsLong(py_tmp); |
| 59 | if (!(pdo = g_slist_nth_data(di->decoder->annotation, ann_id))) { |
| 60 | srd_err("Protocol decoder %s submitted data to non-existent annotation format %d", |
| 61 | di->decoder->name, ann_id); |
| 62 | return SRD_ERR_PYTHON; |
| 63 | } |
| 64 | *annotation_format = ann_id; |
| 65 | |
| 66 | /* Second element must be a list */ |
| 67 | py_tmp = PyList_GetItem(obj, 1); |
| 68 | if (!PyList_Check(py_tmp)) { |
| 69 | srd_err("Protocol decoder %s submitted annotation list, but second element was not a list", |
| 70 | di->decoder->name); |
| 71 | return SRD_ERR_PYTHON; |
| 72 | } |
| 73 | if (py_strlist_to_char(py_tmp, annotation) != SRD_OK) { |
| 74 | srd_err("Protocol decoder %s submitted annotation list, but second element was malformed", |
| 75 | di->decoder->name); |
| 76 | return SRD_ERR_PYTHON; |
| 77 | } |
| 78 | |
| 79 | return SRD_OK; |
| 80 | } |
| 81 | |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 82 | /* TODO: not used, doesn't work actually */ |
| 83 | static PyObject *Decoder_init(PyObject *self, PyObject *args) |
| 84 | { |
| 85 | (void)self; |
| 86 | (void)args; |
| 87 | printf("init Decoder object %p\n", self); |
| 88 | |
| 89 | Py_RETURN_NONE; |
| 90 | } |
| 91 | |
| 92 | static PyObject *Decoder_put(PyObject *self, PyObject *args) |
| 93 | { |
| 94 | GSList *l; |
| 95 | PyObject *data; |
| 96 | struct srd_decoder_instance *di; |
| 97 | struct srd_pd_output *pdo; |
| 98 | uint64_t timeoffset, duration; |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 99 | int output_id, annotation_format, i; |
| 100 | char **annotation, **ann_info; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 101 | |
| 102 | if (!(di = get_di_by_decobject(self))) |
| 103 | return NULL; |
| 104 | |
| 105 | if (!PyArg_ParseTuple(args, "KKiO", &timeoffset, &duration, &output_id, &data)) |
| 106 | return NULL; |
| 107 | |
| 108 | if (!(l = g_slist_nth(di->pd_output, output_id))) { |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 109 | srd_err("Protocol decoder %s submitted invalid output ID %d", |
| 110 | di->decoder->name, output_id); |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
| 113 | pdo = l->data; |
| 114 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 115 | switch (pdo->output_type) { |
| 116 | case SRD_OUTPUT_ANNOTATION: |
| 117 | if (convert_pyobj(di, data, &annotation_format, &annotation) != SRD_OK) |
| 118 | return NULL; |
| 119 | |
| 120 | /* TODO: SRD_OUTPUT_ANNOTATION should go back up to the caller */ |
| 121 | ann_info = g_slist_nth_data(pdo->decoder->annotation, annotation_format); |
| 122 | printf("annotation format %d (%s): ", annotation_format, ann_info[0]); |
| 123 | for (i = 0; annotation[i]; i++) |
| 124 | printf("\"%s\" ", annotation[i]); |
| 125 | printf("\n"); |
| 126 | break; |
| 127 | |
| 128 | case SRD_OUTPUT_PROTOCOL: |
| 129 | |
| 130 | /* TODO: SRD_OUTPUT_PROTOCOL should go up the PD stack. */ |
Bert Vermeulen | 0ee4dd5 | 2012-01-07 03:59:16 +0100 | [diff] [blame^] | 131 | printf("%s protocol data: ", pdo->protocol_id); |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 132 | PyObject_Print(data, stdout, Py_PRINT_RAW); |
| 133 | puts(""); |
| 134 | break; |
| 135 | |
| 136 | default: |
| 137 | srd_err("Protocol decoder %s submitted invalid output type %d", |
| 138 | di->decoder->name, pdo->output_type); |
| 139 | break; |
| 140 | } |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 141 | |
| 142 | Py_RETURN_NONE; |
| 143 | } |
| 144 | |
| 145 | |
Bert Vermeulen | 0ee4dd5 | 2012-01-07 03:59:16 +0100 | [diff] [blame^] | 146 | static PyObject *Decoder_output_new(PyObject *self, PyObject *args) |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 147 | { |
| 148 | PyObject *ret; |
| 149 | struct srd_decoder_instance *di; |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 150 | char *protocol_id; |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 151 | int output_type, pdo_id; |
| 152 | |
| 153 | if (!(di = get_di_by_decobject(self))) |
| 154 | return NULL; |
| 155 | |
| 156 | printf("output_new di %s\n", di->decoder->name); |
| 157 | |
Bert Vermeulen | 0ee4dd5 | 2012-01-07 03:59:16 +0100 | [diff] [blame^] | 158 | if (!PyArg_ParseTuple(args, "is", &output_type, &protocol_id)) |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 159 | return NULL; |
| 160 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 161 | pdo_id = pd_output_new(di, output_type, protocol_id); |
Bert Vermeulen | bc5f5a4 | 2012-01-05 03:31:36 +0100 | [diff] [blame] | 162 | if (pdo_id < 0) |
| 163 | Py_RETURN_NONE; |
| 164 | else |
| 165 | ret = Py_BuildValue("i", pdo_id); |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} }; |
| 171 | static PyMethodDef Decoder_methods[] = { |
| 172 | {"put", Decoder_put, METH_VARARGS, |
| 173 | "Accepts a dictionary with the following keys: time, duration, data"}, |
| 174 | {"output_new", Decoder_output_new, METH_VARARGS, |
| 175 | "Create a new output stream"}, |
| 176 | {NULL, NULL, 0, NULL} |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | typedef struct { |
| 181 | PyObject_HEAD |
| 182 | } sigrok_Decoder_object; |
| 183 | |
| 184 | static PyTypeObject srd_Decoder_type = { |
| 185 | PyVarObject_HEAD_INIT(NULL, 0) |
| 186 | .tp_name = "sigrokdecode.Decoder", |
| 187 | .tp_basicsize = sizeof(sigrok_Decoder_object), |
| 188 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 189 | .tp_doc = "Sigrok Decoder object", |
| 190 | .tp_methods = Decoder_methods, |
| 191 | .tp_init = (initproc) Decoder_init, |
| 192 | }; |
| 193 | |
| 194 | static struct PyModuleDef sigrokdecode_module = { |
| 195 | PyModuleDef_HEAD_INIT, |
| 196 | .m_name = "sigrokdecode", |
| 197 | .m_doc = "sigrokdecode base class", |
| 198 | .m_size = -1, |
| 199 | .m_methods = no_methods, |
| 200 | }; |
| 201 | |
| 202 | PyMODINIT_FUNC PyInit_sigrokdecode(void) |
| 203 | { |
| 204 | PyObject *mod; |
| 205 | |
| 206 | /* tp_new needs to be assigned here for compiler portability */ |
| 207 | srd_Decoder_type.tp_new = PyType_GenericNew; |
| 208 | if (PyType_Ready(&srd_Decoder_type) < 0) |
| 209 | return NULL; |
| 210 | |
| 211 | srd_logic_type.tp_new = PyType_GenericNew; |
| 212 | if (PyType_Ready(&srd_logic_type) < 0) |
| 213 | return NULL; |
| 214 | |
| 215 | mod = PyModule_Create(&sigrokdecode_module); |
| 216 | Py_INCREF(&srd_Decoder_type); |
| 217 | if (PyModule_AddObject(mod, "Decoder", (PyObject *)&srd_Decoder_type) == -1) |
| 218 | return NULL; |
| 219 | Py_INCREF(&srd_logic_type); |
| 220 | if (PyModule_AddObject(mod, "srd_logic", (PyObject *)&srd_logic_type) == -1) |
| 221 | return NULL; |
| 222 | |
| 223 | return mod; |
| 224 | } |
| 225 | |