| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" | 5 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 12 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 13 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" |
| 13 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" | 14 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" |
| 14 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 15 | 16 |
| 16 namespace bluez { | 17 namespace bluez { |
| 17 | 18 |
| 18 namespace { | |
| 19 | |
| 20 const int kExposeCharacteristicsDelayIntervalMs = 100; | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 // static | 19 // static |
| 25 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = | 20 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = |
| 26 "service0000"; | 21 "service0000"; |
| 27 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = | 22 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = |
| 28 "0000180d-0000-1000-8000-00805f9b34fb"; | 23 "0000180d-0000-1000-8000-00805f9b34fb"; |
| 29 | 24 |
| 30 FakeBluetoothGattServiceClient::Properties::Properties( | 25 FakeBluetoothGattServiceClient::Properties::Properties( |
| 31 const PropertyChangedCallback& callback) | 26 const PropertyChangedCallback& callback) |
| 32 : BluetoothGattServiceClient::Properties( | 27 : BluetoothGattServiceClient::Properties( |
| 33 NULL, | 28 NULL, |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 device_path.value() + "/" + kHeartRateServicePathComponent; | 93 device_path.value() + "/" + kHeartRateServicePathComponent; |
| 99 heart_rate_service_properties_.reset(new Properties(base::Bind( | 94 heart_rate_service_properties_.reset(new Properties(base::Bind( |
| 100 &FakeBluetoothGattServiceClient::OnPropertyChanged, | 95 &FakeBluetoothGattServiceClient::OnPropertyChanged, |
| 101 base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); | 96 base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); |
| 102 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); | 97 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); |
| 103 heart_rate_service_properties_->device.ReplaceValue(device_path); | 98 heart_rate_service_properties_->device.ReplaceValue(device_path); |
| 104 heart_rate_service_properties_->primary.ReplaceValue(true); | 99 heart_rate_service_properties_->primary.ReplaceValue(true); |
| 105 | 100 |
| 106 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); | 101 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); |
| 107 | 102 |
| 108 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 103 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 109 FROM_HERE, | 104 FROM_HERE, |
| 110 base::Bind( | 105 base::Bind( |
| 111 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, | 106 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, |
| 112 weak_ptr_factory_.GetWeakPtr()), | 107 weak_ptr_factory_.GetWeakPtr())); |
| 113 base::TimeDelta::FromMilliseconds(kExposeCharacteristicsDelayIntervalMs)); | 108 |
| 109 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 110 FROM_HERE, |
| 111 base::Bind( |
| 112 &FakeBluetoothGattServiceClient::ToggleServicesResolvedProperty, |
| 113 weak_ptr_factory_.GetWeakPtr(), device_path, true)); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void FakeBluetoothGattServiceClient::HideHeartRateService() { | 116 void FakeBluetoothGattServiceClient::HideHeartRateService() { |
| 117 if (!IsHeartRateVisible()) { | 117 if (!IsHeartRateVisible()) { |
| 118 DCHECK(heart_rate_service_path_.empty()); | 118 DCHECK(heart_rate_service_path_.empty()); |
| 119 VLOG(1) << "Fake Heart Rate Service already hidden."; | 119 VLOG(1) << "Fake Heart Rate Service already hidden."; |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 VLOG(2) << "Hiding fake Heart Rate Service."; | 122 VLOG(2) << "Hiding fake Heart Rate Service."; |
| 123 FakeBluetoothGattCharacteristicClient* char_client = | 123 FakeBluetoothGattCharacteristicClient* char_client = |
| 124 static_cast<FakeBluetoothGattCharacteristicClient*>( | 124 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 125 bluez::BluezDBusManager::Get() | 125 bluez::BluezDBusManager::Get() |
| 126 ->GetBluetoothGattCharacteristicClient()); | 126 ->GetBluetoothGattCharacteristicClient()); |
| 127 char_client->HideHeartRateCharacteristics(); | 127 char_client->HideHeartRateCharacteristics(); |
| 128 | 128 |
| 129 // Notify observers before deleting the properties structure so that it | 129 // Notify observers before deleting the properties structure so that it |
| 130 // can be accessed from the observer method. | 130 // can be accessed from the observer method. |
| 131 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); | 131 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); |
| 132 | 132 |
| 133 heart_rate_service_properties_.reset(); | 133 heart_rate_service_properties_.reset(); |
| 134 heart_rate_service_path_.clear(); | 134 heart_rate_service_path_.clear(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void FakeBluetoothGattServiceClient::ExposeHeartRateServiceWithoutDelay( |
| 138 const dbus::ObjectPath& device_path) { |
| 139 if (IsHeartRateVisible()) { |
| 140 DCHECK(!heart_rate_service_path_.empty()); |
| 141 VLOG(1) << "Fake Heart Rate Service already exposed."; |
| 142 return; |
| 143 } |
| 144 VLOG(2) << "Exposing fake Heart Rate Service."; |
| 145 heart_rate_service_path_ = |
| 146 device_path.value() + "/" + kHeartRateServicePathComponent; |
| 147 heart_rate_service_properties_.reset(new Properties(base::Bind( |
| 148 &FakeBluetoothGattServiceClient::OnPropertyChanged, |
| 149 base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); |
| 150 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); |
| 151 heart_rate_service_properties_->device.ReplaceValue(device_path); |
| 152 heart_rate_service_properties_->primary.ReplaceValue(true); |
| 153 |
| 154 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); |
| 155 |
| 156 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 157 bluez::BluezDBusManager::Get()->GetBluetoothGattCharacteristicClient()) |
| 158 ->ExposeHeartRateCharacteristics( |
| 159 dbus::ObjectPath(heart_rate_service_path_)); |
| 160 |
| 161 ToggleServicesResolvedProperty(device_path, true); |
| 162 } |
| 163 |
| 137 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { | 164 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { |
| 138 return !!heart_rate_service_properties_.get(); | 165 return !!heart_rate_service_properties_.get(); |
| 139 } | 166 } |
| 140 | 167 |
| 141 dbus::ObjectPath FakeBluetoothGattServiceClient::GetHeartRateServicePath() | 168 dbus::ObjectPath FakeBluetoothGattServiceClient::GetHeartRateServicePath() |
| 142 const { | 169 const { |
| 143 return dbus::ObjectPath(heart_rate_service_path_); | 170 return dbus::ObjectPath(heart_rate_service_path_); |
| 144 } | 171 } |
| 145 | 172 |
| 146 void FakeBluetoothGattServiceClient::OnPropertyChanged( | 173 void FakeBluetoothGattServiceClient::OnPropertyChanged( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 172 return; | 199 return; |
| 173 } | 200 } |
| 174 FakeBluetoothGattCharacteristicClient* char_client = | 201 FakeBluetoothGattCharacteristicClient* char_client = |
| 175 static_cast<FakeBluetoothGattCharacteristicClient*>( | 202 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 176 bluez::BluezDBusManager::Get() | 203 bluez::BluezDBusManager::Get() |
| 177 ->GetBluetoothGattCharacteristicClient()); | 204 ->GetBluetoothGattCharacteristicClient()); |
| 178 char_client->ExposeHeartRateCharacteristics( | 205 char_client->ExposeHeartRateCharacteristics( |
| 179 dbus::ObjectPath(heart_rate_service_path_)); | 206 dbus::ObjectPath(heart_rate_service_path_)); |
| 180 } | 207 } |
| 181 | 208 |
| 209 void FakeBluetoothGattServiceClient::ToggleServicesResolvedProperty( |
| 210 const dbus::ObjectPath& object_path, |
| 211 bool resolved) { |
| 212 DCHECK(object_path.IsValid()); |
| 213 |
| 214 VLOG(2) << "Toggle the ServicesResolved property to " << resolved |
| 215 << " of device " << object_path.value(); |
| 216 FakeBluetoothDeviceClient* device = static_cast<FakeBluetoothDeviceClient*>( |
| 217 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); |
| 218 // Notify on service discovery complete. |
| 219 device->GetProperties(object_path)->services_resolved.ReplaceValue(true); |
| 220 } |
| 221 |
| 182 } // namespace bluez | 222 } // namespace bluez |
| OLD | NEW |