Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1756)

Side by Side Diff: device/bluetooth/bluetooth_adapter_mac.mm

Issue 2339253002: bluetooth: mac: add connected LE devices to chooser (Closed)
Patch Set: Adding BluetoothAdapter::RetrievedConnectedPeripherals() and the implementation in BluetoothAdapter… Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_adapter_mac.h" 5 #include "device/bluetooth/bluetooth_adapter_mac.h"
6 6
7 #import <IOBluetooth/objc/IOBluetoothDevice.h> 7 #import <IOBluetooth/objc/IOBluetoothDevice.h>
8 #import <IOBluetooth/objc/IOBluetoothHostController.h> 8 #import <IOBluetooth/objc/IOBluetoothHostController.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 66 }
67 67
68 // static 68 // static
69 BluetoothUUID BluetoothAdapterMac::BluetoothUUIDWithCBUUID(CBUUID* uuid) { 69 BluetoothUUID BluetoothAdapterMac::BluetoothUUIDWithCBUUID(CBUUID* uuid) {
70 // UUIDString only available OS X >= 10.10. 70 // UUIDString only available OS X >= 10.10.
71 DCHECK(base::mac::IsAtLeastOS10_10()); 71 DCHECK(base::mac::IsAtLeastOS10_10());
72 std::string uuid_c_string = base::SysNSStringToUTF8([uuid UUIDString]); 72 std::string uuid_c_string = base::SysNSStringToUTF8([uuid UUIDString]);
73 return device::BluetoothUUID(uuid_c_string); 73 return device::BluetoothUUID(uuid_c_string);
74 } 74 }
75 75
76 // static
77 NSArray* BluetoothAdapterMac::CBUUIDArrayWithUUIDList(
78 BluetoothDevice::UUIDList services_uuids) {
79 NSMutableArray* services = nil;
80 if (!services_uuids.empty()) {
81 services = [NSMutableArray array];
82 for (auto& service_uuid : services_uuids) {
83 NSString* uuidString =
84 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str());
85 CBUUID* uuid = [CBUUID UUIDWithString:uuidString];
86 [services addObject:uuid];
87 }
88 }
89 return [services copy];
90 }
91
76 BluetoothAdapterMac::BluetoothAdapterMac() 92 BluetoothAdapterMac::BluetoothAdapterMac()
77 : BluetoothAdapter(), 93 : BluetoothAdapter(),
78 classic_powered_(false), 94 classic_powered_(false),
79 num_discovery_sessions_(0), 95 num_discovery_sessions_(0),
80 classic_discovery_manager_( 96 classic_discovery_manager_(
81 BluetoothDiscoveryManagerMac::CreateClassic(this)), 97 BluetoothDiscoveryManagerMac::CreateClassic(this)),
82 weak_ptr_factory_(this) { 98 weak_ptr_factory_(this) {
83 if (IsLowEnergyAvailable()) { 99 if (IsLowEnergyAvailable()) {
84 low_energy_discovery_manager_.reset( 100 low_energy_discovery_manager_.reset(
85 BluetoothLowEnergyDiscoveryManagerMac::Create(this)); 101 BluetoothLowEnergyDiscoveryManagerMac::Create(this));
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 } 179 }
164 180
165 bool BluetoothAdapterMac::IsDiscovering() const { 181 bool BluetoothAdapterMac::IsDiscovering() const {
166 bool is_discovering = classic_discovery_manager_->IsDiscovering(); 182 bool is_discovering = classic_discovery_manager_->IsDiscovering();
167 if (IsLowEnergyAvailable()) 183 if (IsLowEnergyAvailable())
168 is_discovering = 184 is_discovering =
169 is_discovering || low_energy_discovery_manager_->IsDiscovering(); 185 is_discovering || low_energy_discovery_manager_->IsDiscovering();
170 return is_discovering; 186 return is_discovering;
171 } 187 }
172 188
189 void BluetoothAdapter::RetrievedConnectedPeripherals() {
190 // It is not possible to ask for all connected peripherals with
191 // -[CBCentralManager retrieveConnectedPeripheralsWithServices:] by passing
192 // nil. To try to get most of the peripherals, the search is done with
193 // Generic Access service.
194 CBUUID* genericAccessServiceUUID = [CBUUID UUIDWithString:@"1800"];
195 NSArray* connectedServices = @[ genericAccessServiceUUID ];
196 #pragma clang diagnostic push
197 #pragma clang diagnostic ignored "-Wpartial-availability"
198 // Can remove ignore -Wpartial-availability when 10.8 will not be supported
ortuno 2016/09/28 23:15:52 Is there an issue you could include with this comm
jlebel 2016/10/05 14:09:41 Done.
199 // anymore
200 NSArray* peripherals = [low_energy_central_manager_
201 retrieveConnectedPeripheralsWithServices:connectedServices];
202 #pragma clang diagnostic pop
203 for (CBPeripheral* peripheral in peripherals) {
204 LowEnergyDeviceUpdated(peripheral, nil /* advertisementData */,
ortuno 2016/09/28 23:15:52 LowEnergyDeviceUpdated is meant to be used when re
jlebel 2016/10/05 14:09:41 I don't know how to use the first part and the thi
205 0 /* rssi */);
206 }
207 }
208
173 BluetoothAdapter::UUIDList BluetoothAdapterMac::GetUUIDs() const { 209 BluetoothAdapter::UUIDList BluetoothAdapterMac::GetUUIDs() const {
174 NOTIMPLEMENTED(); 210 NOTIMPLEMENTED();
175 return UUIDList(); 211 return UUIDList();
176 } 212 }
177 213
178 void BluetoothAdapterMac::CreateRfcommService( 214 void BluetoothAdapterMac::CreateRfcommService(
179 const BluetoothUUID& uuid, 215 const BluetoothUUID& uuid,
180 const ServiceOptions& options, 216 const ServiceOptions& options,
181 const CreateServiceCallback& callback, 217 const CreateServiceCallback& callback,
182 const CreateServiceErrorCallback& error_callback) { 218 const CreateServiceErrorCallback& error_callback) {
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 // TODO(krstnmnlsn): If a classic discovery session is already running then 400 // TODO(krstnmnlsn): If a classic discovery session is already running then
365 // we should update its filter. crbug.com/498056 401 // we should update its filter. crbug.com/498056
366 if (!classic_discovery_manager_->StartDiscovery()) { 402 if (!classic_discovery_manager_->StartDiscovery()) {
367 DVLOG(1) << "Failed to add a classic discovery session"; 403 DVLOG(1) << "Failed to add a classic discovery session";
368 return false; 404 return false;
369 } 405 }
370 } 406 }
371 if (transport & BLUETOOTH_TRANSPORT_LE) { 407 if (transport & BLUETOOTH_TRANSPORT_LE) {
372 // Begin a low energy discovery session or update it if one is already 408 // Begin a low energy discovery session or update it if one is already
373 // running. 409 // running.
374 if (IsLowEnergyAvailable()) 410 if (IsLowEnergyAvailable()) {
375 low_energy_discovery_manager_->StartDiscovery( 411 NSArray* services = CBUUIDArrayWithUUIDList(BluetoothDevice::UUIDList());
ortuno 2016/09/28 23:15:52 This seems unrelated to this change.
jlebel 2016/10/05 14:09:41 Done.
376 BluetoothDevice::UUIDList()); 412 low_energy_discovery_manager_->StartDiscovery(services);
413 }
377 } 414 }
378 return true; 415 return true;
379 } 416 }
380 417
381 void BluetoothAdapterMac::Init() { 418 void BluetoothAdapterMac::Init() {
382 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 419 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
383 PollAdapter(); 420 PollAdapter();
384 } 421 }
385 422
386 void BluetoothAdapterMac::InitForTest( 423 void BluetoothAdapterMac::InitForTest(
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 // hash the probability of this occuring with 10,000 devices 551 // hash the probability of this occuring with 10,000 devices
515 // simultaneously present is 1e-6 (see 552 // simultaneously present is 1e-6 (see
516 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We 553 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We
517 // ignore the second device by returning. 554 // ignore the second device by returning.
518 return; 555 return;
519 } 556 }
520 } 557 }
521 558
522 DCHECK(device_mac); 559 DCHECK(device_mac);
523 560
524 // Get Advertised UUIDs 561 if (advertisement_data) {
525 BluetoothDevice::UUIDList advertised_uuids; 562 // Get Advertised UUIDs
526 NSArray* service_uuids = 563 BluetoothDevice::UUIDList advertised_uuids;
527 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; 564 NSArray* service_uuids =
528 for (CBUUID* uuid in service_uuids) { 565 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey];
529 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); 566 for (CBUUID* uuid in service_uuids) {
567 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String]));
568 }
569 NSArray* overflow_service_uuids = [advertisement_data
570 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey];
571 for (CBUUID* uuid in overflow_service_uuids) {
572 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String]));
573 }
574
575 // Get Service Data.
576 BluetoothDevice::ServiceDataMap service_data_map;
577 NSDictionary* service_data =
578 [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey];
579 for (CBUUID* uuid in service_data) {
580 NSData* data = [service_data objectForKey:uuid];
581 const uint8_t* bytes = static_cast<const uint8_t*>([data bytes]);
582 size_t length = [data length];
583 service_data_map.emplace(BluetoothUUID([[uuid UUIDString] UTF8String]),
584 std::vector<uint8_t>(bytes, bytes + length));
585 }
586
587 // Get Tx Power.
588 NSNumber* tx_power =
589 [advertisement_data objectForKey:CBAdvertisementDataTxPowerLevelKey];
590 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]);
591
592 device_mac->UpdateAdvertisementData(
593 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids),
594 std::move(service_data_map),
595 tx_power == nil ? nullptr : &clamped_tx_power);
530 } 596 }
531 NSArray* overflow_service_uuids = [advertisement_data
532 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey];
533 for (CBUUID* uuid in overflow_service_uuids) {
534 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String]));
535 }
536
537 // Get Service Data.
538 BluetoothDevice::ServiceDataMap service_data_map;
539 NSDictionary* service_data =
540 [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey];
541 for (CBUUID* uuid in service_data) {
542 NSData* data = [service_data objectForKey:uuid];
543 const uint8_t* bytes = static_cast<const uint8_t*>([data bytes]);
544 size_t length = [data length];
545 service_data_map.emplace(BluetoothUUID([[uuid UUIDString] UTF8String]),
546 std::vector<uint8_t>(bytes, bytes + length));
547 }
548
549 // Get Tx Power.
550 NSNumber* tx_power =
551 [advertisement_data objectForKey:CBAdvertisementDataTxPowerLevelKey];
552 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]);
553
554 device_mac->UpdateAdvertisementData(
555 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids),
556 std::move(service_data_map),
557 tx_power == nil ? nullptr : &clamped_tx_power);
558 597
559 if (is_new_device) { 598 if (is_new_device) {
560 std::string device_address = 599 std::string device_address =
561 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); 600 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
562 devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac)); 601 devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac));
563 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 602 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
564 DeviceAdded(this, device_mac)); 603 DeviceAdded(this, device_mac));
565 } else { 604 } else {
566 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 605 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
567 DeviceChanged(this, device_mac)); 606 DeviceChanged(this, device_mac));
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 std::string device_address = 685 std::string device_address =
647 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); 686 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
648 DevicesMap::const_iterator iter = devices_.find(device_address); 687 DevicesMap::const_iterator iter = devices_.find(device_address);
649 if (iter == devices_.end()) { 688 if (iter == devices_.end()) {
650 return nil; 689 return nil;
651 } 690 }
652 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); 691 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
653 } 692 }
654 693
655 } // namespace device 694 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698