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_FromLongLong(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");
363 di->
inst_id = g_strdup(inst_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);
443 sess->di_list = g_slist_append(sess->di_list, di);
444 srd_dbg(
"Creating new %s instance %s.", decoder_id, di->
inst_id);
456 srd_dbg(
"%s: Joining decoder thread.", di->
inst_id);
462 srd_dbg(
"%s: Raising want_term, sending got_new.", di->
inst_id);
468 srd_dbg(
"%s: Running join().", di->
inst_id);
470 srd_dbg(
"%s: Call to join() done.", di->
inst_id);
490 srd_dbg(
"%s: Resetting decoder state.", di->
inst_id);
493 condition_list_free(di);
494 match_array_free(di);
500 oldpins_array_free(di);
528 if (!di_bottom || !di_top) {
529 srd_err(
"Invalid from/to instance pair.");
533 if (g_slist_find(sess->di_list, di_top)) {
535 sess->di_list = g_slist_remove(sess->di_list, di_top);
543 gboolean at_least_one_match =
FALSE;
544 for (GSList *out = di_bottom->
decoder->
outputs; out; out = out->next) {
545 const char *o = out->data;
546 for (GSList *in = di_top->
decoder->
inputs; in; in = in->next) {
547 const char *i = in->data;
549 at_least_one_match =
TRUE;
555 if (!at_least_one_match)
556 srd_warn(
"No matching in-/output when stacking %s onto %s.",
562 srd_dbg(
"Stacking %s onto %s.", di_top->
inst_id, di_bottom->
inst_id);
581 if (!strcmp(stack->
inst_id, inst_id))
587 for (l = stack->
next_di; l; l = l->next) {
589 if (!strcmp(tmp->
inst_id, inst_id)) {
623 for (l = sess->di_list; l; l = l->next) {
625 if ((di = srd_inst_find_by_id_stack(inst_id, tmp)) != NULL)
646 srd_err(
"Invalid decoder instance.");
654 srd_err(
"Incorrect number of channels (need %d, got %d).",
661 if (initial_pins->data[i] <= 2)
663 srd_err(
"Invalid initial channel %d pin state: %d.",
664 i, initial_pins->data[i]);
668 s = g_string_sized_new(100);
669 oldpins_array_seed(di);
674 s = g_string_truncate(s, s->len - 2);
675 srd_dbg(
"Initial pins: %s.", s->str);
676 g_string_free(s,
TRUE);
684 PyObject *py_res, *py_samplenum;
688 PyGILState_STATE gstate;
690 srd_dbg(
"Calling start() of instance %s.", di->
inst_id);
692 gstate = PyGILState_Ensure();
695 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"start", NULL))) {
696 srd_exception_catch(
"Protocol decoder instance %s",
698 PyGILState_Release(gstate);
704 py_samplenum = PyLong_FromUnsignedLongLong(0);
705 PyObject_SetAttrString(di->
py_inst,
"samplenum", py_samplenum);
706 Py_DECREF(py_samplenum);
709 PyObject_SetAttrString(di->
py_inst,
"matched", Py_None);
711 PyGILState_Release(gstate);
714 for (l = di->
next_di; l; l = l->next) {
716 if ((ret = srd_inst_start(next_di)) !=
SRD_OK)
739 __attribute__((always_inline))
740 static inline gboolean sample_matches(uint8_t old_sample, uint8_t sample,
struct srd_term *term)
744 switch (term->type) {
753 case SRD_TERM_RISING_EDGE:
754 if (old_sample == 0 && sample == 1)
757 case SRD_TERM_FALLING_EDGE:
758 if (old_sample == 1 && sample == 0)
761 case SRD_TERM_EITHER_EDGE:
762 if ((old_sample == 1 && sample == 0) || (old_sample == 0 && sample == 1))
765 case SRD_TERM_NO_EDGE:
766 if ((old_sample == 0 && sample == 0) || (old_sample == 1 && sample == 1))
770 if (term->num_samples_already_skipped == term->num_samples_to_skip)
772 term->num_samples_already_skipped++;
775 srd_err(
"Unknown term type %d.", term->type);
803 g_slist_free_full(ll, g_free);
827 const uint8_t *sample_pos)
830 int i, byte_offset, bit_offset;
835 oldpins_array_seed(di);
841 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
849 int i, byte_offset, bit_offset;
850 const uint8_t *sample_pos;
857 oldpins_array_seed(di);
865 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
871 struct srd_term *term,
const uint8_t *sample_pos)
873 uint8_t old_sample, sample;
874 int byte_offset, bit_offset, ch;
878 if (term->type == SRD_TERM_SKIP)
879 return sample_matches(0, 0, term);
884 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
887 return sample_matches(old_sample, sample, term);
891 const GSList *cond,
const uint8_t *sample_pos)
894 struct srd_term *term;
898 for (l = cond; l; l = l->next) {
900 if (term->type == SRD_TERM_ALWAYS_FALSE)
902 if (!term_matches(di, term, sample_pos))
909 static gboolean at_least_one_condition_matched(
916 for (i = 0; i < num_conditions; i++) {
926 uint64_t i, j, num_samples_to_process;
928 const uint8_t *sample_pos;
929 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.");
954 update_old_pins_array_initial_pins(di);
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)
1043 PyGILState_STATE gstate;
1050 srd_dbg(
"%s: Starting thread routine for decoder.", di->
inst_id);
1052 gstate = PyGILState_Ensure();
1059 srd_dbg(
"%s: Calling decode().", di->
inst_id);
1060 py_res = PyObject_CallMethod(di->
py_inst,
"decode", NULL);
1061 srd_dbg(
"%s: decode() terminated.", di->
inst_id);
1067 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_EOFError)) {
1068 srd_dbg(
"%s: ignoring EOFError during decode() execution.",
1095 if (!py_res && wanted_term) {
1101 srd_dbg(
"%s: Thread done (!res, want_term).", di->
inst_id);
1103 PyGILState_Release(gstate);
1112 srd_dbg(
"%s: decode() terminated unrequested.", di->
inst_id);
1113 srd_exception_catch(
"Protocol decoder instance %s: ", di->
inst_id);
1114 srd_dbg(
"%s: Thread done (!res, !want_term).", di->
inst_id);
1115 PyGILState_Release(gstate);
1124 srd_dbg(
"%s: decode() terminated (req %d).", di->
inst_id, wanted_term);
1128 PyGILState_Release(gstate);
1130 srd_dbg(
"%s: Thread done (with res).", di->
inst_id);
1191 srd_dbg(
"empty decoder instance");
1195 srd_dbg(
"NULL buffer pointer");
1198 if (inbuflen == 0) {
1199 srd_dbg(
"empty buffer");
1202 if (unitsize == 0) {
1203 srd_dbg(
"unitsize 0");
1208 abs_end_samplenum < abs_start_samplenum) {
1209 srd_dbg(
"Incorrect sample numbers: start=%" PRIu64
", cur=%" 1210 PRIu64
", end=%" PRIu64
".", abs_start_samplenum,
1217 srd_dbg(
"Decoding: abs start sample %" PRIu64
", abs end sample %" 1218 PRIu64
" (%" PRIu64
" samples, %" PRIu64
" bytes, unitsize = " 1219 "%d), instance %s.", abs_start_samplenum, abs_end_samplenum,
1220 abs_end_samplenum - abs_start_samplenum, inbuflen, di->
data_unitsize,
1225 srd_dbg(
"No worker thread for this decoder stack " 1226 "exists yet, creating one: %s.", di->
inst_id);
1271 PyGILState_STATE gstate;
1279 gstate = PyGILState_Ensure();
1280 if (PyObject_HasAttrString(di->
py_inst,
"flush")) {
1281 srd_dbg(
"Calling flush() of instance %s", di->
inst_id);
1282 py_ret = PyObject_CallMethod(di->
py_inst,
"flush", NULL);
1285 PyGILState_Release(gstate);
1288 for (l = di->
next_di; l; l = l->next) {
1289 ret = srd_inst_flush(l->data);
1319 srd_dbg(
"End of sample data: instance %s.", di->
inst_id);
1321 srd_dbg(
"No worker thread, nothing to do.");
1346 for (l = di->
next_di; l; l = l->next) {
1347 ret = srd_inst_send_eof(l->data);
1375 PyGILState_STATE gstate;
1390 srd_dbg(
"Terminating instance %s", di->
inst_id);
1391 srd_inst_join_decode_thread(di);
1392 srd_inst_reset_state(di);
1401 gstate = PyGILState_Ensure();
1402 if (PyObject_HasAttrString(di->
py_inst,
"reset")) {
1403 srd_dbg(
"Calling reset() of instance %s", di->
inst_id);
1404 py_ret = PyObject_CallMethod(di->
py_inst,
"reset", NULL);
1407 PyGILState_Release(gstate);
1410 for (l = di->
next_di; l; l = l->next) {
1411 ret = srd_inst_terminate_reset(l->data);
1424 PyGILState_STATE gstate;
1426 srd_dbg(
"Freeing instance %s.", di->
inst_id);
1428 srd_inst_join_decode_thread(di);
1430 srd_inst_reset_state(di);
1432 gstate = PyGILState_Ensure();
1434 PyGILState_Release(gstate);
1440 for (l = di->
pd_output; l; l = l->next) {
1450 SRD_PRIV void srd_inst_free_all(
struct srd_session *sess)
1455 g_slist_free_full(sess->di_list, (GDestroyNotify)srd_inst_free);
uint64_t inbuflen
Length (in bytes) of the input sample buffer.
gboolean communicate_eof
Requests that .wait() terminates a Python iteration.
GCond got_new_samples_cond
GThread * thread_handle
Handle for this PD stack's worker thread.
GSList * options
List of decoder options.
GSList * channels
List of channels required by this decoder.
Generic/unspecified error.
uint64_t abs_start_samplenum
Absolute start sample number.
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.
int srd_inst_initial_pins_set_all(struct srd_decoder_inst *di, GArray *initial_pins)
Set the list of initial (assumed) pin values.
GCond handled_all_samples_cond
struct srd_decoder_inst * srd_inst_new(struct srd_session *sess, const char *decoder_id, GHashTable *options)
Create a new protocol decoder instance.
int order
The index of the channel, i.e.
uint64_t abs_cur_samplenum
Absolute current samplenumber.
const uint8_t * inbuf
Pointer to the buffer/chunk of input samples.
void * py_dec
sigrokdecode.Decoder class.
gboolean got_new_samples
Indicates whether new samples are available for processing.
GArray * match_array
Array of booleans denoting which conditions matched.
char * id
The ID of the channel.
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
gboolean handled_all_samples
Indicates whether the worker thread has handled all samples.
uint8_t * channel_samples
int srd_inst_option_set(struct srd_decoder_inst *di, GHashTable *options)
Set one or more options in a decoder instance.
struct srd_decoder * decoder
int srd_inst_channel_set_all(struct srd_decoder_inst *di, GHashTable *new_channels)
Set all channels in a decoder instance.
GArray * old_pins_array
Array of "old" (previous sample) pin values.
uint64_t abs_end_samplenum
Absolute end sample number.
struct srd_session * sess
GSList * condition_list
List of conditions a PD wants to wait for.
GSList * opt_channels
List of optional channels for this decoder.
GSList * inputs
List of possible decoder input IDs.
GSList * outputs
List of possible decoder output IDs.
gboolean want_wait_terminate
Requests termination of wait() and decode().
The public libsigrokdecode header file to be used by frontends.
Structure which contains information about one protocol decoder channel.
int decoder_state
Indicates the current state of the decoder stack.
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.
char * name
The (short) decoder name.