Extensions: Store disable reasons in Sync
[chromium-blink-merge.git] / device / bluetooth / bluetooth_low_energy_device_mac.mm
blob0357ad95276aca951b3d3a10d7032ff4c230c32f
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.
5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
7 #import <CoreFoundation/CoreFoundation.h>
9 #include "base/mac/scoped_cftyperef.h"
10 #include "base/mac/sdk_forward_declarations.h"
11 #include "base/strings/sys_string_conversions.h"
13 using device::BluetoothDevice;
14 using device::BluetoothLowEnergyDeviceMac;
16 namespace {
18 // Converts a CBUUID to a Cocoa string.
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.
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];
30   NSUInteger bytesToConvert = [data length];
31   const unsigned char* uuidBytes = (const unsigned char*)[data bytes];
32   NSMutableString* outputString = [NSMutableString stringWithCapacity:16];
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;
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);
63 }  // namespace
65 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac(
66     CBPeripheral* peripheral,
67     NSDictionary* advertisementData,
68     int rssi) {
69   Update(peripheral, advertisementData, rssi);
72 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() {
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   }
93 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const {
94   return GetPeripheralIdentifier(peripheral_);
97 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const {
98   return 0;
101 std::string BluetoothLowEnergyDeviceMac::GetAddress() const {
102   return std::string();
105 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource()
106     const {
107   return VENDOR_ID_UNKNOWN;
110 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const {
111   return 0;
114 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const {
115   return 0;
118 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const {
119   return 0;
122 int BluetoothLowEnergyDeviceMac::GetRSSI() const {
123   return rssi_;
126 bool BluetoothLowEnergyDeviceMac::IsPaired() const {
127   return false;
130 bool BluetoothLowEnergyDeviceMac::IsConnected() const {
131   return [peripheral_ isConnected];
134 bool BluetoothLowEnergyDeviceMac::IsConnectable() const {
135   return connectable_;
138 bool BluetoothLowEnergyDeviceMac::IsConnecting() const {
139   return false;
142 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const {
143   return std::vector<device::BluetoothUUID>();
146 int16 BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const {
147   return kUnknownPower;
150 int16 BluetoothLowEnergyDeviceMac::GetInquiryTxPower() const {
151   NOTIMPLEMENTED();
152   return kUnknownPower;
155 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const {
156   return false;
159 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const {
160   return false;
163 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const {
164   return false;
167 void BluetoothLowEnergyDeviceMac::GetConnectionInfo(
168     const ConnectionInfoCallback& callback) {
169   NOTIMPLEMENTED();
172 void BluetoothLowEnergyDeviceMac::Connect(
173     PairingDelegate* pairing_delegate,
174     const base::Closure& callback,
175     const ConnectErrorCallback& error_callback) {
176   NOTIMPLEMENTED();
179 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) {
180   NOTIMPLEMENTED();
183 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) {
184   NOTIMPLEMENTED();
187 void BluetoothLowEnergyDeviceMac::ConfirmPairing() {
188   NOTIMPLEMENTED();
191 void BluetoothLowEnergyDeviceMac::RejectPairing() {
192   NOTIMPLEMENTED();
195 void BluetoothLowEnergyDeviceMac::CancelPairing() {
196   NOTIMPLEMENTED();
199 void BluetoothLowEnergyDeviceMac::Disconnect(
200     const base::Closure& callback,
201     const ErrorCallback& error_callback) {
202   NOTIMPLEMENTED();
205 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) {
206   NOTIMPLEMENTED();
209 void BluetoothLowEnergyDeviceMac::ConnectToService(
210     const BluetoothUUID& uuid,
211     const ConnectToServiceCallback& callback,
212     const ConnectToServiceErrorCallback& error_callback) {
213   NOTIMPLEMENTED();
216 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely(
217     const device::BluetoothUUID& uuid,
218     const ConnectToServiceCallback& callback,
219     const ConnectToServiceErrorCallback& error_callback) {
220   NOTIMPLEMENTED();
223 void BluetoothLowEnergyDeviceMac::CreateGattConnection(
224     const GattConnectionCallback& callback,
225     const ConnectErrorCallback& error_callback) {
226   NOTIMPLEMENTED();
229 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const {
230   return base::SysNSStringToUTF8([peripheral_ name]);
233 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(
234     CBPeripheral* peripheral) {
235   // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9.
236   if ([peripheral respondsToSelector:@selector(identifier)]) {
237     // When -[CBPeripheral identifier] is available.
238     NSUUID* uuid = [peripheral identifier];
239     NSString* uuidString = [uuid UUIDString];
240     return base::SysNSStringToUTF8(uuidString);
241   }
243   base::ScopedCFTypeRef<CFStringRef> str(
244       CFUUIDCreateString(NULL, [peripheral UUID]));
245   return SysCFStringRefToUTF8(str);