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"
20 @
class BluetoothAdapterMacDelegate
;
21 @
class IOBluetoothDevice
;
22 @
class IOBluetoothDeviceInquiry
;
28 class SequencedTaskRunner
;
34 class BluetoothAdapterMacTest
;
36 class BluetoothAdapterMac
: public BluetoothAdapter
{
38 static base::WeakPtr
<BluetoothAdapter
> CreateAdapter();
41 virtual void AddObserver(BluetoothAdapter::Observer
* observer
) OVERRIDE
;
42 virtual void RemoveObserver(BluetoothAdapter::Observer
* observer
) OVERRIDE
;
43 virtual std::string
GetAddress() const OVERRIDE
;
44 virtual std::string
GetName() const OVERRIDE
;
45 virtual void SetName(const std::string
& name
,
46 const base::Closure
& callback
,
47 const ErrorCallback
& error_callback
) OVERRIDE
;
48 virtual bool IsInitialized() const OVERRIDE
;
49 virtual bool IsPresent() const OVERRIDE
;
50 virtual bool IsPowered() const OVERRIDE
;
51 virtual void SetPowered(
53 const base::Closure
& callback
,
54 const ErrorCallback
& error_callback
) OVERRIDE
;
55 virtual bool IsDiscoverable() const OVERRIDE
;
56 virtual void SetDiscoverable(
58 const base::Closure
& callback
,
59 const ErrorCallback
& error_callback
) OVERRIDE
;
60 virtual bool IsDiscovering() const OVERRIDE
;
61 virtual void ReadLocalOutOfBandPairingData(
62 const BluetoothOutOfBandPairingDataCallback
& callback
,
63 const ErrorCallback
& error_callback
) OVERRIDE
;
64 virtual void CreateRfcommService(
65 const BluetoothUUID
& uuid
,
68 const CreateServiceCallback
& callback
,
69 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
;
70 virtual void CreateL2capService(
71 const BluetoothUUID
& uuid
,
73 const CreateServiceCallback
& callback
,
74 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
;
76 // called by BluetoothAdapterMacDelegate.
77 void DeviceInquiryStarted(IOBluetoothDeviceInquiry
* inquiry
);
78 void DeviceFound(IOBluetoothDeviceInquiry
* inquiry
,
79 IOBluetoothDevice
* device
);
80 void DeviceInquiryComplete(IOBluetoothDeviceInquiry
* inquiry
,
86 virtual void RemovePairingDelegateInternal(
87 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) OVERRIDE
;
90 friend class BluetoothAdapterMacTest
;
92 enum DiscoveryStatus
{
99 BluetoothAdapterMac();
100 virtual ~BluetoothAdapterMac();
103 virtual void AddDiscoverySession(
104 const base::Closure
& callback
,
105 const ErrorCallback
& error_callback
) OVERRIDE
;
106 virtual void RemoveDiscoverySession(
107 const base::Closure
& callback
,
108 const ErrorCallback
& error_callback
) OVERRIDE
;
111 void InitForTest(scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
114 // Updates |devices_| to be consistent with |devices|.
115 void UpdateDevices(NSArray
* devices
);
117 void MaybeStartDeviceInquiry();
118 void MaybeStopDeviceInquiry();
120 typedef std::vector
<std::pair
<base::Closure
, ErrorCallback
> >
121 DiscoveryCallbackList
;
122 void RunCallbacks(const DiscoveryCallbackList
& callback_list
,
125 std::string address_
;
128 DiscoveryStatus discovery_status_
;
130 DiscoveryCallbackList on_start_discovery_callbacks_
;
131 DiscoveryCallbackList on_stop_discovery_callbacks_
;
132 size_t num_discovery_listeners_
;
134 base::scoped_nsobject
<BluetoothAdapterMacDelegate
> adapter_delegate_
;
135 base::scoped_nsobject
<IOBluetoothDeviceInquiry
> device_inquiry_
;
137 // A list of discovered device addresses.
138 // This list is used to check if the same device is discovered twice during
139 // the discovery between consecutive inquiries.
140 base::hash_set
<std::string
> discovered_devices_
;
142 // Timestamp for the recently accessed device.
143 // Used to determine if |devices_| needs an update.
144 base::scoped_nsobject
<NSDate
> recently_accessed_device_timestamp_
;
146 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
148 // List of observers interested in event notifications from us.
149 ObserverList
<BluetoothAdapter::Observer
> observers_
;
151 base::WeakPtrFactory
<BluetoothAdapterMac
> weak_ptr_factory_
;
153 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac
);
156 } // namespace device
158 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_