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