Chromium Code Reviews| 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 namespace { |
| 19 | 20 |
| 20 const int kExposeCharacteristicsDelayIntervalMs = 100; | 21 const int kExposeCharacteristicsDelayIntervalMs = 100; |
| 22 const int kToggleServicesResolvedPropertyDelayIntervalMs = 100; | |
| 21 | 23 |
| 22 } // namespace | 24 } // namespace |
| 23 | 25 |
| 24 // static | 26 // static |
| 25 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = | 27 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = |
| 26 "service0000"; | 28 "service0000"; |
| 27 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = | 29 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = |
| 28 "0000180d-0000-1000-8000-00805f9b34fb"; | 30 "0000180d-0000-1000-8000-00805f9b34fb"; |
| 29 | 31 |
| 30 FakeBluetoothGattServiceClient::Properties::Properties( | 32 FakeBluetoothGattServiceClient::Properties::Properties( |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 heart_rate_service_properties_->primary.ReplaceValue(true); | 106 heart_rate_service_properties_->primary.ReplaceValue(true); |
| 105 | 107 |
| 106 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); | 108 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); |
| 107 | 109 |
| 108 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 110 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 109 FROM_HERE, | 111 FROM_HERE, |
| 110 base::Bind( | 112 base::Bind( |
| 111 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, | 113 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, |
| 112 weak_ptr_factory_.GetWeakPtr()), | 114 weak_ptr_factory_.GetWeakPtr()), |
| 113 base::TimeDelta::FromMilliseconds(kExposeCharacteristicsDelayIntervalMs)); | 115 base::TimeDelta::FromMilliseconds(kExposeCharacteristicsDelayIntervalMs)); |
| 116 | |
| 117 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
|
rkc
2016/05/17 18:38:50
Why does this need to be delayed at all? Can PostT
Miao
2016/05/18 03:26:58
I think PostTask is sufficient. The reason is the
| |
| 118 FROM_HERE, | |
| 119 base::Bind( | |
| 120 &FakeBluetoothGattServiceClient::ToggleServicesResolvedProperty, | |
| 121 weak_ptr_factory_.GetWeakPtr(), device_path, true), | |
| 122 base::TimeDelta::FromMilliseconds( | |
| 123 kToggleServicesResolvedPropertyDelayIntervalMs)); | |
| 114 } | 124 } |
| 115 | 125 |
| 116 void FakeBluetoothGattServiceClient::HideHeartRateService() { | 126 void FakeBluetoothGattServiceClient::HideHeartRateService() { |
| 117 if (!IsHeartRateVisible()) { | 127 if (!IsHeartRateVisible()) { |
| 118 DCHECK(heart_rate_service_path_.empty()); | 128 DCHECK(heart_rate_service_path_.empty()); |
| 119 VLOG(1) << "Fake Heart Rate Service already hidden."; | 129 VLOG(1) << "Fake Heart Rate Service already hidden."; |
| 120 return; | 130 return; |
| 121 } | 131 } |
| 122 VLOG(2) << "Hiding fake Heart Rate Service."; | 132 VLOG(2) << "Hiding fake Heart Rate Service."; |
| 123 FakeBluetoothGattCharacteristicClient* char_client = | 133 FakeBluetoothGattCharacteristicClient* char_client = |
| 124 static_cast<FakeBluetoothGattCharacteristicClient*>( | 134 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 125 bluez::BluezDBusManager::Get() | 135 bluez::BluezDBusManager::Get() |
| 126 ->GetBluetoothGattCharacteristicClient()); | 136 ->GetBluetoothGattCharacteristicClient()); |
| 127 char_client->HideHeartRateCharacteristics(); | 137 char_client->HideHeartRateCharacteristics(); |
| 128 | 138 |
| 129 // Notify observers before deleting the properties structure so that it | 139 // Notify observers before deleting the properties structure so that it |
| 130 // can be accessed from the observer method. | 140 // can be accessed from the observer method. |
| 131 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); | 141 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); |
| 132 | 142 |
| 133 heart_rate_service_properties_.reset(); | 143 heart_rate_service_properties_.reset(); |
| 134 heart_rate_service_path_.clear(); | 144 heart_rate_service_path_.clear(); |
| 135 } | 145 } |
| 136 | 146 |
| 147 void FakeBluetoothGattServiceClient::ExposeHeartRateServiceWithoutDelay( | |
| 148 const dbus::ObjectPath& device_path) { | |
| 149 if (IsHeartRateVisible()) { | |
| 150 DCHECK(!heart_rate_service_path_.empty()); | |
| 151 VLOG(1) << "Fake Heart Rate Service already exposed."; | |
| 152 return; | |
| 153 } | |
| 154 VLOG(2) << "Exposing fake Heart Rate Service."; | |
| 155 heart_rate_service_path_ = | |
| 156 device_path.value() + "/" + kHeartRateServicePathComponent; | |
| 157 heart_rate_service_properties_.reset(new Properties(base::Bind( | |
| 158 &FakeBluetoothGattServiceClient::OnPropertyChanged, | |
| 159 base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); | |
| 160 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); | |
| 161 heart_rate_service_properties_->device.ReplaceValue(device_path); | |
| 162 heart_rate_service_properties_->primary.ReplaceValue(true); | |
| 163 | |
| 164 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); | |
| 165 | |
| 166 static_cast<FakeBluetoothGattCharacteristicClient*>( | |
| 167 bluez::BluezDBusManager::Get()->GetBluetoothGattCharacteristicClient()) | |
| 168 ->ExposeHeartRateCharacteristics( | |
| 169 dbus::ObjectPath(heart_rate_service_path_)); | |
| 170 | |
| 171 ToggleServicesResolvedProperty(device_path, true); | |
| 172 } | |
| 173 | |
| 137 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { | 174 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { |
| 138 return !!heart_rate_service_properties_.get(); | 175 return !!heart_rate_service_properties_.get(); |
| 139 } | 176 } |
| 140 | 177 |
| 141 dbus::ObjectPath FakeBluetoothGattServiceClient::GetHeartRateServicePath() | 178 dbus::ObjectPath FakeBluetoothGattServiceClient::GetHeartRateServicePath() |
| 142 const { | 179 const { |
| 143 return dbus::ObjectPath(heart_rate_service_path_); | 180 return dbus::ObjectPath(heart_rate_service_path_); |
| 144 } | 181 } |
| 145 | 182 |
| 146 void FakeBluetoothGattServiceClient::OnPropertyChanged( | 183 void FakeBluetoothGattServiceClient::OnPropertyChanged( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 172 return; | 209 return; |
| 173 } | 210 } |
| 174 FakeBluetoothGattCharacteristicClient* char_client = | 211 FakeBluetoothGattCharacteristicClient* char_client = |
| 175 static_cast<FakeBluetoothGattCharacteristicClient*>( | 212 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 176 bluez::BluezDBusManager::Get() | 213 bluez::BluezDBusManager::Get() |
| 177 ->GetBluetoothGattCharacteristicClient()); | 214 ->GetBluetoothGattCharacteristicClient()); |
| 178 char_client->ExposeHeartRateCharacteristics( | 215 char_client->ExposeHeartRateCharacteristics( |
| 179 dbus::ObjectPath(heart_rate_service_path_)); | 216 dbus::ObjectPath(heart_rate_service_path_)); |
| 180 } | 217 } |
| 181 | 218 |
| 219 void FakeBluetoothGattServiceClient::ToggleServicesResolvedProperty( | |
| 220 const dbus::ObjectPath& object_path, | |
| 221 bool resolved) { | |
| 222 DCHECK(object_path.IsValid()); | |
| 223 | |
| 224 VLOG(2) << "Toggle the ServicesResolved property to " << resolved | |
| 225 << " of device " << object_path.value(); | |
| 226 FakeBluetoothDeviceClient* device = static_cast<FakeBluetoothDeviceClient*>( | |
| 227 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); | |
| 228 // Notify on service discovery complete. | |
| 229 device->GetProperties(object_path)->services_resolved.ReplaceValue(true); | |
| 230 } | |
| 231 | |
| 182 } // namespace bluez | 232 } // namespace bluez |
| OLD | NEW |