22 #include "libsigrokdecode-internal.h"
70 PyObject *py_di_options, *py_optval;
77 PyGILState_STATE gstate;
80 srd_err(
"Invalid decoder instance.");
85 srd_err(
"Invalid options GHashTable.");
89 gstate = PyGILState_Ensure();
91 if (!PyObject_HasAttrString(di->
decoder->
py_dec,
"options")) {
93 PyGILState_Release(gstate);
94 if (g_hash_table_size(options) == 0) {
98 srd_err(
"Protocol decoder has no options.");
113 if (!(py_di_options = PyObject_GetAttrString(di->
py_inst,
"options")))
115 Py_DECREF(py_di_options);
116 py_di_options = PyDict_New();
117 PyObject_SetAttrString(di->
py_inst,
"options", py_di_options);
121 if ((value = g_hash_table_lookup(options, sdo->
id))) {
123 if (!g_variant_type_equal(g_variant_get_type(value),
124 g_variant_get_type(sdo->
def))) {
125 srd_err(
"Option '%s' should have the same type "
126 "as the default value.", sdo->
id);
133 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
134 val_str = g_variant_get_string(value, NULL);
135 if (!(py_optval = PyUnicode_FromString(val_str))) {
138 srd_err(
"Option '%s' requires a UTF-8 string value.", sdo->
id);
141 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT64)) {
142 val_int = g_variant_get_int64(value);
143 if (!(py_optval = PyLong_FromLong(val_int))) {
146 srd_err(
"Option '%s' has invalid integer value.", sdo->
id);
149 }
else if (g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) {
150 val_double = g_variant_get_double(value);
151 if (!(py_optval = PyFloat_FromDouble(val_double))) {
154 srd_err(
"Option '%s' has invalid float value.",
159 if (PyDict_SetItemString(py_di_options, sdo->
id, py_optval) == -1)
162 g_hash_table_remove(options, sdo->
id);
164 if (g_hash_table_size(options) != 0)
165 srd_warn(
"Unknown options specified for '%s'", di->
inst_id);
170 Py_XDECREF(py_optval);
171 if (PyErr_Occurred()) {
172 srd_exception_catch(
"Stray exception in srd_inst_option_set()");
175 PyGILState_Release(gstate);
181 static gint compare_channel_id(
const struct srd_channel *pdch,
182 const char *channel_id)
184 return strcmp(pdch->
id, channel_id);
203 GHashTable *new_channels)
205 GVariant *channel_val;
209 int *new_channelmap, new_channelnum, num_required_channels, i;
212 srd_dbg(
"Setting channels for instance %s with list of %d channels.",
213 di->
inst_id, g_hash_table_size(new_channels));
215 if (g_hash_table_size(new_channels) == 0)
221 srd_err(
"Protocol decoder %s has no channels to define.",
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);
247 (GCompareFunc)compare_channel_id))) {
250 channel_id, (GCompareFunc)compare_channel_id))) {
251 srd_err(
"Protocol decoder %s has no channel "
253 g_free(new_channelmap);
258 new_channelmap[pdch->
order] = new_channelnum;
259 srd_dbg(
"Setting channel mapping: %s (PD ch idx %d) = input data ch idx %d.",
260 pdch->
id, pdch->
order, new_channelnum);
263 srd_dbg(
"Final channel map:");
269 i - num_required_channels);
271 srd_dbg(
" - PD ch idx %d (%s) = input data ch idx %d (%s)", i,
272 pdch->
id, new_channelmap[i],
273 (i < num_required_channels) ?
"required" :
"optional");
277 for (i = 0; i < num_required_channels; i++) {
278 if (new_channelmap[i] != -1)
281 srd_err(
"Required channel '%s' (index %d) was not specified.",
306 const char *decoder_id, GHashTable *options)
312 PyGILState_STATE gstate;
315 srd_dbg(
"Creating new %s instance.", decoder_id);
317 if (session_is_valid(sess) !=
SRD_OK) {
318 srd_err(
"Invalid session.");
323 srd_err(
"Protocol decoder %s not found.", decoder_id);
333 inst_id = g_hash_table_lookup(options,
"id");
335 di->
inst_id = g_strdup(inst_id);
336 g_hash_table_remove(options,
"id");
341 di->
inst_id = g_strdup_printf(
"%s-%d", decoder_id, i++);
344 di->
inst_id = g_strdup_printf(
"%s-%d", decoder_id, i++);
367 oldpins_array_seed(di);
369 gstate = PyGILState_Ensure();
372 if (!(di->
py_inst = PyObject_CallObject(dec->
py_dec, NULL))) {
373 if (PyErr_Occurred())
374 srd_exception_catch(
"Failed to create %s instance",
376 PyGILState_Release(gstate);
382 PyGILState_Release(gstate);
413 sess->di_list = g_slist_append(sess->di_list, di);
414 srd_dbg(
"Created new %s instance with ID %s.", decoder_id, di->
inst_id);
426 srd_dbg(
"%s: Joining decoder thread.", di->
inst_id);
432 srd_dbg(
"%s: Raising want_term, sending got_new.", di->
inst_id);
438 srd_dbg(
"%s: Running join().", di->
inst_id);
440 srd_dbg(
"%s: Call to join() done.", di->
inst_id);
460 srd_dbg(
"%s: Resetting decoder state.", di->
inst_id);
465 condition_list_free(di);
466 match_array_free(di);
472 oldpins_array_free(di);
494 if (session_is_valid(sess) !=
SRD_OK) {
495 srd_err(
"Invalid session.");
499 if (!di_bottom || !di_top) {
500 srd_err(
"Invalid from/to instance pair.");
504 if (g_slist_find(sess->di_list, di_top)) {
506 sess->di_list = g_slist_remove(sess->di_list, di_top);
512 srd_dbg(
"Stacked %s onto %s.", di_top->
inst_id, di_bottom->
inst_id);
531 if (!strcmp(stack->
inst_id, inst_id))
537 for (l = stack->
next_di; l; l = l->next) {
539 if (!strcmp(tmp->
inst_id, inst_id)) {
568 if (session_is_valid(sess) !=
SRD_OK) {
569 srd_err(
"Invalid session.");
574 for (l = sess->di_list; l; l = l->next) {
576 if ((di = srd_inst_find_by_id_stack(inst_id, tmp)) != NULL)
584 struct srd_session *
sess,
const GSList *stack,
590 if (session_is_valid(sess) !=
SRD_OK) {
591 srd_err(
"Invalid session.");
596 for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
601 di = srd_sess_inst_find_by_obj(sess, tmp->
next_di, obj);
629 struct srd_session *sess;
633 for (l = sessions; di == NULL && l != NULL; l = l->next) {
635 di = srd_sess_inst_find_by_obj(sess, stack, obj);
655 srd_err(
"Invalid decoder instance.");
663 srd_err(
"Incorrect number of channels (need %d, got %d).",
670 if (initial_pins->data[i] <= 2)
672 srd_err(
"Invalid initial channel %d pin state: %d.",
673 i, initial_pins->data[i]);
677 s = g_string_sized_new(100);
678 oldpins_array_seed(di);
683 s = g_string_truncate(s, s->len - 2);
684 srd_dbg(
"Initial pins: %s.", s->str);
685 g_string_free(s, TRUE);
701 srd_dbg(
"%s: Seeding old pins, %s().", di->
inst_id, __func__);
703 arr = g_array_sized_new(FALSE, TRUE,
sizeof(uint8_t), count);
704 g_array_set_size(arr, count);
717 srd_dbg(
"%s: Releasing initial pin state.", di->
inst_id);
730 PyGILState_STATE gstate;
732 srd_dbg(
"Calling start() method on protocol decoder instance %s.",
735 gstate = PyGILState_Ensure();
738 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"start", NULL))) {
739 srd_exception_catch(
"Protocol decoder instance %s",
741 PyGILState_Release(gstate);
747 PyObject_SetAttrString(di->
py_inst,
"samplenum", PyLong_FromLong(0));
750 PyObject_SetAttrString(di->
py_inst,
"matched", Py_None);
752 PyGILState_Release(gstate);
755 for (l = di->
next_di; l; l = l->next) {
757 if ((ret = srd_inst_start(next_di)) !=
SRD_OK)
780 static gboolean sample_matches(uint8_t old_sample, uint8_t sample,
struct srd_term *term)
784 switch (term->type) {
793 case SRD_TERM_RISING_EDGE:
794 if (old_sample == 0 && sample == 1)
797 case SRD_TERM_FALLING_EDGE:
798 if (old_sample == 1 && sample == 0)
801 case SRD_TERM_EITHER_EDGE:
802 if ((old_sample == 1 && sample == 0) || (old_sample == 0 && sample == 1))
805 case SRD_TERM_NO_EDGE:
806 if ((old_sample == 0 && sample == 0) || (old_sample == 1 && sample == 1))
810 if (term->num_samples_already_skipped == term->num_samples_to_skip)
812 term->num_samples_already_skipped++;
815 srd_err(
"Unknown term type %d.", term->type);
843 g_slist_free_full(ll, g_free);
866 const uint8_t *sample_pos)
869 int i, byte_offset, bit_offset;
874 oldpins_array_seed(di);
878 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
886 int i, byte_offset, bit_offset;
887 const uint8_t *sample_pos;
894 oldpins_array_seed(di);
900 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
906 struct srd_term *term,
const uint8_t *sample_pos)
908 uint8_t old_sample, sample;
909 int byte_offset, bit_offset, ch;
913 if (term->type == SRD_TERM_SKIP)
914 return sample_matches(0, 0, term);
919 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
922 return sample_matches(old_sample, sample, term);
926 const GSList *cond,
const uint8_t *sample_pos)
929 struct srd_term *term;
933 for (l = cond; l; l = l->next) {
935 if (!term_matches(di, term, sample_pos))
942 static gboolean at_least_one_condition_matched(
949 for (i = 0; i < num_conditions; i++) {
959 uint64_t i, j, num_samples_to_process;
961 const uint8_t *sample_pos;
962 unsigned int num_conditions;
968 srd_dbg(
"NULL/empty condition list, automatic match.");
973 if (!have_non_null_conds(di)) {
974 srd_dbg(
"Only NULL conditions in list, automatic match.");
982 di->
match_array = g_array_sized_new(FALSE, TRUE,
sizeof(gboolean), num_conditions);
987 update_old_pins_array_initial_pins(di);
1000 di->
match_array->data[j] = all_terms_match(di, cond, sample_pos);
1003 update_old_pins_array(di, sample_pos);
1006 if (at_least_one_condition_matched(di, num_conditions))
1032 if (!di || !found_match)
1035 *found_match = FALSE;
1042 *found_match = find_match(di);
1046 srd_dbg(
"Done, handled all samples (abs cur %" PRIu64
1047 " / abs end %" PRIu64
").",
1053 if (!(*found_match))
1071 static gpointer di_thread(gpointer data)
1076 PyGILState_STATE gstate;
1083 srd_dbg(
"%s: Starting thread routine for decoder.", di->
inst_id);
1085 gstate = PyGILState_Ensure();
1092 srd_dbg(
"%s: Calling decode() method.", di->
inst_id);
1093 py_res = PyObject_CallMethod(di->
py_inst,
"decode", NULL);
1094 srd_dbg(
"%s: decode() method terminated.", di->
inst_id);
1114 if (!py_res && wanted_term) {
1120 srd_dbg(
"%s: Thread done (!res, want_term).", di->
inst_id);
1122 PyGILState_Release(gstate);
1131 srd_dbg(
"%s: decode() terminated unrequested.", di->
inst_id);
1132 srd_exception_catch(
"Protocol decoder instance %s: ", di->
inst_id);
1133 srd_dbg(
"%s: Thread done (!res, !want_term).", di->
inst_id);
1134 PyGILState_Release(gstate);
1143 srd_dbg(
"%s: decode() terminated (req %d).", di->
inst_id, wanted_term);
1147 PyGILState_Release(gstate);
1149 srd_dbg(
"%s: Thread done (with res).", di->
inst_id);
1210 srd_dbg(
"empty decoder instance");
1214 srd_dbg(
"NULL buffer pointer");
1217 if (inbuflen == 0) {
1218 srd_dbg(
"empty buffer");
1221 if (unitsize == 0) {
1222 srd_dbg(
"unitsize 0");
1227 abs_end_samplenum < abs_start_samplenum) {
1228 srd_dbg(
"Incorrect sample numbers: start=%" PRIu64
", cur=%"
1229 PRIu64
", end=%" PRIu64
".", abs_start_samplenum,
1236 srd_dbg(
"Decoding: abs start sample %" PRIu64
", abs end sample %"
1237 PRIu64
" (%" PRIu64
" samples, %" PRIu64
" bytes, unitsize = "
1238 "%d), instance %s.", abs_start_samplenum, abs_end_samplenum,
1239 abs_end_samplenum - abs_start_samplenum, inbuflen, di->
data_unitsize,
1244 srd_dbg(
"No worker thread for this decoder stack "
1245 "exists yet, creating one: %s.", di->
inst_id);
1291 PyGILState_STATE gstate;
1306 srd_dbg(
"Terminating instance %s", di->
inst_id);
1307 srd_inst_join_decode_thread(di);
1308 srd_inst_reset_state(di);
1317 gstate = PyGILState_Ensure();
1318 if (PyObject_HasAttrString(di->
py_inst,
"reset")) {
1319 srd_dbg(
"Calling .reset() of instance %s", di->
inst_id);
1320 py_ret = PyObject_CallMethod(di->
py_inst,
"reset", NULL);
1323 PyGILState_Release(gstate);
1328 for (l = di->
next_di; l; l = l->next) {
1329 ret = srd_inst_terminate_reset(l->data);
1342 PyGILState_STATE gstate;
1344 srd_dbg(
"Freeing instance %s", di->
inst_id);
1346 srd_inst_join_decode_thread(di);
1348 srd_inst_reset_state(di);
1350 gstate = PyGILState_Ensure();
1352 PyGILState_Release(gstate);
1358 for (l = di->
pd_output; l; l = l->next) {
1368 SRD_PRIV void srd_inst_free_all(
struct srd_session *sess)
1370 if (session_is_valid(sess) !=
SRD_OK) {
1371 srd_err(
"Invalid session.");
1375 g_slist_free_full(sess->di_list, (GDestroyNotify)srd_inst_free);
uint8_t * channel_samples
GSList * opt_channels
List of optional channels for 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.
GArray * match_array
Array of booleans denoting which conditions matched.
Structure which contains information about one protocol decoder channel.
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
gboolean got_new_samples
Indicates whether new samples are available for processing.
int srd_inst_option_set(struct srd_decoder_inst *di, GHashTable *options)
Set one or more options in a decoder instance.
const uint8_t * inbuf
Pointer to the buffer/chunk of input samples.
int srd_inst_initial_pins_set_all(struct srd_decoder_inst *di, GArray *initial_pins)
Set the list of initial (assumed) pin values.
char * name
The (short) decoder name.
GArray * old_pins_array
Array of "old" (previous sample) pin values.
uint64_t inbuflen
Length (in bytes) of the input sample buffer.
int order
The index of the channel, i.e.
char * id
The ID of the channel.
GThread * thread_handle
Handle for this PD stack's worker thread.
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.
The public libsigrokdecode header file to be used by frontends.
GCond got_new_samples_cond
GCond handled_all_samples_cond
uint64_t abs_end_samplenum
Absolute end sample number.
int srd_inst_channel_set_all(struct srd_decoder_inst *di, GHashTable *new_channels)
Set all channels in a decoder instance.
GSList * options
List of decoder options.
void * py_dec
sigrokdecode.Decoder class.
struct srd_decoder * decoder
uint64_t abs_start_samplenum
Absolute start sample number.
gboolean want_wait_terminate
Requests termination of wait() and decode().
uint64_t abs_cur_samplenum
Absolute current samplenumber.
gboolean handled_all_samples
Indicates whether the worker thread has handled all samples.
GSList * channels
List of channels required by this decoder.
Generic/unspecified error.
GSList * condition_list
List of conditions a PD wants to wait for.
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_session * sess