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