[email protected] | eb4dae6e | 2012-02-19 00:06:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4d7d2f59 | 2011-10-26 23:46:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | cab208eb0 | 2012-10-18 22:15:23 | [diff] [blame] | 5 | #include "device/bluetooth/bluetooth_device.h" |
[email protected] | 4d7d2f59 | 2011-10-26 23:46:21 | [diff] [blame] | 6 | |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 9 | #include "base/utf_string_conversions.h" |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 10 | #include "device/bluetooth/bluetooth_utils.h" |
[email protected] | 8a92bbd | 2013-02-22 01:07:29 | [diff] [blame] | 11 | #include "grit/device_bluetooth_strings.h" |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 12 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 4d7d2f59 | 2011-10-26 23:46:21 | [diff] [blame] | 13 | |
[email protected] | cab208eb0 | 2012-10-18 22:15:23 | [diff] [blame] | 14 | namespace device { |
[email protected] | 4d7d2f59 | 2011-10-26 23:46:21 | [diff] [blame] | 15 | |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 16 | // static |
| 17 | bool BluetoothDevice::IsUUIDValid(const std::string& uuid) { |
| 18 | return !bluetooth_utils::CanonicalUuid(uuid).empty(); |
| 19 | } |
| 20 | |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 21 | BluetoothDevice::BluetoothDevice() { |
[email protected] | 4d7d2f59 | 2011-10-26 23:46:21 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | BluetoothDevice::~BluetoothDevice() { |
| 25 | } |
| 26 | |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 27 | string16 BluetoothDevice::GetName() const { |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 28 | std::string name = GetDeviceName(); |
| 29 | if (!name.empty()) { |
| 30 | return UTF8ToUTF16(name); |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 31 | } else { |
| 32 | return GetAddressWithLocalizedDeviceTypeName(); |
| 33 | } |
| 34 | } |
| 35 | |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 36 | string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const { |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 37 | string16 address_utf16 = UTF8ToUTF16(GetAddress()); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [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, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 42 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 43 | case DEVICE_PHONE: |
| 44 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 45 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 46 | case DEVICE_MODEM: |
| 47 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 48 | address_utf16); |
[email protected] | e7a2039 | 2012-10-09 19:21:30 | [diff] [blame] | 49 | case DEVICE_AUDIO: |
| 50 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 51 | address_utf16); |
[email protected] | e7a2039 | 2012-10-09 19:21:30 | [diff] [blame] | 52 | case DEVICE_CAR_AUDIO: |
| 53 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 54 | address_utf16); |
[email protected] | e7a2039 | 2012-10-09 19:21:30 | [diff] [blame] | 55 | case DEVICE_VIDEO: |
| 56 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 57 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 58 | case DEVICE_JOYSTICK: |
| 59 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 60 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 61 | case DEVICE_GAMEPAD: |
| 62 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 63 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 64 | case DEVICE_KEYBOARD: |
| 65 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 66 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 67 | case DEVICE_MOUSE: |
| 68 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 69 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 70 | case DEVICE_TABLET: |
| 71 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET, |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 72 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 73 | case DEVICE_KEYBOARD_MOUSE_COMBO: |
| 74 | return l10n_util::GetStringFUTF16( |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 75 | IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 76 | default: |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 77 | return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, |
| 78 | address_utf16); |
[email protected] | cc1bc71 | 2012-10-02 01:56:45 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 82 | BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const { |
| 83 | // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 84 | uint32 bluetooth_class = GetBluetoothClass(); |
| 85 | switch ((bluetooth_class & 0x1f00) >> 8) { |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 86 | case 0x01: |
| 87 | // Computer major device class. |
| 88 | return DEVICE_COMPUTER; |
| 89 | case 0x02: |
| 90 | // Phone major device class. |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 91 | switch ((bluetooth_class & 0xfc) >> 2) { |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [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; |
[email protected] | e7a2039 | 2012-10-09 19:21:30 | [diff] [blame] | 103 | case 0x04: |
| 104 | // Audio major device class. |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 105 | switch ((bluetooth_class & 0xfc) >> 2) { |
[email protected] | e7a2039 | 2012-10-09 19:21:30 | [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; |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 121 | case 0x05: |
| 122 | // Peripheral major device class. |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 123 | switch ((bluetooth_class & 0xc0) >> 6) { |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 124 | case 0x00: |
| 125 | // "Not a keyboard or pointing device." |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 126 | switch ((bluetooth_class & 0x01e) >> 2) { |
[email protected] | 28588ac | 2012-05-01 23:57:46 | [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; |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [diff] [blame] | 137 | case 0x01: |
| 138 | // Keyboard. |
| 139 | return DEVICE_KEYBOARD; |
| 140 | case 0x02: |
| 141 | // Pointing device. |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 142 | switch ((bluetooth_class & 0x01e) >> 2) { |
[email protected] | e40e93f7 | 2012-03-09 03:41:28 | [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 | |
[email protected] | 60625cb | 2013-02-26 20:35:07 | [diff] [blame] | 161 | |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 162 | bool BluetoothDevice::ProvidesServiceWithUUID( |
| 163 | const std::string& uuid) const { |
| 164 | std::string canonical_uuid = bluetooth_utils::CanonicalUuid(uuid); |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame^] | 165 | BluetoothDevice::ServiceList services = GetServices(); |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 166 | for (BluetoothDevice::ServiceList::const_iterator iter = services.begin(); |
| 167 | iter != services.end(); |
| 168 | ++iter) { |
| 169 | if (bluetooth_utils::CanonicalUuid(*iter) == canonical_uuid) |
| 170 | return true; |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
[email protected] | cab208eb0 | 2012-10-18 22:15:23 | [diff] [blame] | 175 | } // namespace device |