Added the GATT server disconnect 05/185005/7 accepted/tizen/unified/20180919.141920 submit/tizen/20180919.001852
authorAmit Purwar <amit.purwar@samsung.com>
Wed, 25 Jul 2018 06:06:49 +0000 (11:36 +0530)
committerPyun DoHyun <dh79.pyun@samsung.com>
Wed, 19 Sep 2018 00:18:13 +0000 (00:18 +0000)
Change-Id: I2c38505d92d8610801670cd063d31fbf7f8df4b4
Signed-off-by: Amit Purwar <amit.purwar@samsung.com>
bt-oal/bluez_hal/src/bt-hal-gatt-server.c
bt-oal/include/oal-event.h
bt-oal/include/oal-gatt.h
bt-oal/oal-gatt.c
bt-service-adaptation/services/gatt/bt-service-gatt.c

index c749f4d..b2313dd 100644 (file)
@@ -1791,9 +1791,92 @@ static bt_status_t gatt_server_open(int server_if, const bt_bdaddr_t *bd_addr, b
        return BT_STATUS_SUCCESS;
 }
 
+static void __le_disconnection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
+               gpointer user_data)
+{
+       GError *g_error = NULL;
+       GVariant *reply = NULL;
+
+       DBG("+");
+
+       reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
+       g_object_unref(proxy);
+       if (reply == NULL) {
+               ERR("Disconnect LE Dbus Call Error");
+               if (g_error) {
+                       ERR("Error: %s\n", g_error->message);
+                       g_clear_error(&g_error);
+               }
+       }
+       g_variant_unref(reply);
+}
+
+
 static bt_status_t gatt_server_close(int server_if, const bt_bdaddr_t *bd_addr, int conn_id)
 {
        CHECK_BTGATT_INIT();
+
+       char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
+       gchar *device_path;
+       GDBusProxy *device_proxy;
+       GDBusConnection *g_conn;
+       struct gatt_client_info_t *conn_info = NULL;
+
+       INFO("+");
+
+       if (NULL == bd_addr) {
+               ERR("bd_addr is NULL");
+               return BT_STATUS_PARM_INVALID;
+       }
+
+       /* GDBUS Connection Info validate */
+       g_conn = _bt_hal_get_system_gconn();
+       if (g_conn == NULL) {
+               ERR("Could not get System DBUS Connection");
+               return  BT_STATUS_FAIL;
+       }
+
+       /* Connection Info validate */
+       conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
+       if (conn_info == NULL) {
+               ERR("No Connection Inforamtion!!!");
+               return BT_STATUS_FAIL;
+       }
+
+       _bt_hal_convert_addr_type_to_string(device_address,
+                               (unsigned char *)bd_addr->address);
+
+       //check if connection has the same device address
+       if (g_strcmp0(conn_info->addr, device_address)  != 0) {
+               ERR("device address [%s] does not match", device_address);
+               return BT_STATUS_FAIL;
+       }
+
+       device_path = _bt_hal_get_device_object_path(conn_info->addr);
+       if (device_path == NULL) {
+               ERR("device_path is NULL");
+               return BT_STATUS_PARM_INVALID;
+       }
+
+       device_proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, BT_HAL_BLUEZ_NAME,
+                       device_path, BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
+
+
+       g_free(device_path);
+       if (device_proxy == NULL)
+               return BT_STATUS_FAIL;
+
+       INFO("Disconnect LE [%s]", device_address);
+
+       g_dbus_proxy_call(device_proxy, "DisconnectLE",
+               NULL,
+               G_DBUS_CALL_FLAGS_NONE,
+               BT_HAL_MAX_DBUS_TIMEOUT,
+               NULL,
+               (GAsyncReadyCallback)__le_disconnection_req_cb, conn_info);
+
+       INFO("-");
        return BT_STATUS_SUCCESS;
 }
 
index 1639420..d7264cf 100644 (file)
@@ -330,6 +330,7 @@ typedef struct {
        int server_inst;
        int conn_id;
        bt_address_t address;
+       int status;
 } event_gatts_conn_t;
 
 typedef struct {
index 7b86c7d..238bc57 100644 (file)
@@ -371,6 +371,8 @@ oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value);
  */
 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response);
 
+oal_status_t gatts_disconnect(int Ins_id, bt_address_t *device_address, int conId);
+
 /**
  * @brief Start LE Device discovery
  *
index 75bd825..e0f3db5 100644 (file)
@@ -621,6 +621,28 @@ oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
        return OAL_STATUS_SUCCESS;
 }
 
+oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
+{
+       int ret = OAL_STATUS_SUCCESS;
+       bdstr_t bdstr;
+
+       API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
+
+       CHECK_OAL_GATT_ENABLED();
+       CHECK_SERVER_INSTANCE(instance_id);
+       CHECK_SERVER_REGISTRATION(instance_id);
+
+       API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
+
+       ret = gatt_api->server->disconnect(gatt_servers[instance_id - 1].server_id, (bt_bdaddr_t *) device_address, conn_id);
+       if (ret != BT_STATUS_SUCCESS) {
+               BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
+               return convert_to_oal_status(ret);
+       }
+       return OAL_STATUS_SUCCESS;
+}
+
+
 /************************************GATT Server Functions*************************************/
 /*Server Callbacks*/
 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
@@ -1224,6 +1246,7 @@ static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bd
        memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
        event->conn_id = conn_id;
        event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
+       event->status = OAL_STATUS_SUCCESS;
 
        send_event(event_type, event, sizeof(event_gatts_conn_t));
 }
index 0c5dd45..36d9474 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;
@@ -4692,6 +4699,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 +4726,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);