• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright (C) 2015 Google, Inc.
3 //
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 //
16 
17 #pragma once
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <base/macros.h>
24 
25 #include "service/common/bluetooth/adapter_state.h"
26 #include "service/common/bluetooth/remote_device_props.h"
27 
28 namespace bluetooth {
29 
30 class A2dpSinkFactory;
31 class A2dpSourceFactory;
32 class AvrcpControlFactory;
33 class AvrcpTargetFactory;
34 class GattClientFactory;
35 class GattServerFactory;
36 class LowEnergyAdvertiserFactory;
37 class LowEnergyScannerFactory;
38 class LowEnergyClientFactory;
39 
40 // Represents the local Bluetooth adapter.
41 class Adapter {
42  public:
43   // The default values returned before the Adapter is fully initialized and
44   // powered. The complete values for these fields are obtained following a
45   // successful call to "Enable".
46   static const char kDefaultAddress[];
47   static const char kDefaultName[];
48 
49   // Observer interface allows other classes to receive notifications from us.
50   // All of the methods in this interface are declared as optional to allow
51   // different layers to process only those events that they are interested in.
52   //
53   // All methods take in an |adapter| argument which points to the Adapter
54   // object that the Observer instance was added to.
55   class Observer {
56    public:
57     virtual ~Observer() = default;
58 
59     // Called when there is a change in the state of the local Bluetooth
60     // |adapter| from |prev_state| to |new_state|.
61     virtual void OnAdapterStateChanged(Adapter* adapter,
62                                        AdapterState prev_state,
63                                        AdapterState new_state);
64 
65     // Called when there is a change in the connection state between the local
66     // |adapter| and a remote device with address |device_address|. If the ACL
67     // state changes from disconnected to connected, then |connected| will be
68     // true and vice versa.
69     virtual void OnDeviceConnectionStateChanged(
70         Adapter* adapter, const std::string& device_address, bool connected);
71 
72     // Called when scanning is enabled or disabled.
73     virtual void OnScanEnableChanged(Adapter* adapter, bool scan_enabled);
74 
75     // Called when a SSP pairing request comes from a remote device.
76     virtual void OnSspRequest(Adapter* adapter,
77                               const std::string& device_address,
78                               const std::string& device_name, int cod,
79                               int pairing_variant, int pass_key);
80 
81     // Called when a remote device bond state changes.
82     virtual void OnBondStateChanged(Adapter* adapter, int status,
83                                     const std::string& device_address,
84                                     int state);
85 
86     // Called in response to |GetBondedDevices|.
87     virtual void OnGetBondedDevices(
88         Adapter* adapter, int status,
89         const std::vector<std::string>& bonded_devices);
90 
91     // Called in response to |GetRemoteDeviceProperties|.
92     virtual void OnGetRemoteDeviceProperties(Adapter* adapter, int status,
93                                              const std::string& device_address,
94                                              const RemoteDeviceProps& props);
95 
96     // Called when a device is found through scanning.
97     virtual void OnDeviceFound(Adapter* adapter,
98                                const RemoteDeviceProps& props);
99   };
100 
101   // Returns an Adapter implementation to be used in production. Don't use these
102   // in tests; use MockAdapter instead.
103   static std::unique_ptr<Adapter> Create();
104 
105   virtual ~Adapter() = default;
106 
107   // Add or remove an observer.
108   virtual void AddObserver(Observer* observer) = 0;
109   virtual void RemoveObserver(Observer* observer) = 0;
110 
111   // Returns the current Adapter state.
112   virtual AdapterState GetState() const = 0;
113 
114   // Returns true, if the adapter radio is current powered.
115   virtual bool IsEnabled() const = 0;
116 
117   // Enables Bluetooth. This method will send a request to the Bluetooth adapter
118   // to power up its radio. Returns true, if the request was successfully sent
119   // to the controller, otherwise returns false. A successful call to this
120   // method only means that the enable request has been sent to the Bluetooth
121   // controller and does not imply that the operation itself succeeded.
122   virtual bool Enable() = 0;
123 
124   // Powers off the Bluetooth radio. Returns true, if the disable request was
125   // successfully sent to the Bluetooth controller.
126   virtual bool Disable() = 0;
127 
128   // Returns the name currently assigned to the local adapter.
129   virtual std::string GetName() const = 0;
130 
131   // Sets the name assigned to the local Bluetooth adapter. This is the name
132   // that the local controller will present to remote devices.
133   virtual bool SetName(const std::string& name) = 0;
134 
135   // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
136   virtual std::string GetAddress() const = 0;
137 
138   // Set discoverability mode.
139   virtual bool SetScanMode(int scan_mode) = 0;
140 
141   // Enable or disable discoverability.
142   virtual bool SetScanEnable(bool scan_enable) = 0;
143 
144   // Reply to an SSP request received in |OnSspRequest|.
145   virtual bool SspReply(const std::string& device_address, int variant,
146                         bool accept, int32_t pass_key) = 0;
147 
148   // Create a bond with device specified by |device_address|.
149   virtual bool CreateBond(const std::string& device_address, int transport) = 0;
150 
151   // Returns true if the local adapter supports the Low-Energy
152   // multi-advertisement feature.
153   virtual bool IsMultiAdvertisementSupported() = 0;
154 
155   // Returns true if the remote device with address |device_address| is
156   // currently connected. This is not a const method as it modifies the state of
157   // the associated internal mutex.
158   virtual bool IsDeviceConnected(const std::string& device_address) = 0;
159 
160   // Returns the total number of trackable advertisements as supported by the
161   // underlying hardware.
162   virtual int GetTotalNumberOfTrackableAdvertisements() = 0;
163 
164   // Returns true if hardware-backed scan filtering is supported.
165   virtual bool IsOffloadedFilteringSupported() = 0;
166 
167   // Returns true if hardware-backed batch scanning is supported.
168   virtual bool IsOffloadedScanBatchingSupported() = 0;
169 
170   // When the stack call completes, |OnGetBondedDevices| will be called.
171   virtual bool GetBondedDevices() = 0;
172 
173   // When the stack call completets, |OnBondStateChanged| will be called.
174   virtual bool RemoveBond(const std::string& device_address) = 0;
175 
176   // When the stack call completets, |OnGetRemoteDeviceProperties| will be
177   // called.
178   virtual bool GetRemoteDeviceProperties(const std::string& device_address) = 0;
179 
180   // Returns a pointer to the A2dpSinkFactory. This can be used to
181   // register per-application A2dpSinkClient instances to perform A2DP sink
182   // operations.
183   virtual A2dpSinkFactory* GetA2dpSinkFactory() const = 0;
184 
185   // Returns a pointer to the A2dpSourceFactory. This can be used to
186   // register per-application A2dpSourceClient instances to perform A2DP source
187   // operations.
188   virtual A2dpSourceFactory* GetA2dpSourceFactory() const = 0;
189 
190   // Returns a pointer to the AvrcpControlFactory. This can be used to register
191   // per-application AvrcpControlClient instances to perform AVRCP control
192   // operations.
193   virtual AvrcpControlFactory* GetAvrcpControlFactory() const = 0;
194 
195   // Returns a pointer to the AvrcpTargetFactory. This can be used to register
196   // per-application AvrcpTargetClient instances to perform AVRCP target
197   // operations.
198   virtual AvrcpTargetFactory* GetAvrcpTargetFactory() const = 0;
199 
200   // Returns a pointer to the LowEnergyClientFactory. This can be used to
201   // register per-application LowEnergyClient instances to perform BLE GAP
202   // operations.
203   virtual LowEnergyClientFactory* GetLowEnergyClientFactory() const = 0;
204 
205   // Returns a pointer to the LowEnergyScannerFactory. This can be used to
206   // register per-application LowEnergyScanner instances to perform scanning.
207   virtual LowEnergyScannerFactory* GetLeScannerFactory() const = 0;
208 
209   // Returns a pointer to the LowEnergyAdvertiserFactory. This can be used to
210   // register per-application LowEnergyAdvertiser instances to perform
211   // advertising.
212   virtual LowEnergyAdvertiserFactory* GetLeAdvertiserFactory() const = 0;
213 
214   // Returns a pointer to the GattClientFactory. This can be used to register
215   // per-application GATT server instances.
216   virtual GattClientFactory* GetGattClientFactory() const = 0;
217 
218   // Returns a pointer to the GattServerFactory. This can be used to register
219   // per-application GATT server instances.
220   virtual GattServerFactory* GetGattServerFactory() const = 0;
221 
222  protected:
223   Adapter() = default;
224 
225  private:
226   DISALLOW_COPY_AND_ASSIGN(Adapter);
227 };
228 
229 }  // namespace bluetooth
230