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
<
93 std::string
, scoped_ptr
<BluetoothServiceRecordWin
>> ServiceRecordMap
;
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 int16
BluetoothDeviceWin::GetInquiryRSSI() const {
192 return kUnknownPower
;
195 int16
BluetoothDeviceWin::GetInquiryTxPower() const {
197 return kUnknownPower
;
200 bool BluetoothDeviceWin::ExpectingPinCode() const {
205 bool BluetoothDeviceWin::ExpectingPasskey() const {
210 bool BluetoothDeviceWin::ExpectingConfirmation() const {
215 void BluetoothDeviceWin::GetConnectionInfo(
216 const ConnectionInfoCallback
& callback
) {
218 callback
.Run(ConnectionInfo());
221 void BluetoothDeviceWin::Connect(
222 PairingDelegate
* pairing_delegate
,
223 const base::Closure
& callback
,
224 const ConnectErrorCallback
& error_callback
) {
228 void BluetoothDeviceWin::SetPinCode(const std::string
& pincode
) {
232 void BluetoothDeviceWin::SetPasskey(uint32 passkey
) {
236 void BluetoothDeviceWin::ConfirmPairing() {
240 void BluetoothDeviceWin::RejectPairing() {
244 void BluetoothDeviceWin::CancelPairing() {
248 void BluetoothDeviceWin::Disconnect(
249 const base::Closure
& callback
,
250 const ErrorCallback
& error_callback
) {
254 void BluetoothDeviceWin::Forget(const ErrorCallback
& error_callback
) {
258 void BluetoothDeviceWin::ConnectToService(
259 const BluetoothUUID
& uuid
,
260 const ConnectToServiceCallback
& callback
,
261 const ConnectToServiceErrorCallback
& error_callback
) {
262 scoped_refptr
<BluetoothSocketWin
> socket(
263 BluetoothSocketWin::CreateBluetoothSocket(
264 ui_task_runner_
, socket_thread_
));
265 socket
->Connect(this, uuid
, base::Bind(callback
, socket
), error_callback
);
268 void BluetoothDeviceWin::ConnectToServiceInsecurely(
269 const BluetoothUUID
& uuid
,
270 const ConnectToServiceCallback
& callback
,
271 const ConnectToServiceErrorCallback
& error_callback
) {
272 error_callback
.Run(kApiUnavailable
);
275 void BluetoothDeviceWin::CreateGattConnection(
276 const GattConnectionCallback
& callback
,
277 const ConnectErrorCallback
& error_callback
) {
278 // TODO(armansito): Implement.
279 error_callback
.Run(ERROR_UNSUPPORTED_DEVICE
);
282 const BluetoothServiceRecordWin
* BluetoothDeviceWin::GetServiceRecord(
283 const device::BluetoothUUID
& uuid
) const {
284 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
285 iter
!= service_record_list_
.end();
287 if ((*iter
)->uuid() == uuid
)
293 } // namespace device