Mark //testing/perf target testonly.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_win.cc
blob1c5cf3c2e952a318907480dfb269b6dadca1f5d5
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"
7 #include <string>
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"
21 namespace {
23 const int kSdpBytesBufferSize = 1024;
25 const char kApiUnavailable[] = "This API is not implemented on this platform.";
27 } // namespace
29 namespace device {
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,
35 net::NetLog* net_log,
36 const net::NetLog::Source& net_log_source)
37 : BluetoothDevice(),
38 ui_task_runner_(ui_task_runner),
39 socket_thread_(socket_thread),
40 net_log_(net_log),
41 net_log_source_(net_log_source) {
42 Update(device_state);
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) {
63 uuids_.clear();
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();
69 ++iter) {
70 BluetoothServiceRecordWin* service_record =
71 new BluetoothServiceRecordWin(device_state.address,
72 (*iter)->name,
73 (*iter)->sdp_bytes,
74 (*iter)->gatt_uuid);
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) {
87 return false;
90 // Checks service collection
91 typedef std::set<BluetoothUUID> UUIDSet;
92 typedef base::ScopedPtrHashMap<std::string, BluetoothServiceRecordWin>
93 ServiceRecordMap;
95 UUIDSet known_services;
96 for (UUIDList::const_iterator iter = uuids_.begin(); iter != uuids_.end();
97 ++iter) {
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();
106 ++iter) {
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()) {
118 return false;
120 UUIDSet added_devices =
121 base::STLSetDifference<UUIDSet>(new_services, known_services);
122 if (!added_devices.empty()) {
123 return false;
126 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
127 iter != service_record_list_.end();
128 ++iter) {
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))
133 return false;
135 return true;
138 void BluetoothDeviceWin::SetVisible(bool visible) {
139 visible_ = visible;
142 uint32 BluetoothDeviceWin::GetBluetoothClass() const {
143 return bluetooth_class_;
146 std::string BluetoothDeviceWin::GetDeviceName() const {
147 return name_;
150 std::string BluetoothDeviceWin::GetAddress() const {
151 return address_;
154 BluetoothDevice::VendorIDSource
155 BluetoothDeviceWin::GetVendorIDSource() const {
156 return VENDOR_ID_UNKNOWN;
159 uint16 BluetoothDeviceWin::GetVendorID() const {
160 return 0;
163 uint16 BluetoothDeviceWin::GetProductID() const {
164 return 0;
167 uint16 BluetoothDeviceWin::GetDeviceID() const {
168 return 0;
171 bool BluetoothDeviceWin::IsPaired() const {
172 return paired_;
175 bool BluetoothDeviceWin::IsConnected() const {
176 return connected_;
179 bool BluetoothDeviceWin::IsConnectable() const {
180 return false;
183 bool BluetoothDeviceWin::IsConnecting() const {
184 return false;
187 BluetoothDevice::UUIDList BluetoothDeviceWin::GetUUIDs() const {
188 return uuids_;
191 bool BluetoothDeviceWin::ExpectingPinCode() const {
192 NOTIMPLEMENTED();
193 return false;
196 bool BluetoothDeviceWin::ExpectingPasskey() const {
197 NOTIMPLEMENTED();
198 return false;
201 bool BluetoothDeviceWin::ExpectingConfirmation() const {
202 NOTIMPLEMENTED();
203 return false;
206 void BluetoothDeviceWin::GetConnectionInfo(
207 const ConnectionInfoCallback& callback) {
208 NOTIMPLEMENTED();
209 callback.Run(ConnectionInfo());
212 void BluetoothDeviceWin::Connect(
213 PairingDelegate* pairing_delegate,
214 const base::Closure& callback,
215 const ConnectErrorCallback& error_callback) {
216 NOTIMPLEMENTED();
219 void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
220 NOTIMPLEMENTED();
223 void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
224 NOTIMPLEMENTED();
227 void BluetoothDeviceWin::ConfirmPairing() {
228 NOTIMPLEMENTED();
231 void BluetoothDeviceWin::RejectPairing() {
232 NOTIMPLEMENTED();
235 void BluetoothDeviceWin::CancelPairing() {
236 NOTIMPLEMENTED();
239 void BluetoothDeviceWin::Disconnect(
240 const base::Closure& callback,
241 const ErrorCallback& error_callback) {
242 NOTIMPLEMENTED();
245 void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
246 NOTIMPLEMENTED();
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();
277 ++iter) {
278 if ((*iter)->uuid() == uuid)
279 return *iter;
281 return NULL;
284 } // namespace device