]> sigrok.org Git - libsigrokdecode.git/blame - decode.c
Fix all warnings and re-enable -Wextra.
[libsigrokdecode.git] / decode.c
CommitLineData
1c6fa20f
UH
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 */
29int sigrokdecode_init(void)
30{
31 /* Py_Initialize() returns void and usually cannot fail. */
32 Py_Initialize();
33
34 /* FIXME */
3a72d2b3 35 /* Allows for ./gui/sigrok-gui in the top-level directory. */
3aca097f 36 PySys_SetPath("libsigrokdecode/scripts");
3a72d2b3
UH
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");
1c6fa20f
UH
41
42 return 0;
43}
44
45/**
46 * TODO
47 *
48 * @param name TODO
49 * @return 0 upon success, non-zero otherwise.
50 */
51int sigrokdecode_load_decoder_file(const char *name)
52{
910e5fd8
UH
53 /* QUICK HACK */
54 name = name;
55
1c6fa20f
UH
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 */
70int 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 PyObject *py_name, *py_module, *py_func, *py_args;
76 PyObject *py_value, *py_result;
77 int ret;
78
79 /* TODO: Use #defines for the return codes. */
80
81 /* Return an error upon unusable input. */
82 if (decodername == NULL)
83 return -1;
84 if (inbuf == NULL)
85 return -2;
86 if (inbuflen == 0) /* No point in working on empty buffers. */
87 return -3;
88 if (outbuf == NULL)
89 return -4;
90 if (outbuflen == NULL)
91 return -5;
92
93 /* Get the name of the decoder module/file as Python string. */
94 if (!(py_name = PyString_FromString(decoder_filename))) {
95 PyErr_Print();
96 return -6;
97 }
98
99 /* "Import" the python file/module. */
100 if (!(py_module = PyImport_Import(py_name))) {
101 PyErr_Print();
102 Py_DECREF(py_name);
103 return -7;
104 }
105 Py_DECREF(py_name);
106
107 /* Get the decoder/function name as Python callable object. */
108 py_func = PyObject_GetAttrString(py_module, decodername);
109 if (!py_func || !PyCallable_Check(py_func)) {
110 if (PyErr_Occurred())
111 PyErr_Print();
112 Py_DECREF(py_module);
113 return -8;
114 }
115
116 /* Create a Python tuple of size 1. */
117 if (!(py_args = PyTuple_New(1))) {
118 PyErr_Print();
119 Py_DECREF(py_func);
120 Py_DECREF(py_module);
121 return -9;
122 }
123
124 /* Get the input buffer as Python "string" (byte array). */
125 /* TODO: int vs. uint64_t for 'inbuflen'? */
126 if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
127 PyErr_Print();
128 Py_DECREF(py_args);
129 Py_DECREF(py_func);
130 Py_DECREF(py_module);
131 return -10;
132 }
133
134 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
135 PyErr_Print();
136 Py_DECREF(py_value);
137 Py_DECREF(py_args);
138 Py_DECREF(py_func);
139 Py_DECREF(py_module);
140 return -11;
141 }
142
143 if (!(py_result = PyObject_CallObject(py_func, py_args))) {
144 PyErr_Print();
145 Py_DECREF(py_value);
146 Py_DECREF(py_args);
147 Py_DECREF(py_func);
148 Py_DECREF(py_module);
149 return -12;
150 }
151
152 if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf,
153 (Py_ssize_t *)outbuflen))) {
154 PyErr_Print();
155 Py_DECREF(py_result);
156 Py_DECREF(py_value);
157 Py_DECREF(py_args);
158 Py_DECREF(py_func);
159 Py_DECREF(py_module);
160 return -13;
161 }
162
163 Py_DECREF(py_result);
164 // Py_DECREF(py_value);
165 Py_DECREF(py_args);
166 Py_DECREF(py_func);
167 Py_DECREF(py_module);
168
169 return 0;
170}
171
172/**
173 * Shutdown libsigrokdecode.
174 *
175 * @return 0 upon success, non-zero otherwise.
176 */
177int sigrokdecode_shutdown(void)
178{
179 /* Py_Finalize() returns void, any finalization errors are ignored. */
180 Py_Finalize();
181
182 return 0;
183}