]> sigrok.org Git - libsigrokdecode.git/blob - decode.c
d4890682e681c9e2dc72ad547c1fa0f9247d2a03
[libsigrokdecode.git] / decode.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include <stdio.h>
23
24 /**
25  * Initialize libsigrokdecode.
26  *
27  * @return 0 upon success, non-zero otherwise.
28  */
29 int sigrokdecode_init(void)
30 {
31         /* Py_Initialize() returns void and usually cannot fail. */
32         Py_Initialize();
33
34         /* FIXME */
35         /* Allows for ./gui/sigrok-gui in the top-level directory. */
36         PySys_SetPath("libsigrokdecode/scripts");
37         /* Allows for ./sigrok-gui in the gui/ directory. */
38         // PySys_SetPath("../libsigrokdecode/scripts");
39         /* Allows for sigrok-gui from anywhere given sigrok is installed. */
40         // PySys_SetPath("/usr/local/share/sigrok");
41
42         return 0;
43 }
44
45 /**
46  * TODO
47  *
48  * @param name TODO
49  * @return 0 upon success, non-zero otherwise.
50  */
51 int sigrokdecode_load_decoder_file(const char *name)
52 {
53         /* QUICK HACK */
54         name = name;
55
56         /* TODO */
57         return 0;
58 }
59
60 /**
61  * Run the specified decoder function.
62  *
63  * @param decodername TODO
64  * @param inbuf TODO
65  * @param inbuflen TODO
66  * @param outbuf TODO
67  * @param outbuflen TODO
68  * @return 0 upon success, non-zero otherwise.
69  */
70 int sigrokdecode_run_decoder(const char *decodername, uint8_t *inbuf,
71                              uint64_t inbuflen, uint8_t **outbuf,
72                              uint64_t *outbuflen)
73 {
74         // const char *decoder_filename = "transitioncounter"; /* FIXME */
75         const char *decoder_filename = "i2c"; /* FIXME */
76         PyObject *py_name, *py_module, *py_func, *py_args;
77         PyObject *py_value, *py_result;
78         int ret;
79
80         /* TODO: Use #defines for the return codes. */
81
82         /* Return an error upon unusable input. */
83         if (decodername == NULL)
84                 return -1;
85         if (inbuf == NULL)
86                 return -2;
87         if (inbuflen == 0) /* No point in working on empty buffers. */
88                 return -3;
89         if (outbuf == NULL)
90                 return -4;
91         if (outbuflen == NULL)
92                 return -5;
93
94         /* Get the name of the decoder module/file as Python string. */
95         if (!(py_name = PyString_FromString(decoder_filename))) {
96                 PyErr_Print();
97                 return -6;
98         }
99
100         /* "Import" the python file/module. */
101         if (!(py_module = PyImport_Import(py_name))) {
102                 PyErr_Print();
103                 Py_DECREF(py_name);
104                 return -7;
105         }
106         Py_DECREF(py_name);
107
108         /* Get the decoder/function name as Python callable object. */
109         py_func = PyObject_GetAttrString(py_module, decodername);
110         if (!py_func || !PyCallable_Check(py_func)) {
111                 if (PyErr_Occurred())
112                         PyErr_Print();
113                 Py_DECREF(py_module);
114                 return -8;
115         }
116
117         /* Create a Python tuple of size 1. */
118         if (!(py_args = PyTuple_New(1))) {
119                 PyErr_Print();
120                 Py_DECREF(py_func);
121                 Py_DECREF(py_module);
122                 return -9;
123         }
124
125         /* Get the input buffer as Python "string" (byte array). */
126         /* TODO: int vs. uint64_t for 'inbuflen'? */
127         if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
128                 PyErr_Print();
129                 Py_DECREF(py_args);
130                 Py_DECREF(py_func);
131                 Py_DECREF(py_module);
132                 return -10;
133         }
134
135         if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
136                 PyErr_Print();
137                 Py_DECREF(py_value);
138                 Py_DECREF(py_args);
139                 Py_DECREF(py_func);
140                 Py_DECREF(py_module);
141                 return -11;
142         }
143
144         if (!(py_result = PyObject_CallObject(py_func, py_args))) {
145                 PyErr_Print();
146                 Py_DECREF(py_value);
147                 Py_DECREF(py_args);
148                 Py_DECREF(py_func);
149                 Py_DECREF(py_module);
150                 return -12;
151         }
152
153         if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf,
154                                          (Py_ssize_t *)outbuflen))) {
155                 PyErr_Print();
156                 Py_DECREF(py_result);
157                 Py_DECREF(py_value);
158                 Py_DECREF(py_args);
159                 Py_DECREF(py_func);
160                 Py_DECREF(py_module);
161                 return -13;
162         }
163
164         Py_DECREF(py_result);
165         // Py_DECREF(py_value);
166         Py_DECREF(py_args);
167         Py_DECREF(py_func);
168         Py_DECREF(py_module);
169
170         return 0;
171 }
172
173 /**
174  * Shutdown libsigrokdecode.
175  *
176  * @return 0 upon success, non-zero otherwise.
177  */
178 int sigrokdecode_shutdown(void)
179 {
180         /* Py_Finalize() returns void, any finalization errors are ignored. */
181         Py_Finalize();
182
183         return 0;
184 }