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.",
283 g_free(new_channelmap);
308 const char *decoder_id, GHashTable *options)
314 PyGILState_STATE gstate;
317 srd_dbg(
"Creating new %s instance.", decoder_id);
323 srd_err(
"Protocol decoder %s not found.", decoder_id);
333 inst_id = g_hash_table_lookup(options,
"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);
414 sess->di_list = g_slist_append(
sess->di_list, di);
415 srd_dbg(
"Created new %s instance with ID %s.", decoder_id, di->
inst_id);
427 srd_dbg(
"%s: Joining decoder thread.", di->
inst_id);
433 srd_dbg(
"%s: Raising want_term, sending got_new.", di->
inst_id);
439 srd_dbg(
"%s: Running join().", di->
inst_id);
441 srd_dbg(
"%s: Call to join() done.", di->
inst_id);
461 srd_dbg(
"%s: Resetting decoder state.", di->
inst_id);
464 condition_list_free(di);
465 match_array_free(di);
471 oldpins_array_free(di);
498 if (!di_bottom || !di_top) {
499 srd_err(
"Invalid from/to instance pair.");
503 if (g_slist_find(
sess->di_list, di_top)) {
505 sess->di_list = g_slist_remove(
sess->di_list, di_top);
511 srd_dbg(
"Stacked %s onto %s.", di_top->
inst_id, di_bottom->
inst_id);
536 for (l = stack->
next_di; l; l = l->next) {
572 for (l =
sess->di_list; l; l = l->next) {
574 if ((di = srd_inst_find_by_id_stack(
inst_id, tmp)) != NULL)
595 srd_err(
"Invalid decoder instance.");
603 srd_err(
"Incorrect number of channels (need %d, got %d).",
610 if (initial_pins->data[i] <= 2)
612 srd_err(
"Invalid initial channel %d pin state: %d.",
613 i, initial_pins->data[i]);
617 s = g_string_sized_new(100);
618 oldpins_array_seed(di);
623 s = g_string_truncate(s, s->len - 2);
624 srd_dbg(
"Initial pins: %s.", s->str);
625 g_string_free(s, TRUE);
641 srd_dbg(
"%s: Seeding old pins, %s().", di->
inst_id, __func__);
643 arr = g_array_sized_new(FALSE, TRUE,
sizeof(uint8_t), count);
644 g_array_set_size(arr, count);
657 srd_dbg(
"%s: Releasing initial pin state.", di->
inst_id);
670 PyGILState_STATE gstate;
672 srd_dbg(
"Calling start() method on protocol decoder instance %s.",
675 gstate = PyGILState_Ensure();
678 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"start", NULL))) {
679 srd_exception_catch(
"Protocol decoder instance %s",
681 PyGILState_Release(gstate);
687 PyObject_SetAttrString(di->
py_inst,
"samplenum", PyLong_FromLong(0));
690 PyObject_SetAttrString(di->
py_inst,
"matched", Py_None);
692 PyGILState_Release(gstate);
695 for (l = di->
next_di; l; l = l->next) {
720 __attribute__((always_inline))
721 static inline gboolean sample_matches(uint8_t old_sample, uint8_t sample,
struct srd_term *term)
725 switch (term->type) {
734 case SRD_TERM_RISING_EDGE:
735 if (old_sample == 0 && sample == 1)
738 case SRD_TERM_FALLING_EDGE:
739 if (old_sample == 1 && sample == 0)
742 case SRD_TERM_EITHER_EDGE:
743 if ((old_sample == 1 && sample == 0) || (old_sample == 0 && sample == 1))
746 case SRD_TERM_NO_EDGE:
747 if ((old_sample == 0 && sample == 0) || (old_sample == 1 && sample == 1))
751 if (term->num_samples_already_skipped == term->num_samples_to_skip)
753 term->num_samples_already_skipped++;
756 srd_err(
"Unknown term type %d.", term->type);
784 g_slist_free_full(ll, g_free);
807 const uint8_t *sample_pos)
810 int i, byte_offset, bit_offset;
815 oldpins_array_seed(di);
819 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
827 int i, byte_offset, bit_offset;
828 const uint8_t *sample_pos;
835 oldpins_array_seed(di);
841 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
847 struct srd_term *term,
const uint8_t *sample_pos)
849 uint8_t old_sample, sample;
850 int byte_offset, bit_offset, ch;
854 if (term->type == SRD_TERM_SKIP)
855 return sample_matches(0, 0, term);
860 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
863 return sample_matches(old_sample, sample, term);
867 const GSList *cond,
const uint8_t *sample_pos)
870 struct srd_term *term;
874 for (l = cond; l; l = l->next) {
876 if (!term_matches(di, term, sample_pos))
883 static gboolean at_least_one_condition_matched(
890 for (i = 0; i < num_conditions; i++) {
900 uint64_t i, j, num_samples_to_process;
902 const uint8_t *sample_pos;
903 unsigned int num_conditions;
909 srd_dbg(
"NULL/empty condition list, automatic match.");
914 if (!have_non_null_conds(di)) {
915 srd_dbg(
"Only NULL conditions in list, automatic match.");
923 di->
match_array = g_array_sized_new(FALSE, TRUE,
sizeof(gboolean), num_conditions);
928 update_old_pins_array_initial_pins(di);
941 di->
match_array->data[j] = all_terms_match(di, cond, sample_pos);
944 update_old_pins_array(di, sample_pos);
947 if (at_least_one_condition_matched(di, num_conditions))
973 if (!di || !found_match)
976 *found_match = FALSE;
983 *found_match = find_match(di);
987 srd_dbg(
"Done, handled all samples (abs cur %" PRIu64
988 " / abs end %" PRIu64
").",
1012 static gpointer di_thread(gpointer data)
1017 PyGILState_STATE gstate;
1024 srd_dbg(
"%s: Starting thread routine for decoder.", di->
inst_id);
1026 gstate = PyGILState_Ensure();
1033 srd_dbg(
"%s: Calling decode() method.", di->
inst_id);
1034 py_res = PyObject_CallMethod(di->
py_inst,
"decode", NULL);
1035 srd_dbg(
"%s: decode() method terminated.", di->
inst_id);
1058 if (!py_res && wanted_term) {
1064 srd_dbg(
"%s: Thread done (!res, want_term).", di->
inst_id);
1066 PyGILState_Release(gstate);
1075 srd_dbg(
"%s: decode() terminated unrequested.", di->
inst_id);
1076 srd_exception_catch(
"Protocol decoder instance %s: ", di->
inst_id);
1077 srd_dbg(
"%s: Thread done (!res, !want_term).", di->
inst_id);
1078 PyGILState_Release(gstate);
1087 srd_dbg(
"%s: decode() terminated (req %d).", di->
inst_id, wanted_term);
1091 PyGILState_Release(gstate);
1093 srd_dbg(
"%s: Thread done (with res).", di->
inst_id);
1154 srd_dbg(
"empty decoder instance");
1158 srd_dbg(
"NULL buffer pointer");
1162 srd_dbg(
"empty buffer");
1165 if (unitsize == 0) {
1166 srd_dbg(
"unitsize 0");
1172 srd_dbg(
"Incorrect sample numbers: start=%" PRIu64
", cur=%" 1180 srd_dbg(
"Decoding: abs start sample %" PRIu64
", abs end sample %" 1181 PRIu64
" (%" PRIu64
" samples, %" PRIu64
" bytes, unitsize = " 1188 srd_dbg(
"No worker thread for this decoder stack " 1189 "exists yet, creating one: %s.", di->
inst_id);
1239 PyGILState_STATE gstate;
1254 srd_dbg(
"Terminating instance %s", di->
inst_id);
1255 srd_inst_join_decode_thread(di);
1256 srd_inst_reset_state(di);
1265 gstate = PyGILState_Ensure();
1266 if (PyObject_HasAttrString(di->
py_inst,
"reset")) {
1267 srd_dbg(
"Calling .reset() of instance %s", di->
inst_id);
1268 py_ret = PyObject_CallMethod(di->
py_inst,
"reset", NULL);
1271 PyGILState_Release(gstate);
1274 for (l = di->
next_di; l; l = l->next) {
1275 ret = srd_inst_terminate_reset(l->data);
1288 PyGILState_STATE gstate;
1290 srd_dbg(
"Freeing instance %s.",
di->
inst_id);
1292 srd_inst_join_decode_thread(
di);
1294 srd_inst_reset_state(
di);
1296 gstate = PyGILState_Ensure();
1298 PyGILState_Release(gstate);
1314 SRD_PRIV void srd_inst_free_all(
struct srd_session *sess)
1319 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.
int decoder_state
Indicates the current state of the decoder stack.
GCond handled_all_samples_cond
GSList * opt_channels
List of optional channels for this decoder.
struct srd_decoder_inst * di
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