Index: device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.cc |
diff --git a/device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.cc b/device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.cc |
index 284a17c7c267265994ac5cf1f644323d166b2e1e..7faf1657429607a6d8696f2b2c9c66894d236ff9 100644 |
--- a/device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.cc |
+++ b/device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.cc |
@@ -4,14 +4,90 @@ |
#include "device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.h" |
+#include <set> |
+#include <string> |
+ |
+#include "base/callback.h" |
#include "base/logging.h" |
-#include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provider.h" |
-#include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.h" |
-#include "device/bluetooth/dbus/fake_bluetooth_gatt_service_service_provider.h" |
+#include "base/strings/string_util.h" |
#include "third_party/cros_system_api/dbus/service_constants.h" |
namespace bluez { |
+std::set<dbus::ObjectPath> FakeBluetoothGattManagerClient::FindServiceProviders( |
+ dbus::ObjectPath application_path) { |
+ std::set<dbus::ObjectPath> services; |
+ for (const auto& service : service_map_) { |
+ if (base::StartsWith(service.first.value(), application_path.value(), |
+ base::CompareCase::SENSITIVE)) |
+ services.insert(service.first); |
+ } |
+ return services; |
+} |
+ |
+std::set<dbus::ObjectPath> |
+FakeBluetoothGattManagerClient::FindCharacteristicProviders( |
+ dbus::ObjectPath application_path) { |
+ std::set<dbus::ObjectPath> characteristics; |
+ for (const auto& characteristic : characteristic_map_) { |
+ if (base::StartsWith(characteristic.first.value(), application_path.value(), |
+ base::CompareCase::SENSITIVE)) { |
+ characteristics.insert(characteristic.first); |
+ } |
+ } |
+ return characteristics; |
+} |
+ |
+std::set<dbus::ObjectPath> |
+FakeBluetoothGattManagerClient::FindDescriptorProviders( |
+ dbus::ObjectPath application_path) { |
+ std::set<dbus::ObjectPath> descriptors; |
+ for (const auto& descriptor : descriptor_map_) { |
+ if (base::StartsWith(descriptor.first.value(), application_path.value(), |
+ base::CompareCase::SENSITIVE)) { |
+ descriptors.insert(descriptor.first); |
+ } |
+ } |
+ return descriptors; |
+} |
+ |
+bool FakeBluetoothGattManagerClient::VerifyProviderHierarchy( |
+ FakeBluetoothGattApplicationServiceProvider* application_provider) { |
+ dbus::ObjectPath application_path = application_provider->object_path(); |
+ std::set<dbus::ObjectPath> services = FindServiceProviders(application_path); |
+ std::set<dbus::ObjectPath> characteristics = |
+ FindCharacteristicProviders(application_path); |
+ std::set<dbus::ObjectPath> descriptors = |
+ FindDescriptorProviders(application_path); |
+ |
+ VLOG(1) << "Verifying " << services.size() |
+ << " services in application: " << application_path.value(); |
+ |
+ for (const auto& descriptor : descriptors) { |
+ if (characteristics.count( |
+ descriptor_map_[descriptor]->characteristic_path()) != 1) { |
+ return false; |
+ } |
+ VLOG(1) << "Descriptor " << descriptor.value() |
+ << " verified, has parent characteristic (" |
+ << descriptor_map_[descriptor]->characteristic_path().value() |
+ << ") in hierarchy."; |
+ } |
+ |
+ for (const auto& characteristic : characteristics) { |
+ if (services.count(characteristic_map_[characteristic]->service_path()) != |
+ 1) { |
+ return false; |
+ } |
+ VLOG(1) << "Characteristic " << characteristic.value() |
+ << " verified, has parent service (" |
+ << characteristic_map_[characteristic]->service_path().value() |
+ << ") in hierarchy."; |
+ } |
+ |
+ return true; |
+} |
+ |
FakeBluetoothGattManagerClient::FakeBluetoothGattManagerClient() {} |
FakeBluetoothGattManagerClient::~FakeBluetoothGattManagerClient() {} |
@@ -20,60 +96,56 @@ FakeBluetoothGattManagerClient::~FakeBluetoothGattManagerClient() {} |
void FakeBluetoothGattManagerClient::Init(dbus::Bus* bus) {} |
// BluetoothGattManagerClient overrides. |
-void FakeBluetoothGattManagerClient::RegisterService( |
- const dbus::ObjectPath& service_path, |
+void FakeBluetoothGattManagerClient::RegisterApplication( |
+ const dbus::ObjectPath& application_path, |
const Options& options, |
const base::Closure& callback, |
const ErrorCallback& error_callback) { |
- VLOG(1) << "Register GATT service: " << service_path.value(); |
- |
- // If a service provider wasn't created before, return error. |
- ServiceMap::iterator iter = service_map_.find(service_path); |
- if (iter == service_map_.end()) { |
- error_callback.Run(bluetooth_gatt_manager::kErrorInvalidArguments, |
- "GATT service doesn't exist: " + service_path.value()); |
- return; |
- } |
- |
- // Check to see if this GATT service was already registered. |
- ServiceProvider* provider = &iter->second; |
- if (provider->first) { |
- error_callback.Run( |
- bluetooth_gatt_manager::kErrorAlreadyExists, |
- "GATT service already registered: " + service_path.value()); |
- return; |
- } |
- |
- // Success! |
- provider->first = true; |
+ VLOG(1) << "Register GATT application: " << application_path.value(); |
+ ApplicationProvider* provider = |
+ GetApplicationServiceProvider(application_path); |
+ if (!provider || provider->second) |
+ error_callback.Run(bluetooth_gatt_service::kErrorFailed, ""); |
+ if (!VerifyProviderHierarchy(provider->first)) |
+ error_callback.Run(bluetooth_gatt_service::kErrorFailed, ""); |
+ provider->second = true; |
callback.Run(); |
} |
-void FakeBluetoothGattManagerClient::UnregisterService( |
- const dbus::ObjectPath& service_path, |
+// BluetoothGattManagerClient overrides. |
+void FakeBluetoothGattManagerClient::UnregisterApplication( |
+ const dbus::ObjectPath& application_path, |
const base::Closure& callback, |
const ErrorCallback& error_callback) { |
- VLOG(1) << "Unregister GATT service: " << service_path.value(); |
+ VLOG(1) << "Unregister GATT application: " << application_path.value(); |
+ ApplicationProvider* provider = |
+ GetApplicationServiceProvider(application_path); |
+ if (!provider || !provider->second) |
+ error_callback.Run(bluetooth_gatt_service::kErrorFailed, ""); |
+ provider->second = false; |
+ callback.Run(); |
+} |
- // If a service provider wasn't created before, return error. |
- ServiceMap::iterator iter = service_map_.find(service_path); |
- if (iter == service_map_.end()) { |
- error_callback.Run(bluetooth_gatt_manager::kErrorInvalidArguments, |
- "GATT service doesn't exist: " + service_path.value()); |
- return; |
- } |
+void FakeBluetoothGattManagerClient::RegisterApplicationServiceProvider( |
+ FakeBluetoothGattApplicationServiceProvider* provider) { |
+ application_map_[provider->object_path()] = |
+ std::pair<FakeBluetoothGattApplicationServiceProvider*, bool>(provider, |
+ false); |
+} |
- // Return error if the GATT service wasn't registered before. |
- ServiceProvider* provider = &iter->second; |
- if (!provider->first) { |
- error_callback.Run(bluetooth_gatt_manager::kErrorDoesNotExist, |
- "GATT service not registered: " + service_path.value()); |
- return; |
- } |
+void FakeBluetoothGattManagerClient::UnregisterApplicationServiceProvider( |
+ FakeBluetoothGattApplicationServiceProvider* provider) { |
+ ApplicationMap::iterator iter = |
+ application_map_.find(provider->object_path()); |
+ if (iter != application_map_.end() && iter->second.first == provider) |
+ application_map_.erase(iter); |
+} |
- // Success! |
- provider->first = false; |
- callback.Run(); |
+FakeBluetoothGattManagerClient::ApplicationProvider* |
+FakeBluetoothGattManagerClient::GetApplicationServiceProvider( |
+ const dbus::ObjectPath& object_path) { |
+ ApplicationMap::iterator iter = application_map_.find(object_path); |
+ return (iter != application_map_.end()) ? &iter->second : nullptr; |
} |
void FakeBluetoothGattManagerClient::RegisterServiceServiceProvider( |
@@ -85,7 +157,7 @@ void FakeBluetoothGattManagerClient::RegisterServiceServiceProvider( |
<< "object path: " << provider->object_path().value(); |
return; |
} |
- service_map_[provider->object_path()] = std::make_pair(false, provider); |
+ service_map_[provider->object_path()] = provider; |
} |
void FakeBluetoothGattManagerClient::RegisterCharacteristicServiceProvider( |
@@ -116,7 +188,7 @@ void FakeBluetoothGattManagerClient::RegisterDescriptorServiceProvider( |
void FakeBluetoothGattManagerClient::UnregisterServiceServiceProvider( |
FakeBluetoothGattServiceServiceProvider* provider) { |
ServiceMap::iterator iter = service_map_.find(provider->object_path()); |
- if (iter != service_map_.end() && iter->second.second == provider) |
+ if (iter != service_map_.end() && iter->second == provider) |
service_map_.erase(iter); |
} |
@@ -136,7 +208,7 @@ FakeBluetoothGattManagerClient::GetServiceServiceProvider( |
ServiceMap::const_iterator iter = service_map_.find(object_path); |
if (iter == service_map_.end()) |
return NULL; |
- return iter->second.second; |
+ return iter->second; |
} |
FakeBluetoothGattCharacteristicServiceProvider* |
@@ -159,11 +231,19 @@ FakeBluetoothGattManagerClient::GetDescriptorServiceProvider( |
} |
bool FakeBluetoothGattManagerClient::IsServiceRegistered( |
- const dbus::ObjectPath& object_path) const { |
- ServiceMap::const_iterator iter = service_map_.find(object_path); |
- if (iter == service_map_.end()) |
+ const dbus::ObjectPath& service_path) const { |
+ const auto& service = service_map_.find(service_path); |
+ if (service == service_map_.end()) |
return false; |
- return iter->second.first; |
+ |
+ for (const auto& application : application_map_) { |
+ if (base::StartsWith(service_path.value(), |
+ application.second.first->object_path().value(), |
+ base::CompareCase::SENSITIVE)) { |
+ return application.second.second; |
+ } |
+ } |
+ return false; |
} |
} // namespace bluez |