| Index: chrome/browser/mac/hardware_utility.mm
|
| diff --git a/chrome/browser/mac/hardware_utility.mm b/chrome/browser/mac/hardware_utility.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6616cb81383fa715471da7bacfc508b08b9b806d
|
| --- /dev/null
|
| +++ b/chrome/browser/mac/hardware_utility.mm
|
| @@ -0,0 +1,83 @@
|
| +// 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/hardware_utility.h"
|
| +
|
| +#import <Foundation/Foundation.h>
|
| +#include <IOKit/IOKitLib.h>
|
| +
|
| +#include "base/mac/foundation_util.h"
|
| +#include "base/mac/scoped_ioobject.h"
|
| +
|
| +namespace {
|
| +
|
| +const int kNoBluetoothDriver = -1;
|
| +const int kUnexpectedError = -2;
|
| +
|
| +// Returns the Bluetooth driver's Link Management Protocol version.
|
| +// Returns kNoBluetoothDriver if no Bluetooth driver is present.
|
| +// Returns kUnexpectedError on an unexpected error.
|
| +int GetBluetoothLMPVersion() {
|
| + base::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict(
|
| + IOServiceMatching("IOBluetoothHCIController"));
|
| + if (!matching_dict)
|
| + return kUnexpectedError;
|
| +
|
| + // IOServiceGetMatchingServices takes ownership of matching_dict.
|
| + io_iterator_t iter;
|
| + int kr = IOServiceGetMatchingServices(
|
| + kIOMasterPortDefault, matching_dict.release(), &iter);
|
| + if (kr != KERN_SUCCESS)
|
| + return kNoBluetoothDriver;
|
| + base::mac::ScopedIOObject<io_iterator_t> scoped_iter(iter);
|
| +
|
| + int version = 0;
|
| + bool success = false;
|
| + base::mac::ScopedIOObject<io_service_t> device;
|
| + while (device.reset(IOIteratorNext(scoped_iter.get())), device) {
|
| + 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:@"LMPVersion"]);
|
| + if (!lmp_version)
|
| + continue;
|
| +
|
| + // A bluetooth device with an LMP version has been found. Record the
|
| + // highest version among all devices.
|
| + success = true;
|
| + version = std::max(version, [lmp_version intValue]);
|
| + }
|
| +
|
| + if (!success)
|
| + return kUnexpectedError;
|
| +
|
| + return version;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace hardware_utility {
|
| +
|
| +BluetoothAvailability GetBluetoothAvailability() {
|
| + static int result = GetBluetoothLMPVersion();
|
| + if (result == kUnexpectedError)
|
| + return BLUETOOTH_AVAILABILITY_ERROR;
|
| + if (result == kNoBluetoothDriver)
|
| + return BLUETOOTH_NOT_AVAILABLE;
|
| +
|
| + // Check the LMP version and use it to determine whether Bluetooth Low Energy
|
| + // is available. This is not necessarily the case, but appears to be true for
|
| + // Apple devices. See http://crbug.com/392166 for more details.
|
| + if (result < 6)
|
| + return BLUETOOTH_AVAILABLE_WITHOUT_LE;
|
| + return BLUETOOTH_AVAILABLE_WITH_LE;
|
| +}
|
| +
|
| +} // namespace hardware_utility
|
|
|