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 "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 9 #include "device/bluetooth/bluetooth_device_win.h" |
| 10 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
| 11 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 12 |
7 namespace device { | 13 namespace device { |
| 14 |
| 15 void BluetoothRemoteGattServiceWin::NotifyServiceDiscComplIfNecessary() { |
| 16 if (discovery_completed_included_charateristics_.size() == |
| 17 included_characteristic_.size() && |
| 18 discovery_completed_included_services_.size() == |
| 19 included_service_.size() && |
| 20 included_services_discovered_ && included_characteristics_discovered_ && |
| 21 !discovery_complete_notified_) { |
| 22 if (is_primary_) |
| 23 adapter_->NotifyGattDiscoveryComplete(this); |
| 24 else |
| 25 parent_service_->GattServiceDiscoverComplete(this); |
| 26 discovery_complete_notified_ = true; |
| 27 } |
| 28 } |
| 29 |
| 30 bool BluetoothRemoteGattServiceWin::IsCharacteristicDiscovered( |
| 31 BTH_LE_UUID& uuid, |
| 32 uint16_t attribute_handle) { |
| 33 BluetoothUUID bt_uuid = |
| 34 task_manager_->BluetoothLowEnergyUuidToBluetoothUuid(uuid); |
| 35 for (const auto& cha : included_characteristic_) { |
| 36 if (bt_uuid == cha.second->GetUUID() && |
| 37 attribute_handle == cha.second->GetAttributeHandle()) { |
| 38 return true; |
| 39 } |
| 40 } |
| 41 return false; |
| 42 } |
| 43 |
| 44 bool BluetoothRemoteGattServiceWin::DoesCharacteristicExist( |
| 45 PBTH_LE_GATT_CHARACTERISTIC characteristics, |
| 46 uint16_t num, |
| 47 BluetoothRemoteGattCharacteristicWin* characteristic) { |
| 48 for (uint16_t i = 0; i < num; i++) { |
| 49 BluetoothUUID uuid = task_manager_->BluetoothLowEnergyUuidToBluetoothUuid( |
| 50 characteristics[i].CharacteristicUuid); |
| 51 if (characteristic->GetUUID() == uuid && |
| 52 characteristic->GetAttributeHandle() == |
| 53 characteristics[i].AttributeHandle) { |
| 54 return true; |
| 55 } |
| 56 } |
| 57 return false; |
| 58 } |
| 59 |
| 60 void BluetoothRemoteGattServiceWin::RemoveIncludedCharacteristic( |
| 61 std::string identifier) { |
| 62 discovery_completed_included_charateristics_.erase(identifier); |
| 63 included_characteristic_.erase(identifier); |
| 64 } |
| 65 |
| 66 void BluetoothRemoteGattServiceWin::ClearIncludedCharacteristics() { |
| 67 discovery_completed_included_charateristics_.clear(); |
| 68 included_characteristic_.clear(); |
| 69 } |
| 70 |
| 71 void BluetoothRemoteGattServiceWin::UpdateIncludedCharacteristics( |
| 72 PBTH_LE_GATT_CHARACTERISTIC characteristics, |
| 73 uint16_t num) { |
| 74 if (num == 0) { |
| 75 ClearIncludedCharacteristics(); |
| 76 return; |
| 77 } |
| 78 |
| 79 // First, remove no longer existed characteristics. |
| 80 std::vector<std::string> to_be_removed; |
| 81 for (const auto& cha : included_characteristic_) { |
| 82 if (!DoesCharacteristicExist(characteristics, num, cha.second.get())) |
| 83 to_be_removed.push_back(cha.second->GetIdentifier()); |
| 84 } |
| 85 for (auto id : to_be_removed) { |
| 86 RemoveIncludedCharacteristic(id); |
| 87 } |
| 88 |
| 89 // Update previously known characteristics. |
| 90 for (auto& cha : included_characteristic_) |
| 91 cha.second->Update(); |
| 92 |
| 93 // Return if no new characteristics have been added. |
| 94 if (included_characteristic_.size() == num) |
| 95 return; |
| 96 |
| 97 // Add new characteristics. |
| 98 for (uint16_t i = 0; i < num; i++) { |
| 99 if (!IsCharacteristicDiscovered(characteristics[i].CharacteristicUuid, |
| 100 characteristics[i].AttributeHandle)) { |
| 101 PBTH_LE_GATT_CHARACTERISTIC info = new BTH_LE_GATT_CHARACTERISTIC(); |
| 102 *info = characteristics[i]; |
| 103 BluetoothRemoteGattCharacteristicWin* characteristic_object = |
| 104 new BluetoothRemoteGattCharacteristicWin(this, info, ui_task_runner_); |
| 105 included_characteristic_[characteristic_object->GetIdentifier()] = |
| 106 make_scoped_ptr(characteristic_object); |
| 107 } |
| 108 } |
| 109 } |
| 110 |
| 111 void BluetoothRemoteGattServiceWin::GetIncludedCharacteristicsCallback( |
| 112 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristics, |
| 113 uint16_t num, |
| 114 HRESULT hr) { |
| 115 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 116 |
| 117 UpdateIncludedCharacteristics(characteristics.get(), num); |
| 118 included_characteristics_discovered_ = true; |
| 119 NotifyServiceDiscComplIfNecessary(); |
| 120 } |
| 121 |
| 122 bool BluetoothRemoteGattServiceWin::IsServiceDiscovered( |
| 123 BluetoothUUID& uuid, |
| 124 uint16_t attribute_handle) { |
| 125 for (const auto& ser : included_service_) { |
| 126 if (uuid == ser.second->GetUUID() && |
| 127 attribute_handle == ser.second->GetAttributeHandle()) { |
| 128 return true; |
| 129 } |
| 130 } |
| 131 return false; |
| 132 } |
| 133 |
| 134 bool BluetoothRemoteGattServiceWin::DoesServiceExist( |
| 135 PBTH_LE_GATT_SERVICE services, |
| 136 uint16_t num, |
| 137 BluetoothRemoteGattServiceWin* service) { |
| 138 for (uint16_t i = 0; i < num; i++) { |
| 139 BluetoothUUID uuid = task_manager_->BluetoothLowEnergyUuidToBluetoothUuid( |
| 140 services[i].ServiceUuid); |
| 141 if (service->GetUUID() == uuid && |
| 142 service->GetAttributeHandle() == services[i].AttributeHandle) { |
| 143 return true; |
| 144 } |
| 145 } |
| 146 return false; |
| 147 } |
| 148 |
| 149 void BluetoothRemoteGattServiceWin::RemoveIncludedService( |
| 150 std::string identifier) { |
| 151 discovery_completed_included_services_.erase(identifier); |
| 152 included_service_.erase(identifier); |
| 153 } |
| 154 |
| 155 void BluetoothRemoteGattServiceWin::ClearIncludedServices() { |
| 156 discovery_completed_included_services_.clear(); |
| 157 included_service_.clear(); |
| 158 } |
| 159 |
| 160 void BluetoothRemoteGattServiceWin::UpdateIncludedServices( |
| 161 PBTH_LE_GATT_SERVICE services, |
| 162 uint16_t num) { |
| 163 if (num == 0) { |
| 164 ClearIncludedServices(); |
| 165 return; |
| 166 } |
| 167 |
| 168 // First, remove no longer exist included service. |
| 169 std::vector<std::string> to_be_removed; |
| 170 for (const auto& ser : included_service_) { |
| 171 if (!DoesServiceExist(services, num, ser.second.get())) { |
| 172 to_be_removed.push_back(ser.second->GetIdentifier()); |
| 173 } |
| 174 } |
| 175 for (auto id : to_be_removed) |
| 176 RemoveIncludedService(id); |
| 177 |
| 178 // Update previously known included services. |
| 179 for (auto& ser : included_service_) |
| 180 ser.second->Update(); |
| 181 |
| 182 // Return if no new services have been added. |
| 183 if (included_service_.size() == num) |
| 184 return; |
| 185 |
| 186 // Add new services. |
| 187 for (uint16_t i = 0; i < num; i++) { |
| 188 BluetoothUUID uuid = task_manager_->BluetoothLowEnergyUuidToBluetoothUuid( |
| 189 services[i].ServiceUuid); |
| 190 if (!IsServiceDiscovered(uuid, services[i].AttributeHandle)) { |
| 191 BluetoothRemoteGattServiceWin* service_object = |
| 192 new BluetoothRemoteGattServiceWin(device_, service_path_, uuid, |
| 193 services[i].AttributeHandle, false, |
| 194 this, ui_task_runner_); |
| 195 included_service_[service_object->GetIdentifier()] = |
| 196 make_scoped_ptr(service_object); |
| 197 } |
| 198 } |
| 199 } |
| 200 |
| 201 void BluetoothRemoteGattServiceWin::GetIncludedServicesCallback( |
| 202 scoped_ptr<BTH_LE_GATT_SERVICE> services, |
| 203 uint16_t num, |
| 204 HRESULT hr) { |
| 205 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 206 |
| 207 UpdateIncludedServices(services.get(), num); |
| 208 included_services_discovered_ = true; |
| 209 NotifyServiceDiscComplIfNecessary(); |
| 210 } |
| 211 |
| 212 void BluetoothRemoteGattServiceWin::GattServiceDiscoverComplete( |
| 213 BluetoothRemoteGattServiceWin* service) { |
| 214 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 215 DCHECK(included_service_.find(service->GetIdentifier()) != |
| 216 included_service_.end()); |
| 217 |
| 218 discovery_completed_included_services_.insert(service->GetIdentifier()); |
| 219 NotifyServiceDiscComplIfNecessary(); |
| 220 } |
| 221 |
| 222 void BluetoothRemoteGattServiceWin::GattCharacteristicDiscoverComplete( |
| 223 BluetoothRemoteGattCharacteristicWin* characteristic) { |
| 224 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 225 DCHECK(included_characteristic_.find(characteristic->GetIdentifier()) != |
| 226 included_characteristic_.end()); |
| 227 |
| 228 discovery_completed_included_charateristics_.insert( |
| 229 characteristic->GetIdentifier()); |
| 230 adapter_->NotifyGattCharacteristicAdded(characteristic); |
| 231 NotifyServiceDiscComplIfNecessary(); |
| 232 } |
| 233 |
8 BluetoothRemoteGattServiceWin::BluetoothRemoteGattServiceWin( | 234 BluetoothRemoteGattServiceWin::BluetoothRemoteGattServiceWin( |
9 BluetoothDeviceWin* device, | 235 BluetoothDeviceWin* device, |
10 base::FilePath service_path, | 236 base::FilePath service_path, |
11 BluetoothUUID service_uuid, | 237 BluetoothUUID service_uuid, |
12 uint16_t service_attribute_handle, | 238 uint16_t service_attribute_handle, |
13 bool is_primary, | 239 bool is_primary, |
14 BluetoothRemoteGattServiceWin* parent_service, | 240 BluetoothRemoteGattServiceWin* parent_service, |
15 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) | 241 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) |
16 : service_path_(service_path), | 242 : device_(device), |
17 device_(device), | 243 service_path_(service_path), |
18 service_uuid_(service_uuid), | 244 service_uuid_(service_uuid), |
19 service_attribute_handle_(service_attribute_handle), | 245 service_attribute_handle_(service_attribute_handle), |
20 is_primary_(is_primary), | 246 is_primary_(is_primary), |
21 parent_service_(parent_service), | 247 parent_service_(parent_service), |
22 ui_task_runner_(ui_task_runner) { | 248 ui_task_runner_(ui_task_runner), |
| 249 discovery_complete_notified_(false), |
| 250 weak_ptr_factory_(this) { |
23 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 251 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
24 DCHECK(!service_path_.empty()); | 252 DCHECK(!service_path_.empty()); |
25 DCHECK(service_uuid_.IsValid()); | 253 DCHECK(service_uuid_.IsValid()); |
26 DCHECK(service_attribute_handle_); | 254 DCHECK(service_attribute_handle_); |
27 DCHECK(device_); | 255 DCHECK(device_); |
28 if (!is_primary_) | 256 if (!is_primary_) |
29 DCHECK(parent_service_); | 257 DCHECK(parent_service_); |
| 258 adapter_ = static_cast<BluetoothAdapterWin*>(device_->GetAdapter()); |
| 259 DCHECK(adapter_); |
| 260 task_manager_ = adapter_->GetWinBluetoothTaskManager(); |
| 261 DCHECK(task_manager_); |
| 262 |
30 Update(); | 263 Update(); |
31 } | 264 } |
32 | 265 |
33 BluetoothRemoteGattServiceWin::~BluetoothRemoteGattServiceWin() {} | 266 BluetoothRemoteGattServiceWin::~BluetoothRemoteGattServiceWin() { |
| 267 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 268 |
| 269 // Only notify adapter for primary service. |
| 270 if (is_primary_) |
| 271 adapter_->NotifyGattServiceRemoved(this); |
| 272 } |
34 | 273 |
35 std::string BluetoothRemoteGattServiceWin::GetIdentifier() const { | 274 std::string BluetoothRemoteGattServiceWin::GetIdentifier() const { |
36 std::string identifier = | 275 std::string identifier = |
37 service_uuid_.value() + "_" + std::to_string(service_attribute_handle_); | 276 service_uuid_.value() + "_" + std::to_string(service_attribute_handle_); |
38 if (is_primary_) | 277 if (is_primary_) |
39 return device_->GetIdentifier() + "/" + identifier; | 278 return device_->GetIdentifier() + "/" + identifier; |
40 else | 279 else |
41 return parent_service_->GetIdentifier() + "/" + identifier; | 280 return parent_service_->GetIdentifier() + "/" + identifier; |
42 } | 281 } |
43 | 282 |
44 BluetoothUUID BluetoothRemoteGattServiceWin::GetUUID() const { | 283 BluetoothUUID BluetoothRemoteGattServiceWin::GetUUID() const { |
45 return const_cast<BluetoothUUID&>(service_uuid_); | 284 return const_cast<BluetoothUUID&>(service_uuid_); |
46 } | 285 } |
47 | 286 |
48 bool BluetoothRemoteGattServiceWin::IsLocal() const { | 287 bool BluetoothRemoteGattServiceWin::IsLocal() const { |
49 return false; | 288 return false; |
50 } | 289 } |
51 | 290 |
52 bool BluetoothRemoteGattServiceWin::IsPrimary() const { | 291 bool BluetoothRemoteGattServiceWin::IsPrimary() const { |
53 return is_primary_; | 292 return is_primary_; |
54 } | 293 } |
55 | 294 |
56 BluetoothDevice* BluetoothRemoteGattServiceWin::GetDevice() const { | 295 BluetoothDevice* BluetoothRemoteGattServiceWin::GetDevice() const { |
57 return device_; | 296 return device_; |
58 } | 297 } |
59 | 298 |
60 std::vector<BluetoothGattCharacteristic*> | 299 std::vector<BluetoothGattCharacteristic*> |
61 BluetoothRemoteGattServiceWin::GetCharacteristics() const { | 300 BluetoothRemoteGattServiceWin::GetCharacteristics() const { |
62 NOTIMPLEMENTED(); | 301 std::vector<BluetoothGattCharacteristic*> has_characteristics; |
63 return std::vector<BluetoothGattCharacteristic*>(); | 302 for (const auto& cha : included_characteristic_) |
| 303 has_characteristics.push_back(cha.second.get()); |
| 304 return has_characteristics; |
64 } | 305 } |
65 | 306 |
66 std::vector<BluetoothGattService*> | 307 std::vector<BluetoothGattService*> |
67 BluetoothRemoteGattServiceWin::GetIncludedServices() const { | 308 BluetoothRemoteGattServiceWin::GetIncludedServices() const { |
68 NOTIMPLEMENTED(); | 309 std::vector<BluetoothGattService*> has_services; |
69 return std::vector<BluetoothGattService*>(); | 310 for (const auto& ser : included_service_) |
| 311 has_services.push_back(ser.second.get()); |
| 312 return has_services; |
70 } | 313 } |
71 | 314 |
72 BluetoothGattCharacteristic* BluetoothRemoteGattServiceWin::GetCharacteristic( | 315 BluetoothGattCharacteristic* BluetoothRemoteGattServiceWin::GetCharacteristic( |
73 const std::string& identifier) const { | 316 const std::string& identifier) const { |
74 NOTIMPLEMENTED(); | 317 GattCharacteristicsMap::const_iterator it = |
| 318 included_characteristic_.find(identifier); |
| 319 if (it != included_characteristic_.end()) |
| 320 return it->second.get(); |
75 return nullptr; | 321 return nullptr; |
76 } | 322 } |
77 | 323 |
78 bool BluetoothRemoteGattServiceWin::AddCharacteristic( | 324 bool BluetoothRemoteGattServiceWin::AddCharacteristic( |
79 device::BluetoothGattCharacteristic* characteristic) { | 325 device::BluetoothGattCharacteristic* characteristic) { |
80 NOTIMPLEMENTED(); | 326 NOTIMPLEMENTED(); |
81 return false; | 327 return false; |
82 } | 328 } |
83 | 329 |
84 bool BluetoothRemoteGattServiceWin::AddIncludedService( | 330 bool BluetoothRemoteGattServiceWin::AddIncludedService( |
(...skipping 11 matching lines...) Expand all Loading... |
96 | 342 |
97 void BluetoothRemoteGattServiceWin::Unregister( | 343 void BluetoothRemoteGattServiceWin::Unregister( |
98 const base::Closure& callback, | 344 const base::Closure& callback, |
99 const ErrorCallback& error_callback) { | 345 const ErrorCallback& error_callback) { |
100 NOTIMPLEMENTED(); | 346 NOTIMPLEMENTED(); |
101 error_callback.Run(); | 347 error_callback.Run(); |
102 } | 348 } |
103 | 349 |
104 void BluetoothRemoteGattServiceWin::Update() { | 350 void BluetoothRemoteGattServiceWin::Update() { |
105 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 351 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
106 NOTIMPLEMENTED(); | 352 |
| 353 task_manager_->PostGetGattIncludedServices( |
| 354 service_path_, service_uuid_, service_attribute_handle_, |
| 355 base::Bind(&BluetoothRemoteGattServiceWin::GetIncludedServicesCallback, |
| 356 weak_ptr_factory_.GetWeakPtr())); |
| 357 task_manager_->PostGetGattIncludedCharacteristics( |
| 358 service_path_, service_uuid_, service_attribute_handle_, |
| 359 base::Bind( |
| 360 &BluetoothRemoteGattServiceWin::GetIncludedCharacteristicsCallback, |
| 361 weak_ptr_factory_.GetWeakPtr())); |
107 } | 362 } |
108 | 363 |
109 uint16_t BluetoothRemoteGattServiceWin::GetAttributeHandle() { | 364 } // namespace device. |
110 return service_attribute_handle_; | |
111 } | |
112 | |
113 } // namespace device. | |
OLD | NEW |