bluetooth_device_win.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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.
4
5#include "device/bluetooth/bluetooth_device_win.h"
6
7#include <string>
8
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"
20
21namespace {
22
23const int kSdpBytesBufferSize = 1024;
24
25}  // namespace
26
27namespace device {
28
29BluetoothDeviceWin::BluetoothDeviceWin(
30    const BluetoothTaskManagerWin::DeviceState& device_state,
31    const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner,
32    const scoped_refptr<BluetoothSocketThread>& socket_thread,
33    net::NetLog* net_log,
34    const net::NetLog::Source& net_log_source)
35    : BluetoothDevice(),
36      ui_task_runner_(ui_task_runner),
37      socket_thread_(socket_thread),
38      net_log_(net_log),
39      net_log_source_(net_log_source) {
40  Update(device_state);
41}
42
43BluetoothDeviceWin::~BluetoothDeviceWin() {
44}
45
46void 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);
57}
58
59void BluetoothDeviceWin::UpdateServices(
60    const BluetoothTaskManagerWin::DeviceState& device_state) {
61  uuids_.clear();
62  service_record_list_.clear();
63
64  for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
65           iter = device_state.service_record_states.begin();
66       iter != device_state.service_record_states.end();
67       ++iter) {
68    BluetoothServiceRecordWin* service_record =
69        new BluetoothServiceRecordWin(device_state.address,
70                                      (*iter)->name,
71                                      (*iter)->sdp_bytes,
72                                      (*iter)->gatt_uuid);
73    service_record_list_.push_back(service_record);
74    uuids_.push_back(service_record->uuid());
75  }
76}
77
78bool 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) {
85    return false;
86  }
87
88  // Checks service collection
89  typedef std::set<BluetoothUUID> UUIDSet;
90  typedef base::ScopedPtrHashMap<std::string, BluetoothServiceRecordWin>
91      ServiceRecordMap;
92
93  UUIDSet known_services;
94  for (UUIDList::const_iterator iter = uuids_.begin(); iter != uuids_.end();
95       ++iter) {
96    known_services.insert((*iter));
97  }
98
99  UUIDSet new_services;
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();
104       ++iter) {
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));
111  }
112
113  UUIDSet removed_services =
114      base::STLSetDifference<UUIDSet>(known_services, new_services);
115  if (!removed_services.empty()) {
116    return false;
117  }
118  UUIDSet added_devices =
119      base::STLSetDifference<UUIDSet>(new_services, known_services);
120  if (!added_devices.empty()) {
121    return false;
122  }
123
124  for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
125       iter != service_record_list_.end();
126       ++iter) {
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))
131      return false;
132  }
133  return true;
134}
135
136void BluetoothDeviceWin::SetVisible(bool visible) {
137  visible_ = visible;
138}
139
140uint32 BluetoothDeviceWin::GetBluetoothClass() const {
141  return bluetooth_class_;
142}
143
144std::string BluetoothDeviceWin::GetDeviceName() const {
145  return name_;
146}
147
148std::string BluetoothDeviceWin::GetAddress() const {
149  return address_;
150}
151
152BluetoothDevice::VendorIDSource
153BluetoothDeviceWin::GetVendorIDSource() const {
154  return VENDOR_ID_UNKNOWN;
155}
156
157uint16 BluetoothDeviceWin::GetVendorID() const {
158  return 0;
159}
160
161uint16 BluetoothDeviceWin::GetProductID() const {
162  return 0;
163}
164
165uint16 BluetoothDeviceWin::GetDeviceID() const {
166  return 0;
167}
168
169int BluetoothDeviceWin::GetRSSI() const {
170  NOTIMPLEMENTED();
171  return kUnknownPower;
172}
173
174int BluetoothDeviceWin::GetCurrentHostTransmitPower() const {
175  NOTIMPLEMENTED();
176  return kUnknownPower;
177}
178
179int BluetoothDeviceWin::GetMaximumHostTransmitPower() const {
180  NOTIMPLEMENTED();
181  return kUnknownPower;
182}
183
184bool BluetoothDeviceWin::IsPaired() const {
185  return paired_;
186}
187
188bool BluetoothDeviceWin::IsConnected() const {
189  return connected_;
190}
191
192bool BluetoothDeviceWin::IsConnectable() const {
193  return false;
194}
195
196bool BluetoothDeviceWin::IsConnecting() const {
197  return false;
198}
199
200BluetoothDevice::UUIDList BluetoothDeviceWin::GetUUIDs() const {
201  return uuids_;
202}
203
204bool BluetoothDeviceWin::ExpectingPinCode() const {
205  NOTIMPLEMENTED();
206  return false;
207}
208
209bool BluetoothDeviceWin::ExpectingPasskey() const {
210  NOTIMPLEMENTED();
211  return false;
212}
213
214bool BluetoothDeviceWin::ExpectingConfirmation() const {
215  NOTIMPLEMENTED();
216  return false;
217}
218
219void BluetoothDeviceWin::Connect(
220    PairingDelegate* pairing_delegate,
221    const base::Closure& callback,
222    const ConnectErrorCallback& error_callback) {
223  NOTIMPLEMENTED();
224}
225
226void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
227  NOTIMPLEMENTED();
228}
229
230void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
231  NOTIMPLEMENTED();
232}
233
234void BluetoothDeviceWin::ConfirmPairing() {
235  NOTIMPLEMENTED();
236}
237
238void BluetoothDeviceWin::RejectPairing() {
239  NOTIMPLEMENTED();
240}
241
242void BluetoothDeviceWin::CancelPairing() {
243  NOTIMPLEMENTED();
244}
245
246void BluetoothDeviceWin::Disconnect(
247    const base::Closure& callback,
248    const ErrorCallback& error_callback) {
249  NOTIMPLEMENTED();
250}
251
252void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
253  NOTIMPLEMENTED();
254}
255
256void 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);
264}
265
266void BluetoothDeviceWin::CreateGattConnection(
267      const GattConnectionCallback& callback,
268      const ConnectErrorCallback& error_callback) {
269  // TODO(armansito): Implement.
270  error_callback.Run(ERROR_UNSUPPORTED_DEVICE);
271}
272
273void BluetoothDeviceWin::StartConnectionMonitor(
274    const base::Closure& callback,
275    const ErrorCallback& error_callback) {
276  NOTIMPLEMENTED();
277}
278
279const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord(
280    const device::BluetoothUUID& uuid) const {
281  for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
282       iter != service_record_list_.end();
283       ++iter) {
284    if ((*iter)->uuid() == uuid)
285      return *iter;
286  }
287  return NULL;
288}
289
290}  // namespace device
291