fake_bluetooth_adapter_client.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_ADAPTER_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_ADAPTER_CLIENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  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_export.h"
  16. #include "device/bluetooth/dbus/bluetooth_adapter_client.h"
  17. namespace bluez {
  18. // FakeBluetoothAdapterClient simulates the behavior of the Bluetooth Daemon
  19. // adapter objects and is used both in test cases in place of a mock and on
  20. // the Linux desktop.
  21. class DEVICE_BLUETOOTH_EXPORT FakeBluetoothAdapterClient
  22. : public BluetoothAdapterClient {
  23. public:
  24. struct Properties : public BluetoothAdapterClient::Properties {
  25. explicit Properties(const PropertyChangedCallback& callback);
  26. ~Properties() override;
  27. // dbus::PropertySet override
  28. void Get(dbus::PropertyBase* property,
  29. dbus::PropertySet::GetCallback callback) override;
  30. void GetAll() override;
  31. void Set(dbus::PropertyBase* property,
  32. dbus::PropertySet::SetCallback callback) override;
  33. };
  34. FakeBluetoothAdapterClient();
  35. ~FakeBluetoothAdapterClient() override;
  36. // BluetoothAdapterClient overrides
  37. void Init(dbus::Bus* bus, const std::string& bluetooth_service_name) override;
  38. void AddObserver(Observer* observer) override;
  39. void RemoveObserver(Observer* observer) override;
  40. std::vector<dbus::ObjectPath> GetAdapters() override;
  41. Properties* GetProperties(const dbus::ObjectPath& object_path) override;
  42. void StartDiscovery(const dbus::ObjectPath& object_path,
  43. ResponseCallback callback) override;
  44. void StopDiscovery(const dbus::ObjectPath& object_path,
  45. ResponseCallback callback) override;
  46. void RemoveDevice(const dbus::ObjectPath& object_path,
  47. const dbus::ObjectPath& device_path,
  48. base::OnceClosure callback,
  49. ErrorCallback error_callback) override;
  50. void SetDiscoveryFilter(const dbus::ObjectPath& object_path,
  51. const DiscoveryFilter& discovery_filter,
  52. base::OnceClosure callback,
  53. ErrorCallback error_callback) override;
  54. void CreateServiceRecord(const dbus::ObjectPath& object_path,
  55. const bluez::BluetoothServiceRecordBlueZ& record,
  56. ServiceRecordCallback callback,
  57. ErrorCallback error_callback) override;
  58. void RemoveServiceRecord(const dbus::ObjectPath& object_path,
  59. uint32_t handle,
  60. base::OnceClosure callback,
  61. ErrorCallback error_callback) override;
  62. void ConnectDevice(const dbus::ObjectPath& object_path,
  63. const std::string& address,
  64. const absl::optional<AddressType>& address_type,
  65. ConnectDeviceCallback callback,
  66. ErrorCallback error_callback) override;
  67. // Sets the current simulation timeout interval.
  68. void SetSimulationIntervalMs(int interval_ms);
  69. // Returns current discovery filter in use by this adapter.
  70. DiscoveryFilter* GetDiscoveryFilter();
  71. // Make SetDiscoveryFilter fail when called next time.
  72. void MakeSetDiscoveryFilterFail();
  73. // Make StartDiscovery fail when called next time.
  74. void MakeStartDiscoveryFail();
  75. // Sets whether discovery simulation should be enabled or not.
  76. void SetDiscoverySimulation(bool enabled);
  77. // Mark the adapter and second adapter as present or not.
  78. void SetPresent(bool present);
  79. void SetSecondPresent(bool present);
  80. // Set adapter UUIDs
  81. void SetUUIDs(const std::vector<std::string>& uuids);
  82. void SetSecondUUIDs(const std::vector<std::string>& uuids);
  83. // Set discoverable timeout
  84. void SetDiscoverableTimeout(uint32_t timeout);
  85. // Object path, name and addresses of the adapters we emulate.
  86. static const char kAdapterPath[];
  87. static const char kAdapterName[];
  88. static const char kAdapterAddress[];
  89. static const char kSecondAdapterPath[];
  90. static const char kSecondAdapterName[];
  91. static const char kSecondAdapterAddress[];
  92. private:
  93. // Property callback passed when we create Properties* structures.
  94. void OnPropertyChanged(const std::string& property_name);
  95. // Posts the delayed task represented by |callback| onto the current
  96. // message loop to be executed after |simulation_interval_ms_| milliseconds.
  97. void PostDelayedTask(base::OnceClosure callback);
  98. // Utility function to update the discovering property.
  99. void UpdateDiscoveringProperty(bool discovering);
  100. // List of observers interested in event notifications from us.
  101. base::ObserverList<Observer>::Unchecked observers_;
  102. // Static properties we return.
  103. std::unique_ptr<Properties> properties_;
  104. std::unique_ptr<Properties> second_properties_;
  105. // Whether the adapter and second adapter should be present or not.
  106. bool present_ = true;
  107. bool second_present_ = false;
  108. // Number of times we've been asked to discover.
  109. int discovering_count_ = 0;
  110. // Current discovery filter
  111. std::unique_ptr<DiscoveryFilter> discovery_filter_;
  112. // When set, next call to SetDiscoveryFilter would fail.
  113. bool set_discovery_filter_should_fail_ = false;
  114. // When set, next call to StartDiscovery would fail.
  115. bool set_start_discovery_should_fail_ = false;
  116. // When set, enables discovery simulation.
  117. bool enable_discovery_simulation_ = true;
  118. // Current timeout interval used when posting delayed tasks.
  119. int simulation_interval_ms_;
  120. // Last used handle value issued for a service record.
  121. uint32_t last_handle_ = 0;
  122. // Service records manually registered with this adapter by handle.
  123. std::map<uint32_t, BluetoothServiceRecordBlueZ> records_;
  124. // Note: This should remain the last member so it'll be destroyed and
  125. // invalidate its weak pointers before any other members are destroyed.
  126. base::WeakPtrFactory<FakeBluetoothAdapterClient> weak_ptr_factory_{this};
  127. };
  128. } // namespace bluez
  129. #endif // DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_ADAPTER_CLIENT_H_