| 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" |
| 11 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" | 12 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 15 #include "device/bluetooth/bluetooth_adapter_mac.h" | 16 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 16 #include "device/bluetooth/bluetooth_device.h" | 17 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "device/bluetooth/bluetooth_low_energy_peripheral_delegate.h" |
| 19 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 17 | 20 |
| 18 using device::BluetoothDevice; | 21 using device::BluetoothDevice; |
| 19 using device::BluetoothLowEnergyDeviceMac; | 22 using device::BluetoothLowEnergyDeviceMac; |
| 20 | 23 |
| 21 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 24 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
| 22 BluetoothAdapterMac* adapter, | 25 BluetoothAdapterMac* adapter, |
| 23 CBPeripheral* peripheral, | 26 CBPeripheral* peripheral, |
| 24 NSDictionary* advertisement_data, | 27 NSDictionary* advertisement_data, |
| 25 int rssi) | 28 int rssi) |
| 26 : BluetoothDeviceMac(adapter), | 29 : BluetoothDeviceMac(adapter), |
| 27 peripheral_(peripheral, base::scoped_policy::RETAIN) { | 30 peripheral_(peripheral, base::scoped_policy::RETAIN) { |
| 28 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 31 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 29 DCHECK(peripheral_.get()); | 32 DCHECK(peripheral_.get()); |
| 33 peripheral_delegate_.reset([[BluetoothLowEnergyPeripheralDelegate alloc] |
| 34 initWithBluetoothLowEnergyDeviceMac:this]); |
| 35 [peripheral_ setDelegate:peripheral_delegate_]; |
| 30 identifier_ = GetPeripheralIdentifier(peripheral); | 36 identifier_ = GetPeripheralIdentifier(peripheral); |
| 31 hash_address_ = GetPeripheralHashAddress(peripheral); | 37 hash_address_ = GetPeripheralHashAddress(peripheral); |
| 32 Update(advertisement_data, rssi); | 38 Update(advertisement_data, rssi); |
| 33 } | 39 } |
| 34 | 40 |
| 35 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | 41 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { |
| 36 if (IsGattConnected()) { | 42 if (IsGattConnected()) { |
| 37 GetMacAdapter()->DisconnectGatt(this); | 43 GetMacAdapter()->DisconnectGatt(this); |
| 38 } | 44 } |
| 39 } | 45 } |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { | 229 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { |
| 224 if (!IsGattConnected()) { | 230 if (!IsGattConnected()) { |
| 225 GetMacAdapter()->CreateGattConnection(this); | 231 GetMacAdapter()->CreateGattConnection(this); |
| 226 } | 232 } |
| 227 } | 233 } |
| 228 | 234 |
| 229 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { | 235 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { |
| 230 GetMacAdapter()->DisconnectGatt(this); | 236 GetMacAdapter()->DisconnectGatt(this); |
| 231 } | 237 } |
| 232 | 238 |
| 239 void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { |
| 240 if (error) { |
| 241 // TODO(http://crbug.com/609320): Need to pass the error. |
| 242 // TODO(http://crbug.com/609844): Decide what to do if we fail to discover |
| 243 // a device services. |
| 244 VLOG(1) << "Can't discover primary services: " |
| 245 << error.localizedDescription.UTF8String << " (" << error.domain |
| 246 << ": " << error.code << ")"; |
| 247 return; |
| 248 } |
| 249 for (CBService* cb_service in GetPeripheral().services) { |
| 250 BluetoothRemoteGattServiceMac* gatt_service = |
| 251 GetBluetoothRemoteGattService(cb_service); |
| 252 if (!gatt_service) { |
| 253 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, |
| 254 true /* is_primary */); |
| 255 gatt_services_.add(gatt_service->GetIdentifier(), |
| 256 base::WrapUnique(gatt_service)); |
| 257 adapter_->NotifyGattServiceAdded(gatt_service); |
| 258 } |
| 259 } |
| 260 // TODO(http://crbug.com/609064): Services are fully discovered once all |
| 261 // characteristics have been found. |
| 262 SetGattServicesDiscoveryComplete(true); |
| 263 adapter_->NotifyGattServicesDiscovered(this); |
| 264 } |
| 265 |
| 266 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
| 267 NSArray* invalidatedServices) { |
| 268 for (CBService* cb_service in invalidatedServices) { |
| 269 BluetoothRemoteGattServiceMac* gatt_service = |
| 270 GetBluetoothRemoteGattService(cb_service); |
| 271 DCHECK(gatt_service); |
| 272 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
| 273 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); |
| 274 adapter_->NotifyGattServiceRemoved(scoped_service.get()); |
| 275 } |
| 276 SetGattServicesDiscoveryComplete(false); |
| 277 [GetPeripheral() discoverServices:nil]; |
| 278 } |
| 279 |
| 233 // static | 280 // static |
| 234 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 281 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
| 235 CBPeripheral* peripheral) { | 282 CBPeripheral* peripheral) { |
| 236 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 283 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 237 NSUUID* uuid = [peripheral identifier]; | 284 NSUUID* uuid = [peripheral identifier]; |
| 238 NSString* uuidString = [uuid UUIDString]; | 285 NSString* uuidString = [uuid UUIDString]; |
| 239 return base::SysNSStringToUTF8(uuidString); | 286 return base::SysNSStringToUTF8(uuidString); |
| 240 } | 287 } |
| 241 | 288 |
| 242 // static | 289 // static |
| 243 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( | 290 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
| 244 CBPeripheral* peripheral) { | 291 CBPeripheral* peripheral) { |
| 245 const size_t kCanonicalAddressNumberOfBytes = 6; | 292 const size_t kCanonicalAddressNumberOfBytes = 6; |
| 246 char raw[kCanonicalAddressNumberOfBytes]; | 293 char raw[kCanonicalAddressNumberOfBytes]; |
| 247 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, | 294 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, |
| 248 sizeof(raw)); | 295 sizeof(raw)); |
| 249 std::string hash = base::HexEncode(raw, sizeof(raw)); | 296 std::string hash = base::HexEncode(raw, sizeof(raw)); |
| 250 return BluetoothDevice::CanonicalizeAddress(hash); | 297 return BluetoothDevice::CanonicalizeAddress(hash); |
| 251 } | 298 } |
| 252 | 299 |
| 253 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { | 300 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { |
| 254 return static_cast<BluetoothAdapterMac*>(this->adapter_); | 301 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
| 255 } | 302 } |
| 256 | 303 |
| 257 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { | 304 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
| 258 return peripheral_; | 305 return peripheral_; |
| 259 } | 306 } |
| 260 | 307 |
| 308 device::BluetoothRemoteGattServiceMac* |
| 309 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( |
| 310 CBService* cb_service) const { |
| 311 for (GattServiceMap::const_iterator it = gatt_services_.begin(); |
| 312 it != gatt_services_.end(); ++it) { |
| 313 device::BluetoothRemoteGattService* gatt_service = it->second; |
| 314 device::BluetoothRemoteGattServiceMac* gatt_service_mac = |
| 315 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
| 316 if (gatt_service_mac->GetService() == cb_service) |
| 317 return gatt_service_mac; |
| 318 } |
| 319 return nullptr; |
| 320 } |
| 321 |
| 261 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral( | 322 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral( |
| 262 BluetoothDevice::ConnectErrorCode error_code) { | 323 BluetoothDevice::ConnectErrorCode error_code) { |
| 324 SetGattServicesDiscoveryComplete(false); |
| 325 // Explicitly take and erase GATT services one by one to ensure that calling |
| 326 // GetGattService on removed service in GattServiceRemoved returns null. |
| 327 std::vector<std::string> service_keys; |
| 328 for (const auto& gatt_service : gatt_services_) { |
| 329 service_keys.push_back(gatt_service.first); |
| 330 } |
| 331 for (const auto& key : service_keys) { |
| 332 gatt_services_.take_and_erase(key); |
| 333 } |
| 263 if (create_gatt_connection_error_callbacks_.empty()) { | 334 if (create_gatt_connection_error_callbacks_.empty()) { |
| 264 // TODO(http://crbug.com/585897): Need to pass the error. | 335 // TODO(http://crbug.com/585897): Need to pass the error. |
| 265 DidDisconnectGatt(); | 336 DidDisconnectGatt(); |
| 266 } else { | 337 } else { |
| 267 DidFailToConnectGatt(error_code); | 338 DidFailToConnectGatt(error_code); |
| 268 } | 339 } |
| 269 } | 340 } |
| OLD | NEW |