OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "device/bluetooth/bluetooth_low_energy_device_mac.h" | |
6 | |
7 #import <CoreFoundation/CoreFoundation.h> | |
8 | |
9 #include "base/mac/scoped_cftyperef.h" | |
10 #include "base/mac/sdk_forward_declarations.h" | |
11 #include "base/strings/sys_string_conversions.h" | |
12 | |
13 using device::BluetoothDevice; | |
14 using device::BluetoothLowEnergyDeviceMac; | |
15 | |
16 namespace { | |
17 | |
18 // Converts a CBUUID to a Cocoa string. | |
19 // | |
20 // The string representation can have the following formats: | |
21 // - 16 bit: xxxx | |
22 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
23 // CBUUID supports only 16 bits and 128 bits formats. | |
24 // | |
25 // In OSX < 10.10, -[uuid UUIDString] method is not implemented. It's why we | |
26 // need to provide this function. | |
27 NSString* stringWithCBUUID(CBUUID* uuid) { | |
28 NSData* data = [uuid data]; | |
29 | |
30 NSUInteger bytesToConvert = [data length]; | |
31 const unsigned char* uuidBytes = (const unsigned char*)[data bytes]; | |
32 NSMutableString* outputString = [NSMutableString stringWithCapacity:16]; | |
33 | |
34 for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; | |
35 currentByteIndex++) { | |
36 switch (currentByteIndex) { | |
37 case 3: | |
38 case 5: | |
39 case 7: | |
40 case 9: | |
41 [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; | |
42 break; | |
43 default: | |
44 [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]]; | |
45 } | |
46 } | |
47 return outputString; | |
48 } | |
49 | |
50 // Converts a CBUUID to a BluetoothUUID. | |
51 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { | |
52 NSString* uuidString = nil; | |
53 if ([uuid respondsToSelector:@selector(UUIDString)]) { | |
groby-ooo-7-16
2015/02/02 19:21:12
Please add a TODO: that this can be removed once w
dvh
2015/02/05 22:26:08
Done.
| |
54 uuidString = [uuid UUIDString]; | |
55 } else { | |
56 uuidString = stringWithCBUUID(uuid); | |
57 } | |
58 std::string uuid_c_string = base::SysNSStringToUTF8(uuidString); | |
59 return device::BluetoothUUID(uuid_c_string); | |
60 } | |
61 | |
62 } // namespace | |
63 | |
64 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | |
65 CBPeripheral* peripheral, | |
66 NSDictionary* advertisementData, | |
67 int rssi) { | |
68 Update(peripheral, advertisementData, rssi); | |
69 } | |
70 | |
71 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | |
72 } | |
73 | |
74 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | |
75 NSDictionary* advertisementData, | |
76 int rssi) { | |
77 peripheral_.reset([peripheral retain]); | |
78 rssi_ = rssi; | |
79 ClearServiceData(); | |
80 NSNumber* nbConnectable = | |
81 [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; | |
82 connectable_ = [nbConnectable boolValue]; | |
83 NSDictionary* serviceData = | |
84 [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; | |
85 for (CBUUID* uuid in serviceData) { | |
86 NSData* data = [serviceData objectForKey:uuid]; | |
87 BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid); | |
88 SetServiceData(serviceUUID, (const char*)[data bytes], [data length]); | |
89 } | |
90 } | |
91 | |
92 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { | |
93 return GetPeripheralIdentifier(peripheral_); | |
94 } | |
95 | |
96 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { | |
97 return 0; | |
98 } | |
99 | |
100 std::string BluetoothLowEnergyDeviceMac::GetAddress() const { | |
101 return std::string(); | |
102 } | |
103 | |
104 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource() | |
105 const { | |
106 return VENDOR_ID_UNKNOWN; | |
107 } | |
108 | |
109 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const { | |
110 return 0; | |
111 } | |
112 | |
113 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const { | |
114 return 0; | |
115 } | |
116 | |
117 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const { | |
118 return 0; | |
119 } | |
120 | |
121 int BluetoothLowEnergyDeviceMac::GetRSSI() const { | |
122 return rssi_; | |
123 } | |
124 | |
125 bool BluetoothLowEnergyDeviceMac::IsPaired() const { | |
126 return false; | |
127 } | |
128 | |
129 bool BluetoothLowEnergyDeviceMac::IsConnected() const { | |
130 return [peripheral_ isConnected]; | |
131 } | |
132 | |
133 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { | |
134 return connectable_; | |
135 } | |
136 | |
137 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { | |
138 return false; | |
139 } | |
140 | |
141 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { | |
142 return std::vector<device::BluetoothUUID>(); | |
143 } | |
144 | |
145 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const { | |
146 return false; | |
147 } | |
148 | |
149 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const { | |
150 return false; | |
151 } | |
152 | |
153 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const { | |
154 return false; | |
155 } | |
156 | |
157 void BluetoothLowEnergyDeviceMac::GetConnectionInfo( | |
158 const ConnectionInfoCallback& callback) { | |
159 NOTIMPLEMENTED(); | |
160 } | |
161 | |
162 void BluetoothLowEnergyDeviceMac::Connect( | |
163 PairingDelegate* pairing_delegate, | |
164 const base::Closure& callback, | |
165 const ConnectErrorCallback& error_callback) { | |
166 NOTIMPLEMENTED(); | |
167 } | |
168 | |
169 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) { | |
170 NOTIMPLEMENTED(); | |
171 } | |
172 | |
173 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) { | |
174 NOTIMPLEMENTED(); | |
175 } | |
176 | |
177 void BluetoothLowEnergyDeviceMac::ConfirmPairing() { | |
178 NOTIMPLEMENTED(); | |
179 } | |
180 | |
181 void BluetoothLowEnergyDeviceMac::RejectPairing() { | |
182 NOTIMPLEMENTED(); | |
183 } | |
184 | |
185 void BluetoothLowEnergyDeviceMac::CancelPairing() { | |
186 NOTIMPLEMENTED(); | |
187 } | |
188 | |
189 void BluetoothLowEnergyDeviceMac::Disconnect( | |
190 const base::Closure& callback, | |
191 const ErrorCallback& error_callback) { | |
192 NOTIMPLEMENTED(); | |
193 } | |
194 | |
195 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) { | |
196 NOTIMPLEMENTED(); | |
197 } | |
198 | |
199 void BluetoothLowEnergyDeviceMac::ConnectToService( | |
200 const BluetoothUUID& uuid, | |
201 const ConnectToServiceCallback& callback, | |
202 const ConnectToServiceErrorCallback& error_callback) { | |
203 NOTIMPLEMENTED(); | |
204 } | |
205 | |
206 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( | |
207 const device::BluetoothUUID& uuid, | |
208 const ConnectToServiceCallback& callback, | |
209 const ConnectToServiceErrorCallback& error_callback) { | |
210 NOTIMPLEMENTED(); | |
211 } | |
212 | |
213 void BluetoothLowEnergyDeviceMac::CreateGattConnection( | |
214 const GattConnectionCallback& callback, | |
215 const ConnectErrorCallback& error_callback) { | |
216 NOTIMPLEMENTED(); | |
217 } | |
218 | |
219 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | |
220 return base::SysNSStringToUTF8([peripheral_ name]); | |
221 } | |
222 | |
223 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | |
224 CBPeripheral* peripheral) { | |
225 if ([peripheral respondsToSelector:@selector(identifier)]) { | |
226 // When -[CBPeripheral identifier] is available. | |
227 NSUUID* uuid = [peripheral identifier]; | |
228 NSString* uuidString = [uuid UUIDString]; | |
229 return base::SysNSStringToUTF8(uuidString); | |
230 } | |
231 | |
232 base::ScopedCFTypeRef<CFStringRef> str( | |
233 CFUUIDCreateString(NULL, [peripheral UUID])); | |
234 return SysCFStringRefToUTF8(str); | |
235 } | |
OLD | NEW |