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::core_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
);
123 void PopulateAdapterState(const device::BluetoothAdapter
& adapter
,
125 out
->discovering
= adapter
.IsDiscovering();
126 out
->available
= adapter
.IsPresent();
127 out
->powered
= adapter
.IsPowered();
128 out
->name
= adapter
.GetName();
129 out
->address
= adapter
.GetAddress();
132 } // namespace bluetooth
133 } // namespace core_api
134 } // namespace extensions