OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_android.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "device/bluetooth/bluetooth_adapter_android.h" | 17 #include "device/bluetooth/bluetooth_adapter_android.h" |
| 18 #include "device/bluetooth/bluetooth_gatt_notify_session_android.h" |
18 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_android.h" | 19 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_android.h" |
19 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" | 20 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" |
20 #include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" | 21 #include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" |
21 | 22 |
22 using base::android::AttachCurrentThread; | 23 using base::android::AttachCurrentThread; |
23 using base::android::JavaParamRef; | 24 using base::android::JavaParamRef; |
24 | 25 |
25 namespace device { | 26 namespace device { |
26 | 27 |
27 // static | 28 // static |
(...skipping 17 matching lines...) Expand all Loading... |
45 base::android::ConvertUTF8ToJavaString(env, instance_id), | 46 base::android::ConvertUTF8ToJavaString(env, instance_id), |
46 chrome_bluetooth_device)); | 47 chrome_bluetooth_device)); |
47 | 48 |
48 return characteristic; | 49 return characteristic; |
49 } | 50 } |
50 | 51 |
51 BluetoothRemoteGattCharacteristicAndroid:: | 52 BluetoothRemoteGattCharacteristicAndroid:: |
52 ~BluetoothRemoteGattCharacteristicAndroid() { | 53 ~BluetoothRemoteGattCharacteristicAndroid() { |
53 Java_ChromeBluetoothRemoteGattCharacteristic_onBluetoothRemoteGattCharacterist
icAndroidDestruction( | 54 Java_ChromeBluetoothRemoteGattCharacteristic_onBluetoothRemoteGattCharacterist
icAndroidDestruction( |
54 AttachCurrentThread(), j_characteristic_); | 55 AttachCurrentThread(), j_characteristic_); |
| 56 |
| 57 if (pending_start_notify_calls_.size()) { |
| 58 OnStartNotifySessionError( |
| 59 device::BluetoothRemoteGattService::GATT_ERROR_FAILED); |
| 60 } |
55 } | 61 } |
56 | 62 |
57 // static | 63 // static |
58 bool BluetoothRemoteGattCharacteristicAndroid::RegisterJNI(JNIEnv* env) { | 64 bool BluetoothRemoteGattCharacteristicAndroid::RegisterJNI(JNIEnv* env) { |
59 return RegisterNativesImpl( | 65 return RegisterNativesImpl( |
60 env); // Generated in ChromeBluetoothRemoteGattCharacteristic_jni.h | 66 env); // Generated in ChromeBluetoothRemoteGattCharacteristic_jni.h |
61 } | 67 } |
62 | 68 |
63 base::android::ScopedJavaLocalRef<jobject> | 69 base::android::ScopedJavaLocalRef<jobject> |
64 BluetoothRemoteGattCharacteristicAndroid::GetJavaObject() { | 70 BluetoothRemoteGattCharacteristicAndroid::GetJavaObject() { |
(...skipping 25 matching lines...) Expand all Loading... |
90 return Java_ChromeBluetoothRemoteGattCharacteristic_getProperties( | 96 return Java_ChromeBluetoothRemoteGattCharacteristic_getProperties( |
91 AttachCurrentThread(), j_characteristic_); | 97 AttachCurrentThread(), j_characteristic_); |
92 } | 98 } |
93 | 99 |
94 BluetoothRemoteGattCharacteristic::Permissions | 100 BluetoothRemoteGattCharacteristic::Permissions |
95 BluetoothRemoteGattCharacteristicAndroid::GetPermissions() const { | 101 BluetoothRemoteGattCharacteristicAndroid::GetPermissions() const { |
96 NOTIMPLEMENTED(); | 102 NOTIMPLEMENTED(); |
97 return 0; | 103 return 0; |
98 } | 104 } |
99 | 105 |
| 106 bool BluetoothRemoteGattCharacteristicAndroid::IsNotifying() const { |
| 107 NOTIMPLEMENTED(); |
| 108 return false; |
| 109 } |
| 110 |
100 std::vector<BluetoothRemoteGattDescriptor*> | 111 std::vector<BluetoothRemoteGattDescriptor*> |
101 BluetoothRemoteGattCharacteristicAndroid::GetDescriptors() const { | 112 BluetoothRemoteGattCharacteristicAndroid::GetDescriptors() const { |
102 EnsureDescriptorsCreated(); | 113 EnsureDescriptorsCreated(); |
103 std::vector<BluetoothRemoteGattDescriptor*> descriptors; | 114 std::vector<BluetoothRemoteGattDescriptor*> descriptors; |
104 for (const auto& map_iter : descriptors_) | 115 for (const auto& map_iter : descriptors_) |
105 descriptors.push_back(map_iter.second); | 116 descriptors.push_back(map_iter.second); |
106 return descriptors; | 117 return descriptors; |
107 } | 118 } |
108 | 119 |
109 BluetoothRemoteGattDescriptor* | 120 BluetoothRemoteGattDescriptor* |
110 BluetoothRemoteGattCharacteristicAndroid::GetDescriptor( | 121 BluetoothRemoteGattCharacteristicAndroid::GetDescriptor( |
111 const std::string& identifier) const { | 122 const std::string& identifier) const { |
112 EnsureDescriptorsCreated(); | 123 EnsureDescriptorsCreated(); |
113 const auto& iter = descriptors_.find(identifier); | 124 const auto& iter = descriptors_.find(identifier); |
114 if (iter == descriptors_.end()) | 125 if (iter == descriptors_.end()) |
115 return nullptr; | 126 return nullptr; |
116 return iter->second; | 127 return iter->second; |
117 } | 128 } |
118 | 129 |
| 130 void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( |
| 131 const NotifySessionCallback& callback, |
| 132 const ErrorCallback& error_callback) { |
| 133 if (!pending_start_notify_calls_.empty()) { |
| 134 pending_start_notify_calls_.push_back( |
| 135 std::make_pair(callback, error_callback)); |
| 136 return; |
| 137 } |
| 138 |
| 139 Properties properties = GetProperties(); |
| 140 |
| 141 bool hasNotify = properties & PROPERTY_NOTIFY; |
| 142 bool hasIndicate = properties & PROPERTY_INDICATE; |
| 143 |
| 144 if (!hasNotify && !hasIndicate) { |
| 145 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; |
| 146 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 147 FROM_HERE, |
| 148 base::Bind(error_callback, |
| 149 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
| 150 return; |
| 151 } |
| 152 |
| 153 std::vector<BluetoothRemoteGattDescriptor*> ccc_descriptor = |
| 154 GetDescriptorsByUUID(BluetoothRemoteGattDescriptor:: |
| 155 ClientCharacteristicConfigurationUuid()); |
| 156 |
| 157 if (ccc_descriptor.size() != 1u) { |
| 158 LOG(ERROR) << "Found " << ccc_descriptor.size() |
| 159 << " client characteristic configuration descriptors."; |
| 160 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 161 FROM_HERE, |
| 162 base::Bind(error_callback, |
| 163 (ccc_descriptor.size() == 0) |
| 164 ? BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED |
| 165 : BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 166 return; |
| 167 } |
| 168 |
| 169 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( |
| 170 AttachCurrentThread(), j_characteristic_, true)) { |
| 171 LOG(ERROR) << "Error enabling characteristic notification"; |
| 172 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 173 FROM_HERE, base::Bind(error_callback, |
| 174 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
| 175 return; |
| 176 } |
| 177 |
| 178 std::vector<uint8_t> value(2); |
| 179 value[0] = hasNotify ? 1 : 2; |
| 180 |
| 181 pending_start_notify_calls_.push_back( |
| 182 std::make_pair(callback, error_callback)); |
| 183 ccc_descriptor[0]->WriteRemoteDescriptor( |
| 184 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: |
| 185 OnStartNotifySessionSuccess, |
| 186 base::Unretained(this)), |
| 187 base::Bind( |
| 188 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, |
| 189 base::Unretained(this))); |
| 190 } |
| 191 |
119 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( | 192 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( |
120 const ValueCallback& callback, | 193 const ValueCallback& callback, |
121 const ErrorCallback& error_callback) { | 194 const ErrorCallback& error_callback) { |
122 if (read_pending_ || write_pending_) { | 195 if (read_pending_ || write_pending_) { |
123 base::ThreadTaskRunnerHandle::Get()->PostTask( | 196 base::ThreadTaskRunnerHandle::Get()->PostTask( |
124 FROM_HERE, | 197 FROM_HERE, |
125 base::Bind(error_callback, | 198 base::Bind(error_callback, |
126 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | 199 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
127 return; | 200 return; |
128 } | 201 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 } | 241 } |
169 | 242 |
170 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( | 243 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( |
171 JNIEnv* env, | 244 JNIEnv* env, |
172 const JavaParamRef<jobject>& jcaller, | 245 const JavaParamRef<jobject>& jcaller, |
173 const JavaParamRef<jbyteArray>& value) { | 246 const JavaParamRef<jbyteArray>& value) { |
174 base::android::JavaByteArrayToByteVector(env, value, &value_); | 247 base::android::JavaByteArrayToByteVector(env, value, &value_); |
175 adapter_->NotifyGattCharacteristicValueChanged(this, value_); | 248 adapter_->NotifyGattCharacteristicValueChanged(this, value_); |
176 } | 249 } |
177 | 250 |
| 251 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionSuccess() { |
| 252 std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; |
| 253 reentrant_safe_callbacks.swap(pending_start_notify_calls_); |
| 254 |
| 255 for (const auto& callback_pair : reentrant_safe_callbacks) { |
| 256 std::unique_ptr<device::BluetoothGattNotifySession> notify_session( |
| 257 new BluetoothGattNotifySessionAndroid(instance_id_)); |
| 258 callback_pair.first.Run(std::move(notify_session)); |
| 259 } |
| 260 } |
| 261 |
| 262 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError( |
| 263 BluetoothRemoteGattService::GattErrorCode error) { |
| 264 std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; |
| 265 reentrant_safe_callbacks.swap(pending_start_notify_calls_); |
| 266 |
| 267 for (auto const& callback_pair : reentrant_safe_callbacks) { |
| 268 callback_pair.second.Run(error); |
| 269 } |
| 270 } |
| 271 |
178 void BluetoothRemoteGattCharacteristicAndroid::OnRead( | 272 void BluetoothRemoteGattCharacteristicAndroid::OnRead( |
179 JNIEnv* env, | 273 JNIEnv* env, |
180 const JavaParamRef<jobject>& jcaller, | 274 const JavaParamRef<jobject>& jcaller, |
181 int32_t status, | 275 int32_t status, |
182 const JavaParamRef<jbyteArray>& value) { | 276 const JavaParamRef<jbyteArray>& value) { |
183 read_pending_ = false; | 277 read_pending_ = false; |
184 | 278 |
185 // Clear callbacks before calling to avoid reentrancy issues. | 279 // Clear callbacks before calling to avoid reentrancy issues. |
186 ValueCallback read_callback = read_callback_; | 280 ValueCallback read_callback = read_callback_; |
187 ErrorCallback read_error_callback = read_error_callback_; | 281 ErrorCallback read_error_callback = read_error_callback_; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 base::android::ConvertJavaStringToUTF8(env, instanceId); | 327 base::android::ConvertJavaStringToUTF8(env, instanceId); |
234 | 328 |
235 DCHECK(!descriptors_.contains(instanceIdString)); | 329 DCHECK(!descriptors_.contains(instanceIdString)); |
236 | 330 |
237 descriptors_.set(instanceIdString, | 331 descriptors_.set(instanceIdString, |
238 BluetoothRemoteGattDescriptorAndroid::Create( | 332 BluetoothRemoteGattDescriptorAndroid::Create( |
239 instanceIdString, bluetooth_gatt_descriptor_wrapper, | 333 instanceIdString, bluetooth_gatt_descriptor_wrapper, |
240 chrome_bluetooth_device)); | 334 chrome_bluetooth_device)); |
241 } | 335 } |
242 | 336 |
243 void BluetoothRemoteGattCharacteristicAndroid::SubscribeToNotifications( | |
244 BluetoothRemoteGattDescriptor* ccc_descriptor, | |
245 const base::Closure& callback, | |
246 const ErrorCallback& error_callback) { | |
247 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( | |
248 AttachCurrentThread(), j_characteristic_.obj(), true)) { | |
249 LOG(ERROR) << "Error enabling characteristic notification"; | |
250 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
251 FROM_HERE, base::Bind(error_callback, | |
252 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | |
253 return; | |
254 } | |
255 | |
256 bool hasNotify = GetProperties() & PROPERTY_NOTIFY; | |
257 std::vector<uint8_t> value(2); | |
258 value[0] = hasNotify ? 1 : 2; | |
259 | |
260 ccc_descriptor->WriteRemoteDescriptor(value, callback, error_callback); | |
261 } | |
262 | |
263 void BluetoothRemoteGattCharacteristicAndroid::UnsubscribeFromNotifications( | |
264 BluetoothRemoteGattDescriptor* ccc_descriptor, | |
265 const base::Closure& callback, | |
266 const ErrorCallback& error_callback) { | |
267 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( | |
268 AttachCurrentThread(), j_characteristic_.obj(), false)) { | |
269 LOG(ERROR) << "Error disabling characteristic notification"; | |
270 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
271 FROM_HERE, | |
272 base::Bind(error_callback, | |
273 device::BluetoothRemoteGattService::GATT_ERROR_FAILED)); | |
274 return; | |
275 } | |
276 | |
277 std::vector<uint8_t> value(2); | |
278 value[0] = 0; | |
279 | |
280 ccc_descriptor->WriteRemoteDescriptor(value, callback, error_callback); | |
281 } | |
282 | |
283 BluetoothRemoteGattCharacteristicAndroid:: | 337 BluetoothRemoteGattCharacteristicAndroid:: |
284 BluetoothRemoteGattCharacteristicAndroid( | 338 BluetoothRemoteGattCharacteristicAndroid( |
285 BluetoothAdapterAndroid* adapter, | 339 BluetoothAdapterAndroid* adapter, |
286 BluetoothRemoteGattServiceAndroid* service, | 340 BluetoothRemoteGattServiceAndroid* service, |
287 const std::string& instance_id) | 341 const std::string& instance_id) |
288 : adapter_(adapter), service_(service), instance_id_(instance_id) {} | 342 : adapter_(adapter), service_(service), instance_id_(instance_id) {} |
289 | 343 |
290 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 344 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
291 const { | 345 const { |
292 if (!descriptors_.empty()) | 346 if (!descriptors_.empty()) |
293 return; | 347 return; |
294 | 348 |
295 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 349 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
296 AttachCurrentThread(), j_characteristic_); | 350 AttachCurrentThread(), j_characteristic_); |
297 } | 351 } |
298 | 352 |
299 } // namespace device | 353 } // namespace device |
OLD | NEW |