22 #include "libsigrokdecode-internal.h"
60 arr = g_array_sized_new(FALSE, TRUE,
sizeof(uint8_t), count);
61 g_array_set_size(arr, count);
74 srd_dbg(
"%s: Releasing initial pin state.", di->
inst_id);
96 PyObject *py_di_options, *py_optval;
103 PyGILState_STATE gstate;
106 srd_err(
"Invalid decoder instance.");
111 srd_err(
"Invalid options GHashTable.");
115 gstate = PyGILState_Ensure();
117 if (!PyObject_HasAttrString(di->
decoder->
py_dec,
"options")) {
119 PyGILState_Release(gstate);
120 if (g_hash_table_size(options) == 0) {
124 srd_err(
"Protocol decoder has no options.");
139 if (!(py_di_options = PyObject_GetAttrString(di->
py_inst,
"options")))
141 Py_DECREF(py_di_options);
142 py_di_options = PyDict_New();
143 PyObject_SetAttrString(di->
py_inst,
"options", py_di_options);
144 Py_DECREF(py_di_options);
148 if ((value = g_hash_table_lookup(options, sdo->
id))) {
150 if (!g_variant_type_equal(g_variant_get_type(value),
151 g_variant_get_type(sdo->
def))) {
152 srd_err(
"Option '%s' should have the same type "
153 "as the default value.", sdo->
id);
160 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
161 val_str = g_variant_get_string(value, NULL);
162 if (!(py_optval = PyUnicode_FromString(val_str))) {
165 srd_err(
"Option '%s' requires a UTF-8 string value.", sdo->
id);
168 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT64)) {
169 val_int = g_variant_get_int64(value);
170 if (!(py_optval = PyLong_FromLong(val_int))) {
173 srd_err(
"Option '%s' has invalid integer value.", sdo->
id);
176 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) {
177 val_double = g_variant_get_double(value);
178 if (!(py_optval = PyFloat_FromDouble(val_double))) {
181 srd_err(
"Option '%s' has invalid float value.",
186 if (PyDict_SetItemString(py_di_options, sdo->
id, py_optval) == -1) {
187 Py_XDECREF(py_optval);
191 g_hash_table_remove(options, sdo->
id);
192 Py_XDECREF(py_optval);
194 if (g_hash_table_size(options) != 0)
195 srd_warn(
"Unknown options specified for '%s'", di->
inst_id);
200 if (PyErr_Occurred()) {
201 srd_exception_catch(
"Stray exception in srd_inst_option_set()");
204 PyGILState_Release(gstate);
210 static gint compare_channel_id(
const struct srd_channel *pdch,
211 const char *channel_id)
213 return strcmp(pdch->
id, channel_id);
232 GHashTable *new_channels)
234 GVariant *channel_val;
238 int *new_channelmap, new_channelnum, num_required_channels, i;
241 srd_dbg(
"Setting channels for instance %s with list of %d channels.",
242 di->
inst_id, g_hash_table_size(new_channels));
244 if (g_hash_table_size(new_channels) == 0)
250 srd_err(
"Protocol decoder %s has no channels to define.",
262 new_channelmap[i] = -1;
264 for (l = g_hash_table_get_keys(new_channels); l; l = l->next) {
265 channel_id = l->data;
266 channel_val = g_hash_table_lookup(new_channels, channel_id);
267 if (!g_variant_is_of_type(channel_val, G_VARIANT_TYPE_INT32)) {
269 srd_err(
"No channel number was specified for %s.",
271 g_free(new_channelmap);
274 new_channelnum = g_variant_get_int32(channel_val);
276 (GCompareFunc)compare_channel_id))) {
279 channel_id, (GCompareFunc)compare_channel_id))) {
280 srd_err(
"Protocol decoder %s has no channel "
282 g_free(new_channelmap);
287 new_channelmap[pdch->
order] = new_channelnum;
288 srd_dbg(
"Setting channel mapping: %s (PD ch idx %d) = input data ch idx %d.",
289 pdch->
id, pdch->
order, new_channelnum);
292 srd_dbg(
"Final channel map:");
298 i - num_required_channels);
300 srd_dbg(
" - PD ch idx %d (%s) = input data ch idx %d (%s)", i,
301 pdch->
id, new_channelmap[i],
302 (i < num_required_channels) ?
"required" :
"optional");
306 for (i = 0; i < num_required_channels; i++) {
307 if (new_channelmap[i] != -1)
310 srd_err(
"Required channel '%s' (index %d) was not specified.",
312 g_free(new_channelmap);
337 const char *decoder_id, GHashTable *options)
343 PyGILState_STATE gstate;
351 srd_err(
"Protocol decoder %s not found.", decoder_id);
361 inst_id = g_hash_table_lookup(options,
"id");
364 g_hash_table_remove(options,
"id");
369 di->
inst_id = g_strdup_printf(
"%s-%d", decoder_id, i++);
372 di->
inst_id = g_strdup_printf(
"%s-%d", decoder_id, i++);
395 oldpins_array_seed(di);
397 gstate = PyGILState_Ensure();
400 if (!(di->
py_inst = PyObject_CallObject(dec->
py_dec, NULL))) {
401 if (PyErr_Occurred())
402 srd_exception_catch(
"Failed to create %s instance",
404 PyGILState_Release(gstate);
410 PyGILState_Release(gstate);
442 sess->di_list = g_slist_append(
sess->di_list, di);
443 srd_dbg(
"Creating new %s instance %s.", decoder_id, di->
inst_id);
455 srd_dbg(
"%s: Joining decoder thread.", di->
inst_id);
461 srd_dbg(
"%s: Raising want_term, sending got_new.", di->
inst_id);
467 srd_dbg(
"%s: Running join().", di->
inst_id);
469 srd_dbg(
"%s: Call to join() done.", di->
inst_id);
489 srd_dbg(
"%s: Resetting decoder state.", di->
inst_id);
492 condition_list_free(di);
493 match_array_free(di);
499 oldpins_array_free(di);
526 if (!di_bottom || !di_top) {
527 srd_err(
"Invalid from/to instance pair.");
531 if (g_slist_find(
sess->di_list, di_top)) {
533 sess->di_list = g_slist_remove(
sess->di_list, di_top);
541 gboolean at_least_one_match = FALSE;
542 for (GSList *out = di_bottom->
decoder->
outputs; out; out = out->next) {
543 const char *o = out->data;
544 for (GSList *in = di_top->
decoder->
inputs; in; in = in->next) {
545 const char *i = in->data;
547 at_least_one_match = TRUE;
553 if (!at_least_one_match)
554 srd_warn(
"No matching in-/output when stacking %s onto %s.",
560 srd_dbg(
"Stacking %s onto %s.", di_top->
inst_id, di_bottom->
inst_id);
585 for (l = stack->
next_di; l; l = l->next) {
621 for (l =
sess->di_list; l; l = l->next) {
623 if ((di = srd_inst_find_by_id_stack(
inst_id, tmp)) != NULL)
644 srd_err(
"Invalid decoder instance.");
652 srd_err(
"Incorrect number of channels (need %d, got %d).",
659 if (initial_pins->data[i] <= 2)
661 srd_err(
"Invalid initial channel %d pin state: %d.",
662 i, initial_pins->data[i]);
666 s = g_string_sized_new(100);
667 oldpins_array_seed(di);
672 s = g_string_truncate(s, s->len - 2);
673 srd_dbg(
"Initial pins: %s.", s->str);
674 g_string_free(s, TRUE);
682 PyObject *py_res, *py_samplenum;
686 PyGILState_STATE gstate;
688 srd_dbg(
"Calling start() of instance %s.", di->
inst_id);
690 gstate = PyGILState_Ensure();
693 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"start", NULL))) {
694 srd_exception_catch(
"Protocol decoder instance %s",
696 PyGILState_Release(gstate);
702 py_samplenum = PyLong_FromLong(0);
703 PyObject_SetAttrString(di->
py_inst,
"samplenum", py_samplenum);
704 Py_DECREF(py_samplenum);
707 PyObject_SetAttrString(di->
py_inst,
"matched", Py_None);
709 PyGILState_Release(gstate);
712 for (l = di->
next_di; l; l = l->next) {
737 __attribute__((always_inline))
738 static inline gboolean sample_matches(uint8_t old_sample, uint8_t sample,
struct srd_term *term)
742 switch (term->type) {
751 case SRD_TERM_RISING_EDGE:
752 if (old_sample == 0 && sample == 1)
755 case SRD_TERM_FALLING_EDGE:
756 if (old_sample == 1 && sample == 0)
759 case SRD_TERM_EITHER_EDGE:
760 if ((old_sample == 1 && sample == 0) || (old_sample == 0 && sample == 1))
763 case SRD_TERM_NO_EDGE:
764 if ((old_sample == 0 && sample == 0) || (old_sample == 1 && sample == 1))
768 if (term->num_samples_already_skipped == term->num_samples_to_skip)
770 term->num_samples_already_skipped++;
773 srd_err(
"Unknown term type %d.", term->type);
801 g_slist_free_full(ll, g_free);
825 const uint8_t *sample_pos)
828 int i, byte_offset, bit_offset;
833 oldpins_array_seed(di);
839 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
847 int i, byte_offset, bit_offset;
848 const uint8_t *sample_pos;
855 oldpins_array_seed(di);
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;
876 if (term->type == SRD_TERM_SKIP)
877 return sample_matches(0, 0, term);
882 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
885 return sample_matches(old_sample, sample, term);
889 const GSList *cond,
const uint8_t *sample_pos)
892 struct srd_term *term;
896 for (l = cond; l; l = l->next) {
898 if (term->type == SRD_TERM_ALWAYS_FALSE)
900 if (!term_matches(di, term, sample_pos))
907 static gboolean at_least_one_condition_matched(
914 for (i = 0; i < num_conditions; i++) {
924 uint64_t i, j, num_samples_to_process;
926 const uint8_t *sample_pos;
927 unsigned int num_conditions;
933 srd_dbg(
"NULL/empty condition list, automatic match.");
938 if (!have_non_null_conds(di)) {
939 srd_dbg(
"Only NULL conditions in list, automatic match.");
947 di->
match_array = g_array_sized_new(FALSE, TRUE,
sizeof(gboolean), num_conditions);
952 update_old_pins_array_initial_pins(di);
965 di->
match_array->data[j] = all_terms_match(di, cond, sample_pos);
968 update_old_pins_array(di, sample_pos);
971 if (at_least_one_condition_matched(di, num_conditions))
997 if (!di || !found_match)
1000 *found_match = FALSE;
1007 *found_match = find_match(di);
1011 srd_dbg(
"Done, handled all samples (abs cur %" PRIu64
1012 " / abs end %" PRIu64
").",
1018 if (!(*found_match))
1036 static gpointer di_thread(gpointer data)
1041 PyGILState_STATE gstate;
1048 srd_dbg(
"%s: Starting thread routine for decoder.", di->
inst_id);
1050 gstate = PyGILState_Ensure();
1057 srd_dbg(
"%s: Calling decode().", di->
inst_id);
1058 py_res = PyObject_CallMethod(di->
py_inst,
"decode", NULL);
1059 srd_dbg(
"%s: decode() terminated.", di->
inst_id);
1082 if (!py_res && wanted_term) {
1088 srd_dbg(
"%s: Thread done (!res, want_term).", di->
inst_id);
1090 PyGILState_Release(gstate);
1099 srd_dbg(
"%s: decode() terminated unrequested.", di->
inst_id);
1100 srd_exception_catch(
"Protocol decoder instance %s: ", di->
inst_id);
1101 srd_dbg(
"%s: Thread done (!res, !want_term).", di->
inst_id);
1102 PyGILState_Release(gstate);
1111 srd_dbg(
"%s: decode() terminated (req %d).", di->
inst_id, wanted_term);
1115 PyGILState_Release(gstate);
1117 srd_dbg(
"%s: Thread done (with res).", di->
inst_id);
1178 srd_dbg(
"empty decoder instance");
1182 srd_dbg(
"NULL buffer pointer");
1186 srd_dbg(
"empty buffer");
1189 if (unitsize == 0) {
1190 srd_dbg(
"unitsize 0");
1196 srd_dbg(
"Incorrect sample numbers: start=%" PRIu64
", cur=%"
1204 srd_dbg(
"Decoding: abs start sample %" PRIu64
", abs end sample %"
1205 PRIu64
" (%" PRIu64
" samples, %" PRIu64
" bytes, unitsize = "
1212 srd_dbg(
"No worker thread for this decoder stack "
1213 "exists yet, creating one: %s.", di->
inst_id);
1263 PyGILState_STATE gstate;
1278 srd_dbg(
"Terminating instance %s", di->
inst_id);
1279 srd_inst_join_decode_thread(di);
1280 srd_inst_reset_state(di);
1289 gstate = PyGILState_Ensure();
1290 if (PyObject_HasAttrString(di->
py_inst,
"reset")) {
1291 srd_dbg(
"Calling reset() of instance %s", di->
inst_id);
1292 py_ret = PyObject_CallMethod(di->
py_inst,
"reset", NULL);
1295 PyGILState_Release(gstate);
1298 for (l = di->
next_di; l; l = l->next) {
1299 ret = srd_inst_terminate_reset(l->data);
1312 PyGILState_STATE gstate;
1314 srd_dbg(
"Freeing instance %s.",
di->
inst_id);
1316 srd_inst_join_decode_thread(
di);
1318 srd_inst_reset_state(
di);
1320 gstate = PyGILState_Ensure();
1322 PyGILState_Release(gstate);
1338 SRD_PRIV void srd_inst_free_all(
struct srd_session *sess)
1343 g_slist_free_full(sess->di_list, (GDestroyNotify)srd_inst_free);