fake_bluetooth_gatt_descriptor_service_provider.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.h"
  5. #include <algorithm>
  6. #include <iterator>
  7. #include "base/callback.h"
  8. #include "base/logging.h"
  9. #include "base/strings/string_util.h"
  10. #include "device/bluetooth/dbus/bluetooth_gatt_attribute_value_delegate.h"
  11. #include "device/bluetooth/dbus/bluez_dbus_manager.h"
  12. #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provider.h"
  13. #include "device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.h"
  14. #include "third_party/cros_system_api/dbus/service_constants.h"
  15. namespace bluez {
  16. namespace {
  17. bool CanWrite(const std::vector<std::string>& flags) {
  18. if (find(flags.begin(), flags.end(), bluetooth_gatt_descriptor::kFlagWrite) !=
  19. flags.end())
  20. return true;
  21. if (find(flags.begin(), flags.end(),
  22. bluetooth_gatt_descriptor::kFlagEncryptWrite) != flags.end())
  23. return true;
  24. if (find(flags.begin(), flags.end(),
  25. bluetooth_gatt_descriptor::kFlagEncryptAuthenticatedWrite) !=
  26. flags.end())
  27. return true;
  28. return false;
  29. }
  30. bool CanRead(const std::vector<std::string>& flags) {
  31. if (find(flags.begin(), flags.end(), bluetooth_gatt_descriptor::kFlagRead) !=
  32. flags.end())
  33. return true;
  34. if (find(flags.begin(), flags.end(),
  35. bluetooth_gatt_descriptor::kFlagEncryptRead) != flags.end())
  36. return true;
  37. if (find(flags.begin(), flags.end(),
  38. bluetooth_gatt_descriptor::kFlagEncryptAuthenticatedRead) !=
  39. flags.end())
  40. return true;
  41. return false;
  42. }
  43. } // namespace
  44. FakeBluetoothGattDescriptorServiceProvider::
  45. FakeBluetoothGattDescriptorServiceProvider(
  46. const dbus::ObjectPath& object_path,
  47. std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate,
  48. const std::string& uuid,
  49. const std::vector<std::string>& flags,
  50. const dbus::ObjectPath& characteristic_path)
  51. : object_path_(object_path),
  52. uuid_(uuid),
  53. flags_(flags),
  54. characteristic_path_(characteristic_path),
  55. delegate_(std::move(delegate)) {
  56. DVLOG(1) << "Creating Bluetooth GATT descriptor: " << object_path_.value();
  57. DCHECK(object_path_.IsValid());
  58. DCHECK(characteristic_path_.IsValid());
  59. DCHECK(!uuid.empty());
  60. DCHECK(delegate_);
  61. DCHECK(base::StartsWith(object_path_.value(),
  62. characteristic_path_.value() + "/",
  63. base::CompareCase::SENSITIVE));
  64. // TODO(rkc): Do something with |flags|.
  65. FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
  66. static_cast<FakeBluetoothGattManagerClient*>(
  67. bluez::BluezDBusManager::Get()->GetBluetoothGattManagerClient());
  68. fake_bluetooth_gatt_manager_client->RegisterDescriptorServiceProvider(this);
  69. }
  70. FakeBluetoothGattDescriptorServiceProvider::
  71. ~FakeBluetoothGattDescriptorServiceProvider() {
  72. DVLOG(1) << "Cleaning up Bluetooth GATT descriptor: " << object_path_.value();
  73. FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
  74. static_cast<FakeBluetoothGattManagerClient*>(
  75. bluez::BluezDBusManager::Get()->GetBluetoothGattManagerClient());
  76. fake_bluetooth_gatt_manager_client->UnregisterDescriptorServiceProvider(this);
  77. }
  78. void FakeBluetoothGattDescriptorServiceProvider::SendValueChanged(
  79. const std::vector<uint8_t>& value) {
  80. DVLOG(1) << "Sent descriptor value changed: " << object_path_.value()
  81. << " UUID: " << uuid_;
  82. }
  83. void FakeBluetoothGattDescriptorServiceProvider::GetValue(
  84. const dbus::ObjectPath& device_path,
  85. device::BluetoothLocalGattService::Delegate::ValueCallback callback) {
  86. DVLOG(1) << "GATT descriptor value Get request: " << object_path_.value()
  87. << " UUID: " << uuid_;
  88. // Check if this descriptor is registered.
  89. FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
  90. static_cast<FakeBluetoothGattManagerClient*>(
  91. bluez::BluezDBusManager::Get()->GetBluetoothGattManagerClient());
  92. FakeBluetoothGattCharacteristicServiceProvider* characteristic =
  93. fake_bluetooth_gatt_manager_client->GetCharacteristicServiceProvider(
  94. characteristic_path_);
  95. if (!characteristic) {
  96. DVLOG(1) << "GATT characteristic for descriptor does not exist: "
  97. << characteristic_path_.value();
  98. return;
  99. }
  100. if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(
  101. characteristic->service_path())) {
  102. DVLOG(1) << "GATT descriptor not registered.";
  103. std::move(callback).Run(
  104. device::BluetoothGattService::GattErrorCode::GATT_ERROR_FAILED,
  105. /*value=*/std::vector<uint8_t>());
  106. return;
  107. }
  108. if (!CanRead(flags_)) {
  109. std::move(callback).Run(
  110. device::BluetoothGattService::GattErrorCode::GATT_ERROR_FAILED,
  111. /*value=*/std::vector<uint8_t>());
  112. return;
  113. }
  114. // Pass on to the delegate.
  115. DCHECK(delegate_);
  116. delegate_->GetValue(device_path, std::move(callback));
  117. }
  118. void FakeBluetoothGattDescriptorServiceProvider::SetValue(
  119. const dbus::ObjectPath& device_path,
  120. const std::vector<uint8_t>& value,
  121. base::OnceClosure callback,
  122. device::BluetoothLocalGattService::Delegate::ErrorCallback error_callback) {
  123. DVLOG(1) << "GATT descriptor value Set request: " << object_path_.value()
  124. << " UUID: " << uuid_;
  125. // Check if this descriptor is registered.
  126. FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
  127. static_cast<FakeBluetoothGattManagerClient*>(
  128. bluez::BluezDBusManager::Get()->GetBluetoothGattManagerClient());
  129. FakeBluetoothGattCharacteristicServiceProvider* characteristic =
  130. fake_bluetooth_gatt_manager_client->GetCharacteristicServiceProvider(
  131. characteristic_path_);
  132. if (!characteristic) {
  133. DVLOG(1) << "GATT characteristic for descriptor does not exist: "
  134. << characteristic_path_.value();
  135. return;
  136. }
  137. if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(
  138. characteristic->service_path())) {
  139. DVLOG(1) << "GATT descriptor not registered.";
  140. std::move(error_callback).Run();
  141. return;
  142. }
  143. if (!CanWrite(flags_)) {
  144. DVLOG(1) << "GATT descriptor not writeable.";
  145. std::move(error_callback).Run();
  146. return;
  147. }
  148. // Pass on to the delegate.
  149. DCHECK(delegate_);
  150. delegate_->SetValue(device_path, value, std::move(callback),
  151. std::move(error_callback));
  152. }
  153. const dbus::ObjectPath&
  154. FakeBluetoothGattDescriptorServiceProvider::object_path() const {
  155. return object_path_;
  156. }
  157. } // namespace bluez