blob: f7bea9ed95422afe8618efb42474ce1d2d08f864 [file] [log] [blame]
[email protected]eb4dae6e2012-02-19 00:06:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4d7d2f592011-10-26 23:46:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cab208eb02012-10-18 22:15:235#include "device/bluetooth/bluetooth_device.h"
[email protected]4d7d2f592011-10-26 23:46:216
[email protected]edcaf14a2013-02-25 17:46:367#include <string>
8
[email protected]e40e93f72012-03-09 03:41:289#include "base/utf_string_conversions.h"
[email protected]edcaf14a2013-02-25 17:46:3610#include "device/bluetooth/bluetooth_utils.h"
[email protected]8a92bbd2013-02-22 01:07:2911#include "grit/device_bluetooth_strings.h"
[email protected]e40e93f72012-03-09 03:41:2812#include "ui/base/l10n/l10n_util.h"
[email protected]4d7d2f592011-10-26 23:46:2113
[email protected]cab208eb02012-10-18 22:15:2314namespace device {
[email protected]4d7d2f592011-10-26 23:46:2115
[email protected]edcaf14a2013-02-25 17:46:3616// static
17bool BluetoothDevice::IsUUIDValid(const std::string& uuid) {
18 return !bluetooth_utils::CanonicalUuid(uuid).empty();
19}
20
[email protected]320447d72013-04-05 03:32:2621BluetoothDevice::BluetoothDevice() {
[email protected]4d7d2f592011-10-26 23:46:2122}
23
24BluetoothDevice::~BluetoothDevice() {
25}
26
[email protected]e40e93f72012-03-09 03:41:2827string16 BluetoothDevice::GetName() const {
[email protected]320447d72013-04-05 03:32:2628 std::string name = GetDeviceName();
29 if (!name.empty()) {
30 return UTF8ToUTF16(name);
[email protected]e40e93f72012-03-09 03:41:2831 } else {
32 return GetAddressWithLocalizedDeviceTypeName();
33 }
34}
35
[email protected]cc1bc712012-10-02 01:56:4536string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const {
[email protected]320447d72013-04-05 03:32:2637 string16 address_utf16 = UTF8ToUTF16(GetAddress());
[email protected]cc1bc712012-10-02 01:56:4538 BluetoothDevice::DeviceType device_type = GetDeviceType();
39 switch (device_type) {
40 case DEVICE_COMPUTER:
41 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER,
[email protected]320447d72013-04-05 03:32:2642 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4543 case DEVICE_PHONE:
44 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE,
[email protected]320447d72013-04-05 03:32:2645 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4546 case DEVICE_MODEM:
47 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM,
[email protected]320447d72013-04-05 03:32:2648 address_utf16);
[email protected]e7a20392012-10-09 19:21:3049 case DEVICE_AUDIO:
50 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO,
[email protected]320447d72013-04-05 03:32:2651 address_utf16);
[email protected]e7a20392012-10-09 19:21:3052 case DEVICE_CAR_AUDIO:
53 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO,
[email protected]320447d72013-04-05 03:32:2654 address_utf16);
[email protected]e7a20392012-10-09 19:21:3055 case DEVICE_VIDEO:
56 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO,
[email protected]320447d72013-04-05 03:32:2657 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4558 case DEVICE_JOYSTICK:
59 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK,
[email protected]320447d72013-04-05 03:32:2660 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4561 case DEVICE_GAMEPAD:
62 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD,
[email protected]320447d72013-04-05 03:32:2663 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4564 case DEVICE_KEYBOARD:
65 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD,
[email protected]320447d72013-04-05 03:32:2666 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4567 case DEVICE_MOUSE:
68 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE,
[email protected]320447d72013-04-05 03:32:2669 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4570 case DEVICE_TABLET:
71 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET,
[email protected]320447d72013-04-05 03:32:2672 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4573 case DEVICE_KEYBOARD_MOUSE_COMBO:
74 return l10n_util::GetStringFUTF16(
[email protected]320447d72013-04-05 03:32:2675 IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address_utf16);
[email protected]cc1bc712012-10-02 01:56:4576 default:
[email protected]320447d72013-04-05 03:32:2677 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN,
78 address_utf16);
[email protected]cc1bc712012-10-02 01:56:4579 }
80}
81
[email protected]e40e93f72012-03-09 03:41:2882BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const {
83 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
[email protected]320447d72013-04-05 03:32:2684 uint32 bluetooth_class = GetBluetoothClass();
85 switch ((bluetooth_class & 0x1f00) >> 8) {
[email protected]e40e93f72012-03-09 03:41:2886 case 0x01:
87 // Computer major device class.
88 return DEVICE_COMPUTER;
89 case 0x02:
90 // Phone major device class.
[email protected]320447d72013-04-05 03:32:2691 switch ((bluetooth_class & 0xfc) >> 2) {
[email protected]e40e93f72012-03-09 03:41:2892 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]e7a20392012-10-09 19:21:30103 case 0x04:
104 // Audio major device class.
[email protected]320447d72013-04-05 03:32:26105 switch ((bluetooth_class & 0xfc) >> 2) {
[email protected]e7a20392012-10-09 19:21:30106 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]e40e93f72012-03-09 03:41:28121 case 0x05:
122 // Peripheral major device class.
[email protected]320447d72013-04-05 03:32:26123 switch ((bluetooth_class & 0xc0) >> 6) {
[email protected]e40e93f72012-03-09 03:41:28124 case 0x00:
125 // "Not a keyboard or pointing device."
[email protected]320447d72013-04-05 03:32:26126 switch ((bluetooth_class & 0x01e) >> 2) {
[email protected]28588ac2012-05-01 23:57:46127 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]e40e93f72012-03-09 03:41:28137 case 0x01:
138 // Keyboard.
139 return DEVICE_KEYBOARD;
140 case 0x02:
141 // Pointing device.
[email protected]320447d72013-04-05 03:32:26142 switch ((bluetooth_class & 0x01e) >> 2) {
[email protected]e40e93f72012-03-09 03:41:28143 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]60625cb2013-02-26 20:35:07161
[email protected]edcaf14a2013-02-25 17:46:36162bool BluetoothDevice::ProvidesServiceWithUUID(
163 const std::string& uuid) const {
164 std::string canonical_uuid = bluetooth_utils::CanonicalUuid(uuid);
[email protected]320447d72013-04-05 03:32:26165 BluetoothDevice::ServiceList services = GetServices();
[email protected]edcaf14a2013-02-25 17:46:36166 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]cab208eb02012-10-18 22:15:23175} // namespace device