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 "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h" | |
keybuk
2014/05/30 22:22:40
this seems to be still needed given the code below
armansito
2014/05/30 22:37:20
It moved to the header.
| |
10 #include "chromeos/dbus/dbus_thread_manager.h" | 9 #include "chromeos/dbus/dbus_thread_manager.h" |
11 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" | 10 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" |
12 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" | 11 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
13 | 12 |
14 namespace chromeos { | 13 namespace chromeos { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 // Stream operator for logging vector<uint8>. | 17 // Stream operator for logging vector<uint8>. |
19 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { | 18 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { |
20 out << "["; | 19 out << "["; |
21 for (std::vector<uint8>::const_iterator iter = bytes.begin(); | 20 for (std::vector<uint8>::const_iterator iter = bytes.begin(); |
22 iter != bytes.end(); ++iter) { | 21 iter != bytes.end(); ++iter) { |
23 out << base::StringPrintf("%02X", *iter); | 22 out << base::StringPrintf("%02X", *iter); |
24 } | 23 } |
25 return out << "]"; | 24 return out << "]"; |
26 } | 25 } |
27 | 26 |
28 } // namespace | 27 } // namespace |
29 | 28 |
30 BluetoothRemoteGattCharacteristicChromeOS:: | 29 BluetoothRemoteGattCharacteristicChromeOS:: |
31 BluetoothRemoteGattCharacteristicChromeOS( | 30 BluetoothRemoteGattCharacteristicChromeOS( |
32 BluetoothRemoteGattServiceChromeOS* service, | 31 BluetoothRemoteGattServiceChromeOS* service, |
33 const dbus::ObjectPath& object_path) | 32 const dbus::ObjectPath& object_path) |
34 : object_path_(object_path), | 33 : object_path_(object_path), |
35 service_(service), | 34 service_(service), |
36 weak_ptr_factory_(this) { | 35 weak_ptr_factory_(this) { |
37 VLOG(1) << "Creating remote GATT characteristic with identifier: " | 36 VLOG(1) << "Creating remote GATT characteristic with identifier: " |
38 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 37 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
38 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | |
39 AddObserver(this); | |
39 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 40 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> |
40 AddObserver(this); | 41 AddObserver(this); |
41 | 42 |
42 // Add all known GATT characteristic descriptors. | 43 // Add all known GATT characteristic descriptors. |
43 const std::vector<dbus::ObjectPath>& gatt_descs = | 44 const std::vector<dbus::ObjectPath>& gatt_descs = |
44 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 45 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> |
45 GetDescriptors(); | 46 GetDescriptors(); |
46 for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_descs.begin(); | 47 for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_descs.begin(); |
47 iter != gatt_descs.end(); ++iter) | 48 iter != gatt_descs.end(); ++iter) |
48 GattDescriptorAdded(*iter); | 49 GattDescriptorAdded(*iter); |
49 } | 50 } |
50 | 51 |
51 BluetoothRemoteGattCharacteristicChromeOS:: | 52 BluetoothRemoteGattCharacteristicChromeOS:: |
52 ~BluetoothRemoteGattCharacteristicChromeOS() { | 53 ~BluetoothRemoteGattCharacteristicChromeOS() { |
53 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 54 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> |
54 RemoveObserver(this); | 55 RemoveObserver(this); |
56 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | |
57 RemoveObserver(this); | |
55 | 58 |
56 // Clean up all the descriptors. There isn't much point in notifying service | 59 // Clean up all the descriptors. There isn't much point in notifying service |
57 // observers for each descriptor that gets removed, so just delete them. | 60 // observers for each descriptor that gets removed, so just delete them. |
58 for (DescriptorMap::iterator iter = descriptors_.begin(); | 61 for (DescriptorMap::iterator iter = descriptors_.begin(); |
59 iter != descriptors_.end(); ++iter) | 62 iter != descriptors_.end(); ++iter) |
60 delete iter->second; | 63 delete iter->second; |
61 } | 64 } |
62 | 65 |
63 std::string BluetoothRemoteGattCharacteristicChromeOS::GetIdentifier() const { | 66 std::string BluetoothRemoteGattCharacteristicChromeOS::GetIdentifier() const { |
64 return object_path_.value(); | 67 return object_path_.value(); |
65 } | 68 } |
66 | 69 |
67 device::BluetoothUUID | 70 device::BluetoothUUID |
68 BluetoothRemoteGattCharacteristicChromeOS::GetUUID() const { | 71 BluetoothRemoteGattCharacteristicChromeOS::GetUUID() const { |
69 BluetoothGattCharacteristicClient::Properties* properties = | 72 BluetoothGattCharacteristicClient::Properties* properties = |
70 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 73 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
71 GetProperties(object_path_); | 74 GetProperties(object_path_); |
72 DCHECK(properties); | 75 DCHECK(properties); |
73 return device::BluetoothUUID(properties->uuid.value()); | 76 return device::BluetoothUUID(properties->uuid.value()); |
74 } | 77 } |
75 | 78 |
76 bool BluetoothRemoteGattCharacteristicChromeOS::IsLocal() const { | 79 bool BluetoothRemoteGattCharacteristicChromeOS::IsLocal() const { |
77 return false; | 80 return false; |
78 } | 81 } |
79 | 82 |
80 const std::vector<uint8>& | 83 const std::vector<uint8>& |
81 BluetoothRemoteGattCharacteristicChromeOS::GetValue() const { | 84 BluetoothRemoteGattCharacteristicChromeOS::GetValue() const { |
82 BluetoothGattCharacteristicClient::Properties* properties = | 85 return cached_value_; |
83 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | |
84 GetProperties(object_path_); | |
85 DCHECK(properties); | |
86 return properties->value.value(); | |
87 } | 86 } |
88 | 87 |
89 device::BluetoothGattService* | 88 device::BluetoothGattService* |
90 BluetoothRemoteGattCharacteristicChromeOS::GetService() const { | 89 BluetoothRemoteGattCharacteristicChromeOS::GetService() const { |
91 return service_; | 90 return service_; |
92 } | 91 } |
93 | 92 |
94 device::BluetoothGattCharacteristic::Properties | 93 device::BluetoothGattCharacteristic::Properties |
95 BluetoothRemoteGattCharacteristicChromeOS::GetProperties() const { | 94 BluetoothRemoteGattCharacteristicChromeOS::GetProperties() const { |
96 // TODO(armansito): Once BlueZ implements properties properly, return those | 95 // TODO(armansito): Once BlueZ implements properties properly, return those |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 VLOG(1) << "Cannot update the value of a remote GATT characteristic."; | 134 VLOG(1) << "Cannot update the value of a remote GATT characteristic."; |
136 return false; | 135 return false; |
137 } | 136 } |
138 | 137 |
139 void BluetoothRemoteGattCharacteristicChromeOS::ReadRemoteCharacteristic( | 138 void BluetoothRemoteGattCharacteristicChromeOS::ReadRemoteCharacteristic( |
140 const ValueCallback& callback, | 139 const ValueCallback& callback, |
141 const ErrorCallback& error_callback) { | 140 const ErrorCallback& error_callback) { |
142 VLOG(1) << "Sending GATT characteristic read request to characteristic: " | 141 VLOG(1) << "Sending GATT characteristic read request to characteristic: " |
143 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | 142 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
144 << "."; | 143 << "."; |
145 BluetoothGattCharacteristicClient::Properties* properties = | 144 |
146 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 145 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()->ReadValue( |
147 GetProperties(object_path_); | 146 object_path_, |
148 DCHECK(properties); | 147 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnValueSuccess, |
149 properties->value.Get( | |
150 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnGetValue, | |
151 weak_ptr_factory_.GetWeakPtr(), | 148 weak_ptr_factory_.GetWeakPtr(), |
152 callback, error_callback)); | 149 callback), |
150 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnError, | |
151 weak_ptr_factory_.GetWeakPtr(), | |
152 error_callback)); | |
153 } | 153 } |
154 | 154 |
155 void BluetoothRemoteGattCharacteristicChromeOS::WriteRemoteCharacteristic( | 155 void BluetoothRemoteGattCharacteristicChromeOS::WriteRemoteCharacteristic( |
156 const std::vector<uint8>& new_value, | 156 const std::vector<uint8>& new_value, |
157 const base::Closure& callback, | 157 const base::Closure& callback, |
158 const ErrorCallback& error_callback) { | 158 const ErrorCallback& error_callback) { |
159 VLOG(1) << "Sending GATT characteristic write request to characteristic: " | 159 VLOG(1) << "Sending GATT characteristic write request to characteristic: " |
160 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | 160 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
161 << ", with value: " << new_value << "."; | 161 << ", with value: " << new_value << "."; |
162 | 162 |
163 // Permission and bonding are handled by BlueZ so no need check it here. | 163 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()->WriteValue( |
164 if (new_value.empty()) { | 164 object_path_, |
165 VLOG(1) << "Nothing to write."; | 165 new_value, |
166 error_callback.Run(); | 166 callback, |
167 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnError, | |
168 weak_ptr_factory_.GetWeakPtr(), | |
169 error_callback)); | |
170 } | |
171 | |
172 void BluetoothRemoteGattCharacteristicChromeOS::GattCharacteristicValueUpdated( | |
173 const dbus::ObjectPath& object_path, | |
174 const std::vector<uint8>& value) { | |
175 if (object_path != object_path_) | |
167 return; | 176 return; |
168 } | |
169 | 177 |
170 BluetoothGattCharacteristicClient::Properties* properties = | 178 cached_value_ = value; |
171 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 179 |
172 GetProperties(object_path_); | 180 VLOG(1) << "GATT characteristic value has changed: " << object_path.value() |
173 DCHECK(properties); | 181 << ": " << value; |
174 properties->value.Set( | 182 DCHECK(service_); |
175 new_value, | 183 service_->NotifyCharacteristicValueChanged(this, value); |
176 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnSetValue, | |
177 weak_ptr_factory_.GetWeakPtr(), | |
178 callback, error_callback)); | |
179 } | 184 } |
180 | 185 |
181 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorAdded( | 186 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorAdded( |
182 const dbus::ObjectPath& object_path) { | 187 const dbus::ObjectPath& object_path) { |
183 if (descriptors_.find(object_path) != descriptors_.end()) { | 188 if (descriptors_.find(object_path) != descriptors_.end()) { |
184 VLOG(1) << "Remote GATT characteristic descriptor already exists: " | 189 VLOG(1) << "Remote GATT characteristic descriptor already exists: " |
185 << object_path.value(); | 190 << object_path.value(); |
186 return; | 191 return; |
187 } | 192 } |
188 | 193 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 | 254 |
250 VLOG(1) << "GATT descriptor property changed: " << object_path.value() | 255 VLOG(1) << "GATT descriptor property changed: " << object_path.value() |
251 << ", property: " << property_name; | 256 << ", property: " << property_name; |
252 | 257 |
253 DCHECK(service_); | 258 DCHECK(service_); |
254 | 259 |
255 service_->NotifyDescriptorValueChanged( | 260 service_->NotifyDescriptorValueChanged( |
256 this, iter->second, properties->value.value()); | 261 this, iter->second, properties->value.value()); |
257 } | 262 } |
258 | 263 |
259 void BluetoothRemoteGattCharacteristicChromeOS::OnGetValue( | 264 void BluetoothRemoteGattCharacteristicChromeOS::OnValueSuccess( |
260 const ValueCallback& callback, | 265 const ValueCallback& callback, |
261 const ErrorCallback& error_callback, | 266 const std::vector<uint8>& value) { |
262 bool success) { | 267 VLOG(1) << "Characteristic value read: " << value; |
263 if (!success) { | 268 cached_value_ = value; |
264 VLOG(1) << "Failed to read the value from the remote characteristic."; | |
265 error_callback.Run(); | |
266 return; | |
267 } | |
268 | 269 |
269 VLOG(1) << "Read value of remote characteristic."; | 270 DCHECK(service_); |
270 BluetoothGattCharacteristicClient::Properties* properties = | 271 service_->NotifyCharacteristicValueChanged(this, cached_value_); |
271 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 272 |
272 GetProperties(object_path_); | 273 callback.Run(value); |
273 DCHECK(properties); | |
274 callback.Run(properties->value.value()); | |
275 } | 274 } |
276 | 275 |
277 void BluetoothRemoteGattCharacteristicChromeOS::OnSetValue( | 276 void BluetoothRemoteGattCharacteristicChromeOS::OnError( |
278 const base::Closure& callback, | |
279 const ErrorCallback& error_callback, | 277 const ErrorCallback& error_callback, |
280 bool success) { | 278 const std::string& error_name, |
281 if (!success) { | 279 const std::string& error_message) { |
282 VLOG(1) << "Failed to write the value of remote characteristic."; | 280 VLOG(1) << "Operation failed: " << error_name << ", message: " |
283 error_callback.Run(); | 281 << error_message; |
284 return; | 282 error_callback.Run(); |
285 } | |
286 | |
287 VLOG(1) << "Wrote value of remote characteristic."; | |
288 callback.Run(); | |
289 } | 283 } |
290 | 284 |
291 } // namespace chromeos | 285 } // namespace chromeos |
OLD | NEW |