[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 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. |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 4 | |
| 5 | #include "device/bluetooth/bluetooth_device_win.h" |
| 6 | |
| 7 | #include <string> |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 8 | |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 9 | #include "base/basictypes.h" |
| 10 | #include "base/logging.h" |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 11 | #include "base/memory/scoped_vector.h" |
[email protected] | 0d8db08 | 2013-06-11 07:27:01 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 13 | #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
[email protected] | e55a50f | 2013-05-05 03:37:09 | [diff] [blame] | 14 | #include "device/bluetooth/bluetooth_profile_win.h" |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 15 | #include "device/bluetooth/bluetooth_service_record_win.h" |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 16 | #include "device/bluetooth/bluetooth_socket_win.h" |
| 17 | #include "device/bluetooth/bluetooth_task_manager_win.h" |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | const int kSdpBytesBufferSize = 1024; |
| 22 | |
| 23 | } // namespace |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 24 | |
| 25 | namespace device { |
| 26 | |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 27 | BluetoothDeviceWin::BluetoothDeviceWin( |
| 28 | const BluetoothTaskManagerWin::DeviceState& state) |
[email protected] | e55a50f | 2013-05-05 03:37:09 | [diff] [blame] | 29 | : BluetoothDevice() { |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 30 | name_ = state.name; |
| 31 | address_ = state.address; |
| 32 | bluetooth_class_ = state.bluetooth_class; |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame] | 33 | visible_ = state.visible; |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 34 | connected_ = state.connected; |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame] | 35 | paired_ = state.authenticated; |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 36 | |
| 37 | for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
| 38 | iter = state.service_record_states.begin(); |
| 39 | iter != state.service_record_states.end(); |
| 40 | ++iter) { |
| 41 | uint8 sdp_bytes_buffer[kSdpBytesBufferSize]; |
| 42 | std::copy((*iter)->sdp_bytes.begin(), |
| 43 | (*iter)->sdp_bytes.end(), |
| 44 | sdp_bytes_buffer); |
| 45 | BluetoothServiceRecord* service_record = new BluetoothServiceRecordWin( |
| 46 | (*iter)->name, |
| 47 | (*iter)->address, |
| 48 | (*iter)->sdp_bytes.size(), |
| 49 | sdp_bytes_buffer); |
| 50 | service_record_list_.push_back(service_record); |
| 51 | service_uuids_.push_back(service_record->uuid()); |
| 52 | } |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | BluetoothDeviceWin::~BluetoothDeviceWin() { |
| 56 | } |
| 57 | |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 58 | void BluetoothDeviceWin::SetVisible(bool visible) { |
| 59 | visible_ = visible; |
| 60 | } |
| 61 | |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame] | 62 | uint32 BluetoothDeviceWin::GetBluetoothClass() const { |
| 63 | return bluetooth_class_; |
| 64 | } |
| 65 | |
| 66 | std::string BluetoothDeviceWin::GetDeviceName() const { |
| 67 | return name_; |
| 68 | } |
| 69 | |
| 70 | std::string BluetoothDeviceWin::GetAddress() const { |
| 71 | return address_; |
| 72 | } |
| 73 | |
[email protected] | 611ae29a | 2013-04-29 21:32:19 | [diff] [blame] | 74 | uint16 BluetoothDeviceWin::GetVendorID() const { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | uint16 BluetoothDeviceWin::GetProductID() const { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | uint16 BluetoothDeviceWin::GetDeviceID() const { |
| 83 | return 0; |
| 84 | } |
| 85 | |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 86 | bool BluetoothDeviceWin::IsPaired() const { |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame] | 87 | return paired_; |
| 88 | } |
| 89 | |
| 90 | bool BluetoothDeviceWin::IsConnected() const { |
| 91 | return connected_; |
| 92 | } |
| 93 | |
| 94 | bool BluetoothDeviceWin::IsConnectable() const { |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
[email protected] | 320447d7 | 2013-04-05 03:32:26 | [diff] [blame] | 98 | bool BluetoothDeviceWin::IsConnecting() const { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | BluetoothDevice::ServiceList BluetoothDeviceWin::GetServices() const { |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 103 | return service_uuids_; |
| 104 | } |
| 105 | |
| 106 | void BluetoothDeviceWin::GetServiceRecords( |
| 107 | const ServiceRecordsCallback& callback, |
| 108 | const ErrorCallback& error_callback) { |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 109 | callback.Run(service_record_list_); |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void BluetoothDeviceWin::ProvidesServiceWithName( |
| 113 | const std::string& name, |
| 114 | const ProvidesServiceCallback& callback) { |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 115 | for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
| 116 | iter != service_record_list_.end(); |
| 117 | ++iter) { |
| 118 | if ((*iter)->name() == name) { |
| 119 | callback.Run(true); |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | callback.Run(false); |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bool BluetoothDeviceWin::ExpectingPinCode() const { |
| 127 | NOTIMPLEMENTED(); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool BluetoothDeviceWin::ExpectingPasskey() const { |
| 132 | NOTIMPLEMENTED(); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | bool BluetoothDeviceWin::ExpectingConfirmation() const { |
| 137 | NOTIMPLEMENTED(); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | void BluetoothDeviceWin::Connect( |
| 142 | PairingDelegate* pairing_delegate, |
| 143 | const base::Closure& callback, |
[email protected] | eb72b66 | 2012-12-18 06:55:41 | [diff] [blame] | 144 | const ConnectErrorCallback& error_callback) { |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 145 | NOTIMPLEMENTED(); |
| 146 | } |
| 147 | |
| 148 | void BluetoothDeviceWin::SetPinCode(const std::string& pincode) { |
| 149 | NOTIMPLEMENTED(); |
| 150 | } |
| 151 | |
| 152 | void BluetoothDeviceWin::SetPasskey(uint32 passkey) { |
| 153 | NOTIMPLEMENTED(); |
| 154 | } |
| 155 | |
| 156 | void BluetoothDeviceWin::ConfirmPairing() { |
| 157 | NOTIMPLEMENTED(); |
| 158 | } |
| 159 | |
| 160 | void BluetoothDeviceWin::RejectPairing() { |
| 161 | NOTIMPLEMENTED(); |
| 162 | } |
| 163 | |
| 164 | void BluetoothDeviceWin::CancelPairing() { |
| 165 | NOTIMPLEMENTED(); |
| 166 | } |
| 167 | |
| 168 | void BluetoothDeviceWin::Disconnect( |
| 169 | const base::Closure& callback, |
| 170 | const ErrorCallback& error_callback) { |
| 171 | NOTIMPLEMENTED(); |
| 172 | } |
| 173 | |
| 174 | void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) { |
| 175 | NOTIMPLEMENTED(); |
| 176 | } |
| 177 | |
| 178 | void BluetoothDeviceWin::ConnectToService( |
| 179 | const std::string& service_uuid, |
| 180 | const SocketCallback& callback) { |
[email protected] | edcaf14a | 2013-02-25 17:46:36 | [diff] [blame] | 181 | for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
| 182 | iter != service_record_list_.end(); |
| 183 | ++iter) { |
| 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); |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | } |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 2862df46 | 2013-04-19 21:07:21 | [diff] [blame] | 196 | void BluetoothDeviceWin::ConnectToProfile( |
| 197 | device::BluetoothProfile* profile, |
[email protected] | 476f29d | 2013-04-24 09:06:51 | [diff] [blame] | 198 | const base::Closure& callback, |
[email protected] | 2862df46 | 2013-04-19 21:07:21 | [diff] [blame] | 199 | const ErrorCallback& error_callback) { |
[email protected] | e55a50f | 2013-05-05 03:37:09 | [diff] [blame] | 200 | if (static_cast<BluetoothProfileWin*>(profile)->Connect(this)) |
| 201 | callback.Run(); |
| 202 | else |
| 203 | error_callback.Run(); |
[email protected] | 2862df46 | 2013-04-19 21:07:21 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 206 | void BluetoothDeviceWin::SetOutOfBandPairingData( |
| 207 | const BluetoothOutOfBandPairingData& data, |
| 208 | const base::Closure& callback, |
| 209 | const ErrorCallback& error_callback) { |
| 210 | NOTIMPLEMENTED(); |
| 211 | } |
| 212 | |
| 213 | void BluetoothDeviceWin::ClearOutOfBandPairingData( |
| 214 | const base::Closure& callback, |
| 215 | const ErrorCallback& error_callback) { |
| 216 | NOTIMPLEMENTED(); |
| 217 | } |
| 218 | |
[email protected] | e55a50f | 2013-05-05 03:37:09 | [diff] [blame] | 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(); |
| 223 | ++iter) { |
[email protected] | 69b6838 | 2013-07-31 15:49:36 | [diff] [blame] | 224 | if ((*iter)->uuid().compare(uuid) == 0) |
| 225 | return *iter; |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 226 | } |
[email protected] | e55a50f | 2013-05-05 03:37:09 | [diff] [blame] | 227 | return NULL; |
[email protected] | d7ba4375 | 2013-02-21 22:27:05 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 0ae9759 | 2012-10-22 22:06:05 | [diff] [blame] | 230 | } // namespace device |