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_descriptor_client.h"
8 #include "base/logging.h"
9 #include "third_party/cros_system_api/dbus/service_constants.h"
13 const char FakeBluetoothGattDescriptorClient::
14 kClientCharacteristicConfigurationPathComponent
[] = "desc0000";
15 const char FakeBluetoothGattDescriptorClient::
16 kClientCharacteristicConfigurationUUID
[] =
17 "00002902-0000-1000-8000-00805f9b34fb";
19 FakeBluetoothGattDescriptorClient::Properties::Properties(
20 const PropertyChangedCallback
& callback
)
21 : BluetoothGattDescriptorClient::Properties(
23 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
27 FakeBluetoothGattDescriptorClient::Properties::~Properties() {
30 void FakeBluetoothGattDescriptorClient::Properties::Get(
31 dbus::PropertyBase
* property
,
32 dbus::PropertySet::GetCallback callback
) {
33 VLOG(1) << "Get " << property
->name();
37 void FakeBluetoothGattDescriptorClient::Properties::GetAll() {
41 void FakeBluetoothGattDescriptorClient::Properties::Set(
42 dbus::PropertyBase
* property
,
43 dbus::PropertySet::SetCallback callback
) {
44 VLOG(1) << "Set " << property
->name();
48 FakeBluetoothGattDescriptorClient::DescriptorData::DescriptorData() {
51 FakeBluetoothGattDescriptorClient::DescriptorData::~DescriptorData() {
54 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient()
55 : weak_ptr_factory_(this) {
58 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() {
61 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus
* bus
) {
64 void FakeBluetoothGattDescriptorClient::AddObserver(Observer
* observer
) {
65 observers_
.AddObserver(observer
);
68 void FakeBluetoothGattDescriptorClient::RemoveObserver(Observer
* observer
) {
69 observers_
.RemoveObserver(observer
);
72 std::vector
<dbus::ObjectPath
>
73 FakeBluetoothGattDescriptorClient::GetDescriptors() {
74 std::vector
<dbus::ObjectPath
> descriptors
;
75 for (PropertiesMap::const_iterator iter
= properties_
.begin();
76 iter
!= properties_
.end(); ++iter
) {
77 descriptors
.push_back(iter
->first
);
82 FakeBluetoothGattDescriptorClient::Properties
*
83 FakeBluetoothGattDescriptorClient::GetProperties(
84 const dbus::ObjectPath
& object_path
) {
85 PropertiesMap::const_iterator iter
= properties_
.find(object_path
);
86 if (iter
== properties_
.end())
88 return iter
->second
->properties
.get();
91 void FakeBluetoothGattDescriptorClient::ReadValue(
92 const dbus::ObjectPath
& object_path
,
93 const ValueCallback
& callback
,
94 const ErrorCallback
& error_callback
) {
95 PropertiesMap::iterator iter
= properties_
.find(object_path
);
96 if (iter
== properties_
.end()) {
97 error_callback
.Run(kUnknownDescriptorError
, "");
101 callback
.Run(iter
->second
->value
);
104 void FakeBluetoothGattDescriptorClient::WriteValue(
105 const dbus::ObjectPath
& object_path
,
106 const std::vector
<uint8
>& value
,
107 const base::Closure
& callback
,
108 const ErrorCallback
& error_callback
) {
109 if (properties_
.find(object_path
) == properties_
.end()) {
110 error_callback
.Run(kUnknownDescriptorError
, "");
114 // Since the only fake descriptor is "Client Characteristic Configuration"
115 // and BlueZ doesn't allow writing to it, return failure.
116 error_callback
.Run("org.bluez.Error.Failed",
117 "Writing to the Client Characteristic Configuration "
118 "descriptor not allowed");
121 dbus::ObjectPath
FakeBluetoothGattDescriptorClient::ExposeDescriptor(
122 const dbus::ObjectPath
& characteristic_path
,
123 const std::string
& uuid
) {
124 if (uuid
!= kClientCharacteristicConfigurationUUID
) {
125 VLOG(2) << "Unsupported UUID: " << uuid
;
126 return dbus::ObjectPath();
129 // CCC descriptor is the only one supported at the moment.
130 DCHECK(characteristic_path
.IsValid());
131 dbus::ObjectPath
object_path(
132 characteristic_path
.value() + "/" +
133 kClientCharacteristicConfigurationPathComponent
);
134 DCHECK(object_path
.IsValid());
135 PropertiesMap::const_iterator iter
= properties_
.find(object_path
);
136 if (iter
!= properties_
.end()) {
137 VLOG(1) << "Descriptor already exposed: " << object_path
.value();
138 return dbus::ObjectPath();
141 Properties
* properties
= new Properties(base::Bind(
142 &FakeBluetoothGattDescriptorClient::OnPropertyChanged
,
143 weak_ptr_factory_
.GetWeakPtr(),
145 properties
->uuid
.ReplaceValue(uuid
);
146 properties
->characteristic
.ReplaceValue(characteristic_path
);
148 DescriptorData
* data
= new DescriptorData();
149 data
->properties
.reset(properties
);
151 data
->value
.push_back(1); // Notifications enabled.
152 data
->value
.push_back(0);
154 properties_
[object_path
] = data
;
156 NotifyDescriptorAdded(object_path
);
161 void FakeBluetoothGattDescriptorClient::HideDescriptor(
162 const dbus::ObjectPath
& descriptor_path
) {
163 PropertiesMap::iterator iter
= properties_
.find(descriptor_path
);
164 if (iter
== properties_
.end()) {
165 VLOG(1) << "Descriptor not exposed: " << descriptor_path
.value();
169 NotifyDescriptorRemoved(descriptor_path
);
172 properties_
.erase(iter
);
175 void FakeBluetoothGattDescriptorClient::OnPropertyChanged(
176 const dbus::ObjectPath
& object_path
,
177 const std::string
& property_name
) {
178 VLOG(2) << "Descriptor property changed: " << object_path
.value()
179 << ": " << property_name
;
181 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
182 GattDescriptorPropertyChanged(object_path
, property_name
));
185 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded(
186 const dbus::ObjectPath
& object_path
) {
187 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
188 GattDescriptorAdded(object_path
));
191 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved(
192 const dbus::ObjectPath
& object_path
) {
193 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
194 GattDescriptorRemoved(object_path
));
197 } // namespace chromeos