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_device_client.h" |
14 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" | 14 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" |
15 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
16 | 16 |
17 namespace bluez { | 17 namespace bluez { |
18 | 18 |
19 // static | 19 // static |
20 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = | 20 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = |
21 "service0000"; | 21 "service0000"; |
| 22 const char FakeBluetoothGattServiceClient::kBatteryServicePathComponent[] = |
| 23 "service0001"; |
22 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = | 24 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = |
23 "0000180d-0000-1000-8000-00805f9b34fb"; | 25 "0000180d-0000-1000-8000-00805f9b34fb"; |
| 26 const char FakeBluetoothGattServiceClient::kBatteryServiceUUID[] = |
| 27 "0000180f-0000-1000-8000-00805f9b34fb"; |
24 | 28 |
25 FakeBluetoothGattServiceClient::Properties::Properties( | 29 FakeBluetoothGattServiceClient::Properties::Properties( |
26 const PropertyChangedCallback& callback) | 30 const PropertyChangedCallback& callback) |
27 : BluetoothGattServiceClient::Properties( | 31 : BluetoothGattServiceClient::Properties( |
28 NULL, | 32 NULL, |
29 bluetooth_gatt_service::kBluetoothGattServiceInterface, | 33 bluetooth_gatt_service::kBluetoothGattServiceInterface, |
30 callback) {} | 34 callback) {} |
31 | 35 |
32 FakeBluetoothGattServiceClient::Properties::~Properties() {} | 36 FakeBluetoothGattServiceClient::Properties::~Properties() {} |
33 | 37 |
(...skipping 29 matching lines...) Expand all Loading... |
63 void FakeBluetoothGattServiceClient::RemoveObserver(Observer* observer) { | 67 void FakeBluetoothGattServiceClient::RemoveObserver(Observer* observer) { |
64 observers_.RemoveObserver(observer); | 68 observers_.RemoveObserver(observer); |
65 } | 69 } |
66 | 70 |
67 std::vector<dbus::ObjectPath> FakeBluetoothGattServiceClient::GetServices() { | 71 std::vector<dbus::ObjectPath> FakeBluetoothGattServiceClient::GetServices() { |
68 std::vector<dbus::ObjectPath> paths; | 72 std::vector<dbus::ObjectPath> paths; |
69 if (heart_rate_service_properties_.get()) { | 73 if (heart_rate_service_properties_.get()) { |
70 DCHECK(!heart_rate_service_path_.empty()); | 74 DCHECK(!heart_rate_service_path_.empty()); |
71 paths.push_back(dbus::ObjectPath(heart_rate_service_path_)); | 75 paths.push_back(dbus::ObjectPath(heart_rate_service_path_)); |
72 } | 76 } |
| 77 if (battery_service_properties_.get()) { |
| 78 DCHECK(!battery_service_path_.empty()); |
| 79 paths.push_back(dbus::ObjectPath(battery_service_path_)); |
| 80 } |
73 return paths; | 81 return paths; |
74 } | 82 } |
75 | 83 |
76 FakeBluetoothGattServiceClient::Properties* | 84 FakeBluetoothGattServiceClient::Properties* |
77 FakeBluetoothGattServiceClient::GetProperties( | 85 FakeBluetoothGattServiceClient::GetProperties( |
78 const dbus::ObjectPath& object_path) { | 86 const dbus::ObjectPath& object_path) { |
79 if (object_path.value() == heart_rate_service_path_) | 87 if (object_path.value() == heart_rate_service_path_) |
80 return heart_rate_service_properties_.get(); | 88 return heart_rate_service_properties_.get(); |
| 89 if (object_path.value() == battery_service_path_) |
| 90 return battery_service_properties_.get(); |
81 return NULL; | 91 return NULL; |
82 } | 92 } |
83 | 93 |
84 void FakeBluetoothGattServiceClient::ExposeHeartRateService( | 94 void FakeBluetoothGattServiceClient::ExposeHeartRateService( |
85 const dbus::ObjectPath& device_path) { | 95 const dbus::ObjectPath& device_path) { |
86 if (IsHeartRateVisible()) { | 96 if (IsHeartRateVisible()) { |
87 DCHECK(!heart_rate_service_path_.empty()); | 97 DCHECK(!heart_rate_service_path_.empty()); |
88 VLOG(1) << "Fake Heart Rate Service already exposed."; | 98 VLOG(1) << "Fake Heart Rate Service already exposed."; |
89 return; | 99 return; |
90 } | 100 } |
91 VLOG(2) << "Exposing fake Heart Rate Service."; | 101 VLOG(2) << "Exposing fake Heart Rate Service."; |
92 heart_rate_service_path_ = | 102 heart_rate_service_path_ = |
93 device_path.value() + "/" + kHeartRateServicePathComponent; | 103 device_path.value() + "/" + kHeartRateServicePathComponent; |
94 heart_rate_service_properties_.reset(new Properties(base::Bind( | 104 heart_rate_service_properties_.reset(new Properties(base::Bind( |
95 &FakeBluetoothGattServiceClient::OnPropertyChanged, | 105 &FakeBluetoothGattServiceClient::OnPropertyChanged, |
96 base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); | 106 base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); |
97 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); | 107 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); |
98 heart_rate_service_properties_->device.ReplaceValue(device_path); | 108 heart_rate_service_properties_->device.ReplaceValue(device_path); |
99 heart_rate_service_properties_->primary.ReplaceValue(true); | 109 heart_rate_service_properties_->primary.ReplaceValue(true); |
100 | 110 |
101 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); | 111 NotifyServiceAdded(GetHeartRateServicePath()); |
102 | 112 |
103 base::ThreadTaskRunnerHandle::Get()->PostTask( | 113 base::ThreadTaskRunnerHandle::Get()->PostTask( |
104 FROM_HERE, | 114 FROM_HERE, |
105 base::Bind( | 115 base::Bind( |
106 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, | 116 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, |
107 weak_ptr_factory_.GetWeakPtr())); | 117 weak_ptr_factory_.GetWeakPtr())); |
108 | |
109 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
110 FROM_HERE, | |
111 base::Bind( | |
112 &FakeBluetoothGattServiceClient::ToggleServicesResolvedProperty, | |
113 weak_ptr_factory_.GetWeakPtr(), device_path, true)); | |
114 } | 118 } |
115 | 119 |
116 void FakeBluetoothGattServiceClient::HideHeartRateService() { | 120 void FakeBluetoothGattServiceClient::HideHeartRateService() { |
117 if (!IsHeartRateVisible()) { | 121 if (!IsHeartRateVisible()) { |
118 DCHECK(heart_rate_service_path_.empty()); | 122 DCHECK(heart_rate_service_path_.empty()); |
119 VLOG(1) << "Fake Heart Rate Service already hidden."; | 123 VLOG(1) << "Fake Heart Rate Service already hidden."; |
120 return; | 124 return; |
121 } | 125 } |
122 VLOG(2) << "Hiding fake Heart Rate Service."; | 126 VLOG(2) << "Hiding fake Heart Rate Service."; |
123 FakeBluetoothGattCharacteristicClient* char_client = | 127 FakeBluetoothGattCharacteristicClient* char_client = |
124 static_cast<FakeBluetoothGattCharacteristicClient*>( | 128 static_cast<FakeBluetoothGattCharacteristicClient*>( |
125 bluez::BluezDBusManager::Get() | 129 bluez::BluezDBusManager::Get() |
126 ->GetBluetoothGattCharacteristicClient()); | 130 ->GetBluetoothGattCharacteristicClient()); |
127 char_client->HideHeartRateCharacteristics(); | 131 char_client->HideHeartRateCharacteristics(); |
128 | 132 |
129 // Notify observers before deleting the properties structure so that it | 133 // Notify observers before deleting the properties structure so that it |
130 // can be accessed from the observer method. | 134 // can be accessed from the observer method. |
131 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); | 135 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); |
132 | 136 |
133 heart_rate_service_properties_.reset(); | 137 heart_rate_service_properties_.reset(); |
134 heart_rate_service_path_.clear(); | 138 heart_rate_service_path_.clear(); |
135 } | 139 } |
136 | 140 |
137 void FakeBluetoothGattServiceClient::ExposeHeartRateServiceWithoutDelay( | 141 void FakeBluetoothGattServiceClient::ExposeBatteryService( |
138 const dbus::ObjectPath& device_path) { | 142 const dbus::ObjectPath& device_path) { |
139 if (IsHeartRateVisible()) { | 143 if (IsBatteryServiceVisible()) { |
140 DCHECK(!heart_rate_service_path_.empty()); | 144 DCHECK(!battery_service_path_.empty()); |
141 VLOG(1) << "Fake Heart Rate Service already exposed."; | 145 VLOG(1) << "Fake Battery Service already exposed."; |
142 return; | 146 return; |
143 } | 147 } |
144 VLOG(2) << "Exposing fake Heart Rate Service."; | 148 |
145 heart_rate_service_path_ = | 149 VLOG(2) << "Exposing fake Battery Service."; |
146 device_path.value() + "/" + kHeartRateServicePathComponent; | 150 battery_service_path_ = |
147 heart_rate_service_properties_.reset(new Properties(base::Bind( | 151 device_path.value() + "/" + kBatteryServicePathComponent; |
| 152 battery_service_properties_.reset(new Properties(base::Bind( |
148 &FakeBluetoothGattServiceClient::OnPropertyChanged, | 153 &FakeBluetoothGattServiceClient::OnPropertyChanged, |
149 base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); | 154 base::Unretained(this), dbus::ObjectPath(battery_service_path_)))); |
150 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); | 155 battery_service_properties_->uuid.ReplaceValue(kBatteryServiceUUID); |
151 heart_rate_service_properties_->device.ReplaceValue(device_path); | 156 battery_service_properties_->device.ReplaceValue(device_path); |
152 heart_rate_service_properties_->primary.ReplaceValue(true); | 157 battery_service_properties_->primary.ReplaceValue(true); |
153 | 158 |
154 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); | 159 NotifyServiceAdded(GetBatteryServicePath()); |
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 } | 160 } |
163 | 161 |
164 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { | 162 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { |
165 return !!heart_rate_service_properties_.get(); | 163 return !!heart_rate_service_properties_.get(); |
166 } | 164 } |
167 | 165 |
| 166 bool FakeBluetoothGattServiceClient::IsBatteryServiceVisible() const { |
| 167 return !!battery_service_properties_.get(); |
| 168 } |
| 169 |
168 dbus::ObjectPath FakeBluetoothGattServiceClient::GetHeartRateServicePath() | 170 dbus::ObjectPath FakeBluetoothGattServiceClient::GetHeartRateServicePath() |
169 const { | 171 const { |
170 return dbus::ObjectPath(heart_rate_service_path_); | 172 return dbus::ObjectPath(heart_rate_service_path_); |
171 } | 173 } |
172 | 174 |
| 175 dbus::ObjectPath FakeBluetoothGattServiceClient::GetBatteryServicePath() const { |
| 176 return dbus::ObjectPath(battery_service_path_); |
| 177 } |
| 178 |
173 void FakeBluetoothGattServiceClient::OnPropertyChanged( | 179 void FakeBluetoothGattServiceClient::OnPropertyChanged( |
174 const dbus::ObjectPath& object_path, | 180 const dbus::ObjectPath& object_path, |
175 const std::string& property_name) { | 181 const std::string& property_name) { |
176 VLOG(2) << "Fake GATT Service property changed: " << object_path.value() | 182 VLOG(2) << "Fake GATT Service property changed: " << object_path.value() |
177 << ": " << property_name; | 183 << ": " << property_name; |
178 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, | 184 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, |
179 GattServicePropertyChanged(object_path, property_name)); | 185 GattServicePropertyChanged(object_path, property_name)); |
180 } | 186 } |
181 | 187 |
182 void FakeBluetoothGattServiceClient::NotifyServiceAdded( | 188 void FakeBluetoothGattServiceClient::NotifyServiceAdded( |
(...skipping 16 matching lines...) Expand all Loading... |
199 return; | 205 return; |
200 } | 206 } |
201 FakeBluetoothGattCharacteristicClient* char_client = | 207 FakeBluetoothGattCharacteristicClient* char_client = |
202 static_cast<FakeBluetoothGattCharacteristicClient*>( | 208 static_cast<FakeBluetoothGattCharacteristicClient*>( |
203 bluez::BluezDBusManager::Get() | 209 bluez::BluezDBusManager::Get() |
204 ->GetBluetoothGattCharacteristicClient()); | 210 ->GetBluetoothGattCharacteristicClient()); |
205 char_client->ExposeHeartRateCharacteristics( | 211 char_client->ExposeHeartRateCharacteristics( |
206 dbus::ObjectPath(heart_rate_service_path_)); | 212 dbus::ObjectPath(heart_rate_service_path_)); |
207 } | 213 } |
208 | 214 |
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 | |
222 } // namespace bluez | 215 } // namespace bluez |
OLD | NEW |