| 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 25 matching lines...) Expand all Loading... |
| 36 NSDictionary* advertisement_data, | 36 NSDictionary* advertisement_data, |
| 37 int rssi) | 37 int rssi) |
| 38 : BluetoothDeviceMac(adapter) { | 38 : BluetoothDeviceMac(adapter) { |
| 39 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 39 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 40 identifier_ = GetPeripheralIdentifier(peripheral); | 40 identifier_ = GetPeripheralIdentifier(peripheral); |
| 41 hash_address_ = GetPeripheralHashAddress(peripheral); | 41 hash_address_ = GetPeripheralHashAddress(peripheral); |
| 42 Update(peripheral, advertisement_data, rssi); | 42 Update(peripheral, advertisement_data, rssi); |
| 43 } | 43 } |
| 44 | 44 |
| 45 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | 45 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { |
| 46 if (IsGattConnected()) { |
| 47 GetMacAdapter()->DisconnectGatt(this); |
| 48 } |
| 46 } | 49 } |
| 47 | 50 |
| 48 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | 51 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, |
| 49 NSDictionary* advertisement_data, | 52 NSDictionary* advertisement_data, |
| 50 int rssi) { | 53 int rssi) { |
| 51 last_update_time_.reset([[NSDate date] retain]); | 54 last_update_time_.reset([[NSDate date] retain]); |
| 52 peripheral_.reset([peripheral retain]); | 55 peripheral_.reset([peripheral retain]); |
| 53 rssi_ = rssi; | 56 rssi_ = rssi; |
| 54 NSNumber* connectable = | 57 NSNumber* connectable = |
| 55 [advertisement_data objectForKey:CBAdvertisementDataIsConnectable]; | 58 [advertisement_data objectForKey:CBAdvertisementDataIsConnectable]; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 123 |
| 121 bool BluetoothLowEnergyDeviceMac::IsGattConnected() const { | 124 bool BluetoothLowEnergyDeviceMac::IsGattConnected() const { |
| 122 return (GetPeripheralState() == CBPeripheralStateConnected); | 125 return (GetPeripheralState() == CBPeripheralStateConnected); |
| 123 } | 126 } |
| 124 | 127 |
| 125 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { | 128 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { |
| 126 return connectable_; | 129 return connectable_; |
| 127 } | 130 } |
| 128 | 131 |
| 129 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { | 132 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { |
| 130 return false; | 133 return (GetPeripheralState() == CBPeripheralStateConnecting); |
| 131 } | 134 } |
| 132 | 135 |
| 133 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { | 136 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { |
| 134 return BluetoothDevice::UUIDList(advertised_uuids_.begin(), | 137 return BluetoothDevice::UUIDList(advertised_uuids_.begin(), |
| 135 advertised_uuids_.end()); | 138 advertised_uuids_.end()); |
| 136 } | 139 } |
| 137 | 140 |
| 138 int16_t BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const { | 141 int16_t BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const { |
| 139 return kUnknownPower; | 142 return kUnknownPower; |
| 140 } | 143 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 NOTIMPLEMENTED(); | 209 NOTIMPLEMENTED(); |
| 207 } | 210 } |
| 208 | 211 |
| 209 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( | 212 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( |
| 210 const device::BluetoothUUID& uuid, | 213 const device::BluetoothUUID& uuid, |
| 211 const ConnectToServiceCallback& callback, | 214 const ConnectToServiceCallback& callback, |
| 212 const ConnectToServiceErrorCallback& error_callback) { | 215 const ConnectToServiceErrorCallback& error_callback) { |
| 213 NOTIMPLEMENTED(); | 216 NOTIMPLEMENTED(); |
| 214 } | 217 } |
| 215 | 218 |
| 216 void BluetoothLowEnergyDeviceMac::CreateGattConnection( | |
| 217 const GattConnectionCallback& callback, | |
| 218 const ConnectErrorCallback& error_callback) { | |
| 219 NOTIMPLEMENTED(); | |
| 220 } | |
| 221 | |
| 222 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { | 219 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { |
| 223 return last_update_time_.get(); | 220 return last_update_time_.get(); |
| 224 } | 221 } |
| 225 | 222 |
| 226 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | 223 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { |
| 227 return base::SysNSStringToUTF8([peripheral_ name]); | 224 return base::SysNSStringToUTF8([peripheral_ name]); |
| 228 } | 225 } |
| 229 | 226 |
| 230 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { | 227 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { |
| 231 // Mac implementation does not yet use the default CreateGattConnection | 228 if (!IsGattConnected()) { |
| 232 // implementation. http://crbug.com/520774 | 229 GetMacAdapter()->CreateGattConnection(this); |
| 233 NOTIMPLEMENTED(); | 230 } |
| 234 } | 231 } |
| 235 | 232 |
| 236 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { | 233 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { |
| 237 // Mac implementation does not yet use the default CreateGattConnection | 234 GetMacAdapter()->DisconnectGatt(this); |
| 238 // implementation. http://crbug.com/520774 | |
| 239 NOTIMPLEMENTED(); | |
| 240 } | 235 } |
| 241 | 236 |
| 242 // static | 237 // static |
| 243 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 238 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
| 244 CBPeripheral* peripheral) { | 239 CBPeripheral* peripheral) { |
| 245 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 240 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 246 NSUUID* uuid = [peripheral identifier]; | 241 NSUUID* uuid = [peripheral identifier]; |
| 247 NSString* uuidString = [uuid UUIDString]; | 242 NSString* uuidString = [uuid UUIDString]; |
| 248 return base::SysNSStringToUTF8(uuidString); | 243 return base::SysNSStringToUTF8(uuidString); |
| 249 } | 244 } |
| 250 | 245 |
| 251 // static | 246 // static |
| 252 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( | 247 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
| 253 CBPeripheral* peripheral) { | 248 CBPeripheral* peripheral) { |
| 254 const size_t kCanonicalAddressNumberOfBytes = 6; | 249 const size_t kCanonicalAddressNumberOfBytes = 6; |
| 255 char raw[kCanonicalAddressNumberOfBytes]; | 250 char raw[kCanonicalAddressNumberOfBytes]; |
| 256 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, | 251 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, |
| 257 sizeof(raw)); | 252 sizeof(raw)); |
| 258 std::string hash = base::HexEncode(raw, sizeof(raw)); | 253 std::string hash = base::HexEncode(raw, sizeof(raw)); |
| 259 return BluetoothDevice::CanonicalizeAddress(hash); | 254 return BluetoothDevice::CanonicalizeAddress(hash); |
| 260 } | 255 } |
| 261 | 256 |
| 257 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { |
| 258 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
| 259 } |
| 260 |
| 261 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
| 262 return peripheral_; |
| 263 } |
| 264 |
| 265 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral() { |
| 266 if (create_gatt_connection_error_callbacks_.empty()) { |
| 267 DidDisconnectGatt(); |
| 268 } else { |
| 269 DidFailToConnectGatt(ERROR_FAILED); |
| 270 } |
| 271 } |
| 272 |
| 262 CBPeripheralState BluetoothLowEnergyDeviceMac::GetPeripheralState() const { | 273 CBPeripheralState BluetoothLowEnergyDeviceMac::GetPeripheralState() const { |
| 263 Class peripheral_class = NSClassFromString(@"CBPeripheral"); | 274 Class peripheral_class = NSClassFromString(@"CBPeripheral"); |
| 264 base::scoped_nsobject<NSMethodSignature> signature([[peripheral_class | 275 base::scoped_nsobject<NSMethodSignature> signature([[peripheral_class |
| 265 instanceMethodSignatureForSelector:@selector(state)] retain]); | 276 instanceMethodSignatureForSelector:@selector(state)] retain]); |
| 266 base::scoped_nsobject<NSInvocation> invocation( | 277 base::scoped_nsobject<NSInvocation> invocation( |
| 267 [[NSInvocation invocationWithMethodSignature:signature] retain]); | 278 [[NSInvocation invocationWithMethodSignature:signature] retain]); |
| 268 [invocation setTarget:peripheral_]; | 279 [invocation setTarget:peripheral_]; |
| 269 [invocation setSelector:@selector(state)]; | 280 [invocation setSelector:@selector(state)]; |
| 270 [invocation invoke]; | 281 [invocation invoke]; |
| 271 CBPeripheralState state = CBPeripheralStateDisconnected; | 282 CBPeripheralState state = CBPeripheralStateDisconnected; |
| 272 [invocation getReturnValue:&state]; | 283 [invocation getReturnValue:&state]; |
| 273 return state; | 284 return state; |
| 274 } | 285 } |
| OLD | NEW |