• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
7 
8 #include <IOKit/IOReturn.h>
9 
10 #include <string>
11 #include <vector>
12 
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_discovery_manager_mac.h"
20 
21 @class IOBluetoothDevice;
22 @class NSArray;
23 @class NSDate;
24 
25 namespace base {
26 
27 class SequencedTaskRunner;
28 
29 }  // namespace base
30 
31 namespace device {
32 
33 class BluetoothAdapterMacTest;
34 
35 class BluetoothAdapterMac : public BluetoothAdapter,
36                             public BluetoothDiscoveryManagerMac::Observer {
37  public:
38   static base::WeakPtr<BluetoothAdapter> CreateAdapter();
39 
40   // BluetoothAdapter:
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(
52       bool powered,
53       const base::Closure& callback,
54       const ErrorCallback& error_callback) OVERRIDE;
55   virtual bool IsDiscoverable() const OVERRIDE;
56   virtual void SetDiscoverable(
57       bool discoverable,
58       const base::Closure& callback,
59       const ErrorCallback& error_callback) OVERRIDE;
60   virtual bool IsDiscovering() const OVERRIDE;
61   virtual void CreateRfcommService(
62       const BluetoothUUID& uuid,
63       int channel,
64       const CreateServiceCallback& callback,
65       const CreateServiceErrorCallback& error_callback) OVERRIDE;
66   virtual void CreateL2capService(
67       const BluetoothUUID& uuid,
68       int psm,
69       const CreateServiceCallback& callback,
70       const CreateServiceErrorCallback& error_callback) OVERRIDE;
71 
72   // BluetoothDiscoveryManagerMac::Observer overrides
73   virtual void DeviceFound(BluetoothDiscoveryManagerMac* manager,
74                            IOBluetoothDevice* device) OVERRIDE;
75   virtual void DiscoveryStopped(BluetoothDiscoveryManagerMac* manager,
76                                 bool unexpected) OVERRIDE;
77 
78   // Registers that a new |device| has connected to the local host.
79   void DeviceConnected(IOBluetoothDevice* device);
80 
81  protected:
82   // BluetoothAdapter:
83   virtual void RemovePairingDelegateInternal(
84       device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE;
85 
86  private:
87   friend class BluetoothAdapterMacTest;
88 
89   BluetoothAdapterMac();
90   virtual ~BluetoothAdapterMac();
91 
92   // BluetoothAdapter:
93   virtual void AddDiscoverySession(
94       const base::Closure& callback,
95       const ErrorCallback& error_callback) OVERRIDE;
96   virtual void RemoveDiscoverySession(
97       const base::Closure& callback,
98       const ErrorCallback& error_callback) OVERRIDE;
99 
100   void Init();
101   void InitForTest(scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
102   void PollAdapter();
103 
104   // Updates |devices_| to include the currently paired devices, as well as any
105   // connected, but unpaired, devices. Notifies observers if any previously
106   // paired or connected devices are no longer present.
107   void UpdateDevices();
108 
109   std::string address_;
110   std::string name_;
111   bool powered_;
112 
113   int num_discovery_sessions_;
114 
115   // Discovery manager for Bluetooth Classic.
116   scoped_ptr<BluetoothDiscoveryManagerMac> classic_discovery_manager_;
117 
118   // A list of discovered device addresses.
119   // This list is used to check if the same device is discovered twice during
120   // the discovery between consecutive inquiries.
121   base::hash_set<std::string> discovered_devices_;
122 
123   // Timestamp for the recently accessed device.
124   // Used to determine if |devices_| needs an update.
125   base::scoped_nsobject<NSDate> recently_accessed_device_timestamp_;
126 
127   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
128 
129   // List of observers interested in event notifications from us.
130   ObserverList<BluetoothAdapter::Observer> observers_;
131 
132   base::WeakPtrFactory<BluetoothAdapterMac> weak_ptr_factory_;
133 
134   DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac);
135 };
136 
137 }  // namespace device
138 
139 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
140