Add service removed event handling logic
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-client.c
index 4fced83..b3214ac 100644 (file)
@@ -123,6 +123,7 @@ typedef struct {
 
 typedef struct {
        bt_bdaddr_t bd_addr;    /*remote server address*/
+       int conn_id;
        int inst_id;            /*server instance id*/
        GSList *gatt_list_services;
 } hal_gattc_server_info_t;
@@ -310,6 +311,7 @@ bt_status_t btif_gattc_add_connection_info(const bt_bdaddr_t *bd_addr, int conn_
        if (server_info == NULL) {
                server_info = g_malloc0(sizeof(hal_gattc_server_info_t));
                memcpy(server_info->bd_addr.address, bd_addr->address, BT_HAL_ADDRESS_LENGTH_MAX);
+               server_info->conn_id = conn_id;
                server_info->inst_id = server_inst_id;
                hal_gattc_server_info_list = g_slist_append(hal_gattc_server_info_list, server_info);
                DBG("Added server connection info in list");
@@ -775,6 +777,8 @@ static bt_status_t _gattc_client_search_service(int conn_id)
                return BT_STATUS_FAIL;
        }
 
+#if 0
+       // TODO: This logic is not able to handle in case service-added event is coming unexpectedly.
        /* Check the service info is stored */
        if (g_slist_length(conn_info->gatt_list_services) > 0) {
                GSList *l = NULL;
@@ -794,6 +798,7 @@ static bt_status_t _gattc_client_search_service(int conn_id)
        } else {
                DBG("No stored service, request to bluez");
        }
+#endif
 
        _bt_hal_convert_addr_type_to_string(device_address,
                        (unsigned char *)conn_info->bd_addr.address);
@@ -3487,6 +3492,7 @@ static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
        /*add gatt server connection info*/
        gatt_conn_info = g_malloc0(sizeof(hal_gattc_server_info_t));
        memcpy(gatt_conn_info->bd_addr.address, gattc_data->bd_addr.address, BT_HAL_ADDRESS_LENGTH_MAX);
+       gatt_conn_info->conn_id = gattc_data->conn_id;
        gatt_conn_info->inst_id = gattc_data->inst_id;
        hal_gattc_server_info_list = g_slist_append(hal_gattc_server_info_list, gatt_conn_info);
 
@@ -3908,7 +3914,6 @@ void _bt_hal_handle_gattc_service_changed_event(gboolean is_added, const char *p
 
        _bt_hal_convert_device_path_to_address(path, address);
        _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
-       ev.change_type = is_added;
        server_info = __bt_find_gatt_conn_info((bt_bdaddr_t *)ev.bdaddr);
        if (server_info == NULL) {
                ERR("service_info is NULL");
@@ -3916,11 +3921,16 @@ void _bt_hal_handle_gattc_service_changed_event(gboolean is_added, const char *p
        }
 
        if (is_added) {
+               /* Get service UUID from path */
                __bt_hal_gattc_get_uuid_from_path(path, &uuid_str);
-               DBG("GATT Service(%s) Added", uuid_str);
-               /* Set UUID by using path */
-               _bt_hal_convert_uuid_string_to_type(ev.uuid, uuid_str);
-               g_free(uuid_str);
+               if (uuid_str) {
+                       DBG("conn_id(%d) GATT Service(%s) Added", server_info->conn_id, uuid_str);
+                       _bt_hal_convert_uuid_string_to_type(ev.uuid, uuid_str);
+                       g_free(uuid_str);
+               } else {
+                       ERR("uuid_str is NULL");
+                       return;
+               }
 
                /* Create new service and append into the list */
                __hal_gattc_get_service_info(server_info, path);
@@ -3934,10 +3944,8 @@ void _bt_hal_handle_gattc_service_changed_event(gboolean is_added, const char *p
                        if (g_strcmp0(service->svc_path, path) == 0) {
                                memcpy(ev.uuid, service->svc_uuid.uu, sizeof(bt_uuid_t));
                                uuid_str = g_malloc0(BT_HAL_UUID_STRING_LEN);
-                               /* Set UUID from stored service info */
                                _bt_hal_convert_uuid_type_to_string(uuid_str, ev.uuid);
-                               DBG("GATT Service(%s) Removed", uuid_str);
-                               g_free(uuid_str);
+                               DBG("conn_id(%d) GATT Service(%s) Removed", server_info->conn_id, uuid_str);
 
                                /* Remove service info in list */
                                server_info->gatt_list_services = g_slist_remove(server_info->gatt_list_services, service);
@@ -3945,9 +3953,17 @@ void _bt_hal_handle_gattc_service_changed_event(gboolean is_added, const char *p
                                break;
                        }
                }
+
+               if (uuid_str) {
+                       g_free(uuid_str);
+               } else {
+                       ERR("uuid_str is NULL");
+                       return;
+               }
        }
 
        /* Send GATT Client service changed event */
+       ev.change_type = is_added;
+       ev.conn_id = server_info->conn_id;
        event_cb(HAL_EV_GATT_CLIENT_SERVICE_CHANGED, (void *)&ev, sizeof(ev));
 }
-