[email protected] | e088f02 | 2014-07-12 01:27:44 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/mac/bluetooth_utility.h" |
| 6 | |
| 7 | #import <Foundation/Foundation.h> |
Avi Drissman | 5904234 | 2020-01-24 21:55:43 | [diff] [blame] | 8 | #import <IOBluetooth/IOBluetooth.h> |
[email protected] | e088f02 | 2014-07-12 01:27:44 | [diff] [blame] | 9 | #include <IOKit/IOKitLib.h> |
| 10 | |
| 11 | #include "base/mac/foundation_util.h" |
| 12 | #include "base/mac/mac_util.h" |
| 13 | #include "base/mac/scoped_ioobject.h" |
[email protected] | e088f02 | 2014-07-12 01:27:44 | [diff] [blame] | 14 | |
| 15 | namespace bluetooth_utility { |
| 16 | |
| 17 | BluetoothAvailability GetBluetoothAvailability() { |
| 18 | base::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict( |
| 19 | IOServiceMatching("IOBluetoothHCIController")); |
| 20 | if (!matching_dict) |
| 21 | return BLUETOOTH_AVAILABILITY_ERROR; |
| 22 | |
| 23 | // IOServiceGetMatchingServices takes ownership of matching_dict. |
| 24 | io_iterator_t iter; |
| 25 | int kr = IOServiceGetMatchingServices( |
| 26 | kIOMasterPortDefault, matching_dict.release(), &iter); |
| 27 | if (kr != KERN_SUCCESS) |
| 28 | return BLUETOOTH_NOT_AVAILABLE; |
| 29 | base::mac::ScopedIOObject<io_iterator_t> scoped_iter(iter); |
| 30 | |
| 31 | int bluetooth_available = false; |
| 32 | base::mac::ScopedIOObject<io_service_t> device; |
| 33 | while (device.reset(IOIteratorNext(scoped_iter.get())), device) { |
| 34 | bluetooth_available = true; |
| 35 | |
| 36 | CFMutableDictionaryRef dict; |
| 37 | kr = IORegistryEntryCreateCFProperties( |
| 38 | device, &dict, kCFAllocatorDefault, kNilOptions); |
| 39 | if (kr != KERN_SUCCESS) |
| 40 | continue; |
| 41 | base::ScopedCFTypeRef<CFMutableDictionaryRef> scoped_dict(dict); |
| 42 | |
| 43 | NSDictionary* objc_dict = base::mac::CFToNSCast(scoped_dict.get()); |
| 44 | NSNumber* lmp_version = |
Leonard Grey | 7dd78aa | 2020-02-12 16:01:54 | [diff] [blame] | 45 | base::mac::ObjCCast<NSNumber>(objc_dict[@"LMPVersion"]); |
[email protected] | e088f02 | 2014-07-12 01:27:44 | [diff] [blame] | 46 | if (!lmp_version) |
| 47 | continue; |
| 48 | |
| 49 | // The LMP version is too low to support Bluetooth LE. |
| 50 | if ([lmp_version intValue] < 6) |
| 51 | continue; |
| 52 | |
Leonard Grey | 7dd78aa | 2020-02-12 16:01:54 | [diff] [blame] | 53 | NSData* data = |
| 54 | base::mac::ObjCCast<NSData>(objc_dict[@"HCISupportedFeatures"]); |
[email protected] | e088f02 | 2014-07-12 01:27:44 | [diff] [blame] | 55 | |
| 56 | NSUInteger supported_features_index = 4; |
| 57 | NSUInteger length = [data length]; |
| 58 | if (length < supported_features_index + 1) |
| 59 | continue; |
| 60 | |
| 61 | // The bytes are indexed in reverse order. |
| 62 | NSUInteger index = length - supported_features_index - 1; |
| 63 | |
| 64 | const unsigned char* bytes = |
| 65 | static_cast<const unsigned char*>([data bytes]); |
| 66 | const unsigned char byte = bytes[index]; |
| 67 | bool le_supported = byte & kBluetoothFeatureLESupportedController; |
| 68 | if (le_supported) |
| 69 | return BLUETOOTH_AVAILABLE_WITH_LE; |
| 70 | } |
| 71 | |
| 72 | return bluetooth_available ? BLUETOOTH_AVAILABLE_WITHOUT_LE |
| 73 | : BLUETOOTH_AVAILABILITY_ERROR; |
| 74 | } |
| 75 | |
| 76 | } // namespace bluetooth_utility |