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
;
37 class BluetoothAdapterMacTest
;
39 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterMac
40 : public BluetoothAdapter
,
41 public BluetoothDiscoveryManagerMac::Observer
,
42 public BluetoothLowEnergyDiscoveryManagerMac::Observer
{
44 static base::WeakPtr
<BluetoothAdapter
> CreateAdapter();
46 // BluetoothAdapter overrides:
47 std::string
GetAddress() const override
;
48 std::string
GetName() const override
;
49 void SetName(const std::string
& name
,
50 const base::Closure
& callback
,
51 const ErrorCallback
& error_callback
) override
;
52 bool IsInitialized() const override
;
53 bool IsPresent() const override
;
54 bool IsPowered() const override
;
55 void SetPowered(bool powered
,
56 const base::Closure
& callback
,
57 const ErrorCallback
& error_callback
) override
;
58 bool IsDiscoverable() const override
;
59 void SetDiscoverable(bool discoverable
,
60 const base::Closure
& callback
,
61 const ErrorCallback
& error_callback
) override
;
62 bool IsDiscovering() const override
;
63 void CreateRfcommService(
64 const BluetoothUUID
& uuid
,
65 const ServiceOptions
& options
,
66 const CreateServiceCallback
& callback
,
67 const CreateServiceErrorCallback
& error_callback
) override
;
68 void CreateL2capService(
69 const BluetoothUUID
& uuid
,
70 const ServiceOptions
& options
,
71 const CreateServiceCallback
& callback
,
72 const CreateServiceErrorCallback
& error_callback
) override
;
73 void RegisterAudioSink(
74 const BluetoothAudioSink::Options
& options
,
75 const AcquiredCallback
& callback
,
76 const BluetoothAudioSink::ErrorCallback
& error_callback
) override
;
77 void RegisterAdvertisement(
78 scoped_ptr
<BluetoothAdvertisement::Data
> advertisement_data
,
79 const CreateAdvertisementCallback
& callback
,
80 const CreateAdvertisementErrorCallback
& error_callback
) override
;
82 // BluetoothDiscoveryManagerMac::Observer overrides:
83 void ClassicDeviceFound(IOBluetoothDevice
* device
) override
;
84 void ClassicDiscoveryStopped(bool unexpected
) override
;
86 // BluetoothLowEnergyDiscoveryManagerMac::Observer override:
87 void LowEnergyDeviceUpdated(CBPeripheral
* peripheral
,
88 NSDictionary
* advertisementData
,
91 // Registers that a new |device| has connected to the local host.
92 void DeviceConnected(IOBluetoothDevice
* device
);
95 // BluetoothAdapter override:
96 void RemovePairingDelegateInternal(
97 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) override
;
100 friend class BluetoothAdapterMacTest
;
102 BluetoothAdapterMac();
103 ~BluetoothAdapterMac() override
;
105 // BluetoothAdapter overrides:
106 void AddDiscoverySession(BluetoothDiscoveryFilter
* discovery_filter
,
107 const base::Closure
& callback
,
108 const ErrorCallback
& error_callback
) override
;
109 void RemoveDiscoverySession(BluetoothDiscoveryFilter
* discovery_filter
,
110 const base::Closure
& callback
,
111 const ErrorCallback
& error_callback
) override
;
112 void SetDiscoveryFilter(scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
113 const base::Closure
& callback
,
114 const ErrorCallback
& error_callback
) override
;
116 // Start classic and/or low energy discovery sessions, according to the
117 // filter. If a discovery session is already running the filter is updated.
118 bool StartDiscovery(BluetoothDiscoveryFilter
* discovery_filter
);
121 void InitForTest(scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
124 // Registers that a new |device| has replied to an Inquiry, is paired, or has
125 // connected to the local host.
126 void ClassicDeviceAdded(IOBluetoothDevice
* device
);
128 // Updates |devices_| to include the currently paired devices, as well as any
129 // connected, but unpaired, devices. Notifies observers if any previously
130 // paired or connected devices are no longer present.
131 void UpdateDevices();
133 std::string address_
;
137 int num_discovery_sessions_
;
139 // Discovery manager for Bluetooth Classic.
140 scoped_ptr
<BluetoothDiscoveryManagerMac
> classic_discovery_manager_
;
142 // Discovery manager for Bluetooth Low Energy.
143 scoped_ptr
<BluetoothLowEnergyDiscoveryManagerMac
>
144 low_energy_discovery_manager_
;
146 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
148 base::WeakPtrFactory
<BluetoothAdapterMac
> weak_ptr_factory_
;
150 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac
);
153 } // namespace device
155 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_