aboutsummaryrefslogtreecommitdiff
path: root/service/adapter.h
blob: a4e517c2cc28f274a01bc95b14b49491651bf5fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
//  Copyright (C) 2015 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#pragma once

#include <memory>
#include <string>
#include <vector>

#include <base/macros.h>

#include "service/common/bluetooth/adapter_state.h"
#include "service/common/bluetooth/remote_device_props.h"

namespace bluetooth {

class A2dpSinkFactory;
class A2dpSourceFactory;
class AvrcpControlFactory;
class AvrcpTargetFactory;
class GattClientFactory;
class GattServerFactory;
class LowEnergyAdvertiserFactory;
class LowEnergyScannerFactory;
class LowEnergyClientFactory;

// Represents the local Bluetooth adapter.
class Adapter {
 public:
  // The default values returned before the Adapter is fully initialized and
  // powered. The complete values for these fields are obtained following a
  // successful call to "Enable".
  static const char kDefaultAddress[];
  static const char kDefaultName[];

  // Observer interface allows other classes to receive notifications from us.
  // All of the methods in this interface are declared as optional to allow
  // different layers to process only those events that they are interested in.
  //
  // All methods take in an |adapter| argument which points to the Adapter
  // object that the Observer instance was added to.
  class Observer {
   public:
    virtual ~Observer() = default;

    // Called when there is a change in the state of the local Bluetooth
    // |adapter| from |prev_state| to |new_state|.
    virtual void OnAdapterStateChanged(Adapter* adapter,
                                       AdapterState prev_state,
                                       AdapterState new_state);

    // Called when there is a change in the connection state between the local
    // |adapter| and a remote device with address |device_address|. If the ACL
    // state changes from disconnected to connected, then |connected| will be
    // true and vice versa.
    virtual void OnDeviceConnectionStateChanged(
        Adapter* adapter, const std::string& device_address, bool connected);

    // Called when scanning is enabled or disabled.
    virtual void OnScanEnableChanged(Adapter* adapter, bool scan_enabled);

    // Called when a SSP pairing request comes from a remote device.
    virtual void OnSspRequest(Adapter* adapter,
                              const std::string& device_address,
                              const std::string& device_name, int cod,
                              int pairing_variant, int pass_key);

    // Called when a remote device bond state changes.
    virtual void OnBondStateChanged(Adapter* adapter, int status,
                                    const std::string& device_address,
                                    int state);

    // Called in response to |GetBondedDevices|.
    virtual void OnGetBondedDevices(
        Adapter* adapter, int status,
        const std::vector<std::string>& bonded_devices);

    // Called in response to |GetRemoteDeviceProperties|.
    virtual void OnGetRemoteDeviceProperties(Adapter* adapter, int status,
                                             const std::string& device_address,
                                             const RemoteDeviceProps& props);

    // Called when a device is found through scanning.
    virtual void OnDeviceFound(Adapter* adapter,
                               const RemoteDeviceProps& props);
  };

  // Returns an Adapter implementation to be used in production. Don't use these
  // in tests; use MockAdapter instead.
  static std::unique_ptr<Adapter> Create();

  virtual ~Adapter() = default;

  // Add or remove an observer.
  virtual void AddObserver(Observer* observer) = 0;
  virtual void RemoveObserver(Observer* observer) = 0;

  // Returns the current Adapter state.
  virtual AdapterState GetState() const = 0;

  // Returns true, if the adapter radio is current powered.
  virtual bool IsEnabled() const = 0;

  // Enables Bluetooth. This method will send a request to the Bluetooth adapter
  // to power up its radio. Returns true, if the request was successfully sent
  // to the controller, otherwise returns false. A successful call to this
  // method only means that the enable request has been sent to the Bluetooth
  // controller and does not imply that the operation itself succeeded.
  virtual bool Enable() = 0;

  // Powers off the Bluetooth radio. Returns true, if the disable request was
  // successfully sent to the Bluetooth controller.
  virtual bool Disable() = 0;

  // Returns the name currently assigned to the local adapter.
  virtual std::string GetName() const = 0;

