Chromium Code Reviews| 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 DisconnectGatt(); | |
|
ortuno
2016/05/09 19:54:01
Let's not disconnect to match the other platforms.
jlebel
2016/05/09 23:05:59
Done.
| |
| 248 return; | |
| 249 } | |
| 250 for (CBService* cb_service in GetPeripheral().services) { | |
| 251 BluetoothRemoteGattServiceMac* gatt_service = | |
| 252 GetBluetoothRemoteGattService(cb_service); | |
| 253 if (!gatt_service) { | |
| 254 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, | |
| 255 true /* is_primary */); | |
| 256 gatt_services_.add(gatt_service->GetIdentifier(), | |
| 257 base::WrapUnique(gatt_service)); | |
| 258 adapter_->NotifyGattServiceAdded(gatt_service); | |
| 259 } | |
| 260 } | |
| 261 // TODO(http://crbug.com/609064): Services are fully discovered once all | |
| 262 // characteristics has been found. | |
|
ortuno
2016/05/09 19:54:01
s/has/have/
jlebel
2016/05/09 23:05:59
Done.
| |
| 263 SetGattServicesDiscoveryComplete(true); | |
| 264 adapter_->NotifyGattServicesDiscovered(this); | |
| 265 } | |
| 266 | |
| 267 void BluetoothLowEnergyDeviceMac::DidModifyServices( | |
| 268 NSArray* invalidatedServices) { | |
| 269 for (CBService* cb_service in invalidatedServices) { | |
| 270 BluetoothRemoteGattServiceMac* gatt_service = | |
| 271 GetBluetoothRemoteGattService(cb_service); | |
| 272 DCHECK(gatt_service); | |
| 273 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | |
| 274 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); | |
| 275 adapter_->NotifyGattServiceRemoved(scoped_service.get()); | |
| 276 } | |
| 277 SetGattServicesDiscoveryComplete(false); | |
| 278 [GetPeripheral() discoverServices:nil]; | |
| 279 } | |
| 280 | |
| 233 // static | 281 // static |
| 234 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 282 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
| 235 CBPeripheral* peripheral) { | 283 CBPeripheral* peripheral) { |
| 236 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 284 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 237 NSUUID* uuid = [peripheral identifier]; | 285 NSUUID* uuid = [peripheral identifier]; |
| 238 NSString* uuidString = [uuid UUIDString]; | 286 NSString* uuidString = [uuid UUIDString]; |
| 239 return base::SysNSStringToUTF8(uuidString); | 287 return base::SysNSStringToUTF8(uuidString); |
| 240 } | 288 } |
| 241 | 289 |
| 242 // static | 290 // static |
| 243 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( | 291 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
| 244 CBPeripheral* peripheral) { | 292 CBPeripheral* peripheral) { |
| 245 const size_t kCanonicalAddressNumberOfBytes = 6; | 293 const size_t kCanonicalAddressNumberOfBytes = 6; |
| 246 char raw[kCanonicalAddressNumberOfBytes]; | 294 char raw[kCanonicalAddressNumberOfBytes]; |
| 247 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, | 295 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, |
| 248 sizeof(raw)); | 296 sizeof(raw)); |
| 249 std::string hash = base::HexEncode(raw, sizeof(raw)); | 297 std::string hash = base::HexEncode(raw, sizeof(raw)); |
| 250 return BluetoothDevice::CanonicalizeAddress(hash); | 298 return BluetoothDevice::CanonicalizeAddress(hash); |
| 251 } | 299 } |
| 252 | 300 |
| 253 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { | 301 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { |
| 254 return static_cast<BluetoothAdapterMac*>(this->adapter_); | 302 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
| 255 } | 303 } |
| 256 | 304 |
| 257 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { | 305 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
| 258 return peripheral_; | 306 return peripheral_; |
| 259 } | 307 } |
| 260 | 308 |
| 309 device::BluetoothRemoteGattServiceMac* | |
| 310 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( | |
| 311 CBService* cb_service) const { | |
| 312 for (GattServiceMap::const_iterator it = gatt_services_.begin(); | |
| 313 it != gatt_services_.end(); ++it) { | |
| 314 device::BluetoothRemoteGattService* gatt_service = it->second; | |
| 315 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | |
| 316 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | |
| 317 if (gatt_service_mac->GetService() == cb_service) | |
| 318 return gatt_service_mac; | |
| 319 } | |
| 320 return nullptr; | |
| 321 } | |
| 322 | |
| 261 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral( | 323 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral( |
| 262 BluetoothDevice::ConnectErrorCode error_code) { | 324 BluetoothDevice::ConnectErrorCode error_code) { |
| 325 SetGattServicesDiscoveryComplete(false); | |
| 326 // Explicitly take and erase GATT services one by one to ensure that calling | |
| 327 // GetGattService on removed service in GattServiceRemoved returns null. | |
| 328 std::vector<std::string> service_keys; | |
| 329 for (const auto& gatt_service : gatt_services_) { | |
| 330 service_keys.push_back(gatt_service.first); | |
| 331 } | |
| 332 for (const auto& key : service_keys) { | |
| 333 gatt_services_.take_and_erase(key); | |
| 334 } | |
| 263 if (create_gatt_connection_error_callbacks_.empty()) { | 335 if (create_gatt_connection_error_callbacks_.empty()) { |
| 264 // TODO(http://crbug.com/585897): Need to pass the error. | 336 // TODO(http://crbug.com/585897): Need to pass the error. |
| 265 DidDisconnectGatt(); | 337 DidDisconnectGatt(); |
| 266 } else { | 338 } else { |
| 267 DidFailToConnectGatt(error_code); | 339 DidFailToConnectGatt(error_code); |
| 268 } | 340 } |
| 269 } | 341 } |
| OLD | NEW |