Mesh: Disallow group deletion if it is in use 44/243844/2
authorAbhay Agarwal <ay.agarwal@samsung.com>
Thu, 10 Sep 2020 11:06:31 +0000 (16:36 +0530)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Fri, 11 Sep 2020 05:20:00 +0000 (10:50 +0530)
Change-Id: I4ec8a71dd53371a3d57a066af91d13d6d77dcc69
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
bt-service/services/include/bt-service-mesh-cdb.h
bt-service/services/mesh/bt-service-mesh-cdb.c
bt-service/services/mesh/bt-service-mesh-network.c

index 8868a0c..054c7e7 100644 (file)
@@ -155,6 +155,9 @@ bool _bt_mesh_conf_insert_group_info(_bt_mesh_cdb_t *cfg,
 bool _bt_mesh_conf_delete_group_entry(_bt_mesh_cdb_t *cfg,
                uint16_t addr);
 
+bool _bt_mesh_is_group_subscribed(_bt_mesh_cdb_t *cfg,
+                                       uint16_t addr);
+
 bool _bt_mesh_conf_add_model_config_data(_bt_mesh_cdb_t *cfg, uint16_t unicast,
                        int element_index, uint32_t model_id, uint16_t group_addr);
 
index 43c9a1b..9bb13b0 100644 (file)
@@ -1739,6 +1739,78 @@ bool _bt_mesh_conf_overwrite_model_config_data(_bt_mesh_cdb_t *cfg, uint16_t uni
        return  __bt_mesh_save_configruation_file(cfg);
 }
 
+bool _bt_mesh_is_group_subscribed(_bt_mesh_cdb_t *cfg, uint16_t group_addr)
+{
+       json_object *jnodes;
+       int i, sz = 0;
+
+       if (!cfg || !cfg->jcfg)
+               return false;
+
+       json_object_object_get_ex(cfg->jcfg, "nodes", &jnodes);
+       if (!jnodes || json_object_get_type(jnodes) != json_type_array)
+               return false;
+
+       sz = json_object_array_length(jnodes);
+
+       for (i = 0; i < sz; ++i) {
+               json_object *jnode, *jarray;
+               int ele_cnt;
+               int j;
+
+               jnode = json_object_array_get_idx(jnodes, i);
+               if (!jnode)
+                       continue;
+
+               if (!json_object_object_get_ex(jnode, "elements", &jarray))
+                       return false;
+
+               if (!jarray || json_object_get_type(jarray) != json_type_array)
+                       return false;
+
+               ele_cnt = json_object_array_length(jarray);
+
+               for (j = 0; j < ele_cnt; ++j) {
+                       json_object *jentry, *jval, *jmods;
+                       int32_t index;
+                       int k, mod_cnt;
+
+                       jentry = json_object_array_get_idx(jarray, j);
+                       if (!json_object_object_get_ex(jentry, "index", &jval))
+                               return false;
+
+                       index = json_object_get_int(jval);
+                       if (index > 0xff)
+                               return false;
+
+                       if (!json_object_object_get_ex(jentry, "models", &jmods))
+                               continue;
+
+                       mod_cnt = json_object_array_length(jmods);
+                       BT_INFO("Mesh: Total Model count in element Index [%d] is [%d]",
+                               index, mod_cnt);
+
+                       for (k = 0; k < mod_cnt; ++k) {
+                               json_object *jmod;
+                               json_object *jgroups, *jgroup;
+
+                               jmod = json_object_array_get_idx(jmods, k);
+
+                               if (!json_object_object_get_ex(jmod, "sub-addr", &jgroups))
+                                       continue;
+
+                               /* Find existing sub-addr group */
+                               jgroup = __mesh_get_sub_group(jgroups, group_addr);
+                               if (jgroup) {
+                                       BT_DBG("sub-addr present in list");
+                                       return true;
+                               }
+                       }
+               }
+       }
+       return false;
+}
+
 bool _bt_mesh_conf_set_network_friendly_name(_bt_mesh_cdb_t *cfg,
                const char *network_name)
 {
index e68ccce..800a26a 100644 (file)
@@ -1697,6 +1697,12 @@ int _bt_mesh_network_remove_group(const char *app_cred,
        }
        grp = (_bt_mesh_group_t*)l1->data;
 
+       /* Do not delete group if a model is subscribed to this */
+       if (_bt_mesh_is_group_subscribed(cdb_cfg, req->group_addr)) {
+               BT_ERR("Mesh: Failed to remove Group: [0x%2.2x]", req->group_addr);
+               return BLUETOOTH_ERROR_AUTHENTICATION_REJECTED;
+       }
+
        BT_INFO("Mesh: To be Removed Group: [0x%2.2x]", req->group_addr);
        if (!_bt_mesh_conf_delete_group_entry(cdb_cfg, req->group_addr)) {
                BT_ERR("Mesh: Failed to remove Group: [0x%2.2x]", req->group_addr);