Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "device/bluetooth/bluetooth_utils.h" |
| 11 | #include "grit/device_bluetooth_strings.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | #include "ui/base/l10n/l10n_util.h" |
| 13 | |
| 14 | namespace device { |
| 15 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | // static |
| 17 | bool BluetoothDevice::IsUUIDValid(const std::string& uuid) { |
| 18 | return !bluetooth_utils::CanonicalUuid(uuid).empty(); |
| 19 | } |
| 20 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 21 | BluetoothDevice::BluetoothDevice() { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | BluetoothDevice::~BluetoothDevice() { |
| 25 | } |
| 26 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 27 | base::string16 BluetoothDevice::GetName() const { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 28 | std::string name = GetDeviceName(); |
| 29 | if (!name.empty()) { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 30 | return base::UTF8ToUTF16(name); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | } else { |
| 32 | return GetAddressWithLocalizedDeviceTypeName(); |
| 33 | } |
| 34 | } |
| 35 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 36 | base::string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const { |
| 37 | base::string16 address_utf16 = base::UTF8ToUTF16(GetAddress()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 38 | BluetoothDevice::DeviceType device_type = GetDeviceType(); |
| 39 | switch (device_type) { |
| 40 | case DEVICE_COMPUTER: |
| 41 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 42 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 43 | case DEVICE_PHONE: |
| 44 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 45 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 46 | case DEVICE_MODEM: |
| 47 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 48 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 49 | case DEVICE_AUDIO: |
| 50 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 51 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 52 | case DEVICE_CAR_AUDIO: |
| 53 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 54 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 55 | case DEVICE_VIDEO: |
| 56 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 57 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 58 | case DEVICE_JOYSTICK: |
| 59 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 60 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 61 | case DEVICE_GAMEPAD: |
| 62 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 63 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 64 | case DEVICE_KEYBOARD: |
| 65 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 66 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 67 | case DEVICE_MOUSE: |
| 68 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 69 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 70 | case DEVICE_TABLET: |
| 71 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 72 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 73 | case DEVICE_KEYBOARD_MOUSE_COMBO: |
| 74 | return l10n_util::GetStringFUTF16( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 75 | IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 76 | default: |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 77 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, |
| 78 | address_utf16); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const { |
| 83 | // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 84 | uint32 bluetooth_class = GetBluetoothClass(); |
| 85 | switch ((bluetooth_class & 0x1f00) >> 8) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 86 | case 0x01: |
| 87 | // Computer major device class. |
| 88 | return DEVICE_COMPUTER; |
| 89 | case 0x02: |
| 90 | // Phone major device class. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 91 | switch ((bluetooth_class & 0xfc) >> 2) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 105 | switch ((bluetooth_class & 0xfc) >> 2) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 123 | switch ((bluetooth_class & 0xc0) >> 6) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 124 | case 0x00: |
| 125 | // "Not a keyboard or pointing device." |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 126 | switch ((bluetooth_class & 0x01e) >> 2) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 142 | switch ((bluetooth_class & 0x01e) >> 2) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 161 | bool BluetoothDevice::IsPairable() const { |
| 162 | DeviceType type = GetDeviceType(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 163 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 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); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 166 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 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. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 173 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 174 | return true; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | bool BluetoothDevice::ProvidesServiceWithUUID( |
| 178 | const std::string& uuid) const { |
| 179 | std::string canonical_uuid = bluetooth_utils::CanonicalUuid(uuid); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 180 | BluetoothDevice::ServiceList services = GetServices(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 190 | } // namespace device |