| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_win.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "device/bluetooth/bluetooth_adapter_win.h" | 8 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 9 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_win.h" | 9 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_win.h" |
| 10 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 10 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
| 11 #include "device/bluetooth/bluetooth_task_manager_win.h" | 11 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 12 | 12 |
| 13 namespace device { | 13 namespace device { |
| 14 | 14 |
| 15 BluetoothRemoteGattCharacteristicWin::BluetoothRemoteGattCharacteristicWin( | 15 BluetoothRemoteGattCharacteristicWin::BluetoothRemoteGattCharacteristicWin( |
| 16 BluetoothRemoteGattServiceWin* parent_service, | 16 BluetoothRemoteGattServiceWin* parent_service, |
| 17 BTH_LE_GATT_CHARACTERISTIC* characteristic_info, | 17 BTH_LE_GATT_CHARACTERISTIC* characteristic_info, |
| 18 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) | 18 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) |
| 19 : parent_service_(parent_service), | 19 : parent_service_(parent_service), |
| 20 characteristic_info_(characteristic_info), | 20 characteristic_info_(characteristic_info), |
| 21 ui_task_runner_(ui_task_runner), | 21 ui_task_runner_(ui_task_runner), |
| 22 characteristic_added_notified_(false), | 22 characteristic_added_notified_(false), |
| 23 characteristic_value_read_or_write_in_progress_(false), |
| 23 weak_ptr_factory_(this) { | 24 weak_ptr_factory_(this) { |
| 24 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 25 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 25 DCHECK(parent_service_); | 26 DCHECK(parent_service_); |
| 26 DCHECK(characteristic_info_); | 27 DCHECK(characteristic_info_); |
| 27 | 28 |
| 28 task_manager_ = | 29 task_manager_ = |
| 29 parent_service_->GetWinAdapter()->GetWinBluetoothTaskManager(); | 30 parent_service_->GetWinAdapter()->GetWinBluetoothTaskManager(); |
| 30 DCHECK(task_manager_); | 31 DCHECK(task_manager_); |
| 31 characteristic_uuid_ = | 32 characteristic_uuid_ = |
| 32 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( | 33 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 49 | 50 |
| 50 BluetoothUUID BluetoothRemoteGattCharacteristicWin::GetUUID() const { | 51 BluetoothUUID BluetoothRemoteGattCharacteristicWin::GetUUID() const { |
| 51 return characteristic_uuid_; | 52 return characteristic_uuid_; |
| 52 } | 53 } |
| 53 | 54 |
| 54 bool BluetoothRemoteGattCharacteristicWin::IsLocal() const { | 55 bool BluetoothRemoteGattCharacteristicWin::IsLocal() const { |
| 55 return false; | 56 return false; |
| 56 } | 57 } |
| 57 | 58 |
| 58 std::vector<uint8_t>& BluetoothRemoteGattCharacteristicWin::GetValue() const { | 59 std::vector<uint8_t>& BluetoothRemoteGattCharacteristicWin::GetValue() const { |
| 59 NOTIMPLEMENTED(); | |
| 60 return const_cast<std::vector<uint8_t>&>(characteristic_value_); | 60 return const_cast<std::vector<uint8_t>&>(characteristic_value_); |
| 61 } | 61 } |
| 62 | 62 |
| 63 BluetoothGattService* BluetoothRemoteGattCharacteristicWin::GetService() const { | 63 BluetoothGattService* BluetoothRemoteGattCharacteristicWin::GetService() const { |
| 64 return parent_service_; | 64 return parent_service_; |
| 65 } | 65 } |
| 66 | 66 |
| 67 BluetoothGattCharacteristic::Properties | 67 BluetoothGattCharacteristic::Properties |
| 68 BluetoothRemoteGattCharacteristicWin::GetProperties() const { | 68 BluetoothRemoteGattCharacteristicWin::GetProperties() const { |
| 69 BluetoothGattCharacteristic::Properties properties = PROPERTY_NONE; | 69 BluetoothGattCharacteristic::Properties properties = PROPERTY_NONE; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 void BluetoothRemoteGattCharacteristicWin::StartNotifySession( | 140 void BluetoothRemoteGattCharacteristicWin::StartNotifySession( |
| 141 const NotifySessionCallback& callback, | 141 const NotifySessionCallback& callback, |
| 142 const ErrorCallback& error_callback) { | 142 const ErrorCallback& error_callback) { |
| 143 NOTIMPLEMENTED(); | 143 NOTIMPLEMENTED(); |
| 144 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); | 144 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic( | 147 void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic( |
| 148 const ValueCallback& callback, | 148 const ValueCallback& callback, |
| 149 const ErrorCallback& error_callback) { | 149 const ErrorCallback& error_callback) { |
| 150 NOTIMPLEMENTED(); | 150 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 151 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); | 151 |
| 152 if (!characteristic_info_.get()->IsReadable) { |
| 153 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_PERMITTED); |
| 154 return; |
| 155 } |
| 156 |
| 157 if (characteristic_value_read_or_write_in_progress_) { |
| 158 error_callback.Run(BluetoothGattService::GATT_ERROR_IN_PROGRESS); |
| 159 return; |
| 160 } |
| 161 |
| 162 characteristic_value_read_or_write_in_progress_ = true; |
| 163 read_remote_characteristic_value_callbacks_ = |
| 164 std::make_pair(callback, error_callback); |
| 165 task_manager_->PostReadGattCharacteristicValue( |
| 166 parent_service_->GetServicePath(), characteristic_info_.get(), |
| 167 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
| 168 OnReadRemoteCharacteristicValueCallback, |
| 169 weak_ptr_factory_.GetWeakPtr())); |
| 152 } | 170 } |
| 153 | 171 |
| 154 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( | 172 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( |
| 155 const std::vector<uint8_t>& new_value, | 173 const std::vector<uint8_t>& new_value, |
| 156 const base::Closure& callback, | 174 const base::Closure& callback, |
| 157 const ErrorCallback& error_callback) { | 175 const ErrorCallback& error_callback) { |
| 158 NOTIMPLEMENTED(); | 176 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 159 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); | 177 |
| 178 if (!characteristic_info_.get()->IsWritable) { |
| 179 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_PERMITTED); |
| 180 return; |
| 181 } |
| 182 |
| 183 if (characteristic_value_read_or_write_in_progress_) { |
| 184 error_callback.Run(BluetoothGattService::GATT_ERROR_IN_PROGRESS); |
| 185 return; |
| 186 } |
| 187 |
| 188 characteristic_value_read_or_write_in_progress_ = true; |
| 189 write_remote_characteristic_value_callbacks_ = |
| 190 std::make_pair(callback, error_callback); |
| 191 task_manager_->PostWriteGattCharacteristicValue( |
| 192 parent_service_->GetServicePath(), characteristic_info_.get(), new_value, |
| 193 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
| 194 OnWriteRemoteCharacteristicValueCallback, |
| 195 weak_ptr_factory_.GetWeakPtr())); |
| 160 } | 196 } |
| 161 | 197 |
| 162 void BluetoothRemoteGattCharacteristicWin::Update() { | 198 void BluetoothRemoteGattCharacteristicWin::Update() { |
| 163 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 199 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 164 | 200 |
| 165 task_manager_->PostGetGattIncludedDescriptors( | 201 task_manager_->PostGetGattIncludedDescriptors( |
| 166 parent_service_->GetServicePath(), characteristic_info_.get(), | 202 parent_service_->GetServicePath(), characteristic_info_.get(), |
| 167 base::Bind(&BluetoothRemoteGattCharacteristicWin:: | 203 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
| 168 OnGetIncludedDescriptorsCallback, | 204 OnGetIncludedDescriptorsCallback, |
| 169 weak_ptr_factory_.GetWeakPtr())); | 205 weak_ptr_factory_.GetWeakPtr())); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( | 282 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( |
| 247 descriptors[i].DescriptorUuid); | 283 descriptors[i].DescriptorUuid); |
| 248 if (descriptor->GetUUID() == uuid && | 284 if (descriptor->GetUUID() == uuid && |
| 249 descriptor->GetAttributeHandle() == descriptors[i].AttributeHandle) { | 285 descriptor->GetAttributeHandle() == descriptors[i].AttributeHandle) { |
| 250 return true; | 286 return true; |
| 251 } | 287 } |
| 252 } | 288 } |
| 253 return false; | 289 return false; |
| 254 } | 290 } |
| 255 | 291 |
| 292 void BluetoothRemoteGattCharacteristicWin:: |
| 293 OnReadRemoteCharacteristicValueCallback( |
| 294 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value, |
| 295 HRESULT hr) { |
| 296 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 297 |
| 298 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 299 callbacks.swap(read_remote_characteristic_value_callbacks_); |
| 300 if (FAILED(hr)) { |
| 301 callbacks.second.Run(HRESULTToGattErrorCode(hr)); |
| 302 } else { |
| 303 characteristic_value_.clear(); |
| 304 for (ULONG i = 0; i < value->DataSize; i++) |
| 305 characteristic_value_.push_back(value->Data[i]); |
| 306 callbacks.first.Run(characteristic_value_); |
| 307 } |
| 308 characteristic_value_read_or_write_in_progress_ = false; |
| 309 } |
| 310 |
| 311 void BluetoothRemoteGattCharacteristicWin:: |
| 312 OnWriteRemoteCharacteristicValueCallback(HRESULT hr) { |
| 313 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 314 |
| 315 std::pair<base::Closure, ErrorCallback> callbacks; |
| 316 callbacks.swap(write_remote_characteristic_value_callbacks_); |
| 317 if (FAILED(hr)) { |
| 318 callbacks.second.Run(HRESULTToGattErrorCode(hr)); |
| 319 } else { |
| 320 callbacks.first.Run(); |
| 321 } |
| 322 characteristic_value_read_or_write_in_progress_ = false; |
| 323 } |
| 324 |
| 325 BluetoothGattService::GattErrorCode |
| 326 BluetoothRemoteGattCharacteristicWin::HRESULTToGattErrorCode(HRESULT hr) { |
| 327 switch (hr) { |
| 328 case E_BLUETOOTH_ATT_READ_NOT_PERMITTED: |
| 329 case E_BLUETOOTH_ATT_WRITE_NOT_PERMITTED: |
| 330 return BluetoothGattService::GATT_ERROR_NOT_PERMITTED; |
| 331 case E_BLUETOOTH_ATT_UNKNOWN_ERROR: |
| 332 return BluetoothGattService::GATT_ERROR_UNKNOWN; |
| 333 case ERROR_INVALID_USER_BUFFER: |
| 334 case E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH: |
| 335 return BluetoothGattService::GATT_ERROR_INVALID_LENGTH; |
| 336 default: |
| 337 return BluetoothGattService::GATT_ERROR_FAILED; |
| 338 } |
| 339 } |
| 340 |
| 256 } // namespace device. | 341 } // namespace device. |
| OLD | NEW |