| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/bluetooth_device.h" | 5 #include "device/bluetooth/bluetooth_device.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 base::Optional<std::string> name = GetName(); | 94 base::Optional<std::string> name = GetName(); |
| 95 if (name && HasGraphicCharacter(name.value())) { | 95 if (name && HasGraphicCharacter(name.value())) { |
| 96 return base::UTF8ToUTF16(name.value()); | 96 return base::UTF8ToUTF16(name.value()); |
| 97 } else { | 97 } else { |
| 98 return GetAddressWithLocalizedDeviceTypeName(); | 98 return GetAddressWithLocalizedDeviceTypeName(); |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 | 101 |
| 102 base::string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const { | 102 base::string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const { |
| 103 base::string16 address_utf16 = base::UTF8ToUTF16(GetAddress()); | 103 base::string16 address_utf16 = base::UTF8ToUTF16(GetAddress()); |
| 104 BluetoothDevice::DeviceType device_type = GetDeviceType(); | 104 BluetoothDeviceType device_type = GetDeviceType(); |
| 105 switch (device_type) { | 105 switch (device_type) { |
| 106 case DEVICE_COMPUTER: | 106 case BluetoothDeviceType::COMPUTER: |
| 107 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER, | 107 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER, |
| 108 address_utf16); | 108 address_utf16); |
| 109 case DEVICE_PHONE: | 109 case BluetoothDeviceType::PHONE: |
| 110 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE, | 110 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE, |
| 111 address_utf16); | 111 address_utf16); |
| 112 case DEVICE_MODEM: | 112 case BluetoothDeviceType::MODEM: |
| 113 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM, | 113 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM, |
| 114 address_utf16); | 114 address_utf16); |
| 115 case DEVICE_AUDIO: | 115 case BluetoothDeviceType::AUDIO: |
| 116 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO, | 116 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO, |
| 117 address_utf16); | 117 address_utf16); |
| 118 case DEVICE_CAR_AUDIO: | 118 case BluetoothDeviceType::CAR_AUDIO: |
| 119 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO, | 119 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO, |
| 120 address_utf16); | 120 address_utf16); |
| 121 case DEVICE_VIDEO: | 121 case BluetoothDeviceType::VIDEO: |
| 122 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO, | 122 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO, |
| 123 address_utf16); | 123 address_utf16); |
| 124 case DEVICE_JOYSTICK: | 124 case BluetoothDeviceType::JOYSTICK: |
| 125 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK, | 125 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK, |
| 126 address_utf16); | 126 address_utf16); |
| 127 case DEVICE_GAMEPAD: | 127 case BluetoothDeviceType::GAMEPAD: |
| 128 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD, | 128 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD, |
| 129 address_utf16); | 129 address_utf16); |
| 130 case DEVICE_KEYBOARD: | 130 case BluetoothDeviceType::KEYBOARD: |
| 131 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD, | 131 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD, |
| 132 address_utf16); | 132 address_utf16); |
| 133 case DEVICE_MOUSE: | 133 case BluetoothDeviceType::MOUSE: |
| 134 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE, | 134 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE, |
| 135 address_utf16); | 135 address_utf16); |
| 136 case DEVICE_TABLET: | 136 case BluetoothDeviceType::TABLET: |
| 137 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET, | 137 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET, |
| 138 address_utf16); | 138 address_utf16); |
| 139 case DEVICE_KEYBOARD_MOUSE_COMBO: | 139 case BluetoothDeviceType::KEYBOARD_MOUSE_COMBO: |
| 140 return l10n_util::GetStringFUTF16( | 140 return l10n_util::GetStringFUTF16( |
| 141 IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address_utf16); | 141 IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address_utf16); |
| 142 default: | 142 default: |
| 143 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, | 143 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, |
| 144 address_utf16); | 144 address_utf16); |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const { | 148 BluetoothDeviceType BluetoothDevice::GetDeviceType() const { |
| 149 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm | 149 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm |
| 150 uint32_t bluetooth_class = GetBluetoothClass(); | 150 uint32_t bluetooth_class = GetBluetoothClass(); |
| 151 switch ((bluetooth_class & 0x1f00) >> 8) { | 151 switch ((bluetooth_class & 0x1f00) >> 8) { |
| 152 case 0x01: | 152 case 0x01: |
| 153 // Computer major device class. | 153 // Computer major device class. |
| 154 return DEVICE_COMPUTER; | 154 return BluetoothDeviceType::COMPUTER; |
| 155 case 0x02: | 155 case 0x02: |
| 156 // Phone major device class. | 156 // Phone major device class. |
| 157 switch ((bluetooth_class & 0xfc) >> 2) { | 157 switch ((bluetooth_class & 0xfc) >> 2) { |
| 158 case 0x01: | 158 case 0x01: |
| 159 case 0x02: | 159 case 0x02: |
| 160 case 0x03: | 160 case 0x03: |
| 161 // Cellular, cordless and smart phones. | 161 // Cellular, cordless and smart phones. |
| 162 return DEVICE_PHONE; | 162 return BluetoothDeviceType::PHONE; |
| 163 case 0x04: | 163 case 0x04: |
| 164 case 0x05: | 164 case 0x05: |
| 165 // Modems: wired or voice gateway and common ISDN access. | 165 // Modems: wired or voice gateway and common ISDN access. |
| 166 return DEVICE_MODEM; | 166 return BluetoothDeviceType::MODEM; |
| 167 } | 167 } |
| 168 break; | 168 break; |
| 169 case 0x04: | 169 case 0x04: |
| 170 // Audio major device class. | 170 // Audio major device class. |
| 171 switch ((bluetooth_class & 0xfc) >> 2) { | 171 switch ((bluetooth_class & 0xfc) >> 2) { |
| 172 case 0x08: | 172 case 0x08: |
| 173 // Car audio. | 173 // Car audio. |
| 174 return DEVICE_CAR_AUDIO; | 174 return BluetoothDeviceType::CAR_AUDIO; |
| 175 case 0x0b: | 175 case 0x0b: |
| 176 case 0x0c: | 176 case 0x0c: |
| 177 case 0x0d: | 177 case 0x0d: |
| 178 case 0x0e: | 178 case 0x0e: |
| 179 case 0x0f: | 179 case 0x0f: |
| 180 case 0x010: | 180 case 0x010: |
| 181 // Video devices. | 181 // Video devices. |
| 182 return DEVICE_VIDEO; | 182 return BluetoothDeviceType::VIDEO; |
| 183 default: | 183 default: |
| 184 return DEVICE_AUDIO; | 184 return BluetoothDeviceType::AUDIO; |
| 185 } | 185 } |
| 186 break; | 186 break; |
| 187 case 0x05: | 187 case 0x05: |
| 188 // Peripheral major device class. | 188 // Peripheral major device class. |
| 189 switch ((bluetooth_class & 0xc0) >> 6) { | 189 switch ((bluetooth_class & 0xc0) >> 6) { |
| 190 case 0x00: | 190 case 0x00: |
| 191 // "Not a keyboard or pointing device." | 191 // "Not a keyboard or pointing device." |
| 192 switch ((bluetooth_class & 0x01e) >> 2) { | 192 switch ((bluetooth_class & 0x01e) >> 2) { |
| 193 case 0x01: | 193 case 0x01: |
| 194 // Joystick. | 194 // Joystick. |
| 195 return DEVICE_JOYSTICK; | 195 return BluetoothDeviceType::JOYSTICK; |
| 196 case 0x02: | 196 case 0x02: |
| 197 // Gamepad. | 197 // Gamepad. |
| 198 return DEVICE_GAMEPAD; | 198 return BluetoothDeviceType::GAMEPAD; |
| 199 default: | 199 default: |
| 200 return DEVICE_PERIPHERAL; | 200 return BluetoothDeviceType::PERIPHERAL; |
| 201 } | 201 } |
| 202 break; | 202 break; |
| 203 case 0x01: | 203 case 0x01: |
| 204 // Keyboard. | 204 // Keyboard. |
| 205 return DEVICE_KEYBOARD; | 205 return BluetoothDeviceType::KEYBOARD; |
| 206 case 0x02: | 206 case 0x02: |
| 207 // Pointing device. | 207 // Pointing device. |
| 208 switch ((bluetooth_class & 0x01e) >> 2) { | 208 switch ((bluetooth_class & 0x01e) >> 2) { |
| 209 case 0x05: | 209 case 0x05: |
| 210 // Digitizer tablet. | 210 // Digitizer tablet. |
| 211 return DEVICE_TABLET; | 211 return BluetoothDeviceType::TABLET; |
| 212 default: | 212 default: |
| 213 // Mouse. | 213 // Mouse. |
| 214 return DEVICE_MOUSE; | 214 return BluetoothDeviceType::MOUSE; |
| 215 } | 215 } |
| 216 break; | 216 break; |
| 217 case 0x03: | 217 case 0x03: |
| 218 // Combo device. | 218 // Combo device. |
| 219 return DEVICE_KEYBOARD_MOUSE_COMBO; | 219 return BluetoothDeviceType::KEYBOARD_MOUSE_COMBO; |
| 220 } | 220 } |
| 221 break; | 221 break; |
| 222 } | 222 } |
| 223 | 223 |
| 224 // Some bluetooth devices, e.g., Microsoft Universal Foldable Keyboard, | 224 // Some bluetooth devices, e.g., Microsoft Universal Foldable Keyboard, |
| 225 // do not expose its bluetooth class. Use its appearance as a work-around. | 225 // do not expose its bluetooth class. Use its appearance as a work-around. |
| 226 // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicVi
ewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml | 226 // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicVi
ewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml |
| 227 uint16_t appearance = GetAppearance(); | 227 uint16_t appearance = GetAppearance(); |
| 228 // appearance: 10-bit category and 6-bit sub-category | 228 // appearance: 10-bit category and 6-bit sub-category |
| 229 switch ((appearance & 0xffc0) >> 6) { | 229 switch ((appearance & 0xffc0) >> 6) { |
| 230 case 0x01: | 230 case 0x01: |
| 231 // Generic phone | 231 // Generic phone |
| 232 return DEVICE_PHONE; | 232 return BluetoothDeviceType::PHONE; |
| 233 case 0x02: | 233 case 0x02: |
| 234 // Generic computer | 234 // Generic computer |
| 235 return DEVICE_COMPUTER; | 235 return BluetoothDeviceType::COMPUTER; |
| 236 case 0x0f: | 236 case 0x0f: |
| 237 // HID subtype | 237 // HID subtype |
| 238 switch (appearance & 0x3f) { | 238 switch (appearance & 0x3f) { |
| 239 case 0x01: | 239 case 0x01: |
| 240 // Keyboard. | 240 // Keyboard. |
| 241 return DEVICE_KEYBOARD; | 241 return BluetoothDeviceType::KEYBOARD; |
| 242 case 0x02: | 242 case 0x02: |
| 243 // Mouse | 243 // Mouse |
| 244 return DEVICE_MOUSE; | 244 return BluetoothDeviceType::MOUSE; |
| 245 case 0x03: | 245 case 0x03: |
| 246 // Joystick | 246 // Joystick |
| 247 return DEVICE_JOYSTICK; | 247 return BluetoothDeviceType::JOYSTICK; |
| 248 case 0x04: | 248 case 0x04: |
| 249 // Gamepad | 249 // Gamepad |
| 250 return DEVICE_GAMEPAD; | 250 return BluetoothDeviceType::GAMEPAD; |
| 251 case 0x05: | 251 case 0x05: |
| 252 // Digitizer tablet | 252 // Digitizer tablet |
| 253 return DEVICE_TABLET; | 253 return BluetoothDeviceType::TABLET; |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 | 256 |
| 257 return DEVICE_UNKNOWN; | 257 return BluetoothDeviceType::UNKNOWN; |
| 258 } | 258 } |
| 259 | 259 |
| 260 bool BluetoothDevice::IsPairable() const { | 260 bool BluetoothDevice::IsPairable() const { |
| 261 DeviceType type = GetDeviceType(); | 261 BluetoothDeviceType type = GetDeviceType(); |
| 262 | 262 |
| 263 // Get the vendor part of the address: "00:11:22" for "00:11:22:33:44:55" | 263 // Get the vendor part of the address: "00:11:22" for "00:11:22:33:44:55" |
| 264 std::string vendor = GetAddress().substr(0, 8); | 264 std::string vendor = GetAddress().substr(0, 8); |
| 265 | 265 |
| 266 // Verbatim "Bluetooth Mouse", model 96674 | 266 // Verbatim "Bluetooth Mouse", model 96674 |
| 267 if (type == DEVICE_MOUSE && vendor == "00:12:A1") | 267 if (type == BluetoothDeviceType::MOUSE && vendor == "00:12:A1") |
| 268 return false; | 268 return false; |
| 269 // Microsoft "Microsoft Bluetooth Notebook Mouse 5000", model X807028-001 | 269 // Microsoft "Microsoft Bluetooth Notebook Mouse 5000", model X807028-001 |
| 270 if (type == DEVICE_MOUSE && vendor == "7C:ED:8D") | 270 if (type == BluetoothDeviceType::MOUSE && vendor == "7C:ED:8D") |
| 271 return false; | 271 return false; |
| 272 // Sony PlayStation Dualshock3 | 272 // Sony PlayStation Dualshock3 |
| 273 if (IsTrustable()) | 273 if (IsTrustable()) |
| 274 return false; | 274 return false; |
| 275 | 275 |
| 276 // TODO: Move this database into a config file. | 276 // TODO: Move this database into a config file. |
| 277 | 277 |
| 278 return true; | 278 return true; |
| 279 } | 279 } |
| 280 | 280 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 if (power < INT8_MIN) { | 488 if (power < INT8_MIN) { |
| 489 return INT8_MIN; | 489 return INT8_MIN; |
| 490 } | 490 } |
| 491 if (power > INT8_MAX) { | 491 if (power > INT8_MAX) { |
| 492 return INT8_MAX; | 492 return INT8_MAX; |
| 493 } | 493 } |
| 494 return static_cast<int8_t>(power); | 494 return static_cast<int8_t>(power); |
| 495 } | 495 } |
| 496 | 496 |
| 497 } // namespace device | 497 } // namespace device |
| OLD | NEW |