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 "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" | 5 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "third_party/cros_system_api/dbus/service_constants.h" | 9 #include "third_party/cros_system_api/dbus/service_constants.h" |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 } | 35 } |
36 | 36 |
37 void FakeBluetoothGattDescriptorClient::Properties::GetAll() { | 37 void FakeBluetoothGattDescriptorClient::Properties::GetAll() { |
38 VLOG(1) << "GetAll"; | 38 VLOG(1) << "GetAll"; |
39 } | 39 } |
40 | 40 |
41 void FakeBluetoothGattDescriptorClient::Properties::Set( | 41 void FakeBluetoothGattDescriptorClient::Properties::Set( |
42 dbus::PropertyBase* property, | 42 dbus::PropertyBase* property, |
43 dbus::PropertySet::SetCallback callback) { | 43 dbus::PropertySet::SetCallback callback) { |
44 VLOG(1) << "Set " << property->name(); | 44 VLOG(1) << "Set " << property->name(); |
45 if (property->name() != value.name()) { | 45 callback.Run(false); |
46 callback.Run(false); | 46 } |
47 return; | |
48 } | |
49 | 47 |
50 // TODO(armansito): Setting the "Value" property should be allowed based | 48 FakeBluetoothGattDescriptorClient::DescriptorData::DescriptorData() { |
51 // on permissions. | 49 } |
52 if (uuid.value() != kClientCharacteristicConfigurationUUID) { | 50 |
53 callback.Run(false); | 51 FakeBluetoothGattDescriptorClient::DescriptorData::~DescriptorData() { |
54 return; | |
55 } | |
56 callback.Run(true); | |
57 property->ReplaceValueWithSetValue(); | |
58 } | 52 } |
59 | 53 |
60 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient() | 54 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient() |
61 : weak_ptr_factory_(this) { | 55 : weak_ptr_factory_(this) { |
62 } | 56 } |
63 | 57 |
64 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() { | 58 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() { |
65 } | 59 } |
66 | 60 |
67 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) { | 61 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) { |
(...skipping 16 matching lines...) Expand all Loading... |
84 } | 78 } |
85 return descriptors; | 79 return descriptors; |
86 } | 80 } |
87 | 81 |
88 FakeBluetoothGattDescriptorClient::Properties* | 82 FakeBluetoothGattDescriptorClient::Properties* |
89 FakeBluetoothGattDescriptorClient::GetProperties( | 83 FakeBluetoothGattDescriptorClient::GetProperties( |
90 const dbus::ObjectPath& object_path) { | 84 const dbus::ObjectPath& object_path) { |
91 PropertiesMap::const_iterator iter = properties_.find(object_path); | 85 PropertiesMap::const_iterator iter = properties_.find(object_path); |
92 if (iter == properties_.end()) | 86 if (iter == properties_.end()) |
93 return NULL; | 87 return NULL; |
94 return iter->second; | 88 return iter->second->properties.get(); |
| 89 } |
| 90 |
| 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, ""); |
| 98 return; |
| 99 } |
| 100 |
| 101 callback.Run(iter->second->value); |
| 102 } |
| 103 |
| 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, ""); |
| 111 return; |
| 112 } |
| 113 |
| 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"); |
95 } | 119 } |
96 | 120 |
97 dbus::ObjectPath FakeBluetoothGattDescriptorClient::ExposeDescriptor( | 121 dbus::ObjectPath FakeBluetoothGattDescriptorClient::ExposeDescriptor( |
98 const dbus::ObjectPath& characteristic_path, | 122 const dbus::ObjectPath& characteristic_path, |
99 const std::string& uuid) { | 123 const std::string& uuid) { |
100 if (uuid != kClientCharacteristicConfigurationUUID) { | 124 if (uuid != kClientCharacteristicConfigurationUUID) { |
101 VLOG(2) << "Unsupported UUID: " << uuid; | 125 VLOG(2) << "Unsupported UUID: " << uuid; |
102 return dbus::ObjectPath(); | 126 return dbus::ObjectPath(); |
103 } | 127 } |
104 | 128 |
105 // CCC descriptor is the only one supported at the moment. | 129 // CCC descriptor is the only one supported at the moment. |
106 DCHECK(characteristic_path.IsValid()); | 130 DCHECK(characteristic_path.IsValid()); |
107 dbus::ObjectPath object_path( | 131 dbus::ObjectPath object_path( |
108 characteristic_path.value() + "/" + | 132 characteristic_path.value() + "/" + |
109 kClientCharacteristicConfigurationPathComponent); | 133 kClientCharacteristicConfigurationPathComponent); |
110 DCHECK(object_path.IsValid()); | 134 DCHECK(object_path.IsValid()); |
111 PropertiesMap::const_iterator iter = properties_.find(object_path); | 135 PropertiesMap::const_iterator iter = properties_.find(object_path); |
112 if (iter != properties_.end()) { | 136 if (iter != properties_.end()) { |
113 VLOG(1) << "Descriptor already exposed: " << object_path.value(); | 137 VLOG(1) << "Descriptor already exposed: " << object_path.value(); |
114 return dbus::ObjectPath(); | 138 return dbus::ObjectPath(); |
115 } | 139 } |
116 | 140 |
117 Properties* properties = new Properties(base::Bind( | 141 Properties* properties = new Properties(base::Bind( |
118 &FakeBluetoothGattDescriptorClient::OnPropertyChanged, | 142 &FakeBluetoothGattDescriptorClient::OnPropertyChanged, |
119 weak_ptr_factory_.GetWeakPtr(), | 143 weak_ptr_factory_.GetWeakPtr(), |
120 object_path)); | 144 object_path)); |
121 properties_[object_path] = properties; | |
122 properties->uuid.ReplaceValue(uuid); | 145 properties->uuid.ReplaceValue(uuid); |
123 properties->characteristic.ReplaceValue(characteristic_path); | 146 properties->characteristic.ReplaceValue(characteristic_path); |
124 | 147 |
125 std::vector<uint8> value; | 148 DescriptorData* data = new DescriptorData(); |
126 value.push_back(0); // Notifications/Indications disabled. | 149 data->properties.reset(properties); |
127 value.push_back(0); | 150 |
128 properties->value.ReplaceValue(value); | 151 data->value.push_back(1); // Notifications enabled. |
| 152 data->value.push_back(0); |
| 153 |
| 154 properties_[object_path] = data; |
129 | 155 |
130 NotifyDescriptorAdded(object_path); | 156 NotifyDescriptorAdded(object_path); |
131 | 157 |
132 return object_path; | 158 return object_path; |
133 } | 159 } |
134 | 160 |
135 void FakeBluetoothGattDescriptorClient::HideDescriptor( | 161 void FakeBluetoothGattDescriptorClient::HideDescriptor( |
136 const dbus::ObjectPath& descriptor_path) { | 162 const dbus::ObjectPath& descriptor_path) { |
137 PropertiesMap::iterator iter = properties_.find(descriptor_path); | 163 PropertiesMap::iterator iter = properties_.find(descriptor_path); |
138 if (iter == properties_.end()) { | 164 if (iter == properties_.end()) { |
139 VLOG(1) << "Descriptor not exposed: " << descriptor_path.value(); | 165 VLOG(1) << "Descriptor not exposed: " << descriptor_path.value(); |
140 return; | 166 return; |
141 } | 167 } |
142 | 168 |
143 NotifyDescriptorRemoved(descriptor_path); | 169 NotifyDescriptorRemoved(descriptor_path); |
144 | 170 |
145 delete iter->second; | 171 delete iter->second; |
146 properties_.erase(iter); | 172 properties_.erase(iter); |
147 } | 173 } |
148 | 174 |
149 void FakeBluetoothGattDescriptorClient::OnPropertyChanged( | 175 void FakeBluetoothGattDescriptorClient::OnPropertyChanged( |
150 const dbus::ObjectPath& object_path, | 176 const dbus::ObjectPath& object_path, |
151 const std::string& property_name) { | 177 const std::string& property_name) { |
152 VLOG(2) << "Descriptor property changed: " << object_path.value() | 178 VLOG(2) << "Descriptor property changed: " << object_path.value() |
153 << ": " << property_name; | 179 << ": " << property_name; |
154 | 180 |
155 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 181 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
156 GattDescriptorPropertyChanged(object_path, property_name)); | 182 GattDescriptorPropertyChanged(object_path, property_name)); |
157 | |
158 // TODO(armansito): Implement CCC behavior (enable/disable notifications | |
159 // or indications characteristics). | |
160 } | 183 } |
161 | 184 |
162 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded( | 185 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded( |
163 const dbus::ObjectPath& object_path) { | 186 const dbus::ObjectPath& object_path) { |
164 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 187 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
165 GattDescriptorAdded(object_path)); | 188 GattDescriptorAdded(object_path)); |
166 } | 189 } |
167 | 190 |
168 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved( | 191 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved( |
169 const dbus::ObjectPath& object_path) { | 192 const dbus::ObjectPath& object_path) { |
170 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 193 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
171 GattDescriptorRemoved(object_path)); | 194 GattDescriptorRemoved(object_path)); |
172 } | 195 } |
173 | 196 |
174 } // namespace chromeos | 197 } // namespace chromeos |
OLD | NEW |