bluetooth_adapter_client.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef DEVICE_BLUETOOTH_DBUS_BLUETOOTH_ADAPTER_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_ADAPTER_CLIENT_H_
  6. #include <cstdint>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "dbus/object_path.h"
  12. #include "dbus/property.h"
  13. #include "device/bluetooth/bluetooth_export.h"
  14. #include "device/bluetooth/dbus/bluez_dbus_client.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace dbus {
  17. class ObjectProxy;
  18. }
  19. namespace bluez {
  20. class BluetoothServiceRecordBlueZ;
  21. // BluetoothAdapterClient is used to communicate with objects representing
  22. // local Bluetooth Adapters.
  23. class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterClient : public BluezDBusClient {
  24. public:
  25. enum AddressType {
  26. kPublic,
  27. kRandom,
  28. };
  29. // A DiscoveryFilter represents a filter passed to the SetDiscoveryFilter
  30. // method.
  31. struct DiscoveryFilter {
  32. DiscoveryFilter();
  33. DiscoveryFilter(const DiscoveryFilter&) = delete;
  34. DiscoveryFilter& operator=(const DiscoveryFilter&) = delete;
  35. ~DiscoveryFilter();
  36. // Copy content of |filter| into this filter
  37. void CopyFrom(const DiscoveryFilter& filter);
  38. std::unique_ptr<std::vector<std::string>> uuids;
  39. std::unique_ptr<int16_t> rssi;
  40. std::unique_ptr<uint16_t> pathloss;
  41. std::unique_ptr<std::string> transport;
  42. };
  43. // Represent an error sent through DBus.
  44. struct Error {
  45. Error(const std::string& name, const std::string& message);
  46. std::string name;
  47. std::string message;
  48. };
  49. // Structure of properties associated with bluetooth adapters.
  50. struct Properties : public dbus::PropertySet {
  51. // The Bluetooth device address of the adapter. Read-only.
  52. dbus::Property<std::string> address;
  53. // The Bluetooth system name, generally derived from the hostname.
  54. dbus::Property<std::string> name;
  55. // The Bluetooth friendly name of the adapter, unlike remote devices,
  56. // this property can be changed to change the presentation for when
  57. // the adapter is discoverable.
  58. dbus::Property<std::string> alias;
  59. // The Bluetooth class of the adapter device. Read-only.
  60. dbus::Property<uint32_t> bluetooth_class;
  61. // Whether the adapter radio is powered.
  62. dbus::Property<bool> powered;
  63. // Whether the adapter is discoverable by other Bluetooth devices.
  64. // |discovering_timeout| is used to automatically disable after a time
  65. // period.
  66. dbus::Property<bool> discoverable;
  67. // Whether the adapter accepts incoming pairing requests from other
  68. // Bluetooth devices. |pairable_timeout| is used to automatically disable
  69. // after a time period.
  70. dbus::Property<bool> pairable;
  71. // The timeout in seconds to cease accepting incoming pairing requests
  72. // after |pairable| is set to true. Zero means adapter remains pairable
  73. // forever.
  74. dbus::Property<uint32_t> pairable_timeout;
  75. // The timeout in seconds to cease the adapter being discoverable by
  76. // other Bluetooth devices after |discoverable| is set to true. Zero
  77. // means adapter remains discoverable forever.
  78. dbus::Property<uint32_t> discoverable_timeout;
  79. // Indicates that the adapter is discovering other Bluetooth Devices.
  80. // Read-only. Use StartDiscovery() to begin discovery.
  81. dbus::Property<bool> discovering;
  82. // List of 128-bit UUIDs that represent the available local services.
  83. // Read-only.
  84. dbus::Property<std::vector<std::string>> uuids;
  85. // Local Device ID information in Linux kernel modalias format. Read-only.
  86. dbus::Property<std::string> modalias;
  87. Properties(dbus::ObjectProxy* object_proxy,
  88. const std::string& interface_name,
  89. const PropertyChangedCallback& callback);
  90. ~Properties() override;
  91. };
  92. // Interface for observing changes from a local bluetooth adapter.
  93. class Observer {
  94. public:
  95. virtual ~Observer() {}
  96. // Called when the adapter with object path |object_path| is added to the
  97. // system.
  98. virtual void AdapterAdded(const dbus::ObjectPath& object_path) {}
  99. // Called when the adapter with object path |object_path| is removed from
  100. // the system.
  101. virtual void AdapterRemoved(const dbus::ObjectPath& object_path) {}
  102. // Called when the adapter with object path |object_path| has a
  103. // change in value of the property named |property_name|.
  104. virtual void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
  105. const std::string& property_name) {}
  106. };
  107. BluetoothAdapterClient(const BluetoothAdapterClient&) = delete;
  108. BluetoothAdapterClient& operator=(const BluetoothAdapterClient&) = delete;
  109. ~BluetoothAdapterClient() override;
  110. // Adds and removes observers for events on all local bluetooth
  111. // adapters. Check the |object_path| parameter of observer methods to
  112. // determine which adapter is issuing the event.
  113. virtual void AddObserver(Observer* observer) = 0;
  114. virtual void RemoveObserver(Observer* observer) = 0;
  115. // Returns the list of adapter object paths known to the system.
  116. virtual std::vector<dbus::ObjectPath> GetAdapters() = 0;
  117. // Obtain the properties for the adapter with object path |object_path|,
  118. // any values should be copied if needed.
  119. virtual Properties* GetProperties(const dbus::ObjectPath& object_path) = 0;
  120. // Callback used to send back the handle of a created service record.
  121. using ServiceRecordCallback = base::OnceCallback<void(uint32_t)>;
  122. // Callback used to send back the device resulting from ConnectDevice().
  123. using ConnectDeviceCallback =
  124. base::OnceCallback<void(const dbus::ObjectPath& device_path)>;
  125. // The ErrorCallback is used by adapter methods to indicate failure.
  126. // It receives two arguments: the name of the error in |error_name| and
  127. // an optional message in |error_message|.
  128. using ErrorCallback =
  129. base::OnceCallback<void(const std::string& error_name,
  130. const std::string& error_message)>;
  131. // Callback used by adapter methods to indicate that a response was
  132. // received with an optional Error in case an error occurred.
  133. using ResponseCallback =
  134. base::OnceCallback<void(const absl::optional<Error>&)>;
  135. // Starts a device discovery on the adapter with object path |object_path|.
  136. virtual void StartDiscovery(const dbus::ObjectPath& object_path,
  137. ResponseCallback callback) = 0;
  138. // DEPRECATED: Use StartDiscovery() above.
  139. void StartDiscovery(const dbus::ObjectPath& object_path,
  140. base::OnceClosure callback,
  141. ErrorCallback error_callback);
  142. // Cancels any previous device discovery on the adapter with object path
  143. // |object_path|.
  144. virtual void StopDiscovery(const dbus::ObjectPath& object_path,
  145. ResponseCallback callback) = 0;
  146. // DEPRECATED: Use StopDiscovery() above.
  147. void StopDiscovery(const dbus::ObjectPath& object_path,
  148. base::OnceClosure callback,
  149. ErrorCallback error_callback);
  150. // Removes from the adapter with object path |object_path| the remote
  151. // device with object path |object_path| from the list of known devices
  152. // and discards any pairing information.
  153. virtual void RemoveDevice(const dbus::ObjectPath& object_path,
  154. const dbus::ObjectPath& device_path,
  155. base::OnceClosure callback,
  156. ErrorCallback error_callback) = 0;
  157. // Sets the device discovery filter on the adapter with object path
  158. // |object_path|. When this method is called with no filter parameter, filter
  159. // is removed.
  160. // SetDiscoveryFilter can be called before StartDiscovery. It is useful when
  161. // client will create first discovery session, to ensure that proper scan
  162. // will be started right after call to StartDiscovery.
  163. virtual void SetDiscoveryFilter(const dbus::ObjectPath& object_path,
  164. const DiscoveryFilter& discovery_filter,
  165. base::OnceClosure callback,
  166. ErrorCallback error_callback) = 0;
  167. // Creates the service record |record| on the adapter with the object path
  168. // |object_path|.
  169. virtual void CreateServiceRecord(const dbus::ObjectPath& object_path,
  170. const BluetoothServiceRecordBlueZ& record,
  171. ServiceRecordCallback callback,
  172. ErrorCallback error_callback) = 0;
  173. // Removes the service record with the uuid |uuid| on the adapter with the
  174. // object path |object_path|.
  175. virtual void RemoveServiceRecord(const dbus::ObjectPath& object_path,
  176. uint32_t handle,
  177. base::OnceClosure callback,
  178. ErrorCallback error_callback) = 0;
  179. // Connects to specified device, even if the device has not been discovered,
  180. // on the adapter with the object path |object_path|. Not providing an
  181. // |address_type| will create a BR/EDR device.
  182. virtual void ConnectDevice(const dbus::ObjectPath& object_path,
  183. const std::string& address,
  184. const absl::optional<AddressType>& address_type,
  185. ConnectDeviceCallback callback,
  186. ErrorCallback error_callback) = 0;
  187. // Creates the instance.
  188. static BluetoothAdapterClient* Create();
  189. // Constants used to indicate exceptional error conditions.
  190. static const char kNoResponseError[];
  191. static const char kUnknownAdapterError[];
  192. protected:
  193. BluetoothAdapterClient();
  194. };
  195. } // namespace bluez
  196. #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_ADAPTER_CLIENT_H_