fake_bluetooth_device_client.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // Copyright (c) 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_FAKE_BLUETOOTH_DEVICE_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_
  6. #include <cstdint>
  7. #include <map>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/bind.h"
  11. #include "base/callback.h"
  12. #include "base/observer_list.h"
  13. #include "dbus/object_path.h"
  14. #include "dbus/property.h"
  15. #include "device/bluetooth/bluetooth_common.h"
  16. #include "device/bluetooth/bluetooth_export.h"
  17. #include "device/bluetooth/dbus/bluetooth_agent_service_provider.h"
  18. #include "device/bluetooth/dbus/bluetooth_device_client.h"
  19. #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h"
  20. #include "third_party/abseil-cpp/absl/types/optional.h"
  21. namespace bluez {
  22. // FakeBluetoothDeviceClient simulates the behavior of the Bluetooth Daemon
  23. // device objects and is used both in test cases in place of a mock and on
  24. // the Linux desktop.
  25. class DEVICE_BLUETOOTH_EXPORT FakeBluetoothDeviceClient
  26. : public BluetoothDeviceClient {
  27. public:
  28. struct Properties : public BluetoothDeviceClient::Properties {
  29. explicit Properties(const PropertyChangedCallback& callback);
  30. ~Properties() override;
  31. // dbus::PropertySet override
  32. void Get(dbus::PropertyBase* property,
  33. dbus::PropertySet::GetCallback callback) override;
  34. void GetAll() override;
  35. void Set(dbus::PropertyBase* property,
  36. dbus::PropertySet::SetCallback callback) override;
  37. };
  38. struct SimulatedPairingOptions {
  39. SimulatedPairingOptions();
  40. ~SimulatedPairingOptions();
  41. bool incoming = false;
  42. std::string pairing_method;
  43. std::string pairing_auth_token;
  44. std::string pairing_action;
  45. };
  46. // Stores properties of a device that is about to be created.
  47. struct IncomingDeviceProperties {
  48. IncomingDeviceProperties();
  49. ~IncomingDeviceProperties();
  50. std::string device_address;
  51. std::string device_alias;
  52. int device_class = 0;
  53. std::string device_name;
  54. std::string device_path;
  55. bool is_trusted = true;
  56. bool incoming = false;
  57. std::string pairing_action;
  58. std::string pairing_auth_token;
  59. std::string pairing_method;
  60. };
  61. FakeBluetoothDeviceClient();
  62. ~FakeBluetoothDeviceClient() override;
  63. // Causes Connect() calls to never finish.
  64. void LeaveConnectionsPending();
  65. // BluetoothDeviceClient overrides
  66. void Init(dbus::Bus* bus, const std::string& bluetooth_service_name) override;
  67. void AddObserver(Observer* observer) override;
  68. void RemoveObserver(Observer* observer) override;
  69. std::vector<dbus::ObjectPath> GetDevicesForAdapter(
  70. const dbus::ObjectPath& adapter_path) override;
  71. Properties* GetProperties(const dbus::ObjectPath& object_path) override;
  72. void Connect(const dbus::ObjectPath& object_path,
  73. base::OnceClosure callback,
  74. ErrorCallback error_callback) override;
  75. void ConnectClassic(const dbus::ObjectPath& object_path,
  76. base::OnceClosure callback,
  77. ErrorCallback error_callback) override;
  78. void ConnectLE(const dbus::ObjectPath& object_path,
  79. base::OnceClosure callback,
  80. ErrorCallback error_callback) override;
  81. void Disconnect(const dbus::ObjectPath& object_path,
  82. base::OnceClosure callback,
  83. ErrorCallback error_callback) override;
  84. void DisconnectLE(const dbus::ObjectPath& object_path,
  85. base::OnceClosure callback,
  86. ErrorCallback error_callback) override;
  87. void ConnectProfile(const dbus::ObjectPath& object_path,
  88. const std::string& uuid,
  89. base::OnceClosure callback,
  90. ErrorCallback error_callback) override;
  91. void DisconnectProfile(const dbus::ObjectPath& object_path,
  92. const std::string& uuid,
  93. base::OnceClosure callback,
  94. ErrorCallback error_callback) override;
  95. void Pair(const dbus::ObjectPath& object_path,
  96. base::OnceClosure callback,
  97. ErrorCallback error_callback) override;
  98. void CancelPairing(const dbus::ObjectPath& object_path,
  99. base::OnceClosure callback,
  100. ErrorCallback error_callback) override;
  101. void GetConnInfo(const dbus::ObjectPath& object_path,
  102. ConnInfoCallback callback,
  103. ErrorCallback error_callback) override;
  104. void SetLEConnectionParameters(const dbus::ObjectPath& object_path,
  105. const ConnectionParameters& conn_params,
  106. base::OnceClosure callback,
  107. ErrorCallback error_callback) override;
  108. void GetServiceRecords(const dbus::ObjectPath& object_path,
  109. ServiceRecordsCallback callback,
  110. ErrorCallback error_callback) override;
  111. void ExecuteWrite(const dbus::ObjectPath& object_path,
  112. base::OnceClosure callback,
  113. ErrorCallback error_callback) override;
  114. void AbortWrite(const dbus::ObjectPath& object_path,
  115. base::OnceClosure callback,
  116. ErrorCallback error_callback) override;
  117. void SetSimulationIntervalMs(int interval_ms);
  118. // Simulates discovery of devices for the given adapter.
  119. void BeginDiscoverySimulation(const dbus::ObjectPath& adapter_path);
  120. void EndDiscoverySimulation(const dbus::ObjectPath& adapter_path);
  121. // Simulates incoming pairing of devices for the given adapter.
  122. void BeginIncomingPairingSimulation(const dbus::ObjectPath& adapter_path);
  123. void EndIncomingPairingSimulation(const dbus::ObjectPath& adapter_path);
  124. // Creates a device from the set we return for the given adapter.
  125. void CreateDevice(const dbus::ObjectPath& adapter_path,
  126. const dbus::ObjectPath& device_path);
  127. // Creates a device with the given properties.
  128. void CreateDeviceWithProperties(const dbus::ObjectPath& adapter_path,
  129. const IncomingDeviceProperties& props);
  130. // Creates and returns a list of dictionary objects as a Value, which contain
  131. // all the data from the constants for devices with predefined behavior.
  132. base::Value GetBluetoothDevicesAsDictionaries() const;
  133. SimulatedPairingOptions* GetPairingOptions(
  134. const dbus::ObjectPath& object_path);
  135. // Removes a device from the set we return for the given adapter.
  136. void RemoveDevice(const dbus::ObjectPath& adapter_path,
  137. const dbus::ObjectPath& device_path);
  138. // Simulates a pairing for the device with the given D-Bus object path,
  139. // |object_path|. Set |incoming_request| to true if simulating an incoming
  140. // pairing request, false for an outgoing one. On successful completion
  141. // |callback| will be called, on failure, |error_callback| is called.
  142. void SimulatePairing(const dbus::ObjectPath& object_path,
  143. bool incoming_request,
  144. base::OnceClosure callback,
  145. ErrorCallback error_callback);
  146. // Updates the connection properties of the fake device that will be returned
  147. // by GetConnInfo.
  148. void UpdateConnectionInfo(uint16_t connection_rssi,
  149. uint16_t transmit_power,
  150. uint16_t max_transmit_power);
  151. // Test specific functions:
  152. // Remove all test devices from this client.
  153. void RemoveAllDevices();
  154. // Create a test Bluetooth device with the given properties.
  155. void CreateTestDevice(
  156. const dbus::ObjectPath& adapter_path,
  157. const absl::optional<std::string> name,
  158. const std::string alias,
  159. const std::string device_address,
  160. const std::vector<std::string>& service_uuids,
  161. device::BluetoothTransport type,
  162. const std::map<std::string, std::vector<uint8_t>>& service_data,
  163. const std::map<uint16_t, std::vector<uint8_t>>& manufacturer_data);
  164. void set_delay_start_discovery(bool value) { delay_start_discovery_ = value; }
  165. // Updates the inquiry RSSI property of fake device with object path
  166. // |object_path| to |rssi|, if the fake device exists.
  167. void UpdateDeviceRSSI(const dbus::ObjectPath& object_path, int16_t rssi);
  168. // Updates service UUIDs, service data, and manufacturer data of the fake
  169. // device with the given |object_path|. The |service_uuids| values are
  170. // appended, while |service_data| and |manufacturer_data| are merged into the
  171. // existing property values. Has no effect if the fake device does not exist.
  172. void UpdateServiceAndManufacturerData(
  173. const dbus::ObjectPath& object_path,
  174. const std::vector<std::string>& service_uuids,
  175. const std::map<std::string, std::vector<uint8_t>>& service_data,
  176. const std::map<uint16_t, std::vector<uint8_t>>& manufacturer_data);
  177. // Updates the EIR property of fake device with object path |object_path| to
  178. // |eir|, if the fake device exists.
  179. void UpdateEIR(const dbus::ObjectPath& object_path,
  180. const std::vector<uint8_t>& eir);
  181. // Adds a pending prepare write request to |object_path|.
  182. void AddPrepareWriteRequest(const dbus::ObjectPath& object_path,
  183. const std::vector<uint8_t>& value);
  184. static const char kTestPinCode[];
  185. static const int kTestPassKey;
  186. static const char kPairingMethodNone[];
  187. static const char kPairingMethodPinCode[];
  188. static const char kPairingMethodPassKey[];
  189. static const char kPairingActionConfirmation[];
  190. static const char kPairingActionDisplay[];
  191. static const char kPairingActionFail[];
  192. static const char kPairingActionRequest[];
  193. // Object paths, names, addresses and bluetooth classes of the devices
  194. // we can emulate.
  195. static const char kPairedDevicePath[];
  196. static const char kPairedDeviceName[];
  197. static const char kPairedDeviceAlias[];
  198. static const char kPairedDeviceAddress[];
  199. static const uint32_t kPairedDeviceClass;
  200. static const char kLegacyAutopairPath[];
  201. static const char kLegacyAutopairName[];
  202. static const char kLegacyAutopairAddress[];
  203. static const uint32_t kLegacyAutopairClass;
  204. static const char kDisplayPinCodePath[];
  205. static const char kDisplayPinCodeName[];
  206. static const char kDisplayPinCodeAddress[];
  207. static const uint32_t kDisplayPinCodeClass;
  208. static const char kVanishingDevicePath[];
  209. static const char kVanishingDeviceName[];
  210. static const char kVanishingDeviceAddress[];
  211. static const uint32_t kVanishingDeviceClass;
  212. static const char kConnectUnpairablePath[];
  213. static const char kConnectUnpairableName[];
  214. static const char kConnectUnpairableAddress[];
  215. static const uint32_t kConnectUnpairableClass;
  216. static const char kDisplayPasskeyPath[];
  217. static const char kDisplayPasskeyName[];
  218. static const char kDisplayPasskeyAddress[];
  219. static const uint32_t kDisplayPasskeyClass;
  220. static const char kRequestPinCodePath[];
  221. static const char kRequestPinCodeName[];
  222. static const char kRequestPinCodeAddress[];
  223. static const uint32_t kRequestPinCodeClass;
  224. static const char kConfirmPasskeyPath[];
  225. static const char kConfirmPasskeyName[];
  226. static const char kConfirmPasskeyAddress[];
  227. static const uint32_t kConfirmPasskeyClass;
  228. static const char kRequestPasskeyPath[];
  229. static const char kRequestPasskeyName[];
  230. static const char kRequestPasskeyAddress[];
  231. static const uint32_t kRequestPasskeyClass;
  232. static const char kUnconnectableDevicePath[];
  233. static const char kUnconnectableDeviceName[];
  234. static const char kUnconnectableDeviceAddress[];
  235. static const uint32_t kUnconnectableDeviceClass;
  236. static const char kUnpairableDevicePath[];
  237. static const char kUnpairableDeviceName[];
  238. static const char kUnpairableDeviceAddress[];
  239. static const uint32_t kUnpairableDeviceClass;
  240. static const char kJustWorksPath[];
  241. static const char kJustWorksName[];
  242. static const char kJustWorksAddress[];
  243. static const uint32_t kJustWorksClass;
  244. static const char kLowEnergyPath[];
  245. static const char kLowEnergyName[];
  246. static const char kLowEnergyAddress[];
  247. static const uint32_t kLowEnergyClass;
  248. static const char kDualPath[];
  249. static const char kDualName[];
  250. static const char kDualAddress[];
  251. static const char kPairedUnconnectableDevicePath[];
  252. static const char kPairedUnconnectableDeviceName[];
  253. static const char kPairedUnconnectableDeviceAlias[];
  254. static const char kPairedUnconnectableDeviceAddress[];
  255. static const uint32_t kPairedUnconnectableDeviceClass;
  256. static const char kConnectedTrustedNotPairedDevicePath[];
  257. static const char kConnectedTrustedNotPairedDeviceAddress[];
  258. static const char kConnectedTrustedNotPairedDeviceName[];
  259. static const uint32_t kConnectedTrustedNotPairedDeviceClass;
  260. private:
  261. // Property callback passed when we create Properties* structures.
  262. void OnPropertyChanged(const dbus::ObjectPath& object_path,
  263. const std::string& property_name);
  264. void DiscoverySimulationTimer();
  265. void IncomingPairingSimulationTimer();
  266. void CompleteSimulatedPairing(const dbus::ObjectPath& object_path,
  267. base::OnceClosure callback,
  268. ErrorCallback error_callback);
  269. void TimeoutSimulatedPairing(const dbus::ObjectPath& object_path,
  270. ErrorCallback error_callback);
  271. void CancelSimulatedPairing(const dbus::ObjectPath& object_path,
  272. ErrorCallback error_callback);
  273. void RejectSimulatedPairing(const dbus::ObjectPath& object_path,
  274. ErrorCallback error_callback);
  275. void FailSimulatedPairing(const dbus::ObjectPath& object_path,
  276. ErrorCallback error_callback);
  277. void AddInputDeviceIfNeeded(const dbus::ObjectPath& object_path,
  278. Properties* properties);
  279. // If fake device with |object_path| exists, sets its inquiry RSSI property
  280. // to false and notifies that the property changed.
  281. void InvalidateDeviceRSSI(const dbus::ObjectPath& object_path);
  282. void PinCodeCallback(const dbus::ObjectPath& object_path,
  283. base::OnceClosure callback,
  284. ErrorCallback error_callback,
  285. BluetoothAgentServiceProvider::Delegate::Status status,
  286. const std::string& pincode);
  287. void PasskeyCallback(const dbus::ObjectPath& object_path,
  288. base::OnceClosure callback,
  289. ErrorCallback error_callback,
  290. BluetoothAgentServiceProvider::Delegate::Status status,
  291. uint32_t passkey);
  292. void ConfirmationCallback(
  293. const dbus::ObjectPath& object_path,
  294. base::OnceClosure callback,
  295. ErrorCallback error_callback,
  296. BluetoothAgentServiceProvider::Delegate::Status status);
  297. void SimulateKeypress(uint16_t entered,
  298. const dbus::ObjectPath& object_path,
  299. base::OnceClosure callback,
  300. ErrorCallback error_callback);
  301. void ConnectionCallback(
  302. const dbus::ObjectPath& object_path,
  303. base::OnceClosure callback,
  304. ErrorCallback error_callback,
  305. BluetoothProfileServiceProvider::Delegate::Status status);
  306. void DisconnectionCallback(
  307. const dbus::ObjectPath& object_path,
  308. base::OnceClosure callback,
  309. ErrorCallback error_callback,
  310. BluetoothProfileServiceProvider::Delegate::Status status);
  311. // List of observers interested in event notifications from us.
  312. base::ObserverList<Observer>::Unchecked observers_;
  313. using PropertiesMap =
  314. std::map<const dbus::ObjectPath, std::unique_ptr<Properties>>;
  315. PropertiesMap properties_map_;
  316. std::vector<dbus::ObjectPath> device_list_;
  317. // Properties which are used to decied which method of pairing should
  318. // be done on request.
  319. using PairingOptionsMap = std::map<const dbus::ObjectPath,
  320. std::unique_ptr<SimulatedPairingOptions>>;
  321. PairingOptionsMap pairing_options_map_;
  322. int simulation_interval_ms_;
  323. uint32_t discovery_simulation_step_;
  324. uint32_t incoming_pairing_simulation_step_;
  325. bool pairing_cancelled_;
  326. int16_t connection_rssi_;
  327. int16_t transmit_power_;
  328. int16_t max_transmit_power_;
  329. // Controls the fake behavior to allow more extensive UI testing without
  330. // having to cycle the discovery simulation.
  331. bool delay_start_discovery_;
  332. // Pending prepare write requests.
  333. std::vector<std::pair<dbus::ObjectPath, std::vector<uint8_t>>>
  334. prepare_write_requests_;
  335. bool should_leave_connections_pending_;
  336. };
  337. } // namespace bluez
  338. #endif // DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_