Commit bc179a18 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Add BluetoothHelper::GetBluetoothPublicAddress()

Will be used to create a Bluetooth Classic connection via the Nearby
Connections library; required because we will not have access to the
full RemoteDeviceRef object when making the request (only the ID will be
available).

Bug: 1106937
Change-Id: I80ea9f180f95525cd8b679c3ac15eb418b1007c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2412839
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Auto-Submit: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#807534}
parent 4afb7da7
...@@ -22,6 +22,9 @@ namespace secure_channel { ...@@ -22,6 +22,9 @@ namespace secure_channel {
// Provides the ability to generate BLE advertisement service data and, given // Provides the ability to generate BLE advertisement service data and, given
// service data that has been received in a BLE discovery session, identify the // service data that has been received in a BLE discovery session, identify the
// device which sent the advertisement. // device which sent the advertisement.
//
// Also provides functionality to retrieve the Bluetooth public address for a
// device for use in Bluetooth Classic connections.
class BluetoothHelper { class BluetoothHelper {
public: public:
virtual ~BluetoothHelper(); virtual ~BluetoothHelper();
...@@ -43,6 +46,10 @@ class BluetoothHelper { ...@@ -43,6 +46,10 @@ class BluetoothHelper {
const std::string& service_data, const std::string& service_data,
const DeviceIdPairSet& device_id_pair_set); const DeviceIdPairSet& device_id_pair_set);
// Note: An empty string is returned if there is no known public address.
virtual std::string GetBluetoothPublicAddress(
const std::string& device_id) = 0;
protected: protected:
BluetoothHelper(); BluetoothHelper();
......
...@@ -130,6 +130,16 @@ BluetoothHelperImpl::PerformIdentifyRemoteDevice( ...@@ -130,6 +130,16 @@ BluetoothHelperImpl::PerformIdentifyRemoteDevice(
return base::nullopt; return base::nullopt;
} }
std::string BluetoothHelperImpl::GetBluetoothPublicAddress(
const std::string& device_id) {
base::Optional<multidevice::RemoteDeviceRef> device =
remote_device_cache_->GetRemoteDevice(base::nullopt /* instance_id */,
device_id /* legacy_device_id */);
if (device)
return device->bluetooth_public_address();
return std::string();
}
base::Optional<BluetoothHelper::DeviceWithBackgroundBool> base::Optional<BluetoothHelper::DeviceWithBackgroundBool>
BluetoothHelperImpl::PerformIdentifyRemoteDevice( BluetoothHelperImpl::PerformIdentifyRemoteDevice(
const std::string& service_data, const std::string& service_data,
......
...@@ -57,6 +57,7 @@ class BluetoothHelperImpl : public BluetoothHelper { ...@@ -57,6 +57,7 @@ class BluetoothHelperImpl : public BluetoothHelper {
base::Optional<DeviceWithBackgroundBool> PerformIdentifyRemoteDevice( base::Optional<DeviceWithBackgroundBool> PerformIdentifyRemoteDevice(
const std::string& service_data, const std::string& service_data,
const DeviceIdPairSet& device_id_pair_set) override; const DeviceIdPairSet& device_id_pair_set) override;
std::string GetBluetoothPublicAddress(const std::string& device_id) override;
base::Optional<BluetoothHelper::DeviceWithBackgroundBool> base::Optional<BluetoothHelper::DeviceWithBackgroundBool>
PerformIdentifyRemoteDevice( PerformIdentifyRemoteDevice(
......
...@@ -83,6 +83,7 @@ multidevice::RemoteDeviceRef CreateLocalDevice(int id) { ...@@ -83,6 +83,7 @@ multidevice::RemoteDeviceRef CreateLocalDevice(int id) {
.SetInstanceId("local instance id " + base::NumberToString(id)) .SetInstanceId("local instance id " + base::NumberToString(id))
.SetPublicKey("local public key " + base::NumberToString(id)) .SetPublicKey("local public key " + base::NumberToString(id))
.SetBeaconSeeds(CreateFakeBeaconSeeds(id)) .SetBeaconSeeds(CreateFakeBeaconSeeds(id))
.SetBluetoothPublicAddress(base::NumberToString(id))
.Build(); .Build();
} }
...@@ -304,6 +305,12 @@ TEST_F(SecureChannelBluetoothHelperImplTest, ...@@ -304,6 +305,12 @@ TEST_F(SecureChannelBluetoothHelperImplTest,
EXPECT_FALSE(device_with_background_bool); EXPECT_FALSE(device_with_background_bool);
} }
TEST_F(SecureChannelBluetoothHelperImplTest, BluetoothPublicAddress) {
EXPECT_EQ("1", test_local_device_1_.bluetooth_public_address());
EXPECT_EQ("1", helper_->GetBluetoothPublicAddress(
test_local_device_1_.GetDeviceId()));
}
} // namespace secure_channel } // namespace secure_channel
} // namespace chromeos } // namespace chromeos
...@@ -34,6 +34,13 @@ void FakeBluetoothHelper::SetIdentifiedDevice( ...@@ -34,6 +34,13 @@ void FakeBluetoothHelper::SetIdentifiedDevice(
is_background_advertisement)}); is_background_advertisement)});
} }
void FakeBluetoothHelper::SetBluetoothPublicAddress(
const std::string& device_id,
const std::string& bluetooth_public_address) {
device_id_to_bluetooth_public_address_map_[device_id] =
bluetooth_public_address;
}
std::unique_ptr<DataWithTimestamp> std::unique_ptr<DataWithTimestamp>
FakeBluetoothHelper::GenerateForegroundAdvertisement( FakeBluetoothHelper::GenerateForegroundAdvertisement(
const DeviceIdPair& device_id_pair) { const DeviceIdPair& device_id_pair) {
...@@ -56,6 +63,11 @@ FakeBluetoothHelper::PerformIdentifyRemoteDevice( ...@@ -56,6 +63,11 @@ FakeBluetoothHelper::PerformIdentifyRemoteDevice(
return service_data_to_device_with_background_bool_map_.at(service_data); return service_data_to_device_with_background_bool_map_.at(service_data);
} }
std::string FakeBluetoothHelper::GetBluetoothPublicAddress(
const std::string& device_id) {
return device_id_to_bluetooth_public_address_map_[device_id];
}
} // namespace secure_channel } // namespace secure_channel
} // namespace chromeos } // namespace chromeos
...@@ -37,6 +37,10 @@ class FakeBluetoothHelper : public BluetoothHelper { ...@@ -37,6 +37,10 @@ class FakeBluetoothHelper : public BluetoothHelper {
multidevice::RemoteDeviceRef identified_device, multidevice::RemoteDeviceRef identified_device,
bool is_background_advertisement); bool is_background_advertisement);
// Sets the data to be returned by a GetBluetoothPublicAddress() call.
void SetBluetoothPublicAddress(const std::string& device_id,
const std::string& bluetooth_public_address);
private: private:
// BluetoothHelper: // BluetoothHelper:
std::unique_ptr<DataWithTimestamp> GenerateForegroundAdvertisement( std::unique_ptr<DataWithTimestamp> GenerateForegroundAdvertisement(
...@@ -44,6 +48,7 @@ class FakeBluetoothHelper : public BluetoothHelper { ...@@ -44,6 +48,7 @@ class FakeBluetoothHelper : public BluetoothHelper {
base::Optional<DeviceWithBackgroundBool> PerformIdentifyRemoteDevice( base::Optional<DeviceWithBackgroundBool> PerformIdentifyRemoteDevice(
const std::string& service_data, const std::string& service_data,
const DeviceIdPairSet& device_id_pair_set) override; const DeviceIdPairSet& device_id_pair_set) override;
std::string GetBluetoothPublicAddress(const std::string& device_id) override;
std::unordered_map<DeviceIdPair, DataWithTimestamp, DeviceIdPairHash> std::unordered_map<DeviceIdPair, DataWithTimestamp, DeviceIdPairHash>
device_id_pair_to_service_data_map_; device_id_pair_to_service_data_map_;
...@@ -51,6 +56,9 @@ class FakeBluetoothHelper : public BluetoothHelper { ...@@ -51,6 +56,9 @@ class FakeBluetoothHelper : public BluetoothHelper {
std::unordered_map<std::string, DeviceWithBackgroundBool> std::unordered_map<std::string, DeviceWithBackgroundBool>
service_data_to_device_with_background_bool_map_; service_data_to_device_with_background_bool_map_;
std::unordered_map<std::string, std::string>
device_id_to_bluetooth_public_address_map_;
DISALLOW_COPY_AND_ASSIGN(FakeBluetoothHelper); DISALLOW_COPY_AND_ASSIGN(FakeBluetoothHelper);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment