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