| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 ClearServiceData(); | 54 ClearServiceData(); |
| 55 NSDictionary* service_data = | 55 NSDictionary* service_data = |
| 56 [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey]; | 56 [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey]; |
| 57 for (CBUUID* uuid in service_data) { | 57 for (CBUUID* uuid in service_data) { |
| 58 NSData* data = [service_data objectForKey:uuid]; | 58 NSData* data = [service_data objectForKey:uuid]; |
| 59 BluetoothUUID service_uuid = | 59 BluetoothUUID service_uuid = |
| 60 BluetoothAdapterMac::BluetoothUUIDWithCBUUID(uuid); | 60 BluetoothAdapterMac::BluetoothUUIDWithCBUUID(uuid); |
| 61 SetServiceData(service_uuid, static_cast<const char*>([data bytes]), | 61 SetServiceData(service_uuid, static_cast<const char*>([data bytes]), |
| 62 [data length]); | 62 [data length]); |
| 63 } | 63 } |
| 64 |
| 65 std::unordered_set<BluetoothUUID, BluetoothUUIDHash> uuid_set; |
| 64 NSArray* service_uuids = | 66 NSArray* service_uuids = |
| 65 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; | 67 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; |
| 66 for (CBUUID* uuid in service_uuids) { | 68 for (CBUUID* uuid in service_uuids) { |
| 67 advertised_uuids_.insert( | 69 uuid_set.emplace([[uuid UUIDString] UTF8String]); |
| 68 BluetoothUUID(std::string([[uuid UUIDString] UTF8String]))); | |
| 69 } | 70 } |
| 70 NSArray* overflow_service_uuids = [advertisement_data | 71 NSArray* overflow_service_uuids = [advertisement_data |
| 71 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; | 72 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; |
| 72 for (CBUUID* uuid in overflow_service_uuids) { | 73 for (CBUUID* uuid in overflow_service_uuids) { |
| 73 advertised_uuids_.insert( | 74 uuid_set.emplace([[uuid UUIDString] UTF8String]); |
| 74 BluetoothUUID(std::string([[uuid UUIDString] UTF8String]))); | |
| 75 } | 75 } |
| 76 |
| 77 advertised_uuids_ = UUIDList(uuid_set.begin(), uuid_set.end()); |
| 76 } | 78 } |
| 77 | 79 |
| 78 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { | 80 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { |
| 79 return identifier_; | 81 return identifier_; |
| 80 } | 82 } |
| 81 | 83 |
| 82 uint32_t BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { | 84 uint32_t BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { |
| 83 return 0x1F00; // Unspecified Device Class | 85 return 0x1F00; // Unspecified Device Class |
| 84 } | 86 } |
| 85 | 87 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 136 } |
| 135 | 137 |
| 136 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { | 138 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { |
| 137 return connectable_; | 139 return connectable_; |
| 138 } | 140 } |
| 139 | 141 |
| 140 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { | 142 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { |
| 141 return ([peripheral_ state] == CBPeripheralStateConnecting); | 143 return ([peripheral_ state] == CBPeripheralStateConnecting); |
| 142 } | 144 } |
| 143 | 145 |
| 144 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { | |
| 145 return BluetoothDevice::UUIDList(advertised_uuids_.begin(), | |
| 146 advertised_uuids_.end()); | |
| 147 } | |
| 148 | |
| 149 int16_t BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const { | 146 int16_t BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const { |
| 150 return kUnknownPower; | 147 return kUnknownPower; |
| 151 } | 148 } |
| 152 | 149 |
| 153 int16_t BluetoothLowEnergyDeviceMac::GetInquiryTxPower() const { | 150 int16_t BluetoothLowEnergyDeviceMac::GetInquiryTxPower() const { |
| 154 NOTIMPLEMENTED(); | 151 NOTIMPLEMENTED(); |
| 155 return kUnknownPower; | 152 return kUnknownPower; |
| 156 } | 153 } |
| 157 | 154 |
| 158 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const { | 155 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 // Notify when all services have been discovered. | 281 // Notify when all services have been discovered. |
| 285 bool discovery_complete = | 282 bool discovery_complete = |
| 286 std::find_if_not( | 283 std::find_if_not( |
| 287 gatt_services_.begin(), gatt_services_.end(), | 284 gatt_services_.begin(), gatt_services_.end(), |
| 288 [](std::pair<std::string, BluetoothRemoteGattService*> pair) { | 285 [](std::pair<std::string, BluetoothRemoteGattService*> pair) { |
| 289 BluetoothRemoteGattService* gatt_service = pair.second; | 286 BluetoothRemoteGattService* gatt_service = pair.second; |
| 290 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) | 287 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) |
| 291 ->IsDiscoveryComplete(); | 288 ->IsDiscoveryComplete(); |
| 292 }) == gatt_services_.end(); | 289 }) == gatt_services_.end(); |
| 293 if (discovery_complete) { | 290 if (discovery_complete) { |
| 291 UpdateServiceUUIDs(); |
| 294 SetGattServicesDiscoveryComplete(true); | 292 SetGattServicesDiscoveryComplete(true); |
| 295 adapter_->NotifyGattServicesDiscovered(this); | 293 adapter_->NotifyGattServicesDiscovered(this); |
| 296 } | 294 } |
| 297 } | 295 } |
| 298 | 296 |
| 299 void BluetoothLowEnergyDeviceMac::DidModifyServices( | 297 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
| 300 NSArray* invalidatedServices) { | 298 NSArray* invalidatedServices) { |
| 301 for (CBService* cb_service in invalidatedServices) { | 299 for (CBService* cb_service in invalidatedServices) { |
| 302 BluetoothRemoteGattServiceMac* gatt_service = | 300 BluetoothRemoteGattServiceMac* gatt_service = |
| 303 GetBluetoothRemoteGattService(cb_service); | 301 GetBluetoothRemoteGattService(cb_service); |
| 304 DCHECK(gatt_service); | 302 DCHECK(gatt_service); |
| 305 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 303 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
| 306 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); | 304 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); |
| 307 adapter_->NotifyGattServiceRemoved(scoped_service.get()); | 305 adapter_->NotifyGattServiceRemoved(scoped_service.get()); |
| 308 } | 306 } |
| 307 service_uuids_.clear(); |
| 309 SetGattServicesDiscoveryComplete(false); | 308 SetGattServicesDiscoveryComplete(false); |
| 310 [GetPeripheral() discoverServices:nil]; | 309 [GetPeripheral() discoverServices:nil]; |
| 311 } | 310 } |
| 312 | 311 |
| 313 void BluetoothLowEnergyDeviceMac::DidUpdateValue( | 312 void BluetoothLowEnergyDeviceMac::DidUpdateValue( |
| 314 CBCharacteristic* characteristic, | 313 CBCharacteristic* characteristic, |
| 315 NSError* error) { | 314 NSError* error) { |
| 316 BluetoothRemoteGattServiceMac* gatt_service = | 315 BluetoothRemoteGattServiceMac* gatt_service = |
| 317 GetBluetoothRemoteGattService(characteristic.service); | 316 GetBluetoothRemoteGattService(characteristic.service); |
| 318 DCHECK(gatt_service); | 317 DCHECK(gatt_service); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 GattServiceMap gatt_services_swapped; | 386 GattServiceMap gatt_services_swapped; |
| 388 gatt_services_swapped.swap(gatt_services_); | 387 gatt_services_swapped.swap(gatt_services_); |
| 389 gatt_services_swapped.clear(); | 388 gatt_services_swapped.clear(); |
| 390 if (create_gatt_connection_error_callbacks_.empty()) { | 389 if (create_gatt_connection_error_callbacks_.empty()) { |
| 391 // TODO(http://crbug.com/585897): Need to pass the error. | 390 // TODO(http://crbug.com/585897): Need to pass the error. |
| 392 DidDisconnectGatt(); | 391 DidDisconnectGatt(); |
| 393 } else { | 392 } else { |
| 394 DidFailToConnectGatt(error_code); | 393 DidFailToConnectGatt(error_code); |
| 395 } | 394 } |
| 396 } | 395 } |
| OLD | NEW |