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.
5 #include "device/bluetooth/bluetooth_device_win.h"
9 #include "base/basictypes.h"
10 #include "base/containers/scoped_ptr_hash_map.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/stringprintf.h"
15 #include "device/bluetooth/bluetooth_adapter_win.h"
16 #include "device/bluetooth/bluetooth_service_record_win.h"
17 #include "device/bluetooth/bluetooth_socket_thread.h"
18 #include "device/bluetooth/bluetooth_socket_win.h"
19 #include "device/bluetooth/bluetooth_task_manager_win.h"
20 #include "device/bluetooth/bluetooth_uuid.h"
24 const char kApiUnavailable
[] = "This API is not implemented on this platform.";
30 BluetoothDeviceWin::BluetoothDeviceWin(
31 BluetoothAdapterWin
* adapter
,
32 const BluetoothTaskManagerWin::DeviceState
& device_state
,
33 const scoped_refptr
<base::SequencedTaskRunner
>& ui_task_runner
,
34 const scoped_refptr
<BluetoothSocketThread
>& socket_thread
,
36 const net::NetLog::Source
& net_log_source
)
37 : BluetoothDevice(adapter
),
38 ui_task_runner_(ui_task_runner
),
39 socket_thread_(socket_thread
),
41 net_log_source_(net_log_source
) {
45 BluetoothDeviceWin::~BluetoothDeviceWin() {
48 uint32
BluetoothDeviceWin::GetBluetoothClass() const {
49 return bluetooth_class_
;
52 std::string
BluetoothDeviceWin::GetAddress() const {
56 BluetoothDevice::VendorIDSource
57 BluetoothDeviceWin::GetVendorIDSource() const {
58 return VENDOR_ID_UNKNOWN
;
61 uint16
BluetoothDeviceWin::GetVendorID() const {
65 uint16
BluetoothDeviceWin::GetProductID() const {
69 uint16
BluetoothDeviceWin::GetDeviceID() const {
73 bool BluetoothDeviceWin::IsPaired() const {
77 bool BluetoothDeviceWin::IsConnected() const {
81 bool BluetoothDeviceWin::IsGattConnected() const {
86 bool BluetoothDeviceWin::IsConnectable() const {
90 bool BluetoothDeviceWin::IsConnecting() const {
94 BluetoothDevice::UUIDList
BluetoothDeviceWin::GetUUIDs() const {
98 int16
BluetoothDeviceWin::GetInquiryRSSI() const {
102 int16
BluetoothDeviceWin::GetInquiryTxPower() const {
104 return kUnknownPower
;
107 bool BluetoothDeviceWin::ExpectingPinCode() const {
112 bool BluetoothDeviceWin::ExpectingPasskey() const {
117 bool BluetoothDeviceWin::ExpectingConfirmation() const {
122 void BluetoothDeviceWin::GetConnectionInfo(
123 const ConnectionInfoCallback
& callback
) {
125 callback
.Run(ConnectionInfo());
128 void BluetoothDeviceWin::Connect(
129 PairingDelegate
* pairing_delegate
,
130 const base::Closure
& callback
,
131 const ConnectErrorCallback
& error_callback
) {
135 void BluetoothDeviceWin::SetPinCode(const std::string
& pincode
) {
139 void BluetoothDeviceWin::SetPasskey(uint32 passkey
) {
143 void BluetoothDeviceWin::ConfirmPairing() {
147 void BluetoothDeviceWin::RejectPairing() {
151 void BluetoothDeviceWin::CancelPairing() {
155 void BluetoothDeviceWin::Disconnect(
156 const base::Closure
& callback
,
157 const ErrorCallback
& error_callback
) {
161 void BluetoothDeviceWin::Forget(const ErrorCallback
& error_callback
) {
165 void BluetoothDeviceWin::ConnectToService(
166 const BluetoothUUID
& uuid
,
167 const ConnectToServiceCallback
& callback
,
168 const ConnectToServiceErrorCallback
& error_callback
) {
169 scoped_refptr
<BluetoothSocketWin
> socket(
170 BluetoothSocketWin::CreateBluetoothSocket(
171 ui_task_runner_
, socket_thread_
));
172 socket
->Connect(this, uuid
, base::Bind(callback
, socket
), error_callback
);
175 void BluetoothDeviceWin::ConnectToServiceInsecurely(
176 const BluetoothUUID
& uuid
,
177 const ConnectToServiceCallback
& callback
,
178 const ConnectToServiceErrorCallback
& error_callback
) {
179 error_callback
.Run(kApiUnavailable
);
182 void BluetoothDeviceWin::CreateGattConnection(
183 const GattConnectionCallback
& callback
,
184 const ConnectErrorCallback
& error_callback
) {
185 // TODO(armansito): Implement.
186 error_callback
.Run(ERROR_UNSUPPORTED_DEVICE
);
189 const BluetoothServiceRecordWin
* BluetoothDeviceWin::GetServiceRecord(
190 const device::BluetoothUUID
& uuid
) const {
191 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
192 iter
!= service_record_list_
.end();
194 if ((*iter
)->uuid() == uuid
)
200 bool BluetoothDeviceWin::IsEqual(
201 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
202 if (address_
!= device_state
.address
|| name_
!= device_state
.name
||
203 bluetooth_class_
!= device_state
.bluetooth_class
||
204 visible_
!= device_state
.visible
||
205 connected_
!= device_state
.connected
||
206 paired_
!= device_state
.authenticated
) {
210 // Checks service collection
211 typedef std::set
<BluetoothUUID
> UUIDSet
;
212 typedef base::ScopedPtrHashMap
<
213 std::string
, scoped_ptr
<BluetoothServiceRecordWin
>> ServiceRecordMap
;
215 UUIDSet known_services
;
216 for (UUIDList::const_iterator iter
= uuids_
.begin(); iter
!= uuids_
.end();
218 known_services
.insert((*iter
));
221 UUIDSet new_services
;
222 ServiceRecordMap new_service_records
;
223 for (ScopedVector
<BluetoothTaskManagerWin::ServiceRecordState
>::const_iterator
224 iter
= device_state
.service_record_states
.begin();
225 iter
!= device_state
.service_record_states
.end(); ++iter
) {
226 BluetoothServiceRecordWin
* service_record
= new BluetoothServiceRecordWin(
227 address_
, (*iter
)->name
, (*iter
)->sdp_bytes
, (*iter
)->gatt_uuid
);
228 new_services
.insert(service_record
->uuid());
229 new_service_records
.set(
230 service_record
->uuid().canonical_value(),
231 scoped_ptr
<BluetoothServiceRecordWin
>(service_record
));
234 UUIDSet removed_services
=
235 base::STLSetDifference
<UUIDSet
>(known_services
, new_services
);
236 if (!removed_services
.empty()) {
239 UUIDSet added_devices
=
240 base::STLSetDifference
<UUIDSet
>(new_services
, known_services
);
241 if (!added_devices
.empty()) {
245 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
246 iter
!= service_record_list_
.end(); ++iter
) {
247 BluetoothServiceRecordWin
* service_record
= (*iter
);
248 BluetoothServiceRecordWin
* new_service_record
=
249 new_service_records
.get((*iter
)->uuid().canonical_value());
250 if (!service_record
->IsEqual(*new_service_record
))
256 void BluetoothDeviceWin::Update(
257 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
258 address_
= device_state
.address
;
259 // Note: Callers are responsible for providing a canonicalized address.
260 DCHECK_EQ(address_
, BluetoothDevice::CanonicalizeAddress(address_
));
261 name_
= device_state
.name
;
262 bluetooth_class_
= device_state
.bluetooth_class
;
263 visible_
= device_state
.visible
;
264 connected_
= device_state
.connected
;
265 paired_
= device_state
.authenticated
;
266 UpdateServices(device_state
);
269 std::string
BluetoothDeviceWin::GetDeviceName() const {
273 void BluetoothDeviceWin::CreateGattConnectionImpl() {
274 // Windows implementation does not use the default CreateGattConnection
279 void BluetoothDeviceWin::DisconnectGatt() {
280 // Windows implementation does not use the default CreateGattConnection
285 void BluetoothDeviceWin::SetVisible(bool visible
) {
289 void BluetoothDeviceWin::UpdateServices(
290 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
292 service_record_list_
.clear();
294 for (ScopedVector
<BluetoothTaskManagerWin::ServiceRecordState
>::const_iterator
295 iter
= device_state
.service_record_states
.begin();
296 iter
!= device_state
.service_record_states
.end(); ++iter
) {
297 BluetoothServiceRecordWin
* service_record
=
298 new BluetoothServiceRecordWin(device_state
.address
, (*iter
)->name
,
299 (*iter
)->sdp_bytes
, (*iter
)->gatt_uuid
);
300 service_record_list_
.push_back(service_record
);
301 uuids_
.push_back(service_record
->uuid());
305 } // namespace device