Chromium Code Reviews| Index: chrome/browser/mac/bluetooth_utility.mm |
| diff --git a/chrome/browser/mac/bluetooth_utility.mm b/chrome/browser/mac/bluetooth_utility.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2fbea72388915e01e0cca83914fa432c13d3e5b6 |
| --- /dev/null |
| +++ b/chrome/browser/mac/bluetooth_utility.mm |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/mac/bluetooth_utility.h" |
| + |
| +#import <Foundation/Foundation.h> |
| +#include <IOKit/IOKitLib.h> |
| + |
| +#include "base/mac/foundation_util.h" |
| +#include "base/mac/mac_util.h" |
| +#include "base/mac/scoped_ioobject.h" |
| +#include "base/mac/sdk_forward_declarations.h" |
| + |
| +namespace { |
| + |
| +const char* kBluetoothServiceKey = "IOBluetoothHCIController"; |
|
Mark Mentovai
2014/07/11 18:05:04
These constants don’t really need to exist outside
erikchen
2014/07/11 18:56:56
Done.
|
| +NSString* kLMPVersionKey = @"LMPVersion"; |
| +NSString* kSupportedFeaturesKey = @"HCISupportedFeatures"; |
| +const int kSupportedFeaturesLEIndex = 3; |
| +const int kSupportedFeaturesSize = 8; |
| + |
| +// The first LMP version that supports Bluetooth LE. |
| +const int kFirstLELMPVersion = 6; |
| + |
| +} // namespace |
| + |
| +namespace bluetooth_utility { |
| + |
| +BluetoothAvailability GetBluetoothAvailability() { |
| + base::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict( |
| + IOServiceMatching(kBluetoothServiceKey)); |
| + if (!matching_dict) |
| + return BLUETOOTH_AVAILABILITY_ERROR; |
| + |
| + // IOServiceGetMatchingServices takes ownership of matching_dict. |
| + io_iterator_t iter; |
| + int kr = IOServiceGetMatchingServices( |
| + kIOMasterPortDefault, matching_dict.release(), &iter); |
| + if (kr != KERN_SUCCESS) |
| + return BLUETOOTH_NOT_AVAILABLE; |
| + base::mac::ScopedIOObject<io_iterator_t> scoped_iter(iter); |
| + |
| + int bluetooth_available = false; |
| + base::mac::ScopedIOObject<io_service_t> device; |
| + while (device.reset(IOIteratorNext(scoped_iter.get())), device) { |
| + bluetooth_available = true; |
| + |
| + CFMutableDictionaryRef dict; |
| + kr = IORegistryEntryCreateCFProperties( |
| + device, &dict, kCFAllocatorDefault, kNilOptions); |
| + if (kr != KERN_SUCCESS) |
| + continue; |
| + base::ScopedCFTypeRef<CFMutableDictionaryRef> scoped_dict(dict); |
| + |
| + NSDictionary* objc_dict = base::mac::CFToNSCast(scoped_dict.get()); |
| + NSNumber* lmp_version = |
| + base::mac::ObjCCast<NSNumber>([objc_dict objectForKey:kLMPVersionKey]); |
| + if (!lmp_version) |
| + continue; |
| + |
| + if ([lmp_version intValue] < kFirstLELMPVersion) |
| + continue; |
| + |
| + // Check the supported features registry entry for Bluetooth LE |
| + // availability. The relevant bit has a different meaning on OSX 10.6, and |
| + // could change again in the future. |
| + if (base::mac::IsOSLionOrEarlier()) { |
| + // All we know is that the LMP version supports LE. Assume that the |
| + // bluetooth radio is LE compatible. |
| + return BLUETOOTH_AVAILABLE_WITH_LE; |
|
Mark Mentovai
2014/07/11 18:05:04
For histogramming purposes, I think you want to br
erikchen
2014/07/11 18:56:56
Done.
|
| + } |
| + |
| + NSData* data = base::mac::ObjCCast<NSData>( |
| + [objc_dict objectForKey:kSupportedFeaturesKey]); |
| + if (!data || [data length] != kSupportedFeaturesSize) |
|
Mark Mentovai
2014/07/11 18:05:04
Don’t check the size, there’s evidence in the head
erikchen
2014/07/11 18:56:56
Right, fixed.
|
| + continue; |
| + |
| + // The result is a big endian unsigned long long. |
| + // We want the fifth byte, which is at index 3. |
|
Mark Mentovai
2014/07/11 18:05:04
Something doesn’t add up here, but it’s probably b
erikchen
2014/07/11 18:56:56
The index is interpreted in reverse order (or so w
|
| + const unsigned char* bytes = |
| + static_cast<const unsigned char*>([data bytes]); |
| + const unsigned char byte = bytes[kSupportedFeaturesLEIndex]; |
| + bool le_supported = byte & kBluetoothFeatureLESupportedController; |
| + if (le_supported) |
| + return BLUETOOTH_AVAILABLE_WITH_LE; |
| + } |
| + |
| + return bluetooth_available ? BLUETOOTH_AVAILABLE_WITHOUT_LE |
| + : BLUETOOTH_AVAILABILITY_ERROR; |
| +} |
| + |
| +} // namespace bluetooth_utility |