Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_gatt_manager_client.cc
blobc09232f5352b6405d76ebd04697b1aba727c35a7
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.
5 #include "chromeos/dbus/fake_bluetooth_gatt_manager_client.h"
7 #include "base/logging.h"
8 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_service_provider.h"
9 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_service_provider.h"
10 #include "chromeos/dbus/fake_bluetooth_gatt_service_service_provider.h"
11 #include "third_party/cros_system_api/dbus/service_constants.h"
13 namespace chromeos {
15 FakeBluetoothGattManagerClient::FakeBluetoothGattManagerClient() {
18 FakeBluetoothGattManagerClient::~FakeBluetoothGattManagerClient() {
21 // DBusClient override.
22 void FakeBluetoothGattManagerClient::Init(dbus::Bus* bus) {
25 // BluetoothGattManagerClient overrides.
26 void FakeBluetoothGattManagerClient::RegisterService(
27 const dbus::ObjectPath& service_path,
28 const Options& options,
29 const base::Closure& callback,
30 const ErrorCallback& error_callback) {
31 VLOG(1) << "Register GATT service: " << service_path.value();
33 // If a service provider wasn't created before, return error.
34 ServiceMap::iterator iter = service_map_.find(service_path);
35 if (iter == service_map_.end()) {
36 error_callback.Run(bluetooth_gatt_manager::kErrorInvalidArguments,
37 "GATT service doesn't exist: " + service_path.value());
38 return;
41 // Check to see if this GATT service was already registered.
42 ServiceProvider* provider = &iter->second;
43 if (provider->first) {
44 error_callback.Run(
45 bluetooth_gatt_manager::kErrorAlreadyExists,
46 "GATT service already registered: " + service_path.value());
47 return;
50 // Success!
51 provider->first = true;
52 callback.Run();
55 void FakeBluetoothGattManagerClient::UnregisterService(
56 const dbus::ObjectPath& service_path,
57 const base::Closure& callback,
58 const ErrorCallback& error_callback) {
59 VLOG(1) << "Unregister GATT service: " << service_path.value();
61 // If a service provider wasn't created before, return error.
62 ServiceMap::iterator iter = service_map_.find(service_path);
63 if (iter == service_map_.end()) {
64 error_callback.Run(bluetooth_gatt_manager::kErrorInvalidArguments,
65 "GATT service doesn't exist: " + service_path.value());
66 return;
69 // Return error if the GATT service wasn't registered before.
70 ServiceProvider* provider = &iter->second;
71 if (!provider->first) {
72 error_callback.Run(
73 bluetooth_gatt_manager::kErrorDoesNotExist,
74 "GATT service not registered: " + service_path.value());
75 return;
78 // Success!
79 provider->first = false;
80 callback.Run();
83 void FakeBluetoothGattManagerClient::RegisterServiceServiceProvider(
84 FakeBluetoothGattServiceServiceProvider* provider) {
85 // Ignore, if a service provider is already registered for the object path.
86 ServiceMap::iterator iter = service_map_.find(provider->object_path());
87 if (iter != service_map_.end()) {
88 VLOG(1) << "GATT service service provider already registered for "
89 << "object path: " << provider->object_path().value();
90 return;
92 service_map_[provider->object_path()] = std::make_pair(false, provider);
95 void FakeBluetoothGattManagerClient::RegisterCharacteristicServiceProvider(
96 FakeBluetoothGattCharacteristicServiceProvider* provider) {
97 // Ignore, if a service provider is already registered for the object path.
98 CharacteristicMap::iterator iter =
99 characteristic_map_.find(provider->object_path());
100 if (iter != characteristic_map_.end()) {
101 VLOG(1) << "GATT characteristic service provider already registered for "
102 << "object path: " << provider->object_path().value();
103 return;
105 characteristic_map_[provider->object_path()] = provider;
108 void FakeBluetoothGattManagerClient::RegisterDescriptorServiceProvider(
109 FakeBluetoothGattDescriptorServiceProvider* provider) {
110 // Ignore, if a service provider is already registered for the object path.
111 DescriptorMap::iterator iter =
112 descriptor_map_.find(provider->object_path());
113 if (iter != descriptor_map_.end()) {
114 VLOG(1) << "GATT descriptor service provider already registered for "
115 << "object path: " << provider->object_path().value();
116 return;
118 descriptor_map_[provider->object_path()] = provider;
121 void FakeBluetoothGattManagerClient::UnregisterServiceServiceProvider(
122 FakeBluetoothGattServiceServiceProvider* provider) {
123 ServiceMap::iterator iter =
124 service_map_.find(provider->object_path());
125 if (iter != service_map_.end() && iter->second.second == provider)
126 service_map_.erase(iter);
129 void FakeBluetoothGattManagerClient::UnregisterCharacteristicServiceProvider(
130 FakeBluetoothGattCharacteristicServiceProvider* provider) {
131 characteristic_map_.erase(provider->object_path());
134 void FakeBluetoothGattManagerClient::UnregisterDescriptorServiceProvider(
135 FakeBluetoothGattDescriptorServiceProvider* provider) {
136 descriptor_map_.erase(provider->object_path());
139 FakeBluetoothGattServiceServiceProvider*
140 FakeBluetoothGattManagerClient::GetServiceServiceProvider(
141 const dbus::ObjectPath& object_path) const {
142 ServiceMap::const_iterator iter = service_map_.find(object_path);
143 if (iter == service_map_.end())
144 return NULL;
145 return iter->second.second;
148 FakeBluetoothGattCharacteristicServiceProvider*
149 FakeBluetoothGattManagerClient::GetCharacteristicServiceProvider(
150 const dbus::ObjectPath& object_path) const {
151 CharacteristicMap::const_iterator iter =
152 characteristic_map_.find(object_path);
153 if (iter == characteristic_map_.end())
154 return NULL;
155 return iter->second;
158 FakeBluetoothGattDescriptorServiceProvider*
159 FakeBluetoothGattManagerClient::GetDescriptorServiceProvider(
160 const dbus::ObjectPath& object_path) const {
161 DescriptorMap::const_iterator iter = descriptor_map_.find(object_path);
162 if (iter == descriptor_map_.end())
163 return NULL;
164 return iter->second;
167 bool FakeBluetoothGattManagerClient::IsServiceRegistered(
168 const dbus::ObjectPath& object_path) const {
169 ServiceMap::const_iterator iter = service_map_.find(object_path);
170 if (iter == service_map_.end())
171 return false;
172 return iter->second.first;
175 } // namespace chromeos