+ */
+__attribute__((always_inline))
+static inline gboolean sample_matches(uint8_t old_sample, uint8_t sample, struct srd_term *term)
+{
+ /* Caller ensures term != NULL. */
+
+ switch (term->type) {
+ case SRD_TERM_HIGH:
+ if (sample == 1)
+ return TRUE;
+ break;
+ case SRD_TERM_LOW:
+ if (sample == 0)
+ return TRUE;
+ break;
+ case SRD_TERM_RISING_EDGE:
+ if (old_sample == 0 && sample == 1)
+ return TRUE;
+ break;
+ case SRD_TERM_FALLING_EDGE:
+ if (old_sample == 1 && sample == 0)
+ return TRUE;
+ break;
+ case SRD_TERM_EITHER_EDGE:
+ if ((old_sample == 1 && sample == 0) || (old_sample == 0 && sample == 1))
+ return TRUE;
+ break;
+ case SRD_TERM_NO_EDGE:
+ if ((old_sample == 0 && sample == 0) || (old_sample == 1 && sample == 1))
+ return TRUE;
+ break;
+ case SRD_TERM_SKIP:
+ if (term->num_samples_already_skipped == term->num_samples_to_skip)
+ return TRUE;
+ term->num_samples_already_skipped++;
+ break;
+ default:
+ srd_err("Unknown term type %d.", term->type);
+ break;
+ }
+
+ return FALSE;
+}
+
+/** @private */
+SRD_PRIV void match_array_free(struct srd_decoder_inst *di)
+{
+ if (!di || !di->match_array)
+ return;
+
+ g_array_free(di->match_array, TRUE);
+ di->match_array = NULL;
+}
+
+/** @private */
+SRD_PRIV void condition_list_free(struct srd_decoder_inst *di)
+{
+ GSList *l, *ll;
+
+ if (!di)
+ return;
+
+ for (l = di->condition_list; l; l = l->next) {
+ ll = l->data;
+ if (ll)
+ g_slist_free_full(ll, g_free);
+ }
+
+ di->condition_list = NULL;
+}
+
+static gboolean have_non_null_conds(const struct srd_decoder_inst *di)
+{
+ GSList *l, *cond;
+
+ if (!di)
+ return FALSE;
+
+ for (l = di->condition_list; l; l = l->next) {
+ cond = l->data;
+ if (cond)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static void update_old_pins_array(struct srd_decoder_inst *di,
+ const uint8_t *sample_pos)
+{
+ uint8_t sample;
+ int i, byte_offset, bit_offset;
+
+ if (!di || !di->dec_channelmap || !sample_pos)
+ return;
+
+ oldpins_array_seed(di);
+ for (i = 0; i < di->dec_num_channels; i++) {
+ byte_offset = di->dec_channelmap[i] / 8;
+ bit_offset = di->dec_channelmap[i] % 8;
+ sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
+ di->old_pins_array->data[i] = sample;
+ }
+}
+
+static void update_old_pins_array_initial_pins(struct srd_decoder_inst *di)
+{
+ uint8_t sample;
+ int i, byte_offset, bit_offset;
+ const uint8_t *sample_pos;
+
+ if (!di || !di->dec_channelmap)
+ return;
+
+ sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
+
+ oldpins_array_seed(di);
+ for (i = 0; i < di->dec_num_channels; i++) {
+ if (di->old_pins_array->data[i] != SRD_INITIAL_PIN_SAME_AS_SAMPLE0)
+ continue;
+ byte_offset = di->dec_channelmap[i] / 8;
+ bit_offset = di->dec_channelmap[i] % 8;
+ sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
+ di->old_pins_array->data[i] = sample;
+ }
+}
+
+static gboolean term_matches(const struct srd_decoder_inst *di,
+ struct srd_term *term, const uint8_t *sample_pos)
+{
+ uint8_t old_sample, sample;
+ int byte_offset, bit_offset, ch;
+
+ /* Caller ensures di, di->dec_channelmap, term, sample_pos != NULL. */
+
+ if (term->type == SRD_TERM_SKIP)
+ return sample_matches(0, 0, term);
+
+ ch = term->channel;
+ byte_offset = di->dec_channelmap[ch] / 8;
+ bit_offset = di->dec_channelmap[ch] % 8;
+ sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
+ old_sample = di->old_pins_array->data[ch];
+
+ return sample_matches(old_sample, sample, term);
+}
+
+static gboolean all_terms_match(const struct srd_decoder_inst *di,
+ const GSList *cond, const uint8_t *sample_pos)
+{
+ const GSList *l;
+ struct srd_term *term;
+
+ /* Caller ensures di, cond, sample_pos != NULL. */
+
+ for (l = cond; l; l = l->next) {
+ term = l->data;
+ if (!term_matches(di, term, sample_pos))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean at_least_one_condition_matched(
+ const struct srd_decoder_inst *di, unsigned int num_conditions)
+{
+ unsigned int i;
+
+ /* Caller ensures di != NULL. */
+
+ for (i = 0; i < num_conditions; i++) {
+ if (di->match_array->data[i])
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean find_match(struct srd_decoder_inst *di)
+{
+ uint64_t i, j, num_samples_to_process;
+ GSList *l, *cond;
+ const uint8_t *sample_pos;
+ unsigned int num_conditions;
+
+ /* Caller ensures di != NULL. */
+
+ /* Check whether the condition list is NULL/empty. */
+ if (!di->condition_list) {
+ srd_dbg("NULL/empty condition list, automatic match.");
+ return TRUE;
+ }
+
+ /* Check whether we have any non-NULL conditions. */
+ if (!have_non_null_conds(di)) {
+ srd_dbg("Only NULL conditions in list, automatic match.");
+ return TRUE;
+ }
+
+ num_samples_to_process = di->abs_end_samplenum - di->abs_cur_samplenum;
+ num_conditions = g_slist_length(di->condition_list);
+
+ /* di->match_array is NULL here. Create a new GArray. */
+ di->match_array = g_array_sized_new(FALSE, TRUE, sizeof(gboolean), num_conditions);
+ g_array_set_size(di->match_array, num_conditions);
+
+ /* Sample 0: Set di->old_pins_array for SRD_INITIAL_PIN_SAME_AS_SAMPLE0 pins. */
+ if (di->abs_cur_samplenum == 0)
+ update_old_pins_array_initial_pins(di);
+
+ for (i = 0; i < num_samples_to_process; i++, (di->abs_cur_samplenum)++) {
+
+ sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
+
+ /* Check whether the current sample matches at least one of the conditions (logical OR). */
+ /* IMPORTANT: We need to check all conditions, even if there was a match already! */
+ for (l = di->condition_list, j = 0; l; l = l->next, j++) {
+ cond = l->data;
+ if (!cond)
+ continue;
+ /* All terms in 'cond' must match (logical AND). */
+ di->match_array->data[j] = all_terms_match(di, cond, sample_pos);
+ }
+
+ update_old_pins_array(di, sample_pos);
+
+ /* If at least one condition matched we're done. */
+ if (at_least_one_condition_matched(di, num_conditions))
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+/**
+ * Process available samples and check if they match the defined conditions.