| 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" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 BluetoothUUID BluetoothRemoteGattCharacteristicWin::GetUUID() const { | 52 BluetoothUUID BluetoothRemoteGattCharacteristicWin::GetUUID() const { |
| 53 return characteristic_uuid_; | 53 return characteristic_uuid_; |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool BluetoothRemoteGattCharacteristicWin::IsLocal() const { | 56 bool BluetoothRemoteGattCharacteristicWin::IsLocal() const { |
| 57 return false; | 57 return false; |
| 58 } | 58 } |
| 59 | 59 |
| 60 std::vector<uint8_t>& BluetoothRemoteGattCharacteristicWin::GetValue() const { | 60 std::vector<uint8_t>& BluetoothRemoteGattCharacteristicWin::GetValue() const { |
| 61 NOTIMPLEMENTED(); | |
| 62 return const_cast<std::vector<uint8_t>&>(characteristic_value_); | 61 return const_cast<std::vector<uint8_t>&>(characteristic_value_); |
| 63 } | 62 } |
| 64 | 63 |
| 65 BluetoothGattService* BluetoothRemoteGattCharacteristicWin::GetService() const { | 64 BluetoothGattService* BluetoothRemoteGattCharacteristicWin::GetService() const { |
| 66 return parent_service_; | 65 return parent_service_; |
| 67 } | 66 } |
| 68 | 67 |
| 69 BluetoothGattCharacteristic::Properties | 68 BluetoothGattCharacteristic::Properties |
| 70 BluetoothRemoteGattCharacteristicWin::GetProperties() const { | 69 BluetoothRemoteGattCharacteristicWin::GetProperties() const { |
| 71 BluetoothGattCharacteristic::Properties properties = PROPERTY_NONE; | 70 BluetoothGattCharacteristic::Properties properties = PROPERTY_NONE; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 void BluetoothRemoteGattCharacteristicWin::StartNotifySession( | 141 void BluetoothRemoteGattCharacteristicWin::StartNotifySession( |
| 143 const NotifySessionCallback& callback, | 142 const NotifySessionCallback& callback, |
| 144 const ErrorCallback& error_callback) { | 143 const ErrorCallback& error_callback) { |
| 145 NOTIMPLEMENTED(); | 144 NOTIMPLEMENTED(); |
| 146 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); | 145 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); |
| 147 } | 146 } |
| 148 | 147 |
| 149 void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic( | 148 void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic( |
| 150 const ValueCallback& callback, | 149 const ValueCallback& callback, |
| 151 const ErrorCallback& error_callback) { | 150 const ErrorCallback& error_callback) { |
| 152 NOTIMPLEMENTED(); | 151 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 153 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); | 152 |
| 153 if (!characteristic_info_.get()->IsReadable) { |
| 154 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_PERMITTED); |
| 155 return; |
| 156 } |
| 157 |
| 158 read_remote_characteristic_value_callbacks_.push_back( |
| 159 std::make_pair(callback, error_callback)); |
| 160 task_manager_->PostReadCharacteristicValue( |
| 161 parent_service_->GetServicePath(), characteristic_info_.get(), |
| 162 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
| 163 OnReadRemoteCharacteristicValueCallback, |
| 164 weak_ptr_factory_.GetWeakPtr())); |
| 154 } | 165 } |
| 155 | 166 |
| 156 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( | 167 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( |
| 157 const std::vector<uint8_t>& new_value, | 168 const std::vector<uint8_t>& new_value, |
| 158 const base::Closure& callback, | 169 const base::Closure& callback, |
| 159 const ErrorCallback& error_callback) { | 170 const ErrorCallback& error_callback) { |
| 160 NOTIMPLEMENTED(); | 171 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 161 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); | 172 |
| 173 if (!characteristic_info_.get()->IsWritable) { |
| 174 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_PERMITTED); |
| 175 return; |
| 176 } |
| 177 |
| 178 write_remote_characteristic_value_callbacks_.push_back( |
| 179 std::make_pair(callback, error_callback)); |
| 180 task_manager_->PostWriteCharacteristicValue( |
| 181 parent_service_->GetServicePath(), characteristic_info_.get(), new_value, |
| 182 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
| 183 OnWriteRemoteCharacteristicValueCallback, |
| 184 weak_ptr_factory_.GetWeakPtr())); |
| 162 } | 185 } |
| 163 | 186 |
| 164 void BluetoothRemoteGattCharacteristicWin::Update() { | 187 void BluetoothRemoteGattCharacteristicWin::Update() { |
| 165 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 188 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 166 | 189 |
| 167 task_manager_->PostGetGattIncludedDescriptors( | 190 task_manager_->PostGetGattIncludedDescriptors( |
| 168 parent_service_->GetServicePath(), characteristic_info_.get(), | 191 parent_service_->GetServicePath(), characteristic_info_.get(), |
| 169 base::Bind(&BluetoothRemoteGattCharacteristicWin:: | 192 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
| 170 OnGetIncludedDescriptorsCallback, | 193 OnGetIncludedDescriptorsCallback, |
| 171 weak_ptr_factory_.GetWeakPtr())); | 194 weak_ptr_factory_.GetWeakPtr())); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( | 271 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( |
| 249 descriptors[i].DescriptorUuid); | 272 descriptors[i].DescriptorUuid); |
| 250 if (descriptor->GetUUID() == uuid && | 273 if (descriptor->GetUUID() == uuid && |
| 251 descriptor->GetAttributeHandle() == descriptors[i].AttributeHandle) { | 274 descriptor->GetAttributeHandle() == descriptors[i].AttributeHandle) { |
| 252 return true; | 275 return true; |
| 253 } | 276 } |
| 254 } | 277 } |
| 255 return false; | 278 return false; |
| 256 } | 279 } |
| 257 | 280 |
| 281 void BluetoothRemoteGattCharacteristicWin:: |
| 282 OnReadRemoteCharacteristicValueCallback( |
| 283 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value, |
| 284 HRESULT hr) { |
| 285 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 286 |
| 287 if (FAILED(hr)) { |
| 288 for (const auto& callback : read_remote_characteristic_value_callbacks_) |
| 289 callback.second.Run(HRESULTToGattErrorCode(hr)); |
| 290 } else { |
| 291 characteristic_value_.clear(); |
| 292 for (ULONG i = 0; i < value->DataSize; i++) |
| 293 characteristic_value_.push_back(value->Data[i]); |
| 294 for (const auto& callback : read_remote_characteristic_value_callbacks_) |
| 295 callback.first.Run(characteristic_value_); |
| 296 } |
| 297 |
| 298 read_remote_characteristic_value_callbacks_.clear(); |
| 299 } |
| 300 |
| 301 void BluetoothRemoteGattCharacteristicWin:: |
| 302 OnWriteRemoteCharacteristicValueCallback(HRESULT hr) { |
| 303 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 304 |
| 305 if (FAILED(hr)) { |
| 306 for (const auto& callback : write_remote_characteristic_value_callbacks_) |
| 307 callback.second.Run(HRESULTToGattErrorCode(hr)); |
| 308 } else { |
| 309 for (const auto& callback : write_remote_characteristic_value_callbacks_) |
| 310 callback.first.Run(); |
| 311 } |
| 312 |
| 313 write_remote_characteristic_value_callbacks_.clear(); |
| 314 } |
| 315 |
| 316 BluetoothGattService::GattErrorCode |
| 317 BluetoothRemoteGattCharacteristicWin::HRESULTToGattErrorCode(HRESULT hr) { |
| 318 switch (hr) { |
| 319 case E_BLUETOOTH_ATT_READ_NOT_PERMITTED: |
| 320 case E_BLUETOOTH_ATT_WRITE_NOT_PERMITTED: |
| 321 return BluetoothGattService::GATT_ERROR_NOT_PERMITTED; |
| 322 case E_BLUETOOTH_ATT_UNKNOWN_ERROR: |
| 323 return BluetoothGattService::GATT_ERROR_UNKNOWN; |
| 324 case ERROR_INVALID_USER_BUFFER: |
| 325 case E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH: |
| 326 return BluetoothGattService::GATT_ERROR_INVALID_LENGTH; |
| 327 default: |
| 328 return BluetoothGattService::GATT_ERROR_FAILED; |
| 329 } |
| 330 } |
| 331 |
| 258 } // namespace device. | 332 } // namespace device. |
| OLD | NEW |