| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "dbus/object_path.h" | 14 #include "dbus/object_path.h" |
| 15 #include "device/bluetooth/bluetooth_adapter.h" | 15 #include "device/bluetooth/bluetooth_adapter.h" |
| 16 #include "device/bluetooth/bluetooth_adapter_factory.h" | 16 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 17 #include "device/bluetooth/bluetooth_device.h" | 17 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "device/bluetooth/bluetooth_discovery_filter.h" |
| 18 #include "device/bluetooth/bluetooth_gatt_connection.h" | 19 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 19 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 20 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 20 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 21 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 21 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" | 22 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" |
| 22 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 23 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
| 23 #include "device/bluetooth/bluetooth_uuid.h" | 24 #include "device/bluetooth/bluetooth_uuid.h" |
| 24 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 25 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 25 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | 26 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" |
| 26 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" | 27 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" |
| 27 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | 28 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" |
| 28 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" | 29 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" |
| 29 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h" | 30 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h" |
| 30 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" | 31 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" |
| 31 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" | 32 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" |
| 32 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 33 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 35 |
| 35 using device::BluetoothAdapter; | 36 using device::BluetoothAdapter; |
| 36 using device::BluetoothDevice; | 37 using device::BluetoothDevice; |
| 38 using device::BluetoothDiscoveryFilter; |
| 37 using device::BluetoothRemoteGattCharacteristic; | 39 using device::BluetoothRemoteGattCharacteristic; |
| 38 using device::BluetoothGattConnection; | 40 using device::BluetoothGattConnection; |
| 39 using device::BluetoothRemoteGattDescriptor; | 41 using device::BluetoothRemoteGattDescriptor; |
| 40 using device::BluetoothRemoteGattService; | 42 using device::BluetoothRemoteGattService; |
| 41 using device::BluetoothGattNotifySession; | 43 using device::BluetoothGattNotifySession; |
| 42 using device::BluetoothUUID; | 44 using device::BluetoothUUID; |
| 43 using device::TestBluetoothAdapterObserver; | 45 using device::TestBluetoothAdapterObserver; |
| 44 | 46 |
| 47 using UUIDSet = device::BluetoothDevice::UUIDSet; |
| 48 |
| 49 typedef std::unordered_map<device::BluetoothDevice*, |
| 50 device::BluetoothDevice::UUIDSet> |
| 51 DeviceToUUIDs; |
| 52 |
| 45 namespace bluez { | 53 namespace bluez { |
| 46 | 54 |
| 47 namespace { | 55 namespace { |
| 48 | 56 |
| 57 const BluetoothUUID kGenericAccessServiceUUID( |
| 58 bluez::FakeBluetoothGattServiceClient::kGenericAccessServiceUUID); |
| 59 const BluetoothUUID kBatteryServiceUUID( |
| 60 bluez::FakeBluetoothGattServiceClient::kBatteryServiceUUID); |
| 61 const BluetoothUUID kHeartRateServiceUUID( |
| 62 bluez::FakeBluetoothGattServiceClient::kHeartRateServiceUUID); |
| 49 const BluetoothUUID kHeartRateMeasurementUUID( | 63 const BluetoothUUID kHeartRateMeasurementUUID( |
| 50 bluez::FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID); | 64 bluez::FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID); |
| 51 const BluetoothUUID kBodySensorLocationUUID( | 65 const BluetoothUUID kBodySensorLocationUUID( |
| 52 bluez::FakeBluetoothGattCharacteristicClient::kBodySensorLocationUUID); | 66 bluez::FakeBluetoothGattCharacteristicClient::kBodySensorLocationUUID); |
| 53 const BluetoothUUID kHeartRateControlPointUUID( | 67 const BluetoothUUID kHeartRateControlPointUUID( |
| 54 bluez::FakeBluetoothGattCharacteristicClient::kHeartRateControlPointUUID); | 68 bluez::FakeBluetoothGattCharacteristicClient::kHeartRateControlPointUUID); |
| 55 | 69 |
| 56 // Compares GATT characteristic/descriptor values. Returns true, if the values | 70 // Compares GATT characteristic/descriptor values. Returns true, if the values |
| 57 // are equal. | 71 // are equal. |
| 58 bool ValuesEqual(const std::vector<uint8_t>& value0, | 72 bool ValuesEqual(const std::vector<uint8_t>& value0, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 136 |
| 123 void GetAdapter() { | 137 void GetAdapter() { |
| 124 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | 138 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 125 &BluetoothGattBlueZTest::AdapterCallback, base::Unretained(this))); | 139 &BluetoothGattBlueZTest::AdapterCallback, base::Unretained(this))); |
| 126 base::RunLoop().Run(); | 140 base::RunLoop().Run(); |
| 127 ASSERT_TRUE(adapter_.get() != NULL); | 141 ASSERT_TRUE(adapter_.get() != NULL); |
| 128 ASSERT_TRUE(adapter_->IsInitialized()); | 142 ASSERT_TRUE(adapter_->IsInitialized()); |
| 129 ASSERT_TRUE(adapter_->IsPresent()); | 143 ASSERT_TRUE(adapter_->IsPresent()); |
| 130 } | 144 } |
| 131 | 145 |
| 146 BluetoothDevice* AddLeDevice() { |
| 147 fake_bluetooth_device_client_->CreateDevice( |
| 148 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), |
| 149 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 150 bluez::FakeBluetoothDeviceClient::Properties* properties1 = |
| 151 fake_bluetooth_device_client_->GetProperties( |
| 152 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 153 properties1->connected.ReplaceValue(true); |
| 154 |
| 155 return adapter_->GetDevice( |
| 156 bluez::FakeBluetoothDeviceClient::kLowEnergyAddress); |
| 157 } |
| 158 |
| 159 BluetoothDevice* AddDualDevice() { |
| 160 fake_bluetooth_device_client_->CreateDevice( |
| 161 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), |
| 162 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kDualPath)); |
| 163 bluez::FakeBluetoothDeviceClient::Properties* properties2 = |
| 164 fake_bluetooth_device_client_->GetProperties( |
| 165 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kDualPath)); |
| 166 properties2->connected.ReplaceValue(true); |
| 167 |
| 168 return adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kDualAddress); |
| 169 }; |
| 170 |
| 132 void BatteryServiceShouldBeComplete(BluetoothDevice* device) { | 171 void BatteryServiceShouldBeComplete(BluetoothDevice* device) { |
| 133 ASSERT_TRUE(device); | 172 ASSERT_TRUE(device); |
| 134 ASSERT_GE(device->GetGattServices().size(), 1u); | 173 ASSERT_GE(device->GetGattServices().size(), 1u); |
| 135 | 174 |
| 136 BluetoothRemoteGattService* service = device->GetGattService( | 175 BluetoothRemoteGattService* service = device->GetGattService( |
| 137 fake_bluetooth_gatt_service_client_->GetBatteryServicePath().value()); | 176 fake_bluetooth_gatt_service_client_->GetBatteryServicePath().value()); |
| 138 | 177 |
| 139 ASSERT_TRUE(service); | 178 ASSERT_TRUE(service); |
| 140 EXPECT_TRUE(service->IsPrimary()); | 179 EXPECT_TRUE(service->IsPrimary()); |
| 141 EXPECT_EQ(BluetoothUUID( | 180 EXPECT_EQ(BluetoothUUID( |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 std::unique_ptr<device::BluetoothGattConnection> gatt_conn_; | 297 std::unique_ptr<device::BluetoothGattConnection> gatt_conn_; |
| 259 ScopedVector<BluetoothGattNotifySession> update_sessions_; | 298 ScopedVector<BluetoothGattNotifySession> update_sessions_; |
| 260 scoped_refptr<BluetoothAdapter> adapter_; | 299 scoped_refptr<BluetoothAdapter> adapter_; |
| 261 | 300 |
| 262 int success_callback_count_; | 301 int success_callback_count_; |
| 263 int error_callback_count_; | 302 int error_callback_count_; |
| 264 std::vector<uint8_t> last_read_value_; | 303 std::vector<uint8_t> last_read_value_; |
| 265 BluetoothRemoteGattService::GattErrorCode last_service_error_; | 304 BluetoothRemoteGattService::GattErrorCode last_service_error_; |
| 266 }; | 305 }; |
| 267 | 306 |
| 307 TEST_F(BluetoothGattBlueZTest, |
| 308 RetrieveGattConnectedDevicesWithDiscoveryFilter_NoFilter) { |
| 309 BluetoothDevice* le = AddLeDevice(); |
| 310 BluetoothDevice* dual = AddDualDevice(); |
| 311 |
| 312 BluetoothDiscoveryFilter discovery_filter(device::BLUETOOTH_TRANSPORT_LE); |
| 313 DeviceToUUIDs result = |
| 314 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| 315 discovery_filter); |
| 316 |
| 317 EXPECT_EQ(DeviceToUUIDs({{le, UUIDSet()}, {dual, UUIDSet()}}), result); |
| 318 } |
| 319 |
| 320 TEST_F(BluetoothGattBlueZTest, |
| 321 RetrieveGattConnectedDevicesWithDiscoveryFilter_NonMatchingFilter) { |
| 322 AddLeDevice(); |
| 323 AddDualDevice(); |
| 324 |
| 325 BluetoothDiscoveryFilter discovery_filter(device::BLUETOOTH_TRANSPORT_LE); |
| 326 discovery_filter.AddUUID(kBatteryServiceUUID); |
| 327 |
| 328 DeviceToUUIDs result = |
| 329 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| 330 discovery_filter); |
| 331 |
| 332 EXPECT_TRUE(result.empty()); |
| 333 } |
| 334 |
| 335 TEST_F( |
| 336 BluetoothGattBlueZTest, |
| 337 RetrieveGattConnectedDevicesWithDiscoveryFilter_OneMatchingServiceOneDevice)
{ |
| 338 AddLeDevice(); |
| 339 BluetoothDevice* dual = AddDualDevice(); |
| 340 |
| 341 BluetoothDiscoveryFilter discovery_filter(device::BLUETOOTH_TRANSPORT_LE); |
| 342 discovery_filter.AddUUID(kGenericAccessServiceUUID); |
| 343 |
| 344 DeviceToUUIDs result = |
| 345 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| 346 discovery_filter); |
| 347 |
| 348 EXPECT_EQ(DeviceToUUIDs({{dual, UUIDSet({kGenericAccessServiceUUID})}}), |
| 349 result); |
| 350 } |
| 351 |
| 352 TEST_F( |
| 353 BluetoothGattBlueZTest, |
| 354 RetrieveGattConnectedDevicesWithDiscoveryFilter_OneMatchingServiceTwoDevices
) { |
| 355 BluetoothDevice* le = AddLeDevice(); |
| 356 BluetoothDevice* dual = AddDualDevice(); |
| 357 |
| 358 BluetoothDiscoveryFilter discovery_filter(device::BLUETOOTH_TRANSPORT_LE); |
| 359 discovery_filter.AddUUID(kHeartRateServiceUUID); |
| 360 |
| 361 DeviceToUUIDs result = |
| 362 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| 363 discovery_filter); |
| 364 |
| 365 EXPECT_EQ(DeviceToUUIDs({{le, UUIDSet({kHeartRateServiceUUID})}, |
| 366 {dual, UUIDSet({kHeartRateServiceUUID})}}), |
| 367 result); |
| 368 } |
| 369 |
| 370 TEST_F(BluetoothGattBlueZTest, |
| 371 RetrieveGattConnectedDevicesWithDiscoveryFilter_TwoServicesTwoDevices) { |
| 372 BluetoothDevice* le = AddLeDevice(); |
| 373 BluetoothDevice* dual = AddDualDevice(); |
| 374 |
| 375 BluetoothDiscoveryFilter discovery_filter(device::BLUETOOTH_TRANSPORT_LE); |
| 376 discovery_filter.AddUUID(kGenericAccessServiceUUID); |
| 377 discovery_filter.AddUUID(kHeartRateServiceUUID); |
| 378 |
| 379 DeviceToUUIDs result = |
| 380 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| 381 discovery_filter); |
| 382 |
| 383 EXPECT_EQ(DeviceToUUIDs({{le, UUIDSet({kHeartRateServiceUUID})}, |
| 384 {dual, UUIDSet({kHeartRateServiceUUID, |
| 385 kGenericAccessServiceUUID})}}), |
| 386 result); |
| 387 } |
| 388 |
| 389 TEST_F( |
| 390 BluetoothGattBlueZTest, |
| 391 RetrieveGattConnectedDevicesWithDiscoveryFilter_OneMatchingServiceOneNonMatc
hingService) { |
| 392 AddLeDevice(); |
| 393 BluetoothDevice* dual = AddDualDevice(); |
| 394 BluetoothDiscoveryFilter discovery_filter(device::BLUETOOTH_TRANSPORT_LE); |
| 395 discovery_filter.AddUUID(kGenericAccessServiceUUID); |
| 396 discovery_filter.AddUUID(kBatteryServiceUUID); |
| 397 |
| 398 DeviceToUUIDs result = |
| 399 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| 400 discovery_filter); |
| 401 |
| 402 EXPECT_EQ(DeviceToUUIDs({{dual, UUIDSet({kGenericAccessServiceUUID})}}), |
| 403 result); |
| 404 } |
| 405 |
| 268 TEST_F(BluetoothGattBlueZTest, GattConnection) { | 406 TEST_F(BluetoothGattBlueZTest, GattConnection) { |
| 269 fake_bluetooth_device_client_->CreateDevice( | 407 fake_bluetooth_device_client_->CreateDevice( |
| 270 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | 408 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), |
| 271 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); | 409 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 272 BluetoothDevice* device = | 410 BluetoothDevice* device = |
| 273 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress); | 411 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress); |
| 274 ASSERT_TRUE(device); | 412 ASSERT_TRUE(device); |
| 275 ASSERT_FALSE(device->IsConnected()); | 413 ASSERT_FALSE(device->IsConnected()); |
| 276 ASSERT_FALSE(device->IsGattConnected()); | 414 ASSERT_FALSE(device->IsGattConnected()); |
| 277 ASSERT_FALSE(gatt_conn_.get()); | 415 ASSERT_FALSE(gatt_conn_.get()); |
| (...skipping 1201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1479 | 1617 |
| 1480 EXPECT_EQ(1, success_callback_count_); | 1618 EXPECT_EQ(1, success_callback_count_); |
| 1481 EXPECT_EQ(0, error_callback_count_); | 1619 EXPECT_EQ(0, error_callback_count_); |
| 1482 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); | 1620 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); |
| 1483 EXPECT_TRUE(characteristic->IsNotifying()); | 1621 EXPECT_TRUE(characteristic->IsNotifying()); |
| 1484 EXPECT_EQ(1U, update_sessions_.size()); | 1622 EXPECT_EQ(1U, update_sessions_.size()); |
| 1485 EXPECT_TRUE(update_sessions_[0]->IsActive()); | 1623 EXPECT_TRUE(update_sessions_[0]->IsActive()); |
| 1486 } | 1624 } |
| 1487 | 1625 |
| 1488 } // namespace bluez | 1626 } // namespace bluez |
| OLD | NEW |