bluetooth_remote_gatt_descriptor_android.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/bluetooth_remote_gatt_descriptor_android.h"
  5. #include <memory>
  6. #include "base/android/jni_android.h"
  7. #include "base/android/jni_array.h"
  8. #include "base/android/jni_string.h"
  9. #include "base/bind.h"
  10. #include "base/location.h"
  11. #include "base/notreached.h"
  12. #include "base/task/single_thread_task_runner.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "device/bluetooth/bluetooth_remote_gatt_service_android.h"
  15. #include "device/bluetooth/jni_headers/ChromeBluetoothRemoteGattDescriptor_jni.h"
  16. using base::android::AttachCurrentThread;
  17. using base::android::JavaParamRef;
  18. using base::android::JavaRef;
  19. namespace device {
  20. // static
  21. std::unique_ptr<BluetoothRemoteGattDescriptorAndroid>
  22. BluetoothRemoteGattDescriptorAndroid::Create(
  23. const std::string& instance_id,
  24. const JavaRef<jobject>& /* BluetoothGattDescriptorWrapper */
  25. bluetooth_gatt_descriptor_wrapper,
  26. const JavaRef<jobject>& /* chromeBluetoothDevice */
  27. chrome_bluetooth_device) {
  28. std::unique_ptr<BluetoothRemoteGattDescriptorAndroid> descriptor(
  29. new BluetoothRemoteGattDescriptorAndroid(instance_id));
  30. descriptor->j_descriptor_.Reset(
  31. Java_ChromeBluetoothRemoteGattDescriptor_create(
  32. AttachCurrentThread(), reinterpret_cast<intptr_t>(descriptor.get()),
  33. bluetooth_gatt_descriptor_wrapper, chrome_bluetooth_device));
  34. return descriptor;
  35. }
  36. BluetoothRemoteGattDescriptorAndroid::~BluetoothRemoteGattDescriptorAndroid() {
  37. Java_ChromeBluetoothRemoteGattDescriptor_onBluetoothRemoteGattDescriptorAndroidDestruction(
  38. AttachCurrentThread(), j_descriptor_);
  39. }
  40. base::android::ScopedJavaLocalRef<jobject>
  41. BluetoothRemoteGattDescriptorAndroid::GetJavaObject() {
  42. return base::android::ScopedJavaLocalRef<jobject>(j_descriptor_);
  43. }
  44. std::string BluetoothRemoteGattDescriptorAndroid::GetIdentifier() const {
  45. return instance_id_;
  46. }
  47. BluetoothUUID BluetoothRemoteGattDescriptorAndroid::GetUUID() const {
  48. return device::BluetoothUUID(
  49. ConvertJavaStringToUTF8(Java_ChromeBluetoothRemoteGattDescriptor_getUUID(
  50. AttachCurrentThread(), j_descriptor_)));
  51. }
  52. const std::vector<uint8_t>& BluetoothRemoteGattDescriptorAndroid::GetValue()
  53. const {
  54. return value_;
  55. }
  56. BluetoothRemoteGattCharacteristic*
  57. BluetoothRemoteGattDescriptorAndroid::GetCharacteristic() const {
  58. NOTIMPLEMENTED();
  59. return nullptr;
  60. }
  61. BluetoothRemoteGattCharacteristic::Permissions
  62. BluetoothRemoteGattDescriptorAndroid::GetPermissions() const {
  63. NOTIMPLEMENTED();
  64. return 0;
  65. }
  66. void BluetoothRemoteGattDescriptorAndroid::ReadRemoteDescriptor(
  67. ValueCallback callback) {
  68. if (read_pending_ || write_pending_) {
  69. base::ThreadTaskRunnerHandle::Get()->PostTask(
  70. FROM_HERE, base::BindOnce(std::move(callback),
  71. BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  72. /*value=*/std::vector<uint8_t>()));
  73. return;
  74. }
  75. if (!Java_ChromeBluetoothRemoteGattDescriptor_readRemoteDescriptor(
  76. AttachCurrentThread(), j_descriptor_)) {
  77. base::ThreadTaskRunnerHandle::Get()->PostTask(
  78. FROM_HERE, base::BindOnce(std::move(callback),
  79. BluetoothGattService::GATT_ERROR_FAILED,
  80. /*value=*/std::vector<uint8_t>()));
  81. return;
  82. }
  83. read_pending_ = true;
  84. read_callback_ = std::move(callback);
  85. }
  86. void BluetoothRemoteGattDescriptorAndroid::WriteRemoteDescriptor(
  87. const std::vector<uint8_t>& new_value,
  88. base::OnceClosure callback,
  89. ErrorCallback error_callback) {
  90. if (read_pending_ || write_pending_) {
  91. base::ThreadTaskRunnerHandle::Get()->PostTask(
  92. FROM_HERE,
  93. base::BindOnce(std::move(error_callback),
  94. BluetoothGattService::GATT_ERROR_IN_PROGRESS));
  95. return;
  96. }
  97. JNIEnv* env = AttachCurrentThread();
  98. if (!Java_ChromeBluetoothRemoteGattDescriptor_writeRemoteDescriptor(
  99. env, j_descriptor_, base::android::ToJavaByteArray(env, new_value))) {
  100. base::ThreadTaskRunnerHandle::Get()->PostTask(
  101. FROM_HERE,
  102. base::BindOnce(std::move(error_callback),
  103. BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED));
  104. return;
  105. }
  106. write_pending_ = true;
  107. write_callback_ = std::move(callback);
  108. write_error_callback_ = std::move(error_callback);
  109. }
  110. void BluetoothRemoteGattDescriptorAndroid::OnRead(
  111. JNIEnv* env,
  112. const JavaParamRef<jobject>& jcaller,
  113. int32_t status,
  114. const JavaParamRef<jbyteArray>& value) {
  115. read_pending_ = false;
  116. // Clear callbacks before calling to avoid reentrancy issues.
  117. ValueCallback read_callback = std::move(read_callback_);
  118. if (!read_callback)
  119. return;
  120. if (status == 0) { // android.bluetooth.BluetoothGatt.GATT_SUCCESS
  121. base::android::JavaByteArrayToByteVector(env, value, &value_);
  122. std::move(read_callback).Run(/*error_code=*/absl::nullopt, value_);
  123. // TODO(https://crbug.com/584369): Call GattDescriptorValueChanged.
  124. } else {
  125. std::move(read_callback)
  126. .Run(BluetoothRemoteGattServiceAndroid::GetGattErrorCode(status),
  127. /*value=*/std::vector<uint8_t>());
  128. }
  129. }
  130. void BluetoothRemoteGattDescriptorAndroid::OnWrite(
  131. JNIEnv* env,
  132. const JavaParamRef<jobject>& jcaller,
  133. int32_t status) {
  134. write_pending_ = false;
  135. // Clear callbacks before calling to avoid reentrancy issues.
  136. base::OnceClosure write_callback = std::move(write_callback_);
  137. ErrorCallback write_error_callback = std::move(write_error_callback_);
  138. if (status == 0 // android.bluetooth.BluetoothGatt.GATT_SUCCESS
  139. && !write_callback.is_null()) {
  140. std::move(write_callback).Run();
  141. // TODO(https://crbug.com/584369): Call GattDescriptorValueChanged.
  142. } else if (!write_error_callback.is_null()) {
  143. std::move(write_error_callback)
  144. .Run(BluetoothRemoteGattServiceAndroid::GetGattErrorCode(status));
  145. }
  146. }
  147. BluetoothRemoteGattDescriptorAndroid::BluetoothRemoteGattDescriptorAndroid(
  148. const std::string& instance_id)
  149. : instance_id_(instance_id) {}
  150. } // namespace device