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