  // Sets the name assigned to the local Bluetooth adapter. This is the name
  // that the local controller will present to remote devices.
  virtual bool SetName(const std::string& name) = 0;

  // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
  virtual std::string GetAddress() const = 0;

  // Set discoverability mode.
  virtual bool SetScanMode(int scan_mode) = 0;

  // Enable or disable discoverability.
  virtual bool SetScanEnable(bool scan_enable) = 0;

  // Reply to an SSP request received in |OnSspRequest|.
  virtual bool SspReply(const std::string& device_address, int variant,
                        bool accept, int32_t pass_key) = 0;

  // Create a bond with device specified by |device_address|.
  virtual bool CreateBond(const std::string& device_address, int transport) = 0;

  // Returns true if the local adapter supports the Low-Energy
  // multi-advertisement feature.
  virtual bool IsMultiAdvertisementSupported() = 0;

  // Returns true if the remote device with address |device_address| is
  // currently connected. This is not a const method as it modifies the state of
  // the associated internal mutex.
  virtual bool IsDeviceConnected(const std::string& device_address) = 0;

  // Returns the total number of trackable advertisements as supported by the
  // underlying hardware.
  virtual int GetTotalNumberOfTrackableAdvertisements() = 0;

  // Returns true if hardware-backed scan filtering is supported.
  virtual bool IsOffloadedFilteringSupported() = 0;

  // Returns true if hardware-backed batch scanning is supported.
  virtual bool IsOffloadedScanBatchingSupported() = 0;

  // When the stack call completes, |OnGetBondedDevices| will be called.
  virtual bool GetBondedDevices() = 0;

  // When the stack call completets, |OnBondStateChanged| will be called.
  virtual bool RemoveBond(const std::string& device_address) = 0;

  // When the stack call completets, |OnGetRemoteDeviceProperties| will be
  // called.
  virtual bool GetRemoteDeviceProperties(const std::string& device_address) = 0;

  // Returns a pointer to the A2dpSinkFactory. This can be used to
  // register per-application A2dpSinkClient instances to perform A2DP sink
  // operations.
  virtual A2dpSinkFactory* GetA2dpSinkFactory() const = 0;

  // Returns a pointer to the A2dpSourceFactory. This can be used to
  // register per-application A2dpSourceClient instances to perform A2DP source
  // operations.
  virtual A2dpSourceFactory* GetA2dpSourceFactory() const = 0;

  // Returns a pointer to the AvrcpControlFactory. This can be used to register
  // per-application AvrcpControlClient instances to perform AVRCP control
  // operations.
  virtual AvrcpControlFactory* GetAvrcpControlFactory() const = 0;

  // Returns a pointer to the AvrcpTargetFactory. This can be used to register
  // per-application AvrcpTargetClient instances to perform AVRCP target
  // operations.
  virtual AvrcpTargetFactory* GetAvrcpTargetFactory() const = 0;

  // Returns a pointer to the LowEnergyClientFactory. This can be used to
  // register per-application LowEnergyClient instances to perform BLE GAP
  // operations.
  virtual LowEnergyClientFactory* GetLowEnergyClientFactory() const = 0;

  // Returns a pointer to the LowEnergyScannerFactory. This can be used to
  // register per-application LowEnergyScanner instances to perform scanning.
  virtual LowEnergyScannerFactory* GetLeScannerFactory() const = 0;

  // Returns a pointer to the LowEnergyAdvertiserFactory. This can be used to
  // register per-application LowEnergyAdvertiser instances to perform
  // advertising.
  virtual LowEnergyAdvertiserFactory* GetLeAdvertiserFactory() const = 0;

  // Returns a pointer to the GattClientFactory. This can be used to register
  // per-application GATT server instances.
  virtual GattClientFactory* GetGattClientFactory() const = 0;

  // Returns a pointer to the GattServerFactory. This can be used to register
  // per-application GATT server instances.
  virtual GattServerFactory* GetGattServerFactory() const = 0;

 protected:
  Adapter() = default;

 private:
  DISALLOW_COPY_AND_ASSIGN(Adapter);
};

}  // namespace bluetooth