5d37a548c6c3fb9f0c67b6a3a69947f434156e4d
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_device_mac.mm
1 // Copyright 2013 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_device_mac.h"
6
7 #include <IOBluetooth/Bluetooth.h>
8 #import <IOBluetooth/objc/IOBluetoothDevice.h>
9 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
10 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
11
12 #include <string>
13
14 #include "base/basictypes.h"
15 #include "base/hash.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/sys_string_conversions.h"
19 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
20 #include "device/bluetooth/bluetooth_profile_mac.h"
21 #include "device/bluetooth/bluetooth_service_record_mac.h"
22 #include "device/bluetooth/bluetooth_socket_mac.h"
23
24 // Replicate specific 10.7 SDK declarations for building with prior SDKs.
25 #if !defined(MAC_OS_X_VERSION_10_7) || \
26     MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
27
28 @interface IOBluetoothDevice (LionSDKDeclarations)
29 - (NSString*)addressString;
30 - (NSString*)name;
31 - (unsigned int)classOfDevice;
32 - (NSArray*)services;
33 @end
34
35 #endif  // MAC_OS_X_VERSION_10_7
36
37 namespace {
38
39 // Converts |uuid| to a IOBluetoothSDPUUID instance.
40 //
41 // |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
42 IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) {
43   DCHECK(uuid.size() == 36);
44   DCHECK(uuid[8] == '-');
45   DCHECK(uuid[13] == '-');
46   DCHECK(uuid[18] == '-');
47   DCHECK(uuid[23] == '-');
48   std::string numbers_only = uuid;
49   numbers_only.erase(23, 1);
50   numbers_only.erase(18, 1);
51   numbers_only.erase(13, 1);
52   numbers_only.erase(8, 1);
53   std::vector<uint8> uuid_bytes_vector;
54   base::HexStringToBytes(numbers_only, &uuid_bytes_vector);
55   DCHECK(uuid_bytes_vector.size() == 16);
56
57   return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0]
58                                     length:uuid_bytes_vector.size()];
59 }
60
61 }  // namespace
62
63 namespace device {
64
65 BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device)
66     : BluetoothDevice(), device_([device retain]) {
67 }
68
69 BluetoothDeviceMac::~BluetoothDeviceMac() {
70   [device_ release];
71 }
72
73 uint32 BluetoothDeviceMac::GetBluetoothClass() const {
74   return [device_ classOfDevice];
75 }
76
77 std::string BluetoothDeviceMac::GetDeviceName() const {
78   return base::SysNSStringToUTF8([device_ name]);
79 }
80
81 std::string BluetoothDeviceMac::GetAddress() const {
82   return base::SysNSStringToUTF8([device_ addressString]);
83 }
84
85 BluetoothDevice::VendorIDSource
86 BluetoothDeviceMac::GetVendorIDSource() const {
87   return VENDOR_ID_UNKNOWN;
88 }
89
90 uint16 BluetoothDeviceMac::GetVendorID() const {
91   return 0;
92 }
93
94 uint16 BluetoothDeviceMac::GetProductID() const {
95   return 0;
96 }
97
98 uint16 BluetoothDeviceMac::GetDeviceID() const {
99   return 0;
100 }
101
102 bool BluetoothDeviceMac::IsPaired() const {
103   return [device_ isPaired];
104 }
105
106 bool BluetoothDeviceMac::IsConnected() const {
107   return [device_ isConnected];
108 }
109
110 bool BluetoothDeviceMac::IsConnectable() const {
111   return false;
112 }
113
114 bool BluetoothDeviceMac::IsConnecting() const {
115   return false;
116 }
117
118 // TODO(keybuk): BluetoothServiceRecord is deprecated; implement this method
119 // without using BluetoothServiceRecord.
120 BluetoothDevice::UUIDList BluetoothDeviceMac::GetUUIDs() const {
121   UUIDList uuids;
122   for (IOBluetoothSDPServiceRecord* service in [device_ services]) {
123     BluetoothServiceRecordMac service_record(service);
124     uuids.push_back(service_record.uuid());
125   }
126   return uuids;
127 }
128
129 bool BluetoothDeviceMac::ExpectingPinCode() const {
130   NOTIMPLEMENTED();
131   return false;
132 }
133
134 bool BluetoothDeviceMac::ExpectingPasskey() const {
135   NOTIMPLEMENTED();
136   return false;
137 }
138
139 bool BluetoothDeviceMac::ExpectingConfirmation() const {
140   NOTIMPLEMENTED();
141   return false;
142 }
143
144 void BluetoothDeviceMac::Connect(
145     PairingDelegate* pairing_delegate,
146     const base::Closure& callback,
147     const ConnectErrorCallback& error_callback) {
148   NOTIMPLEMENTED();
149 }
150
151 void BluetoothDeviceMac::SetPinCode(const std::string& pincode) {
152   NOTIMPLEMENTED();
153 }
154
155 void BluetoothDeviceMac::SetPasskey(uint32 passkey) {
156   NOTIMPLEMENTED();
157 }
158
159 void BluetoothDeviceMac::ConfirmPairing() {
160   NOTIMPLEMENTED();
161 }
162
163 void BluetoothDeviceMac::RejectPairing() {
164   NOTIMPLEMENTED();
165 }
166
167 void BluetoothDeviceMac::CancelPairing() {
168   NOTIMPLEMENTED();
169 }
170
171 void BluetoothDeviceMac::Disconnect(
172     const base::Closure& callback,
173     const ErrorCallback& error_callback) {
174   NOTIMPLEMENTED();
175 }
176
177 void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) {
178   NOTIMPLEMENTED();
179 }
180
181 void BluetoothDeviceMac::ConnectToService(
182     const std::string& service_uuid,
183     const SocketCallback& callback) {
184   IOBluetoothSDPServiceRecord* record =
185       [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID(service_uuid)];
186   if (record != nil) {
187     BluetoothServiceRecordMac service_record(record);
188     scoped_refptr<BluetoothSocket> socket(
189         BluetoothSocketMac::CreateBluetoothSocket(service_record));
190     if (socket.get() != NULL)
191       callback.Run(socket);
192   }
193 }
194
195 void BluetoothDeviceMac::ConnectToProfile(
196     device::BluetoothProfile* profile,
197     const base::Closure& callback,
198     const ErrorCallback& error_callback) {
199   if (static_cast<BluetoothProfileMac*>(profile)->Connect(device_))
200     callback.Run();
201   else
202     error_callback.Run();
203 }
204
205 void BluetoothDeviceMac::SetOutOfBandPairingData(
206     const BluetoothOutOfBandPairingData& data,
207     const base::Closure& callback,
208     const ErrorCallback& error_callback) {
209   NOTIMPLEMENTED();
210 }
211
212 void BluetoothDeviceMac::ClearOutOfBandPairingData(
213     const base::Closure& callback,
214     const ErrorCallback& error_callback) {
215   NOTIMPLEMENTED();
216 }
217
218 }  // namespace device