Handle GATT client app's termination
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / gatt / bt-service-gatt.c
index 9cba6a0..d9e080b 100644 (file)
@@ -238,6 +238,10 @@ static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att
                bluetooth_gatt_server_indication_params_t *param);
 
 static void __bt_remove_all_service_handles(int instance_id);
+static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
+
+static int __bt_do_unregister_gatt_instance(int instance_id);
+static void __bt_service_reset_gatt_data(void);
 
 #ifdef TIZEN_GATT_CLIENT
 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
@@ -311,20 +315,31 @@ void _bt_check_adv_app_termination(const char *name)
                if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
                        BT_DBG("Match found, name: %s", name);
 
-                       /* Unregister all service handles with stack */
-                       __bt_remove_all_service_handles(app->instance_id);
-
-                       /* If Advertising is enabled, stop it */
-                       if (app->adv_handle > 0) {
-                               BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
-                               /* Disable adv if running */
-                               BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
-                                               app->adv_handle, name);
-                               _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
-                       }
+                       /* TODO 1: If App has both GATT client / server role? */
+                       /* TODO 2: Need to manage app info as list, not array.
+                                  This loop always run for MAX count if any apps are terminated.
+                        */
 
                        /* Save instances of all apps that need to be unregistered */
-                       apps[app->instance_id] = 1;
+                       if (app->instance_id != -1) {
+                               /* GATT server */
+                               /* Unregister all service handles with stack */
+                               __bt_remove_all_service_handles(app->instance_id);
+
+                               /* If Advertising is enabled, stop it */
+                               if (app->adv_handle > 0) {
+                                       BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
+                                       /* Disable adv if running */
+                                       BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
+                                                       app->adv_handle, name);
+                                       _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
+                               }
+
+                               apps[app->instance_id] = 1;  /* App holds a GATT client Instance */
+                       } else if (app->client_id != -1) {
+                               /* GATT client */
+                               apps[app->client_id] = 1;         /* App holds a GATT server Instance */
+                       }
                }
        }
 
@@ -332,8 +347,8 @@ void _bt_check_adv_app_termination(const char *name)
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
                if (apps[k] == 1) {
                        BT_INFO("Unregister app[%d]", k);
-                       /* Unregister server instance */
-                       __bt_unregister_gatt_server_instance(k);
+                       /* Unregister client or server instance */
+                       __bt_do_unregister_gatt_instance(k);
                }
        }
 }
@@ -424,6 +439,8 @@ static void __bt_register_default_gatt_client()
 
 int _bt_gatt_init(void)
 {
+       const char *stack_name = NULL;
+
        BT_DBG("+");
 
        if (OAL_STATUS_SUCCESS != gatt_enable()) {
@@ -433,15 +450,21 @@ int _bt_gatt_init(void)
 
        /* Register gatt event handler */
        _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
-#ifdef TIZEN_GATT_CLIENT
-       /*In bluedroid product, defacult gatt client is handled differently*/
-       __bt_register_default_gatt_client();
-#endif
+
+       __bt_service_reset_gatt_data();
+
+       stack_name = oal_get_stack_name();
+
+       if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
+               /*In the platform, defacult gatt client should be registered */
+               __bt_register_default_gatt_client();
+       }
+
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
 }
 
-static void __bt_service_reset_gatt_data()
+static void __bt_service_reset_gatt_data(void)
 {
        int k;
 
@@ -459,6 +482,11 @@ static void __bt_service_reset_gatt_data()
                        memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
                        numapps[k].adv_data_len = 0;
                        numapps[k].scan_rsp_len = 0;
+
+                       /* GATT client */
+                       numapps[k].client_id = -1;
+                       memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
+                       numapps[k].is_watcher_enabled = FALSE;
        }
 }
 
@@ -547,7 +575,7 @@ char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
 {
        int k;
-       BT_DBG("+");
+       BT_DBG("");
        bt_service_app_info_t *info = NULL;
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
                info = &numapps[k];
@@ -557,13 +585,12 @@ void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instan
                        break;
                }
        }
-       BT_DBG("-");
 }
 
 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
 {
        int k;
-       BT_DBG("+");
+       BT_DBG("");
        bt_service_app_info_t *info = NULL;
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
                info = &numapps[k];
@@ -573,13 +600,11 @@ void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int in
                        break;
                }
        }
