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" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 BluetoothRemoteGattDescriptor* | 121 BluetoothRemoteGattDescriptor* |
122 BluetoothRemoteGattCharacteristicAndroid::GetDescriptor( | 122 BluetoothRemoteGattCharacteristicAndroid::GetDescriptor( |
123 const std::string& identifier) const { | 123 const std::string& identifier) const { |
124 EnsureDescriptorsCreated(); | 124 EnsureDescriptorsCreated(); |
125 const auto& iter = descriptors_.find(identifier); | 125 const auto& iter = descriptors_.find(identifier); |
126 if (iter == descriptors_.end()) | 126 if (iter == descriptors_.end()) |
127 return nullptr; | 127 return nullptr; |
128 return iter->second.get(); | 128 return iter->second.get(); |
129 } | 129 } |
130 | 130 |
131 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( | 131 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristicImpl( |
132 const ValueCallback& callback, | 132 const ValueCallback& callback, |
133 const ErrorCallback& error_callback) { | 133 const ErrorCallback& error_callback) { |
134 if (read_pending_ || write_pending_) { | 134 DCHECK(!HasPendingGattOperation()); |
scheib
2017/04/21 23:33:11
Drop reference to base class details and check the
| |
135 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
136 FROM_HERE, | |
137 base::Bind(error_callback, | |
138 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | |
139 return; | |
140 } | |
141 | 135 |
142 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( | 136 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( |
143 AttachCurrentThread(), j_characteristic_)) { | 137 AttachCurrentThread(), j_characteristic_)) { |
144 base::ThreadTaskRunnerHandle::Get()->PostTask( | 138 base::ThreadTaskRunnerHandle::Get()->PostTask( |
145 FROM_HERE, base::Bind(error_callback, | 139 FROM_HERE, base::Bind(error_callback, |
146 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 140 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
147 return; | 141 return; |
148 } | 142 } |
149 | 143 |
150 read_pending_ = true; | 144 SetPendingGattOperation(true); |
151 read_callback_ = callback; | 145 read_callback_ = callback; |
152 read_error_callback_ = error_callback; | 146 read_error_callback_ = error_callback; |
153 } | 147 } |
154 | 148 |
155 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( | 149 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristicImpl( |
156 const std::vector<uint8_t>& value, | 150 const std::vector<uint8_t>& value, |
157 const base::Closure& callback, | 151 const base::Closure& callback, |
158 const ErrorCallback& error_callback) { | 152 const ErrorCallback& error_callback) { |
159 if (read_pending_ || write_pending_) { | 153 DCHECK(!HasPendingGattOperation()); |
160 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
161 FROM_HERE, | |
162 base::Bind(error_callback, | |
163 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | |
164 return; | |
165 } | |
166 | 154 |
167 JNIEnv* env = AttachCurrentThread(); | 155 JNIEnv* env = AttachCurrentThread(); |
168 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( | 156 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( |
169 env, j_characteristic_, base::android::ToJavaByteArray(env, value))) { | 157 env, j_characteristic_, base::android::ToJavaByteArray(env, value))) { |
170 base::ThreadTaskRunnerHandle::Get()->PostTask( | 158 base::ThreadTaskRunnerHandle::Get()->PostTask( |
171 FROM_HERE, base::Bind(error_callback, | 159 FROM_HERE, base::Bind(error_callback, |
172 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 160 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
173 return; | 161 return; |
174 } | 162 } |
175 | 163 |
176 write_pending_ = true; | 164 SetPendingGattOperation(true); |
177 write_callback_ = callback; | 165 write_callback_ = callback; |
178 write_error_callback_ = error_callback; | 166 write_error_callback_ = error_callback; |
179 } | 167 } |
180 | 168 |
181 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( | 169 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( |
182 JNIEnv* env, | 170 JNIEnv* env, |
183 const JavaParamRef<jobject>& jcaller, | 171 const JavaParamRef<jobject>& jcaller, |
184 const JavaParamRef<jbyteArray>& value) { | 172 const JavaParamRef<jbyteArray>& value) { |
185 base::android::JavaByteArrayToByteVector(env, value, &value_); | 173 base::android::JavaByteArrayToByteVector(env, value, &value_); |
186 adapter_->NotifyGattCharacteristicValueChanged(this, value_); | 174 adapter_->NotifyGattCharacteristicValueChanged(this, value_); |
187 } | 175 } |
188 | 176 |
189 void BluetoothRemoteGattCharacteristicAndroid::OnRead( | 177 void BluetoothRemoteGattCharacteristicAndroid::OnRead( |
190 JNIEnv* env, | 178 JNIEnv* env, |
191 const JavaParamRef<jobject>& jcaller, | 179 const JavaParamRef<jobject>& jcaller, |
192 int32_t status, | 180 int32_t status, |
193 const JavaParamRef<jbyteArray>& value) { | 181 const JavaParamRef<jbyteArray>& value) { |
194 read_pending_ = false; | 182 SetPendingGattOperation(false); |
195 | 183 |
196 // Clear callbacks before calling to avoid reentrancy issues. | 184 // Clear callbacks before calling to avoid reentrancy issues. |
197 ValueCallback read_callback = read_callback_; | 185 ValueCallback read_callback = read_callback_; |
198 ErrorCallback read_error_callback = read_error_callback_; | 186 ErrorCallback read_error_callback = read_error_callback_; |
199 read_callback_.Reset(); | 187 read_callback_.Reset(); |
200 read_error_callback_.Reset(); | 188 read_error_callback_.Reset(); |
201 | 189 |
202 if (status == 0 // android.bluetooth.BluetoothGatt.GATT_SUCCESS | 190 if (status == 0 // android.bluetooth.BluetoothGatt.GATT_SUCCESS |
203 && !read_callback.is_null()) { | 191 && !read_callback.is_null()) { |
204 base::android::JavaByteArrayToByteVector(env, value, &value_); | 192 base::android::JavaByteArrayToByteVector(env, value, &value_); |
205 read_callback.Run(value_); | 193 read_callback.Run(value_); |
206 } else if (!read_error_callback.is_null()) { | 194 } else if (!read_error_callback.is_null()) { |
207 read_error_callback.Run( | 195 read_error_callback.Run( |
208 BluetoothRemoteGattServiceAndroid::GetGattErrorCode(status)); | 196 BluetoothRemoteGattServiceAndroid::GetGattErrorCode(status)); |
209 } | 197 } |
210 } | 198 } |
211 | 199 |
212 void BluetoothRemoteGattCharacteristicAndroid::OnWrite( | 200 void BluetoothRemoteGattCharacteristicAndroid::OnWrite( |
213 JNIEnv* env, | 201 JNIEnv* env, |
214 const JavaParamRef<jobject>& jcaller, | 202 const JavaParamRef<jobject>& jcaller, |
215 int32_t status) { | 203 int32_t status) { |
216 write_pending_ = false; | 204 SetPendingGattOperation(false); |
217 | 205 |
218 // Clear callbacks before calling to avoid reentrancy issues. | 206 // Clear callbacks before calling to avoid reentrancy issues. |
219 base::Closure write_callback = write_callback_; | 207 base::Closure write_callback = write_callback_; |
220 ErrorCallback write_error_callback = write_error_callback_; | 208 ErrorCallback write_error_callback = write_error_callback_; |
221 write_callback_.Reset(); | 209 write_callback_.Reset(); |
222 write_error_callback_.Reset(); | 210 write_error_callback_.Reset(); |
223 | 211 |
224 if (status == 0 // android.bluetooth.BluetoothGatt.GATT_SUCCESS | 212 if (status == 0 // android.bluetooth.BluetoothGatt.GATT_SUCCESS |
225 && !write_callback.is_null()) { | 213 && !write_callback.is_null()) { |
226 write_callback.Run(); | 214 write_callback.Run(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 286 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
299 const { | 287 const { |
300 if (!descriptors_.empty()) | 288 if (!descriptors_.empty()) |
301 return; | 289 return; |
302 | 290 |
303 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 291 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
304 AttachCurrentThread(), j_characteristic_); | 292 AttachCurrentThread(), j_characteristic_); |
305 } | 293 } |
306 | 294 |
307 } // namespace device | 295 } // namespace device |
OLD | NEW |