fake_bluetooth_gatt_manager_client.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_MANAGER_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_MANAGER_CLIENT_H_
  6. #include <map>
  7. #include <set>
  8. #include <utility>
  9. #include "base/callback_forward.h"
  10. #include "dbus/bus.h"
  11. #include "dbus/object_path.h"
  12. #include "device/bluetooth/bluetooth_export.h"
  13. #include "device/bluetooth/dbus/bluetooth_gatt_manager_client.h"
  14. #include "device/bluetooth/dbus/fake_bluetooth_gatt_application_service_provider.h"
  15. #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provider.h"
  16. #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.h"
  17. #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_service_provider.h"
  18. namespace bluez {
  19. class FakeBluetoothGattApplicationServiceProvider;
  20. class FakeBluetoothGattCharacteristicServiceProvider;
  21. class FakeBluetoothGattDescriptorServiceProvider;
  22. class FakeBluetoothGattServiceServiceProvider;
  23. // FakeBluetoothGattManagerClient simulates the behavior of the Bluetooth
  24. // daemon's GATT manager object and is used both in test cases in place of a
  25. // mock and on the Linux desktop.
  26. class DEVICE_BLUETOOTH_EXPORT FakeBluetoothGattManagerClient
  27. : public BluetoothGattManagerClient {
  28. public:
  29. FakeBluetoothGattManagerClient();
  30. FakeBluetoothGattManagerClient(const FakeBluetoothGattManagerClient&) =
  31. delete;
  32. FakeBluetoothGattManagerClient& operator=(
  33. const FakeBluetoothGattManagerClient&) = delete;
  34. ~FakeBluetoothGattManagerClient() override;
  35. // DBusClient override.
  36. void Init(dbus::Bus* bus, const std::string& bluetooth_service_name) override;
  37. // BluetoothGattManagerClient overrides.
  38. void RegisterApplication(const dbus::ObjectPath& adapter_object_path,
  39. const dbus::ObjectPath& application_path,
  40. const Options& options,
  41. base::OnceClosure callback,
  42. ErrorCallback error_callback) override;
  43. void UnregisterApplication(const dbus::ObjectPath& adapter_object_path,
  44. const dbus::ObjectPath& application_path,
  45. base::OnceClosure callback,
  46. ErrorCallback error_callback) override;
  47. // Register, unregister, and retrieve pointers to application service
  48. // providers. Automatically called from the application provider constructor
  49. // and destructors.
  50. void RegisterApplicationServiceProvider(
  51. FakeBluetoothGattApplicationServiceProvider* provider);
  52. void RegisterServiceServiceProvider(
  53. FakeBluetoothGattServiceServiceProvider* provider);
  54. void RegisterCharacteristicServiceProvider(
  55. FakeBluetoothGattCharacteristicServiceProvider* provider);
  56. void RegisterDescriptorServiceProvider(
  57. FakeBluetoothGattDescriptorServiceProvider* provider);
  58. void UnregisterApplicationServiceProvider(
  59. FakeBluetoothGattApplicationServiceProvider* provider);
  60. void UnregisterServiceServiceProvider(
  61. FakeBluetoothGattServiceServiceProvider* provider);
  62. void UnregisterCharacteristicServiceProvider(
  63. FakeBluetoothGattCharacteristicServiceProvider* provider);
  64. void UnregisterDescriptorServiceProvider(
  65. FakeBluetoothGattDescriptorServiceProvider* provider);
  66. // Return a pointer to the service provider that corresponds to the object
  67. // path |object_path| if it exists.
  68. FakeBluetoothGattServiceServiceProvider* GetServiceServiceProvider(
  69. const dbus::ObjectPath& object_path) const;
  70. FakeBluetoothGattCharacteristicServiceProvider*
  71. GetCharacteristicServiceProvider(const dbus::ObjectPath& object_path) const;
  72. FakeBluetoothGattDescriptorServiceProvider* GetDescriptorServiceProvider(
  73. const dbus::ObjectPath& object_path) const;
  74. bool IsServiceRegistered(const dbus::ObjectPath& object_path) const;
  75. private:
  76. // The boolean indicates whether this application service provider is
  77. // registered or not.
  78. using ApplicationProvider =
  79. std::pair<FakeBluetoothGattApplicationServiceProvider*, bool>;
  80. // Mappings for GATT application, service, characteristic, and descriptor
  81. // service providers. The fake GATT manager stores references to all
  82. // instances created so that they can be obtained by tests.
  83. using ApplicationMap = std::map<dbus::ObjectPath, ApplicationProvider>;
  84. using ServiceMap =
  85. std::map<dbus::ObjectPath, FakeBluetoothGattServiceServiceProvider*>;
  86. using CharacteristicMap =
  87. std::map<dbus::ObjectPath,
  88. FakeBluetoothGattCharacteristicServiceProvider*>;
  89. using DescriptorMap =
  90. std::map<dbus::ObjectPath, FakeBluetoothGattDescriptorServiceProvider*>;
  91. // Return a pointer to the Application provider that corresponds to the object
  92. // path |object_path| if it exists.
  93. ApplicationProvider* GetApplicationServiceProvider(
  94. const dbus::ObjectPath& object_path);
  95. // Find attribute providers in this application.
  96. std::set<dbus::ObjectPath> FindServiceProviders(
  97. dbus::ObjectPath application_path);
  98. std::set<dbus::ObjectPath> FindCharacteristicProviders(
  99. dbus::ObjectPath application_path);
  100. std::set<dbus::ObjectPath> FindDescriptorProviders(
  101. dbus::ObjectPath application_path);
  102. // Verify that the attribute hierarchy exposed by this application provider
  103. // is correct. i.e., all descriptors have a characteristic, which all have a
  104. // service and all the attributes are under this application's object path.
  105. bool VerifyProviderHierarchy(
  106. FakeBluetoothGattApplicationServiceProvider* application_provider);
  107. ApplicationMap application_map_;
  108. ServiceMap service_map_;
  109. CharacteristicMap characteristic_map_;
  110. DescriptorMap descriptor_map_;
  111. };
  112. } // namespace bluez
  113. #endif // DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_MANAGER_CLIENT_H_