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_service_record_win.h"
16 #include "device/bluetooth/bluetooth_socket_thread.h"
17 #include "device/bluetooth/bluetooth_socket_win.h"
18 #include "device/bluetooth/bluetooth_task_manager_win.h"
19 #include "device/bluetooth/bluetooth_uuid.h"
23 const char kApiUnavailable
[] = "This API is not implemented on this platform.";
29 BluetoothDeviceWin::BluetoothDeviceWin(
30 const BluetoothTaskManagerWin::DeviceState
& device_state
,
31 const scoped_refptr
<base::SequencedTaskRunner
>& ui_task_runner
,
32 const scoped_refptr
<BluetoothSocketThread
>& socket_thread
,
34 const net::NetLog::Source
& net_log_source
)
36 ui_task_runner_(ui_task_runner
),
37 socket_thread_(socket_thread
),
39 net_log_source_(net_log_source
) {
43 BluetoothDeviceWin::~BluetoothDeviceWin() {
46 void BluetoothDeviceWin::Update(
47 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
48 address_
= device_state
.address
;
49 // Note: Callers are responsible for providing a canonicalized address.
50 DCHECK_EQ(address_
, BluetoothDevice::CanonicalizeAddress(address_
));
51 name_
= device_state
.name
;
52 bluetooth_class_
= device_state
.bluetooth_class
;
53 visible_
= device_state
.visible
;
54 connected_
= device_state
.connected
;
55 paired_
= device_state
.authenticated
;
56 UpdateServices(device_state
);
59 void BluetoothDeviceWin::UpdateServices(
60 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
62 service_record_list_
.clear();
64 for (ScopedVector
<BluetoothTaskManagerWin::ServiceRecordState
>::const_iterator
65 iter
= device_state
.service_record_states
.begin();
66 iter
!= device_state
.service_record_states
.end();
68 BluetoothServiceRecordWin
* service_record
=
69 new BluetoothServiceRecordWin(device_state
.address
,
73 service_record_list_
.push_back(service_record
);
74 uuids_
.push_back(service_record
->uuid());
78 bool BluetoothDeviceWin::IsEqual(
79 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
80 if (address_
!= device_state
.address
|| name_
!= device_state
.name
||
81 bluetooth_class_
!= device_state
.bluetooth_class
||
82 visible_
!= device_state
.visible
||
83 connected_
!= device_state
.connected
||
84 paired_
!= device_state
.authenticated
) {
88 // Checks service collection
89 typedef std::set
<BluetoothUUID
> UUIDSet
;
90 typedef base::ScopedPtrHashMap
<
91 std::string
, scoped_ptr
<BluetoothServiceRecordWin
>> ServiceRecordMap
;
93 UUIDSet known_services
;
94 for (UUIDList::const_iterator iter
= uuids_
.begin(); iter
!= uuids_
.end();
96 known_services
.insert((*iter
));
100 ServiceRecordMap new_service_records
;
101 for (ScopedVector
<BluetoothTaskManagerWin::ServiceRecordState
>::const_iterator
102 iter
= device_state
.service_record_states
.begin();
103 iter
!= device_state
.service_record_states
.end();
105 BluetoothServiceRecordWin
* service_record
= new BluetoothServiceRecordWin(
106 address_
, (*iter
)->name
, (*iter
)->sdp_bytes
, (*iter
)->gatt_uuid
);
107 new_services
.insert(service_record
->uuid());
108 new_service_records
.set(
109 service_record
->uuid().canonical_value(),
110 scoped_ptr
<BluetoothServiceRecordWin
>(service_record
));
113 UUIDSet removed_services
=
114 base::STLSetDifference
<UUIDSet
>(known_services
, new_services
);
115 if (!removed_services
.empty()) {
118 UUIDSet added_devices
=
119 base::STLSetDifference
<UUIDSet
>(new_services
, known_services
);
120 if (!added_devices
.empty()) {
124 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
125 iter
!= service_record_list_
.end();
127 BluetoothServiceRecordWin
* service_record
= (*iter
);
128 BluetoothServiceRecordWin
* new_service_record
=
129 new_service_records
.get((*iter
)->uuid().canonical_value());
130 if (!service_record
->IsEqual(*new_service_record
))
136 void BluetoothDeviceWin::SetVisible(bool visible
) {
140 uint32
BluetoothDeviceWin::GetBluetoothClass() const {
141 return bluetooth_class_
;
144 std::string
BluetoothDeviceWin::GetDeviceName() const {
148 std::string
BluetoothDeviceWin::GetAddress() const {
152 BluetoothDevice::VendorIDSource
153 BluetoothDeviceWin::GetVendorIDSource() const {
154 return VENDOR_ID_UNKNOWN
;
157 uint16
BluetoothDeviceWin::GetVendorID() const {
161 uint16
BluetoothDeviceWin::GetProductID() const {
165 uint16
BluetoothDeviceWin::GetDeviceID() const {
169 bool BluetoothDeviceWin::IsPaired() const {
173 bool BluetoothDeviceWin::IsConnected() const {
177 bool BluetoothDeviceWin::IsConnectable() const {
181 bool BluetoothDeviceWin::IsConnecting() const {
185 BluetoothDevice::UUIDList
BluetoothDeviceWin::GetUUIDs() const {
189 int16
BluetoothDeviceWin::GetInquiryRSSI() const {
190 return kUnknownPower
;
193 int16
BluetoothDeviceWin::GetInquiryTxPower() const {
195 return kUnknownPower
;
198 bool BluetoothDeviceWin::ExpectingPinCode() const {
203 bool BluetoothDeviceWin::ExpectingPasskey() const {
208 bool BluetoothDeviceWin::ExpectingConfirmation() const {
213 void BluetoothDeviceWin::GetConnectionInfo(
214 const ConnectionInfoCallback
& callback
) {
216 callback
.Run(ConnectionInfo());
219 void BluetoothDeviceWin::Connect(
220 PairingDelegate
* pairing_delegate
,
221 const base::Closure
& callback
,
222 const ConnectErrorCallback
& error_callback
) {
226 void BluetoothDeviceWin::SetPinCode(const std::string
& pincode
) {
230 void BluetoothDeviceWin::SetPasskey(uint32 passkey
) {
234 void BluetoothDeviceWin::ConfirmPairing() {
238 void BluetoothDeviceWin::RejectPairing() {
242 void BluetoothDeviceWin::CancelPairing() {
246 void BluetoothDeviceWin::Disconnect(
247 const base::Closure
& callback
,
248 const ErrorCallback
& error_callback
) {
252 void BluetoothDeviceWin::Forget(const ErrorCallback
& error_callback
) {
256 void BluetoothDeviceWin::ConnectToService(
257 const BluetoothUUID
& uuid
,
258 const ConnectToServiceCallback
& callback
,
259 const ConnectToServiceErrorCallback
& error_callback
) {
260 scoped_refptr
<BluetoothSocketWin
> socket(
261 BluetoothSocketWin::CreateBluetoothSocket(
262 ui_task_runner_
, socket_thread_
));
263 socket
->Connect(this, uuid
, base::Bind(callback
, socket
), error_callback
);
266 void BluetoothDeviceWin::ConnectToServiceInsecurely(
267 const BluetoothUUID
& uuid
,
268 const ConnectToServiceCallback
& callback
,
269 const ConnectToServiceErrorCallback
& error_callback
) {
270 error_callback
.Run(kApiUnavailable
);
273 void BluetoothDeviceWin::CreateGattConnection(
274 const GattConnectionCallback
& callback
,
275 const ConnectErrorCallback
& error_callback
) {
276 // TODO(armansito): Implement.
277 error_callback
.Run(ERROR_UNSUPPORTED_DEVICE
);
280 const BluetoothServiceRecordWin
* BluetoothDeviceWin::GetServiceRecord(
281 const device::BluetoothUUID
& uuid
) const {
282 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
283 iter
!= service_record_list_
.end();
285 if ((*iter
)->uuid() == uuid
)
291 } // namespace device