Fix the coverity issues
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-cdb.c
index 43c9a1b..6771eda 100644 (file)
@@ -1171,7 +1171,7 @@ bool _bt_mesh_conf_get_element_count(_bt_mesh_cdb_t *cfg,
        }
 
        sz = json_object_array_length(jarray);
-       if (sz == 0 ){
+       if (sz == 0{
                BT_ERR("Mesh: Element array length is 0");
                return false;
        }
@@ -1638,11 +1638,10 @@ bool _bt_mesh_conf_delete_model_config_data(_bt_mesh_cdb_t *cfg, uint16_t unicas
                return false;
 
        /* Delete group address */
-       if (!json_object_object_get_ex(jmodel, "sub-addr", &jgroups)) {
+       if (!json_object_object_get_ex(jmodel, "sub-addr", &jgroups))
                return false;
-       }
 
-       if(!__mesh_del_sub_group(jgroups, group_addr))
+       if (!__mesh_del_sub_group(jgroups, group_addr))
                return false;
 
        /* Save */
@@ -1669,17 +1668,15 @@ bool _bt_mesh_conf_delete_all_model_config_data(_bt_mesh_cdb_t *cfg, uint16_t un
        if (!jmodel)
                return false;
 
-       /* Delete all roup address */
-       if (!json_object_object_get_ex(jmodel, "sub-addr", &jgroups)) {
+       /* Delete all group address */
+       if (!json_object_object_get_ex(jmodel, "sub-addr", &jgroups))
                return false;
-       }
 
        sz = json_object_array_length(jgroups);
        BT_INFO("Mesh: Total sub-addr entry present is [%d]", sz);
 
-       for (int i = sz - 1; i >= 0 ; --i) {
+       for (int i = sz - 1; i >= 0 ; --i)
                json_object_array_del_idx(jgroups, i, 1);
-       }
 
        /* Save */
        return  __bt_mesh_save_configruation_file(cfg);
@@ -1739,6 +1736,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)
 {
@@ -2031,6 +2100,7 @@ _bt_mesh_cdb_t* _bt_mesh_conf_load(const char *file_name,
        sz = read(fd, str, st.st_size);
        if (sz != st.st_size) {
                BT_ERR("Mesh: Failed to read configuration file [%s]", file_name);
+               close(fd);
                g_free(str);
                return NULL;
        }