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