Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / fake_bluetooth_gatt_descriptor_client.cc
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
5 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "third_party/cros_system_api/dbus/service_constants.h"
10
11 namespace chromeos {
12
13 const char FakeBluetoothGattDescriptorClient::
14     kClientCharacteristicConfigurationPathComponent[] = "desc0000";
15 const char FakeBluetoothGattDescriptorClient::
16     kClientCharacteristicConfigurationUUID[] =
17         "00002902-0000-1000-8000-00805f9b34fb";
18
19 FakeBluetoothGattDescriptorClient::Properties::Properties(
20     const PropertyChangedCallback& callback)
21     : BluetoothGattDescriptorClient::Properties(
22           NULL,
23           bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface,
24           callback) {
25 }
26
27 FakeBluetoothGattDescriptorClient::Properties::~Properties() {
28 }
29
30 void FakeBluetoothGattDescriptorClient::Properties::Get(
31     dbus::PropertyBase* property,
32     dbus::PropertySet::GetCallback callback) {
33   VLOG(1) << "Get " << property->name();
34   callback.Run(true);
35 }
36
37 void FakeBluetoothGattDescriptorClient::Properties::GetAll() {
38   VLOG(1) << "GetAll";
39 }
40
41 void FakeBluetoothGattDescriptorClient::Properties::Set(
42     dbus::PropertyBase* property,
43     dbus::PropertySet::SetCallback callback) {
44   VLOG(1) << "Set " << property->name();
45   if (property->name() != value.name()) {
46     callback.Run(false);
47     return;
48   }
49
50   // TODO(armansito): Setting the "Value" property should be allowed based
51   // on permissions.
52   if (uuid.value() != kClientCharacteristicConfigurationUUID) {
53     callback.Run(false);
54     return;
55   }
56   callback.Run(true);
57   property->ReplaceValueWithSetValue();
58 }
59
60 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient()
61     : weak_ptr_factory_(this) {
62 }
63
64 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() {
65 }
66
67 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) {
68 }
69
70 void FakeBluetoothGattDescriptorClient::AddObserver(Observer* observer) {
71   observers_.AddObserver(observer);
72 }
73
74 void FakeBluetoothGattDescriptorClient::RemoveObserver(Observer* observer) {
75   observers_.RemoveObserver(observer);
76 }
77
78 std::vector<dbus::ObjectPath>
79 FakeBluetoothGattDescriptorClient::GetDescriptors() {
80   std::vector<dbus::ObjectPath> descriptors;
81   for (PropertiesMap::const_iterator iter = properties_.begin();
82        iter != properties_.end(); ++iter) {
83     descriptors.push_back(iter->first);
84   }
85   return descriptors;
86 }
87
88 FakeBluetoothGattDescriptorClient::Properties*
89 FakeBluetoothGattDescriptorClient::GetProperties(
90     const dbus::ObjectPath& object_path) {
91   PropertiesMap::const_iterator iter = properties_.find(object_path);
92   if (iter == properties_.end())
93     return NULL;
94   return iter->second;
95 }
96
97 dbus::ObjectPath FakeBluetoothGattDescriptorClient::ExposeDescriptor(
98     const dbus::ObjectPath& characteristic_path,
99     const std::string& uuid) {
100   if (uuid != kClientCharacteristicConfigurationUUID) {
101     VLOG(2) << "Unsupported UUID: " << uuid;
102     return dbus::ObjectPath();
103   }
104
105   // CCC descriptor is the only one supported at the moment.
106   DCHECK(characteristic_path.IsValid());
107   dbus::ObjectPath object_path(
108       characteristic_path.value() + "/" +
109       kClientCharacteristicConfigurationPathComponent);
110   DCHECK(object_path.IsValid());
111   PropertiesMap::const_iterator iter = properties_.find(object_path);
112   if (iter != properties_.end()) {
113     VLOG(1) << "Descriptor already exposed: " << object_path.value();
114     return dbus::ObjectPath();
115   }
116
117   Properties* properties = new Properties(base::Bind(
118       &FakeBluetoothGattDescriptorClient::OnPropertyChanged,
119       weak_ptr_factory_.GetWeakPtr(),
120       object_path));
121   properties_[object_path] = properties;
122   properties->uuid.ReplaceValue(uuid);
123   properties->characteristic.ReplaceValue(characteristic_path);
124
125   std::vector<uint8> value;
126   value.push_back(0);  // Notifications/Indications disabled.
127   value.push_back(0);
128   properties->value.ReplaceValue(value);
129
130   NotifyDescriptorAdded(object_path);
131
132   return object_path;
133 }
134
135 void FakeBluetoothGattDescriptorClient::HideDescriptor(
136     const dbus::ObjectPath& descriptor_path) {
137   PropertiesMap::iterator iter = properties_.find(descriptor_path);
138   if (iter == properties_.end()) {
139     VLOG(1) << "Descriptor not exposed: " << descriptor_path.value();
140     return;
141   }
142
143   NotifyDescriptorRemoved(descriptor_path);
144
145   delete iter->second;
146   properties_.erase(iter);
147 }
148
149 void FakeBluetoothGattDescriptorClient::OnPropertyChanged(
150     const dbus::ObjectPath& object_path,
151     const std::string& property_name) {
152   VLOG(2) << "Descriptor property changed: " << object_path.value()
153           << ": " << property_name;
154
155   FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
156                     GattDescriptorPropertyChanged(object_path, property_name));
157
158   // TODO(armansito): Implement CCC behavior (enable/disable notifications
159   // or indications characteristics).
160 }
161
162 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded(
163     const dbus::ObjectPath& object_path) {
164   FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
165                     GattDescriptorAdded(object_path));
166 }
167
168 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved(
169     const dbus::ObjectPath& object_path) {
170   FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
171                     GattDescriptorRemoved(object_path));
172 }
173
174 }  // namespace chromeos