mesh: Keep element and model lists sorted and unique
authorMichał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Fri, 26 Jul 2019 06:36:04 +0000 (08:36 +0200)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 16:24:25 +0000 (21:54 +0530)
This keeps composition data unchanged even if elements or models are
registered in a different order.

Change-Id: Ic918001bc152e92ba6f08274159d81b9a65b20b1
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/node.c

index 77d2e16..5f4892e 100644 (file)
@@ -161,6 +161,20 @@ static bool match_element_idx(const void *a, const void *b)
        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;
@@ -172,6 +186,29 @@ static bool match_element_path(const void *a, const void *b)
        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))
@@ -288,6 +325,17 @@ void node_remove(struct mesh_node *node)
        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)
 {
@@ -306,7 +354,10 @@ static bool add_models(struct mesh_node *node, struct node_element *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;
@@ -335,7 +386,8 @@ static void add_internal_model(struct mesh_node *node, uint32_t mod_id,
        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,
@@ -1027,12 +1079,12 @@ bool node_parse_composition(struct mesh_node *node, uint8_t *data,
                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;
                }
@@ -1049,12 +1101,12 @@ bool node_parse_composition(struct mesh_node *node, uint8_t *data,
                        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;
                }
@@ -1152,12 +1204,9 @@ static void get_models_from_properties(struct node_element *ele,
                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;
        }
@@ -1167,7 +1216,8 @@ static void get_models_from_properties(struct node_element *ele,
                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);
        }
 }
 
@@ -1217,7 +1267,11 @@ static bool get_element_properties(struct mesh_node *node, const char *path,
        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);