Mesh: Refactor Model subscription & foreach API's 31/239831/3
authorAnupam Roy <anupam.r@samsung.com>
Thu, 30 Jul 2020 05:39:51 +0000 (11:09 +0530)
committerAnupam Roy <anupam.r@samsung.com>
Thu, 30 Jul 2020 06:45:45 +0000 (12:15 +0530)
This patch refactors following
- Group & Virtual Group subscription API's
- Fix improper handling of element_foreach_models API

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

index 43e9c9f..3ab088c 100644 (file)
@@ -1857,6 +1857,7 @@ int bt_mesh_element_foreach_models(bt_mesh_element_h element,
        bt_mesh_element_s *element_s;
 
        GPtrArray *models = NULL;
+       GSList *l = NULL;
        uint32_t *model_info;
        int error_code = BT_ERROR_NONE;
        int i;
@@ -1912,6 +1913,7 @@ int bt_mesh_element_foreach_models(bt_mesh_element_h element,
        g_strlcpy(net.token.token, network_s->token, 17);
        g_strlcpy(net.name.name, network_s->name, BT_MESH_NETWORK_NAME_STRING_MAX_LEN + 1);
 
+       BT_INFO("Mesh: Get Models for element of Remote Node");
        error_code = _bt_get_error_code(bluetooth_mesh_element_get_all_models(&net,
                                /* Node identity */node_s->unicast,
                                /* eleement identity */element_s->index,
@@ -1923,9 +1925,28 @@ int bt_mesh_element_foreach_models(bt_mesh_element_h element,
        }
 
        total = models->len;
+       BT_INFO("Mesh: Total models in element [%d]", total);
        for (i = 0; i < models->len; i++) {
                model_info = g_ptr_array_index(models, i);
                if (model_info) {
+                       BT_INFO("Mesh: Model ID [0x%4.4x]", *model_info);
+                       /* Skip if Model is already added in the element */
+                       l = g_slist_find_custom(element_s->models, GUINT_TO_POINTER(*model_info),
+                                               (GCompareFunc)__compare_model_id);
+                       if (l) {
+                               bt_mesh_model_id_s modid;
+                               bt_mesh_model_s *mod = (bt_mesh_model_s*)l->data;
+                               modid.company_id = mod->id >> 16;
+                               modid.model_id = mod->id;
+
+                               BT_INFO("Mesh: Model ID [0x%4.4x] is already present in element", *model_info);
+                               if (!callback(BT_ERROR_NONE, (bt_mesh_element_h)element_s, total,
+                                                       (bt_mesh_model_h) mod,
+                                                       &modid, user_data)) {
+                                       break;
+                               }
+                               continue;
+                       }
                        /* Create and insert model object in list */
                        bt_mesh_model_s *mod;
                        bt_mesh_model_id_s modid;
@@ -1940,6 +1961,9 @@ int bt_mesh_element_foreach_models(bt_mesh_element_h element,
 
                        modid.company_id = *model_info >> 16;
                        modid.model_id = *model_info;
+                       BT_INFO("Mesh: Model was not present in Element, created & inserted");
+                       BT_INFO("Mesh: Total Models present in Element [%d]",
+                                       g_slist_length(element_s->models));
                        /* Send Callback */
                        if (!callback(BT_ERROR_NONE, (bt_mesh_element_h)element_s, total,
                                                (bt_mesh_model_h) mod,
@@ -3096,10 +3120,23 @@ int bt_mesh_model_configure_group_subscription(bt_mesh_model_subscription_op_e m
        BT_CHECK_INPUT_PARAMETER(callback);
 
        model_s = (bt_mesh_model_s*) model;
-       group_s = (bt_mesh_group_s*) model;
+       BT_MESH_VALIDATE_HANDLE(model_s, model_list);
+
+       group_s = (bt_mesh_group_s*) group;
+       BT_MESH_VALIDATE_HANDLE(group_s, group_list);
+
        element_s = (bt_mesh_element_s*) model_s->parent;
+       BT_MESH_VALIDATE_HANDLE(element_s, element_list);
+
        node_s = (bt_mesh_node_s*) element_s->parent;
+       BT_MESH_VALIDATE_HANDLE(node_s, node_list);
+
        network_s = (bt_mesh_network_s*) node_s->parent;
+       BT_MESH_VALIDATE_HANDLE(network_s, networks);
+
+       BT_INFO("Mesh: Configure Group Request [%d]", model_op);
+       BT_INFO("Mesh: Group Addr [0x%2.2x] Is Virtual [%s]",
+                       group_s->addr, group_s->is_virtual? "YES" : "NO");
 
        /* Check group belongs to the same network */
        if (network_s != group_s->parent)
@@ -3109,16 +3146,6 @@ int bt_mesh_model_configure_group_subscription(bt_mesh_model_subscription_op_e m
        if (group_s->is_virtual)
                return BT_ERROR_INVALID_PARAMETER;
 
-       BT_CHECK_INPUT_PARAMETER(element_s);
-       BT_CHECK_INPUT_PARAMETER(node_s);
-       BT_CHECK_INPUT_PARAMETER(network_s);
-
-       BT_MESH_VALIDATE_HANDLE(model_s, model_list);
-       BT_MESH_VALIDATE_HANDLE(group_s, group_list);
-       BT_MESH_VALIDATE_HANDLE(element_s, element_list);
-       BT_MESH_VALIDATE_HANDLE(node_s, node_list);
-       BT_MESH_VALIDATE_HANDLE(network_s, networks);
-
        /* Return error, if node is not attached */
        if (!node_s->is_attached)
                return BT_ERROR_INVALID_PARAMETER;
@@ -3127,6 +3154,10 @@ int bt_mesh_model_configure_group_subscription(bt_mesh_model_subscription_op_e m
                req.sub_addr = group_s->addr;
        req.is_virtual_sub = false;
        req.op = model_op;
+       g_strlcpy(req.net_uuid, network_s->uuid, 33);
+       req.primary_unicast = node_s->unicast;
+       req.elem_index = element_s->index;
+       req.model = model_s->id;
 
        error_code = _bt_get_error_code(bluetooth_mesh_model_configure_group_sub(&req));
        if (error_code != BT_ERROR_NONE) {
@@ -3160,10 +3191,23 @@ int bt_mesh_model_configure_virtual_group_subscription(bt_mesh_model_subscriptio
        BT_CHECK_INPUT_PARAMETER(callback);
 
        model_s = (bt_mesh_model_s*) model;
-       group_s = (bt_mesh_group_s*) model;
+       BT_MESH_VALIDATE_HANDLE(model_s, model_list);
+
+       group_s = (bt_mesh_group_s*) group;
+       BT_MESH_VALIDATE_HANDLE(group_s, group_list);
+
        element_s = (bt_mesh_element_s*) model_s->parent;
+       BT_MESH_VALIDATE_HANDLE(element_s, element_list);
+
        node_s = (bt_mesh_node_s*) element_s->parent;
+       BT_MESH_VALIDATE_HANDLE(node_s, node_list);
+
        network_s = (bt_mesh_network_s*) node_s->parent;
+       BT_MESH_VALIDATE_HANDLE(network_s, networks);
+
+       BT_INFO("Mesh: Configure Virtual Group Request [%d]", model_op);
+       BT_INFO("Mesh: Group Addr [0x%2.2x] Is Virtual [%s]",
+                       group_s->addr, group_s->is_virtual? "YES" : "NO");
 
        /* Check group belongs to the same network */
        if (network_s != group_s->parent)
@@ -3173,25 +3217,18 @@ int bt_mesh_model_configure_virtual_group_subscription(bt_mesh_model_subscriptio
        if (!group_s->is_virtual)
                return BT_ERROR_INVALID_PARAMETER;
 
-       BT_CHECK_INPUT_PARAMETER(element_s);
-       BT_CHECK_INPUT_PARAMETER(node_s);
-       BT_CHECK_INPUT_PARAMETER(network_s);
-
-       BT_MESH_VALIDATE_HANDLE(model_s, model_list);
-       BT_MESH_VALIDATE_HANDLE(group_s, group_list);
-       BT_MESH_VALIDATE_HANDLE(element_s, element_list);
-       BT_MESH_VALIDATE_HANDLE(node_s, node_list);
-       BT_MESH_VALIDATE_HANDLE(network_s, networks);
-
        /* Return error, if node is not attached */
        if (!node_s->is_attached)
                return BT_ERROR_INVALID_PARAMETER;
 
        if (model_op != BT_MESH_MODEL_SUBSCRIPTION_DELETE_ALL)
                req.sub_addr = group_s->addr;
-
        req.is_virtual_sub = true;
        req.op = model_op;
+       g_strlcpy(req.net_uuid, network_s->uuid, 33);
+       req.primary_unicast = node_s->unicast;
+       req.elem_index = element_s->index;
+       req.model = model_s->id;
 
        error_code = _bt_get_error_code(bluetooth_mesh_model_configure_virtual_group_sub(&req));
        if (error_code != BT_ERROR_NONE) {