- add sources.
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_device.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 "device/bluetooth/bluetooth_device.h"
6
7 #include <string>
8
9 #include "base/strings/utf_string_conversions.h"
10 #include "device/bluetooth/bluetooth_utils.h"
11 #include "grit/device_bluetooth_strings.h"
12 #include "ui/base/l10n/l10n_util.h"
13
14 namespace device {
15
16 // static
17 bool BluetoothDevice::IsUUIDValid(const std::string& uuid) {
18   return !bluetooth_utils::CanonicalUuid(uuid).empty();
19 }
20
21 BluetoothDevice::BluetoothDevice() {
22 }
23
24 BluetoothDevice::~BluetoothDevice() {
25 }
26
27 string16 BluetoothDevice::GetName() const {
28   std::string name = GetDeviceName();
29   if (!name.empty()) {
30     return UTF8ToUTF16(name);
31   } else {
32     return GetAddressWithLocalizedDeviceTypeName();
33   }
34 }
35
36 string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const {
37   string16 address_utf16 = UTF8ToUTF16(GetAddress());
38   BluetoothDevice::DeviceType device_type = GetDeviceType();
39   switch (device_type) {
40     case DEVICE_COMPUTER:
41       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER,
42                                         address_utf16);
43     case DEVICE_PHONE:
44       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE,
45                                         address_utf16);
46     case DEVICE_MODEM:
47       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM,
48                                         address_utf16);
49     case DEVICE_AUDIO:
50       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO,
51                                         address_utf16);
52     case DEVICE_CAR_AUDIO:
53       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO,
54                                         address_utf16);
55     case DEVICE_VIDEO:
56       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO,
57                                         address_utf16);
58     case DEVICE_JOYSTICK:
59       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK,
60                                         address_utf16);
61     case DEVICE_GAMEPAD:
62       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD,
63                                         address_utf16);
64     case DEVICE_KEYBOARD:
65       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD,
66                                         address_utf16);
67     case DEVICE_MOUSE:
68       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE,
69                                         address_utf16);
70     case DEVICE_TABLET:
71       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET,
72                                         address_utf16);
73     case DEVICE_KEYBOARD_MOUSE_COMBO:
74       return l10n_util::GetStringFUTF16(
75           IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address_utf16);
76     default:
77       return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN,
78                                         address_utf16);
79   }
80 }
81
82 BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const {
83   // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
84   uint32 bluetooth_class = GetBluetoothClass();
85   switch ((bluetooth_class & 0x1f00) >> 8) {
86     case 0x01:
87       // Computer major device class.
88       return DEVICE_COMPUTER;
89     case 0x02:
90       // Phone major device class.
91       switch ((bluetooth_class & 0xfc) >> 2) {
92         case 0x01:
93         case 0x02:
94         case 0x03:
95           // Cellular, cordless and smart phones.
96           return DEVICE_PHONE;
97         case 0x04:
98         case 0x05:
99           // Modems: wired or voice gateway and common ISDN access.
100           return DEVICE_MODEM;
101       }
102       break;
103     case 0x04:
104       // Audio major device class.
105       switch ((bluetooth_class & 0xfc) >> 2) {
106         case 0x08:
107           // Car audio.
108           return DEVICE_CAR_AUDIO;
109         case 0x0b:
110         case 0x0c:
111         case 0x0d:
112         case 0x0e:
113         case 0x0f:
114         case 0x010:
115           // Video devices.
116           return DEVICE_VIDEO;
117         default:
118           return DEVICE_AUDIO;
119       }
120       break;
121     case 0x05:
122       // Peripheral major device class.
123       switch ((bluetooth_class & 0xc0) >> 6) {
124         case 0x00:
125           // "Not a keyboard or pointing device."
126           switch ((bluetooth_class & 0x01e) >> 2) {
127             case 0x01:
128               // Joystick.
129               return DEVICE_JOYSTICK;
130             case 0x02:
131               // Gamepad.
132               return DEVICE_GAMEPAD;
133             default:
134               return DEVICE_PERIPHERAL;
135           }
136           break;
137         case 0x01:
138           // Keyboard.
139           return DEVICE_KEYBOARD;
140         case 0x02:
141           // Pointing device.
142           switch ((bluetooth_class & 0x01e) >> 2) {
143             case 0x05:
144               // Digitizer tablet.
145               return DEVICE_TABLET;
146             default:
147               // Mouse.
148               return DEVICE_MOUSE;
149           }
150           break;
151         case 0x03:
152           // Combo device.
153           return DEVICE_KEYBOARD_MOUSE_COMBO;
154       }
155       break;
156   }
157
158   return DEVICE_UNKNOWN;
159 }
160
161 bool BluetoothDevice::IsPairable() const {
162   DeviceType type = GetDeviceType();
163
164   // Get the vendor part of the address: "00:11:22" for "00:11:22:33:44:55"
165   std::string vendor = GetAddress().substr(0, 8);
166
167   // Verbatim "Bluetooth Mouse", model 96674
168   if ((type == DEVICE_MOUSE && vendor == "00:12:A1") ||
169   // Microsoft "Microsoft Bluetooth Notebook Mouse 5000", model X807028-001
170       (type == DEVICE_MOUSE && vendor == "7C:ED:8D"))
171       return false;
172   // TODO: Move this database into a config file.
173
174   return true;
175 }
176
177 bool BluetoothDevice::ProvidesServiceWithUUID(
178     const std::string& uuid) const {
179   std::string canonical_uuid = bluetooth_utils::CanonicalUuid(uuid);
180   BluetoothDevice::ServiceList services = GetServices();
181   for (BluetoothDevice::ServiceList::const_iterator iter = services.begin();
182        iter != services.end();
183        ++iter) {
184     if (bluetooth_utils::CanonicalUuid(*iter) == canonical_uuid)
185       return true;
186   }
187   return false;
188 }
189
190 }  // namespace device