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 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.10. | |
54 if ([uuid respondsToSelector:@selector(UUIDString)]) { | |
55 uuidString = [uuid UUIDString]; | |
56 } else { | |
57 uuidString = stringWithCBUUID(uuid); | |
58 } | |
59 std::string uuid_c_string = base::SysNSStringToUTF8(uuidString); | |
60 return device::BluetoothUUID(uuid_c_string); | |
61 } | |
62 | |
63 } // namespace | |
64 | |
65 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | |
66 CBPeripheral* peripheral, | |
67 NSDictionary* advertisementData, | |
68 int rssi) { | |
69 Update(peripheral, advertisementData, rssi); | |
70 } | |
71 | |
72 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | |
73 } | |
74 | |
75 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | |
76 NSDictionary* advertisementData, | |
77 int rssi) { | |
78 peripheral_.reset([peripheral retain]); | |
79 rssi_ = rssi; | |
80 ClearServiceData(); | |
81 NSNumber* nbConnectable = | |
82 [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; | |
83 connectable_ = [nbConnectable boolValue]; | |
84 NSDictionary* serviceData = | |
85 [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; | |
86 for (CBUUID* uuid in serviceData) { | |
87 NSData* data = [serviceData objectForKey:uuid]; | |
88 BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid); | |
89 SetServiceData(serviceUUID, (const char*)[data bytes], [data length]); | |
90 } | |
91 } | |
92 | |
93 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { | |
94 return GetPeripheralIdentifier(peripheral_); | |
95 } | |
96 | |
97 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { | |
98 return 0; | |
99 } | |
100 | |
101 std::string BluetoothLowEnergyDeviceMac::GetAddress() const { | |
102 return std::string(); | |
103 } | |
104 | |
105 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource() | |
106 const { | |
107 return VENDOR_ID_UNKNOWN; | |
108 } | |
109 | |
110 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const { | |
111 return 0; | |
112 } | |
113 | |
114 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const { | |
115 return 0; | |
116 } | |
117 | |
118 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const { | |
119 return 0; | |
120 } | |
121 | |
122 int BluetoothLowEnergyDeviceMac::GetRSSI() const { | |
123 return rssi_; | |
124 } | |
125 | |
126 bool BluetoothLowEnergyDeviceMac::IsPaired() const { | |
127 return false; | |
128 } | |
129 | |
130 bool BluetoothLowEnergyDeviceMac::IsConnected() const { | |
131 return [peripheral_ isConnected]; | |
132 } | |
133 | |
134 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { | |
135 return connectable_; | |
136 } | |
137 | |
138 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { | |
139 return false; | |
140 } | |
141 | |
142 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { | |
143 return std::vector<device::BluetoothUUID>(); | |
144 } | |
145 | |
146 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const { | |
147 return false; | |
148 } | |
149 | |
150 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const { | |
151 return false; | |
152 } | |
153 | |
154 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const { | |
155 return false; | |
156 } | |
157 | |
158 void BluetoothLowEnergyDeviceMac::GetConnectionInfo( | |
159 const ConnectionInfoCallback& callback) { | |
160 NOTIMPLEMENTED(); | |
161 } | |
162 | |
163 void BluetoothLowEnergyDeviceMac::Connect( | |
164 PairingDelegate* pairing_delegate, | |
165 const base::Closure& callback, | |
166 const ConnectErrorCallback& error_callback) { | |
167 NOTIMPLEMENTED(); | |
168 } | |
169 | |
170 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) { | |
171 NOTIMPLEMENTED(); | |
172 } | |
173 | |
174 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) { | |
175 NOTIMPLEMENTED(); | |
176 } | |
177 | |
178 void BluetoothLowEnergyDeviceMac::ConfirmPairing() { | |
179 NOTIMPLEMENTED(); | |
180 } | |
181 | |
182 void BluetoothLowEnergyDeviceMac::RejectPairing() { | |
183 NOTIMPLEMENTED(); | |
184 } | |
185 | |
186 void BluetoothLowEnergyDeviceMac::CancelPairing() { | |
187 NOTIMPLEMENTED(); | |
188 } | |
189 | |
190 void BluetoothLowEnergyDeviceMac::Disconnect( | |
191 const base::Closure& callback, | |
192 const ErrorCallback& error_callback) { | |
193 NOTIMPLEMENTED(); | |
194 } | |
195 | |
196 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) { | |
197 NOTIMPLEMENTED(); | |
198 } | |
199 | |
200 void BluetoothLowEnergyDeviceMac::ConnectToService( | |
201 const BluetoothUUID& uuid, | |
202 const ConnectToServiceCallback& callback, | |
203 const ConnectToServiceErrorCallback& error_callback) { | |
204 NOTIMPLEMENTED(); | |
205 } | |
206 | |
207 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( | |
208 const device::BluetoothUUID& uuid, | |
209 const ConnectToServiceCallback& callback, | |
210 const ConnectToServiceErrorCallback& error_callback) { | |
211 NOTIMPLEMENTED(); | |
212 } | |
213 | |
214 void BluetoothLowEnergyDeviceMac::CreateGattConnection( | |
215 const GattConnectionCallback& callback, | |
216 const ConnectErrorCallback& error_callback) { | |
217 NOTIMPLEMENTED(); | |
218 } | |
219 | |
220 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | |
221 return base::SysNSStringToUTF8([peripheral_ name]); | |
222 } | |
223 | |
224 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | |
225 CBPeripheral* peripheral) { | |
226 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9. | |
227 if ([peripheral respondsToSelector:@selector(identifier)]) { | |
228 // When -[CBPeripheral identifier] is available. | |
229 NSUUID* uuid = [peripheral identifier]; | |
230 NSString* uuidString = [uuid UUIDString]; | |
231 return base::SysNSStringToUTF8(uuidString); | |
232 } | |
233 | |
234 base::ScopedCFTypeRef<CFStringRef> str( | |
235 CFUUIDCreateString(NULL, [peripheral UUID])); | |
236 return SysCFStringRefToUTF8(str); | |
237 } | |
OLD | NEW |