Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 1 | // |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 2 | // Copyright (C) 2015 Google, Inc. |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 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 | |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 19 | #include <memory> |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 22 | |
| 23 | #include <base/macros.h> |
| 24 | |
Arman Uguray | 234138e | 2015-10-06 15:56:36 -0700 | [diff] [blame] | 25 | #include "service/common/bluetooth/adapter_state.h" |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 26 | #include "service/common/bluetooth/remote_device_props.h" |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 27 | |
| 28 | namespace bluetooth { |
| 29 | |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 30 | class A2dpSinkFactory; |
Bailey Forrest | 780e29c | 2018-08-21 17:20:29 -0700 | [diff] [blame] | 31 | class A2dpSourceFactory; |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 32 | class AvrcpControlFactory; |
Bailey Forrest | 780e29c | 2018-08-21 17:20:29 -0700 | [diff] [blame] | 33 | class AvrcpTargetFactory; |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 34 | class GattClientFactory; |
| 35 | class GattServerFactory; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 36 | class LowEnergyAdvertiserFactory; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 37 | class LowEnergyScannerFactory; |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 38 | class LowEnergyClientFactory; |
| 39 | |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 40 | // Represents the local Bluetooth adapter. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 41 | class Adapter { |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 42 | public: |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 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 | |
Arman Uguray | 4cebc7e | 2015-08-20 11:38:49 -0700 | [diff] [blame] | 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. |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 52 | // |
| 53 | // All methods take in an |adapter| argument which points to the Adapter |
| 54 | // object that the Observer instance was added to. |
Arman Uguray | 4cebc7e | 2015-08-20 11:38:49 -0700 | [diff] [blame] | 55 | class Observer { |
| 56 | public: |
| 57 | virtual ~Observer() = default; |
| 58 | |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 59 | // Called when there is a change in the state of the local Bluetooth |
| 60 | // |adapter| from |prev_state| to |new_state|. |
Arman Uguray | 4cebc7e | 2015-08-20 11:38:49 -0700 | [diff] [blame] | 61 | virtual void OnAdapterStateChanged(Adapter* adapter, |
| 62 | AdapterState prev_state, |
| 63 | AdapterState new_state); |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 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); |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 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); |
Arman Uguray | 4cebc7e | 2015-08-20 11:38:49 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 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; |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 106 | |
Arman Uguray | 4cebc7e | 2015-08-20 11:38:49 -0700 | [diff] [blame] | 107 | // Add or remove an observer. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 108 | virtual void AddObserver(Observer* observer) = 0; |
| 109 | virtual void RemoveObserver(Observer* observer) = 0; |
Arman Uguray | 4cebc7e | 2015-08-20 11:38:49 -0700 | [diff] [blame] | 110 | |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 111 | // Returns the current Adapter state. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 112 | virtual AdapterState GetState() const = 0; |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 113 | |
| 114 | // Returns true, if the adapter radio is current powered. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 115 | virtual bool IsEnabled() const = 0; |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 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. |
Martin Brabham | 1a88ace | 2019-05-10 12:42:15 -0700 | [diff] [blame] | 122 | virtual bool Enable() = 0; |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 123 | |
| 124 | // Powers off the Bluetooth radio. Returns true, if the disable request was |
| 125 | // successfully sent to the Bluetooth controller. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 126 | virtual bool Disable() = 0; |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 127 | |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 128 | // Returns the name currently assigned to the local adapter. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 129 | virtual std::string GetName() const = 0; |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 130 | |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 131 | // Sets the name assigned to the local Bluetooth adapter. This is the name |
| 132 | // that the local controller will present to remote devices. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 133 | virtual bool SetName(const std::string& name) = 0; |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 134 | |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 135 | // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX). |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 136 | virtual std::string GetAddress() const = 0; |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 137 | |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 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 | |
Arman Uguray | 10b54c4 | 2015-08-21 14:59:57 -0700 | [diff] [blame] | 151 | // Returns true if the local adapter supports the Low-Energy |
| 152 | // multi-advertisement feature. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 153 | virtual bool IsMultiAdvertisementSupported() = 0; |
Arman Uguray | 10b54c4 | 2015-08-21 14:59:57 -0700 | [diff] [blame] | 154 | |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 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. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 158 | virtual bool IsDeviceConnected(const std::string& device_address) = 0; |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 159 | |
Arman Uguray | 6791e9a | 2015-11-17 13:29:55 -0800 | [diff] [blame] | 160 | // Returns the total number of trackable advertisements as supported by the |
| 161 | // underlying hardware. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 162 | virtual int GetTotalNumberOfTrackableAdvertisements() = 0; |
Arman Uguray | 6791e9a | 2015-11-17 13:29:55 -0800 | [diff] [blame] | 163 | |
| 164 | // Returns true if hardware-backed scan filtering is supported. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 165 | virtual bool IsOffloadedFilteringSupported() = 0; |
Arman Uguray | 6791e9a | 2015-11-17 13:29:55 -0800 | [diff] [blame] | 166 | |
| 167 | // Returns true if hardware-backed batch scanning is supported. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 168 | virtual bool IsOffloadedScanBatchingSupported() = 0; |
Arman Uguray | 6791e9a | 2015-11-17 13:29:55 -0800 | [diff] [blame] | 169 | |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 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 | |
Bailey Forrest | 780e29c | 2018-08-21 17:20:29 -0700 | [diff] [blame] | 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 | |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 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 | |
Bailey Forrest | 780e29c | 2018-08-21 17:20:29 -0700 | [diff] [blame] | 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 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 200 | // Returns a pointer to the LowEnergyClientFactory. This can be used to |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 201 | // register per-application LowEnergyClient instances to perform BLE GAP |
| 202 | // operations. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 203 | virtual LowEnergyClientFactory* GetLowEnergyClientFactory() const = 0; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 204 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 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 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 209 | // Returns a pointer to the LowEnergyAdvertiserFactory. This can be used to |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 210 | // register per-application LowEnergyAdvertiser instances to perform |
| 211 | // advertising. |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 212 | virtual LowEnergyAdvertiserFactory* GetLeAdvertiserFactory() const = 0; |
| 213 | |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 214 | // Returns a pointer to the GattClientFactory. This can be used to register |
| 215 | // per-application GATT server instances. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 216 | virtual GattClientFactory* GetGattClientFactory() const = 0; |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 217 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 218 | // Returns a pointer to the GattServerFactory. This can be used to register |
| 219 | // per-application GATT server instances. |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 220 | virtual GattServerFactory* GetGattServerFactory() const = 0; |
| 221 | |
| 222 | protected: |
| 223 | Adapter() = default; |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 224 | |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 225 | private: |
Arman Uguray | 4fdadb6 | 2015-08-13 16:09:35 -0700 | [diff] [blame] | 226 | DISALLOW_COPY_AND_ASSIGN(Adapter); |
| 227 | }; |
| 228 | |
| 229 | } // namespace bluetooth |