struct srd_decoder_inst *di;
char *inst_id;
+ i = 1;
srd_dbg("Creating new %s instance.", decoder_id);
if (session_is_valid(sess) != SRD_OK) {
di->decoder = dec;
di->sess = sess;
+
if (options) {
inst_id = g_hash_table_lookup(options, "id");
- di->inst_id = g_strdup(inst_id ? inst_id : decoder_id);
+ if (inst_id)
+ di->inst_id = g_strdup(inst_id);
g_hash_table_remove(options, "id");
- } else
- di->inst_id = g_strdup(decoder_id);
+ }
+
+ /* Create a unique instance ID (as none was provided). */
+ if (!di->inst_id) {
+ di->inst_id = g_strdup_printf("%s-%d", decoder_id, i++);
+ while (srd_inst_find_by_id(sess, di->inst_id)) {
+ g_free(di->inst_id);
+ di->inst_id = g_strdup_printf("%s-%d", decoder_id, i++);
+ }
+ }
/*
* Prepare a default channel map, where samples come in the
/* Instance takes input from a frontend by default. */
sess->di_list = g_slist_append(sess->di_list, di);
+ srd_dbg("Created new %s instance with ID %s.", decoder_id, di->inst_id);
return di;
}
return SRD_OK;
}
+/**
+ * Search a decoder instance and its stack for instance ID.
+ *
+ * @param[in] inst_id ID to search for.
+ * @param[in] stack A decoder instance, potentially with stacked instances.
+ *
+ * @return The matching instance, or NULL.
+ */
+static struct srd_decoder_inst *srd_inst_find_by_id_stack(const char *inst_id,
+ struct srd_decoder_inst *stack)
+{
+ const GSList *l;
+ struct srd_decoder_inst *tmp, *di;
+
+ if (!strcmp(stack->inst_id, inst_id))
+ return stack;
+
+ /* Otherwise, look recursively in our stack. */
+ di = NULL;
+ if (stack->next_di) {
+ for (l = stack->next_di; l; l = l->next) {
+ tmp = l->data;
+ if (!strcmp(tmp->inst_id, inst_id)) {
+ di = tmp;
+ break;
+ }
+ }
+ }
+
+ return di;
+}
+
/**
* Find a decoder instance by its instance ID.
*
- * Only the bottom level of instances are searched -- instances already stacked
- * on top of another one will not be found.
+ * This will recurse to find the instance anywhere in the stack tree of the
+ * given session.
*
* @param sess The session holding the protocol decoder instance.
* @param inst_id The instance ID to be found.
di = NULL;
for (l = sess->di_list; l; l = l->next) {
tmp = l->data;
- if (!strcmp(tmp->inst_id, inst_id)) {
- di = tmp;
+ if ((di = srd_inst_find_by_id_stack(inst_id, tmp)) != NULL)
break;
- }
}
return di;
}
/** @private */
-SRD_PRIV void srd_inst_free_all(struct srd_session *sess, GSList *stack)
+SRD_PRIV void srd_inst_free_all(struct srd_session *sess)
{
- GSList *l;
- struct srd_decoder_inst *di;
-
if (session_is_valid(sess) != SRD_OK) {
srd_err("Invalid session.");
return;
}
- di = NULL;
- for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
- di = l->data;
- if (di->next_di)
- srd_inst_free_all(sess, di->next_di);
- srd_inst_free(di);
- }
- if (!stack) {
- g_slist_free(sess->di_list);
- sess->di_list = NULL;
- }
+ g_slist_free_full(sess->di_list, (GDestroyNotify)srd_inst_free);
}
/** @} */