Mesh: Handle mesh application termination event
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-network.c
index 353e949..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);
@@ -408,8 +442,9 @@ int _bt_mesh_network_unload(const char *app_cred,
                const char *sender, bluetooth_mesh_network_t *network)
 {
        GSList *l;
-       uint8_t net_uuid[16];
+       oal_uuid_t net_uuid;
        _bt_mesh_cdb_t *cdb_cfg;
+       int ret = OAL_STATUS_SUCCESS;
 
        BT_INFO("Mesh: Unload Network Configuration");
        /* If Scanning is going on */
@@ -420,10 +455,19 @@ int _bt_mesh_network_unload(const char *app_cred,
        }
 
        _bt_mesh_util_convert_string_to_hex(network->uuid,
-                       strlen(network->uuid), net_uuid, 16);
+                       strlen(network->uuid), net_uuid.uuid, 16);
+
+       /* Release Mesh Network */
+       ret = mesh_network_release(&net_uuid);
+       if (ret != OAL_STATUS_SUCCESS) {
+               BT_ERR("ret: %d", ret);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       BT_INFO("Mesh: Network released");
 
        /* Find CDB */
-       l = g_slist_find_custom(cdb_list, net_uuid,
+       l = g_slist_find_custom(cdb_list, net_uuid.uuid,
                        __mesh_compare_app_network_uuid);
        if (!l) {
                BT_ERR("Mesh: Could not find Network Entry: unexpected!!");