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 "device/bluetooth/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 "chromeos/dbus/bluetooth_gatt_characteristic_client.h" | 9 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h" |
10 #include "chromeos/dbus/dbus_thread_manager.h" | 10 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
11 #include "third_party/cros_system_api/dbus/service_constants.h" | 11 #include "third_party/cros_system_api/dbus/service_constants.h" |
12 | 12 |
13 namespace chromeos { | 13 namespace bluez { |
14 | 14 |
15 const char FakeBluetoothGattDescriptorClient:: | 15 const char FakeBluetoothGattDescriptorClient:: |
16 kClientCharacteristicConfigurationPathComponent[] = "desc0000"; | 16 kClientCharacteristicConfigurationPathComponent[] = "desc0000"; |
17 const char FakeBluetoothGattDescriptorClient:: | 17 const char FakeBluetoothGattDescriptorClient:: |
18 kClientCharacteristicConfigurationUUID[] = | 18 kClientCharacteristicConfigurationUUID[] = |
19 "00002902-0000-1000-8000-00805f9b34fb"; | 19 "00002902-0000-1000-8000-00805f9b34fb"; |
20 | 20 |
21 FakeBluetoothGattDescriptorClient::Properties::Properties( | 21 FakeBluetoothGattDescriptorClient::Properties::Properties( |
22 const PropertyChangedCallback& callback) | 22 const PropertyChangedCallback& callback) |
23 : BluetoothGattDescriptorClient::Properties( | 23 : BluetoothGattDescriptorClient::Properties( |
24 NULL, | 24 NULL, |
25 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, | 25 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, |
26 callback) { | 26 callback) {} |
27 } | |
28 | 27 |
29 FakeBluetoothGattDescriptorClient::Properties::~Properties() { | 28 FakeBluetoothGattDescriptorClient::Properties::~Properties() {} |
30 } | |
31 | 29 |
32 void FakeBluetoothGattDescriptorClient::Properties::Get( | 30 void FakeBluetoothGattDescriptorClient::Properties::Get( |
33 dbus::PropertyBase* property, | 31 dbus::PropertyBase* property, |
34 dbus::PropertySet::GetCallback callback) { | 32 dbus::PropertySet::GetCallback callback) { |
35 VLOG(1) << "Get " << property->name(); | 33 VLOG(1) << "Get " << property->name(); |
36 callback.Run(true); | 34 callback.Run(true); |
37 } | 35 } |
38 | 36 |
39 void FakeBluetoothGattDescriptorClient::Properties::GetAll() { | 37 void FakeBluetoothGattDescriptorClient::Properties::GetAll() { |
40 VLOG(1) << "GetAll"; | 38 VLOG(1) << "GetAll"; |
41 } | 39 } |
42 | 40 |
43 void FakeBluetoothGattDescriptorClient::Properties::Set( | 41 void FakeBluetoothGattDescriptorClient::Properties::Set( |
44 dbus::PropertyBase* property, | 42 dbus::PropertyBase* property, |
45 dbus::PropertySet::SetCallback callback) { | 43 dbus::PropertySet::SetCallback callback) { |
46 VLOG(1) << "Set " << property->name(); | 44 VLOG(1) << "Set " << property->name(); |
47 callback.Run(false); | 45 callback.Run(false); |
48 } | 46 } |
49 | 47 |
50 FakeBluetoothGattDescriptorClient::DescriptorData::DescriptorData() { | 48 FakeBluetoothGattDescriptorClient::DescriptorData::DescriptorData() {} |
51 } | |
52 | 49 |
53 FakeBluetoothGattDescriptorClient::DescriptorData::~DescriptorData() { | 50 FakeBluetoothGattDescriptorClient::DescriptorData::~DescriptorData() {} |
54 } | |
55 | 51 |
56 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient() | 52 FakeBluetoothGattDescriptorClient::FakeBluetoothGattDescriptorClient() |
57 : weak_ptr_factory_(this) { | 53 : weak_ptr_factory_(this) {} |
58 } | |
59 | 54 |
60 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() { | 55 FakeBluetoothGattDescriptorClient::~FakeBluetoothGattDescriptorClient() { |
61 for(PropertiesMap::iterator iter = properties_.begin(); iter != | 56 for (PropertiesMap::iterator iter = properties_.begin(); |
62 properties_.end(); iter++) | 57 iter != properties_.end(); iter++) |
63 delete iter->second; | 58 delete iter->second; |
64 } | 59 } |
65 | 60 |
66 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) { | 61 void FakeBluetoothGattDescriptorClient::Init(dbus::Bus* bus) {} |
67 } | |
68 | 62 |
69 void FakeBluetoothGattDescriptorClient::AddObserver(Observer* observer) { | 63 void FakeBluetoothGattDescriptorClient::AddObserver(Observer* observer) { |
70 observers_.AddObserver(observer); | 64 observers_.AddObserver(observer); |
71 } | 65 } |
72 | 66 |
73 void FakeBluetoothGattDescriptorClient::RemoveObserver(Observer* observer) { | 67 void FakeBluetoothGattDescriptorClient::RemoveObserver(Observer* observer) { |
74 observers_.RemoveObserver(observer); | 68 observers_.RemoveObserver(observer); |
75 } | 69 } |
76 | 70 |
77 std::vector<dbus::ObjectPath> | 71 std::vector<dbus::ObjectPath> |
(...skipping 22 matching lines...) Expand all Loading... |
100 PropertiesMap::iterator iter = properties_.find(object_path); | 94 PropertiesMap::iterator iter = properties_.find(object_path); |
101 if (iter == properties_.end()) { | 95 if (iter == properties_.end()) { |
102 error_callback.Run(kUnknownDescriptorError, ""); | 96 error_callback.Run(kUnknownDescriptorError, ""); |
103 return; | 97 return; |
104 } | 98 } |
105 | 99 |
106 // Assign the value of the descriptor as necessary | 100 // Assign the value of the descriptor as necessary |
107 Properties* properties = iter->second->properties.get(); | 101 Properties* properties = iter->second->properties.get(); |
108 if (properties->uuid.value() == kClientCharacteristicConfigurationUUID) { | 102 if (properties->uuid.value() == kClientCharacteristicConfigurationUUID) { |
109 BluetoothGattCharacteristicClient::Properties* chrc_props = | 103 BluetoothGattCharacteristicClient::Properties* chrc_props = |
110 DBusThreadManager::Get() | 104 bluez::BluezDBusManager::Get() |
111 ->GetBluetoothGattCharacteristicClient() | 105 ->GetBluetoothGattCharacteristicClient() |
112 ->GetProperties(properties->characteristic.value()); | 106 ->GetProperties(properties->characteristic.value()); |
113 DCHECK(chrc_props); | 107 DCHECK(chrc_props); |
114 | 108 |
115 uint8_t value_byte = chrc_props->notifying.value() ? 0x01 : 0x00; | 109 uint8_t value_byte = chrc_props->notifying.value() ? 0x01 : 0x00; |
116 const std::vector<uint8_t>& cur_value = properties->value.value(); | 110 const std::vector<uint8_t>& cur_value = properties->value.value(); |
117 | 111 |
118 if (!cur_value.size() || cur_value[0] != value_byte) { | 112 if (!cur_value.size() || cur_value[0] != value_byte) { |
119 std::vector<uint8_t> value = {value_byte, 0x00}; | 113 std::vector<uint8_t> value = {value_byte, 0x00}; |
120 properties->value.ReplaceValue(value); | 114 properties->value.ReplaceValue(value); |
(...skipping 23 matching lines...) Expand all Loading... |
144 dbus::ObjectPath FakeBluetoothGattDescriptorClient::ExposeDescriptor( | 138 dbus::ObjectPath FakeBluetoothGattDescriptorClient::ExposeDescriptor( |
145 const dbus::ObjectPath& characteristic_path, | 139 const dbus::ObjectPath& characteristic_path, |
146 const std::string& uuid) { | 140 const std::string& uuid) { |
147 if (uuid != kClientCharacteristicConfigurationUUID) { | 141 if (uuid != kClientCharacteristicConfigurationUUID) { |
148 VLOG(2) << "Unsupported UUID: " << uuid; | 142 VLOG(2) << "Unsupported UUID: " << uuid; |
149 return dbus::ObjectPath(); | 143 return dbus::ObjectPath(); |
150 } | 144 } |
151 | 145 |
152 // CCC descriptor is the only one supported at the moment. | 146 // CCC descriptor is the only one supported at the moment. |
153 DCHECK(characteristic_path.IsValid()); | 147 DCHECK(characteristic_path.IsValid()); |
154 dbus::ObjectPath object_path( | 148 dbus::ObjectPath object_path(characteristic_path.value() + "/" + |
155 characteristic_path.value() + "/" + | 149 kClientCharacteristicConfigurationPathComponent); |
156 kClientCharacteristicConfigurationPathComponent); | |
157 DCHECK(object_path.IsValid()); | 150 DCHECK(object_path.IsValid()); |
158 PropertiesMap::const_iterator iter = properties_.find(object_path); | 151 PropertiesMap::const_iterator iter = properties_.find(object_path); |
159 if (iter != properties_.end()) { | 152 if (iter != properties_.end()) { |
160 VLOG(1) << "Descriptor already exposed: " << object_path.value(); | 153 VLOG(1) << "Descriptor already exposed: " << object_path.value(); |
161 return dbus::ObjectPath(); | 154 return dbus::ObjectPath(); |
162 } | 155 } |
163 | 156 |
164 Properties* properties = new Properties(base::Bind( | 157 Properties* properties = new Properties( |
165 &FakeBluetoothGattDescriptorClient::OnPropertyChanged, | 158 base::Bind(&FakeBluetoothGattDescriptorClient::OnPropertyChanged, |
166 weak_ptr_factory_.GetWeakPtr(), | 159 weak_ptr_factory_.GetWeakPtr(), object_path)); |
167 object_path)); | |
168 properties->uuid.ReplaceValue(uuid); | 160 properties->uuid.ReplaceValue(uuid); |
169 properties->characteristic.ReplaceValue(characteristic_path); | 161 properties->characteristic.ReplaceValue(characteristic_path); |
170 | 162 |
171 DescriptorData* data = new DescriptorData(); | 163 DescriptorData* data = new DescriptorData(); |
172 data->properties.reset(properties); | 164 data->properties.reset(properties); |
173 | 165 |
174 properties_[object_path] = data; | 166 properties_[object_path] = data; |
175 | 167 |
176 NotifyDescriptorAdded(object_path); | 168 NotifyDescriptorAdded(object_path); |
177 | 169 |
(...skipping 10 matching lines...) Expand all Loading... |
188 | 180 |
189 NotifyDescriptorRemoved(descriptor_path); | 181 NotifyDescriptorRemoved(descriptor_path); |
190 | 182 |
191 delete iter->second; | 183 delete iter->second; |
192 properties_.erase(iter); | 184 properties_.erase(iter); |
193 } | 185 } |
194 | 186 |
195 void FakeBluetoothGattDescriptorClient::OnPropertyChanged( | 187 void FakeBluetoothGattDescriptorClient::OnPropertyChanged( |
196 const dbus::ObjectPath& object_path, | 188 const dbus::ObjectPath& object_path, |
197 const std::string& property_name) { | 189 const std::string& property_name) { |
198 VLOG(2) << "Descriptor property changed: " << object_path.value() | 190 VLOG(2) << "Descriptor property changed: " << object_path.value() << ": " |
199 << ": " << property_name; | 191 << property_name; |
200 | 192 |
201 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 193 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
202 GattDescriptorPropertyChanged(object_path, property_name)); | 194 GattDescriptorPropertyChanged(object_path, property_name)); |
203 } | 195 } |
204 | 196 |
205 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded( | 197 void FakeBluetoothGattDescriptorClient::NotifyDescriptorAdded( |
206 const dbus::ObjectPath& object_path) { | 198 const dbus::ObjectPath& object_path) { |
207 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 199 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
208 GattDescriptorAdded(object_path)); | 200 GattDescriptorAdded(object_path)); |
209 } | 201 } |
210 | 202 |
211 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved( | 203 void FakeBluetoothGattDescriptorClient::NotifyDescriptorRemoved( |
212 const dbus::ObjectPath& object_path) { | 204 const dbus::ObjectPath& object_path) { |
213 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 205 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
214 GattDescriptorRemoved(object_path)); | 206 GattDescriptorRemoved(object_path)); |
215 } | 207 } |
216 | 208 |
217 } // namespace chromeos | 209 } // namespace bluez |
OLD | NEW |