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 int kSdpBytesBufferSize
= 1024;
25 const char kApiUnavailable
[] = "This API is not implemented on this platform.";
31 BluetoothDeviceWin::BluetoothDeviceWin(
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
)
38 ui_task_runner_(ui_task_runner
),
39 socket_thread_(socket_thread
),
41 net_log_source_(net_log_source
) {
45 BluetoothDeviceWin::~BluetoothDeviceWin() {
48 void BluetoothDeviceWin::Update(
49 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
50 address_
= device_state
.address
;
51 // Note: Callers are responsible for providing a canonicalized address.
52 DCHECK_EQ(address_
, BluetoothDevice::CanonicalizeAddress(address_
));
53 name_
= device_state
.name
;
54 bluetooth_class_
= device_state
.bluetooth_class
;
55 visible_
= device_state
.visible
;
56 connected_
= device_state
.connected
;
57 paired_
= device_state
.authenticated
;
58 UpdateServices(device_state
);
61 void BluetoothDeviceWin::UpdateServices(
62 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
64 service_record_list_
.clear();
66 for (ScopedVector
<BluetoothTaskManagerWin::ServiceRecordState
>::const_iterator
67 iter
= device_state
.service_record_states
.begin();
68 iter
!= device_state
.service_record_states
.end();
70 BluetoothServiceRecordWin
* service_record
=
71 new BluetoothServiceRecordWin(device_state
.address
,
75 service_record_list_
.push_back(service_record
);
76 uuids_
.push_back(service_record
->uuid());
80 bool BluetoothDeviceWin::IsEqual(
81 const BluetoothTaskManagerWin::DeviceState
& device_state
) {
82 if (address_
!= device_state
.address
|| name_
!= device_state
.name
||
83 bluetooth_class_
!= device_state
.bluetooth_class
||
84 visible_
!= device_state
.visible
||
85 connected_
!= device_state
.connected
||
86 paired_
!= device_state
.authenticated
) {
90 // Checks service collection
91 typedef std::set
<BluetoothUUID
> UUIDSet
;
92 typedef base::ScopedPtrHashMap
<std::string
, BluetoothServiceRecordWin
>
95 UUIDSet known_services
;
96 for (UUIDList::const_iterator iter
= uuids_
.begin(); iter
!= uuids_
.end();
98 known_services
.insert((*iter
));
101 UUIDSet new_services
;
102 ServiceRecordMap new_service_records
;
103 for (ScopedVector
<BluetoothTaskManagerWin::ServiceRecordState
>::const_iterator
104 iter
= device_state
.service_record_states
.begin();
105 iter
!= device_state
.service_record_states
.end();
107 BluetoothServiceRecordWin
* service_record
= new BluetoothServiceRecordWin(
108 address_
, (*iter
)->name
, (*iter
)->sdp_bytes
, (*iter
)->gatt_uuid
);
109 new_services
.insert(service_record
->uuid());
110 new_service_records
.set(
111 service_record
->uuid().canonical_value(),
112 scoped_ptr
<BluetoothServiceRecordWin
>(service_record
));
115 UUIDSet removed_services
=
116 base::STLSetDifference
<UUIDSet
>(known_services
, new_services
);
117 if (!removed_services
.empty()) {
120 UUIDSet added_devices
=
121 base::STLSetDifference
<UUIDSet
>(new_services
, known_services
);
122 if (!added_devices
.empty()) {
126 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
127 iter
!= service_record_list_
.end();
129 BluetoothServiceRecordWin
* service_record
= (*iter
);
130 BluetoothServiceRecordWin
* new_service_record
=
131 new_service_records
.get((*iter
)->uuid().canonical_value());
132 if (!service_record
->IsEqual(*new_service_record
))
138 void BluetoothDeviceWin::SetVisible(bool visible
) {
142 uint32
BluetoothDeviceWin::GetBluetoothClass() const {
143 return bluetooth_class_
;
146 std::string
BluetoothDeviceWin::GetDeviceName() const {
150 std::string
BluetoothDeviceWin::GetAddress() const {
154 BluetoothDevice::VendorIDSource
155 BluetoothDeviceWin::GetVendorIDSource() const {
156 return VENDOR_ID_UNKNOWN
;
159 uint16
BluetoothDeviceWin::GetVendorID() const {
163 uint16
BluetoothDeviceWin::GetProductID() const {
167 uint16
BluetoothDeviceWin::GetDeviceID() const {
171 bool BluetoothDeviceWin::IsPaired() const {
175 bool BluetoothDeviceWin::IsConnected() const {
179 bool BluetoothDeviceWin::IsConnectable() const {
183 bool BluetoothDeviceWin::IsConnecting() const {
187 BluetoothDevice::UUIDList
BluetoothDeviceWin::GetUUIDs() const {
191 bool BluetoothDeviceWin::ExpectingPinCode() const {
196 bool BluetoothDeviceWin::ExpectingPasskey() const {
201 bool BluetoothDeviceWin::ExpectingConfirmation() const {
206 void BluetoothDeviceWin::GetConnectionInfo(
207 const ConnectionInfoCallback
& callback
) {
209 callback
.Run(ConnectionInfo());
212 void BluetoothDeviceWin::Connect(
213 PairingDelegate
* pairing_delegate
,
214 const base::Closure
& callback
,
215 const ConnectErrorCallback
& error_callback
) {
219 void BluetoothDeviceWin::SetPinCode(const std::string
& pincode
) {
223 void BluetoothDeviceWin::SetPasskey(uint32 passkey
) {
227 void BluetoothDeviceWin::ConfirmPairing() {
231 void BluetoothDeviceWin::RejectPairing() {
235 void BluetoothDeviceWin::CancelPairing() {
239 void BluetoothDeviceWin::Disconnect(
240 const base::Closure
& callback
,
241 const ErrorCallback
& error_callback
) {
245 void BluetoothDeviceWin::Forget(const ErrorCallback
& error_callback
) {
249 void BluetoothDeviceWin::ConnectToService(
250 const BluetoothUUID
& uuid
,
251 const ConnectToServiceCallback
& callback
,
252 const ConnectToServiceErrorCallback
& error_callback
) {
253 scoped_refptr
<BluetoothSocketWin
> socket(
254 BluetoothSocketWin::CreateBluetoothSocket(
255 ui_task_runner_
, socket_thread_
));
256 socket
->Connect(this, uuid
, base::Bind(callback
, socket
), error_callback
);
259 void BluetoothDeviceWin::ConnectToServiceInsecurely(
260 const BluetoothUUID
& uuid
,
261 const ConnectToServiceCallback
& callback
,
262 const ConnectToServiceErrorCallback
& error_callback
) {
263 error_callback
.Run(kApiUnavailable
);
266 void BluetoothDeviceWin::CreateGattConnection(
267 const GattConnectionCallback
& callback
,
268 const ConnectErrorCallback
& error_callback
) {
269 // TODO(armansito): Implement.
270 error_callback
.Run(ERROR_UNSUPPORTED_DEVICE
);
273 const BluetoothServiceRecordWin
* BluetoothDeviceWin::GetServiceRecord(
274 const device::BluetoothUUID
& uuid
) const {
275 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
276 iter
!= service_record_list_
.end();
278 if ((*iter
)->uuid() == uuid
)
284 } // namespace device