From 1d57bee95b48359bccaaa63439b0b9370606d1dd Mon Sep 17 00:00:00 2001 From: himanshu Date: Fri, 20 Sep 2019 21:22:35 +0530 Subject: [PATCH] Add and remove device from whitelist Change-Id: I476224d047e4e5a2b7ddb7b79d209bb6a5baba00 Signed-off-by: himanshu --- bt-oal/bluez_hal/src/bt-hal-adapter-le.c | 46 ++++++++++++++++++++++ bt-oal/bluez_hal/src/bt-hal-adapter-le.h | 2 + bt-oal/bluez_hal/src/bt-hal-bluetooth.c | 1 + bt-oal/hardware/bluetooth.h | 12 ++++++ bt-oal/include/oal-adapter-mgr.h | 16 ++++++++ bt-oal/oal-adapter-mgr.c | 21 ++++++++++ .../services/adapter/bt-service-core-adapter-le.c | 30 +++++++++++++- .../services/bt-request-handler.c | 22 +++++++++++ .../services/include/bt-service-core-adapter-le.h | 1 + 9 files changed, 150 insertions(+), 1 deletion(-) diff --git a/bt-oal/bluez_hal/src/bt-hal-adapter-le.c b/bt-oal/bluez_hal/src/bt-hal-adapter-le.c index 152bca5..3acf1cf 100644 --- a/bt-oal/bluez_hal/src/bt-hal-adapter-le.c +++ b/bt-oal/bluez_hal/src/bt-hal-adapter-le.c @@ -965,3 +965,49 @@ void _bt_hal_unregister_gatt_le_dbus_handler_cb(void) { gatt_le_event_cb = NULL; } + +/*add/remove remote device address from white list*/ +int _bt_hal_adapter_le_set_white_list(bt_bdaddr_t *device_address, bt_dev_addr_type_t address_type, bool is_add) +{ + GDBusProxy *proxy; + char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 }; + GError *error = NULL; + GVariant *ret; + + if (address_type != BLUETOOTH_HAL_DEVICE_PUBLIC_ADDRESS && + address_type != BLUETOOTH_HAL_DEVICE_RANDOM_ADDRESS) + return BT_STATUS_PARM_INVALID; + + _bt_hal_convert_addr_type_to_string(address, device_address->address); + + proxy = _bt_hal_get_adapter_proxy(); + if (proxy == NULL) + return BT_STATUS_FAIL; + if (is_add) + ret = g_dbus_proxy_call_sync(proxy, "AddDeviceWhiteList", + g_variant_new("(su)", address, address_type), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + else + ret = g_dbus_proxy_call_sync(proxy, "RemoveDeviceWhiteList", + g_variant_new("(su)", address, address_type), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + + if (error) { + if (is_add) + ERR("RemoveDeviceWhiteList Fail: %s", error->message); + else + ERR("AddDeviceWhiteList Fail: %s", error->message); + g_clear_error(&error); + return BT_STATUS_FAIL; + } + + if (ret) + g_variant_unref(ret); + if (is_add) + INFO("Device Added to white list"); + else + INFO("Device Removed from white list"); + return BT_STATUS_SUCCESS; +} diff --git a/bt-oal/bluez_hal/src/bt-hal-adapter-le.h b/bt-oal/bluez_hal/src/bt-hal-adapter-le.h index d20e475..ab51773 100644 --- a/bt-oal/bluez_hal/src/bt-hal-adapter-le.h +++ b/bt-oal/bluez_hal/src/bt-hal-adapter-le.h @@ -81,6 +81,8 @@ void _bt_hal_get_gatt_server_instance_initialized(int *instance); int _bt_hal_le_init(void); void _bt_hal_le_deinit(void); + +int _bt_hal_adapter_le_set_white_list(bt_bdaddr_t *device_address, bt_dev_addr_type_t address_type, bool is_add); #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/bt-oal/bluez_hal/src/bt-hal-bluetooth.c b/bt-oal/bluez_hal/src/bt-hal-bluetooth.c index 513b550..1f8a36d 100644 --- a/bt-oal/bluez_hal/src/bt-hal-bluetooth.c +++ b/bt-oal/bluez_hal/src/bt-hal-bluetooth.c @@ -461,6 +461,7 @@ static const bt_interface_t bluetooth_if = { .set_le_static_random_address = _bt_hal_set_le_static_random_address, .set_hal_adapter_request_state = set_hal_adapter_request_state, .set_hal_le_request_state = set_hal_le_request_state, + .adapter_le_set_white_list = _bt_hal_adapter_le_set_white_list, #endif }; diff --git a/bt-oal/hardware/bluetooth.h b/bt-oal/hardware/bluetooth.h index 603c61e..581581e 100644 --- a/bt-oal/hardware/bluetooth.h +++ b/bt-oal/hardware/bluetooth.h @@ -440,6 +440,14 @@ typedef enum { BT_DISC_ROLE_DUAL } bt_disc_role_type_t; +/** +* This is Bluetooth device address type +*/ +typedef enum { + BLUETOOTH_HAL_DEVICE_PUBLIC_ADDRESS = 0x00, + BLUETOOTH_HAL_DEVICE_RANDOM_ADDRESS +} bt_dev_addr_type_t; + /** Bluetooth Trusted Profiles */ typedef enum { BT_TRUSTED_PROFILE_PBAP = 1, @@ -858,6 +866,10 @@ typedef struct { */ int (*set_hal_le_request_state)(int enable); + /* + * adds/removes the remote device address to/from the white list + */ + int (*adapter_le_set_white_list)(bt_bdaddr_t *device_address, bt_dev_addr_type_t address_type, bool is_add); #endif } bt_interface_t; diff --git a/bt-oal/include/oal-adapter-mgr.h b/bt-oal/include/oal-adapter-mgr.h index 23b98c4..83e0b35 100644 --- a/bt-oal/include/oal-adapter-mgr.h +++ b/bt-oal/include/oal-adapter-mgr.h @@ -444,6 +444,22 @@ oal_status_t adapter_set_le_static_random_address(int enable); * @pre Adapter must be enabled with adapter_enable() followed by OAL_EVENT_ADAPTER_ENABLED */ oal_status_t is_advertising(void); + +/** + * @brief Add/removes device to/from whitelist + * + * @param remote device address + * @param remote device address type + * @param whether to add/remove from white list + * + * @details adds/removes device to/from controller's white list. + * + * @return OAL_STATUS_SUCCESS on success, otherwise a non-zero error value. + * @retval #OAL_STATUS_SUCCESS Successful + * + * @pre Adapter must be enabled with adapter_enable() followed by OAL_EVENT_ADAPTER_ENABLED + */ +oal_status_t adapter_set_white_list(bt_address_t* device_address, int address_type, bool is_add); #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/bt-oal/oal-adapter-mgr.c b/bt-oal/oal-adapter-mgr.c index e3f2d7d..4022dc6 100644 --- a/bt-oal/oal-adapter-mgr.c +++ b/bt-oal/oal-adapter-mgr.c @@ -738,6 +738,27 @@ oal_status_t adapter_set_le_static_random_address(int enable) return ret; } +oal_status_t adapter_set_white_list(bt_address_t *device_address, int address_type, bool is_add) +{ + int ret; + bdstr_t bdstr; + + CHECK_OAL_INITIALIZED(); + API_TRACE(); + + BT_INFO("BT remote device Address: %s", bdt_bd2str(device_address, &bdstr)); + + ret = blued_api->adapter_le_set_white_list((bt_bdaddr_t*)device_address, address_type, is_add); + if (ret != BT_STATUS_SUCCESS) { + if(is_add) + BT_ERR("Add to White List Failed: [%s]",status2string(ret)); + else + BT_ERR("Remove from White List Failed: [%s]",status2string(ret)); + } + ret = convert_to_oal_status(ret); + return ret; +} + static void cb_adapter_properties(bt_status_t status, int num_properties, bt_property_t *properties) diff --git a/bt-service-adaptation/services/adapter/bt-service-core-adapter-le.c b/bt-service-adaptation/services/adapter/bt-service-core-adapter-le.c index 1e638a3..3901536 100644 --- a/bt-service-adaptation/services/adapter/bt-service-core-adapter-le.c +++ b/bt-service-adaptation/services/adapter/bt-service-core-adapter-le.c @@ -1732,6 +1732,35 @@ gboolean _bt_is_le_coded_phy_supported(void) return FALSE; } +int _bt_set_white_list(bluetooth_device_address_t *device_address, int address_type, bool is_add) +{ + int result = BLUETOOTH_ERROR_NONE; + if (__bt_is_factory_test_mode()) { + if(is_add) + BT_ERR("Unable to add to white list in factory binary !!"); + else + BT_ERR("Unable to remove white list in factory binary !!"); + return BLUETOOTH_ERROR_NOT_SUPPORT; + } + + BT_CHECK_PARAMETER(device_address, return); + + if(_bt_adapter_get_status() != BT_ACTIVATED ){ + BT_ERR("Bluetooth adapter is disabled"); + return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; + } + + result = adapter_set_white_list((bt_address_t*)device_address, address_type, is_add); + if (result != OAL_STATUS_SUCCESS) { + if(is_add) + BT_ERR("Add to White List Failed %d", result); + else + BT_ERR("Remove from white list Failed %d", result); + return BLUETOOTH_ERROR_INTERNAL; + } + + return result; +} /*************************************** LE Scan APIs *********************************************/ @@ -1760,4 +1789,3 @@ int _bt_set_le_static_random_address(gboolean is_enable) return result; } - diff --git a/bt-service-adaptation/services/bt-request-handler.c b/bt-service-adaptation/services/bt-request-handler.c index dd6a6eb..ad15ee8 100644 --- a/bt-service-adaptation/services/bt-request-handler.c +++ b/bt-service-adaptation/services/bt-request-handler.c @@ -840,6 +840,28 @@ int __bt_bluez_request(int function_name, result = _bt_set_authorization(&address, authorize); break; } + case BT_ADD_WHITE_LIST: { + bluetooth_device_address_t address = { {0} }; + int address_type = 0; + bool is_add = true; + __bt_service_get_parameters(in_param1, + &address, sizeof(bluetooth_device_address_t)); + __bt_service_get_parameters(in_param2, + &address_type, sizeof(int)); + result = _bt_set_white_list(&address, address_type, is_add); + break; + } + case BT_REMOVE_WHITE_LIST: { + bluetooth_device_address_t address = { {0} }; + int address_type = 0; + bool is_add = false; + __bt_service_get_parameters(in_param1, + &address, sizeof(bluetooth_device_address_t)); + __bt_service_get_parameters(in_param2, + &address_type, sizeof(int)); + result = _bt_set_white_list(&address, address_type, is_add); + break; + } case BT_IS_DEVICE_CONNECTED: { bluetooth_device_address_t address = { {0} }; gboolean connected = FALSE; diff --git a/bt-service-adaptation/services/include/bt-service-core-adapter-le.h b/bt-service-adaptation/services/include/bt-service-core-adapter-le.h index 0bfa5d0..c8bb849 100644 --- a/bt-service-adaptation/services/include/bt-service-core-adapter-le.h +++ b/bt-service-adaptation/services/include/bt-service-core-adapter-le.h @@ -95,6 +95,7 @@ gboolean _bt_is_le_coded_phy_supported(void); int _bt_set_le_static_random_address(gboolean is_enable); +int _bt_set_white_list(bluetooth_device_address_t *device_address, int address_type, bool is_add); #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.7.4