Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / api / bluetooth / bluetooth_api_utils.cc
1 // Copyright (c) 2012 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 "extensions/browser/api/bluetooth/bluetooth_api_utils.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h"
9 #include "device/bluetooth/bluetooth_adapter.h"
10 #include "device/bluetooth/bluetooth_device.h"
11 #include "extensions/common/api/bluetooth.h"
12
13 namespace bluetooth = extensions::core_api::bluetooth;
14
15 using device::BluetoothDevice;
16 using bluetooth::VendorIdSource;
17
18 namespace {
19
20 bool ConvertVendorIDSourceToApi(const BluetoothDevice::VendorIDSource& input,
21                                 bluetooth::VendorIdSource* output) {
22   switch (input) {
23     case BluetoothDevice::VENDOR_ID_UNKNOWN:
24       *output = bluetooth::VENDOR_ID_SOURCE_NONE;
25       return true;
26     case BluetoothDevice::VENDOR_ID_BLUETOOTH:
27       *output = bluetooth::VENDOR_ID_SOURCE_BLUETOOTH;
28       return true;
29     case BluetoothDevice::VENDOR_ID_USB:
30       *output = bluetooth::VENDOR_ID_SOURCE_USB;
31       return true;
32     default:
33       NOTREACHED();
34       return false;
35   }
36 }
37
38 bool ConvertDeviceTypeToApi(const BluetoothDevice::DeviceType& input,
39                             bluetooth::DeviceType* output) {
40   switch (input) {
41     case BluetoothDevice::DEVICE_UNKNOWN:
42       *output = bluetooth::DEVICE_TYPE_NONE;
43       return true;
44     case BluetoothDevice::DEVICE_COMPUTER:
45       *output = bluetooth::DEVICE_TYPE_COMPUTER;
46       return true;
47     case BluetoothDevice::DEVICE_PHONE:
48       *output = bluetooth::DEVICE_TYPE_PHONE;
49       return true;
50     case BluetoothDevice::DEVICE_MODEM:
51       *output = bluetooth::DEVICE_TYPE_MODEM;
52       return true;
53     case BluetoothDevice::DEVICE_AUDIO:
54       *output = bluetooth::DEVICE_TYPE_AUDIO;
55       return true;
56     case BluetoothDevice::DEVICE_CAR_AUDIO:
57       *output = bluetooth::DEVICE_TYPE_CARAUDIO;
58       return true;
59     case BluetoothDevice::DEVICE_VIDEO:
60       *output = bluetooth::DEVICE_TYPE_VIDEO;
61       return true;
62     case BluetoothDevice::DEVICE_PERIPHERAL:
63       *output = bluetooth::DEVICE_TYPE_PERIPHERAL;
64       return true;
65     case BluetoothDevice::DEVICE_JOYSTICK:
66       *output = bluetooth::DEVICE_TYPE_JOYSTICK;
67       return true;
68     case BluetoothDevice::DEVICE_GAMEPAD:
69       *output = bluetooth::DEVICE_TYPE_GAMEPAD;
70       return true;
71     case BluetoothDevice::DEVICE_KEYBOARD:
72       *output = bluetooth::DEVICE_TYPE_KEYBOARD;
73       return true;
74     case BluetoothDevice::DEVICE_MOUSE:
75       *output = bluetooth::DEVICE_TYPE_MOUSE;
76       return true;
77     case BluetoothDevice::DEVICE_TABLET:
78       *output = bluetooth::DEVICE_TYPE_TABLET;
79       return true;
80     case BluetoothDevice::DEVICE_KEYBOARD_MOUSE_COMBO:
81       *output = bluetooth::DEVICE_TYPE_KEYBOARDMOUSECOMBO;
82       return true;
83     default:
84       return false;
85   }
86 }
87
88 }  // namespace
89
90 namespace extensions {
91 namespace core_api {
92 namespace bluetooth {
93
94 void BluetoothDeviceToApiDevice(const device::BluetoothDevice& device,
95                                 Device* out) {
96   out->address = device.GetAddress();
97   out->name.reset(new std::string(base::UTF16ToUTF8(device.GetName())));
98   out->device_class.reset(new int(device.GetBluetoothClass()));
99
100   // Only include the Device ID members when one exists for the device, and
101   // always include all or none.
102   if (ConvertVendorIDSourceToApi(device.GetVendorIDSource(),
103                                  &(out->vendor_id_source)) &&
104       out->vendor_id_source != VENDOR_ID_SOURCE_NONE) {
105     out->vendor_id.reset(new int(device.GetVendorID()));
106     out->product_id.reset(new int(device.GetProductID()));
107     out->device_id.reset(new int(device.GetDeviceID()));
108   }
109
110   ConvertDeviceTypeToApi(device.GetDeviceType(), &(out->type));
111
112   out->paired.reset(new bool(device.IsPaired()));
113   out->connected.reset(new bool(device.IsConnected()));
114
115   int rssi = device.GetRSSI();
116   if (rssi != BluetoothDevice::kUnknownPower)
117     out->rssi.reset(new int(rssi));
118
119   if (*out->connected) {
120     int current_transmit_power = device.GetCurrentHostTransmitPower();
121     if (current_transmit_power != BluetoothDevice::kUnknownPower)
122       out->current_host_transmit_power.reset(new int(current_transmit_power));
123     int maximum_transmit_power = device.GetMaximumHostTransmitPower();
124     if (maximum_transmit_power != BluetoothDevice::kUnknownPower)
125       out->maximum_host_transmit_power.reset(new int(maximum_transmit_power));
126   }
127
128   std::vector<std::string>* string_uuids = new std::vector<std::string>();
129   const device::BluetoothDevice::UUIDList& uuids = device.GetUUIDs();
130   for (device::BluetoothDevice::UUIDList::const_iterator iter = uuids.begin();
131        iter != uuids.end(); ++iter)
132     string_uuids->push_back(iter->canonical_value());
133   out->uuids.reset(string_uuids);
134 }
135
136 void PopulateAdapterState(const device::BluetoothAdapter& adapter,
137                           AdapterState* out) {
138   out->discovering = adapter.IsDiscovering();
139   out->available = adapter.IsPresent();
140   out->powered = adapter.IsPowered();
141   out->name = adapter.GetName();
142   out->address = adapter.GetAddress();
143 }
144
145 }  // namespace bluetooth
146 }  // namespace core_api
147 }  // namespace extensions