]> sigrok.org Git - libsigrokdecode.git/blob - decoder.c
srd: configure.ac: Fix version number magic.
[libsigrokdecode.git] / decoder.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22 #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include <dirent.h>
24
25 /* The list of protocol decoders. */
26 GSList *pd_list = NULL;
27 GSList *di_list = NULL;
28
29
30 /**
31  * Returns the list of supported/loaded protocol decoders.
32  *
33  * This is a GSList containing the names of the decoders as strings.
34  *
35  * @return List of decoders, NULL if none are supported or loaded.
36  */
37 GSList *srd_list_decoders(void)
38 {
39
40         return pd_list;
41 }
42
43
44 /**
45  * Get the decoder with the specified ID.
46  *
47  * @param id The ID string of the decoder to return.
48  * @return The decoder with the specified ID, or NULL if not found.
49  */
50 struct srd_decoder *srd_get_decoder_by_id(const char *id)
51 {
52         GSList *l;
53         struct srd_decoder *dec;
54
55         for (l = srd_list_decoders(); l; l = l->next) {
56                 dec = l->data;
57                 if (!strcmp(dec->id, id))
58                         return dec;
59         }
60
61         return NULL;
62 }
63
64
65 /**
66  * TODO
67  *
68  * @param name TODO
69  *
70  * @return SRD_OK upon success, a (negative) error code otherwise.
71  */
72 int srd_load_decoder(const char *name, struct srd_decoder **dec)
73 {
74         struct srd_decoder *d;
75         PyObject *py_mod, *py_res;
76         int r;
77
78         fprintf(stdout, "%s: %s\n", __func__, name);
79
80         /* "Import" the Python module. */
81         if (!(py_mod = PyImport_ImportModule(name))) { /* NEWREF */
82                 PyErr_Print(); /* Returns void. */
83                 return SRD_ERR_PYTHON; /* TODO: More specific error? */
84         }
85
86         /* Get the 'Decoder' class as Python object. */
87         py_res = PyObject_GetAttrString(py_mod, "Decoder"); /* NEWREF */
88         if (!py_res) {
89                 if (PyErr_Occurred())
90                         PyErr_Print(); /* Returns void. */
91                 Py_XDECREF(py_mod);
92                 fprintf(stderr, "Decoder class not found in PD module %s\n", name);
93                 return SRD_ERR_PYTHON; /* TODO: More specific error? */
94         }
95
96         if (!(d = malloc(sizeof(struct srd_decoder))))
97                 return SRD_ERR_MALLOC;
98
99         if ((r = h_str(py_res, "id", &(d->id))) < 0)
100                 return r;
101
102         if ((r = h_str(py_res, "name", &(d->name))) < 0)
103                 return r;
104
105         if ((r = h_str(py_res, "longname",
106                        &(d->longname))) < 0)
107                 return r;
108
109         if ((r = h_str(py_res, "desc", &(d->desc))) < 0)
110                 return r;
111
112         if ((r = h_str(py_res, "longdesc",
113                        &(d->longdesc))) < 0)
114                 return r;
115
116         if ((r = h_str(py_res, "author", &(d->author))) < 0)
117                 return r;
118
119         if ((r = h_str(py_res, "email", &(d->email))) < 0)
120                 return r;
121
122         if ((r = h_str(py_res, "license", &(d->license))) < 0)
123                 return r;
124
125         d->py_mod = py_mod;
126         d->py_decobj = py_res;
127
128         /* TODO: Handle func, inputformats, outputformats. */
129         /* Note: They must at least be set to NULL, will segfault otherwise. */
130         d->func = NULL;
131         d->inputformats = NULL;
132         d->outputformats = NULL;
133
134         *dec = d;
135
136         return SRD_OK;
137 }
138
139
140 /**
141  * TODO
142  */
143 int srd_unload_decoder(struct srd_decoder *dec)
144 {
145         g_free(dec->id);
146         g_free(dec->name);
147         g_free(dec->desc);
148         g_free(dec->func);
149
150         /* TODO: Free everything in inputformats and outputformats. */
151
152         if (dec->inputformats != NULL)
153                 g_slist_free(dec->inputformats);
154         if (dec->outputformats != NULL)
155                 g_slist_free(dec->outputformats);
156
157         Py_XDECREF(dec->py_decobj);
158         Py_XDECREF(dec->py_mod);
159
160         /* TODO: (g_)free dec itself? */
161
162         return SRD_OK;
163 }
164
165
166 int srd_load_all_decoders(void)
167 {
168         DIR *dir;
169         struct dirent *dp;
170         int ret;
171         char *decodername;
172         struct srd_decoder *dec;
173
174         if (!(dir = opendir(DECODERS_DIR))) {
175                 Py_Finalize(); /* Returns void. */
176                 return SRD_ERR_DECODERS_DIR;
177         }
178
179         while ((dp = readdir(dir)) != NULL) {
180                 /* Ignore filenames which don't end with ".py". */
181                 if (!g_str_has_suffix(dp->d_name, ".py"))
182                         continue;
183
184                 /* Decoder name == filename (without .py suffix). */
185                 decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3);
186
187                 /* TODO: Error handling. Use g_try_malloc(). */
188                 if (!(dec = malloc(sizeof(struct srd_decoder)))) {
189                         Py_Finalize(); /* Returns void. */
190                         return SRD_ERR_MALLOC;
191                 }
192
193                 /* Load the decoder. */
194                 /* TODO: Warning if loading fails for a decoder. */
195                 if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) {
196                         /* Append it to the list of supported/loaded decoders. */
197                         pd_list = g_slist_append(pd_list, dec);
198                 }
199         }
200         closedir(dir);
201
202         return SRD_OK;
203 }
204
205
206 /**
207  * TODO
208  */
209 int srd_unload_all_decoders(void)
210 {
211         GSList *l;
212         struct srd_decoder *dec;
213
214         for (l = srd_list_decoders(); l; l = l->next) {
215                 dec = l->data;
216                 /* TODO: Error handling. */
217                 srd_unload_decoder(dec);
218         }
219
220         return SRD_OK;
221 }
222
223
224