22 #include "libsigrokdecode-internal.h" 34 extern SRD_PRIV PyObject *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.",
153 if (PyDict_SetItemString(py_di_options, sdo->
id, py_optval) == -1)
156 g_hash_table_remove(options, sdo->
id);
158 if (g_hash_table_size(options) != 0)
159 srd_warn(
"Unknown options specified for '%s'", di->
inst_id);
164 Py_XDECREF(py_optval);
165 if (PyErr_Occurred()) {
166 srd_exception_catch(
"Stray exception in srd_inst_option_set()");
174 static gint compare_channel_id(
const struct srd_channel *pdch,
175 const char *channel_id)
177 return strcmp(pdch->
id, channel_id);
196 GHashTable *new_channels)
198 GVariant *channel_val;
202 int *new_channelmap, new_channelnum, num_required_channels, i;
205 srd_dbg(
"Setting channels for instance %s with list of %d channels.",
206 di->
inst_id, g_hash_table_size(new_channels));
208 if (g_hash_table_size(new_channels) == 0)
214 srd_err(
"Protocol decoder %s has no channels to define.",
226 new_channelmap[i] = -1;
228 for (l = g_hash_table_get_keys(new_channels); l; l = l->next) {
229 channel_id = l->data;
230 channel_val = g_hash_table_lookup(new_channels, channel_id);
231 if (!g_variant_is_of_type(channel_val, G_VARIANT_TYPE_INT32)) {
233 srd_err(
"No channel number was specified for %s.",
235 g_free(new_channelmap);
238 new_channelnum = g_variant_get_int32(channel_val);
240 (GCompareFunc)compare_channel_id))) {
243 channel_id, (GCompareFunc)compare_channel_id))) {
244 srd_err(
"Protocol decoder %s has no channel " 246 g_free(new_channelmap);
251 new_channelmap[pdch->
order] = new_channelnum;
252 srd_dbg(
"Setting channel mapping: %s (index %d) = channel %d.",
253 pdch->
id, pdch->
order, new_channelnum);
256 srd_dbg(
"Final channel map:");
259 srd_dbg(
" - index %d = channel %d (%s)", i, new_channelmap[i],
260 (i < num_required_channels) ?
"required" :
"optional");
264 for (i = 0; i < num_required_channels; i++) {
265 if (new_channelmap[i] != -1)
268 srd_err(
"Required channel '%s' (index %d) was not specified.",
293 const char *decoder_id, GHashTable *options)
300 srd_dbg(
"Creating new %s instance.", decoder_id);
302 if (session_is_valid(sess) !=
SRD_OK) {
303 srd_err(
"Invalid session.");
308 srd_err(
"Protocol decoder %s not found.", decoder_id);
317 inst_id = g_hash_table_lookup(options,
"id");
318 di->
inst_id = g_strdup(inst_id ? inst_id : decoder_id);
319 g_hash_table_remove(options,
"id");
321 di->
inst_id = g_strdup(decoder_id);
342 if (!(di->
py_inst = PyObject_CallObject(dec->
py_dec, NULL))) {
343 if (PyErr_Occurred())
344 srd_exception_catch(
"Failed to create %s instance",
358 sess->di_list = g_slist_append(sess->di_list, di);
379 if (session_is_valid(sess) !=
SRD_OK) {
380 srd_err(
"Invalid session.");
384 if (!di_bottom || !di_top) {
385 srd_err(
"Invalid from/to instance pair.");
389 if (g_slist_find(sess->di_list, di_top)) {
391 sess->di_list = g_slist_remove(sess->di_list, di_top);
397 srd_dbg(
"Stacked %s onto %s.", di_top->
inst_id, di_bottom->
inst_id);
421 if (session_is_valid(sess) !=
SRD_OK) {
422 srd_err(
"Invalid session.");
427 for (l = sess->di_list; l; l = l->next) {
429 if (!strcmp(tmp->
inst_id, inst_id)) {
439 struct srd_session *
sess,
const GSList *stack,
446 srd_err(
"Invalid session.");
451 for (l = stack ? stack :
sess->di_list; di == NULL && l != NULL; l = l->next) {
456 di = srd_sess_inst_find_by_obj(
sess, tmp->
next_di, obj);
484 struct srd_session *sess;
488 for (l = sessions; di == NULL && l != NULL; l = l->next) {
490 di = srd_sess_inst_find_by_obj(sess, stack, obj);
504 srd_dbg(
"Calling start() method on protocol decoder instance %s.",
507 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"start", NULL))) {
508 srd_exception_catch(
"Protocol decoder instance %s",
515 for (l = di->
next_di; l; l = l->next) {
517 if ((ret = srd_inst_start(next_di)) !=
SRD_OK)
541 uint64_t start_samplenum, uint64_t end_samplenum,
542 const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
550 srd_dbg(
"empty decoder instance");
554 srd_dbg(
"NULL buffer pointer");
558 srd_dbg(
"empty buffer");
562 srd_dbg(
"unitsize 0");
568 srd_dbg(
"Decoding: start sample %" PRIu64
", end sample %" 569 PRIu64
" (%" PRIu64
" samples, %" PRIu64
" bytes, unitsize = " 570 "%d), instance %s.", start_samplenum, end_samplenum,
571 end_samplenum - start_samplenum, inbuflen, di->
data_unitsize,
581 logic = PyObject_New(srd_logic, (PyTypeObject *)srd_logic_type);
584 logic->start_samplenum = start_samplenum;
586 logic->inbuf = (uint8_t *)inbuf;
587 logic->inbuflen = inbuflen;
588 logic->sample = PyList_New(2);
589 Py_INCREF(logic->sample);
592 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"decode",
593 "KKO", start_samplenum, end_samplenum, logic))) {
594 srd_exception_catch(
"Protocol decoder instance %s",
610 srd_dbg(
"Freeing instance %s", di->
inst_id);
617 for (l = di->
pd_output; l; l = l->next) {
627 SRD_PRIV void srd_inst_free_all(
struct srd_session *sess, GSList *stack)
633 srd_err(
"Invalid session.");
638 for (l = stack ? stack :
sess->di_list; di == NULL && l != NULL; l = l->next) {
645 g_slist_free(
sess->di_list);
646 sess->di_list = NULL;
uint8_t * channel_samples
The public libsigrokdecode header file to be used by frontends.
struct srd_decoder_inst * srd_inst_new(struct srd_session *sess, const char *decoder_id, GHashTable *options)
Create a new protocol decoder instance.
int srd_inst_option_set(struct srd_decoder_inst *di, GHashTable *options)
Set one or more options in a decoder instance.
struct srd_decoder * decoder
SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d)
Get the API version of the specified decoder.
void * py_dec
sigrokdecode.Decoder class.
struct srd_session * sess
GSList * opt_channels
List of optional channels for this decoder.
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.
int order
The index of the channel, i.e.
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.
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
char * name
The (short) decoder name.
Structure which contains information about one protocol decoder channel.
GSList * options
List of decoder options.
int srd_inst_channel_set_all(struct srd_decoder_inst *di, GHashTable *new_channels)
Set all channels in a decoder instance.
GSList * channels
List of channels required by this decoder.
char * id
The ID of the channel.