Return error code properly
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / gatt / bt-service-gatt.c
index 0c5dd45..c6a1315 100644 (file)
@@ -1455,6 +1455,10 @@ static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
           in this event */
        _bt_convert_addr_type_to_string(address,
                        (unsigned char *)dev_addr.addr);
+
+       if (event->status != OAL_STATUS_SUCCESS)
+               result = BLUETOOTH_ERROR_INTERNAL;
+
        __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
                        address, BT_ADDRESS_STRING_SIZE);
 
@@ -1504,6 +1508,9 @@ static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *even
        _bt_convert_addr_type_to_string(address,
                        (unsigned char *)dev_addr.addr);
 
+       if (event->status != OAL_STATUS_SUCCESS)
+               result = BLUETOOTH_ERROR_INTERNAL;
+
        if (NULL ==  _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
                if (NULL !=  _bt_get_request_info_data(BT_CONNECT_LE, address)) {
                        result = BLUETOOTH_ERROR_INTERNAL;
@@ -1698,10 +1705,18 @@ static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_ac
        GVariant *param = NULL;
        int result = BLUETOOTH_ERROR_NONE;
        struct gatt_server_req_info *req_info = NULL;
+       bluetooth_device_address_t dev_addr;
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
 
        BT_INFO("GATT Server ACQUIRE  Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
        BT_INFO("GATT Server ACQUIRE Notify  Req Transaction ID:[%d]", event->attr_trans.trans_id);
        BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
+       BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
+
+       memcpy(dev_addr.addr, event->address.addr, 6);
+       _bt_convert_addr_type_to_string(address,
+                       (unsigned char *)dev_addr.addr);
+       BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
 
        req_info = g_new0(struct gatt_server_req_info, 1);
        req_info->request_id = event->attr_trans.trans_id;
@@ -1710,11 +1725,12 @@ static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_ac
        req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
        gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
 
-       param = g_variant_new("(iiiiii)", result,
+       param = g_variant_new("(iiiiiis)", result,
                        event->attr_trans.conn_id,
                        event->attr_trans.trans_id,
                        event->attr_trans.attr_handle,
-                       event->mtu, event->attr_trans.offset);
+                       event->mtu, event->attr_trans.offset,
+                       address);
 
        BT_INFO("GATT Server ACQUIRE Notify  Req Attribute : ");
 
@@ -2464,7 +2480,7 @@ int _bt_get_att_mtu(bluetooth_device_address_t *address,
                BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
                                conn_info->addr, conn_info->connection_id, conn_info->instance_id);
        } else {
-               BT_ERR("GATT Client [%s] is not yet connected..");
+               BT_ERR("GATT Client [%s] is not yet connected..", addr);
                return BLUETOOTH_ERROR_NOT_CONNECTED;
        }
 
@@ -2473,8 +2489,15 @@ int _bt_get_att_mtu(bluetooth_device_address_t *address,
                BT_ERR("ret: %d", ret);
                return BLUETOOTH_ERROR_INTERNAL;
        }
+
        BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
        *mtu = (unsigned int)stack_mtu;
+
+       if (*mtu == 0) {
+               BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
+               return BLUETOOTH_ERROR_NOT_CONNECTED;
+       }
+
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -2682,13 +2705,13 @@ static void __bt_build_service_browse_info(int conn_id,
                memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
                _bt_uuid_to_string(&uuid, uuid_string);
 
-               BT_INFO("Service UUID formed [%s] strlen [%d]", uuid_string, strlen(uuid_string));
+               BT_INFO("Service UUID formed [%s] strlen [%zd]", uuid_string, strlen(uuid_string));
 
                /* Fill UUID of service */
                g_strlcpy(info->uuids[count], uuid_string,
                                BLUETOOTH_UUID_STRING_MAX);
 
-               BT_INFO("Service UUID formed  TO be sent [%s] strlen [%d]",
+               BT_INFO("Service UUID formed  TO be sent [%s] strlen [%zd]",
                                info->uuids[count], strlen(info->uuids[count]));
                /* Fill instance ID of service */
                info->inst_id[count] = svc_info->inst_id;
@@ -3556,7 +3579,7 @@ static void __bt_handle_client_descriptor_read_data(
        if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
                result = BLUETOOTH_ERROR_INTERNAL;
        else {
-               BT_INFO("desc data len:", event_data->data_len);
+               BT_INFO("desc data len:%d", event_data->data_len);
                if (event_data->data_len > 0) {
                        /* DEBUG */
                        for (i = 0; i < event_data->data_len; i++)
@@ -4692,6 +4715,7 @@ int _bt_disconnect_le_device(bluetooth_device_address_t *address,
                int client_id)
 {
        struct gatt_server_info_t *conn_info = NULL;
+       struct gatt_client_info_t *rem_client_conn_info = NULL;
        invocation_info_t *req_info = NULL;
        int ret = OAL_STATUS_SUCCESS;
        char *addr;
@@ -4718,23 +4742,33 @@ int _bt_disconnect_le_device(bluetooth_device_address_t *address,
        }
        /* Check if remote GATT Server is connected or not */
        conn_info = __bt_find_remote_gatt_server_info(addr);
-       if (!conn_info) {
-               BT_ERR("GATT Server is not connected..");
-               g_free(addr);
-               return BLUETOOTH_ERROR_NOT_IN_OPERATION;
-       }
+       if (conn_info) {
+               /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
+               if (client_id == 0) {
+                       BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
+                                       client_id);
+                       BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
+                       client_id = gatt_default_client;
+               }
 
-       /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
-       if (client_id == 0) {
-               BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
-                               client_id);
-               BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
-               client_id = gatt_default_client;
-       }
+               BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
+               ret = gattc_disconnect(client_id, (bt_address_t*)(address),
+                               conn_info->connection_id);
+       } else {
+               /* check if remote client is connected */
+               rem_client_conn_info = __bt_find_remote_gatt_client_info(addr);
 
-       BT_INFO("Disconnect using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
-       ret = gattc_disconnect(client_id, (bt_address_t*)(address),
-                       conn_info->connection_id);
+               if (!rem_client_conn_info || client_id != 0) {
+                       BT_ERR("GATT device is not connected..");
+                       g_free(addr);
+                       return BLUETOOTH_ERROR_NOT_IN_OPERATION;
+               }
+
+               BT_INFO("Disconnect remote gatt client ");
+
+               ret = gatts_disconnect(rem_client_conn_info->instance_id,
+                       (bt_address_t*)(address), rem_client_conn_info->connection_id);
+       }
 
        if (ret != OAL_STATUS_SUCCESS) {
                BT_ERR("ret: %d", ret);