22 #include "libsigrokdecode-internal.h" 34 extern SRD_PRIV PyObject *srd_logic_type;
72 PyObject *py_di_options, *py_optval;
81 srd_err(
"Invalid decoder instance.");
86 srd_err(
"Invalid options GHashTable.");
90 if (!PyObject_HasAttrString(di->
decoder->
py_dec,
"options")) {
92 if (g_hash_table_size(options) == 0) {
96 srd_err(
"Protocol decoder has no options.");
111 if (!(py_di_options = PyObject_GetAttrString(di->
py_inst,
"options")))
113 Py_DECREF(py_di_options);
114 py_di_options = PyDict_New();
115 PyObject_SetAttrString(di->
py_inst,
"options", py_di_options);
119 if ((value = g_hash_table_lookup(options, sdo->
id))) {
121 if (!g_variant_type_equal(g_variant_get_type(value),
122 g_variant_get_type(sdo->
def))) {
123 srd_err(
"Option '%s' should have the same type " 124 "as the default value.", sdo->
id);
131 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
132 val_str = g_variant_get_string(value, NULL);
133 if (!(py_optval = PyUnicode_FromString(val_str))) {
136 srd_err(
"Option '%s' requires a UTF-8 string value.", sdo->
id);
139 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT64)) {
140 val_int = g_variant_get_int64(value);
141 if (!(py_optval = PyLong_FromLong(val_int))) {
144 srd_err(
"Option '%s' has invalid integer value.", sdo->
id);
147 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) {
148 val_double = g_variant_get_double(value);
149 if (!(py_optval = PyFloat_FromDouble(val_double))) {
152 srd_err(
"Option '%s' has invalid float value.",
157 if (PyDict_SetItemString(py_di_options, sdo->
id, py_optval) == -1)
160 g_hash_table_remove(options, sdo->
id);
162 if (g_hash_table_size(options) != 0)
163 srd_warn(
"Unknown options specified for '%s'", di->
inst_id);
168 Py_XDECREF(py_optval);
169 if (PyErr_Occurred()) {
170 srd_exception_catch(
"Stray exception in srd_inst_option_set()");
178 static gint compare_channel_id(
const struct srd_channel *pdch,
179 const char *channel_id)
181 return strcmp(pdch->
id, channel_id);
200 GHashTable *new_channels)
202 GVariant *channel_val;
206 int *new_channelmap, new_channelnum, num_required_channels, i;
209 srd_dbg(
"Setting channels for instance %s with list of %d channels.",
210 di->
inst_id, g_hash_table_size(new_channels));
212 if (g_hash_table_size(new_channels) == 0)
218 srd_err(
"Protocol decoder %s has no channels to define.",
230 new_channelmap[i] = -1;
232 for (l = g_hash_table_get_keys(new_channels); l; l = l->next) {
233 channel_id = l->data;
234 channel_val = g_hash_table_lookup(new_channels, channel_id);
235 if (!g_variant_is_of_type(channel_val, G_VARIANT_TYPE_INT32)) {
237 srd_err(
"No channel number was specified for %s.",
239 g_free(new_channelmap);
242 new_channelnum = g_variant_get_int32(channel_val);
244 (GCompareFunc)compare_channel_id))) {
247 channel_id, (GCompareFunc)compare_channel_id))) {
248 srd_err(
"Protocol decoder %s has no channel " 250 g_free(new_channelmap);
255 new_channelmap[pdch->
order] = new_channelnum;
256 srd_dbg(
"Setting channel mapping: %s (index %d) = channel %d.",
257 pdch->
id, pdch->
order, new_channelnum);
260 srd_dbg(
"Final channel map:");
263 srd_dbg(
" - index %d = channel %d (%s)", i, new_channelmap[i],
264 (i < num_required_channels) ?
"required" :
"optional");
268 for (i = 0; i < num_required_channels; i++) {
269 if (new_channelmap[i] != -1)
272 srd_err(
"Required channel '%s' (index %d) was not specified.",
297 const char *decoder_id, GHashTable *options)
305 srd_dbg(
"Creating new %s instance.", decoder_id);
307 if (session_is_valid(sess) !=
SRD_OK) {
308 srd_err(
"Invalid session.");
313 srd_err(
"Protocol decoder %s not found.", decoder_id);
323 inst_id = g_hash_table_lookup(options,
"id");
325 di->
inst_id = g_strdup(inst_id);
326 g_hash_table_remove(options,
"id");
331 di->
inst_id = g_strdup_printf(
"%s-%d", decoder_id, i++);
334 di->
inst_id = g_strdup_printf(
"%s-%d", decoder_id, i++);
357 di->
old_pins_array = g_array_sized_new(FALSE, TRUE,
sizeof(uint8_t),
364 if (!(di->
py_inst = PyObject_CallObject(dec->
py_dec, NULL))) {
365 if (PyErr_Occurred())
366 srd_exception_catch(
"Failed to create %s instance",
402 sess->di_list = g_slist_append(sess->di_list, di);
403 srd_dbg(
"Created new %s instance with ID %s.", decoder_id, di->
inst_id);
415 srd_dbg(
"%s: Joining decoder thread.", di->
inst_id);
421 srd_dbg(
"%s: Raising want_term, sending got_new.", di->
inst_id);
427 srd_dbg(
"%s: Running join().", di->
inst_id);
429 srd_dbg(
"%s: Call to join() done.", di->
inst_id);
449 srd_dbg(
"%s: Resetting decoder state.", di->
inst_id);
454 condition_list_free(di);
455 match_array_free(di);
461 oldpins_array_free(di);
484 if (session_is_valid(sess) !=
SRD_OK) {
485 srd_err(
"Invalid session.");
489 if (!di_bottom || !di_top) {
490 srd_err(
"Invalid from/to instance pair.");
494 if (g_slist_find(sess->di_list, di_top)) {
496 sess->di_list = g_slist_remove(sess->di_list, di_top);
502 srd_dbg(
"Stacked %s onto %s.", di_top->
inst_id, di_bottom->
inst_id);
527 for (l = stack->
next_di; l; l = l->next) {
558 if (session_is_valid(sess) !=
SRD_OK) {
559 srd_err(
"Invalid session.");
564 for (l = sess->di_list; l; l = l->next) {
566 if ((di = srd_inst_find_by_id_stack(inst_id, tmp)) != NULL)
574 struct srd_session *
sess,
const GSList *stack,
581 srd_err(
"Invalid session.");
586 for (l = stack ? stack :
sess->di_list; di == NULL && l != NULL; l = l->next) {
591 di = srd_sess_inst_find_by_obj(
sess, tmp->
next_di, obj);
619 struct srd_session *sess;
623 for (l = sessions; di == NULL && l != NULL; l = l->next) {
625 di = srd_sess_inst_find_by_obj(sess, stack, obj);
645 srd_err(
"Invalid decoder instance.");
653 srd_err(
"Incorrect number of channels (need %d, got %d).",
660 if (initial_pins->data[i] <= 2)
662 srd_err(
"Invalid initial channel %d pin state: %d.",
663 i, initial_pins->data[i]);
667 s = g_string_sized_new(100);
672 s = g_string_truncate(s, s->len - 2);
673 srd_dbg(
"Initial pins: %s.", s->str);
674 g_string_free(s, TRUE);
687 srd_dbg(
"%s: Releasing initial pin state.", di->
inst_id);
701 srd_dbg(
"Calling start() method on protocol decoder instance %s.",
705 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"start", NULL))) {
706 srd_exception_catch(
"Protocol decoder instance %s",
713 PyObject_SetAttrString(di->
py_inst,
"samplenum", PyLong_FromLong(0));
716 PyObject_SetAttrString(di->
py_inst,
"matched", Py_None);
719 for (l = di->
next_di; l; l = l->next) {
721 if ((ret = srd_inst_start(next_di)) !=
SRD_OK)
744 static gboolean sample_matches(uint8_t old_sample, uint8_t sample,
struct srd_term *term)
749 switch (term->type) {
758 case SRD_TERM_RISING_EDGE:
759 if (old_sample == 0 && sample == 1)
762 case SRD_TERM_FALLING_EDGE:
763 if (old_sample == 1 && sample == 0)
766 case SRD_TERM_EITHER_EDGE:
767 if ((old_sample == 1 && sample == 0) || (old_sample == 0 && sample == 1))
770 case SRD_TERM_NO_EDGE:
771 if ((old_sample == 0 && sample == 0) || (old_sample == 1 && sample == 1))
775 if (term->num_samples_already_skipped == term->num_samples_to_skip)
777 term->num_samples_already_skipped++;
780 srd_err(
"Unknown term type %d.", term->type);
808 g_slist_free_full(ll, g_free);
831 const uint8_t *sample_pos)
834 int i, byte_offset, bit_offset;
842 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
850 int i, byte_offset, bit_offset;
851 const uint8_t *sample_pos;
863 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
869 struct srd_term *term,
const uint8_t *sample_pos)
871 uint8_t old_sample, sample;
872 int byte_offset, bit_offset, ch;
878 old_sample = sample = 0;
880 if (term->type != SRD_TERM_SKIP) {
884 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
888 return sample_matches(old_sample, sample, term);
892 const GSList *cond,
const uint8_t *sample_pos)
895 struct srd_term *term;
897 if (!di || !cond || !sample_pos)
900 for (l = cond; l; l = l->next) {
902 if (!term_matches(di, term, sample_pos))
909 static gboolean at_least_one_condition_matched(
917 for (i = 0; i < num_conditions; i++) {
927 static uint64_t s = 0;
928 uint64_t i, j, num_samples_to_process;
930 const uint8_t *sample_pos;
931 unsigned int num_conditions;
935 srd_dbg(
"NULL/empty condition list, automatic match.");
940 if (!have_non_null_conds(di)) {
941 srd_dbg(
"Only NULL conditions in list, automatic match.");
949 di->
match_array = g_array_sized_new(FALSE, TRUE,
sizeof(gboolean), num_conditions);
954 update_old_pins_array_initial_pins(di);
956 for (i = 0, s = 0; i < num_samples_to_process; i++, s++, (di->
abs_cur_samplenum)++) {
967 di->
match_array->data[j] = all_terms_match(di, cond, sample_pos);
970 update_old_pins_array(di, sample_pos);
973 if (at_least_one_condition_matched(di, num_conditions))
999 if (!di || !found_match)
1002 *found_match = FALSE;
1009 *found_match = find_match(di);
1013 srd_dbg(
"Done, handled all samples (abs cur %" PRIu64
1014 " / abs end %" PRIu64
").",
1020 if (!(*found_match))
1038 static gpointer di_thread(gpointer data)
1049 srd_dbg(
"%s: Starting thread routine for decoder.", di->
inst_id);
1056 srd_dbg(
"%s: Calling decode() method.", di->
inst_id);
1057 py_res = PyObject_CallMethod(di->
py_inst,
"decode", NULL);
1058 srd_dbg(
"%s: decode() method terminated.", di->
inst_id);
1078 if (!py_res && wanted_term) {
1084 srd_dbg(
"%s: Thread done (!res, want_term).", di->
inst_id);
1094 srd_dbg(
"%s: decode() terminated unrequested.", di->
inst_id);
1095 srd_exception_catch(
"Protocol decoder instance %s: ", di->
inst_id);
1096 srd_dbg(
"%s: Thread done (!res, !want_term).", di->
inst_id);
1105 srd_dbg(
"%s: decode() terminated (req %d).", di->
inst_id, wanted_term);
1109 srd_dbg(
"%s: Thread done (with res).", di->
inst_id);
1174 srd_dbg(
"empty decoder instance");
1178 srd_dbg(
"NULL buffer pointer");
1182 srd_dbg(
"empty buffer");
1185 if (unitsize == 0) {
1186 srd_dbg(
"unitsize 0");
1192 srd_dbg(
"Incorrect sample numbers: start=%" PRIu64
", cur=%" 1200 srd_dbg(
"Decoding: abs start sample %" PRIu64
", abs end sample %" 1201 PRIu64
" (%" PRIu64
" samples, %" PRIu64
" bytes, unitsize = " 1206 apiver = srd_decoder_apiver(di->
decoder);
1213 logic = PyObject_New(srd_logic, (PyTypeObject *)srd_logic_type);
1218 logic->inbuf = (uint8_t *)
inbuf;
1220 logic->sample = PyList_New(2);
1221 Py_INCREF(logic->sample);
1224 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"decode",
1226 srd_exception_catch(
"Protocol decoder instance %s",
1235 srd_dbg(
"No worker thread for this decoder stack " 1236 "exists yet, creating one: %s.", di->
inst_id);
1271 srd_dbg(
"Freeing instance %s", di->
inst_id);
1273 srd_inst_join_decode_thread(di);
1274 srd_inst_reset_state(di);
1281 for (l = di->
pd_output; l; l = l->next) {
1291 SRD_PRIV void srd_inst_free_all(
struct srd_session *sess)
1293 if (session_is_valid(sess) !=
SRD_OK) {
1294 srd_err(
"Invalid session.");
1298 g_slist_free_full(sess->di_list, (GDestroyNotify)srd_inst_free);
struct srd_decoder * decoder
GThread * thread_handle
Handle for this PD stack's worker thread.
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
uint64_t abs_end_samplenum
Absolute end sample number.
struct srd_session * sess
int srd_inst_option_set(struct srd_decoder_inst *di, GHashTable *options)
Set one or more options in a decoder instance.
gboolean want_wait_terminate
Requests termination of wait() and decode().
gboolean got_new_samples
Indicates whether new samples are available for processing.
uint64_t inbuflen
Length (in bytes) of the input sample buffer.
int order
The index of the channel, i.e.
Structure which contains information about one protocol decoder channel.
const uint8_t * inbuf
Pointer to the buffer/chunk of input samples.
GCond got_new_samples_cond
GArray * match_array
Array of booleans denoting which conditions matched.
Generic/unspecified error.
GCond handled_all_samples_cond
GSList * opt_channels
List of optional channels for this decoder.
void * py_dec
sigrokdecode.Decoder class.
uint64_t abs_cur_samplenum
Absolute current samplenumber.
char * id
The ID of the channel.
GArray * old_pins_array
Array of "old" (previous sample) pin values.
The public libsigrokdecode header file to be used by frontends.
uint64_t abs_start_samplenum
Absolute start sample number.
GSList * condition_list
List of conditions a PD wants to wait for.
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.
char * name
The (short) decoder name.
GSList * options
List of decoder options.
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.
gboolean handled_all_samples
Indicates whether the worker thread has handled all samples.
GSList * channels
List of channels required by this decoder.
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_channel_set_all(struct srd_decoder_inst *di, GHashTable *new_channels)
Set all channels in a decoder instance.
int srd_inst_initial_pins_set_all(struct srd_decoder_inst *di, GArray *initial_pins)
Set the list of initial (assumed) pin values.
uint8_t * channel_samples