| 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 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 235 |
| 236 if (!IsGattConnected()) { | 236 if (!IsGattConnected()) { |
| 237 // Don't create characteristics if the device disconnected. | 237 // Don't create characteristics if the device disconnected. |
| 238 return; | 238 return; |
| 239 } | 239 } |
| 240 | 240 |
| 241 BluetoothRemoteGattServiceMac* gatt_service = | 241 BluetoothRemoteGattServiceMac* gatt_service = |
| 242 GetBluetoothRemoteGattService(cb_service); | 242 GetBluetoothRemoteGattService(cb_service); |
| 243 DCHECK(gatt_service); | 243 DCHECK(gatt_service); |
| 244 gatt_service->DidDiscoverCharacteristics(); | 244 gatt_service->DidDiscoverCharacteristics(); |
| 245 | 245 SendNotificationIfDiscoveryComplete(); |
| 246 // Notify when all services have been discovered. | |
| 247 bool discovery_complete = | |
| 248 std::find_if_not( | |
| 249 gatt_services_.begin(), gatt_services_.end(), | |
| 250 [](GattServiceMap::value_type& pair) { | |
| 251 BluetoothRemoteGattService* gatt_service = pair.second.get(); | |
| 252 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) | |
| 253 ->IsDiscoveryComplete(); | |
| 254 }) == gatt_services_.end(); | |
| 255 if (discovery_complete) { | |
| 256 device_uuids_.ReplaceServiceUUIDs(gatt_services_); | |
| 257 SetGattServicesDiscoveryComplete(true); | |
| 258 adapter_->NotifyGattServicesDiscovered(this); | |
| 259 adapter_->NotifyDeviceChanged(this); | |
| 260 } | |
| 261 } | 246 } |
| 262 | 247 |
| 263 void BluetoothLowEnergyDeviceMac::DidModifyServices( | 248 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
| 264 NSArray* invalidatedServices) { | 249 NSArray* invalidatedServices) { |
| 265 VLOG(1) << "DidModifyServices: "; | 250 VLOG(1) << "DidModifyServices: "; |
| 266 for (CBService* cb_service in invalidatedServices) { | 251 for (CBService* cb_service in invalidatedServices) { |
| 267 BluetoothRemoteGattServiceMac* gatt_service = | 252 BluetoothRemoteGattServiceMac* gatt_service = |
| 268 GetBluetoothRemoteGattService(cb_service); | 253 GetBluetoothRemoteGattService(cb_service); |
| 269 DCHECK(gatt_service); | 254 DCHECK(gatt_service); |
| 270 VLOG(1) << gatt_service->GetUUID().canonical_value(); | 255 VLOG(1) << gatt_service->GetUUID().canonical_value(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 void BluetoothLowEnergyDeviceMac::DidUpdateNotificationState( | 287 void BluetoothLowEnergyDeviceMac::DidUpdateNotificationState( |
| 303 CBCharacteristic* characteristic, | 288 CBCharacteristic* characteristic, |
| 304 NSError* error) { | 289 NSError* error) { |
| 305 VLOG(1) << "DidUpdateNotificationState"; | 290 VLOG(1) << "DidUpdateNotificationState"; |
| 306 BluetoothRemoteGattServiceMac* gatt_service = | 291 BluetoothRemoteGattServiceMac* gatt_service = |
| 307 GetBluetoothRemoteGattService(characteristic.service); | 292 GetBluetoothRemoteGattService(characteristic.service); |
| 308 DCHECK(gatt_service); | 293 DCHECK(gatt_service); |
| 309 gatt_service->DidUpdateNotificationState(characteristic, error); | 294 gatt_service->DidUpdateNotificationState(characteristic, error); |
| 310 } | 295 } |
| 311 | 296 |
| 297 void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors( |
| 298 CBCharacteristic* cb_characteristic, |
| 299 NSError* error) { |
| 300 if (error) { |
| 301 // TODO(http://crbug.com/609320): Need to pass the error. |
| 302 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 303 VLOG(1) << "Can't discover descriptors: " |
| 304 << error.localizedDescription.UTF8String << " (" << error.domain |
| 305 << ": " << error.code << ")"; |
| 306 return; |
| 307 } |
| 308 VLOG(1) << "DidDiscoverDescriptors."; |
| 309 if (!IsGattConnected()) { |
| 310 // Don't discover descriptors if the device disconnected. |
| 311 return; |
| 312 } |
| 313 BluetoothRemoteGattServiceMac* gatt_service = |
| 314 GetBluetoothRemoteGattService(cb_characteristic.service); |
| 315 DCHECK(gatt_service); |
| 316 gatt_service->DidDiscoverDescriptors(cb_characteristic); |
| 317 SendNotificationIfDiscoveryComplete(); |
| 318 } |
| 319 |
| 312 // static | 320 // static |
| 313 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 321 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
| 314 CBPeripheral* peripheral) { | 322 CBPeripheral* peripheral) { |
| 315 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 323 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 316 NSUUID* uuid = [peripheral identifier]; | 324 NSUUID* uuid = [peripheral identifier]; |
| 317 NSString* uuidString = [uuid UUIDString]; | 325 NSString* uuidString = [uuid UUIDString]; |
| 318 return base::SysNSStringToUTF8(uuidString); | 326 return base::SysNSStringToUTF8(uuidString); |
| 319 } | 327 } |
| 320 | 328 |
| 321 // static | 329 // static |
| 322 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( | 330 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
| 323 CBPeripheral* peripheral) { | 331 CBPeripheral* peripheral) { |
| 324 const size_t kCanonicalAddressNumberOfBytes = 6; | 332 const size_t kCanonicalAddressNumberOfBytes = 6; |
| 325 char raw[kCanonicalAddressNumberOfBytes]; | 333 char raw[kCanonicalAddressNumberOfBytes]; |
| 326 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, | 334 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, |
| 327 sizeof(raw)); | 335 sizeof(raw)); |
| 328 std::string hash = base::HexEncode(raw, sizeof(raw)); | 336 std::string hash = base::HexEncode(raw, sizeof(raw)); |
| 329 return BluetoothDevice::CanonicalizeAddress(hash); | 337 return BluetoothDevice::CanonicalizeAddress(hash); |
| 330 } | 338 } |
| 331 | 339 |
| 340 void BluetoothLowEnergyDeviceMac::SendNotificationIfDiscoveryComplete() { |
| 341 // Notify when all services have been discovered. |
| 342 bool discovery_complete = |
| 343 std::find_if_not( |
| 344 gatt_services_.begin(), gatt_services_.end(), |
| 345 [](GattServiceMap::value_type& pair) { |
| 346 BluetoothRemoteGattService* gatt_service = pair.second.get(); |
| 347 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) |
| 348 ->IsDiscoveryComplete(); |
| 349 }) == gatt_services_.end(); |
| 350 if (discovery_complete) { |
| 351 device_uuids_.ReplaceServiceUUIDs(gatt_services_); |
| 352 SetGattServicesDiscoveryComplete(true); |
| 353 adapter_->NotifyGattServicesDiscovered(this); |
| 354 adapter_->NotifyDeviceChanged(this); |
| 355 } |
| 356 } |
| 357 |
| 332 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { | 358 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { |
| 333 return static_cast<BluetoothAdapterMac*>(this->adapter_); | 359 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
| 334 } | 360 } |
| 335 | 361 |
| 336 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { | 362 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
| 337 return peripheral_; | 363 return peripheral_; |
| 338 } | 364 } |
| 339 | 365 |
| 340 device::BluetoothRemoteGattServiceMac* | 366 device::BluetoothRemoteGattServiceMac* |
| 341 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( | 367 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 364 // 2. When we cancel a pending connection request. | 390 // 2. When we cancel a pending connection request. |
| 365 if (create_gatt_connection_error_callbacks_.empty()) { | 391 if (create_gatt_connection_error_callbacks_.empty()) { |
| 366 // If there are no pending callbacks then the connection broke (#1). | 392 // If there are no pending callbacks then the connection broke (#1). |
| 367 DidDisconnectGatt(); | 393 DidDisconnectGatt(); |
| 368 return; | 394 return; |
| 369 } | 395 } |
| 370 // Else we canceled the connection request (#2). | 396 // Else we canceled the connection request (#2). |
| 371 // TODO(http://crbug.com/585897): Need to pass the error. | 397 // TODO(http://crbug.com/585897): Need to pass the error. |
| 372 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); | 398 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); |
| 373 } | 399 } |
| OLD | NEW |