]>
Commit | Line | Data |
---|---|---|
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 | */ | |
29 | int sigrokdecode_init(void) | |
30 | { | |
31 | /* Py_Initialize() returns void and usually cannot fail. */ | |
32 | Py_Initialize(); | |
33 | ||
34 | /* FIXME */ | |
3aca097f | 35 | PySys_SetPath("libsigrokdecode/scripts"); |
1c6fa20f UH |
36 | |
37 | return 0; | |
38 | } | |
39 | ||
40 | /** | |
41 | * TODO | |
42 | * | |
43 | * @param name TODO | |
44 | * @return 0 upon success, non-zero otherwise. | |
45 | */ | |
46 | int sigrokdecode_load_decoder_file(const char *name) | |
47 | { | |
48 | /* TODO */ | |
49 | return 0; | |
50 | } | |
51 | ||
52 | /** | |
53 | * Run the specified decoder function. | |
54 | * | |
55 | * @param decodername TODO | |
56 | * @param inbuf TODO | |
57 | * @param inbuflen TODO | |
58 | * @param outbuf TODO | |
59 | * @param outbuflen TODO | |
60 | * @return 0 upon success, non-zero otherwise. | |
61 | */ | |
62 | int sigrokdecode_run_decoder(const char *decodername, uint8_t *inbuf, | |
63 | uint64_t inbuflen, uint8_t **outbuf, | |
64 | uint64_t *outbuflen) | |
65 | { | |
66 | const char *decoder_filename = "transitioncounter"; /* FIXME */ | |
67 | PyObject *py_name, *py_module, *py_func, *py_args; | |
68 | PyObject *py_value, *py_result; | |
69 | int ret; | |
70 | ||
71 | /* TODO: Use #defines for the return codes. */ | |
72 | ||
73 | /* Return an error upon unusable input. */ | |
74 | if (decodername == NULL) | |
75 | return -1; | |
76 | if (inbuf == NULL) | |
77 | return -2; | |
78 | if (inbuflen == 0) /* No point in working on empty buffers. */ | |
79 | return -3; | |
80 | if (outbuf == NULL) | |
81 | return -4; | |
82 | if (outbuflen == NULL) | |
83 | return -5; | |
84 | ||
85 | /* Get the name of the decoder module/file as Python string. */ | |
86 | if (!(py_name = PyString_FromString(decoder_filename))) { | |
87 | PyErr_Print(); | |
88 | return -6; | |
89 | } | |
90 | ||
91 | /* "Import" the python file/module. */ | |
92 | if (!(py_module = PyImport_Import(py_name))) { | |
93 | PyErr_Print(); | |
94 | Py_DECREF(py_name); | |
95 | return -7; | |
96 | } | |
97 | Py_DECREF(py_name); | |
98 | ||
99 | /* Get the decoder/function name as Python callable object. */ | |
100 | py_func = PyObject_GetAttrString(py_module, decodername); | |
101 | if (!py_func || !PyCallable_Check(py_func)) { | |
102 | if (PyErr_Occurred()) | |
103 | PyErr_Print(); | |
104 | Py_DECREF(py_module); | |
105 | return -8; | |
106 | } | |
107 | ||
108 | /* Create a Python tuple of size 1. */ | |
109 | if (!(py_args = PyTuple_New(1))) { | |
110 | PyErr_Print(); | |
111 | Py_DECREF(py_func); | |
112 | Py_DECREF(py_module); | |
113 | return -9; | |
114 | } | |
115 | ||
116 | /* Get the input buffer as Python "string" (byte array). */ | |
117 | /* TODO: int vs. uint64_t for 'inbuflen'? */ | |
118 | if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) { | |
119 | PyErr_Print(); | |
120 | Py_DECREF(py_args); | |
121 | Py_DECREF(py_func); | |
122 | Py_DECREF(py_module); | |
123 | return -10; | |
124 | } | |
125 | ||
126 | if (PyTuple_SetItem(py_args, 0, py_value) != 0) { | |
127 | PyErr_Print(); | |
128 | Py_DECREF(py_value); | |
129 | Py_DECREF(py_args); | |
130 | Py_DECREF(py_func); | |
131 | Py_DECREF(py_module); | |
132 | return -11; | |
133 | } | |
134 | ||
135 | if (!(py_result = PyObject_CallObject(py_func, py_args))) { | |
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 -12; | |
142 | } | |
143 | ||
144 | if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf, | |
145 | (Py_ssize_t *)outbuflen))) { | |
146 | PyErr_Print(); | |
147 | Py_DECREF(py_result); | |
148 | Py_DECREF(py_value); | |
149 | Py_DECREF(py_args); | |
150 | Py_DECREF(py_func); | |
151 | Py_DECREF(py_module); | |
152 | return -13; | |
153 | } | |
154 | ||
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 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | /** | |
165 | * Shutdown libsigrokdecode. | |
166 | * | |
167 | * @return 0 upon success, non-zero otherwise. | |
168 | */ | |
169 | int sigrokdecode_shutdown(void) | |
170 | { | |
171 | /* Py_Finalize() returns void, any finalization errors are ignored. */ | |
172 | Py_Finalize(); | |
173 | ||
174 | return 0; | |
175 | } |