bluetooth_device_win.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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. 4 5#include "device/bluetooth/bluetooth_device_win.h" 6 7#include <string> 8 9#include "base/basictypes.h" 10#include "base/hash.h" 11#include "base/logging.h" 12#include "base/memory/scoped_vector.h" 13#include "base/stringprintf.h" 14#include "device/bluetooth/bluetooth_out_of_band_pairing_data.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" 18 19namespace { 20 21const int kSdpBytesBufferSize = 1024; 22 23} // namespace 24 25namespace device { 26 27BluetoothDeviceWin::BluetoothDeviceWin( 28 const BluetoothTaskManagerWin::DeviceState& state) 29 : BluetoothDevice(), device_fingerprint_(ComputeDeviceFingerprint(state)) { 30 name_ = state.name; 31 address_ = state.address; 32 bluetooth_class_ = state.bluetooth_class; 33 connected_ = state.connected; 34 bonded_ = state.authenticated; 35 36 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator 37 iter = state.service_record_states.begin(); 38 iter != state.service_record_states.end(); 39 ++iter) { 40 uint8 sdp_bytes_buffer[kSdpBytesBufferSize]; 41 std::copy((*iter)->sdp_bytes.begin(), 42 (*iter)->sdp_bytes.end(), 43 sdp_bytes_buffer); 44 BluetoothServiceRecord* service_record = new BluetoothServiceRecordWin( 45 (*iter)->name, 46 (*iter)->address, 47 (*iter)->sdp_bytes.size(), 48 sdp_bytes_buffer); 49 service_record_list_.push_back(service_record); 50 service_uuids_.push_back(service_record->uuid()); 51 } 52} 53 54BluetoothDeviceWin::~BluetoothDeviceWin() { 55} 56 57void BluetoothDeviceWin::SetVisible(bool visible) { 58 visible_ = visible; 59} 60 61bool BluetoothDeviceWin::IsPaired() const { 62 return false; 63} 64 65const BluetoothDevice::ServiceList& BluetoothDeviceWin::GetServices() const { 66 return service_uuids_; 67} 68 69void BluetoothDeviceWin::GetServiceRecords( 70 const ServiceRecordsCallback& callback, 71 const ErrorCallback& error_callback) { 72 callback.Run(service_record_list_); 73} 74 75void BluetoothDeviceWin::ProvidesServiceWithName( 76 const std::string& name, 77 const ProvidesServiceCallback& callback) { 78 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); 79 iter != service_record_list_.end(); 80 ++iter) { 81 if ((*iter)->name() == name) { 82 callback.Run(true); 83 return; 84 } 85 } 86 callback.Run(false); 87} 88 89bool BluetoothDeviceWin::ExpectingPinCode() const { 90 NOTIMPLEMENTED(); 91 return false; 92} 93 94bool BluetoothDeviceWin::ExpectingPasskey() const { 95 NOTIMPLEMENTED(); 96 return false; 97} 98 99bool BluetoothDeviceWin::ExpectingConfirmation() const { 100 NOTIMPLEMENTED(); 101 return false; 102} 103 104void BluetoothDeviceWin::Connect( 105 PairingDelegate* pairing_delegate, 106 const base::Closure& callback, 107 const ConnectErrorCallback& error_callback) { 108 NOTIMPLEMENTED(); 109} 110 111void BluetoothDeviceWin::SetPinCode(const std::string& pincode) { 112 NOTIMPLEMENTED(); 113} 114 115void BluetoothDeviceWin::SetPasskey(uint32 passkey) { 116 NOTIMPLEMENTED(); 117} 118 119void BluetoothDeviceWin::ConfirmPairing() { 120 NOTIMPLEMENTED(); 121} 122 123void BluetoothDeviceWin::RejectPairing() { 124 NOTIMPLEMENTED(); 125} 126 127void BluetoothDeviceWin::CancelPairing() { 128 NOTIMPLEMENTED(); 129} 130 131void BluetoothDeviceWin::Disconnect( 132 const base::Closure& callback, 133 const ErrorCallback& error_callback) { 134 NOTIMPLEMENTED(); 135} 136 137void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) { 138 NOTIMPLEMENTED(); 139} 140 141void BluetoothDeviceWin::ConnectToService( 142 const std::string& service_uuid, 143 const SocketCallback& callback) { 144 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); 145 iter != service_record_list_.end(); 146 ++iter) { 147 if ((*iter)->uuid() == service_uuid) { 148 // If multiple service records are found, use the first one that works. 149 scoped_refptr<BluetoothSocket> socket( 150 BluetoothSocketWin::CreateBluetoothSocket(**iter)); 151 if (socket.get() != NULL) { 152 callback.Run(socket); 153 return; 154 } 155 } 156 } 157} 158 159void BluetoothDeviceWin::SetOutOfBandPairingData( 160 const BluetoothOutOfBandPairingData& data, 161 const base::Closure& callback, 162 const ErrorCallback& error_callback) { 163 NOTIMPLEMENTED(); 164} 165 166void BluetoothDeviceWin::ClearOutOfBandPairingData( 167 const base::Closure& callback, 168 const ErrorCallback& error_callback) { 169 NOTIMPLEMENTED(); 170} 171 172// static 173uint32 BluetoothDeviceWin::ComputeDeviceFingerprint( 174 const BluetoothTaskManagerWin::DeviceState& state) { 175 std::string device_string = base::StringPrintf("%s%s%u%s%s%s", 176 state.name.c_str(), 177 state.address.c_str(), 178 state.bluetooth_class, 179 state.visible ? "true" : "false", 180 state.connected ? "true" : "false", 181 state.authenticated ? "true" : "false"); 182 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator 183 iter = state.service_record_states.begin(); 184 iter != state.service_record_states.end(); 185 ++iter) { 186 base::StringAppendF(&device_string, 187 "%s%s%d", 188 (*iter)->name.c_str(), 189 (*iter)->address.c_str(), 190 (*iter)->sdp_bytes.size()); 191 } 192 return base::Hash(device_string); 193} 194 195} // namespace device 196