+ /*
+ * The typical mode of operation is to apply a user specified
+ * mapping of sigrok channels to decoder inputs. Lack of mapping
+ * specs will assign no signals (compatible behaviour to earlier
+ * implementations).
+ *
+ * Alternatively we can assign sigrok logic channels to decoder
+ * inputs in the strict order of channel indices, or when their
+ * names match. Users need to request this automatic assignment
+ * though, because this behaviour differs from earlier versions.
+ *
+ * Even more sophisticated heuristics of mapping sigrok channels
+ * to decoder inputs are not implemented here. Later versions
+ * could translate the Pulseview approach to the C language, but
+ * it's better to stabilize that logic first before porting it.
+ */
+ assign = ASSIGN_USER_SPEC;
+ keyword = g_hash_table_lookup(channel_map, keyword_assign);
+ if (g_hash_table_size(channel_map) != 1) {
+ assign = ASSIGN_USER_SPEC;
+ } else if (!keyword) {
+ assign = ASSIGN_USER_SPEC;
+ } else if (strcmp(keyword, assign_by_index) == 0) {
+ assign = ASSIGN_BY_INDEX;
+ } else if (strcmp(keyword, assign_by_name) == 0) {
+ assign = ASSIGN_BY_NAMES;
+ } else {
+ g_critical("Unknown type of decoder channel assignment: %s.",
+ keyword);
+ return;
+ }
+
+ pd = di->decoder;
+ if (assign == ASSIGN_BY_INDEX) {
+ /*
+ * Iterate the protocol decoder's list of input signals
+ * (mandatory and optional, never more than the decoder's
+ * total channels count). Assign sigrok logic channels
+ * until either is exhausted. Use sigrok channels in the
+ * very order of their declaration in the input stream.
+ */
+ l_ch = channel_list;
+ l_pd = pd->channels;
+ while (l_ch && l_pd) {
+ ch = l_ch->data;
+ l_ch = l_ch->next;
+ if (ch->type != SR_CHANNEL_LOGIC)
+ break;
+ if (ch->index >= di->dec_num_channels)
+ break;
+ pdch = l_pd->data;
+ l_pd = l_pd->next;
+ if (!l_pd)
+ l_pd = pd->opt_channels;
+ /* TODO Emit an INFO message. */
+ g_hash_table_insert(channel_map,
+ g_strdup(pdch->id), g_strdup(ch->name));
+ }
+ } else if (assign == ASSIGN_BY_NAMES) {
+ /*
+ * Iterate the protocol decoder's list of input signals.
+ * Search for sigrok logic channels that have matching
+ * names (case insensitive comparison). Not finding a
+ * sigrok channel of a given name is non-fatal (could be
+ * an optional channel, the decoder will check later for
+ * all mandatory channels to be assigned).
+ */
+ l_pd = pd->channels;
+ l_pdo = pd->opt_channels;
+ while (l_pd) {
+ pdch = l_pd->data;
+ l_pd = l_pd->next;
+ if (!l_pd) {
+ l_pd = l_pdo;
+ l_pdo = NULL;
+ }
+ ch = find_channel(channel_list, pdch->id, FALSE);
+ if (!ch) {
+ /* TODO Emit a WARN message. */
+ /* if (l_pdo) ...; */
+ continue;
+ }
+ if (ch->type != SR_CHANNEL_LOGIC) {
+ /* TODO Emit a WARN message. */
+ continue;
+ }
+ /* TODO Emit an INFO message. */
+ g_hash_table_insert(channel_map,
+ g_strdup(pdch->id), g_strdup(ch->name));
+ }
+ }
+