fake_bluetooth_gatt_service_client.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2014 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_GATT_SERVICE_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_SERVICE_CLIENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/observer_list.h"
  11. #include "dbus/object_path.h"
  12. #include "dbus/property.h"
  13. #include "device/bluetooth/bluetooth_export.h"
  14. #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h"
  15. namespace bluez {
  16. // FakeBluetoothGattServiceClient simulates the behavior of the Bluetooth Daemon
  17. // GATT service objects and is used in test cases in place of a mock and on the
  18. // Linux desktop.
  19. class DEVICE_BLUETOOTH_EXPORT FakeBluetoothGattServiceClient
  20. : public BluetoothGattServiceClient {
  21. public:
  22. struct Properties : public BluetoothGattServiceClient::Properties {
  23. explicit Properties(const PropertyChangedCallback& callback);
  24. ~Properties() override;
  25. // dbus::PropertySet override
  26. void Get(dbus::PropertyBase* property,
  27. dbus::PropertySet::GetCallback callback) override;
  28. void GetAll() override;
  29. void Set(dbus::PropertyBase* property,
  30. dbus::PropertySet::SetCallback callback) override;
  31. };
  32. FakeBluetoothGattServiceClient();
  33. FakeBluetoothGattServiceClient(const FakeBluetoothGattServiceClient&) =
  34. delete;
  35. FakeBluetoothGattServiceClient& operator=(
  36. const FakeBluetoothGattServiceClient&) = delete;
  37. ~FakeBluetoothGattServiceClient() override;
  38. // DBusClient override.
  39. void Init(dbus::Bus* bus, const std::string& bluetooth_service_name) override;
  40. // BluetoothGattServiceClient overrides.
  41. void AddObserver(Observer* observer) override;
  42. void RemoveObserver(Observer* observer) override;
  43. std::vector<dbus::ObjectPath> GetServices() override;
  44. Properties* GetProperties(const dbus::ObjectPath& object_path) override;
  45. // Makes a service visible for device with object path |device_path|. Note
  46. // that only one instance of a specific service is simulated at a time. Hence,
  47. // this method will fail, if the service is already visible.
  48. void ExposeHeartRateService(const dbus::ObjectPath& device_path);
  49. void HideHeartRateService();
  50. // Makes a service visible for device with object path |device_path|. Note
  51. // that only one instance of a specific service is simulated at a time. Hence,
  52. // this method will fail, if the service is already visible.
  53. void ExposeBatteryService(const dbus::ObjectPath& device_path);
  54. // Returns whether or not the Heart Rate Service is visible.
  55. bool IsHeartRateVisible() const;
  56. // Returns whether or not the Battery Service is visible.
  57. bool IsBatteryServiceVisible() const;
  58. // Returns the current object path of the visible Heart Rate service. If the
  59. // service is not visible, returns an invalid empty path.
  60. dbus::ObjectPath GetHeartRateServicePath() const;
  61. // Returns the current object path of the visible Battery service. If the
  62. // service is not visible, returns an invalid empty path.
  63. dbus::ObjectPath GetBatteryServicePath() const;
  64. // Final object path components and the corresponding UUIDs of the GATT
  65. // services that we emulate. Service paths are hierarchical to Bluetooth
  66. // device paths, so if the path component is "service0000", and the device
  67. // path is "/org/foo/device0", the service path is
  68. // "/org/foo/device0/service0000".
  69. static const char kHeartRateServicePathComponent[];
  70. static const char kHeartRateServiceUUID[];
  71. // Final object path components and the corresponding UUIDs of the GATT
  72. // services that we emulate. Service paths are hierarchical to Bluetooth
  73. // device paths, so if the path component is "service0001", and the device
  74. // path is "/org/foo/device0", the service path is
  75. // "/org/foo/device0/service0001".
  76. static const char kBatteryServicePathComponent[];
  77. static const char kBatteryServiceUUID[];
  78. static const char kGenericAccessServiceUUID[];
  79. private:
  80. // Property callback passed when we create Properties structures.
  81. void OnPropertyChanged(const dbus::ObjectPath& object_path,
  82. const std::string& property_name);
  83. // Notifies observers.
  84. void NotifyServiceAdded(const dbus::ObjectPath& object_path);
  85. void NotifyServiceRemoved(const dbus::ObjectPath& object_path);
  86. // Tells FakeBluetoothGattCharacteristicClient to expose GATT characteristics.
  87. // This is scheduled from ExposeHeartRateService to simulate asynchronous
  88. // retrieval of characteristics. If the Heart Rate Service is hidden at the
  89. // time this method is called, then it does nothing.
  90. void ExposeHeartRateCharacteristics();
  91. // Static properties we return. As long as a service is exposed, this will be
  92. // non-null. Otherwise it will be null.
  93. std::unique_ptr<Properties> heart_rate_service_properties_;
  94. std::unique_ptr<Properties> battery_service_properties_;
  95. std::string heart_rate_service_path_;
  96. std::string battery_service_path_;
  97. // List of observers interested in event notifications from us.
  98. base::ObserverList<Observer>::Unchecked observers_;
  99. // Weak pointer factory for generating 'this' pointers that might live longer
  100. // than we do.
  101. // Note: This should remain the last member so it'll be destroyed and
  102. // invalidate its weak pointers before any other members are destroyed.
  103. base::WeakPtrFactory<FakeBluetoothGattServiceClient> weak_ptr_factory_{this};
  104. };
  105. } // namespace bluez
  106. #endif // DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_SERVICE_CLIENT_H_