-       BT_DBG("-");
 }
 
 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
 {
        int k;
-       BT_DBG("+");
        bt_service_app_info_t *info = NULL;
 
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
@@ -596,7 +621,7 @@ void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int
 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
 {
        int k;
-       BT_DBG("+");
+       BT_DBG("");
        bt_service_app_info_t *info = NULL;
 
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
@@ -610,6 +635,55 @@ void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len,
        }
 }
 
+static int __bt_do_unregister_gatt_instance(int instance_id)
+{
+       int ret = OAL_STATUS_SUCCESS;
+       int k;
+
+       BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
+
+       /* Reset data: instance_id parameter could be either for GATT Server or for GATT client  */
+       for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+               if (numapps[k].instance_id == instance_id) {
+                       BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
+                       numapps[k].is_initialized = FALSE;
+                       numapps[k].instance_id = -1;
+                       numapps[k].adv_handle = 0;
+                       numapps[k].adv_instance = -1;
+                       memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
+                       memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
+                       memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
+                       memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
+                       numapps[k].adv_data_len = 0;
+                       numapps[k].scan_rsp_len = 0;
+
+                       /* Its a GATT Server Instance */
+                       ret = gatts_unregister(instance_id);
+                       if (ret != OAL_STATUS_SUCCESS) {
+                               BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
+                               return BLUETOOTH_ERROR_INTERNAL;
+                       }
+                       break;
+               } else if (numapps[k].client_id == instance_id) {
+                       BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
+                       numapps[k].client_id = -1;
+                       numapps[k].is_initialized = FALSE;
+                       memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
+                       memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
+                       memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
+
+                       /* Its a GATT Client Instance */
+                       ret = gattc_deregister(instance_id);
+                       if (ret != OAL_STATUS_SUCCESS) {
+                               BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
+                               return BLUETOOTH_ERROR_INTERNAL;
+                       }
+                       break;
+               }
+       }
+       return BLUETOOTH_ERROR_NONE;
+}
+
 static int __bt_unregister_gatt_server_instance(int server_instance)
 {
        int ret = OAL_STATUS_SUCCESS;
@@ -803,7 +877,6 @@ static void __bt_gatt_handle_pending_request_info(int result,
        GArray *out_param;
        invocation_info_t *req_info = NULL;
        ret_if(data == NULL);
-       BT_DBG("+");
 
        for (l = _bt_get_invocation_list(); l != NULL; ) {
                req_info = l->data;
@@ -1340,7 +1413,7 @@ struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
                        continue;
 
                if (!g_strcmp0(info->addr, address)) {
-                       BT_INFO("Remote GATT client found addr[%s]", info->addr);
+                       BT_DBG("Remote GATT client found addr[%s]", info->addr);
                        return info;
                }
        }
@@ -1376,7 +1449,7 @@ struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
                        continue;
 
                if (!g_strcmp0(info->addr, address)) {
-                       BT_INFO("Remote GATT Server found addr[%s]", info->addr);
+                       BT_DBG("Remote GATT Server found addr[%s]", info->addr);
                        return info;
                }
        }
@@ -2167,7 +2240,6 @@ static void __bt_gatt_event_handler(int event_type, gpointer event_data)
        default:
                break;
        }
