Commit 94876d90 authored by Qiyu Hu's avatar Qiyu Hu Committed by Commit Bot

bluetooth: Add EIR as device property

CQ-DEPEND=CL:1316428

Bug: chromium:887029,b:118334521
Test: unittest
Change-Id: I02bf1d4af3f08762c7ecf0ff69637ad53f0e014b
Reviewed-on: https://chromium-review.googlesource.com/c/1313412
Commit-Queue: Qiyu Hu <qiyuh@google.com>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605879}
parent b6eb9cd8
...@@ -138,6 +138,19 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter ...@@ -138,6 +138,19 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter
virtual void DeviceMTUChanged(BluetoothAdapter* adapter, virtual void DeviceMTUChanged(BluetoothAdapter* adapter,
BluetoothDevice* device, BluetoothDevice* device,
uint16_t mtu) {} uint16_t mtu) {}
// This function is implemented for ChromeOS only.
// Called when advertisement is received from |device|. |eir| is the
// extended inquiry response specified in Bluetooth Core Spec, Vol 3,
// Part C, Section 11.
//
// Override this function to observe LE advertisements. Whenever |rssi| of
// |device| changes, this function is called with the latest |eir| from
// |device|. This function is never called on classic |device|.
virtual void DeviceAdvertisementReceived(BluetoothAdapter* adapter,
BluetoothDevice* device,
int16_t rssi,
const std::vector<uint8_t>& eir) {}
#endif #endif
// Called when the device |device| is removed from the adapter |adapter|, // Called when the device |device| is removed from the adapter |adapter|,
......
...@@ -683,6 +683,13 @@ void BluetoothAdapterBlueZ::DevicePropertyChanged( ...@@ -683,6 +683,13 @@ void BluetoothAdapterBlueZ::DevicePropertyChanged(
if (property_name == properties->mtu.name()) if (property_name == properties->mtu.name())
NotifyDeviceMTUChanged(device_bluez, properties->mtu.value()); NotifyDeviceMTUChanged(device_bluez, properties->mtu.value());
// We use the RSSI as a proxy for receiving an advertisement because it's
// usually updated whenever an advertisement is received.
if (property_name == properties->rssi.name() && properties->rssi.is_valid() &&
properties->eir.is_valid())
NotifyDeviceAdvertisementReceived(device_bluez, properties->rssi.value(),
properties->eir.value());
if (property_name == properties->services_resolved.name() && if (property_name == properties->services_resolved.name() &&
properties->services_resolved.value()) { properties->services_resolved.value()) {
device_bluez->UpdateGattServices(object_path); device_bluez->UpdateGattServices(object_path);
...@@ -1135,6 +1142,16 @@ void BluetoothAdapterBlueZ::NotifyDeviceMTUChanged(BluetoothDeviceBlueZ* device, ...@@ -1135,6 +1142,16 @@ void BluetoothAdapterBlueZ::NotifyDeviceMTUChanged(BluetoothDeviceBlueZ* device,
observer.DeviceMTUChanged(this, device, mtu); observer.DeviceMTUChanged(this, device, mtu);
} }
void BluetoothAdapterBlueZ::NotifyDeviceAdvertisementReceived(
BluetoothDeviceBlueZ* device,
int16_t rssi,
const std::vector<uint8_t>& eir) {
DCHECK(device->adapter_ == this);
for (auto& observer : observers_)
observer.DeviceAdvertisementReceived(this, device, rssi, eir);
}
void BluetoothAdapterBlueZ::UseProfile( void BluetoothAdapterBlueZ::UseProfile(
const BluetoothUUID& uuid, const BluetoothUUID& uuid,
const dbus::ObjectPath& device_path, const dbus::ObjectPath& device_path,
......
...@@ -167,6 +167,11 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterBlueZ ...@@ -167,6 +167,11 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterBlueZ
// Announce to observers MTU change in ATT communication to |device|. // Announce to observers MTU change in ATT communication to |device|.
void NotifyDeviceMTUChanged(BluetoothDeviceBlueZ* device, uint16_t mtu); void NotifyDeviceMTUChanged(BluetoothDeviceBlueZ* device, uint16_t mtu);
// Announce to observers advertisement received from |device|.
void NotifyDeviceAdvertisementReceived(BluetoothDeviceBlueZ* device,
int16_t rssi,
const std::vector<uint8_t>& eir);
// Returns the object path of the adapter. // Returns the object path of the adapter.
const dbus::ObjectPath& object_path() const { return object_path_; } const dbus::ObjectPath& object_path() const { return object_path_; }
......
...@@ -2399,6 +2399,45 @@ TEST_F(BluetoothBlueZTest, DeviceMTUChanged) { ...@@ -2399,6 +2399,45 @@ TEST_F(BluetoothBlueZTest, DeviceMTUChanged) {
EXPECT_EQ(2, observer.device_mtu_changed_count()); EXPECT_EQ(2, observer.device_mtu_changed_count());
EXPECT_EQ(128, observer.last_mtu_value()); EXPECT_EQ(128, observer.last_mtu_value());
} }
TEST_F(BluetoothBlueZTest, DeviceAdvertisementReceived) {
// Simulate reception of advertisement from a device.
GetAdapter();
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
BluetoothDevice* device =
adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress);
ASSERT_TRUE(device);
// Install an observer; expect DeviceAdvertisementReceived method to be called
// with the EIR and RSSI.
TestBluetoothAdapterObserver observer(adapter_);
bluez::FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath));
ASSERT_EQ(0, observer.device_advertisement_received_count());
std::vector<uint8_t> eir = {0x01, 0x02, 0x03};
properties->eir.ReplaceValue(eir);
properties->rssi.ReplaceValue(-73);
properties->rssi.set_valid(true);
properties->NotifyPropertyChanged(properties->rssi.name());
EXPECT_EQ(0, observer.device_advertisement_received_count()); // EIR invalid
properties->eir.set_valid(true);
properties->rssi.set_valid(false);
properties->NotifyPropertyChanged(properties->rssi.name());
EXPECT_EQ(0, observer.device_advertisement_received_count()); // RSSI invalid
properties->rssi.set_valid(true);
properties->NotifyPropertyChanged(properties->rssi.name());
EXPECT_EQ(1, observer.device_advertisement_received_count());
EXPECT_EQ(eir, observer.device_eir());
}
#endif #endif
TEST_F(BluetoothBlueZTest, DeviceUuidsChanged) { TEST_F(BluetoothBlueZTest, DeviceUuidsChanged) {
......
...@@ -209,6 +209,7 @@ BluetoothDeviceClient::Properties::Properties( ...@@ -209,6 +209,7 @@ BluetoothDeviceClient::Properties::Properties(
RegisterProperty(bluetooth_device::kAdvertisingDataFlagsProperty, RegisterProperty(bluetooth_device::kAdvertisingDataFlagsProperty,
&advertising_data_flags); &advertising_data_flags);
RegisterProperty(bluetooth_device::kMTUProperty, &mtu); RegisterProperty(bluetooth_device::kMTUProperty, &mtu);
RegisterProperty(bluetooth_device::kEIRProperty, &eir);
} }
BluetoothDeviceClient::Properties::~Properties() = default; BluetoothDeviceClient::Properties::~Properties() = default;
......
...@@ -126,6 +126,9 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceClient : public BluezDBusClient { ...@@ -126,6 +126,9 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceClient : public BluezDBusClient {
// The MTU used in ATT communication with the remote device. Read-only. // The MTU used in ATT communication with the remote device. Read-only.
dbus::Property<uint16_t> mtu; dbus::Property<uint16_t> mtu;
// The EIR advertised by the remote device. Read-only.
dbus::Property<std::vector<uint8_t>> eir;
Properties(dbus::ObjectProxy* object_proxy, Properties(dbus::ObjectProxy* object_proxy,
const std::string& interface_name, const std::string& interface_name,
const PropertyChangedCallback& callback); const PropertyChangedCallback& callback);
......
...@@ -42,6 +42,8 @@ void TestBluetoothAdapterObserver::Reset() { ...@@ -42,6 +42,8 @@ void TestBluetoothAdapterObserver::Reset() {
device_new_paired_status_ = false; device_new_paired_status_ = false;
device_mtu_changed_count_ = 0; device_mtu_changed_count_ = 0;
device_mtu_ = 0; device_mtu_ = 0;
device_advertisement_received_count_ = 0;
device_eir_.clear();
#endif #endif
device_removed_count_ = 0; device_removed_count_ = 0;
last_device_ = NULL; last_device_ = NULL;
...@@ -158,6 +160,18 @@ void TestBluetoothAdapterObserver::DeviceMTUChanged( ...@@ -158,6 +160,18 @@ void TestBluetoothAdapterObserver::DeviceMTUChanged(
QuitMessageLoop(); QuitMessageLoop();
} }
void TestBluetoothAdapterObserver::DeviceAdvertisementReceived(
device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
int16_t rssi,
const std::vector<uint8_t>& eir) {
++device_advertisement_received_count_;
last_device_ = device;
device_eir_ = eir;
QuitMessageLoop();
}
#endif #endif
void TestBluetoothAdapterObserver::DeviceRemoved(BluetoothAdapter* adapter, void TestBluetoothAdapterObserver::DeviceRemoved(BluetoothAdapter* adapter,
......
...@@ -45,6 +45,10 @@ class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer { ...@@ -45,6 +45,10 @@ class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer {
void DeviceMTUChanged(device::BluetoothAdapter* adapter, void DeviceMTUChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device, device::BluetoothDevice* device,
uint16_t mtu) override; uint16_t mtu) override;
void DeviceAdvertisementReceived(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
int16_t rssi,
const std::vector<uint8_t>& eir) override;
#endif #endif
void DeviceRemoved(BluetoothAdapter* adapter, void DeviceRemoved(BluetoothAdapter* adapter,
BluetoothDevice* device) override; BluetoothDevice* device) override;
...@@ -102,6 +106,10 @@ class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer { ...@@ -102,6 +106,10 @@ class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer {
bool device_new_paired_status() const { return device_new_paired_status_; } bool device_new_paired_status() const { return device_new_paired_status_; }
int device_mtu_changed_count() const { return device_mtu_changed_count_; } int device_mtu_changed_count() const { return device_mtu_changed_count_; }
uint16_t last_mtu_value() const { return device_mtu_; } uint16_t last_mtu_value() const { return device_mtu_; }
int device_advertisement_received_count() const {
return device_advertisement_received_count_;
}
const std::vector<uint8_t>& device_eir() const { return device_eir_; }
#endif #endif
int device_removed_count() const { return device_removed_count_; } int device_removed_count() const { return device_removed_count_; }
BluetoothDevice* last_device() const { return last_device_; } BluetoothDevice* last_device() const { return last_device_; }
...@@ -187,6 +195,8 @@ class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer { ...@@ -187,6 +195,8 @@ class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer {
bool device_new_paired_status_; bool device_new_paired_status_;
int device_mtu_changed_count_; int device_mtu_changed_count_;
uint16_t device_mtu_; uint16_t device_mtu_;
int device_advertisement_received_count_;
std::vector<uint8_t> device_eir_;
#endif #endif
int device_removed_count_; int device_removed_count_;
BluetoothDevice* last_device_; BluetoothDevice* last_device_;
......
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