22 #include "libsigrokdecode-internal.h"
34 extern SRD_PRIV PyTypeObject srd_logic_type;
68 PyObject *py_di_options, *py_optval;
77 srd_err(
"Invalid decoder instance.");
82 srd_err(
"Invalid options GHashTable.");
86 if (!PyObject_HasAttrString(di->
decoder->
py_dec,
"options")) {
88 if (g_hash_table_size(options) == 0) {
92 srd_err(
"Protocol decoder has no options.");
107 if (!(py_di_options = PyObject_GetAttrString(di->
py_inst,
"options")))
109 Py_DECREF(py_di_options);
110 py_di_options = PyDict_New();
111 PyObject_SetAttrString(di->
py_inst,
"options", py_di_options);
115 if ((value = g_hash_table_lookup(options, sdo->
id))) {
117 if (!g_variant_type_equal(g_variant_get_type(value),
118 g_variant_get_type(sdo->
def))) {
119 srd_err(
"Option '%s' should have the same type "
120 "as the default value.", sdo->
id);
127 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
128 val_str = g_variant_get_string(value, NULL);
129 if (!(py_optval = PyUnicode_FromString(val_str))) {
132 srd_err(
"Option '%s' requires a UTF-8 string value.", sdo->
id);
135 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT64)) {
136 val_int = g_variant_get_int64(value);
137 if (!(py_optval = PyLong_FromLong(val_int))) {
140 srd_err(
"Option '%s' has invalid integer value.", sdo->
id);
143 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) {
144 val_double = g_variant_get_double(value);
145 if (!(py_optval = PyFloat_FromDouble(val_double))) {
148 srd_err(
"Option '%s' has invalid float value.", sdo->
id);
152 if (PyDict_SetItemString(py_di_options, sdo->
id, py_optval) == -1)
155 g_hash_table_remove(options, sdo->
id);
157 if (g_hash_table_size(options) != 0)
158 srd_warn(
"Unknown options specified for '%s'", di->
inst_id);
163 Py_XDECREF(py_optval);
164 if (PyErr_Occurred()) {
165 srd_exception_catch(
"Stray exception in srd_inst_option_set().");
173 static gint compare_channel_id(
const struct srd_channel *pdch,
174 const char *channel_id)
176 return strcmp(pdch->
id, channel_id);
198 GHashTable *new_channels,
int unit_size)
200 GVariant *channel_val;
204 int *new_channelmap, new_channelnum, num_required_channels, i;
207 srd_dbg(
"Setting channels for instance %s with list of %d channels, "
208 "unitsize %d.", di->
inst_id, g_hash_table_size(new_channels), unit_size);
210 if (g_hash_table_size(new_channels) == 0)
216 srd_err(
"Protocol decoder %s has no channels to define.",
221 new_channelmap = NULL;
224 srd_err(
"Failed to g_malloc() new channel map.");
233 new_channelmap[i] = -1;
235 for (l = g_hash_table_get_keys(new_channels); l; l = l->next) {
236 channel_id = l->data;
237 channel_val = g_hash_table_lookup(new_channels, channel_id);
238 if (!g_variant_is_of_type(channel_val, G_VARIANT_TYPE_INT32)) {
240 srd_err(
"No channel number was specified for %s.",
242 g_free(new_channelmap);
245 new_channelnum = g_variant_get_int32(channel_val);
246 if (new_channelnum >= 8 * unit_size) {
247 srd_err(
"Channel index %d not within data unit (%d bit).",
248 new_channelnum, 8 * unit_size);
249 g_free(new_channelmap);
253 (GCompareFunc)compare_channel_id))) {
256 channel_id, (GCompareFunc) compare_channel_id))) {
257 srd_err(
"Protocol decoder %s has no channel "
259 g_free(new_channelmap);
264 new_channelmap[pdch->
order] = new_channelnum;
265 srd_dbg(
"Setting channel mapping: %s (index %d) = channel %d.",
266 pdch->
id, pdch->
order, new_channelnum);
270 srd_dbg(
"Final channel map:");
273 srd_dbg(
" - index %d = channel %d (%s)", i, new_channelmap[i],
274 (i < num_required_channels) ?
"required" :
"optional");
278 for (i = 0; i < num_required_channels; i++) {
279 if (new_channelmap[i] != -1)
282 srd_err(
"Required channel '%s' (index %d) was not specified.",
307 const char *decoder_id, GHashTable *options)
314 srd_dbg(
"Creating new %s instance.", decoder_id);
316 if (session_is_valid(sess) !=
SRD_OK) {
317 srd_err(
"Invalid session.");
322 srd_err(
"Protocol decoder %s not found.", decoder_id);
327 srd_err(
"Failed to g_malloc() instance.");
334 inst_id = g_hash_table_lookup(options,
"id");
335 di->
inst_id = g_strdup(inst_id ? inst_id : decoder_id);
336 g_hash_table_remove(options,
"id");
338 di->
inst_id = g_strdup(decoder_id);
349 srd_err(
"Failed to g_malloc() channel map.");
361 srd_err(
"Failed to g_malloc() sample buffer.");
369 if (!(di->
py_inst = PyObject_CallObject(dec->
py_dec, NULL))) {
370 if (PyErr_Occurred())
371 srd_exception_catch(
"failed to create %s instance: ",
385 sess->di_list = g_slist_append(sess->di_list, di);
406 if (session_is_valid(sess) !=
SRD_OK) {
407 srd_err(
"Invalid session.");
411 if (!di_bottom || !di_top) {
412 srd_err(
"Invalid from/to instance pair.");
416 if (g_slist_find(sess->di_list, di_top)) {
418 sess->di_list = g_slist_remove(sess->di_list, di_top);
424 srd_dbg(
"Stacked %s on top of %s.", di_top->
inst_id, di_bottom->
inst_id);
448 if (session_is_valid(sess) !=
SRD_OK) {
449 srd_err(
"Invalid session.");
454 for (l = sess->di_list; l; l = l->next) {
456 if (!strcmp(tmp->
inst_id, inst_id)) {
466 struct srd_session *
sess,
const GSList *stack,
472 if (session_is_valid(sess) !=
SRD_OK) {
473 srd_err(
"Invalid session.");
478 for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
483 di = srd_sess_inst_find_by_obj(sess, tmp->
next_di, obj);
511 struct srd_session *sess;
515 for (l = sessions; di == NULL && l != NULL; l = l->next) {
517 di = srd_sess_inst_find_by_obj(sess, stack, obj);
531 srd_dbg(
"Calling start() method on protocol decoder instance %s.",
534 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"start", NULL))) {
535 srd_exception_catch(
"Protocol decoder instance %s: ",
542 for (l = di->
next_di; l; l = l->next) {
544 if ((ret = srd_inst_start(next_di)) !=
SRD_OK)
569 uint64_t start_samplenum, uint64_t end_samplenum,
570 const uint8_t *inbuf, uint64_t inbuflen)
575 srd_dbg(
"Calling decode() on instance %s with %" PRIu64
" bytes "
576 "starting at sample %" PRIu64
".", di->
inst_id, inbuflen,
581 srd_dbg(
"empty decoder instance");
585 srd_dbg(
"NULL buffer pointer");
589 srd_dbg(
"empty buffer");
597 logic = PyObject_New(
srd_logic, &srd_logic_type);
602 logic->
inbuf = (uint8_t *)inbuf;
604 logic->
sample = PyList_New(2);
608 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"decode",
609 "KKO", start_samplenum, end_samplenum, logic))) {
610 srd_exception_catch(
"Protocol decoder instance %s: ", di->
inst_id);
624 srd_dbg(
"Freeing instance %s", di->
inst_id);
630 for (l = di->
pd_output; l; l = l->next) {
640 SRD_PRIV void srd_inst_free_all(
struct srd_session *sess, GSList *stack)
645 if (session_is_valid(sess) !=
SRD_OK) {
646 srd_err(
"Invalid session.");
651 for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
654 srd_inst_free_all(sess, di->
next_di);
658 g_slist_free(sess->di_list);
659 sess->di_list = NULL;
uint8_t * channel_samples
GSList * opt_channels
List of optional channels for this decoder.
The public libsigrokdecode header file to be used by frontends.
PyObject * py_dec
sigrokdecode.Decoder class.
struct srd_decoder_inst * srd_inst_new(struct srd_session *sess, const char *decoder_id, GHashTable *options)
Create a new protocol decoder instance.
Structure which contains information about one protocol decoder channel.
int srd_inst_option_set(struct srd_decoder_inst *di, GHashTable *options)
Set one or more options in a decoder instance.
char * name
The (short) decoder name.
int order
The index of the channel, i.e.
char * id
The ID of the channel.
PyObject_HEAD struct srd_decoder_inst * di
int srd_inst_channel_set_all(struct srd_decoder_inst *di, GHashTable *new_channels, int unit_size)
Set all channels in a decoder instance.
int srd_inst_stack(struct srd_session *sess, struct srd_decoder_inst *di_bottom, struct srd_decoder_inst *di_top)
Stack a decoder instance on top of another.
Generic/unspecified error.
struct srd_decoder_inst * srd_inst_find_by_id(struct srd_session *sess, const char *inst_id)
Find a decoder instance by its instance ID.
GSList * options
List of decoder options.
struct srd_decoder * decoder
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
Malloc/calloc/realloc error.
GSList * channels
List of channels required by this decoder.
struct srd_session * sess