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();
45 if (property->name() != value.name()) {
50 // TODO(armansito): Setting the "Value" property should be allowed based
52 if (uuid.value() != kClientCharacteristicConfigurationUUID) {
57 property->ReplaceValueWithSetValue();
60 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient()
61 : weak_ptr_factory_(this) {
64 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() {
67 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) {
70 void FakeBluetoothGattDescriptorClient::AddObserver(Observer* observer) {
71 observers_.AddObserver(observer);
74 void FakeBluetoothGattDescriptorClient::RemoveObserver(Observer* observer) {
75 observers_.RemoveObserver(observer);
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);
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())
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();
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();
117 Properties* properties = new Properties(base::Bind(
118 &FakeBluetoothGattDescriptorClient::OnPropertyChanged,
119 weak_ptr_factory_.GetWeakPtr(),
121 properties_[object_path] = properties;
122 properties->uuid.ReplaceValue(uuid);
123 properties->characteristic.ReplaceValue(characteristic_path);
125 std::vector<uint8> value;
126 value.push_back(0); // Notifications/Indications disabled.
128 properties->value.ReplaceValue(value);
130 NotifyDescriptorAdded(object_path);
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();
143 NotifyDescriptorRemoved(descriptor_path);
146 properties_.erase(iter);
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;
155 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
156 GattDescriptorPropertyChanged(object_path, property_name));
158 // TODO(armansito): Implement CCC behavior (enable/disable notifications
159 // or indications characteristics).
162 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded(
163 const dbus::ObjectPath& object_path) {
164 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
165 GattDescriptorAdded(object_path));
168 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved(
169 const dbus::ObjectPath& object_path) {
170 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
171 GattDescriptorRemoved(object_path));
174 } // namespace chromeos