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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 130 |
128 bool BluetoothLowEnergyDeviceMac::IsGattConnected() const { | 131 bool BluetoothLowEnergyDeviceMac::IsGattConnected() const { |
129 return ([peripheral_ state] == CBPeripheralStateConnected); | 132 return ([peripheral_ state] == CBPeripheralStateConnected); |
130 } | 133 } |
131 | 134 |
132 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { | 135 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { |
133 return connectable_; | 136 return connectable_; |
134 } | 137 } |
135 | 138 |
136 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { | 139 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { |
137 return false; | 140 return ([peripheral_ state] == CBPeripheralStateConnecting); |
138 } | 141 } |
139 | 142 |
140 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { | 143 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { |
141 return BluetoothDevice::UUIDList(advertised_uuids_.begin(), | 144 return BluetoothDevice::UUIDList(advertised_uuids_.begin(), |
142 advertised_uuids_.end()); | 145 advertised_uuids_.end()); |
143 } | 146 } |
144 | 147 |
145 int16_t BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const { | 148 int16_t BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const { |
146 return kUnknownPower; | 149 return kUnknownPower; |
147 } | 150 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 NOTIMPLEMENTED(); | 216 NOTIMPLEMENTED(); |
214 } | 217 } |
215 | 218 |
216 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( | 219 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( |
217 const device::BluetoothUUID& uuid, | 220 const device::BluetoothUUID& uuid, |
218 const ConnectToServiceCallback& callback, | 221 const ConnectToServiceCallback& callback, |
219 const ConnectToServiceErrorCallback& error_callback) { | 222 const ConnectToServiceErrorCallback& error_callback) { |
220 NOTIMPLEMENTED(); | 223 NOTIMPLEMENTED(); |
221 } | 224 } |
222 | 225 |
223 void BluetoothLowEnergyDeviceMac::CreateGattConnection( | |
224 const GattConnectionCallback& callback, | |
225 const ConnectErrorCallback& error_callback) { | |
226 NOTIMPLEMENTED(); | |
227 } | |
228 | |
229 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { | 226 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { |
230 return last_update_time_.get(); | 227 return last_update_time_.get(); |
231 } | 228 } |
232 | 229 |
233 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | 230 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { |
234 return base::SysNSStringToUTF8([peripheral_ name]); | 231 return base::SysNSStringToUTF8([peripheral_ name]); |
235 } | 232 } |
236 | 233 |
237 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { | 234 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { |
238 // Mac implementation does not yet use the default CreateGattConnection | 235 if (!IsGattConnected()) { |
239 // implementation. http://crbug.com/520774 | 236 GetMacAdapter()->CreateGattConnection(this); |
240 NOTIMPLEMENTED(); | 237 } |
241 } | 238 } |
242 | 239 |
243 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { | 240 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { |
244 // Mac implementation does not yet use the default CreateGattConnection | 241 GetMacAdapter()->DisconnectGatt(this); |
245 // implementation. http://crbug.com/520774 | |
246 NOTIMPLEMENTED(); | |
247 } | 242 } |
248 | 243 |
249 // static | 244 // static |
250 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 245 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
251 CBPeripheral* peripheral) { | 246 CBPeripheral* peripheral) { |
252 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 247 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
253 NSUUID* uuid = [peripheral identifier]; | 248 NSUUID* uuid = [peripheral identifier]; |
254 NSString* uuidString = [uuid UUIDString]; | 249 NSString* uuidString = [uuid UUIDString]; |
255 return base::SysNSStringToUTF8(uuidString); | 250 return base::SysNSStringToUTF8(uuidString); |
256 } | 251 } |
257 | 252 |
258 // static | 253 // static |
259 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( | 254 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
260 CBPeripheral* peripheral) { | 255 CBPeripheral* peripheral) { |
261 const size_t kCanonicalAddressNumberOfBytes = 6; | 256 const size_t kCanonicalAddressNumberOfBytes = 6; |
262 char raw[kCanonicalAddressNumberOfBytes]; | 257 char raw[kCanonicalAddressNumberOfBytes]; |
263 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, | 258 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, |
264 sizeof(raw)); | 259 sizeof(raw)); |
265 std::string hash = base::HexEncode(raw, sizeof(raw)); | 260 std::string hash = base::HexEncode(raw, sizeof(raw)); |
266 return BluetoothDevice::CanonicalizeAddress(hash); | 261 return BluetoothDevice::CanonicalizeAddress(hash); |
267 } | 262 } |
| 263 |
| 264 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { |
| 265 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
| 266 } |
| 267 |
| 268 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
| 269 return peripheral_; |
| 270 } |
| 271 |
| 272 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral() { |
| 273 if (create_gatt_connection_error_callbacks_.empty()) { |
| 274 DidDisconnectGatt(); |
| 275 } else { |
| 276 DidFailToConnectGatt(ERROR_FAILED); |
| 277 } |
| 278 } |
OLD | NEW |