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"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/strings/stringprintf.h"
13 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
14 #include "device/bluetooth/bluetooth_profile_win.h"
15 #include "device/bluetooth/bluetooth_service_record_win.h"
16 #include "device/bluetooth/bluetooth_socket_win.h"
17 #include "device/bluetooth/bluetooth_task_manager_win.h"
21 const int kSdpBytesBufferSize = 1024;
27 BluetoothDeviceWin::BluetoothDeviceWin(
28 const BluetoothTaskManagerWin::DeviceState& state)
31 address_ = state.address;
32 bluetooth_class_ = state.bluetooth_class;
33 visible_ = state.visible;
34 connected_ = state.connected;
35 paired_ = state.authenticated;
37 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
38 iter = state.service_record_states.begin();
39 iter != state.service_record_states.end();
41 uint8 sdp_bytes_buffer[kSdpBytesBufferSize];
42 std::copy((*iter)->sdp_bytes.begin(),
43 (*iter)->sdp_bytes.end(),
45 BluetoothServiceRecord* service_record = new BluetoothServiceRecordWin(
48 (*iter)->sdp_bytes.size(),
50 service_record_list_.push_back(service_record);
51 service_uuids_.push_back(service_record->uuid());
55 BluetoothDeviceWin::~BluetoothDeviceWin() {
58 void BluetoothDeviceWin::SetVisible(bool visible) {
62 uint32 BluetoothDeviceWin::GetBluetoothClass() const {
63 return bluetooth_class_;
66 std::string BluetoothDeviceWin::GetDeviceName() const {
70 std::string BluetoothDeviceWin::GetAddress() const {
74 uint16 BluetoothDeviceWin::GetVendorID() const {
78 uint16 BluetoothDeviceWin::GetProductID() const {
82 uint16 BluetoothDeviceWin::GetDeviceID() const {
86 bool BluetoothDeviceWin::IsPaired() const {
90 bool BluetoothDeviceWin::IsConnected() const {
94 bool BluetoothDeviceWin::IsConnectable() const {
98 bool BluetoothDeviceWin::IsConnecting() const {
102 BluetoothDevice::ServiceList BluetoothDeviceWin::GetServices() const {
103 return service_uuids_;
106 void BluetoothDeviceWin::GetServiceRecords(
107 const ServiceRecordsCallback& callback,
108 const ErrorCallback& error_callback) {
109 callback.Run(service_record_list_);
112 void BluetoothDeviceWin::ProvidesServiceWithName(
113 const std::string& name,
114 const ProvidesServiceCallback& callback) {
115 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
116 iter != service_record_list_.end();
118 if ((*iter)->name() == name) {
126 bool BluetoothDeviceWin::ExpectingPinCode() const {
131 bool BluetoothDeviceWin::ExpectingPasskey() const {
136 bool BluetoothDeviceWin::ExpectingConfirmation() const {
141 void BluetoothDeviceWin::Connect(
142 PairingDelegate* pairing_delegate,
143 const base::Closure& callback,
144 const ConnectErrorCallback& error_callback) {
148 void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
152 void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
156 void BluetoothDeviceWin::ConfirmPairing() {
160 void BluetoothDeviceWin::RejectPairing() {
164 void BluetoothDeviceWin::CancelPairing() {
168 void BluetoothDeviceWin::Disconnect(
169 const base::Closure& callback,
170 const ErrorCallback& error_callback) {
174 void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
178 void BluetoothDeviceWin::ConnectToService(
179 const std::string& service_uuid,
180 const SocketCallback& callback) {
181 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
182 iter != service_record_list_.end();
184 if ((*iter)->uuid() == service_uuid) {
185 // If multiple service records are found, use the first one that works.
186 scoped_refptr<BluetoothSocket> socket(
187 BluetoothSocketWin::CreateBluetoothSocket(**iter));
188 if (socket.get() != NULL) {
189 callback.Run(socket);
196 void BluetoothDeviceWin::ConnectToProfile(
197 device::BluetoothProfile* profile,
198 const base::Closure& callback,
199 const ErrorCallback& error_callback) {
200 if (static_cast<BluetoothProfileWin*>(profile)->Connect(this))
203 error_callback.Run();
206 void BluetoothDeviceWin::SetOutOfBandPairingData(
207 const BluetoothOutOfBandPairingData& data,
208 const base::Closure& callback,
209 const ErrorCallback& error_callback) {
213 void BluetoothDeviceWin::ClearOutOfBandPairingData(
214 const base::Closure& callback,
215 const ErrorCallback& error_callback) {
219 const BluetoothServiceRecord* BluetoothDeviceWin::GetServiceRecord(
220 const std::string& uuid) const {
221 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
222 iter != service_record_list_.end();
224 if ((*iter)->uuid().compare(uuid) == 0)
230 } // namespace device