OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_low_energy_device_mac.h" | 5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
6 | 6 |
7 #import <CoreFoundation/CoreFoundation.h> | 7 #import <CoreFoundation/CoreFoundation.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // Don't create services if the device disconnected. | 199 // Don't create services if the device disconnected. |
200 return; | 200 return; |
201 } | 201 } |
202 | 202 |
203 for (CBService* cb_service in GetPeripheral().services) { | 203 for (CBService* cb_service in GetPeripheral().services) { |
204 BluetoothRemoteGattServiceMac* gatt_service = | 204 BluetoothRemoteGattServiceMac* gatt_service = |
205 GetBluetoothRemoteGattService(cb_service); | 205 GetBluetoothRemoteGattService(cb_service); |
206 if (!gatt_service) { | 206 if (!gatt_service) { |
207 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, | 207 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, |
208 true /* is_primary */); | 208 true /* is_primary */); |
209 auto result_iter = gatt_services_.add(gatt_service->GetIdentifier(), | 209 auto result_iter = gatt_services_.insert(std::make_pair( |
210 base::WrapUnique(gatt_service)); | 210 gatt_service->GetIdentifier(), base::WrapUnique(gatt_service))); |
211 DCHECK(result_iter.second); | 211 DCHECK(result_iter.second); |
212 adapter_->NotifyGattServiceAdded(gatt_service); | 212 adapter_->NotifyGattServiceAdded(gatt_service); |
213 } | 213 } |
214 } | 214 } |
215 for (GattServiceMap::const_iterator it = gatt_services_.begin(); | 215 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { |
216 it != gatt_services_.end(); ++it) { | 216 device::BluetoothRemoteGattService* gatt_service = it->second.get(); |
217 device::BluetoothRemoteGattService* gatt_service = it->second; | |
218 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 217 device::BluetoothRemoteGattServiceMac* gatt_service_mac = |
219 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 218 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
220 gatt_service_mac->DiscoverCharacteristics(); | 219 gatt_service_mac->DiscoverCharacteristics(); |
221 } | 220 } |
222 } | 221 } |
223 | 222 |
224 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( | 223 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( |
225 CBService* cb_service, | 224 CBService* cb_service, |
226 NSError* error) { | 225 NSError* error) { |
227 if (error) { | 226 if (error) { |
(...skipping 13 matching lines...) Expand all Loading... |
241 | 240 |
242 BluetoothRemoteGattServiceMac* gatt_service = | 241 BluetoothRemoteGattServiceMac* gatt_service = |
243 GetBluetoothRemoteGattService(cb_service); | 242 GetBluetoothRemoteGattService(cb_service); |
244 DCHECK(gatt_service); | 243 DCHECK(gatt_service); |
245 gatt_service->DidDiscoverCharacteristics(); | 244 gatt_service->DidDiscoverCharacteristics(); |
246 | 245 |
247 // Notify when all services have been discovered. | 246 // Notify when all services have been discovered. |
248 bool discovery_complete = | 247 bool discovery_complete = |
249 std::find_if_not( | 248 std::find_if_not( |
250 gatt_services_.begin(), gatt_services_.end(), | 249 gatt_services_.begin(), gatt_services_.end(), |
251 [](std::pair<std::string, BluetoothRemoteGattService*> pair) { | 250 [](GattServiceMap::value_type& pair) { |
252 BluetoothRemoteGattService* gatt_service = pair.second; | 251 BluetoothRemoteGattService* gatt_service = pair.second.get(); |
253 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) | 252 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) |
254 ->IsDiscoveryComplete(); | 253 ->IsDiscoveryComplete(); |
255 }) == gatt_services_.end(); | 254 }) == gatt_services_.end(); |
256 if (discovery_complete) { | 255 if (discovery_complete) { |
257 device_uuids_.ReplaceServiceUUIDs(gatt_services_); | 256 device_uuids_.ReplaceServiceUUIDs(gatt_services_); |
258 SetGattServicesDiscoveryComplete(true); | 257 SetGattServicesDiscoveryComplete(true); |
259 adapter_->NotifyGattServicesDiscovered(this); | 258 adapter_->NotifyGattServicesDiscovered(this); |
260 adapter_->NotifyDeviceChanged(this); | 259 adapter_->NotifyDeviceChanged(this); |
261 } | 260 } |
262 } | 261 } |
263 | 262 |
264 void BluetoothLowEnergyDeviceMac::DidModifyServices( | 263 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
265 NSArray* invalidatedServices) { | 264 NSArray* invalidatedServices) { |
266 VLOG(1) << "DidModifyServices: "; | 265 VLOG(1) << "DidModifyServices: "; |
267 for (CBService* cb_service in invalidatedServices) { | 266 for (CBService* cb_service in invalidatedServices) { |
268 BluetoothRemoteGattServiceMac* gatt_service = | 267 BluetoothRemoteGattServiceMac* gatt_service = |
269 GetBluetoothRemoteGattService(cb_service); | 268 GetBluetoothRemoteGattService(cb_service); |
270 DCHECK(gatt_service); | 269 DCHECK(gatt_service); |
271 VLOG(1) << gatt_service->GetUUID().canonical_value(); | 270 VLOG(1) << gatt_service->GetUUID().canonical_value(); |
272 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 271 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
273 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); | 272 std::move(gatt_services_[gatt_service->GetIdentifier()]); |
| 273 gatt_services_.erase(gatt_service->GetIdentifier()); |
274 adapter_->NotifyGattServiceRemoved(scoped_service.get()); | 274 adapter_->NotifyGattServiceRemoved(scoped_service.get()); |
275 } | 275 } |
276 device_uuids_.ClearServiceUUIDs(); | 276 device_uuids_.ClearServiceUUIDs(); |
277 SetGattServicesDiscoveryComplete(false); | 277 SetGattServicesDiscoveryComplete(false); |
278 adapter_->NotifyDeviceChanged(this); | 278 adapter_->NotifyDeviceChanged(this); |
279 [GetPeripheral() discoverServices:nil]; | 279 [GetPeripheral() discoverServices:nil]; |
280 } | 280 } |
281 | 281 |
282 void BluetoothLowEnergyDeviceMac::DidUpdateValue( | 282 void BluetoothLowEnergyDeviceMac::DidUpdateValue( |
283 CBCharacteristic* characteristic, | 283 CBCharacteristic* characteristic, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 return static_cast<BluetoothAdapterMac*>(this->adapter_); | 333 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
334 } | 334 } |
335 | 335 |
336 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { | 336 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
337 return peripheral_; | 337 return peripheral_; |
338 } | 338 } |
339 | 339 |
340 device::BluetoothRemoteGattServiceMac* | 340 device::BluetoothRemoteGattServiceMac* |
341 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( | 341 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( |
342 CBService* cb_service) const { | 342 CBService* cb_service) const { |
343 for (GattServiceMap::const_iterator it = gatt_services_.begin(); | 343 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { |
344 it != gatt_services_.end(); ++it) { | 344 device::BluetoothRemoteGattService* gatt_service = it->second.get(); |
345 device::BluetoothRemoteGattService* gatt_service = it->second; | |
346 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 345 device::BluetoothRemoteGattServiceMac* gatt_service_mac = |
347 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 346 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
348 if (gatt_service_mac->GetService() == cb_service) | 347 if (gatt_service_mac->GetService() == cb_service) |
349 return gatt_service_mac; | 348 return gatt_service_mac; |
350 } | 349 } |
351 return nullptr; | 350 return nullptr; |
352 } | 351 } |
353 | 352 |
354 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 353 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
355 SetGattServicesDiscoveryComplete(false); | 354 SetGattServicesDiscoveryComplete(false); |
356 // Removing all services at once to ensure that calling GetGattService on | 355 // Removing all services at once to ensure that calling GetGattService on |
357 // removed service in GattServiceRemoved returns null. | 356 // removed service in GattServiceRemoved returns null. |
358 GattServiceMap gatt_services_swapped; | 357 GattServiceMap gatt_services_swapped; |
359 gatt_services_swapped.swap(gatt_services_); | 358 gatt_services_swapped.swap(gatt_services_); |
360 gatt_services_swapped.clear(); | 359 gatt_services_swapped.clear(); |
361 device_uuids_.ClearServiceUUIDs(); | 360 device_uuids_.ClearServiceUUIDs(); |
362 // There are two cases in which this function will be called: | 361 // There are two cases in which this function will be called: |
363 // 1. When the connection to the device breaks (either because | 362 // 1. When the connection to the device breaks (either because |
364 // we closed it or the device closed it). | 363 // we closed it or the device closed it). |
365 // 2. When we cancel a pending connection request. | 364 // 2. When we cancel a pending connection request. |
366 if (create_gatt_connection_error_callbacks_.empty()) { | 365 if (create_gatt_connection_error_callbacks_.empty()) { |
367 // If there are no pending callbacks then the connection broke (#1). | 366 // If there are no pending callbacks then the connection broke (#1). |
368 DidDisconnectGatt(); | 367 DidDisconnectGatt(); |
369 return; | 368 return; |
370 } | 369 } |
371 // Else we canceled the connection request (#2). | 370 // Else we canceled the connection request (#2). |
372 // TODO(http://crbug.com/585897): Need to pass the error. | 371 // TODO(http://crbug.com/585897): Need to pass the error. |
373 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); | 372 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); |
374 } | 373 } |
OLD | NEW |