Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_win.cc
blob4d7d2a907131db02b8a0febe5328704c9b7a6f3a
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_adapter_win.h"
16 #include "device/bluetooth/bluetooth_service_record_win.h"
17 #include "device/bluetooth/bluetooth_socket_thread.h"
18 #include "device/bluetooth/bluetooth_socket_win.h"
19 #include "device/bluetooth/bluetooth_task_manager_win.h"
20 #include "device/bluetooth/bluetooth_uuid.h"
22 namespace {
24 const char kApiUnavailable[] = "This API is not implemented on this platform.";
26 } // namespace
28 namespace device {
30 BluetoothDeviceWin::BluetoothDeviceWin(
31 BluetoothAdapterWin* adapter,
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(adapter),
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 uint32 BluetoothDeviceWin::GetBluetoothClass() const {
49 return bluetooth_class_;
52 std::string BluetoothDeviceWin::GetAddress() const {
53 return address_;
56 BluetoothDevice::VendorIDSource
57 BluetoothDeviceWin::GetVendorIDSource() const {
58 return VENDOR_ID_UNKNOWN;
61 uint16 BluetoothDeviceWin::GetVendorID() const {
62 return 0;
65 uint16 BluetoothDeviceWin::GetProductID() const {
66 return 0;
69 uint16 BluetoothDeviceWin::GetDeviceID() const {
70 return 0;
73 bool BluetoothDeviceWin::IsPaired() const {
74 return paired_;
77 bool BluetoothDeviceWin::IsConnected() const {
78 return connected_;
81 bool BluetoothDeviceWin::IsGattConnected() const {
82 NOTIMPLEMENTED();
83 return false;
86 bool BluetoothDeviceWin::IsConnectable() const {
87 return false;
90 bool BluetoothDeviceWin::IsConnecting() const {
91 return false;
94 BluetoothDevice::UUIDList BluetoothDeviceWin::GetUUIDs() const {
95 return uuids_;
98 int16 BluetoothDeviceWin::GetInquiryRSSI() const {
99 return kUnknownPower;
102 int16 BluetoothDeviceWin::GetInquiryTxPower() const {
103 NOTIMPLEMENTED();
104 return kUnknownPower;
107 bool BluetoothDeviceWin::ExpectingPinCode() const {
108 NOTIMPLEMENTED();
109 return false;
112 bool BluetoothDeviceWin::ExpectingPasskey() const {
113 NOTIMPLEMENTED();
114 return false;
117 bool BluetoothDeviceWin::ExpectingConfirmation() const {
118 NOTIMPLEMENTED();
119 return false;
122 void BluetoothDeviceWin::GetConnectionInfo(
123 const ConnectionInfoCallback& callback) {
124 NOTIMPLEMENTED();
125 callback.Run(ConnectionInfo());
128 void BluetoothDeviceWin::Connect(
129 PairingDelegate* pairing_delegate,
130 const base::Closure& callback,
131 const ConnectErrorCallback& error_callback) {
132 NOTIMPLEMENTED();
135 void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
136 NOTIMPLEMENTED();
139 void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
140 NOTIMPLEMENTED();
143 void BluetoothDeviceWin::ConfirmPairing() {
144 NOTIMPLEMENTED();
147 void BluetoothDeviceWin::RejectPairing() {
148 NOTIMPLEMENTED();
151 void BluetoothDeviceWin::CancelPairing() {
152 NOTIMPLEMENTED();
155 void BluetoothDeviceWin::Disconnect(
156 const base::Closure& callback,
157 const ErrorCallback& error_callback) {
158 NOTIMPLEMENTED();
161 void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
162 NOTIMPLEMENTED();
165 void BluetoothDeviceWin::ConnectToService(
166 const BluetoothUUID& uuid,
167 const ConnectToServiceCallback& callback,
168 const ConnectToServiceErrorCallback& error_callback) {
169 scoped_refptr<BluetoothSocketWin> socket(
170 BluetoothSocketWin::CreateBluetoothSocket(
171 ui_task_runner_, socket_thread_));
172 socket->Connect(this, uuid, base::Bind(callback, socket), error_callback);
175 void BluetoothDeviceWin::ConnectToServiceInsecurely(
176 const BluetoothUUID& uuid,
177 const ConnectToServiceCallback& callback,
178 const ConnectToServiceErrorCallback& error_callback) {
179 error_callback.Run(kApiUnavailable);
182 void BluetoothDeviceWin::CreateGattConnection(
183 const GattConnectionCallback& callback,
184 const ConnectErrorCallback& error_callback) {
185 // TODO(armansito): Implement.
186 error_callback.Run(ERROR_UNSUPPORTED_DEVICE);
189 const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord(
190 const device::BluetoothUUID& uuid) const {
191 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
192 iter != service_record_list_.end();
193 ++iter) {
194 if ((*iter)->uuid() == uuid)
195 return *iter;
197 return NULL;
200 bool BluetoothDeviceWin::IsEqual(
201 const BluetoothTaskManagerWin::DeviceState& device_state) {
202 if (address_ != device_state.address || name_ != device_state.name ||
203 bluetooth_class_ != device_state.bluetooth_class ||
204 visible_ != device_state.visible ||
205 connected_ != device_state.connected ||
206 paired_ != device_state.authenticated) {
207 return false;
210 // Checks service collection
211 typedef std::set<BluetoothUUID> UUIDSet;
212 typedef base::ScopedPtrHashMap<
213 std::string, scoped_ptr<BluetoothServiceRecordWin>> ServiceRecordMap;
215 UUIDSet known_services;
216 for (UUIDList::const_iterator iter = uuids_.begin(); iter != uuids_.end();
217 ++iter) {
218 known_services.insert((*iter));
221 UUIDSet new_services;
222 ServiceRecordMap new_service_records;
223 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
224 iter = device_state.service_record_states.begin();
225 iter != device_state.service_record_states.end(); ++iter) {
226 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin(
227 address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid);
228 new_services.insert(service_record->uuid());
229 new_service_records.set(
230 service_record->uuid().canonical_value(),
231 scoped_ptr<BluetoothServiceRecordWin>(service_record));
234 UUIDSet removed_services =
235 base::STLSetDifference<UUIDSet>(known_services, new_services);
236 if (!removed_services.empty()) {
237 return false;
239 UUIDSet added_devices =
240 base::STLSetDifference<UUIDSet>(new_services, known_services);
241 if (!added_devices.empty()) {
242 return false;
245 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
246 iter != service_record_list_.end(); ++iter) {
247 BluetoothServiceRecordWin* service_record = (*iter);
248 BluetoothServiceRecordWin* new_service_record =
249 new_service_records.get((*iter)->uuid().canonical_value());
250 if (!service_record->IsEqual(*new_service_record))
251 return false;
253 return true;
256 void BluetoothDeviceWin::Update(
257 const BluetoothTaskManagerWin::DeviceState& device_state) {
258 address_ = device_state.address;
259 // Note: Callers are responsible for providing a canonicalized address.
260 DCHECK_EQ(address_, BluetoothDevice::CanonicalizeAddress(address_));
261 name_ = device_state.name;
262 bluetooth_class_ = device_state.bluetooth_class;
263 visible_ = device_state.visible;
264 connected_ = device_state.connected;
265 paired_ = device_state.authenticated;
266 UpdateServices(device_state);
269 std::string BluetoothDeviceWin::GetDeviceName() const {
270 return name_;
273 void BluetoothDeviceWin::CreateGattConnectionImpl() {
274 // Windows implementation does not use the default CreateGattConnection
275 // implementation.
276 NOTIMPLEMENTED();
279 void BluetoothDeviceWin::DisconnectGatt() {
280 // Windows implementation does not use the default CreateGattConnection
281 // implementation.
282 NOTIMPLEMENTED();
285 void BluetoothDeviceWin::SetVisible(bool visible) {
286 visible_ = visible;
289 void BluetoothDeviceWin::UpdateServices(
290 const BluetoothTaskManagerWin::DeviceState& device_state) {
291 uuids_.clear();
292 service_record_list_.clear();
294 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
295 iter = device_state.service_record_states.begin();
296 iter != device_state.service_record_states.end(); ++iter) {
297 BluetoothServiceRecordWin* service_record =
298 new BluetoothServiceRecordWin(device_state.address, (*iter)->name,
299 (*iter)->sdp_bytes, (*iter)->gatt_uuid);
300 service_record_list_.push_back(service_record);
301 uuids_.push_back(service_record->uuid());
305 } // namespace device