Loading system/service/low_energy_client.cpp +45 −1 Original line number Diff line number Diff line Loading @@ -281,7 +281,8 @@ LowEnergyClient::LowEnergyClient( is_setting_adv_data_(false), adv_started_(false), adv_start_callback_(nullptr), adv_stop_callback_(nullptr) { adv_stop_callback_(nullptr), scan_started_(false) { } LowEnergyClient::~LowEnergyClient() { Loading @@ -296,6 +297,49 @@ LowEnergyClient::~LowEnergyClient() { GetClientHALInterface()->multi_adv_disable(client_id_); hal::BluetoothGattInterface::Get()-> GetClientHALInterface()->unregister_client(client_id_); // Stop any scans started by this client. if (scan_started_.load()) StopScan(); } bool LowEnergyClient::StartScan(const ScanSettings& settings, const std::vector<ScanFilter>& filters) { VLOG(2) << __func__; // Cannot start a scan if the adapter is not enabled. if (!adapter_.IsEnabled()) { LOG(ERROR) << "Cannot scan while Bluetooth is disabled"; return false; } // TODO(jpawlowski): Push settings and filtering logic below the HAL. bt_status_t status = hal::BluetoothGattInterface::Get()-> StartScan(client_id_); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to initiate scanning for client: " << client_id_; return false; } scan_started_ = true; return true; } bool LowEnergyClient::StopScan() { VLOG(2) << __func__; // TODO(armansito): We don't support batch scanning yet so call // StopRegularScanForClient directly. In the future we will need to // conditionally call a batch scan API here. bt_status_t status = hal::BluetoothGattInterface::Get()-> StopScan(client_id_); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to stop scan for client: " << client_id_; return false; } scan_started_ = false; return true; } bool LowEnergyClient::StartAdvertising(const AdvertiseSettings& settings, Loading system/service/low_energy_client.h +26 −1 Original line number Diff line number Diff line Loading @@ -27,6 +27,8 @@ #include "service/common/bluetooth/advertise_data.h" #include "service/common/bluetooth/advertise_settings.h" #include "service/common/bluetooth/low_energy_constants.h" #include "service/common/bluetooth/scan_filter.h" #include "service/common/bluetooth/scan_settings.h" #include "service/common/bluetooth/uuid.h" #include "service/hal/bluetooth_gatt_interface.h" Loading @@ -47,9 +49,20 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, // Callback type used to return the result of asynchronous operations below. using StatusCallback = std::function<void(BLEStatus)>; // Initiates a BLE device scan for this client using the given |settings| and // |filters|. See the documentation for ScanSettings and ScanFilter for how // these parameters can be configured. Return true on success, false // otherwise. Please see logs for details in case of error. bool StartScan(const ScanSettings& settings, const std::vector<ScanFilter>& filters); // Stops an ongoing BLE device scan for this client. bool StopScan(); // Starts advertising based on the given advertising and scan response // data and the provided |settings|. Reports the result of the operation in // |callback|. // |callback|. Return true on success, false otherwise. Please see logs for // details in case of error. bool StartAdvertising(const AdvertiseSettings& settings, const AdvertiseData& advertise_data, const AdvertiseData& scan_response, Loading @@ -71,6 +84,9 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, return advertise_settings_; } // Returns the current scan settings. const ScanSettings& scan_settings() const { return scan_settings_; } // BluetoothClientInstace overrides: const UUID& GetAppIdentifier() const override; int GetInstanceId() const override; Loading Loading @@ -136,6 +152,15 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, std::unique_ptr<StatusCallback> adv_start_callback_; std::unique_ptr<StatusCallback> adv_stop_callback_; // Protects device scan related members below. std::mutex scan_fields_lock_; // Current scan settings. ScanSettings scan_settings_; // If true, then this client have a BLE device scan in progress. std::atomic_bool scan_started_; DISALLOW_COPY_AND_ASSIGN(LowEnergyClient); }; Loading system/service/test/low_energy_client_unittest.cpp +33 −1 Original line number Diff line number Diff line Loading @@ -34,7 +34,10 @@ namespace { class MockGattHandler : public hal::FakeBluetoothGattInterface::TestClientHandler { public: MockGattHandler() = default; MockGattHandler() { ON_CALL(*this, Scan(false)) .WillByDefault(Return(BT_STATUS_SUCCESS)); } ~MockGattHandler() override = default; MOCK_METHOD1(RegisterClient, bt_status_t(bt_uuid_t*)); Loading Loading @@ -804,5 +807,34 @@ TEST_F(LowEnergyClientPostRegisterTest, AdvertiseDataParsing) { EXPECT_EQ(BLE_STATUS_FAILURE, last_status); } TEST_F(LowEnergyClientPostRegisterTest, ScanSettings) { EXPECT_CALL(mock_adapter_, IsEnabled()) .WillOnce(Return(false)) .WillRepeatedly(Return(true)); ScanSettings settings; std::vector<ScanFilter> filters; // Adapter is not enabled. EXPECT_FALSE(le_client_->StartScan(settings, filters)); //TODO(jpawlowski): add tests checking settings and filter parsing when // implemented // These should succeed and result in a HAL call EXPECT_CALL(*mock_handler_, Scan(true)) .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); EXPECT_TRUE(le_client_->StartScan(settings, filters)); // These should succeed and result in a HAL call EXPECT_CALL(*mock_handler_, Scan(false)) .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); EXPECT_TRUE(le_client_->StopScan()); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); } } // namespace } // namespace bluetooth Loading
system/service/low_energy_client.cpp +45 −1 Original line number Diff line number Diff line Loading @@ -281,7 +281,8 @@ LowEnergyClient::LowEnergyClient( is_setting_adv_data_(false), adv_started_(false), adv_start_callback_(nullptr), adv_stop_callback_(nullptr) { adv_stop_callback_(nullptr), scan_started_(false) { } LowEnergyClient::~LowEnergyClient() { Loading @@ -296,6 +297,49 @@ LowEnergyClient::~LowEnergyClient() { GetClientHALInterface()->multi_adv_disable(client_id_); hal::BluetoothGattInterface::Get()-> GetClientHALInterface()->unregister_client(client_id_); // Stop any scans started by this client. if (scan_started_.load()) StopScan(); } bool LowEnergyClient::StartScan(const ScanSettings& settings, const std::vector<ScanFilter>& filters) { VLOG(2) << __func__; // Cannot start a scan if the adapter is not enabled. if (!adapter_.IsEnabled()) { LOG(ERROR) << "Cannot scan while Bluetooth is disabled"; return false; } // TODO(jpawlowski): Push settings and filtering logic below the HAL. bt_status_t status = hal::BluetoothGattInterface::Get()-> StartScan(client_id_); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to initiate scanning for client: " << client_id_; return false; } scan_started_ = true; return true; } bool LowEnergyClient::StopScan() { VLOG(2) << __func__; // TODO(armansito): We don't support batch scanning yet so call // StopRegularScanForClient directly. In the future we will need to // conditionally call a batch scan API here. bt_status_t status = hal::BluetoothGattInterface::Get()-> StopScan(client_id_); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to stop scan for client: " << client_id_; return false; } scan_started_ = false; return true; } bool LowEnergyClient::StartAdvertising(const AdvertiseSettings& settings, Loading
system/service/low_energy_client.h +26 −1 Original line number Diff line number Diff line Loading @@ -27,6 +27,8 @@ #include "service/common/bluetooth/advertise_data.h" #include "service/common/bluetooth/advertise_settings.h" #include "service/common/bluetooth/low_energy_constants.h" #include "service/common/bluetooth/scan_filter.h" #include "service/common/bluetooth/scan_settings.h" #include "service/common/bluetooth/uuid.h" #include "service/hal/bluetooth_gatt_interface.h" Loading @@ -47,9 +49,20 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, // Callback type used to return the result of asynchronous operations below. using StatusCallback = std::function<void(BLEStatus)>; // Initiates a BLE device scan for this client using the given |settings| and // |filters|. See the documentation for ScanSettings and ScanFilter for how // these parameters can be configured. Return true on success, false // otherwise. Please see logs for details in case of error. bool StartScan(const ScanSettings& settings, const std::vector<ScanFilter>& filters); // Stops an ongoing BLE device scan for this client. bool StopScan(); // Starts advertising based on the given advertising and scan response // data and the provided |settings|. Reports the result of the operation in // |callback|. // |callback|. Return true on success, false otherwise. Please see logs for // details in case of error. bool StartAdvertising(const AdvertiseSettings& settings, const AdvertiseData& advertise_data, const AdvertiseData& scan_response, Loading @@ -71,6 +84,9 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, return advertise_settings_; } // Returns the current scan settings. const ScanSettings& scan_settings() const { return scan_settings_; } // BluetoothClientInstace overrides: const UUID& GetAppIdentifier() const override; int GetInstanceId() const override; Loading Loading @@ -136,6 +152,15 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, std::unique_ptr<StatusCallback> adv_start_callback_; std::unique_ptr<StatusCallback> adv_stop_callback_; // Protects device scan related members below. std::mutex scan_fields_lock_; // Current scan settings. ScanSettings scan_settings_; // If true, then this client have a BLE device scan in progress. std::atomic_bool scan_started_; DISALLOW_COPY_AND_ASSIGN(LowEnergyClient); }; Loading
system/service/test/low_energy_client_unittest.cpp +33 −1 Original line number Diff line number Diff line Loading @@ -34,7 +34,10 @@ namespace { class MockGattHandler : public hal::FakeBluetoothGattInterface::TestClientHandler { public: MockGattHandler() = default; MockGattHandler() { ON_CALL(*this, Scan(false)) .WillByDefault(Return(BT_STATUS_SUCCESS)); } ~MockGattHandler() override = default; MOCK_METHOD1(RegisterClient, bt_status_t(bt_uuid_t*)); Loading Loading @@ -804,5 +807,34 @@ TEST_F(LowEnergyClientPostRegisterTest, AdvertiseDataParsing) { EXPECT_EQ(BLE_STATUS_FAILURE, last_status); } TEST_F(LowEnergyClientPostRegisterTest, ScanSettings) { EXPECT_CALL(mock_adapter_, IsEnabled()) .WillOnce(Return(false)) .WillRepeatedly(Return(true)); ScanSettings settings; std::vector<ScanFilter> filters; // Adapter is not enabled. EXPECT_FALSE(le_client_->StartScan(settings, filters)); //TODO(jpawlowski): add tests checking settings and filter parsing when // implemented // These should succeed and result in a HAL call EXPECT_CALL(*mock_handler_, Scan(true)) .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); EXPECT_TRUE(le_client_->StartScan(settings, filters)); // These should succeed and result in a HAL call EXPECT_CALL(*mock_handler_, Scan(false)) .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); EXPECT_TRUE(le_client_->StopScan()); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); } } // namespace } // namespace bluetooth