OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.h" | 5 #include "device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.h" |
6 | 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "base/callback.h" |
7 #include "base/logging.h" | 11 #include "base/logging.h" |
8 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provi
der.h" | 12 #include "base/strings/string_util.h" |
9 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.
h" | |
10 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_service_provider.h" | |
11 #include "third_party/cros_system_api/dbus/service_constants.h" | 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
12 | 14 |
13 namespace bluez { | 15 namespace bluez { |
14 | 16 |
| 17 std::set<dbus::ObjectPath> FakeBluetoothGattManagerClient::FindServiceProviders( |
| 18 dbus::ObjectPath application_path) { |
| 19 std::set<dbus::ObjectPath> services; |
| 20 for (const auto& service : service_map_) { |
| 21 if (base::StartsWith(service.first.value(), application_path.value(), |
| 22 base::CompareCase::SENSITIVE)) |
| 23 services.insert(service.first); |
| 24 } |
| 25 return services; |
| 26 } |
| 27 |
| 28 std::set<dbus::ObjectPath> |
| 29 FakeBluetoothGattManagerClient::FindCharacteristicProviders( |
| 30 dbus::ObjectPath application_path) { |
| 31 std::set<dbus::ObjectPath> characteristics; |
| 32 for (const auto& characteristic : characteristic_map_) { |
| 33 if (base::StartsWith(characteristic.first.value(), application_path.value(), |
| 34 base::CompareCase::SENSITIVE)) { |
| 35 characteristics.insert(characteristic.first); |
| 36 } |
| 37 } |
| 38 return characteristics; |
| 39 } |
| 40 |
| 41 std::set<dbus::ObjectPath> |
| 42 FakeBluetoothGattManagerClient::FindDescriptorProviders( |
| 43 dbus::ObjectPath application_path) { |
| 44 std::set<dbus::ObjectPath> descriptors; |
| 45 for (const auto& descriptor : descriptor_map_) { |
| 46 if (base::StartsWith(descriptor.first.value(), application_path.value(), |
| 47 base::CompareCase::SENSITIVE)) { |
| 48 descriptors.insert(descriptor.first); |
| 49 } |
| 50 } |
| 51 return descriptors; |
| 52 } |
| 53 |
| 54 bool FakeBluetoothGattManagerClient::VerifyProviderHierarchy( |
| 55 FakeBluetoothGattApplicationServiceProvider* application_provider) { |
| 56 dbus::ObjectPath application_path = application_provider->object_path(); |
| 57 std::set<dbus::ObjectPath> services = FindServiceProviders(application_path); |
| 58 std::set<dbus::ObjectPath> characteristics = |
| 59 FindCharacteristicProviders(application_path); |
| 60 std::set<dbus::ObjectPath> descriptors = |
| 61 FindDescriptorProviders(application_path); |
| 62 |
| 63 VLOG(1) << "Verifying " << services.size() |
| 64 << " services in application: " << application_path.value(); |
| 65 |
| 66 for (const auto& descriptor : descriptors) { |
| 67 if (characteristics.count( |
| 68 descriptor_map_[descriptor]->characteristic_path()) != 1) { |
| 69 return false; |
| 70 } |
| 71 VLOG(1) << "Descriptor " << descriptor.value() |
| 72 << " verified, has parent characteristic (" |
| 73 << descriptor_map_[descriptor]->characteristic_path().value() |
| 74 << ") in hierarchy."; |
| 75 } |
| 76 |
| 77 for (const auto& characteristic : characteristics) { |
| 78 if (services.count(characteristic_map_[characteristic]->service_path()) != |
| 79 1) { |
| 80 return false; |
| 81 } |
| 82 VLOG(1) << "Characteristic " << characteristic.value() |
| 83 << " verified, has parent service (" |
| 84 << characteristic_map_[characteristic]->service_path().value() |
| 85 << ") in hierarchy."; |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
15 FakeBluetoothGattManagerClient::FakeBluetoothGattManagerClient() {} | 91 FakeBluetoothGattManagerClient::FakeBluetoothGattManagerClient() {} |
16 | 92 |
17 FakeBluetoothGattManagerClient::~FakeBluetoothGattManagerClient() {} | 93 FakeBluetoothGattManagerClient::~FakeBluetoothGattManagerClient() {} |
18 | 94 |
19 // DBusClient override. | 95 // DBusClient override. |
20 void FakeBluetoothGattManagerClient::Init(dbus::Bus* bus) {} | 96 void FakeBluetoothGattManagerClient::Init(dbus::Bus* bus) {} |
21 | 97 |
22 // BluetoothGattManagerClient overrides. | 98 // BluetoothGattManagerClient overrides. |
23 void FakeBluetoothGattManagerClient::RegisterService( | 99 void FakeBluetoothGattManagerClient::RegisterApplication( |
24 const dbus::ObjectPath& service_path, | 100 const dbus::ObjectPath& application_path, |
25 const Options& options, | 101 const Options& options, |
26 const base::Closure& callback, | 102 const base::Closure& callback, |
27 const ErrorCallback& error_callback) { | 103 const ErrorCallback& error_callback) { |
28 VLOG(1) << "Register GATT service: " << service_path.value(); | 104 VLOG(1) << "Register GATT application: " << application_path.value(); |
29 | 105 ApplicationProvider* provider = |
30 // If a service provider wasn't created before, return error. | 106 GetApplicationServiceProvider(application_path); |
31 ServiceMap::iterator iter = service_map_.find(service_path); | 107 if (!provider || provider->second) |
32 if (iter == service_map_.end()) { | 108 error_callback.Run(bluetooth_gatt_service::kErrorFailed, ""); |
33 error_callback.Run(bluetooth_gatt_manager::kErrorInvalidArguments, | 109 if (!VerifyProviderHierarchy(provider->first)) |
34 "GATT service doesn't exist: " + service_path.value()); | 110 error_callback.Run(bluetooth_gatt_service::kErrorFailed, ""); |
35 return; | 111 provider->second = true; |
36 } | |
37 | |
38 // Check to see if this GATT service was already registered. | |
39 ServiceProvider* provider = &iter->second; | |
40 if (provider->first) { | |
41 error_callback.Run( | |
42 bluetooth_gatt_manager::kErrorAlreadyExists, | |
43 "GATT service already registered: " + service_path.value()); | |
44 return; | |
45 } | |
46 | |
47 // Success! | |
48 provider->first = true; | |
49 callback.Run(); | 112 callback.Run(); |
50 } | 113 } |
51 | 114 |
52 void FakeBluetoothGattManagerClient::UnregisterService( | 115 // BluetoothGattManagerClient overrides. |
53 const dbus::ObjectPath& service_path, | 116 void FakeBluetoothGattManagerClient::UnregisterApplication( |
| 117 const dbus::ObjectPath& application_path, |
54 const base::Closure& callback, | 118 const base::Closure& callback, |
55 const ErrorCallback& error_callback) { | 119 const ErrorCallback& error_callback) { |
56 VLOG(1) << "Unregister GATT service: " << service_path.value(); | 120 VLOG(1) << "Unregister GATT application: " << application_path.value(); |
57 | 121 ApplicationProvider* provider = |
58 // If a service provider wasn't created before, return error. | 122 GetApplicationServiceProvider(application_path); |
59 ServiceMap::iterator iter = service_map_.find(service_path); | 123 if (!provider || !provider->second) |
60 if (iter == service_map_.end()) { | 124 error_callback.Run(bluetooth_gatt_service::kErrorFailed, ""); |
61 error_callback.Run(bluetooth_gatt_manager::kErrorInvalidArguments, | 125 provider->second = false; |
62 "GATT service doesn't exist: " + service_path.value()); | |
63 return; | |
64 } | |
65 | |
66 // Return error if the GATT service wasn't registered before. | |
67 ServiceProvider* provider = &iter->second; | |
68 if (!provider->first) { | |
69 error_callback.Run(bluetooth_gatt_manager::kErrorDoesNotExist, | |
70 "GATT service not registered: " + service_path.value()); | |
71 return; | |
72 } | |
73 | |
74 // Success! | |
75 provider->first = false; | |
76 callback.Run(); | 126 callback.Run(); |
77 } | 127 } |
78 | 128 |
| 129 void FakeBluetoothGattManagerClient::RegisterApplicationServiceProvider( |
| 130 FakeBluetoothGattApplicationServiceProvider* provider) { |
| 131 application_map_[provider->object_path()] = |
| 132 std::pair<FakeBluetoothGattApplicationServiceProvider*, bool>(provider, |
| 133 false); |
| 134 } |
| 135 |
| 136 void FakeBluetoothGattManagerClient::UnregisterApplicationServiceProvider( |
| 137 FakeBluetoothGattApplicationServiceProvider* provider) { |
| 138 ApplicationMap::iterator iter = |
| 139 application_map_.find(provider->object_path()); |
| 140 if (iter != application_map_.end() && iter->second.first == provider) |
| 141 application_map_.erase(iter); |
| 142 } |
| 143 |
| 144 FakeBluetoothGattManagerClient::ApplicationProvider* |
| 145 FakeBluetoothGattManagerClient::GetApplicationServiceProvider( |
| 146 const dbus::ObjectPath& object_path) { |
| 147 ApplicationMap::iterator iter = application_map_.find(object_path); |
| 148 return (iter != application_map_.end()) ? &iter->second : nullptr; |
| 149 } |
| 150 |
79 void FakeBluetoothGattManagerClient::RegisterServiceServiceProvider( | 151 void FakeBluetoothGattManagerClient::RegisterServiceServiceProvider( |
80 FakeBluetoothGattServiceServiceProvider* provider) { | 152 FakeBluetoothGattServiceServiceProvider* provider) { |
81 // Ignore, if a service provider is already registered for the object path. | 153 // Ignore, if a service provider is already registered for the object path. |
82 ServiceMap::iterator iter = service_map_.find(provider->object_path()); | 154 ServiceMap::iterator iter = service_map_.find(provider->object_path()); |
83 if (iter != service_map_.end()) { | 155 if (iter != service_map_.end()) { |
84 VLOG(1) << "GATT service service provider already registered for " | 156 VLOG(1) << "GATT service service provider already registered for " |
85 << "object path: " << provider->object_path().value(); | 157 << "object path: " << provider->object_path().value(); |
86 return; | 158 return; |
87 } | 159 } |
88 service_map_[provider->object_path()] = std::make_pair(false, provider); | 160 service_map_[provider->object_path()] = provider; |
89 } | 161 } |
90 | 162 |
91 void FakeBluetoothGattManagerClient::RegisterCharacteristicServiceProvider( | 163 void FakeBluetoothGattManagerClient::RegisterCharacteristicServiceProvider( |
92 FakeBluetoothGattCharacteristicServiceProvider* provider) { | 164 FakeBluetoothGattCharacteristicServiceProvider* provider) { |
93 // Ignore, if a service provider is already registered for the object path. | 165 // Ignore, if a service provider is already registered for the object path. |
94 CharacteristicMap::iterator iter = | 166 CharacteristicMap::iterator iter = |
95 characteristic_map_.find(provider->object_path()); | 167 characteristic_map_.find(provider->object_path()); |
96 if (iter != characteristic_map_.end()) { | 168 if (iter != characteristic_map_.end()) { |
97 VLOG(1) << "GATT characteristic service provider already registered for " | 169 VLOG(1) << "GATT characteristic service provider already registered for " |
98 << "object path: " << provider->object_path().value(); | 170 << "object path: " << provider->object_path().value(); |
(...skipping 10 matching lines...) Expand all Loading... |
109 VLOG(1) << "GATT descriptor service provider already registered for " | 181 VLOG(1) << "GATT descriptor service provider already registered for " |
110 << "object path: " << provider->object_path().value(); | 182 << "object path: " << provider->object_path().value(); |
111 return; | 183 return; |
112 } | 184 } |
113 descriptor_map_[provider->object_path()] = provider; | 185 descriptor_map_[provider->object_path()] = provider; |
114 } | 186 } |
115 | 187 |
116 void FakeBluetoothGattManagerClient::UnregisterServiceServiceProvider( | 188 void FakeBluetoothGattManagerClient::UnregisterServiceServiceProvider( |
117 FakeBluetoothGattServiceServiceProvider* provider) { | 189 FakeBluetoothGattServiceServiceProvider* provider) { |
118 ServiceMap::iterator iter = service_map_.find(provider->object_path()); | 190 ServiceMap::iterator iter = service_map_.find(provider->object_path()); |
119 if (iter != service_map_.end() && iter->second.second == provider) | 191 if (iter != service_map_.end() && iter->second == provider) |
120 service_map_.erase(iter); | 192 service_map_.erase(iter); |
121 } | 193 } |
122 | 194 |
123 void FakeBluetoothGattManagerClient::UnregisterCharacteristicServiceProvider( | 195 void FakeBluetoothGattManagerClient::UnregisterCharacteristicServiceProvider( |
124 FakeBluetoothGattCharacteristicServiceProvider* provider) { | 196 FakeBluetoothGattCharacteristicServiceProvider* provider) { |
125 characteristic_map_.erase(provider->object_path()); | 197 characteristic_map_.erase(provider->object_path()); |
126 } | 198 } |
127 | 199 |
128 void FakeBluetoothGattManagerClient::UnregisterDescriptorServiceProvider( | 200 void FakeBluetoothGattManagerClient::UnregisterDescriptorServiceProvider( |
129 FakeBluetoothGattDescriptorServiceProvider* provider) { | 201 FakeBluetoothGattDescriptorServiceProvider* provider) { |
130 descriptor_map_.erase(provider->object_path()); | 202 descriptor_map_.erase(provider->object_path()); |
131 } | 203 } |
132 | 204 |
133 FakeBluetoothGattServiceServiceProvider* | 205 FakeBluetoothGattServiceServiceProvider* |
134 FakeBluetoothGattManagerClient::GetServiceServiceProvider( | 206 FakeBluetoothGattManagerClient::GetServiceServiceProvider( |
135 const dbus::ObjectPath& object_path) const { | 207 const dbus::ObjectPath& object_path) const { |
136 ServiceMap::const_iterator iter = service_map_.find(object_path); | 208 ServiceMap::const_iterator iter = service_map_.find(object_path); |
137 if (iter == service_map_.end()) | 209 if (iter == service_map_.end()) |
138 return NULL; | 210 return NULL; |
139 return iter->second.second; | 211 return iter->second; |
140 } | 212 } |
141 | 213 |
142 FakeBluetoothGattCharacteristicServiceProvider* | 214 FakeBluetoothGattCharacteristicServiceProvider* |
143 FakeBluetoothGattManagerClient::GetCharacteristicServiceProvider( | 215 FakeBluetoothGattManagerClient::GetCharacteristicServiceProvider( |
144 const dbus::ObjectPath& object_path) const { | 216 const dbus::ObjectPath& object_path) const { |
145 CharacteristicMap::const_iterator iter = | 217 CharacteristicMap::const_iterator iter = |
146 characteristic_map_.find(object_path); | 218 characteristic_map_.find(object_path); |
147 if (iter == characteristic_map_.end()) | 219 if (iter == characteristic_map_.end()) |
148 return NULL; | 220 return NULL; |
149 return iter->second; | 221 return iter->second; |
150 } | 222 } |
151 | 223 |
152 FakeBluetoothGattDescriptorServiceProvider* | 224 FakeBluetoothGattDescriptorServiceProvider* |
153 FakeBluetoothGattManagerClient::GetDescriptorServiceProvider( | 225 FakeBluetoothGattManagerClient::GetDescriptorServiceProvider( |
154 const dbus::ObjectPath& object_path) const { | 226 const dbus::ObjectPath& object_path) const { |
155 DescriptorMap::const_iterator iter = descriptor_map_.find(object_path); | 227 DescriptorMap::const_iterator iter = descriptor_map_.find(object_path); |
156 if (iter == descriptor_map_.end()) | 228 if (iter == descriptor_map_.end()) |
157 return NULL; | 229 return NULL; |
158 return iter->second; | 230 return iter->second; |
159 } | 231 } |
160 | 232 |
161 bool FakeBluetoothGattManagerClient::IsServiceRegistered( | 233 bool FakeBluetoothGattManagerClient::IsServiceRegistered( |
162 const dbus::ObjectPath& object_path) const { | 234 const dbus::ObjectPath& service_path) const { |
163 ServiceMap::const_iterator iter = service_map_.find(object_path); | 235 const auto& service = service_map_.find(service_path); |
164 if (iter == service_map_.end()) | 236 if (service == service_map_.end()) |
165 return false; | 237 return false; |
166 return iter->second.first; | 238 |
| 239 for (const auto& application : application_map_) { |
| 240 if (base::StartsWith(service_path.value(), |
| 241 application.second.first->object_path().value(), |
| 242 base::CompareCase::SENSITIVE)) { |
| 243 return application.second.second; |
| 244 } |
| 245 } |
| 246 return false; |
167 } | 247 } |
168 | 248 |
169 } // namespace bluez | 249 } // namespace bluez |
OLD | NEW |