From 05e256c0c26e9731b22af9d824bb3daad47f02c4 Mon Sep 17 00:00:00 2001 From: Anupam Roy Date: Mon, 25 Jul 2016 15:50:29 +0530 Subject: [PATCH] [Adapt: HAL] Implement Device Set Authorization(Trust) API Change-Id: Ia08b209b9ce2316acfc9f175683cc0c8cb815636 Signed-off-by: Anupam Roy --- bt-oal/bluez_hal/hardware/bluetooth.h | 3 + bt-oal/bluez_hal/src/bt-hal-agent.c | 2 + bt-oal/bluez_hal/src/bt-hal-bluetooth.c | 8 ++ bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.c | 108 ++++++++++++++++++++++ bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.h | 2 + 5 files changed, 123 insertions(+) diff --git a/bt-oal/bluez_hal/hardware/bluetooth.h b/bt-oal/bluez_hal/hardware/bluetooth.h index 68ce7e8..eef11e4 100755 --- a/bt-oal/bluez_hal/hardware/bluetooth.h +++ b/bt-oal/bluez_hal/hardware/bluetooth.h @@ -654,6 +654,9 @@ typedef struct { /* Tizen Specific: Send service level Authorization response */ int (*authorize_response)(const bt_bdaddr_t *bd_addr, bt_service_id_t service_id, uint8_t authorize, uint8_t save_settings); + + /** Set auto authorization for peer device. Should be a paired device */ + int (*set_authorization)(bt_bdaddr_t *bd_addr, uint8_t auth); } bt_interface_t; /** TODO: Need to add APIs for Service Discovery, Service authorization and diff --git a/bt-oal/bluez_hal/src/bt-hal-agent.c b/bt-oal/bluez_hal/src/bt-hal-agent.c index 08e7e93..e7b6cde 100644 --- a/bt-oal/bluez_hal/src/bt-hal-agent.c +++ b/bt-oal/bluez_hal/src/bt-hal-agent.c @@ -629,6 +629,8 @@ static gboolean __bt_hal_authorize_request(GapAgentPrivate *agent, GDBusProxy *d INFO("Trusted device, so authorize\n"); gap_agent_reply_authorize(agent, GAP_AGENT_ACCEPT, NULL); goto done; + } else { + INFO("Device is not Trusted, so prompt user to accept or reject authorization \n"); } /* diff --git a/bt-oal/bluez_hal/src/bt-hal-bluetooth.c b/bt-oal/bluez_hal/src/bt-hal-bluetooth.c index 8a76fca..c533f19 100644 --- a/bt-oal/bluez_hal/src/bt-hal-bluetooth.c +++ b/bt-oal/bluez_hal/src/bt-hal-bluetooth.c @@ -292,6 +292,12 @@ static int authorize_response(const bt_bdaddr_t *bd_addr, bt_service_id_t servic return _bt_hal_device_authorize_response(bd_addr, service_id, authorize, save_settings); } +int set_authorization(bt_bdaddr_t *bd_addr, uint8_t auth) +{ + DBG("+"); + return _bt_hal_device_set_trust(bd_addr, auth); +} + static const bt_interface_t bluetooth_if = { .size = sizeof(bt_interface_t), .init = init, @@ -321,7 +327,9 @@ static const bt_interface_t bluetooth_if = { .get_connection_state = get_connection_state, .set_os_callouts = set_os_callouts, .read_energy_info = read_energy_info, + /* Tizen Specific API's*/ .authorize_response = authorize_response, + .set_authorization = set_authorization, }; static const bt_interface_t *get_bluetooth_interface(void) diff --git a/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.c b/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.c index ba1de57..0af9880 100644 --- a/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.c +++ b/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.c @@ -398,6 +398,114 @@ int _bt_hal_device_authorize_response(const bt_bdaddr_t *bd_addr, bt_service_id_ return BT_STATUS_SUCCESS; } +int _bt_hal_device_set_trust(const bt_bdaddr_t *bd_addr, uint8_t trust) +{ + char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 }; + gchar *device_path = NULL; + GDBusProxy *device_proxy; + gboolean previous_value; + gboolean authorize; + GDBusConnection *conn; + GError *error = NULL; + GVariant *result = NULL; + GVariant *temp = NULL; + int ret = BT_STATUS_SUCCESS; + DBG("+"); + + if (!bd_addr) + return BT_STATUS_PARM_INVALID; + + if (trust == 1) + authorize = TRUE; + else + authorize = FALSE; + + conn = _bt_get_system_gconn(); + + if (conn == NULL) { + ERR("Failed to get DBUS connection"); + return BT_STATUS_FAIL; + } + + _bt_convert_addr_type_to_string(address, bd_addr->address); + + device_path = _bt_get_device_object_path(address); + + if (device_path == NULL) { + ERR("No paired device"); + return BT_STATUS_FAIL; + } + + DBG("Device path [%s]", device_path); + device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, + NULL, BT_HAL_BLUEZ_NAME, + device_path, BT_HAL_PROPERTIES_INTERFACE, NULL, &error); + + g_free(device_path); + + if (device_proxy == NULL) { + if (error) { + g_dbus_error_strip_remote_error(error); + ERR("Device proxy get failed %s", error->message); + g_error_free(error); + } + ERR("Failed to get Device Proxy"); + return BT_STATUS_FAIL; + } + + result = g_dbus_proxy_call_sync(device_proxy, "Get", + g_variant_new("(ss)", BT_HAL_DEVICE_INTERFACE, "Trusted"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (error != NULL) { + ERR("Getting property failed: [%s]\n", error->message); + g_error_free(error); + g_object_unref(device_proxy); + return BT_STATUS_FAIL; + } + + /* Fetch GVaraint*/ + g_variant_get(result, "(v)", &temp); + previous_value = g_variant_get_boolean(temp); + DBG("Previous value [%d]", previous_value); + + /* If the input is same with previous value, return error. */ + if (previous_value == authorize) { + ERR("Same value: %d", previous_value); + g_object_unref(device_proxy); + g_object_unref(result); + g_object_unref(temp); + return BT_STATUS_PARM_INVALID; + } + + DBG("Set authotrize [%d]", authorize); + result = g_dbus_proxy_call_sync(device_proxy, "Set", + g_variant_new("(ssv)", BT_HAL_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (error) { + ERR("SetProperty error: [%s]", error->message); + g_error_free(error); + ret = BT_STATUS_FAIL; + } + + g_object_unref(device_proxy); + + if (result) + g_object_unref(result); + if (temp) + g_object_unref(temp); + + DBG("-"); + return ret; +} + static void __bt_hal_device_service_search_cb(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data) { diff --git a/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.h b/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.h index 7896dcf..1d83adc 100644 --- a/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.h +++ b/bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.h @@ -58,6 +58,8 @@ int _bt_hal_dbus_get_remote_device_services(const bt_bdaddr_t *remote_addr); int _bt_hal_device_authorize_response(const bt_bdaddr_t *bd_addr, bt_service_id_t service_id, uint8_t authorize, uint8_t save_settings); +int _bt_hal_device_set_trust(const bt_bdaddr_t *bd_addr, uint8_t trust); + #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.7.4