1 // Copyright 2013 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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
8 #include <IOKit/IOReturn.h>
13 #include "base/containers/hash_tables.h"
14 #include "base/mac/scoped_nsobject.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "device/bluetooth/bluetooth_adapter.h"
19 #include "device/bluetooth/bluetooth_audio_sink.h"
20 #include "device/bluetooth/bluetooth_discovery_manager_mac.h"
21 #include "device/bluetooth/bluetooth_export.h"
22 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
23 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h"
25 @
class IOBluetoothDevice
;
31 class SequencedTaskRunner
;
35 @
class BluetoothLowEnergyCentralManagerDelegate
;
39 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterMac
40 : public BluetoothAdapter
,
41 public BluetoothDiscoveryManagerMac::Observer
,
42 public BluetoothLowEnergyDiscoveryManagerMac::Observer
{
44 static base::WeakPtr
<BluetoothAdapterMac
> CreateAdapter();
45 static base::WeakPtr
<BluetoothAdapterMac
> CreateAdapterForTest(
48 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
50 // BluetoothAdapter overrides:
51 std::string
GetAddress() const override
;
52 std::string
GetName() const override
;
53 void SetName(const std::string
& name
,
54 const base::Closure
& callback
,
55 const ErrorCallback
& error_callback
) override
;
56 bool IsInitialized() const override
;
57 bool IsPresent() const override
;
58 bool IsPowered() const override
;
59 void SetPowered(bool powered
,
60 const base::Closure
& callback
,
61 const ErrorCallback
& error_callback
) override
;
62 bool IsDiscoverable() const override
;
63 void SetDiscoverable(bool discoverable
,
64 const base::Closure
& callback
,
65 const ErrorCallback
& error_callback
) override
;
66 bool IsDiscovering() const override
;
67 void CreateRfcommService(
68 const BluetoothUUID
& uuid
,
69 const ServiceOptions
& options
,
70 const CreateServiceCallback
& callback
,
71 const CreateServiceErrorCallback
& error_callback
) override
;
72 void CreateL2capService(
73 const BluetoothUUID
& uuid
,
74 const ServiceOptions
& options
,
75 const CreateServiceCallback
& callback
,
76 const CreateServiceErrorCallback
& error_callback
) override
;
77 void RegisterAudioSink(
78 const BluetoothAudioSink::Options
& options
,
79 const AcquiredCallback
& callback
,
80 const BluetoothAudioSink::ErrorCallback
& error_callback
) override
;
81 void RegisterAdvertisement(
82 scoped_ptr
<BluetoothAdvertisement::Data
> advertisement_data
,
83 const CreateAdvertisementCallback
& callback
,
84 const CreateAdvertisementErrorCallback
& error_callback
) override
;
86 // BluetoothDiscoveryManagerMac::Observer overrides:
87 void ClassicDeviceFound(IOBluetoothDevice
* device
) override
;
88 void ClassicDiscoveryStopped(bool unexpected
) override
;
90 // Registers that a new |device| has connected to the local host.
91 void DeviceConnected(IOBluetoothDevice
* device
);
93 // We only use CoreBluetooth when OS X >= 10.10. This because the
94 // CBCentralManager destructor was found to crash on the mac_chromium_rel_ng
95 // builder running 10.9.5. May also cause blued to crash on OS X 10.9.5
96 // (crbug.com/506287).
97 static bool IsLowEnergyAvailable();
99 // Resets |low_energy_central_manager_| to |central_manager| and sets
100 // |low_energy_central_manager_delegate_| as the manager's delegate. Should
101 // be called only when |IsLowEnergyAvailable()|.
102 void SetCentralManagerForTesting(CBCentralManager
* central_manager
);
105 // BluetoothAdapter override:
106 void RemovePairingDelegateInternal(
107 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) override
;
110 // The length of time that must elapse since the last Inquiry response (on
111 // Classic devices) or call to BluetoothLowEnergyDevice::Update() (on Low
112 // Energy) before a discovered device is considered to be no longer available.
113 const static NSTimeInterval kDiscoveryTimeoutSec
;
115 friend class BluetoothTestMac
;
116 friend class BluetoothAdapterMacTest
;
117 friend class BluetoothLowEnergyCentralManagerBridge
;
119 BluetoothAdapterMac();
120 ~BluetoothAdapterMac() override
;
122 // BluetoothAdapter overrides:
123 void AddDiscoverySession(
124 BluetoothDiscoveryFilter
* discovery_filter
,
125 const base::Closure
& callback
,
126 const DiscoverySessionErrorCallback
& error_callback
) override
;
127 void RemoveDiscoverySession(
128 BluetoothDiscoveryFilter
* discovery_filter
,
129 const base::Closure
& callback
,
130 const DiscoverySessionErrorCallback
& error_callback
) override
;
131 void SetDiscoveryFilter(
132 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
133 const base::Closure
& callback
,
134 const DiscoverySessionErrorCallback
& error_callback
) override
;
136 // Start classic and/or low energy discovery sessions, according to the
137 // filter. If a discovery session is already running the filter is updated.
138 bool StartDiscovery(BluetoothDiscoveryFilter
* discovery_filter
);
141 void InitForTest(scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
144 // Registers that a new |device| has replied to an Inquiry, is paired, or has
145 // connected to the local host.
146 void ClassicDeviceAdded(IOBluetoothDevice
* device
);
148 // BluetoothLowEnergyDiscoveryManagerMac::Observer override:
149 void LowEnergyDeviceUpdated(CBPeripheral
* peripheral
,
150 NSDictionary
* advertisement_data
,
153 // Updates |devices_| when there is a change to the CBCentralManager's state.
154 void LowEnergyCentralManagerUpdatedState();
156 // Removes from |devices_| any previously paired, connected or seen devices
157 // which are no longer present. Notifies observers.
158 void RemoveTimedOutDevices();
160 // Updates |devices_| to include the currently paired devices and notifies
162 void AddPairedDevices();
164 std::string address_
;
166 bool classic_powered_
;
167 int num_discovery_sessions_
;
169 // Discovery manager for Bluetooth Classic.
170 scoped_ptr
<BluetoothDiscoveryManagerMac
> classic_discovery_manager_
;
172 // Discovery manager for Bluetooth Low Energy.
173 scoped_ptr
<BluetoothLowEnergyDiscoveryManagerMac
>
174 low_energy_discovery_manager_
;
176 // Underlying CoreBluetooth CBCentralManager and its delegate.
177 base::scoped_nsobject
<CBCentralManager
> low_energy_central_manager_
;
178 base::scoped_nsobject
<BluetoothLowEnergyCentralManagerDelegate
>
179 low_energy_central_manager_delegate_
;
181 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
183 base::WeakPtrFactory
<BluetoothAdapterMac
> weak_ptr_factory_
;
185 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac
);
188 } // namespace device
190 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_