]> sigrok.org Git - libsigrokdecode.git/blame - decode.c
More paths to the libsigrokdecode Python scripts.
[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{
53 /* TODO */
54 return 0;
55}
56
57/**
58 * Run the specified decoder function.
59 *
60 * @param decodername TODO
61 * @param inbuf TODO
62 * @param inbuflen TODO
63 * @param outbuf TODO
64 * @param outbuflen TODO
65 * @return 0 upon success, non-zero otherwise.
66 */
67int sigrokdecode_run_decoder(const char *decodername, uint8_t *inbuf,
68 uint64_t inbuflen, uint8_t **outbuf,
69 uint64_t *outbuflen)
70{
71 const char *decoder_filename = "transitioncounter"; /* FIXME */
72 PyObject *py_name, *py_module, *py_func, *py_args;
73 PyObject *py_value, *py_result;
74 int ret;
75
76 /* TODO: Use #defines for the return codes. */
77
78 /* Return an error upon unusable input. */
79 if (decodername == NULL)
80 return -1;
81 if (inbuf == NULL)
82 return -2;
83 if (inbuflen == 0) /* No point in working on empty buffers. */
84 return -3;
85 if (outbuf == NULL)
86 return -4;
87 if (outbuflen == NULL)
88 return -5;
89
90 /* Get the name of the decoder module/file as Python string. */
91 if (!(py_name = PyString_FromString(decoder_filename))) {
92 PyErr_Print();
93 return -6;
94 }
95
96 /* "Import" the python file/module. */
97 if (!(py_module = PyImport_Import(py_name))) {
98 PyErr_Print();
99 Py_DECREF(py_name);
100 return -7;
101 }
102 Py_DECREF(py_name);
103
104 /* Get the decoder/function name as Python callable object. */
105 py_func = PyObject_GetAttrString(py_module, decodername);
106 if (!py_func || !PyCallable_Check(py_func)) {
107 if (PyErr_Occurred())
108 PyErr_Print();
109 Py_DECREF(py_module);
110 return -8;
111 }
112
113 /* Create a Python tuple of size 1. */
114 if (!(py_args = PyTuple_New(1))) {
115 PyErr_Print();
116 Py_DECREF(py_func);
117 Py_DECREF(py_module);
118 return -9;
119 }
120
121 /* Get the input buffer as Python "string" (byte array). */
122 /* TODO: int vs. uint64_t for 'inbuflen'? */
123 if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
124 PyErr_Print();
125 Py_DECREF(py_args);
126 Py_DECREF(py_func);
127 Py_DECREF(py_module);
128 return -10;
129 }
130
131 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
132 PyErr_Print();
133 Py_DECREF(py_value);
134 Py_DECREF(py_args);
135 Py_DECREF(py_func);
136 Py_DECREF(py_module);
137 return -11;
138 }
139
140 if (!(py_result = PyObject_CallObject(py_func, py_args))) {
141 PyErr_Print();
142 Py_DECREF(py_value);
143 Py_DECREF(py_args);
144 Py_DECREF(py_func);
145 Py_DECREF(py_module);
146 return -12;
147 }
148
149 if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf,
150 (Py_ssize_t *)outbuflen))) {
151 PyErr_Print();
152 Py_DECREF(py_result);
153 Py_DECREF(py_value);
154 Py_DECREF(py_args);
155 Py_DECREF(py_func);
156 Py_DECREF(py_module);
157 return -13;
158 }
159
160 Py_DECREF(py_result);
161 // Py_DECREF(py_value);
162 Py_DECREF(py_args);
163 Py_DECREF(py_func);
164 Py_DECREF(py_module);
165
166 return 0;
167}
168
169/**
170 * Shutdown libsigrokdecode.
171 *
172 * @return 0 upon success, non-zero otherwise.
173 */
174int sigrokdecode_shutdown(void)
175{
176 /* Py_Finalize() returns void, any finalization errors are ignored. */
177 Py_Finalize();
178
179 return 0;
180}