Mesh: Handle mesh application termination event
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-network.c
index eddcc53..15daa0c 100644 (file)
@@ -44,6 +44,7 @@
 
 #include "bt-service-mesh-network.h"
 #include "bt-service-mesh-cdb.h"
+#include "bt-service-mesh-main.h"
 #include "bt-service-mesh-nodes.h"
 #include "bt-service-mesh-keys.h"
 #include "bt-service-mesh-util.h"
@@ -223,6 +224,39 @@ int _bt_mesh_network_remove_node_configuration(
        return BLUETOOTH_ERROR_NONE;
 }
 
+void _bt_check_mesh_app_termination(const char *name)
+{
+       GSList *l;
+       _bt_mesh_cdb_t *cdb_cfg = NULL;
+       const char *app_cred = NULL;
+       BT_INFO("Mesh: App terminated [%s]", name);
+
+       /* TODO: Fetch app cred, when support is added */
+
+       for (l = cdb_list; l != NULL;) {
+               cdb_cfg = (_bt_mesh_cdb_t*)l->data;
+               l = g_slist_next(l);
+
+               if (g_strcmp0(cdb_cfg->owner, name) == 0) {
+
+                       bluetooth_mesh_network_t network;
+                       memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
+
+                       _bt_mesh_util_convert_hex_to_string((uint8_t *) cdb_cfg->uuid,
+                                       16, network.uuid, sizeof(network.uuid));
+
+                       BT_INFO("Mesh: Got Network for unloading: UUID [%s]",
+                                       network.uuid);
+
+                       if (BLUETOOTH_ERROR_NONE != _bt_mesh_network_unload(app_cred, name, &network))
+                               BT_ERR("Mesh: Network unloading failed!!");
+                       else
+                               BT_INFO("Mesh: Network unloading Success!!");
+               }
+       }
+       _bt_mesh_handle_app_termination(name);
+}
+
 void _bt_mesh_network_unload_net_configuration(_bt_mesh_cdb_t *cdb_cfg)
 {
        BT_INFO("Mesh: Unload Network Configuration");
@@ -352,7 +386,7 @@ int _bt_mesh_network_create_cdb(int result,
        BT_INFO("Mesh: CDB File path[%s]", file_path);
        BT_INFO("Mesh: CDB App Cred[%s]", app_creds);
        cdb_cfg = _bt_mesh_conf_database_create(file_path, uuid,
-                       token, network, app_creds);
+                       token, network, sender, app_creds);
 
        /* Cleanup */
        g_free(dir_path);