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); | |
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 } | 46 } |
59 | 47 |
60 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient() | 48 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient() |
61 : weak_ptr_factory_(this) { | 49 : weak_ptr_factory_(this) { |
62 } | 50 } |
63 | 51 |
64 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() { | 52 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() { |
65 } | 53 } |
66 | 54 |
67 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) { | 55 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) { |
(...skipping 16 matching lines...) Expand all Loading... |
84 } | 72 } |
85 return descriptors; | 73 return descriptors; |
86 } | 74 } |
87 | 75 |
88 FakeBluetoothGattDescriptorClient::Properties* | 76 FakeBluetoothGattDescriptorClient::Properties* |
89 FakeBluetoothGattDescriptorClient::GetProperties( | 77 FakeBluetoothGattDescriptorClient::GetProperties( |
90 const dbus::ObjectPath& object_path) { | 78 const dbus::ObjectPath& object_path) { |
91 PropertiesMap::const_iterator iter = properties_.find(object_path); | 79 PropertiesMap::const_iterator iter = properties_.find(object_path); |
92 if (iter == properties_.end()) | 80 if (iter == properties_.end()) |
93 return NULL; | 81 return NULL; |
94 return iter->second; | 82 return iter->second->properties.get(); |
| 83 } |
| 84 |
| 85 void FakeBluetoothGattDescriptorClient::ReadValue( |
| 86 const dbus::ObjectPath& object_path, |
| 87 const ValueCallback& callback, |
| 88 const ErrorCallback& error_callback) { |
| 89 PropertiesMap::iterator iter = properties_.find(object_path); |
| 90 if (iter == properties_.end()) { |
| 91 error_callback.Run(kUnknownDescriptorError, ""); |
| 92 return; |
| 93 } |
| 94 |
| 95 callback.Run(iter->second->value); |
| 96 } |
| 97 |
| 98 void FakeBluetoothGattDescriptorClient::WriteValue( |
| 99 const dbus::ObjectPath& object_path, |
| 100 const std::vector<uint8>& value, |
| 101 const base::Closure& callback, |
| 102 const ErrorCallback& error_callback) { |
| 103 if (properties_.find(object_path) == properties_.end()) { |
| 104 error_callback.Run(kUnknownDescriptorError, ""); |
| 105 return; |
| 106 } |
| 107 |
| 108 // Since the only fake descriptor is "Client Characteristic Configuration" |
| 109 // and BlueZ doesn't allow writing to it, return failure. |
| 110 error_callback.Run("org.bluez.Error.Failed", |
| 111 "Writing to the Client Characteristic Configuration " |
| 112 "descriptor not allowed"); |
95 } | 113 } |
96 | 114 |
97 dbus::ObjectPath FakeBluetoothGattDescriptorClient::ExposeDescriptor( | 115 dbus::ObjectPath FakeBluetoothGattDescriptorClient::ExposeDescriptor( |
98 const dbus::ObjectPath& characteristic_path, | 116 const dbus::ObjectPath& characteristic_path, |
99 const std::string& uuid) { | 117 const std::string& uuid) { |
100 if (uuid != kClientCharacteristicConfigurationUUID) { | 118 if (uuid != kClientCharacteristicConfigurationUUID) { |
101 VLOG(2) << "Unsupported UUID: " << uuid; | 119 VLOG(2) << "Unsupported UUID: " << uuid; |
102 return dbus::ObjectPath(); | 120 return dbus::ObjectPath(); |
103 } | 121 } |
104 | 122 |
105 // CCC descriptor is the only one supported at the moment. | 123 // CCC descriptor is the only one supported at the moment. |
106 DCHECK(characteristic_path.IsValid()); | 124 DCHECK(characteristic_path.IsValid()); |
107 dbus::ObjectPath object_path( | 125 dbus::ObjectPath object_path( |
108 characteristic_path.value() + "/" + | 126 characteristic_path.value() + "/" + |
109 kClientCharacteristicConfigurationPathComponent); | 127 kClientCharacteristicConfigurationPathComponent); |
110 DCHECK(object_path.IsValid()); | 128 DCHECK(object_path.IsValid()); |
111 PropertiesMap::const_iterator iter = properties_.find(object_path); | 129 PropertiesMap::const_iterator iter = properties_.find(object_path); |
112 if (iter != properties_.end()) { | 130 if (iter != properties_.end()) { |
113 VLOG(1) << "Descriptor already exposed: " << object_path.value(); | 131 VLOG(1) << "Descriptor already exposed: " << object_path.value(); |
114 return dbus::ObjectPath(); | 132 return dbus::ObjectPath(); |
115 } | 133 } |
116 | 134 |
117 Properties* properties = new Properties(base::Bind( | 135 Properties* properties = new Properties(base::Bind( |
118 &FakeBluetoothGattDescriptorClient::OnPropertyChanged, | 136 &FakeBluetoothGattDescriptorClient::OnPropertyChanged, |
119 weak_ptr_factory_.GetWeakPtr(), | 137 weak_ptr_factory_.GetWeakPtr(), |
120 object_path)); | 138 object_path)); |
121 properties_[object_path] = properties; | |
122 properties->uuid.ReplaceValue(uuid); | 139 properties->uuid.ReplaceValue(uuid); |
123 properties->characteristic.ReplaceValue(characteristic_path); | 140 properties->characteristic.ReplaceValue(characteristic_path); |
124 | 141 |
125 std::vector<uint8> value; | 142 DescriptorData* data = new DescriptorData(); |
126 value.push_back(0); // Notifications/Indications disabled. | 143 data->properties.reset(properties); |
127 value.push_back(0); | 144 |
128 properties->value.ReplaceValue(value); | 145 data->value.push_back(1); // Notifications enabled. |
| 146 data->value.push_back(0); |
| 147 |
| 148 properties_[object_path] = data; |
129 | 149 |
130 NotifyDescriptorAdded(object_path); | 150 NotifyDescriptorAdded(object_path); |
131 | 151 |
132 return object_path; | 152 return object_path; |
133 } | 153 } |
134 | 154 |
135 void FakeBluetoothGattDescriptorClient::HideDescriptor( | 155 void FakeBluetoothGattDescriptorClient::HideDescriptor( |
136 const dbus::ObjectPath& descriptor_path) { | 156 const dbus::ObjectPath& descriptor_path) { |
137 PropertiesMap::iterator iter = properties_.find(descriptor_path); | 157 PropertiesMap::iterator iter = properties_.find(descriptor_path); |
138 if (iter == properties_.end()) { | 158 if (iter == properties_.end()) { |
139 VLOG(1) << "Descriptor not exposed: " << descriptor_path.value(); | 159 VLOG(1) << "Descriptor not exposed: " << descriptor_path.value(); |
140 return; | 160 return; |
141 } | 161 } |
142 | 162 |
143 NotifyDescriptorRemoved(descriptor_path); | 163 NotifyDescriptorRemoved(descriptor_path); |
144 | 164 |
145 delete iter->second; | 165 delete iter->second; |
146 properties_.erase(iter); | 166 properties_.erase(iter); |
147 } | 167 } |
148 | 168 |
149 void FakeBluetoothGattDescriptorClient::OnPropertyChanged( | 169 void FakeBluetoothGattDescriptorClient::OnPropertyChanged( |
150 const dbus::ObjectPath& object_path, | 170 const dbus::ObjectPath& object_path, |
151 const std::string& property_name) { | 171 const std::string& property_name) { |
152 VLOG(2) << "Descriptor property changed: " << object_path.value() | 172 VLOG(2) << "Descriptor property changed: " << object_path.value() |
153 << ": " << property_name; | 173 << ": " << property_name; |
154 | 174 |
155 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 175 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
156 GattDescriptorPropertyChanged(object_path, property_name)); | 176 GattDescriptorPropertyChanged(object_path, property_name)); |
157 | |
158 // TODO(armansito): Implement CCC behavior (enable/disable notifications | |
159 // or indications characteristics). | |
160 } | 177 } |
161 | 178 |
162 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded( | 179 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded( |
163 const dbus::ObjectPath& object_path) { | 180 const dbus::ObjectPath& object_path) { |
164 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 181 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
165 GattDescriptorAdded(object_path)); | 182 GattDescriptorAdded(object_path)); |
166 } | 183 } |
167 | 184 |
168 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved( | 185 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved( |
169 const dbus::ObjectPath& object_path) { | 186 const dbus::ObjectPath& object_path) { |
170 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 187 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
171 GattDescriptorRemoved(object_path)); | 188 GattDescriptorRemoved(object_path)); |
172 } | 189 } |
173 | 190 |
174 } // namespace chromeos | 191 } // namespace chromeos |
OLD | NEW |