OLD | NEW |
(Empty) | |
| 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/hardware_utility.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 #include <IOKit/IOKitLib.h> |
| 9 |
| 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/mac/scoped_ioobject.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const int kNoBluetoothDriver = -1; |
| 16 const int kUnexpectedError = -2; |
| 17 |
| 18 // Returns the Bluetooth driver's Link Management Protocol version. |
| 19 // Returns kNoBluetoothDriver if no Bluetooth driver is present. |
| 20 // Returns kUnexpectedError on an unexpected error. |
| 21 int GetBluetoothLMPVersion() { |
| 22 base::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict( |
| 23 IOServiceMatching("IOBluetoothHCIController")); |
| 24 if (!matching_dict) |
| 25 return kUnexpectedError; |
| 26 |
| 27 // IOServiceGetMatchingServices takes ownership of matching_dict. |
| 28 io_iterator_t iter; |
| 29 int kr = IOServiceGetMatchingServices( |
| 30 kIOMasterPortDefault, matching_dict.release(), &iter); |
| 31 if (kr != KERN_SUCCESS) |
| 32 return kNoBluetoothDriver; |
| 33 base::mac::ScopedIOObject<io_iterator_t> scoped_iter(iter); |
| 34 |
| 35 int version = 0; |
| 36 bool success = false; |
| 37 base::mac::ScopedIOObject<io_service_t> device; |
| 38 while (device.reset(IOIteratorNext(scoped_iter.get())), device) { |
| 39 CFMutableDictionaryRef dict; |
| 40 kr = IORegistryEntryCreateCFProperties( |
| 41 device, &dict, kCFAllocatorDefault, kNilOptions); |
| 42 if (kr != KERN_SUCCESS) |
| 43 continue; |
| 44 base::ScopedCFTypeRef<CFMutableDictionaryRef> scoped_dict(dict); |
| 45 |
| 46 NSDictionary* objc_dict = base::mac::CFToNSCast(scoped_dict.get()); |
| 47 NSNumber* lmp_version = |
| 48 base::mac::ObjCCast<NSNumber>([objc_dict objectForKey:@"LMPVersion"]); |
| 49 if (!lmp_version) |
| 50 continue; |
| 51 |
| 52 // A bluetooth device with an LMP version has been found. Record the |
| 53 // highest version among all devices. |
| 54 success = true; |
| 55 version = std::max(version, [lmp_version intValue]); |
| 56 } |
| 57 |
| 58 if (!success) |
| 59 return kUnexpectedError; |
| 60 |
| 61 return version; |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
| 66 namespace hardware_utility { |
| 67 |
| 68 BluetoothAvailability GetBluetoothAvailability() { |
| 69 static int result = GetBluetoothLMPVersion(); |
| 70 if (result == kUnexpectedError) |
| 71 return BLUETOOTH_AVAILABILITY_ERROR; |
| 72 if (result == kNoBluetoothDriver) |
| 73 return BLUETOOTH_NOT_AVAILABLE; |
| 74 |
| 75 // Check the LMP version and use it to determine whether Bluetooth Low Energy |
| 76 // is available. This is not necessarily the case, but appears to be true for |
| 77 // Apple devices. See http://crbug.com/392166 for more details. |
| 78 if (result < 6) |
| 79 return BLUETOOTH_AVAILABLE_WITHOUT_LE; |
| 80 return BLUETOOTH_AVAILABLE_WITH_LE; |
| 81 } |
| 82 |
| 83 } // namespace hardware_utility |
OLD | NEW |