Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_service_record_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_service_record_mac.h"
6
7 #include <IOBluetooth/Bluetooth.h>
8 #import <IOBluetooth/IOBluetoothUtilities.h>
9 #import <IOBluetooth/objc/IOBluetoothDevice.h>
10 #import <IOBluetooth/objc/IOBluetoothSDPDataElement.h>
11 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
12 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
13
14 #include <string>
15
16 #include "base/basictypes.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/sys_string_conversions.h"
19
20 namespace {
21
22 void ExtractUuid(IOBluetoothSDPDataElement* service_class_data,
23                  std::string* uuid) {
24   NSArray* inner_elements = [service_class_data getArrayValue];
25   IOBluetoothSDPUUID* sdp_uuid = nil;
26   for (IOBluetoothSDPDataElement* inner_element in inner_elements) {
27     if ([inner_element getTypeDescriptor] == kBluetoothSDPDataElementTypeUUID) {
28       sdp_uuid = [[inner_element getUUIDValue] getUUIDWithLength:16];
29       break;
30     }
31   }
32   if (sdp_uuid != nil) {
33     const uint8* uuid_bytes = reinterpret_cast<const uint8*>([sdp_uuid bytes]);
34     *uuid = base::StringPrintf(
35         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
36         uuid_bytes[0], uuid_bytes[1], uuid_bytes[2], uuid_bytes[3],
37         uuid_bytes[4], uuid_bytes[5], uuid_bytes[6], uuid_bytes[7],
38         uuid_bytes[8], uuid_bytes[9], uuid_bytes[10], uuid_bytes[11],
39         uuid_bytes[12], uuid_bytes[13], uuid_bytes[14], uuid_bytes[15]);
40   }
41 }
42
43 }  // namespace
44
45 namespace device {
46
47 BluetoothServiceRecordMac::BluetoothServiceRecordMac(
48     IOBluetoothSDPServiceRecord* record)
49     : BluetoothServiceRecord() {
50   name_ = base::SysNSStringToUTF8([record getServiceName]);
51   device_ = [[record device] retain];
52   address_ = base::SysNSStringToUTF8(IOBluetoothNSStringFromDeviceAddress(
53       [device_ getAddress]));
54
55   // TODO(youngki): Extract these values from |record|.
56   supports_hid_ = false;
57   hid_reconnect_initiate_ = false;
58   hid_normally_connectable_ = false;
59
60   supports_rfcomm_ =
61       [record getRFCOMMChannelID:&rfcomm_channel_] == kIOReturnSuccess;
62
63   const BluetoothSDPServiceAttributeID service_class_id = 1;
64
65   IOBluetoothSDPDataElement* service_class_data =
66       [record getAttributeDataElement:service_class_id];
67   if ([service_class_data getTypeDescriptor] ==
68           kBluetoothSDPDataElementTypeDataElementSequence) {
69     std::string uuid_str;
70     ExtractUuid(service_class_data, &uuid_str);
71     uuid_ = BluetoothUUID(uuid_str);
72   }
73 }
74
75 BluetoothServiceRecordMac::~BluetoothServiceRecordMac() {
76   [device_ release];
77 }
78
79 }  // namespace device