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