blob: a4e517c2cc28f274a01bc95b14b49491651bf5fe [file] [log] [blame]
Arman Uguray4fdadb62015-08-13 16:09:35 -07001//
Bailey Forrest62aa15f2017-01-30 17:38:58 -08002// Copyright (C) 2015 Google, Inc.
Arman Uguray4fdadb62015-08-13 16:09:35 -07003//
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
Arman Uguray0a0d3932015-11-19 15:57:57 -080019#include <memory>
Bailey Forrest62aa15f2017-01-30 17:38:58 -080020#include <string>
21#include <vector>
Arman Uguray4fdadb62015-08-13 16:09:35 -070022
23#include <base/macros.h>
24
Arman Uguray234138e2015-10-06 15:56:36 -070025#include "service/common/bluetooth/adapter_state.h"
Bailey Forrest62aa15f2017-01-30 17:38:58 -080026#include "service/common/bluetooth/remote_device_props.h"
Arman Uguray4fdadb62015-08-13 16:09:35 -070027
28namespace bluetooth {
29
Bailey Forrest62aa15f2017-01-30 17:38:58 -080030class A2dpSinkFactory;
Bailey Forrest780e29c2018-08-21 17:20:29 -070031class A2dpSourceFactory;
Bailey Forrest62aa15f2017-01-30 17:38:58 -080032class AvrcpControlFactory;
Bailey Forrest780e29c2018-08-21 17:20:29 -070033class AvrcpTargetFactory;
Arman Uguray0a0d3932015-11-19 15:57:57 -080034class GattClientFactory;
35class GattServerFactory;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070036class LowEnergyAdvertiserFactory;
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070037class LowEnergyScannerFactory;
Arman Uguray0a0d3932015-11-19 15:57:57 -080038class LowEnergyClientFactory;
39
Arman Uguray4fdadb62015-08-13 16:09:35 -070040// Represents the local Bluetooth adapter.
Arman Uguray0a0d3932015-11-19 15:57:57 -080041class Adapter {
Arman Uguray4fdadb62015-08-13 16:09:35 -070042 public:
Arman Uguray03b1f0f2015-08-17 17:23:42 -070043 // 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
Arman Uguray4cebc7e2015-08-20 11:38:49 -070049 // 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.
Arman Uguray0f29c002015-11-13 15:05:48 -080052 //
53 // All methods take in an |adapter| argument which points to the Adapter
54 // object that the Observer instance was added to.
Arman Uguray4cebc7e2015-08-20 11:38:49 -070055 class Observer {
56 public:
57 virtual ~Observer() = default;
58
Arman Uguray0f29c002015-11-13 15:05:48 -080059 // Called when there is a change in the state of the local Bluetooth
60 // |adapter| from |prev_state| to |new_state|.
Arman Uguray4cebc7e2015-08-20 11:38:49 -070061 virtual void OnAdapterStateChanged(Adapter* adapter,
62 AdapterState prev_state,
63 AdapterState new_state);
Arman Uguray0f29c002015-11-13 15:05:48 -080064
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);
Bailey Forrest62aa15f2017-01-30 17:38:58 -080071
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);
Arman Uguray4cebc7e2015-08-20 11:38:49 -070099 };
100
Arman Uguray0a0d3932015-11-19 15:57:57 -0800101 // 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;
Arman Uguray2117e522015-08-14 17:23:47 -0700106
Arman Uguray4cebc7e2015-08-20 11:38:49 -0700107 // Add or remove an observer.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800108 virtual void AddObserver(Observer* observer) = 0;
109 virtual void RemoveObserver(Observer* observer) = 0;
Arman Uguray4cebc7e2015-08-20 11:38:49 -0700110
Arman Uguray2117e522015-08-14 17:23:47 -0700111 // Returns the current Adapter state.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800112 virtual AdapterState GetState() const = 0;
Arman Uguray4fdadb62015-08-13 16:09:35 -0700113
114 // Returns true, if the adapter radio is current powered.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800115 virtual bool IsEnabled() const = 0;
Arman Uguray4fdadb62015-08-13 16:09:35 -0700116
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.
Martin Brabham1a88ace2019-05-10 12:42:15 -0700122 virtual bool Enable() = 0;
Arman Uguray4fdadb62015-08-13 16:09:35 -0700123
124 // Powers off the Bluetooth radio. Returns true, if the disable request was
125 // successfully sent to the Bluetooth controller.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800126 virtual bool Disable() = 0;
Arman Uguray4fdadb62015-08-13 16:09:35 -0700127
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700128 // Returns the name currently assigned to the local adapter.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800129 virtual std::string GetName() const = 0;
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700130
Arman Uguray4fdadb62015-08-13 16:09:35 -0700131 // Sets the name assigned to the local Bluetooth adapter. This is the name
132 // that the local controller will present to remote devices.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800133 virtual bool SetName(const std::string& name) = 0;
Arman Uguray4fdadb62015-08-13 16:09:35 -0700134
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700135 // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
Arman Uguray0a0d3932015-11-19 15:57:57 -0800136 virtual std::string GetAddress() const = 0;
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700137
Bailey Forrest62aa15f2017-01-30 17:38:58 -0800138 // 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
Arman Uguray10b54c42015-08-21 14:59:57 -0700151 // Returns true if the local adapter supports the Low-Energy
152 // multi-advertisement feature.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800153 virtual bool IsMultiAdvertisementSupported() = 0;
Arman Uguray10b54c42015-08-21 14:59:57 -0700154
Arman Uguray0f29c002015-11-13 15:05:48 -0800155 // 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.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800158 virtual bool IsDeviceConnected(const std::string& device_address) = 0;
Arman Uguray0f29c002015-11-13 15:05:48 -0800159
Arman Uguray6791e9a2015-11-17 13:29:55 -0800160 // Returns the total number of trackable advertisements as supported by the
161 // underlying hardware.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800162 virtual int GetTotalNumberOfTrackableAdvertisements() = 0;
Arman Uguray6791e9a2015-11-17 13:29:55 -0800163
164 // Returns true if hardware-backed scan filtering is supported.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800165 virtual bool IsOffloadedFilteringSupported() = 0;
Arman Uguray6791e9a2015-11-17 13:29:55 -0800166
167 // Returns true if hardware-backed batch scanning is supported.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800168 virtual bool IsOffloadedScanBatchingSupported() = 0;
Arman Uguray6791e9a2015-11-17 13:29:55 -0800169
Bailey Forrest62aa15f2017-01-30 17:38:58 -0800170 // 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
Bailey Forrest780e29c2018-08-21 17:20:29 -0700185 // 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
Bailey Forrest62aa15f2017-01-30 17:38:58 -0800190 // 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
Bailey Forrest780e29c2018-08-21 17:20:29 -0700195 // 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
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700200 // Returns a pointer to the LowEnergyClientFactory. This can be used to
Arman Uguray0f2d4892015-09-22 14:20:42 -0700201 // register per-application LowEnergyClient instances to perform BLE GAP
202 // operations.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800203 virtual LowEnergyClientFactory* GetLowEnergyClientFactory() const = 0;
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700204
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700205 // 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
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700209 // Returns a pointer to the LowEnergyAdvertiserFactory. This can be used to
Myles Watson911d1ae2016-11-28 16:44:40 -0800210 // register per-application LowEnergyAdvertiser instances to perform
211 // advertising.
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700212 virtual LowEnergyAdvertiserFactory* GetLeAdvertiserFactory() const = 0;
213
Arman Uguray50a31542015-11-10 18:03:36 -0800214 // Returns a pointer to the GattClientFactory. This can be used to register
215 // per-application GATT server instances.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800216 virtual GattClientFactory* GetGattClientFactory() const = 0;
Arman Uguray50a31542015-11-10 18:03:36 -0800217
Arman Uguray0f2d4892015-09-22 14:20:42 -0700218 // Returns a pointer to the GattServerFactory. This can be used to register
219 // per-application GATT server instances.
Arman Uguray0a0d3932015-11-19 15:57:57 -0800220 virtual GattServerFactory* GetGattServerFactory() const = 0;
221
222 protected:
223 Adapter() = default;
Arman Uguray0f2d4892015-09-22 14:20:42 -0700224
Arman Uguray4fdadb62015-08-13 16:09:35 -0700225 private:
Arman Uguray4fdadb62015-08-13 16:09:35 -0700226 DISALLOW_COPY_AND_ASSIGN(Adapter);
227};
228
229} // namespace bluetooth