Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "device/bluetooth/bluetooth_remote_gatt_descriptor_winrt.h" |
| 6 | |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 9 | #include "base/bind.h" |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 10 | #include "base/logging.h" |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
| 12 | #include "base/strings/stringprintf.h" |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 13 | #include "base/threading/thread_task_runner_handle.h" |
Reilly Grant | 4bc137c | 2018-12-13 22:55:15 | [diff] [blame] | 14 | #include "base/win/post_async_results.h" |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 15 | #include "base/win/winrt_storage_util.h" |
| 16 | #include "device/bluetooth/bluetooth_remote_gatt_service_winrt.h" |
| 17 | #include "device/bluetooth/event_utils_winrt.h" |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 18 | |
| 19 | namespace device { |
| 20 | |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 21 | namespace { |
| 22 | |
Reilly Grant | dc08cac | 2018-12-07 01:11:01 | [diff] [blame] | 23 | using ABI::Windows::Devices::Bluetooth::BluetoothCacheMode_Uncached; |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 24 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 25 | GattCommunicationStatus; |
| 26 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
| 27 | GattCommunicationStatus_Success; |
| 28 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::GattReadResult; |
| 29 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
| 30 | GattWriteResult; |
| 31 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 32 | IGattDescriptor; |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 33 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
Reilly Grant | dc08cac | 2018-12-07 01:11:01 | [diff] [blame] | 34 | IGattDescriptor2; |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 35 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
| 36 | IGattReadResult; |
| 37 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
Reilly Grant | dc08cac | 2018-12-07 01:11:01 | [diff] [blame] | 38 | IGattReadResult2; |
| 39 | using ABI::Windows::Devices::Bluetooth::GenericAttributeProfile:: |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 40 | IGattWriteResult; |
| 41 | using ABI::Windows::Foundation::IAsyncOperation; |
| 42 | using ABI::Windows::Storage::Streams::IBuffer; |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 43 | using Microsoft::WRL::ComPtr; |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | // static |
| 48 | std::unique_ptr<BluetoothRemoteGattDescriptorWinrt> |
| 49 | BluetoothRemoteGattDescriptorWinrt::Create( |
| 50 | BluetoothRemoteGattCharacteristic* characteristic, |
| 51 | ComPtr<IGattDescriptor> descriptor) { |
| 52 | DCHECK(descriptor); |
| 53 | GUID guid; |
| 54 | HRESULT hr = descriptor->get_Uuid(&guid); |
| 55 | if (FAILED(hr)) { |
| 56 | VLOG(2) << "Getting UUID failed: " << logging::SystemErrorCodeToString(hr); |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | uint16_t attribute_handle; |
| 61 | hr = descriptor->get_AttributeHandle(&attribute_handle); |
| 62 | if (FAILED(hr)) { |
| 63 | VLOG(2) << "Getting AttributeHandle failed: " |
| 64 | << logging::SystemErrorCodeToString(hr); |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | return base::WrapUnique(new BluetoothRemoteGattDescriptorWinrt( |
| 69 | characteristic, std::move(descriptor), BluetoothUUID(guid), |
| 70 | attribute_handle)); |
| 71 | } |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 72 | |
| 73 | BluetoothRemoteGattDescriptorWinrt::~BluetoothRemoteGattDescriptorWinrt() = |
| 74 | default; |
| 75 | |
| 76 | std::string BluetoothRemoteGattDescriptorWinrt::GetIdentifier() const { |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 77 | return identifier_; |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | BluetoothUUID BluetoothRemoteGattDescriptorWinrt::GetUUID() const { |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 81 | return uuid_; |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | BluetoothGattCharacteristic::Permissions |
| 85 | BluetoothRemoteGattDescriptorWinrt::GetPermissions() const { |
| 86 | NOTIMPLEMENTED(); |
| 87 | return BluetoothGattCharacteristic::Permissions(); |
| 88 | } |
| 89 | |
| 90 | const std::vector<uint8_t>& BluetoothRemoteGattDescriptorWinrt::GetValue() |
| 91 | const { |
| 92 | return value_; |
| 93 | } |
| 94 | |
| 95 | BluetoothRemoteGattCharacteristic* |
| 96 | BluetoothRemoteGattDescriptorWinrt::GetCharacteristic() const { |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 97 | return characteristic_; |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void BluetoothRemoteGattDescriptorWinrt::ReadRemoteDescriptor( |
danakj | fcba225f | 2019-04-23 17:06:49 | [diff] [blame^] | 101 | ValueCallback callback, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 102 | ErrorCallback error_callback) { |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 103 | if (pending_read_callbacks_ || pending_write_callbacks_) { |
| 104 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 105 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 106 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 107 | BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | ComPtr<IAsyncOperation<GattReadResult*>> read_value_op; |
Reilly Grant | dc08cac | 2018-12-07 01:11:01 | [diff] [blame] | 112 | HRESULT hr = descriptor_->ReadValueWithCacheModeAsync( |
| 113 | BluetoothCacheMode_Uncached, &read_value_op); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 114 | if (FAILED(hr)) { |
Reilly Grant | dc08cac | 2018-12-07 01:11:01 | [diff] [blame] | 115 | VLOG(2) << "GattDescriptor::ReadValueWithCacheModeAsync failed: " |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 116 | << logging::SystemErrorCodeToString(hr); |
| 117 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 118 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 119 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 120 | BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 121 | return; |
| 122 | } |
| 123 | |
Reilly Grant | 4bc137c | 2018-12-13 22:55:15 | [diff] [blame] | 124 | hr = base::win::PostAsyncResults( |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 125 | std::move(read_value_op), |
| 126 | base::BindOnce(&BluetoothRemoteGattDescriptorWinrt::OnReadValue, |
| 127 | weak_ptr_factory_.GetWeakPtr())); |
| 128 | |
| 129 | if (FAILED(hr)) { |
| 130 | VLOG(2) << "PostAsyncResults failed: " |
| 131 | << logging::SystemErrorCodeToString(hr); |
| 132 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 133 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 134 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 135 | BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 136 | return; |
| 137 | } |
| 138 | |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 139 | pending_read_callbacks_ = std::make_unique<PendingReadCallbacks>( |
danakj | fcba225f | 2019-04-23 17:06:49 | [diff] [blame^] | 140 | std::move(callback), std::move(error_callback)); |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void BluetoothRemoteGattDescriptorWinrt::WriteRemoteDescriptor( |
| 144 | const std::vector<uint8_t>& value, |
danakj | fcba225f | 2019-04-23 17:06:49 | [diff] [blame^] | 145 | base::OnceClosure callback, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 146 | ErrorCallback error_callback) { |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 147 | if (pending_read_callbacks_ || pending_write_callbacks_) { |
| 148 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 149 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 150 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 151 | BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | ComPtr<IGattDescriptor2> descriptor_2; |
| 156 | HRESULT hr = descriptor_.As(&descriptor_2); |
| 157 | if (FAILED(hr)) { |
| 158 | VLOG(2) << "As IGattDescriptor2 failed: " |
| 159 | << logging::SystemErrorCodeToString(hr); |
| 160 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 161 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 162 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 163 | BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | ComPtr<IBuffer> buffer; |
| 168 | hr = base::win::CreateIBufferFromData(value.data(), value.size(), &buffer); |
| 169 | if (FAILED(hr)) { |
| 170 | VLOG(2) << "base::win::CreateIBufferFromData failed: " |
| 171 | << logging::SystemErrorCodeToString(hr); |
| 172 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 173 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 174 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 175 | BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | ComPtr<IAsyncOperation<GattWriteResult*>> write_value_op; |
| 180 | hr = descriptor_2->WriteValueWithResultAsync(buffer.Get(), &write_value_op); |
| 181 | if (FAILED(hr)) { |
| 182 | VLOG(2) << "GattDescriptor::WriteValueWithResultAsync failed: " |
| 183 | << logging::SystemErrorCodeToString(hr); |
| 184 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 185 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 186 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 187 | BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 188 | return; |
| 189 | } |
| 190 | |
Reilly Grant | 4bc137c | 2018-12-13 22:55:15 | [diff] [blame] | 191 | hr = base::win::PostAsyncResults( |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 192 | std::move(write_value_op), |
| 193 | base::BindOnce( |
| 194 | &BluetoothRemoteGattDescriptorWinrt::OnWriteValueWithResult, |
| 195 | weak_ptr_factory_.GetWeakPtr())); |
| 196 | |
| 197 | if (FAILED(hr)) { |
| 198 | VLOG(2) << "PostAsyncResults failed: " |
| 199 | << logging::SystemErrorCodeToString(hr); |
| 200 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 201 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 202 | base::BindOnce(std::move(error_callback), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 203 | BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 204 | return; |
| 205 | } |
| 206 | |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 207 | pending_write_callbacks_ = std::make_unique<PendingWriteCallbacks>( |
danakj | fcba225f | 2019-04-23 17:06:49 | [diff] [blame^] | 208 | std::move(callback), std::move(error_callback)); |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 209 | } |
| 210 | |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 211 | IGattDescriptor* BluetoothRemoteGattDescriptorWinrt::GetDescriptorForTesting() { |
| 212 | return descriptor_.Get(); |
| 213 | } |
| 214 | |
| 215 | BluetoothRemoteGattDescriptorWinrt::PendingReadCallbacks::PendingReadCallbacks( |
| 216 | ValueCallback callback, |
| 217 | ErrorCallback error_callback) |
| 218 | : callback(std::move(callback)), |
| 219 | error_callback(std::move(error_callback)) {} |
| 220 | |
| 221 | BluetoothRemoteGattDescriptorWinrt::PendingReadCallbacks:: |
| 222 | ~PendingReadCallbacks() = default; |
| 223 | |
| 224 | BluetoothRemoteGattDescriptorWinrt::PendingWriteCallbacks:: |
| 225 | PendingWriteCallbacks(base::OnceClosure callback, |
| 226 | ErrorCallback error_callback) |
| 227 | : callback(std::move(callback)), |
| 228 | error_callback(std::move(error_callback)) {} |
| 229 | |
| 230 | BluetoothRemoteGattDescriptorWinrt::PendingWriteCallbacks:: |
| 231 | ~PendingWriteCallbacks() = default; |
| 232 | |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 233 | BluetoothRemoteGattDescriptorWinrt::BluetoothRemoteGattDescriptorWinrt( |
| 234 | BluetoothRemoteGattCharacteristic* characteristic, |
| 235 | Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth:: |
| 236 | GenericAttributeProfile::IGattDescriptor> |
| 237 | descriptor, |
| 238 | BluetoothUUID uuid, |
| 239 | uint16_t attribute_handle) |
| 240 | : characteristic_(characteristic), |
| 241 | descriptor_(std::move(descriptor)), |
| 242 | uuid_(std::move(uuid)), |
| 243 | identifier_(base::StringPrintf("%s/%s_%04x", |
| 244 | characteristic_->GetIdentifier().c_str(), |
| 245 | uuid_.value().c_str(), |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 246 | attribute_handle)), |
| 247 | weak_ptr_factory_(this) {} |
| 248 | |
| 249 | void BluetoothRemoteGattDescriptorWinrt::OnReadValue( |
| 250 | ComPtr<IGattReadResult> read_result) { |
| 251 | DCHECK(pending_read_callbacks_); |
| 252 | auto pending_read_callbacks = std::move(pending_read_callbacks_); |
| 253 | |
| 254 | if (!read_result) { |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 255 | std::move(pending_read_callbacks->error_callback) |
| 256 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | |
| 260 | GattCommunicationStatus status; |
| 261 | HRESULT hr = read_result->get_Status(&status); |
| 262 | if (FAILED(hr)) { |
| 263 | VLOG(2) << "Getting GATT Communication Status failed: " |
| 264 | << logging::SystemErrorCodeToString(hr); |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 265 | std::move(pending_read_callbacks->error_callback) |
| 266 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
| 270 | if (status != GattCommunicationStatus_Success) { |
| 271 | VLOG(2) << "Unexpected GattCommunicationStatus: " << status; |
| 272 | ComPtr<IGattReadResult2> read_result_2; |
| 273 | hr = read_result.As(&read_result_2); |
| 274 | if (FAILED(hr)) { |
| 275 | VLOG(2) << "As IGattReadResult2 failed: " |
| 276 | << logging::SystemErrorCodeToString(hr); |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 277 | std::move(pending_read_callbacks->error_callback) |
| 278 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 282 | std::move(pending_read_callbacks->error_callback) |
| 283 | .Run(BluetoothRemoteGattServiceWinrt::GetGattErrorCode( |
| 284 | read_result_2.Get())); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
| 288 | ComPtr<IBuffer> value; |
| 289 | hr = read_result->get_Value(&value); |
| 290 | if (FAILED(hr)) { |
| 291 | VLOG(2) << "Getting Descriptor Value failed: " |
| 292 | << logging::SystemErrorCodeToString(hr); |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 293 | std::move(pending_read_callbacks->error_callback) |
| 294 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
| 298 | uint8_t* data = nullptr; |
| 299 | uint32_t length = 0; |
| 300 | hr = base::win::GetPointerToBufferData(value.Get(), &data, &length); |
| 301 | if (FAILED(hr)) { |
| 302 | VLOG(2) << "Getting Pointer To Buffer Data failed: " |
| 303 | << logging::SystemErrorCodeToString(hr); |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 304 | std::move(pending_read_callbacks->error_callback) |
| 305 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | |
| 309 | value_.assign(data, data + length); |
danakj | fcba225f | 2019-04-23 17:06:49 | [diff] [blame^] | 310 | std::move(pending_read_callbacks->callback).Run(value_); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void BluetoothRemoteGattDescriptorWinrt::OnWriteValueWithResult( |
| 314 | ComPtr<IGattWriteResult> write_result) { |
| 315 | DCHECK(pending_write_callbacks_); |
| 316 | auto pending_write_callbacks = std::move(pending_write_callbacks_); |
| 317 | |
| 318 | if (!write_result) { |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 319 | std::move(pending_write_callbacks->error_callback) |
| 320 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | |
| 324 | GattCommunicationStatus status; |
| 325 | HRESULT hr = write_result->get_Status(&status); |
| 326 | if (FAILED(hr)) { |
| 327 | VLOG(2) << "Getting GATT Communication Status failed: " |
| 328 | << logging::SystemErrorCodeToString(hr); |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 329 | std::move(pending_write_callbacks->error_callback) |
| 330 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
| 334 | if (status != GattCommunicationStatus_Success) { |
| 335 | VLOG(2) << "Unexpected GattCommunicationStatus: " << status; |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 336 | std::move(pending_write_callbacks->error_callback) |
| 337 | .Run(BluetoothRemoteGattServiceWinrt::GetGattErrorCode( |
| 338 | write_result.Get())); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | |
| 342 | std::move(pending_write_callbacks->callback).Run(); |
| 343 | } |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 344 | |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 345 | } // namespace device |