| 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_bluez.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h" |
| 6 | 6 |
| 7 #include <iostream> |
| 8 #include <iterator> |
| 7 #include <limits> | 9 #include <limits> |
| 8 #include <memory> | |
| 9 #include <utility> | |
| 10 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/callback_forward.h" |
| 11 #include "base/logging.h" | 14 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "dbus/property.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_bluez.h" | 17 #include "device/bluetooth/bluetooth_adapter_bluez.h" |
| 14 #include "device/bluetooth/bluetooth_device.h" | 18 #include "device/bluetooth/bluetooth_device.h" |
| 19 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 20 #include "device/bluetooth/bluetooth_gatt_descriptor_bluez.h" |
| 15 #include "device/bluetooth/bluetooth_gatt_notify_session_bluez.h" | 21 #include "device/bluetooth/bluetooth_gatt_notify_session_bluez.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h" | 22 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 17 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_bluez.h" | |
| 18 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h" | 23 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h" |
| 24 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h" |
| 19 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 25 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" | 26 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 21 | 27 |
| 22 namespace bluez { | 28 namespace bluez { |
| 23 | 29 |
| 24 namespace { | 30 namespace { |
| 25 | 31 |
| 26 // Stream operator for logging vector<uint8_t>. | 32 // Stream operator for logging vector<uint8_t>. |
| 27 std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t> bytes) { | 33 std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t> bytes) { |
| 28 out << "["; | 34 out << "["; |
| 29 for (std::vector<uint8_t>::const_iterator iter = bytes.begin(); | 35 for (std::vector<uint8_t>::const_iterator iter = bytes.begin(); |
| 30 iter != bytes.end(); ++iter) { | 36 iter != bytes.end(); ++iter) { |
| 31 out << base::StringPrintf("%02X", *iter); | 37 out << base::StringPrintf("%02X", *iter); |
| 32 } | 38 } |
| 33 return out << "]"; | 39 return out << "]"; |
| 34 } | 40 } |
| 35 | 41 |
| 36 } // namespace | 42 } // namespace |
| 37 | 43 |
| 38 BluetoothRemoteGattCharacteristicBlueZ::BluetoothRemoteGattCharacteristicBlueZ( | 44 BluetoothRemoteGattCharacteristicBlueZ::BluetoothRemoteGattCharacteristicBlueZ( |
| 39 BluetoothRemoteGattServiceBlueZ* service, | 45 BluetoothRemoteGattServiceBlueZ* service, |
| 40 const dbus::ObjectPath& object_path) | 46 const dbus::ObjectPath& object_path) |
| 41 : object_path_(object_path), | 47 : BluetoothGattCharacteristicBlueZ(service, object_path), |
| 42 service_(service), | |
| 43 num_notify_sessions_(0), | 48 num_notify_sessions_(0), |
| 44 notify_call_pending_(false), | 49 notify_call_pending_(false), |
| 45 weak_ptr_factory_(this) { | 50 weak_ptr_factory_(this) { |
| 46 VLOG(1) << "Creating remote GATT characteristic with identifier: " | 51 VLOG(1) << "Creating remote GATT characteristic with identifier: " |
| 47 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 52 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 48 bluez::BluezDBusManager::Get() | 53 bluez::BluezDBusManager::Get() |
| 49 ->GetBluetoothGattDescriptorClient() | 54 ->GetBluetoothGattDescriptorClient() |
| 50 ->AddObserver(this); | 55 ->AddObserver(this); |
| 51 | 56 |
| 52 // Add all known GATT characteristic descriptors. | 57 // Add all known GATT characteristic descriptors. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 72 delete iter->second; | 77 delete iter->second; |
| 73 | 78 |
| 74 // Report an error for all pending calls to StartNotifySession. | 79 // Report an error for all pending calls to StartNotifySession. |
| 75 while (!pending_start_notify_calls_.empty()) { | 80 while (!pending_start_notify_calls_.empty()) { |
| 76 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | 81 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); |
| 77 pending_start_notify_calls_.pop(); | 82 pending_start_notify_calls_.pop(); |
| 78 callbacks.second.Run(device::BluetoothGattService::GATT_ERROR_FAILED); | 83 callbacks.second.Run(device::BluetoothGattService::GATT_ERROR_FAILED); |
| 79 } | 84 } |
| 80 } | 85 } |
| 81 | 86 |
| 82 std::string BluetoothRemoteGattCharacteristicBlueZ::GetIdentifier() const { | |
| 83 return object_path_.value(); | |
| 84 } | |
| 85 | |
| 86 device::BluetoothUUID BluetoothRemoteGattCharacteristicBlueZ::GetUUID() const { | 87 device::BluetoothUUID BluetoothRemoteGattCharacteristicBlueZ::GetUUID() const { |
| 87 bluez::BluetoothGattCharacteristicClient::Properties* properties = | 88 bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| 88 bluez::BluezDBusManager::Get() | 89 bluez::BluezDBusManager::Get() |
| 89 ->GetBluetoothGattCharacteristicClient() | 90 ->GetBluetoothGattCharacteristicClient() |
| 90 ->GetProperties(object_path_); | 91 ->GetProperties(object_path()); |
| 91 DCHECK(properties); | 92 DCHECK(properties); |
| 92 return device::BluetoothUUID(properties->uuid.value()); | 93 return device::BluetoothUUID(properties->uuid.value()); |
| 93 } | 94 } |
| 94 | 95 |
| 95 bool BluetoothRemoteGattCharacteristicBlueZ::IsLocal() const { | 96 bool BluetoothRemoteGattCharacteristicBlueZ::IsLocal() const { |
| 96 return false; | 97 return false; |
| 97 } | 98 } |
| 98 | 99 |
| 99 const std::vector<uint8_t>& BluetoothRemoteGattCharacteristicBlueZ::GetValue() | 100 const std::vector<uint8_t>& BluetoothRemoteGattCharacteristicBlueZ::GetValue() |
| 100 const { | 101 const { |
| 101 bluez::BluetoothGattCharacteristicClient::Properties* properties = | 102 bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| 102 bluez::BluezDBusManager::Get() | 103 bluez::BluezDBusManager::Get() |
| 103 ->GetBluetoothGattCharacteristicClient() | 104 ->GetBluetoothGattCharacteristicClient() |
| 104 ->GetProperties(object_path_); | 105 ->GetProperties(object_path()); |
| 105 | 106 |
| 106 DCHECK(properties); | 107 DCHECK(properties); |
| 107 | 108 |
| 108 return properties->value.value(); | 109 return properties->value.value(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 device::BluetoothGattService* | |
| 112 BluetoothRemoteGattCharacteristicBlueZ::GetService() const { | |
| 113 return service_; | |
| 114 } | |
| 115 | |
| 116 device::BluetoothGattCharacteristic::Properties | 112 device::BluetoothGattCharacteristic::Properties |
| 117 BluetoothRemoteGattCharacteristicBlueZ::GetProperties() const { | 113 BluetoothRemoteGattCharacteristicBlueZ::GetProperties() const { |
| 118 bluez::BluetoothGattCharacteristicClient::Properties* properties = | 114 bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| 119 bluez::BluezDBusManager::Get() | 115 bluez::BluezDBusManager::Get() |
| 120 ->GetBluetoothGattCharacteristicClient() | 116 ->GetBluetoothGattCharacteristicClient() |
| 121 ->GetProperties(object_path_); | 117 ->GetProperties(object_path()); |
| 122 DCHECK(properties); | 118 DCHECK(properties); |
| 123 | 119 |
| 124 Properties props = PROPERTY_NONE; | 120 Properties props = PROPERTY_NONE; |
| 125 const std::vector<std::string>& flags = properties->flags.value(); | 121 const std::vector<std::string>& flags = properties->flags.value(); |
| 126 for (std::vector<std::string>::const_iterator iter = flags.begin(); | 122 for (std::vector<std::string>::const_iterator iter = flags.begin(); |
| 127 iter != flags.end(); ++iter) { | 123 iter != flags.end(); ++iter) { |
| 128 if (*iter == bluetooth_gatt_characteristic::kFlagBroadcast) | 124 if (*iter == bluetooth_gatt_characteristic::kFlagBroadcast) |
| 129 props |= PROPERTY_BROADCAST; | 125 props |= PROPERTY_BROADCAST; |
| 130 if (*iter == bluetooth_gatt_characteristic::kFlagRead) | 126 if (*iter == bluetooth_gatt_characteristic::kFlagRead) |
| 131 props |= PROPERTY_READ; | 127 props |= PROPERTY_READ; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 143 props |= PROPERTY_EXTENDED_PROPERTIES; | 139 props |= PROPERTY_EXTENDED_PROPERTIES; |
| 144 if (*iter == bluetooth_gatt_characteristic::kFlagReliableWrite) | 140 if (*iter == bluetooth_gatt_characteristic::kFlagReliableWrite) |
| 145 props |= PROPERTY_RELIABLE_WRITE; | 141 props |= PROPERTY_RELIABLE_WRITE; |
| 146 if (*iter == bluetooth_gatt_characteristic::kFlagWritableAuxiliaries) | 142 if (*iter == bluetooth_gatt_characteristic::kFlagWritableAuxiliaries) |
| 147 props |= PROPERTY_WRITABLE_AUXILIARIES; | 143 props |= PROPERTY_WRITABLE_AUXILIARIES; |
| 148 } | 144 } |
| 149 | 145 |
| 150 return props; | 146 return props; |
| 151 } | 147 } |
| 152 | 148 |
| 153 device::BluetoothGattCharacteristic::Permissions | |
| 154 BluetoothRemoteGattCharacteristicBlueZ::GetPermissions() const { | |
| 155 // TODO(armansito): Once BlueZ defines the permissions, return the correct | |
| 156 // values here. | |
| 157 return PERMISSION_NONE; | |
| 158 } | |
| 159 | |
| 160 bool BluetoothRemoteGattCharacteristicBlueZ::IsNotifying() const { | 149 bool BluetoothRemoteGattCharacteristicBlueZ::IsNotifying() const { |
| 161 bluez::BluetoothGattCharacteristicClient::Properties* properties = | 150 bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| 162 bluez::BluezDBusManager::Get() | 151 bluez::BluezDBusManager::Get() |
| 163 ->GetBluetoothGattCharacteristicClient() | 152 ->GetBluetoothGattCharacteristicClient() |
| 164 ->GetProperties(object_path_); | 153 ->GetProperties(object_path()); |
| 165 DCHECK(properties); | 154 DCHECK(properties); |
| 166 | 155 |
| 167 return properties->notifying.value(); | 156 return properties->notifying.value(); |
| 168 } | 157 } |
| 169 | 158 |
| 170 std::vector<device::BluetoothGattDescriptor*> | |
| 171 BluetoothRemoteGattCharacteristicBlueZ::GetDescriptors() const { | |
| 172 std::vector<device::BluetoothGattDescriptor*> descriptors; | |
| 173 for (DescriptorMap::const_iterator iter = descriptors_.begin(); | |
| 174 iter != descriptors_.end(); ++iter) | |
| 175 descriptors.push_back(iter->second); | |
| 176 return descriptors; | |
| 177 } | |
| 178 | |
| 179 device::BluetoothGattDescriptor* | |
| 180 BluetoothRemoteGattCharacteristicBlueZ::GetDescriptor( | |
| 181 const std::string& identifier) const { | |
| 182 DescriptorMap::const_iterator iter = | |
| 183 descriptors_.find(dbus::ObjectPath(identifier)); | |
| 184 if (iter == descriptors_.end()) | |
| 185 return NULL; | |
| 186 return iter->second; | |
| 187 } | |
| 188 | |
| 189 bool BluetoothRemoteGattCharacteristicBlueZ::AddDescriptor( | 159 bool BluetoothRemoteGattCharacteristicBlueZ::AddDescriptor( |
| 190 device::BluetoothGattDescriptor* descriptor) { | 160 device::BluetoothGattDescriptor* descriptor) { |
| 191 VLOG(1) << "Descriptors cannot be added to a remote GATT characteristic."; | 161 VLOG(1) << "Descriptors cannot be added to a remote GATT characteristic."; |
| 192 return false; | 162 return false; |
| 193 } | 163 } |
| 194 | 164 |
| 195 bool BluetoothRemoteGattCharacteristicBlueZ::UpdateValue( | 165 bool BluetoothRemoteGattCharacteristicBlueZ::UpdateValue( |
| 196 const std::vector<uint8_t>& value) { | 166 const std::vector<uint8_t>& value) { |
| 197 VLOG(1) << "Cannot update the value of a remote GATT characteristic."; | 167 VLOG(1) << "Cannot update the value of a remote GATT characteristic."; |
| 198 return false; | 168 return false; |
| 199 } | 169 } |
| 200 | 170 |
| 201 void BluetoothRemoteGattCharacteristicBlueZ::ReadRemoteCharacteristic( | |
| 202 const ValueCallback& callback, | |
| 203 const ErrorCallback& error_callback) { | |
| 204 VLOG(1) << "Sending GATT characteristic read request to characteristic: " | |
| 205 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | |
| 206 << "."; | |
| 207 | |
| 208 bluez::BluezDBusManager::Get() | |
| 209 ->GetBluetoothGattCharacteristicClient() | |
| 210 ->ReadValue(object_path_, callback, | |
| 211 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnError, | |
| 212 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 213 } | |
| 214 | |
| 215 void BluetoothRemoteGattCharacteristicBlueZ::WriteRemoteCharacteristic( | |
| 216 const std::vector<uint8_t>& new_value, | |
| 217 const base::Closure& callback, | |
| 218 const ErrorCallback& error_callback) { | |
| 219 VLOG(1) << "Sending GATT characteristic write request to characteristic: " | |
| 220 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | |
| 221 << ", with value: " << new_value << "."; | |
| 222 | |
| 223 bluez::BluezDBusManager::Get() | |
| 224 ->GetBluetoothGattCharacteristicClient() | |
| 225 ->WriteValue(object_path_, new_value, callback, | |
| 226 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnError, | |
| 227 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 228 } | |
| 229 | |
| 230 void BluetoothRemoteGattCharacteristicBlueZ::StartNotifySession( | 171 void BluetoothRemoteGattCharacteristicBlueZ::StartNotifySession( |
| 231 const NotifySessionCallback& callback, | 172 const NotifySessionCallback& callback, |
| 232 const ErrorCallback& error_callback) { | 173 const ErrorCallback& error_callback) { |
| 233 VLOG(1) << __func__; | 174 VLOG(1) << __func__; |
| 234 | 175 |
| 235 if (num_notify_sessions_ > 0) { | 176 if (num_notify_sessions_ > 0) { |
| 236 // The characteristic might have stopped notifying even though the session | 177 // The characteristic might have stopped notifying even though the session |
| 237 // count is nonzero. This means that notifications stopped outside of our | 178 // count is nonzero. This means that notifications stopped outside of our |
| 238 // control and we should reset the count. If the characteristic is still | 179 // control and we should reset the count. If the characteristic is still |
| 239 // notifying, then return success. Otherwise, reset the count and treat | 180 // notifying, then return success. Otherwise, reset the count and treat |
| 240 // this call as if the count were 0. | 181 // this call as if the count were 0. |
| 241 if (IsNotifying()) { | 182 if (IsNotifying()) { |
| 242 // Check for overflows, though unlikely. | 183 // Check for overflows, though unlikely. |
| 243 if (num_notify_sessions_ == std::numeric_limits<size_t>::max()) { | 184 if (num_notify_sessions_ == std::numeric_limits<size_t>::max()) { |
| 244 error_callback.Run(device::BluetoothGattService::GATT_ERROR_FAILED); | 185 error_callback.Run(device::BluetoothGattService::GATT_ERROR_FAILED); |
| 245 return; | 186 return; |
| 246 } | 187 } |
| 247 | 188 |
| 248 ++num_notify_sessions_; | 189 ++num_notify_sessions_; |
| 249 DCHECK(service_); | 190 DCHECK(service_); |
| 250 DCHECK(service_->GetAdapter()); | 191 DCHECK(service_->GetAdapter()); |
| 251 DCHECK(service_->GetDevice()); | 192 DCHECK(service_->GetDevice()); |
| 252 std::unique_ptr<device::BluetoothGattNotifySession> session( | 193 std::unique_ptr<device::BluetoothGattNotifySession> session( |
| 253 new BluetoothGattNotifySessionBlueZ( | 194 new BluetoothGattNotifySessionBlueZ( |
| 254 service_->GetAdapter(), service_->GetDevice()->GetAddress(), | 195 service_->GetAdapter(), service_->GetDevice()->GetAddress(), |
| 255 service_->GetIdentifier(), GetIdentifier(), object_path_)); | 196 service_->GetIdentifier(), GetIdentifier(), object_path())); |
| 256 callback.Run(std::move(session)); | 197 callback.Run(std::move(session)); |
| 257 return; | 198 return; |
| 258 } | 199 } |
| 259 | 200 |
| 260 num_notify_sessions_ = 0; | 201 num_notify_sessions_ = 0; |
| 261 } | 202 } |
| 262 | 203 |
| 263 // Queue the callbacks if there is a pending call to bluetoothd. | 204 // Queue the callbacks if there is a pending call to bluetoothd. |
| 264 if (notify_call_pending_) { | 205 if (notify_call_pending_) { |
| 265 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); | 206 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); |
| 266 return; | 207 return; |
| 267 } | 208 } |
| 268 | 209 |
| 269 notify_call_pending_ = true; | 210 notify_call_pending_ = true; |
| 270 bluez::BluezDBusManager::Get() | 211 bluez::BluezDBusManager::Get() |
| 271 ->GetBluetoothGattCharacteristicClient() | 212 ->GetBluetoothGattCharacteristicClient() |
| 272 ->StartNotify( | 213 ->StartNotify( |
| 273 object_path_, | 214 object_path(), |
| 274 base::Bind( | 215 base::Bind( |
| 275 &BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess, | 216 &BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess, |
| 276 weak_ptr_factory_.GetWeakPtr(), callback), | 217 weak_ptr_factory_.GetWeakPtr(), callback), |
| 277 base::Bind( | 218 base::Bind( |
| 278 &BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifyError, | 219 &BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifyError, |
| 279 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 220 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 280 } | 221 } |
| 281 | 222 |
| 223 void BluetoothRemoteGattCharacteristicBlueZ::ReadRemoteCharacteristic( |
| 224 const ValueCallback& callback, |
| 225 const ErrorCallback& error_callback) { |
| 226 VLOG(1) << "Sending GATT characteristic read request to characteristic: " |
| 227 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| 228 << "."; |
| 229 |
| 230 bluez::BluezDBusManager::Get() |
| 231 ->GetBluetoothGattCharacteristicClient() |
| 232 ->ReadValue(object_path(), callback, |
| 233 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnError, |
| 234 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 235 } |
| 236 |
| 237 void BluetoothRemoteGattCharacteristicBlueZ::WriteRemoteCharacteristic( |
| 238 const std::vector<uint8_t>& new_value, |
| 239 const base::Closure& callback, |
| 240 const ErrorCallback& error_callback) { |
| 241 VLOG(1) << "Sending GATT characteristic write request to characteristic: " |
| 242 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| 243 << ", with value: " << new_value << "."; |
| 244 |
| 245 bluez::BluezDBusManager::Get() |
| 246 ->GetBluetoothGattCharacteristicClient() |
| 247 ->WriteValue(object_path(), new_value, callback, |
| 248 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnError, |
| 249 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 250 } |
| 251 |
| 282 void BluetoothRemoteGattCharacteristicBlueZ::RemoveNotifySession( | 252 void BluetoothRemoteGattCharacteristicBlueZ::RemoveNotifySession( |
| 283 const base::Closure& callback) { | 253 const base::Closure& callback) { |
| 284 VLOG(1) << __func__; | 254 VLOG(1) << __func__; |
| 285 | 255 |
| 286 if (num_notify_sessions_ > 1) { | 256 if (num_notify_sessions_ > 1) { |
| 287 DCHECK(!notify_call_pending_); | 257 DCHECK(!notify_call_pending_); |
| 288 --num_notify_sessions_; | 258 --num_notify_sessions_; |
| 289 callback.Run(); | 259 callback.Run(); |
| 290 return; | 260 return; |
| 291 } | 261 } |
| 292 | 262 |
| 293 // Notifications may have stopped outside our control. If the characteristic | 263 // Notifications may have stopped outside our control. If the characteristic |
| 294 // is no longer notifying, return success. | 264 // is no longer notifying, return success. |
| 295 if (!IsNotifying()) { | 265 if (!IsNotifying()) { |
| 296 num_notify_sessions_ = 0; | 266 num_notify_sessions_ = 0; |
| 297 callback.Run(); | 267 callback.Run(); |
| 298 return; | 268 return; |
| 299 } | 269 } |
| 300 | 270 |
| 301 if (notify_call_pending_ || num_notify_sessions_ == 0) { | 271 if (notify_call_pending_ || num_notify_sessions_ == 0) { |
| 302 callback.Run(); | 272 callback.Run(); |
| 303 return; | 273 return; |
| 304 } | 274 } |
| 305 | 275 |
| 306 DCHECK(num_notify_sessions_ == 1); | 276 DCHECK(num_notify_sessions_ == 1); |
| 307 notify_call_pending_ = true; | 277 notify_call_pending_ = true; |
| 308 bluez::BluezDBusManager::Get() | 278 bluez::BluezDBusManager::Get() |
| 309 ->GetBluetoothGattCharacteristicClient() | 279 ->GetBluetoothGattCharacteristicClient() |
| 310 ->StopNotify( | 280 ->StopNotify( |
| 311 object_path_, | 281 object_path(), |
| 312 base::Bind( | 282 base::Bind( |
| 313 &BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifySuccess, | 283 &BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifySuccess, |
| 314 weak_ptr_factory_.GetWeakPtr(), callback), | 284 weak_ptr_factory_.GetWeakPtr(), callback), |
| 315 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifyError, | 285 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifyError, |
| 316 weak_ptr_factory_.GetWeakPtr(), callback)); | 286 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 317 } | 287 } |
| 318 | 288 |
| 319 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded( | 289 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded( |
| 320 const dbus::ObjectPath& object_path) { | 290 const dbus::ObjectPath& object_path) { |
| 321 if (descriptors_.find(object_path) != descriptors_.end()) { | 291 if (descriptors_.find(object_path) != descriptors_.end()) { |
| 322 VLOG(1) << "Remote GATT characteristic descriptor already exists: " | 292 VLOG(1) << "Remote GATT characteristic descriptor already exists: " |
| 323 << object_path.value(); | 293 << object_path.value(); |
| 324 return; | 294 return; |
| 325 } | 295 } |
| 326 | 296 |
| 327 bluez::BluetoothGattDescriptorClient::Properties* properties = | 297 bluez::BluetoothGattDescriptorClient::Properties* properties = |
| 328 bluez::BluezDBusManager::Get() | 298 bluez::BluezDBusManager::Get() |
| 329 ->GetBluetoothGattDescriptorClient() | 299 ->GetBluetoothGattDescriptorClient() |
| 330 ->GetProperties(object_path); | 300 ->GetProperties(object_path); |
| 331 DCHECK(properties); | 301 DCHECK(properties); |
| 332 if (properties->characteristic.value() != object_path_) { | 302 if (properties->characteristic.value() != this->object_path()) { |
| 333 VLOG(3) << "Remote GATT descriptor does not belong to this characteristic."; | 303 VLOG(3) << "Remote GATT descriptor does not belong to this characteristic."; |
| 334 return; | 304 return; |
| 335 } | 305 } |
| 336 | 306 |
| 337 VLOG(1) << "Adding new remote GATT descriptor for GATT characteristic: " | 307 VLOG(1) << "Adding new remote GATT descriptor for GATT characteristic: " |
| 338 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 308 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 339 | 309 |
| 340 BluetoothRemoteGattDescriptorBlueZ* descriptor = | 310 BluetoothGattDescriptorBlueZ* descriptor = |
| 341 new BluetoothRemoteGattDescriptorBlueZ(this, object_path); | 311 new BluetoothGattDescriptorBlueZ(this, object_path, false /* is_local */); |
| 342 descriptors_[object_path] = descriptor; | 312 descriptors_[object_path] = descriptor; |
| 343 DCHECK(descriptor->GetIdentifier() == object_path.value()); | 313 DCHECK(descriptor->GetIdentifier() == object_path.value()); |
| 344 DCHECK(descriptor->GetUUID().IsValid()); | 314 DCHECK(descriptor->GetUUID().IsValid()); |
| 345 DCHECK(service_); | 315 DCHECK(service_); |
| 346 | 316 |
| 347 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, true /* added */); | 317 static_cast<BluetoothRemoteGattServiceBlueZ*>(service_) |
| 318 ->NotifyDescriptorAddedOrRemoved(this, descriptor, true /* added */); |
| 348 } | 319 } |
| 349 | 320 |
| 350 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorRemoved( | 321 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorRemoved( |
| 351 const dbus::ObjectPath& object_path) { | 322 const dbus::ObjectPath& object_path) { |
| 352 DescriptorMap::iterator iter = descriptors_.find(object_path); | 323 DescriptorMap::iterator iter = descriptors_.find(object_path); |
| 353 if (iter == descriptors_.end()) { | 324 if (iter == descriptors_.end()) { |
| 354 VLOG(2) << "Unknown descriptor removed: " << object_path.value(); | 325 VLOG(2) << "Unknown descriptor removed: " << object_path.value(); |
| 355 return; | 326 return; |
| 356 } | 327 } |
| 357 | 328 |
| 358 VLOG(1) << "Removing remote GATT descriptor from characteristic: " | 329 VLOG(1) << "Removing remote GATT descriptor from characteristic: " |
| 359 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 330 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 360 | 331 |
| 361 BluetoothRemoteGattDescriptorBlueZ* descriptor = iter->second; | 332 BluetoothGattDescriptorBlueZ* descriptor = iter->second; |
| 362 DCHECK(descriptor->object_path() == object_path); | 333 DCHECK(descriptor->object_path() == object_path); |
| 363 descriptors_.erase(iter); | 334 descriptors_.erase(iter); |
| 364 | 335 |
| 365 DCHECK(service_); | 336 DCHECK(service_); |
| 366 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, false /* added */); | 337 static_cast<BluetoothRemoteGattServiceBlueZ*>(service_) |
| 338 ->NotifyDescriptorAddedOrRemoved(this, descriptor, false /* added */); |
| 367 | 339 |
| 368 delete descriptor; | 340 delete descriptor; |
| 369 } | 341 } |
| 370 | 342 |
| 371 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorPropertyChanged( | 343 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorPropertyChanged( |
| 372 const dbus::ObjectPath& object_path, | 344 const dbus::ObjectPath& object_path, |
| 373 const std::string& property_name) { | 345 const std::string& property_name) { |
| 374 DescriptorMap::iterator iter = descriptors_.find(object_path); | 346 DescriptorMap::iterator iter = descriptors_.find(object_path); |
| 375 if (iter == descriptors_.end()) { | 347 if (iter == descriptors_.end()) { |
| 376 VLOG(2) << "Unknown descriptor removed: " << object_path.value(); | 348 VLOG(2) << "Unknown descriptor removed: " << object_path.value(); |
| 377 return; | 349 return; |
| 378 } | 350 } |
| 379 | 351 |
| 380 bluez::BluetoothGattDescriptorClient::Properties* properties = | 352 bluez::BluetoothGattDescriptorClient::Properties* properties = |
| 381 bluez::BluezDBusManager::Get() | 353 bluez::BluezDBusManager::Get() |
| 382 ->GetBluetoothGattDescriptorClient() | 354 ->GetBluetoothGattDescriptorClient() |
| 383 ->GetProperties(object_path); | 355 ->GetProperties(object_path); |
| 384 | 356 |
| 385 DCHECK(properties); | 357 DCHECK(properties); |
| 386 | 358 |
| 387 if (property_name != properties->value.name()) | 359 if (property_name != properties->value.name()) |
| 388 return; | 360 return; |
| 389 | 361 |
| 390 DCHECK(service_); | 362 DCHECK(service_); |
| 391 service_->NotifyDescriptorValueChanged(this, iter->second, | 363 static_cast<BluetoothRemoteGattServiceBlueZ*>(service_) |
| 392 properties->value.value()); | 364 ->NotifyDescriptorValueChanged(this, iter->second, |
| 393 } | 365 properties->value.value()); |
| 394 | |
| 395 void BluetoothRemoteGattCharacteristicBlueZ::OnError( | |
| 396 const ErrorCallback& error_callback, | |
| 397 const std::string& error_name, | |
| 398 const std::string& error_message) { | |
| 399 VLOG(1) << "Operation failed: " << error_name | |
| 400 << ", message: " << error_message; | |
| 401 error_callback.Run( | |
| 402 BluetoothRemoteGattServiceBlueZ::DBusErrorToServiceError(error_name)); | |
| 403 } | 366 } |
| 404 | 367 |
| 405 void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess( | 368 void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess( |
| 406 const NotifySessionCallback& callback) { | 369 const NotifySessionCallback& callback) { |
| 407 VLOG(1) << "Started notifications from characteristic: " | 370 VLOG(1) << "Started notifications from characteristic: " |
| 408 << object_path_.value(); | 371 << object_path().value(); |
| 409 DCHECK(num_notify_sessions_ == 0); | 372 DCHECK(num_notify_sessions_ == 0); |
| 410 DCHECK(notify_call_pending_); | 373 DCHECK(notify_call_pending_); |
| 411 | 374 |
| 412 ++num_notify_sessions_; | 375 ++num_notify_sessions_; |
| 413 notify_call_pending_ = false; | 376 notify_call_pending_ = false; |
| 414 | 377 |
| 415 // Invoke the queued callbacks for this operation. | 378 // Invoke the queued callbacks for this operation. |
| 416 DCHECK(service_); | 379 DCHECK(service_); |
| 417 DCHECK(service_->GetDevice()); | 380 DCHECK(service_->GetDevice()); |
| 418 std::unique_ptr<device::BluetoothGattNotifySession> session( | 381 std::unique_ptr<device::BluetoothGattNotifySession> session( |
| 419 new BluetoothGattNotifySessionBlueZ( | 382 new BluetoothGattNotifySessionBlueZ( |
| 420 service_->GetAdapter(), service_->GetDevice()->GetAddress(), | 383 service_->GetAdapter(), service_->GetDevice()->GetAddress(), |
| 421 service_->GetIdentifier(), GetIdentifier(), object_path_)); | 384 service_->GetIdentifier(), GetIdentifier(), object_path())); |
| 422 callback.Run(std::move(session)); | 385 callback.Run(std::move(session)); |
| 423 | 386 |
| 424 ProcessStartNotifyQueue(); | 387 ProcessStartNotifyQueue(); |
| 425 } | 388 } |
| 426 | 389 |
| 427 void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifyError( | 390 void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifyError( |
| 428 const ErrorCallback& error_callback, | 391 const ErrorCallback& error_callback, |
| 429 const std::string& error_name, | 392 const std::string& error_name, |
| 430 const std::string& error_message) { | 393 const std::string& error_message) { |
| 431 VLOG(1) << "Failed to start notifications from characteristic: " | 394 VLOG(1) << "Failed to start notifications from characteristic: " |
| 432 << object_path_.value() << ": " << error_name << ", " | 395 << object_path().value() << ": " << error_name << ", " |
| 433 << error_message; | 396 << error_message; |
| 434 DCHECK(num_notify_sessions_ == 0); | 397 DCHECK(num_notify_sessions_ == 0); |
| 435 DCHECK(notify_call_pending_); | 398 DCHECK(notify_call_pending_); |
| 436 | 399 |
| 437 notify_call_pending_ = false; | 400 notify_call_pending_ = false; |
| 438 | 401 |
| 439 error_callback.Run( | 402 error_callback.Run( |
| 440 BluetoothRemoteGattServiceBlueZ::DBusErrorToServiceError(error_name)); | 403 BluetoothRemoteGattServiceBlueZ::DBusErrorToServiceError(error_name)); |
| 441 | 404 |
| 442 ProcessStartNotifyQueue(); | 405 ProcessStartNotifyQueue(); |
| 443 } | 406 } |
| 444 | 407 |
| 445 void BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifySuccess( | 408 void BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifySuccess( |
| 446 const base::Closure& callback) { | 409 const base::Closure& callback) { |
| 447 DCHECK(notify_call_pending_); | 410 DCHECK(notify_call_pending_); |
| 448 DCHECK(num_notify_sessions_ == 1); | 411 DCHECK(num_notify_sessions_ == 1); |
| 449 | 412 |
| 450 notify_call_pending_ = false; | 413 notify_call_pending_ = false; |
| 451 --num_notify_sessions_; | 414 --num_notify_sessions_; |
| 452 callback.Run(); | 415 callback.Run(); |
| 453 | 416 |
| 454 ProcessStartNotifyQueue(); | 417 ProcessStartNotifyQueue(); |
| 455 } | 418 } |
| 456 | 419 |
| 457 void BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifyError( | 420 void BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifyError( |
| 458 const base::Closure& callback, | 421 const base::Closure& callback, |
| 459 const std::string& error_name, | 422 const std::string& error_name, |
| 460 const std::string& error_message) { | 423 const std::string& error_message) { |
| 461 VLOG(1) << "Call to stop notifications failed for characteristic: " | 424 VLOG(1) << "Call to stop notifications failed for characteristic: " |
| 462 << object_path_.value() << ": " << error_name << ", " | 425 << object_path().value() << ": " << error_name << ", " |
| 463 << error_message; | 426 << error_message; |
| 464 | 427 |
| 465 // Since this is a best effort operation, treat this as success. | 428 // Since this is a best effort operation, treat this as success. |
| 466 OnStopNotifySuccess(callback); | 429 OnStopNotifySuccess(callback); |
| 467 } | 430 } |
| 468 | 431 |
| 469 void BluetoothRemoteGattCharacteristicBlueZ::ProcessStartNotifyQueue() { | 432 void BluetoothRemoteGattCharacteristicBlueZ::ProcessStartNotifyQueue() { |
| 470 while (!pending_start_notify_calls_.empty()) { | 433 while (!pending_start_notify_calls_.empty()) { |
| 471 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | 434 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); |
| 472 pending_start_notify_calls_.pop(); | 435 pending_start_notify_calls_.pop(); |
| 473 StartNotifySession(callbacks.first, callbacks.second); | 436 StartNotifySession(callbacks.first, callbacks.second); |
| 474 } | 437 } |
| 475 } | 438 } |
| 476 | 439 |
| 440 void BluetoothRemoteGattCharacteristicBlueZ::OnError( |
| 441 const ErrorCallback& error_callback, |
| 442 const std::string& error_name, |
| 443 const std::string& error_message) { |
| 444 VLOG(1) << "Operation failed: " << error_name |
| 445 << ", message: " << error_message; |
| 446 error_callback.Run( |
| 447 BluetoothGattServiceBlueZ::DBusErrorToServiceError(error_name)); |
| 448 } |
| 449 |
| 477 } // namespace bluez | 450 } // namespace bluez |
| OLD | NEW |