Home | History | Annotate | Download | only in bluetooth
      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/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"
     18 
     19 namespace {
     20 
     21 const int kSdpBytesBufferSize = 1024;
     22 
     23 }  // namespace
     24 
     25 namespace device {
     26 
     27 BluetoothDeviceWin::BluetoothDeviceWin(
     28     const BluetoothTaskManagerWin::DeviceState& state)
     29     : BluetoothDevice() {
     30   name_ = state.name;
     31   address_ = state.address;
     32   bluetooth_class_ = state.bluetooth_class;
     33   visible_ = state.visible;
     34   connected_ = state.connected;
     35   paired_ = state.authenticated;
     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   }
     53 }
     54 
     55 BluetoothDeviceWin::~BluetoothDeviceWin() {
     56 }
     57 
     58 void BluetoothDeviceWin::SetVisible(bool visible) {
     59   visible_ = visible;
     60 }
     61 
     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 
     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 
     86 bool BluetoothDeviceWin::IsPaired() const {
     87   return paired_;
     88 }
     89 
     90 bool BluetoothDeviceWin::IsConnected() const {
     91   return connected_;
     92 }
     93 
     94 bool BluetoothDeviceWin::IsConnectable() const {
     95   return false;
     96 }
     97 
     98 bool BluetoothDeviceWin::IsConnecting() const {
     99   return false;
    100 }
    101 
    102 BluetoothDevice::ServiceList BluetoothDeviceWin::GetServices() const {
    103   return service_uuids_;
    104 }
    105 
    106 void BluetoothDeviceWin::GetServiceRecords(
    107     const ServiceRecordsCallback& callback,
    108     const ErrorCallback& error_callback) {
    109   callback.Run(service_record_list_);
    110 }
    111 
    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();
    117        ++iter) {
    118     if ((*iter)->name() == name) {
    119       callback.Run(true);
    120       return;
    121     }
    122   }
    123   callback.Run(false);
    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,
    144     const ConnectErrorCallback& error_callback) {
    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) {
    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   }
    194 }
    195 
    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))
    201     callback.Run();
    202   else
    203     error_callback.Run();
    204 }
    205 
    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 
    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) {
    224     if ((*iter)->uuid().compare(uuid) == 0)
    225       return *iter;
    226   }
    227   return NULL;
    228 }
    229 
    230 }  // namespace device
    231