Extensions: Store disable reasons in Sync
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_win.cc
blobde00817726eadf017ec4d293ccf1c81fa344d60d
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<
93 std::string, scoped_ptr<BluetoothServiceRecordWin>> 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 int16 BluetoothDeviceWin::GetInquiryRSSI() const {
192 return kUnknownPower;
195 int16 BluetoothDeviceWin::GetInquiryTxPower() const {
196 NOTIMPLEMENTED();
197 return kUnknownPower;
200 bool BluetoothDeviceWin::ExpectingPinCode() const {
201 NOTIMPLEMENTED();
202 return false;
205 bool BluetoothDeviceWin::ExpectingPasskey() const {
206 NOTIMPLEMENTED();
207 return false;
210 bool BluetoothDeviceWin::ExpectingConfirmation() const {
211 NOTIMPLEMENTED();
212 return false;
215 void BluetoothDeviceWin::GetConnectionInfo(
216 const ConnectionInfoCallback& callback) {
217 NOTIMPLEMENTED();
218 callback.Run(ConnectionInfo());
221 void BluetoothDeviceWin::Connect(
222 PairingDelegate* pairing_delegate,
223 const base::Closure& callback,
224 const ConnectErrorCallback& error_callback) {
225 NOTIMPLEMENTED();
228 void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
229 NOTIMPLEMENTED();
232 void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
233 NOTIMPLEMENTED();
236 void BluetoothDeviceWin::ConfirmPairing() {
237 NOTIMPLEMENTED();
240 void BluetoothDeviceWin::RejectPairing() {
241 NOTIMPLEMENTED();
244 void BluetoothDeviceWin::CancelPairing() {
245 NOTIMPLEMENTED();
248 void BluetoothDeviceWin::Disconnect(
249 const base::Closure& callback,
250 const ErrorCallback& error_callback) {
251 NOTIMPLEMENTED();
254 void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
255 NOTIMPLEMENTED();
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();
286 ++iter) {
287 if ((*iter)->uuid() == uuid)
288 return *iter;
290 return NULL;
293 } // namespace device