Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_api_utils.cc
blob05bd0765245fab6e495ec3d7c27769220a679acf
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;
18 namespace {
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;
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;
88 } // namespace
90 namespace extensions {
91 namespace core_api {
92 namespace bluetooth {
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()));
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,
124 AdapterState* out) {
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