bluetooth_device_win.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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/logging.h"
11#include "base/memory/scoped_vector.h"
12#include "base/sequenced_task_runner.h"
13#include "base/strings/stringprintf.h"
14#include "device/bluetooth/bluetooth_service_record_win.h"
15#include "device/bluetooth/bluetooth_socket_thread.h"
16#include "device/bluetooth/bluetooth_socket_win.h"
17#include "device/bluetooth/bluetooth_task_manager_win.h"
18#include "device/bluetooth/bluetooth_uuid.h"
19
20namespace {
21
22const int kSdpBytesBufferSize = 1024;
23
24}  // namespace
25
26namespace device {
27
28BluetoothDeviceWin::BluetoothDeviceWin(
29    const BluetoothTaskManagerWin::DeviceState& state,
30    scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
31    scoped_refptr<BluetoothSocketThread> socket_thread,
32    net::NetLog* net_log,
33    const net::NetLog::Source& net_log_source)
34    : BluetoothDevice(),
35      ui_task_runner_(ui_task_runner),
36      socket_thread_(socket_thread),
37      net_log_(net_log),
38      net_log_source_(net_log_source) {
39  name_ = state.name;
40  address_ = CanonicalizeAddress(state.address);
41  bluetooth_class_ = state.bluetooth_class;
42  visible_ = state.visible;
43  connected_ = state.connected;
44  paired_ = state.authenticated;
45
46  for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
47       iter = state.service_record_states.begin();
48       iter != state.service_record_states.end();
49       ++iter) {
50    uint8 sdp_bytes_buffer[kSdpBytesBufferSize];
51    std::copy((*iter)->sdp_bytes.begin(),
52              (*iter)->sdp_bytes.end(),
53              sdp_bytes_buffer);
54    BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin(
55        (*iter)->name,
56        (*iter)->address,
57        (*iter)->sdp_bytes.size(),
58        sdp_bytes_buffer);
59    service_record_list_.push_back(service_record);
60    uuids_.push_back(service_record->uuid());
61  }
62}
63
64BluetoothDeviceWin::~BluetoothDeviceWin() {
65}
66
67void BluetoothDeviceWin::SetVisible(bool visible) {
68  visible_ = visible;
69}
70
71void BluetoothDeviceWin::AddObserver(
72    device::BluetoothDevice::Observer* observer) {
73  DCHECK(observer);
74  observers_.AddObserver(observer);
75}
76
77void BluetoothDeviceWin::RemoveObserver(
78    device::BluetoothDevice::Observer* observer) {
79  DCHECK(observer);
80  observers_.RemoveObserver(observer);
81}
82
83
84uint32 BluetoothDeviceWin::GetBluetoothClass() const {
85  return bluetooth_class_;
86}
87
88std::string BluetoothDeviceWin::GetDeviceName() const {
89  return name_;
90}
91
92std::string BluetoothDeviceWin::GetAddress() const {
93  return address_;
94}
95
96BluetoothDevice::VendorIDSource
97BluetoothDeviceWin::GetVendorIDSource() const {
98  return VENDOR_ID_UNKNOWN;
99}
100
101uint16 BluetoothDeviceWin::GetVendorID() const {
102  return 0;
103}
104
105uint16 BluetoothDeviceWin::GetProductID() const {
106  return 0;
107}
108
109uint16 BluetoothDeviceWin::GetDeviceID() const {
110  return 0;
111}
112
113int BluetoothDeviceWin::GetRSSI() const {
114  NOTIMPLEMENTED();
115  return kUnknownPower;
116}
117
118int BluetoothDeviceWin::GetCurrentHostTransmitPower() const {
119  NOTIMPLEMENTED();
120  return kUnknownPower;
121}
122
123int BluetoothDeviceWin::GetMaximumHostTransmitPower() const {
124  NOTIMPLEMENTED();
125  return kUnknownPower;
126}
127
128bool BluetoothDeviceWin::IsPaired() const {
129  return paired_;
130}
131
132bool BluetoothDeviceWin::IsConnected() const {
133  return connected_;
134}
135
136bool BluetoothDeviceWin::IsConnectable() const {
137  return false;
138}
139
140bool BluetoothDeviceWin::IsConnecting() const {
141  return false;
142}
143
144BluetoothDevice::UUIDList BluetoothDeviceWin::GetUUIDs() const {
145  return uuids_;
146}
147
148bool BluetoothDeviceWin::ExpectingPinCode() const {
149  NOTIMPLEMENTED();
150  return false;
151}
152
153bool BluetoothDeviceWin::ExpectingPasskey() const {
154  NOTIMPLEMENTED();
155  return false;
156}
157
158bool BluetoothDeviceWin::ExpectingConfirmation() const {
159  NOTIMPLEMENTED();
160  return false;
161}
162
163void BluetoothDeviceWin::Connect(
164    PairingDelegate* pairing_delegate,
165    const base::Closure& callback,
166    const ConnectErrorCallback& error_callback) {
167  NOTIMPLEMENTED();
168}
169
170void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
171  NOTIMPLEMENTED();
172}
173
174void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
175  NOTIMPLEMENTED();
176}
177
178void BluetoothDeviceWin::ConfirmPairing() {
179  NOTIMPLEMENTED();
180}
181
182void BluetoothDeviceWin::RejectPairing() {
183  NOTIMPLEMENTED();
184}
185
186void BluetoothDeviceWin::CancelPairing() {
187  NOTIMPLEMENTED();
188}
189
190void BluetoothDeviceWin::Disconnect(
191    const base::Closure& callback,
192    const ErrorCallback& error_callback) {
193  NOTIMPLEMENTED();
194}
195
196void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
197  NOTIMPLEMENTED();
198}
199
200void BluetoothDeviceWin::ConnectToService(
201    const BluetoothUUID& uuid,
202    const ConnectToServiceCallback& callback,
203    const ConnectToServiceErrorCallback& error_callback) {
204  scoped_refptr<BluetoothSocketWin> socket(
205      BluetoothSocketWin::CreateBluetoothSocket(
206          ui_task_runner_, socket_thread_, NULL, net::NetLog::Source()));
207  socket->Connect(this, uuid, base::Bind(callback, socket), error_callback);
208}
209
210void BluetoothDeviceWin::CreateGattConnection(
211      const GattConnectionCallback& callback,
212      const ConnectErrorCallback& error_callback) {
213  // TODO(armansito): Implement.
214  error_callback.Run(ERROR_UNSUPPORTED_DEVICE);
215}
216
217void BluetoothDeviceWin::StartConnectionMonitor(
218    const base::Closure& callback,
219    const ErrorCallback& error_callback) {
220  NOTIMPLEMENTED();
221}
222
223const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord(
224    const device::BluetoothUUID& uuid) const {
225  for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
226       iter != service_record_list_.end();
227       ++iter) {
228    if ((*iter)->uuid() == uuid)
229      return *iter;
230  }
231  return NULL;
232}
233
234}  // namespace device
235