diff --git a/mesh/node.c b/mesh/node.c
index 857d0d0..5eb2e66 100644
--- a/mesh/node.c
+++ b/mesh/node.c
return (element->idx == index);
}
+static int compare_element_idx(const void *a, const void *b, void *user_data)
+{
+ uint32_t a_idx = ((const struct node_element *)a)->idx;
+ uint32_t b_idx = ((const struct node_element *)b)->idx;
+
+ if (a_idx < b_idx)
+ return -1;
+
+ if (a_idx > b_idx)
+ return 1;
+
+ return 0;
+}
+
static bool match_element_path(const void *a, const void *b)
{
const struct node_element *element = a;
return (!strcmp(element->path, path));
}
+static bool match_model_id(const void *a, const void *b)
+{
+ const struct mesh_model *mod = a;
+ uint32_t mod_id = L_PTR_TO_UINT(b);
+
+ return mesh_model_get_model_id(mod) == mod_id;
+}
+
+static int compare_model_id(const void *a, const void *b, void *user_data)
+{
+ uint32_t a_id = mesh_model_get_model_id(a);
+ uint32_t b_id = mesh_model_get_model_id(b);
+
+ if (a_id < b_id)
+ return -1;
+
+ if (a_id > b_id)
+ return 1;
+
+ return 0;
+}
+
+
struct mesh_node *node_find_by_addr(uint16_t addr)
{
if (!IS_UNICAST(addr))
free_node_resources(node);
}
+static bool element_add_model(struct node_element *ele, struct mesh_model *mod)
+{
+ uint32_t mod_id = mesh_model_get_model_id(mod);
+
+ if (l_queue_find(ele->models, match_model_id, L_UINT_TO_PTR(mod_id)))
+ return false;
+
+ l_queue_insert(ele->models, mod, compare_model_id, NULL);
+ return true;
+}
+
static bool add_models(struct mesh_node *node, struct node_element *ele,
struct mesh_config_element *db_ele)
{
if (!mod)
return false;
- l_queue_push_tail(ele->models, mod);
+ if (!element_add_model(ele, mod)) {
+ mesh_model_free(mod);
+ return false;
+ }
}
return true;
if (!ele->models)
ele->models = l_queue_new();
- l_queue_push_tail(ele->models, mod);
+ if (!element_add_model(ele, mod))
+ mesh_model_free(mod);
}
static bool add_element(struct mesh_node *node,
while (len >= 2 && m--) {
mod_id = l_get_le16(data);
mod = mesh_model_new(ele->idx, mod_id);
- if (!mod) {
+ if (!mod || !element_add_model(ele, mod)) {
+ mesh_model_free(mod);
element_free(ele);
goto fail;
}
- l_queue_push_tail(ele->models, mod);
data += 2;
len -= 2;
}
mod_id |= (vendor_id << 16);
mod = mesh_model_vendor_new(ele->idx, vendor_id,
mod_id);
- if (!mod) {
+ if (!mod || !element_add_model(ele, mod)) {
+ mesh_model_free(mod);
element_free(ele);
goto fail;
}
- l_queue_push_tail(ele->models, mod);
data += 4;
len -= 4;
}
while (l_dbus_message_iter_next_entry(&ids, &mod_id)) {
struct mesh_model *mod;
- /* Skip internally implemented models */
- if ((VENDOR_ID_MASK | mod_id) == CONFIG_SRV_MODEL)
- continue;
-
mod = mesh_model_new(ele->idx, mod_id);
- l_queue_push_tail(ele->models, mod);
+ if (!element_add_model(ele, mod))
+ mesh_model_free(mod);
}
return;
}
struct mesh_model *mod;
mod = mesh_model_vendor_new(ele->idx, vendor_id, mod_id);
- l_queue_push_tail(ele->models, mod);
+ if (!element_add_model(ele, mod))
+ mesh_model_free(mod);
}
}
if (!idx || !mods || !vendor_mods)
goto fail;
- l_queue_push_tail(node->elements, ele);
+ if (l_queue_find(node->elements, match_element_idx,
+ L_UINT_TO_PTR(ele->idx)))
+ goto fail;
+
+ l_queue_insert(node->elements, ele, compare_element_idx, NULL);
ele->path = l_strdup(path);