Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 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" |
Uwe Hermann | 7319141 | 2012-01-03 19:56:01 +0100 | [diff] [blame] | 22 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 23 | #include "sigrokdecode-internal.h" |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 24 | #include <dirent.h> |
| 25 | |
| 26 | /* The list of protocol decoders. */ |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 27 | GSList *pd_list = NULL; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 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 | |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 40 | return pd_list; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 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 | /** |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 66 | * Load a protocol decoder module into the embedded python interpreter. |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 67 | * |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 68 | * @param name The module name to be loaded. |
| 69 | * @param dec Pointer to the struct srd_decoder filled with the loaded module. |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 70 | * |
| 71 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 72 | */ |
| 73 | int srd_load_decoder(const char *name, struct srd_decoder **dec) |
| 74 | { |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 75 | PyObject *py_mod, *py_res, *py_annlist, *py_ann; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 76 | struct srd_decoder *d; |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 77 | int alen, r, i; |
| 78 | char **ann; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 79 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 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); |
Bert Vermeulen | b231546 | 2012-01-09 00:13:03 +0100 | [diff] [blame] | 92 | srd_err("Decoder class not found in PD module %s", name); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 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 | |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 99 | if ((r = h_str(py_res, "id", &(d->id))) < 0) |
Uwe Hermann | 09b0acb | 2011-12-04 23:36:52 +0100 | [diff] [blame] | 100 | return r; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 101 | |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 102 | if ((r = h_str(py_res, "name", &(d->name))) < 0) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 103 | return r; |
| 104 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 105 | if ((r = h_str(py_res, "longname", &(d->longname))) < 0) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 106 | return r; |
| 107 | |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 108 | if ((r = h_str(py_res, "desc", &(d->desc))) < 0) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 109 | return r; |
| 110 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 111 | if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 112 | return r; |
| 113 | |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 114 | if ((r = h_str(py_res, "author", &(d->author))) < 0) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 115 | return r; |
| 116 | |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 117 | if ((r = h_str(py_res, "license", &(d->license))) < 0) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 118 | return r; |
| 119 | |
| 120 | d->py_mod = py_mod; |
| 121 | d->py_decobj = py_res; |
| 122 | |
| 123 | /* TODO: Handle func, inputformats, outputformats. */ |
| 124 | /* Note: They must at least be set to NULL, will segfault otherwise. */ |
| 125 | d->func = NULL; |
| 126 | d->inputformats = NULL; |
| 127 | d->outputformats = NULL; |
| 128 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 129 | /* Convert class annotation attribute to GSList of **char */ |
Uwe Hermann | e97b6ef | 2012-01-10 21:05:09 +0100 | [diff] [blame] | 130 | d->annotations = NULL; |
| 131 | if (PyObject_HasAttrString(py_res, "annotations")) { |
| 132 | py_annlist = PyObject_GetAttrString(py_res, "annotations"); |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 133 | if (!PyList_Check(py_annlist)) { |
Uwe Hermann | e97b6ef | 2012-01-10 21:05:09 +0100 | [diff] [blame] | 134 | srd_err("Protocol decoder module %s annotations should be a list", name); |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 135 | return SRD_ERR_PYTHON; |
| 136 | } |
| 137 | alen = PyList_Size(py_annlist); |
| 138 | for (i = 0; i < alen; i++) { |
| 139 | py_ann = PyList_GetItem(py_annlist, i); |
| 140 | if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) { |
| 141 | srd_err("Protocol decoder module %s annotation %d should be a list with two elements", |
| 142 | name, i+1); |
| 143 | return SRD_ERR_PYTHON; |
| 144 | } |
| 145 | |
| 146 | if (py_strlist_to_char(py_ann, &ann) != SRD_OK) |
| 147 | return SRD_ERR_PYTHON; |
Uwe Hermann | e97b6ef | 2012-01-10 21:05:09 +0100 | [diff] [blame] | 148 | d->annotations = g_slist_append(d->annotations, ann); |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 152 | *dec = d; |
| 153 | |
| 154 | return SRD_OK; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * TODO |
| 160 | */ |
| 161 | int srd_unload_decoder(struct srd_decoder *dec) |
| 162 | { |
| 163 | g_free(dec->id); |
| 164 | g_free(dec->name); |
Bert Vermeulen | b231546 | 2012-01-09 00:13:03 +0100 | [diff] [blame] | 165 | g_free(dec->longname); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 166 | g_free(dec->desc); |
Bert Vermeulen | b231546 | 2012-01-09 00:13:03 +0100 | [diff] [blame] | 167 | g_free(dec->longdesc); |
| 168 | g_free(dec->author); |
| 169 | g_free(dec->license); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 170 | g_free(dec->func); |
| 171 | |
| 172 | /* TODO: Free everything in inputformats and outputformats. */ |
| 173 | |
| 174 | if (dec->inputformats != NULL) |
| 175 | g_slist_free(dec->inputformats); |
| 176 | if (dec->outputformats != NULL) |
| 177 | g_slist_free(dec->outputformats); |
| 178 | |
| 179 | Py_XDECREF(dec->py_decobj); |
| 180 | Py_XDECREF(dec->py_mod); |
| 181 | |
| 182 | /* TODO: (g_)free dec itself? */ |
| 183 | |
| 184 | return SRD_OK; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | int srd_load_all_decoders(void) |
| 189 | { |
| 190 | DIR *dir; |
| 191 | struct dirent *dp; |
| 192 | int ret; |
| 193 | char *decodername; |
| 194 | struct srd_decoder *dec; |
| 195 | |
| 196 | if (!(dir = opendir(DECODERS_DIR))) { |
| 197 | Py_Finalize(); /* Returns void. */ |
| 198 | return SRD_ERR_DECODERS_DIR; |
| 199 | } |
| 200 | |
| 201 | while ((dp = readdir(dir)) != NULL) { |
| 202 | /* Ignore filenames which don't end with ".py". */ |
| 203 | if (!g_str_has_suffix(dp->d_name, ".py")) |
| 204 | continue; |
| 205 | |
| 206 | /* Decoder name == filename (without .py suffix). */ |
| 207 | decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3); |
| 208 | |
| 209 | /* TODO: Error handling. Use g_try_malloc(). */ |
| 210 | if (!(dec = malloc(sizeof(struct srd_decoder)))) { |
| 211 | Py_Finalize(); /* Returns void. */ |
| 212 | return SRD_ERR_MALLOC; |
| 213 | } |
| 214 | |
| 215 | /* Load the decoder. */ |
| 216 | /* TODO: Warning if loading fails for a decoder. */ |
| 217 | if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) { |
| 218 | /* Append it to the list of supported/loaded decoders. */ |
Bert Vermeulen | e508088 | 2011-12-07 09:56:49 +0100 | [diff] [blame] | 219 | pd_list = g_slist_append(pd_list, dec); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | closedir(dir); |
| 223 | |
| 224 | return SRD_OK; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** |
| 229 | * TODO |
| 230 | */ |
| 231 | int srd_unload_all_decoders(void) |
| 232 | { |
| 233 | GSList *l; |
| 234 | struct srd_decoder *dec; |
| 235 | |
| 236 | for (l = srd_list_decoders(); l; l = l->next) { |
| 237 | dec = l->data; |
| 238 | /* TODO: Error handling. */ |
| 239 | srd_unload_decoder(dec); |
| 240 | } |
| 241 | |
| 242 | return SRD_OK; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |