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.
5 #include "extensions/browser/api/bluetooth/bluetooth_api_utils.h"
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"
13 namespace bluetooth
= extensions::api::bluetooth
;
15 using device::BluetoothDevice
;
16 using bluetooth::VendorIdSource
;
20 bool ConvertVendorIDSourceToApi(const BluetoothDevice::VendorIDSource
& input
,
21 bluetooth::VendorIdSource
* output
) {
23 case BluetoothDevice::VENDOR_ID_UNKNOWN
:
24 *output
= bluetooth::VENDOR_ID_SOURCE_NONE
;
26 case BluetoothDevice::VENDOR_ID_BLUETOOTH
:
27 *output
= bluetooth::VENDOR_ID_SOURCE_BLUETOOTH
;
29 case BluetoothDevice::VENDOR_ID_USB
:
30 *output
= bluetooth::VENDOR_ID_SOURCE_USB
;
38 bool ConvertDeviceTypeToApi(const BluetoothDevice::DeviceType
& input
,
39 bluetooth::DeviceType
* output
) {
41 case BluetoothDevice::DEVICE_UNKNOWN
:
42 *output
= bluetooth::DEVICE_TYPE_NONE
;
44 case BluetoothDevice::DEVICE_COMPUTER
:
45 *output
= bluetooth::DEVICE_TYPE_COMPUTER
;
47 case BluetoothDevice::DEVICE_PHONE
:
48 *output
= bluetooth::DEVICE_TYPE_PHONE
;
50 case BluetoothDevice::DEVICE_MODEM
:
51 *output
= bluetooth::DEVICE_TYPE_MODEM
;
53 case BluetoothDevice::DEVICE_AUDIO
:
54 *output
= bluetooth::DEVICE_TYPE_AUDIO
;
56 case BluetoothDevice::DEVICE_CAR_AUDIO
:
57 *output
= bluetooth::DEVICE_TYPE_CARAUDIO
;
59 case BluetoothDevice::DEVICE_VIDEO
:
60 *output
= bluetooth::DEVICE_TYPE_VIDEO
;
62 case BluetoothDevice::DEVICE_PERIPHERAL
:
63 *output
= bluetooth::DEVICE_TYPE_PERIPHERAL
;
65 case BluetoothDevice::DEVICE_JOYSTICK
:
66 *output
= bluetooth::DEVICE_TYPE_JOYSTICK
;
68 case BluetoothDevice::DEVICE_GAMEPAD
:
69 *output
= bluetooth::DEVICE_TYPE_GAMEPAD
;
71 case BluetoothDevice::DEVICE_KEYBOARD
:
72 *output
= bluetooth::DEVICE_TYPE_KEYBOARD
;
74 case BluetoothDevice::DEVICE_MOUSE
:
75 *output
= bluetooth::DEVICE_TYPE_MOUSE
;
77 case BluetoothDevice::DEVICE_TABLET
:
78 *output
= bluetooth::DEVICE_TYPE_TABLET
;
80 case BluetoothDevice::DEVICE_KEYBOARD_MOUSE_COMBO
:
81 *output
= bluetooth::DEVICE_TYPE_KEYBOARDMOUSECOMBO
;
90 namespace extensions
{
94 void BluetoothDeviceToApiDevice(const device::BluetoothDevice
& device
,
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()));
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()));
110 ConvertDeviceTypeToApi(device
.GetDeviceType(), &(out
->type
));
112 out
->paired
.reset(new bool(device
.IsPaired()));
113 out
->connected
.reset(new bool(device
.IsConnected()));
115 std::vector
<std::string
>* string_uuids
= new std::vector
<std::string
>();
116 const device::BluetoothDevice::UUIDList
& uuids
= device
.GetUUIDs();
117 for (device::BluetoothDevice::UUIDList::const_iterator iter
= uuids
.begin();
118 iter
!= uuids
.end(); ++iter
)
119 string_uuids
->push_back(iter
->canonical_value());
120 out
->uuids
.reset(string_uuids
);
122 if (device
.GetInquiryRSSI() != device::BluetoothDevice::kUnknownPower
)
123 out
->inquiry_rssi
.reset(new int(device
.GetInquiryRSSI()));
125 out
->inquiry_rssi
.reset();
127 if (device
.GetInquiryTxPower() != device::BluetoothDevice::kUnknownPower
)
128 out
->inquiry_tx_power
.reset(new int(device
.GetInquiryTxPower()));
130 out
->inquiry_tx_power
.reset();
133 void PopulateAdapterState(const device::BluetoothAdapter
& adapter
,
135 out
->discovering
= adapter
.IsDiscovering();
136 out
->available
= adapter
.IsPresent();
137 out
->powered
= adapter
.IsPowered();
138 out
->name
= adapter
.GetName();
139 out
->address
= adapter
.GetAddress();
142 } // namespace bluetooth
144 } // namespace extensions