Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_win.cc
blob67c2aec154c2aaaad1b89c6b3293f4f1c834572a
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 } // namespace
27 namespace device {
29 BluetoothDeviceWin::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);
43 BluetoothDeviceWin::~BluetoothDeviceWin() {
46 void 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);
59 void BluetoothDeviceWin::UpdateServices(
60 const BluetoothTaskManagerWin::DeviceState& device_state) {
61 uuids_.clear();
62 service_record_list_.clear();
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());
78 bool 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;
88 // Checks service collection
89 typedef std::set<BluetoothUUID> UUIDSet;
90 typedef base::ScopedPtrHashMap<std::string, BluetoothServiceRecordWin>
91 ServiceRecordMap;
93 UUIDSet known_services;
94 for (UUIDList::const_iterator iter = uuids_.begin(); iter != uuids_.end();
95 ++iter) {
96 known_services.insert((*iter));
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));
113 UUIDSet removed_services =
114 base::STLSetDifference<UUIDSet>(known_services, new_services);
115 if (!removed_services.empty()) {
116 return false;
118 UUIDSet added_devices =
119 base::STLSetDifference<UUIDSet>(new_services, known_services);
120 if (!added_devices.empty()) {
121 return false;
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;
133 return true;
136 void BluetoothDeviceWin::SetVisible(bool visible) {
137 visible_ = visible;
140 uint32 BluetoothDeviceWin::GetBluetoothClass() const {
141 return bluetooth_class_;
144 std::string BluetoothDeviceWin::GetDeviceName() const {
145 return name_;
148 std::string BluetoothDeviceWin::GetAddress() const {
149 return address_;
152 BluetoothDevice::VendorIDSource
153 BluetoothDeviceWin::GetVendorIDSource() const {
154 return VENDOR_ID_UNKNOWN;
157 uint16 BluetoothDeviceWin::GetVendorID() const {
158 return 0;
161 uint16 BluetoothDeviceWin::GetProductID() const {
162 return 0;
165 uint16 BluetoothDeviceWin::GetDeviceID() const {
166 return 0;
169 int BluetoothDeviceWin::GetRSSI() const {
170 NOTIMPLEMENTED();
171 return kUnknownPower;
174 int BluetoothDeviceWin::GetCurrentHostTransmitPower() const {
175 NOTIMPLEMENTED();
176 return kUnknownPower;
179 int BluetoothDeviceWin::GetMaximumHostTransmitPower() const {
180 NOTIMPLEMENTED();
181 return kUnknownPower;
184 bool BluetoothDeviceWin::IsPaired() const {
185 return paired_;
188 bool BluetoothDeviceWin::IsConnected() const {
189 return connected_;
192 bool BluetoothDeviceWin::IsConnectable() const {
193 return false;
196 bool BluetoothDeviceWin::IsConnecting() const {
197 return false;
200 BluetoothDevice::UUIDList BluetoothDeviceWin::GetUUIDs() const {
201 return uuids_;
204 bool BluetoothDeviceWin::ExpectingPinCode() const {
205 NOTIMPLEMENTED();
206 return false;
209 bool BluetoothDeviceWin::ExpectingPasskey() const {
210 NOTIMPLEMENTED();
211 return false;
214 bool BluetoothDeviceWin::ExpectingConfirmation() const {
215 NOTIMPLEMENTED();
216 return false;
219 void BluetoothDeviceWin::Connect(
220 PairingDelegate* pairing_delegate,
221 const base::Closure& callback,
222 const ConnectErrorCallback& error_callback) {
223 NOTIMPLEMENTED();
226 void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
227 NOTIMPLEMENTED();
230 void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
231 NOTIMPLEMENTED();
234 void BluetoothDeviceWin::ConfirmPairing() {
235 NOTIMPLEMENTED();
238 void BluetoothDeviceWin::RejectPairing() {
239 NOTIMPLEMENTED();
242 void BluetoothDeviceWin::CancelPairing() {
243 NOTIMPLEMENTED();
246 void BluetoothDeviceWin::Disconnect(
247 const base::Closure& callback,
248 const ErrorCallback& error_callback) {
249 NOTIMPLEMENTED();
252 void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
253 NOTIMPLEMENTED();
256 void 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);
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 void BluetoothDeviceWin::StartConnectionMonitor(
274 const base::Closure& callback,
275 const ErrorCallback& error_callback) {
276 NOTIMPLEMENTED();
279 const 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;
287 return NULL;
290 } // namespace device