• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_adapter.h"
6 
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "device/bluetooth/bluetooth_device.h"
10 #include "device/bluetooth/bluetooth_discovery_session.h"
11 
12 namespace device {
13 
14 #if !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
15 //static
CreateAdapter(const InitCallback & init_callback)16 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
17     const InitCallback& init_callback) {
18   return base::WeakPtr<BluetoothAdapter>();
19 }
20 #endif  // !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
21 
22 const int BluetoothAdapter::kChannelAuto = 0;
23 const int BluetoothAdapter::kPsmAuto = 0;
24 
BluetoothAdapter()25 BluetoothAdapter::BluetoothAdapter()
26     : weak_ptr_factory_(this) {
27 }
28 
~BluetoothAdapter()29 BluetoothAdapter::~BluetoothAdapter() {
30   STLDeleteValues(&devices_);
31 }
32 
GetWeakPtrForTesting()33 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::GetWeakPtrForTesting() {
34   return weak_ptr_factory_.GetWeakPtr();
35 }
36 
StartDiscoverySession(const DiscoverySessionCallback & callback,const ErrorCallback & error_callback)37 void BluetoothAdapter::StartDiscoverySession(
38     const DiscoverySessionCallback& callback,
39     const ErrorCallback& error_callback) {
40   AddDiscoverySession(
41       base::Bind(&BluetoothAdapter::OnStartDiscoverySession,
42                  weak_ptr_factory_.GetWeakPtr(),
43                  callback),
44       error_callback);
45 }
46 
GetDevices()47 BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() {
48   ConstDeviceList const_devices =
49     const_cast<const BluetoothAdapter *>(this)->GetDevices();
50 
51   DeviceList devices;
52   for (ConstDeviceList::const_iterator i = const_devices.begin();
53        i != const_devices.end(); ++i)
54     devices.push_back(const_cast<BluetoothDevice *>(*i));
55 
56   return devices;
57 }
58 
GetDevices() const59 BluetoothAdapter::ConstDeviceList BluetoothAdapter::GetDevices() const {
60   ConstDeviceList devices;
61   for (DevicesMap::const_iterator iter = devices_.begin();
62        iter != devices_.end();
63        ++iter)
64     devices.push_back(iter->second);
65 
66   return devices;
67 }
68 
GetDevice(const std::string & address)69 BluetoothDevice* BluetoothAdapter::GetDevice(const std::string& address) {
70   return const_cast<BluetoothDevice *>(
71       const_cast<const BluetoothAdapter *>(this)->GetDevice(address));
72 }
73 
GetDevice(const std::string & address) const74 const BluetoothDevice* BluetoothAdapter::GetDevice(
75     const std::string& address) const {
76   std::string canonicalized_address =
77       BluetoothDevice::CanonicalizeAddress(address);
78   if (canonicalized_address.empty())
79     return NULL;
80 
81   DevicesMap::const_iterator iter = devices_.find(canonicalized_address);
82   if (iter != devices_.end())
83     return iter->second;
84 
85   return NULL;
86 }
87 
AddPairingDelegate(BluetoothDevice::PairingDelegate * pairing_delegate,PairingDelegatePriority priority)88 void BluetoothAdapter::AddPairingDelegate(
89     BluetoothDevice::PairingDelegate* pairing_delegate,
90     PairingDelegatePriority priority) {
91   // Remove the delegate, if it already exists, before inserting to allow a
92   // change of priority.
93   RemovePairingDelegate(pairing_delegate);
94 
95   // Find the first point with a lower priority, or the end of the list.
96   std::list<PairingDelegatePair>::iterator iter = pairing_delegates_.begin();
97   while (iter != pairing_delegates_.end() && iter->second >= priority)
98     ++iter;
99 
100   pairing_delegates_.insert(iter, std::make_pair(pairing_delegate, priority));
101 }
102 
RemovePairingDelegate(BluetoothDevice::PairingDelegate * pairing_delegate)103 void BluetoothAdapter::RemovePairingDelegate(
104     BluetoothDevice::PairingDelegate* pairing_delegate) {
105   for (std::list<PairingDelegatePair>::iterator iter =
106        pairing_delegates_.begin(); iter != pairing_delegates_.end(); ++iter) {
107     if (iter->first == pairing_delegate) {
108       RemovePairingDelegateInternal(pairing_delegate);
109       pairing_delegates_.erase(iter);
110       return;
111     }
112   }
113 }
114 
DefaultPairingDelegate()115 BluetoothDevice::PairingDelegate* BluetoothAdapter::DefaultPairingDelegate() {
116   if (pairing_delegates_.empty())
117     return NULL;
118 
119   return pairing_delegates_.front().first;
120 }
121 
OnStartDiscoverySession(const DiscoverySessionCallback & callback)122 void BluetoothAdapter::OnStartDiscoverySession(
123     const DiscoverySessionCallback& callback) {
124   VLOG(1) << "Discovery session started!";
125   scoped_ptr<BluetoothDiscoverySession> discovery_session(
126       new BluetoothDiscoverySession(scoped_refptr<BluetoothAdapter>(this)));
127   discovery_sessions_.insert(discovery_session.get());
128   callback.Run(discovery_session.Pass());
129 }
130 
MarkDiscoverySessionsAsInactive()131 void BluetoothAdapter::MarkDiscoverySessionsAsInactive() {
132   // As sessions are marked as inactive they will notify the adapter that they
133   // have become inactive, upon which the adapter will remove them from
134   // |discovery_sessions_|. To avoid invalidating the iterator, make a copy
135   // here.
136   std::set<BluetoothDiscoverySession*> temp(discovery_sessions_);
137   for (std::set<BluetoothDiscoverySession*>::iterator
138           iter = temp.begin();
139        iter != temp.end(); ++iter) {
140     (*iter)->MarkAsInactive();
141   }
142 }
143 
DiscoverySessionBecameInactive(BluetoothDiscoverySession * discovery_session)144 void BluetoothAdapter::DiscoverySessionBecameInactive(
145     BluetoothDiscoverySession* discovery_session) {
146   DCHECK(!discovery_session->IsActive());
147   discovery_sessions_.erase(discovery_session);
148 }
149 
150 }  // namespace device
151