-       BT_DBG("-");
 }
 
 int _bt_gatt_server_add_service(char *sender, int service_type,
@@ -2558,13 +2630,13 @@ int _bt_get_att_mtu(bluetooth_device_address_t *address,
 
 #ifdef TIZEN_GATT_CLIENT
 /* GATT Client utility static functions */
-static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
+static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
 {
        GSList *l;
        bt_gatt_service_info_list_t *info = NULL;
 
        for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
-               info = (bt_gatt_service_info_list_t*)l->data;
+               info = (bt_gatt_service_info_list_t *)l->data;
                if (info == NULL)
                        continue;
 
@@ -2575,14 +2647,14 @@ static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
        return NULL;
 }
 
-static bt_gatt_service_info_t__bt_find_matching_service(
+static bt_gatt_service_info_t *__bt_find_matching_service(
                bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
 {
        GSList *l;
        bt_gatt_service_info_t *info = NULL;
 
        for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
-               info = (bt_gatt_service_info_t*)l->data;
+               info = (bt_gatt_service_info_t *)l->data;
                if (info == NULL)
                        continue;
 
@@ -2595,14 +2667,14 @@ static bt_gatt_service_info_t* __bt_find_matching_service(
        return NULL;
 }
 
-static bt_gatt_char_info_t__bt_find_matching_charc(
+static bt_gatt_char_info_t *__bt_find_matching_charc(
                bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
 {
        GSList *l;
        bt_gatt_char_info_t *info = NULL;
 
        for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
-               info = (bt_gatt_char_info_t*)l->data;
+               info = (bt_gatt_char_info_t *)l->data;
                if (info == NULL)
                        continue;
 
@@ -2615,14 +2687,14 @@ static bt_gatt_char_info_t* __bt_find_matching_charc(
        return NULL;
 }
 
-static bt_gatt_descriptor_info_t__bt_find_matching_desc(
+static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
                bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
 {
        GSList *l;
        bt_gatt_descriptor_info_t *info = NULL;
 
        for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
-               info = (bt_gatt_descriptor_info_t*)l->data;
+               info = (bt_gatt_descriptor_info_t *)l->data;
                if (info == NULL)
                        continue;
 
@@ -2732,7 +2804,7 @@ static void __bt_build_service_browse_info(int conn_id,
 
        service_uuid_t uuid;
        struct gatt_server_info_t *conn_info = NULL;
-       int count = 1;
+       int count = 0;
        char uuid_string[BLUETOOTH_UUID_STRING_MAX];
 
        conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
@@ -2743,7 +2815,6 @@ static void __bt_build_service_browse_info(int conn_id,
        _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
 
        svc_info_list = __bt_get_service_info_list(conn_id);
-
        if (!svc_info_list)
                return;
 
@@ -2788,8 +2859,6 @@ static void __bt_build_char_browse_info(int conn_id,
        int count = 0;
        char uuid_string[BLUETOOTH_UUID_STRING_MAX];
 
-       BT_INFO("+");
-
        conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
        if (!conn_info)
                return;
@@ -2805,7 +2874,6 @@ static void __bt_build_char_browse_info(int conn_id,
        }
 
        info->count = g_slist_length(svc_info->chars);
-       BT_INFO("Total count of Characteristics [%d]", info->count);
 
        for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
                char_info = (bt_gatt_char_info_t*)l->data;
@@ -2828,7 +2896,7 @@ static void __bt_build_char_browse_info(int conn_id,
                /* Increment count of services browsed */
                count++;
        }
-       BT_INFO("Total characteristics browsed [%d]", count);
+       BT_DBG("Total characteristics browsed [%d]", count);
 }
 
 static void __bt_build_descriptor_browse_info(int conn_id,
@@ -2857,7 +2925,6 @@ static void __bt_build_descriptor_browse_info(int conn_id,
        info->char_props_map = char_info->props;
 
        info->count = g_slist_length(char_info->descs);
-       BT_INFO("Total count of Descriptors [%d]", info->count);
 
        if (!char_info->descs) {
                BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
@@ -2887,20 +2954,50 @@ static void __bt_build_descriptor_browse_info(int conn_id,
        BT_INFO("Total descriptors browsed [%d]", count);
 }
 
-
-static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
+static void __bt_free_service_info(bt_gatt_service_info_t *svc)
 {
-       GSList *l;
-       GSList *ll;
-       GSList *lll;
-       GSList *llll;
-
-       bt_gatt_service_info_list_t * svc_info_list = NULL;
-       bt_gatt_service_info_t *svc = NULL;
+       GSList *ll, *lll, *llll;
        bt_gatt_char_info_t *chr = NULL;
        bt_gatt_descriptor_info_t *desc = NULL;
        bt_gatt_included_service_info_t *incl_svc = NULL;
 
+       BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
+       /* Delete all chars and its descriptors */
+       for (ll = svc->chars; ll != NULL; ) {
+               chr = (bt_gatt_char_info_t *)ll->data;
+               ll = g_slist_next(ll);
+               if (chr == NULL)
+                       continue;
+
+               for (lll = chr->descs; lll != NULL; ) {
+                       desc = (bt_gatt_descriptor_info_t *)lll->data;
+                       lll = g_slist_next(lll);
+                       if (desc == NULL)
+                               continue;
+                       chr->descs = g_slist_remove(chr->descs, desc);
+                       g_free(desc);
+               }
+               svc->chars = g_slist_remove(svc->chars, chr);
+               g_free(chr);
+       }
+
+       /* Delete all included services */
+       for (llll = svc->included_svcs; llll != NULL; ) {
+               incl_svc = (bt_gatt_included_service_info_t *)llll->data;
+               llll = g_slist_next(llll);
+               if (incl_svc == NULL)
+                       continue;
+               svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
+               g_free(incl_svc);
+       }
+}
+
+static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
+{
+       bt_gatt_service_info_list_t *svc_info_list = NULL;
+       bt_gatt_service_info_t *svc = NULL;
+       GSList *l;
+
        svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
        if (!svc_info_list) {
                BT_INFO("Could not find Svc Info list for the connection ID [%d]",
@@ -2909,43 +3006,13 @@ static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
        }
 
        BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
-       for (l = svc_info_list->services; l;) {
-               svc = (bt_gatt_service_info_t*)l->data;
+       for (l = svc_info_list->services; l != NULL; ) {
+               svc = (bt_gatt_service_info_t *)l->data;
                l = g_slist_next(l);
                if (svc == NULL)
                        continue;
 
-               BT_DBG("Service info Is Prim[%d] Inst ID [%d]",
-                               svc->is_primary, svc->inst_id);
-               /* Delete all chars and its descriptors */
-               for (ll = svc->chars; ll;) {
-                       chr = (bt_gatt_char_info_t*)ll->data;
-                       ll = g_slist_next(ll);
-                       if (chr == NULL)
-                               continue;
-
-                       for (lll = chr->descs; lll;) {
-                               desc = (bt_gatt_descriptor_info_t *)lll->data;
-                               lll = g_slist_next(lll);
-                               if (desc == NULL)
-                                       continue;
-                               chr->descs = g_slist_remove(chr->descs, desc);
-                               g_free(desc);
-                       }
-                       svc->chars = g_slist_remove(svc->chars, chr);
-                       g_free(chr);
-               }
-
-               /* Delete all included services */
-               for (llll = svc->included_svcs; llll;) {
-                       incl_svc = (bt_gatt_included_service_info_t*)llll->data;
-                       llll = g_slist_next(llll);
-                       if (incl_svc == NULL)
-                               continue;
-
-                       svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
-                       g_free(incl_svc);
-               }
+               __bt_free_service_info(svc);
                svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
                g_free(svc);
        }
@@ -2964,7 +3031,7 @@ int _bt_register_gatt_client_instance(const char *sender,
        oal_uuid_t uuid;
 
        /* App should ensure that it should not send */
-       BT_INFO("###Check on which instance GATT Client instance can be initialized....");
+       BT_INFO("### Check on which instance GATT Client instance can be initialized....");
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
                if (numapps[k].is_initialized == 1) {
                        BT_INFO("Instance ID [%d] is already in use..Check next slot",
@@ -3030,11 +3097,8 @@ static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
 
        /* Platform GATT client framwork does not use Default GATT client instance
           This GATT client instance is never deregistred in the lifetime of bt-service */
-       BT_INFO("Default UUID [%s] current registered uuid [%s]",
-                       DEFAULT_GATT_CLIENT_UUID, uuid_string);
        if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
-               BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
-                               uuid_string, data->client_if);
+               BT_INFO("Default client Instance Registered");
                gatt_default_client = data->client_if;
                g_free(uuid_string);
                return;
@@ -3044,8 +3108,7 @@ static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
                info = &numapps[k];
 
                if (g_strcmp0(info->uuid, uuid_string) == 0) {
-                       BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
-                       BT_INFO("Slot [%d] occupied", k);
+                       BT_INFO("Found GATT client..  sender [%s]  Slot [%d] occupied", info->sender, k);
                        info->is_initialized = TRUE;
                        info->client_id = data->client_if;
                        __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
@@ -3196,26 +3259,20 @@ static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
 static void __bt_handle_client_service_search_result(
                event_gattc_service_result_t *event_data)
 {
-       BT_INFO("received the gatt service search result");
        /* Pre: status is never fail from OAL */
 
        /* Find service list from address */
        bt_gatt_service_info_list_t *svc_info_list;
        bt_gatt_service_info_t *svc_info;
-       BT_INFO("Search Result: status [%d] conn_id [%d]",
-                       event_data->conn_status.status,
-                       event_data->conn_status.conn_id);
 
        svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
        if (!svc_info_list) {
-               BT_INFO("Service info list not present for this connection ID, means first time browse");
+               BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
                /* Means for this conn_id, no services are ever browsed, first time,
                   create service info list for this conn_id */
                svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
                svc_info_list->conn_id = event_data->conn_status.conn_id;
                list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
-       } else {
-               BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
        }
 
        /* send list and current service's uuid and instance id to find it  */
@@ -3226,9 +3283,8 @@ static void __bt_handle_client_service_search_result(
           in remote GATT device, update uuid info in svc info list structure, to be used when
           search is completed */
        if (!svc_info) {
-               BT_INFO("Service Not found in svc info list for this connection ID");
                if (svc_info_list->info.is_changed) {
-                       BT_INFO("Service Changed indication already found for this connection ID");
+                       BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
                        memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
                }
                /* Create and add new service in service list */
@@ -3237,9 +3293,8 @@ static void __bt_handle_client_service_search_result(
                svc_info->inst_id = event_data->srvc_id.id.inst_id;
                svc_info->is_primary = event_data->srvc_id.is_prmry;
                svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
-               BT_INFO("Service created and added in Svc info listf or this connection ID");
+               BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
        } else {
-               BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
                /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
                svc_info->is_removed = 0;
        }
@@ -3248,7 +3303,6 @@ static void __bt_handle_client_service_search_result(
 static void __bt_handle_client_service_search_completed(
                event_gattc_conn_status_t *event_data)
 {
-       BT_INFO("+");
        struct gatt_server_info_t *conn_info = NULL;
        bt_gatt_service_info_list_t *svc_info_list;
        bt_gatt_service_info_t *svc_info;
@@ -3332,9 +3386,6 @@ static void __bt_handle_client_characteristic_search_result(
        bt_gatt_char_info_t *char_info;
        bt_char_browse_info_t browse_info;
 
-       BT_INFO("Characteristic search result status [%d]",
-                       event_data->conn_status.status);
-
        memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
 
        /* If success */
@@ -3356,12 +3407,10 @@ static void __bt_handle_client_characteristic_search_result(
 
                /* Find Matching char from service info in event */
                char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
-               if (char_info == NULL)
-                       BT_ERR("char_info is NULL");
 
                /* If not found, then add new characteristic and return */
                if (!char_info) {
-                       BT_INFO(" add new characteristic");
+                       BT_DBG("Add new characteristic");
                        char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
                        memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
 
@@ -3370,7 +3419,7 @@ static void __bt_handle_client_characteristic_search_result(
                        svc_info->chars = g_slist_append(svc_info->chars, char_info);
                } else {
                        /* If found, then return */
-                       BT_INFO("update char property as Characteristic browsed is already present");
+                       BT_DBG("update char property as Characteristic browsed is already present");
                        char_info->props |= event_data->char_prop;
                }
        } else {
@@ -3378,9 +3427,17 @@ static void __bt_handle_client_characteristic_search_result(
                /* Find char list from service in event */
                /* Find service list from address */
                svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+               if (svc_info_list == NULL) {
+                       BT_ERR("svc_info_list is NULL");
+                       return;
+               }
 
                /* Find service info from service in event */
                svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+               if (svc_info == NULL) {
+                       BT_ERR("svc_info is NULL");
+                       return;
+               }
 
                /* Build char list from service in event */
                __bt_build_char_browse_info(event_data->conn_status.conn_id,
@@ -3412,10 +3469,24 @@ static void __bt_handle_client_descriptor_search_result(
        if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
                /* Find service list from address */
                svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+               if (svc_info_list == NULL) {
+                       BT_ERR("svc_info_list is NULL");
+                       return;
+               }
+
                svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+               if (svc_info == NULL) {
+                       BT_ERR("svc_info is NULL");
+                       return;
+               }
+
                char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
-               desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
+               if (char_info == NULL) {
+                       BT_ERR("char_info is NULL");
+                       return;
+               }
 
+               desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
                /* If not found, add new descriptor and return */
                if (!desc_info) {
                        desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
@@ -3434,12 +3505,24 @@ static void __bt_handle_client_descriptor_search_result(
                /* Find included service list from service in event */
                /* Create response and return by sending event*/
                svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+               if (svc_info_list == NULL) {
+                       BT_ERR("svc_info_list is NULL");
+                       return;
+               }
 
                /* Find service info from service in event */
                svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+               if (svc_info == NULL) {
+                       BT_ERR("svc_info is NULL");
+                       return;
+               }
 
                /* Find char info from char in event */
                char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
+               if (char_info == NULL) {
+                       BT_ERR("char_info is NULL");
+                       return;
+               }
 
                /* Build descriptor list from char in event */
                __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
@@ -4068,20 +4151,54 @@ static void __bt_handle_client_notification_data(event_gattc_notify_data *event_
 
 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
 {
+       bt_gatt_service_info_list_t *svc_info_list;
+       GSList *l;
+       bt_gatt_service_info_t *svc_info;
        GVariant *param = NULL;
        char *address_str = NULL;
        char *uuid_str = NULL;
 
+       svc_info_list = __bt_get_service_info_list(event_data->conn_id);
+       if (svc_info_list == NULL) {
+               BT_ERR("svc_info_list is NULL");
+               return;
+       }
+
+       if (event_data->change_type) {
+               /* Add service UUID in list */
+               svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
+               memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+               svc_info->inst_id = event_data->inst_id;
+               svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
+               svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
+               BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
+       } else {
+               /* Remove service UUID in list */
+               for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
+                       svc_info = (bt_gatt_service_info_t *)l->data;
+                       if (svc_info == NULL)
+                               continue;
+
+                       if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
+                               svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
+                               __bt_free_service_info(svc_info);
+                               break;
+                       }
+               }
+       }
+
        address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
        uuid_str = g_malloc0(BT_UUID_STRING_MAX);
        _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
        _bt_uuid_to_string(&event_data->uuid, uuid_str);
 
-       param = g_variant_new("(iss)", event_data->change_type, address_str, uuid_str);
+       param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
 
        _bt_send_event(BT_GATT_CLIENT_EVENT,
                        BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
                        param);
+       g_free(address_str);
+       g_free(uuid_str);
 }
 
 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
@@ -4089,18 +4206,17 @@ gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address
        char *addr;
        struct gatt_server_info_t *conn_info =  NULL;
        gboolean connected = FALSE;
-       BT_INFO("+");
 
        addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
        _bt_convert_addr_type_to_string(addr,
                        (unsigned char *)&(address->addr));
 
-       BT_INFO("Check Connected or not for [%s]", addr);
+       BT_DBG("Check GATT connection status of [%s]", addr);
        /* Check if device is already in connected list */
        conn_info = _bt_find_remote_gatt_server_info(addr);
 
        if (conn_info) {
-               BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
+               BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
                connected = TRUE;
        } else {
                struct gatt_client_info_t *client_info = NULL;
@@ -4111,7 +4227,7 @@ gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address
                client_info = _bt_find_remote_gatt_client_info(addr);
 
                if (client_info) {
-                       BT_INFO("Remote Client device [%s] is Connected", client_info->addr);
+                       BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
                        connected = TRUE;
                } else {
                        BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
@@ -4136,8 +4252,6 @@ int _bt_connect_le_device(bluetooth_device_address_t *address,
 
        BT_CHECK_PARAMETER(address, return);
 
-       BT_INFO("+");
-
        addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
        _bt_convert_addr_type_to_string(addr, address->addr);
        BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
@@ -4220,6 +4334,10 @@ int _bt_gatt_get_primary_services(char *address)
                return BLUETOOTH_ERROR_NOT_CONNECTED;
        }
 
+       BT_INFO("Remove GATT server info from List..");
+       /* Remove all services from info list_gatt_info */
+       __bt_cleanup_remote_services(conn_info);
+
        /* Send Primary Service Browsing request to stack */
        ret = gattc_search_service(conn_info->connection_id, NULL);
        if (ret != OAL_STATUS_SUCCESS) {
@@ -4238,7 +4356,6 @@ int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
        oal_gatt_srvc_id_t srvc_id;
        int ret = OAL_STATUS_SUCCESS;
        char *addr;
-       BT_INFO("+");
 
        /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
        req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
@@ -4259,7 +4376,7 @@ int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
        /* Check if remote GATT Server is connected or not */
        conn_info = _bt_find_remote_gatt_server_info(addr);
        if (conn_info) {
-               BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+               BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
                                conn_info->addr, conn_info->connection_id);
        } else {
                BT_ERR("GATT Server is not yet connected..");
@@ -4295,8 +4412,6 @@ int _bt_gatt_get_all_characteristic_properties(
 
        BT_CHECK_PARAMETER(chr, return);
 
-       BT_INFO("+");
-
        /* Check if any app is already browsing descriptors of the same char of
           particular service on the same remote GATT Server */
        req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
@@ -4319,7 +4434,7 @@ int _bt_gatt_get_all_characteristic_properties(
        /* Check if remote GATT Server is connected or not */
        conn_info = _bt_find_remote_gatt_server_info(addr);
        if (conn_info) {
-               BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+               BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
                                conn_info->addr, conn_info->connection_id);
        } else {
                BT_ERR("GATT Server is not yet connected..");
@@ -4358,8 +4473,6 @@ int _bt_gatt_read_characteristic_value(
 
        BT_CHECK_PARAMETER(chr, return);
 
-       BT_INFO("+");
-
        /* Check if any app is already Reading characteristic of the same char of
           particular service on the same remote GATT Server */
        req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
@@ -4382,7 +4495,7 @@ int _bt_gatt_read_characteristic_value(
        /* Check if remote GATT Server is connected or not */
        conn_info = _bt_find_remote_gatt_server_info(addr);
        if (conn_info) {
-               BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+               BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
                                conn_info->addr, conn_info->connection_id);
        } else {
                BT_ERR("GATT Server is not yet connected..");
@@ -4422,8 +4535,6 @@ int _bt_gatt_read_descriptor_value(
 
        BT_CHECK_PARAMETER(desc, return);
 
-       BT_INFO("+");
-
        /* Check if any app is already Reading descriptors of the same char of
           particular service on the same remote GATT Server */
        req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
@@ -4448,7 +4559,7 @@ int _bt_gatt_read_descriptor_value(
        /* Check if remote GATT Server is connected or not */
        conn_info = _bt_find_remote_gatt_server_info(addr);
        if (conn_info) {
-               BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+               BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
                                conn_info->addr, conn_info->connection_id);
        } else {
                BT_ERR("GATT Server is not yet connected..");
@@ -4489,15 +4600,13 @@ int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd
 
        BT_CHECK_PARAMETER(chr, return);
 
-       BT_INFO("+");
-
        addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
        _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
 
        /* Check if remote GATT Server is connected or not */
        conn_info = _bt_find_remote_gatt_server_info(addr);
        if (conn_info) {
-               BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+               BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
                                conn_info->addr, conn_info->connection_id);
        } else {
                BT_ERR("GATT Server is not yet connected..");
@@ -4536,15 +4645,13 @@ int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd,
 
        BT_CHECK_PARAMETER(chr, return);
 
-       BT_INFO("+");
-
        addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
        _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
 
        /* Check if remote GATT Server is connected or not */
        conn_info = _bt_find_remote_gatt_server_info(addr);
        if (conn_info) {
-               BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+               BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
                                conn_info->addr, conn_info->connection_id);
        } else {
                BT_ERR("GATT Server is not yet connected..");
@@ -4861,8 +4968,8 @@ int _bt_gatt_watch_service_changed_indication(const char *sender,
        int k;
        bt_service_app_info_t *info = NULL;
 
-       BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
-                       is_enabled, sender);
+       BT_INFO("%s Servic changed Indication watcher for app [%s]",
+                       is_enabled ? "Enable":"Disable", sender);
 
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
                info = &numapps[k];
@@ -4870,7 +4977,7 @@ int _bt_gatt_watch_service_changed_indication(const char *sender,
                if (g_strcmp0(sender, info->sender) == 0 &&
                                memcmp(info->address.addr, address->addr,
                                        sizeof(bluetooth_device_address_t)) == 0) {
-                       BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
+                       BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
                        info->is_watcher_enabled = is_enabled;
                }
        }