]> sigrok.org Git - libsigrokdecode.git/blob - module_sigrokdecode.c
Added "autostuff" to the .gitignore.
[libsigrokdecode.git] / module_sigrokdecode.c
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 "config.h"
22
23 /* lives in type_logic.c */
24 extern PyTypeObject srd_logic_type;
25
26
27 /* TODO: not used, doesn't work actually */
28 static PyObject *Decoder_init(PyObject *self, PyObject *args)
29 {
30         (void)self;
31         (void)args;
32         printf("init Decoder object %p\n", self);
33
34         Py_RETURN_NONE;
35 }
36
37 static PyObject *Decoder_put(PyObject *self, PyObject *args)
38 {
39         GSList *l;
40         PyObject *data;
41         struct srd_decoder_instance *di;
42         struct srd_pd_output *pdo;
43         uint64_t timeoffset, duration;
44         int output_id;
45
46         if (!(di = get_di_by_decobject(self)))
47                 return NULL;
48
49         if (!PyArg_ParseTuple(args, "KKiO", &timeoffset, &duration, &output_id, &data))
50                 return NULL;
51
52         if (!(l = g_slist_nth(di->pd_output, output_id))) {
53                 /* PD supplied invalid output id */
54                 /* TODO: better error message */
55                 return NULL;
56         }
57         pdo = l->data;
58
59         /* TODO: SRD_OUTPUT_ANNOTATION should go back up to the caller,
60          * and SRD_OUTPUT_PROTOCOL should go up the PD stack.
61          */
62         printf("stream %d: ", pdo->output_type);
63         PyObject_Print(data, stdout, Py_PRINT_RAW);
64         puts("");
65
66         Py_RETURN_NONE;
67 }
68
69
70 static PyObject *Decoder_output_new(PyObject *self, PyObject *py_output_type)
71 {
72         PyObject *ret;
73         struct srd_decoder_instance *di;
74         char *protocol_id, *description;
75         int output_type, pdo_id;
76
77         if (!(di = get_di_by_decobject(self)))
78                 return NULL;
79
80         printf("output_new di %s\n", di->decoder->name);
81
82         if (!PyArg_ParseTuple(py_output_type, "i:output_type", &output_type))
83                 return NULL;
84
85         protocol_id = "i2c";
86         description = "blah";
87         pdo_id = pd_output_new(di, output_type, protocol_id, description);
88         if (pdo_id < 0)
89                 Py_RETURN_NONE;
90         else
91                 ret = Py_BuildValue("i", pdo_id);
92
93         return ret;
94 }
95
96 static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} };
97 static PyMethodDef Decoder_methods[] = {
98         {"put", Decoder_put, METH_VARARGS,
99          "Accepts a dictionary with the following keys: time, duration, data"},
100         {"output_new", Decoder_output_new, METH_VARARGS,
101          "Create a new output stream"},
102         {NULL, NULL, 0, NULL}
103 };
104
105
106 typedef struct {
107         PyObject_HEAD
108 } sigrok_Decoder_object;
109
110 static PyTypeObject srd_Decoder_type = {
111         PyVarObject_HEAD_INIT(NULL, 0)
112         .tp_name = "sigrokdecode.Decoder",
113         .tp_basicsize = sizeof(sigrok_Decoder_object),
114         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
115         .tp_doc = "Sigrok Decoder object",
116         .tp_methods = Decoder_methods,
117         .tp_init = (initproc) Decoder_init,
118 };
119
120 static struct PyModuleDef sigrokdecode_module = {
121         PyModuleDef_HEAD_INIT,
122         .m_name = "sigrokdecode",
123         .m_doc = "sigrokdecode base class",
124         .m_size = -1,
125         .m_methods = no_methods,
126 };
127
128 PyMODINIT_FUNC PyInit_sigrokdecode(void)
129 {
130         PyObject *mod;
131
132         /* tp_new needs to be assigned here for compiler portability */
133         srd_Decoder_type.tp_new = PyType_GenericNew;
134         if (PyType_Ready(&srd_Decoder_type) < 0)
135                 return NULL;
136
137         srd_logic_type.tp_new = PyType_GenericNew;
138         if (PyType_Ready(&srd_logic_type) < 0)
139                 return NULL;
140
141         mod = PyModule_Create(&sigrokdecode_module);
142         Py_INCREF(&srd_Decoder_type);
143         if (PyModule_AddObject(mod, "Decoder", (PyObject *)&srd_Decoder_type) == -1)
144                 return NULL;
145         Py_INCREF(&srd_logic_type);
146         if (PyModule_AddObject(mod, "srd_logic", (PyObject *)&srd_logic_type) == -1)
147                 return NULL;
148
149         return mod;
150 }
151