fake_bluetooth_gatt_descriptor_client.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_DESCRIPTOR_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_DESCRIPTOR_CLIENT_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "dbus/object_path.h"
  13. #include "device/bluetooth/bluetooth_export.h"
  14. #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h"
  15. namespace bluez {
  16. // FakeBluetoothGattDescriptorClient simulates the behavior of the Bluetooth
  17. // Daemon GATT characteristic descriptor objects and is used in test cases in
  18. // place of a mock and on the Linux desktop.
  19. class DEVICE_BLUETOOTH_EXPORT FakeBluetoothGattDescriptorClient
  20. : public BluetoothGattDescriptorClient {
  21. public:
  22. struct Properties : public BluetoothGattDescriptorClient::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. FakeBluetoothGattDescriptorClient();
  33. FakeBluetoothGattDescriptorClient(const FakeBluetoothGattDescriptorClient&) =
  34. delete;
  35. FakeBluetoothGattDescriptorClient& operator=(
  36. const FakeBluetoothGattDescriptorClient&) = delete;
  37. ~FakeBluetoothGattDescriptorClient() override;
  38. // DBusClient override.
  39. void Init(dbus::Bus* bus, const std::string& bluetooth_service_name) override;
  40. // BluetoothGattDescriptorClient overrides.
  41. void AddObserver(Observer* observer) override;
  42. void RemoveObserver(Observer* observer) override;
  43. std::vector<dbus::ObjectPath> GetDescriptors() override;
  44. Properties* GetProperties(const dbus::ObjectPath& object_path) override;
  45. void ReadValue(const dbus::ObjectPath& object_path,
  46. ValueCallback callback,
  47. ErrorCallback error_callback) override;
  48. void WriteValue(const dbus::ObjectPath& object_path,
  49. const std::vector<uint8_t>& value,
  50. base::OnceClosure callback,
  51. ErrorCallback error_callback) override;
  52. // Makes the descriptor with the UUID |uuid| visible under the characteristic
  53. // with object path |characteristic_path|. Descriptor object paths are
  54. // hierarchical to their characteristics. |uuid| must belong to a descriptor
  55. // for which there is a constant defined below, otherwise this method has no
  56. // effect. Returns the object path of the created descriptor. In the no-op
  57. // case, returns an invalid path.
  58. dbus::ObjectPath ExposeDescriptor(const dbus::ObjectPath& characteristic_path,
  59. const std::string& uuid);
  60. void HideDescriptor(const dbus::ObjectPath& descriptor_path);
  61. // Object path components and UUIDs of GATT characteristic descriptors.
  62. static const char kClientCharacteristicConfigurationPathComponent[];
  63. static const char kClientCharacteristicConfigurationUUID[];
  64. private:
  65. // Property callback passed when we create Properties structures.
  66. void OnPropertyChanged(const dbus::ObjectPath& object_path,
  67. const std::string& property_name);
  68. // Notifies observers.
  69. void NotifyDescriptorAdded(const dbus::ObjectPath& object_path);
  70. void NotifyDescriptorRemoved(const dbus::ObjectPath& object_path);
  71. // Mapping from object paths to Properties structures.
  72. struct DescriptorData {
  73. DescriptorData();
  74. ~DescriptorData();
  75. std::unique_ptr<Properties> properties;
  76. };
  77. typedef std::map<dbus::ObjectPath, DescriptorData*> PropertiesMap;
  78. PropertiesMap properties_;
  79. // List of observers interested in event notifications from us.
  80. base::ObserverList<Observer>::Unchecked observers_;
  81. // Weak pointer factory for generating 'this' pointers that might live longer
  82. // than we do.
  83. // Note: This should remain the last member so it'll be destroyed and
  84. // invalidate its weak pointers before any other members are destroyed.
  85. base::WeakPtrFactory<FakeBluetoothGattDescriptorClient> weak_ptr_factory_{
  86. this};
  87. };
  88. } // namespace bluez
  89. #endif // DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_DESCRIPTOR_CLIENT_H_