adapter.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #pragma once
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include <base/macros.h>
  21. #include "service/common/bluetooth/adapter_state.h"
  22. #include "service/common/bluetooth/remote_device_props.h"
  23. namespace bluetooth {
  24. class A2dpSinkFactory;
  25. class A2dpSourceFactory;
  26. class AvrcpControlFactory;
  27. class AvrcpTargetFactory;
  28. class GattClientFactory;
  29. class GattServerFactory;
  30. class LowEnergyAdvertiserFactory;
  31. class LowEnergyScannerFactory;
  32. class LowEnergyClientFactory;
  33. // Represents the local Bluetooth adapter.
  34. class Adapter {
  35. public:
  36. // The default values returned before the Adapter is fully initialized and
  37. // powered. The complete values for these fields are obtained following a
  38. // successful call to "Enable".
  39. static const char kDefaultAddress[];
  40. static const char kDefaultName[];
  41. // Observer interface allows other classes to receive notifications from us.
  42. // All of the methods in this interface are declared as optional to allow
  43. // different layers to process only those events that they are interested in.
  44. //
  45. // All methods take in an |adapter| argument which points to the Adapter
  46. // object that the Observer instance was added to.
  47. class Observer {
  48. public:
  49. virtual ~Observer() = default;
  50. // Called when there is a change in the state of the local Bluetooth
  51. // |adapter| from |prev_state| to |new_state|.
  52. virtual void OnAdapterStateChanged(Adapter* adapter,
  53. AdapterState prev_state,
  54. AdapterState new_state);
  55. // Called when there is a change in the connection state between the local
  56. // |adapter| and a remote device with address |device_address|. If the ACL
  57. // state changes from disconnected to connected, then |connected| will be
  58. // true and vice versa.
  59. virtual void OnDeviceConnectionStateChanged(
  60. Adapter* adapter, const std::string& device_address, bool connected);
  61. // Called when scanning is enabled or disabled.
  62. virtual void OnScanEnableChanged(Adapter* adapter, bool scan_enabled);
  63. // Called when a SSP pairing request comes from a remote device.
  64. virtual void OnSspRequest(Adapter* adapter,
  65. const std::string& device_address,
  66. const std::string& device_name, int cod,
  67. int pairing_variant, int pass_key);
  68. // Called when a remote device bond state changes.
  69. virtual void OnBondStateChanged(Adapter* adapter, int status,
  70. const std::string& device_address,
  71. int state);
  72. // Called in response to |GetBondedDevices|.
  73. virtual void OnGetBondedDevices(
  74. Adapter* adapter, int status,
  75. const std::vector<std::string>& bonded_devices);
  76. // Called in response to |GetRemoteDeviceProperties|.
  77. virtual void OnGetRemoteDeviceProperties(Adapter* adapter, int status,
  78. const std::string& device_address,
  79. const RemoteDeviceProps& props);
  80. // Called when a device is found through scanning.
  81. virtual void OnDeviceFound(Adapter* adapter,
  82. const RemoteDeviceProps& props);
  83. };
  84. // Returns an Adapter implementation to be used in production. Don't use these
  85. // in tests; use MockAdapter instead.
  86. static std::unique_ptr<Adapter> Create();
  87. virtual ~Adapter() = default;
  88. // Add or remove an observer.
  89. virtual void AddObserver(Observer* observer) = 0;
  90. virtual void RemoveObserver(Observer* observer) = 0;
  91. // Returns the current Adapter state.
  92. virtual AdapterState GetState() const = 0;
  93. // Returns true, if the adapter radio is current powered.
  94. virtual bool IsEnabled() const = 0;
  95. // Enables Bluetooth. This method will send a request to the Bluetooth adapter
  96. // to power up its radio. Returns true, if the request was successfully sent
  97. // to the controller, otherwise returns false. A successful call to this
  98. // method only means that the enable request has been sent to the Bluetooth
  99. // controller and does not imply that the operation itself succeeded.
  100. virtual bool Enable() = 0;
  101. // Powers off the Bluetooth radio. Returns true, if the disable request was
  102. // successfully sent to the Bluetooth controller.
  103. virtual bool Disable() = 0;
  104. // Returns the name currently assigned to the local adapter.
  105. virtual std::string GetName() const = 0;
  106. // Sets the name assigned to the local Bluetooth adapter. This is the name
  107. // that the local controller will present to remote devices.
  108. virtual bool SetName(const std::string& name) = 0;
  109. // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
  110. virtual std::string GetAddress() const = 0;
  111. // Set discoverability mode.
  112. virtual bool SetScanMode(int scan_mode) = 0;
  113. // Enable or disable discoverability.
  114. virtual bool SetScanEnable(bool scan_enable) = 0;
  115. // Reply to an SSP request received in |OnSspRequest|.
  116. virtual bool SspReply(const std::string& device_address, int variant,
  117. bool accept, int32_t pass_key) = 0;
  118. // Create a bond with device specified by |device_address|.
  119. virtual bool CreateBond(const std::string& device_address, int transport) = 0;
  120. // Returns true if the local adapter supports the Low-Energy
  121. // multi-advertisement feature.
  122. virtual bool IsMultiAdvertisementSupported() = 0;
  123. // Returns true if the remote device with address |device_address| is
  124. // currently connected. This is not a const method as it modifies the state of
  125. // the associated internal mutex.
  126. virtual bool IsDeviceConnected(const std::string& device_address) = 0;
  127. // Returns the total number of trackable advertisements as supported by the
  128. // underlying hardware.
  129. virtual int GetTotalNumberOfTrackableAdvertisements() = 0;
  130. // Returns true if hardware-backed scan filtering is supported.
  131. virtual bool IsOffloadedFilteringSupported() = 0;
  132. // Returns true if hardware-backed batch scanning is supported.
  133. virtual bool IsOffloadedScanBatchingSupported() = 0;
  134. // When the stack call completes, |OnGetBondedDevices| will be called.
  135. virtual bool GetBondedDevices() = 0;
  136. // When the stack call completets, |OnBondStateChanged| will be called.
  137. virtual bool RemoveBond(const std::string& device_address) = 0;
  138. // When the stack call completets, |OnGetRemoteDeviceProperties| will be
  139. // called.
  140. virtual bool GetRemoteDeviceProperties(const std::string& device_address) = 0;
  141. // Returns a pointer to the A2dpSinkFactory. This can be used to
  142. // register per-application A2dpSinkClient instances to perform A2DP sink
  143. // operations.
  144. virtual A2dpSinkFactory* GetA2dpSinkFactory() const = 0;
  145. // Returns a pointer to the A2dpSourceFactory. This can be used to
  146. // register per-application A2dpSourceClient instances to perform A2DP source
  147. // operations.
  148. virtual A2dpSourceFactory* GetA2dpSourceFactory() const = 0;
  149. // Returns a pointer to the AvrcpControlFactory. This can be used to register
  150. // per-application AvrcpControlClient instances to perform AVRCP control
  151. // operations.
  152. virtual AvrcpControlFactory* GetAvrcpControlFactory() const = 0;
  153. // Returns a pointer to the AvrcpTargetFactory. This can be used to register
  154. // per-application AvrcpTargetClient instances to perform AVRCP target
  155. // operations.
  156. virtual AvrcpTargetFactory* GetAvrcpTargetFactory() const = 0;
  157. // Returns a pointer to the LowEnergyClientFactory. This can be used to
  158. // register per-application LowEnergyClient instances to perform BLE GAP
  159. // operations.
  160. virtual LowEnergyClientFactory* GetLowEnergyClientFactory() const = 0;
  161. // Returns a pointer to the LowEnergyScannerFactory. This can be used to
  162. // register per-application LowEnergyScanner instances to perform scanning.
  163. virtual LowEnergyScannerFactory* GetLeScannerFactory() const = 0;
  164. // Returns a pointer to the LowEnergyAdvertiserFactory. This can be used to
  165. // register per-application LowEnergyAdvertiser instances to perform
  166. // advertising.
  167. virtual LowEnergyAdvertiserFactory* GetLeAdvertiserFactory() const = 0;
  168. // Returns a pointer to the GattClientFactory. This can be used to register
  169. // per-application GATT server instances.
  170. virtual GattClientFactory* GetGattClientFactory() const = 0;
  171. // Returns a pointer to the GattServerFactory. This can be used to register
  172. // per-application GATT server instances.
  173. virtual GattServerFactory* GetGattServerFactory() const = 0;
  174. protected:
  175. Adapter() = default;
  176. private:
  177. DISALLOW_COPY_AND_ASSIGN(Adapter);
  178. };
  179. } // namespace bluetooth