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(BluetoothDiscoveryFilter
* discovery_filter
,
124 const base::Closure
& callback
,
125 const ErrorCallback
& error_callback
) override
;
126 void RemoveDiscoverySession(BluetoothDiscoveryFilter
* discovery_filter
,
127 const base::Closure
& callback
,
128 const ErrorCallback
& error_callback
) override
;
129 void SetDiscoveryFilter(scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
130 const base::Closure
& callback
,
131 const ErrorCallback
& error_callback
) override
;
133 // Start classic and/or low energy discovery sessions, according to the
134 // filter. If a discovery session is already running the filter is updated.
135 bool StartDiscovery(BluetoothDiscoveryFilter
* discovery_filter
);
138 void InitForTest(scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
141 // Registers that a new |device| has replied to an Inquiry, is paired, or has
142 // connected to the local host.
143 void ClassicDeviceAdded(IOBluetoothDevice
* device
);
145 // BluetoothLowEnergyDiscoveryManagerMac::Observer override:
146 void LowEnergyDeviceUpdated(CBPeripheral
* peripheral
,
147 NSDictionary
* advertisement_data
,
150 // Updates |devices_| when there is a change to the CBCentralManager's state.
151 void LowEnergyCentralManagerUpdatedState();
153 // Removes from |devices_| any previously paired, connected or seen devices
154 // which are no longer present. Notifies observers.
155 void RemoveTimedOutDevices();
157 // Updates |devices_| to include the currently paired devices and notifies
159 void AddPairedDevices();
161 std::string address_
;
163 bool classic_powered_
;
164 int num_discovery_sessions_
;
166 // Discovery manager for Bluetooth Classic.
167 scoped_ptr
<BluetoothDiscoveryManagerMac
> classic_discovery_manager_
;
169 // Discovery manager for Bluetooth Low Energy.
170 scoped_ptr
<BluetoothLowEnergyDiscoveryManagerMac
>
171 low_energy_discovery_manager_
;
173 // Underlying CoreBluetooth CBCentralManager and its delegate.
174 base::scoped_nsobject
<CBCentralManager
> low_energy_central_manager_
;
175 base::scoped_nsobject
<BluetoothLowEnergyCentralManagerDelegate
>
176 low_energy_central_manager_delegate_
;
178 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
180 base::WeakPtrFactory
<BluetoothAdapterMac
> weak_ptr_factory_
;
182 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac
);
185 } // namespace device
187 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_