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 hardware_utility { | |
14 | |
15 // Returns the Bluetooth driver's Link Management Protocol version. | |
16 // Returns -1 if no Bluetooth driver is present. | |
Mark Mentovai
2014/07/09 17:53:00
Use constants for these.
erikchen
2014/07/09 20:14:42
Done.
| |
17 // Returns -2 on an unexpected error. | |
18 int GetBluetoothLMPVersion() { | |
Mark Mentovai
2014/07/09 17:53:00
Put this in an unnamed namespace.
erikchen
2014/07/09 20:14:42
Done.
| |
19 base::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict( | |
20 IOServiceMatching("IOBluetoothHCIController")); | |
21 if (!matching_dict) | |
22 return -2; | |
23 | |
24 // IOServiceGetMatchingServices takes ownership of matching_dict. | |
25 io_iterator_t iter; | |
26 int kr = IOServiceGetMatchingServices( | |
27 kIOMasterPortDefault, matching_dict.release(), &iter); | |
28 if (kr != KERN_SUCCESS) | |
29 return -1; | |
30 base::mac::ScopedIOObject<io_iterator_t> scoped_iter(iter); | |
31 | |
32 int version = 0; | |
33 bool success = false; | |
34 base::mac::ScopedIOObject<io_service_t> device; | |
35 while (device.reset(IOIteratorNext(scoped_iter.get())), device) { | |
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 // Check the LMP version. | |
44 // See the following link for the relationship between LMP version and | |
45 // Bluetooth Core Specification version. | |
46 // https://www.bluetooth.org/en-us/specification/assigned-numbers/link-manag er | |
47 // Bluetooth Low Energy is available in Bluetooth Core Specification | |
48 // Version 4.0+. | |
Mark Mentovai
2014/07/09 17:53:00
As I understand it, Bluetooth 4.0 allows devices t
erikchen
2014/07/09 20:14:42
Hm. Right you are.
I spent some time looking int
| |
49 // http://en.wikipedia.org/wiki/Bluetooth_low_energy | |
50 NSDictionary* objc_dict = base::mac::CFToNSCast(scoped_dict.get()); | |
51 NSNumber* lmp_version = | |
52 base::mac::ObjCCast<NSNumber>([objc_dict objectForKey:@"LMPVersion"]); | |
53 if (!lmp_version) | |
54 continue; | |
55 | |
56 // A bluetooth device with an LMP version has been found. Record the | |
57 // highest version among all devices. | |
58 success = true; | |
59 version = std::max(version, [lmp_version intValue]); | |
60 } | |
61 | |
62 if (!success) | |
63 return -2; | |
64 | |
65 return version; | |
66 } | |
67 | |
68 BluetoothAvailability GetBluetoothAvailability() { | |
69 static int result = GetBluetoothLMPVersion(); | |
70 if (result == -2) | |
71 return BLUETOOTH_AVAILABILITY_ERROR; | |
72 if (result == -1) | |
73 return BLUETOOTH_NOT_AVAILABLE; | |
74 if (result < 6) | |
75 return BLUETOOTH_AVAILABLE_WITHOUT_LE; | |
76 return BLUETOOTH_AVAILABLE_WITH_LE; | |
77 } | |
78 | |
79 } // namespace hardware_utility | |
OLD | NEW |