Avi Drissman | 0db8484 | 2022-09-13 19:47:04 | [diff] [blame^] | 1 | // Copyright 2018 The Chromium Authors |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 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( |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 103 | ValueCallback callback) { |
| 104 | if (pending_read_callback_ || pending_write_callbacks_) { |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 105 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 106 | FROM_HERE, base::BindOnce(std::move(callback), |
| 107 | BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 108 | /*value=*/std::vector<uint8_t>())); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | ComPtr<IAsyncOperation<GattReadResult*>> read_value_op; |
Reilly Grant | dc08cac | 2018-12-07 01:11:01 | [diff] [blame] | 113 | HRESULT hr = descriptor_->ReadValueWithCacheModeAsync( |
| 114 | BluetoothCacheMode_Uncached, &read_value_op); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 115 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 116 | BLUETOOTH_LOG(ERROR) |
| 117 | << "GattDescriptor::ReadValueWithCacheModeAsync failed: " |
| 118 | << logging::SystemErrorCodeToString(hr); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 119 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 120 | FROM_HERE, base::BindOnce(std::move(callback), |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 121 | BluetoothGattService::GATT_ERROR_FAILED, |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 122 | /*value=*/std::vector<uint8_t>())); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
Reilly Grant | 4bc137c | 2018-12-13 22:55:15 | [diff] [blame] | 126 | hr = base::win::PostAsyncResults( |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 127 | std::move(read_value_op), |
| 128 | base::BindOnce(&BluetoothRemoteGattDescriptorWinrt::OnReadValue, |
| 129 | weak_ptr_factory_.GetWeakPtr())); |
| 130 | |
| 131 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 132 | BLUETOOTH_LOG(ERROR) << "PostAsyncResults failed: " |
| 133 | << logging::SystemErrorCodeToString(hr); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 134 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 135 | FROM_HERE, base::BindOnce(std::move(callback), |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 136 | BluetoothGattService::GATT_ERROR_FAILED, |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 137 | /*value=*/std::vector<uint8_t>())); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 141 | pending_read_callback_ = std::move(callback); |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void BluetoothRemoteGattDescriptorWinrt::WriteRemoteDescriptor( |
| 145 | const std::vector<uint8_t>& value, |
danakj | fcba225f | 2019-04-23 17:06:49 | [diff] [blame] | 146 | base::OnceClosure callback, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 147 | ErrorCallback error_callback) { |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 148 | if (pending_read_callback_ || pending_write_callbacks_) { |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 149 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 150 | FROM_HERE, |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 151 | base::BindOnce(std::move(error_callback), |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 152 | BluetoothGattService::GATT_ERROR_IN_PROGRESS)); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
| 156 | ComPtr<IGattDescriptor2> descriptor_2; |
| 157 | HRESULT hr = descriptor_.As(&descriptor_2); |
| 158 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 159 | BLUETOOTH_LOG(ERROR) << "As IGattDescriptor2 failed: " |
| 160 | << logging::SystemErrorCodeToString(hr); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 161 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 162 | FROM_HERE, base::BindOnce(std::move(error_callback), |
| 163 | BluetoothGattService::GATT_ERROR_FAILED)); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | |
| 167 | ComPtr<IBuffer> buffer; |
| 168 | hr = base::win::CreateIBufferFromData(value.data(), value.size(), &buffer); |
| 169 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 170 | BLUETOOTH_LOG(ERROR) << "base::win::CreateIBufferFromData failed: " |
| 171 | << logging::SystemErrorCodeToString(hr); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 172 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 173 | FROM_HERE, base::BindOnce(std::move(error_callback), |
| 174 | BluetoothGattService::GATT_ERROR_FAILED)); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | ComPtr<IAsyncOperation<GattWriteResult*>> write_value_op; |
| 179 | hr = descriptor_2->WriteValueWithResultAsync(buffer.Get(), &write_value_op); |
| 180 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 181 | BLUETOOTH_LOG(ERROR) << "GattDescriptor::WriteValueWithResultAsync failed: " |
| 182 | << logging::SystemErrorCodeToString(hr); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 183 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 184 | FROM_HERE, base::BindOnce(std::move(error_callback), |
| 185 | BluetoothGattService::GATT_ERROR_FAILED)); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
Reilly Grant | 4bc137c | 2018-12-13 22:55:15 | [diff] [blame] | 189 | hr = base::win::PostAsyncResults( |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 190 | std::move(write_value_op), |
| 191 | base::BindOnce( |
| 192 | &BluetoothRemoteGattDescriptorWinrt::OnWriteValueWithResult, |
| 193 | weak_ptr_factory_.GetWeakPtr())); |
| 194 | |
| 195 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 196 | BLUETOOTH_LOG(ERROR) << "PostAsyncResults failed: " |
| 197 | << logging::SystemErrorCodeToString(hr); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 198 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 199 | FROM_HERE, base::BindOnce(std::move(error_callback), |
| 200 | BluetoothGattService::GATT_ERROR_FAILED)); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 204 | pending_write_callbacks_ = std::make_unique<PendingWriteCallbacks>( |
danakj | fcba225f | 2019-04-23 17:06:49 | [diff] [blame] | 205 | std::move(callback), std::move(error_callback)); |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 206 | } |
| 207 | |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 208 | IGattDescriptor* BluetoothRemoteGattDescriptorWinrt::GetDescriptorForTesting() { |
| 209 | return descriptor_.Get(); |
| 210 | } |
| 211 | |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 212 | BluetoothRemoteGattDescriptorWinrt::PendingWriteCallbacks:: |
| 213 | PendingWriteCallbacks(base::OnceClosure callback, |
| 214 | ErrorCallback error_callback) |
| 215 | : callback(std::move(callback)), |
| 216 | error_callback(std::move(error_callback)) {} |
| 217 | |
| 218 | BluetoothRemoteGattDescriptorWinrt::PendingWriteCallbacks:: |
| 219 | ~PendingWriteCallbacks() = default; |
| 220 | |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 221 | BluetoothRemoteGattDescriptorWinrt::BluetoothRemoteGattDescriptorWinrt( |
| 222 | BluetoothRemoteGattCharacteristic* characteristic, |
| 223 | Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth:: |
| 224 | GenericAttributeProfile::IGattDescriptor> |
| 225 | descriptor, |
| 226 | BluetoothUUID uuid, |
| 227 | uint16_t attribute_handle) |
| 228 | : characteristic_(characteristic), |
| 229 | descriptor_(std::move(descriptor)), |
| 230 | uuid_(std::move(uuid)), |
| 231 | identifier_(base::StringPrintf("%s/%s_%04x", |
| 232 | characteristic_->GetIdentifier().c_str(), |
| 233 | uuid_.value().c_str(), |
Jeremy Roman | 2b7c950 | 2019-08-21 22:34:08 | [diff] [blame] | 234 | attribute_handle)) {} |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 235 | |
| 236 | void BluetoothRemoteGattDescriptorWinrt::OnReadValue( |
| 237 | ComPtr<IGattReadResult> read_result) { |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 238 | DCHECK(pending_read_callback_); |
| 239 | auto pending_read_callback = std::move(pending_read_callback_); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 240 | |
| 241 | if (!read_result) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 242 | BLUETOOTH_LOG(ERROR) |
| 243 | << "GattDescriptor::ReadValueWithCacheModeAsync returned no result"; |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 244 | std::move(pending_read_callback) |
| 245 | .Run(BluetoothGattService::GATT_ERROR_FAILED, |
| 246 | /*value=*/std::vector<uint8_t>()); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
| 250 | GattCommunicationStatus status; |
| 251 | HRESULT hr = read_result->get_Status(&status); |
| 252 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 253 | BLUETOOTH_LOG(ERROR) << "Getting GATT Communication Status failed: " |
| 254 | << logging::SystemErrorCodeToString(hr); |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 255 | std::move(pending_read_callback) |
| 256 | .Run(BluetoothGattService::GATT_ERROR_FAILED, |
| 257 | /*value=*/std::vector<uint8_t>()); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (status != GattCommunicationStatus_Success) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 262 | BLUETOOTH_LOG(ERROR) << "Unexpected GattCommunicationStatus: " << status; |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 263 | ComPtr<IGattReadResult2> read_result_2; |
| 264 | hr = read_result.As(&read_result_2); |
| 265 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 266 | BLUETOOTH_LOG(ERROR) << "As IGattReadResult2 failed: " |
| 267 | << logging::SystemErrorCodeToString(hr); |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 268 | std::move(pending_read_callback) |
| 269 | .Run(BluetoothGattService::GATT_ERROR_FAILED, |
| 270 | /*value=*/std::vector<uint8_t>()); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 274 | std::move(pending_read_callback) |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 275 | .Run(BluetoothRemoteGattServiceWinrt::GetGattErrorCode( |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 276 | read_result_2.Get()), |
| 277 | /*value=*/std::vector<uint8_t>()); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
| 281 | ComPtr<IBuffer> value; |
| 282 | hr = read_result->get_Value(&value); |
| 283 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 284 | BLUETOOTH_LOG(ERROR) << "Getting Descriptor Value failed: " |
| 285 | << logging::SystemErrorCodeToString(hr); |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 286 | std::move(pending_read_callback) |
| 287 | .Run(BluetoothGattService::GATT_ERROR_FAILED, |
| 288 | /*value=*/std::vector<uint8_t>()); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 289 | return; |
| 290 | } |
| 291 | |
| 292 | uint8_t* data = nullptr; |
| 293 | uint32_t length = 0; |
| 294 | hr = base::win::GetPointerToBufferData(value.Get(), &data, &length); |
| 295 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 296 | BLUETOOTH_LOG(ERROR) << "Getting Pointer To Buffer Data failed: " |
| 297 | << logging::SystemErrorCodeToString(hr); |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 298 | std::move(pending_read_callback) |
| 299 | .Run(BluetoothGattService::GATT_ERROR_FAILED, |
| 300 | /*value=*/std::vector<uint8_t>()); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | |
| 304 | value_.assign(data, data + length); |
Anton Bikineev | 15c0700 | 2021-05-15 17:55:02 | [diff] [blame] | 305 | std::move(pending_read_callback).Run(/*error_code=*/absl::nullopt, value_); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void BluetoothRemoteGattDescriptorWinrt::OnWriteValueWithResult( |
| 309 | ComPtr<IGattWriteResult> write_result) { |
| 310 | DCHECK(pending_write_callbacks_); |
| 311 | auto pending_write_callbacks = std::move(pending_write_callbacks_); |
| 312 | |
| 313 | if (!write_result) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 314 | BLUETOOTH_LOG(ERROR) |
| 315 | << "GattDescriptor::WriteValueWithResultAsync returned no result"; |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 316 | std::move(pending_write_callbacks->error_callback) |
| 317 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | |
| 321 | GattCommunicationStatus status; |
| 322 | HRESULT hr = write_result->get_Status(&status); |
| 323 | if (FAILED(hr)) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 324 | BLUETOOTH_LOG(ERROR) << "Getting GATT Communication Status failed: " |
| 325 | << logging::SystemErrorCodeToString(hr); |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 326 | std::move(pending_write_callbacks->error_callback) |
| 327 | .Run(BluetoothGattService::GATT_ERROR_FAILED); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | |
| 331 | if (status != GattCommunicationStatus_Success) { |
Martin Kreichgauer | c8a7001 | 2020-01-31 01:08:52 | [diff] [blame] | 332 | BLUETOOTH_LOG(ERROR) << "Unexpected GattCommunicationStatus: " << status; |
danakj | 056b9357 | 2019-04-23 16:04:45 | [diff] [blame] | 333 | std::move(pending_write_callbacks->error_callback) |
| 334 | .Run(BluetoothRemoteGattServiceWinrt::GetGattErrorCode( |
| 335 | write_result.Get())); |
Jan Wilken Dörrie | d0a3dc6 | 2018-08-06 10:15:45 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
| 339 | std::move(pending_write_callbacks->callback).Run(); |
| 340 | } |
Jan Wilken Dörrie | 7dd23fd6 | 2018-08-06 09:06:57 | [diff] [blame] | 341 | |
Jan Wilken Doerrie | 2a3e6c9 | 2018-07-31 08:39:16 | [diff] [blame] | 342 | } // namespace device |