Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2101283003: arc: bluetooth: Implement Gatt server request to read/write (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gs2
Patch Set: Add GattServerCallbacks type to store the callbacks Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/arc/bluetooth/arc_bluetooth_bridge.h" 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <iomanip> 10 #include <iomanip>
11 #include <string> 11 #include <string>
12 #include <utility>
12 13
13 #include "base/bind.h" 14 #include "base/bind.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/posix/eintr_wrapper.h" 16 #include "base/posix/eintr_wrapper.h"
16 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
19 #include "base/time/time.h" 20 #include "base/time/time.h"
20 #include "components/arc/arc_bridge_service.h" 21 #include "components/arc/arc_bridge_service.h"
21 #include "components/arc/bluetooth/bluetooth_type_converters.h" 22 #include "components/arc/bluetooth/bluetooth_type_converters.h"
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 true /* is_notify */, mojo::Array<uint8_t>::From(value)); 286 true /* is_notify */, mojo::Array<uint8_t>::From(value));
286 } 287 }
287 288
288 void ArcBluetoothBridge::GattDescriptorValueChanged( 289 void ArcBluetoothBridge::GattDescriptorValueChanged(
289 BluetoothAdapter* adapter, 290 BluetoothAdapter* adapter,
290 BluetoothRemoteGattDescriptor* descriptor, 291 BluetoothRemoteGattDescriptor* descriptor,
291 const std::vector<uint8_t>& value) { 292 const std::vector<uint8_t>& value) {
292 // Placeholder for GATT client functionality 293 // Placeholder for GATT client functionality
293 } 294 }
294 295
296 // We will use odd transaction_id for read and even id for write to make sure
297 // that it does not overlap and make it easy to determine the transaction type.
298 int32_t ArcBluetoothBridge::CreateGattReadTransaction(
299 const GattServerReadCallbacks& callbacks) {
300 static int32_t transaction_id = 1;
301 transaction_id += 2;
302 gatt_server_read_callbacks_[transaction_id] = callbacks;
303 return transaction_id;
304 }
305
306 int32_t ArcBluetoothBridge::CreateGattWriteTransaction(
307 const GattServerWriteCallbacks& callbacks) {
308 static int32_t transaction_id = 2;
309 transaction_id += 2;
310 gatt_server_write_callbacks_[transaction_id] = callbacks;
311 return transaction_id;
312 }
313
314 template <class T>
315 void ArcBluetoothBridge::OnGattAttributeReadRequest(
316 const BluetoothDevice* device,
317 const T* gatt_obj,
318 int offset,
319 const GattServerReadCallbacks& callbacks) {
320 if (!HasBluetoothInstance())
321 return;
322
323 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
324 return;
325
326 int32_t transaction_id = CreateGattReadTransaction(callbacks);
327
328 arc_bridge_service()->bluetooth()->instance()->RequestGattRead(
329 mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
330 ConvertGattIdentifierToId(gatt_obj->GetIdentifier()), offset,
331 false /*is_long */);
332 }
333
334 template <class T>
335 void ArcBluetoothBridge::OnGattAttributeWriteRequest(
336 const BluetoothDevice* device,
337 const T* gatt_obj,
338 const std::vector<uint8_t>& value,
339 int offset,
340 const GattServerWriteCallbacks& callbacks) {
341 if (!HasBluetoothInstance())
342 return;
343
344 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
345 return;
346
347 int32_t transaction_id = CreateGattWriteTransaction(callbacks);
348
349 arc_bridge_service()->bluetooth()->instance()->RequestGattWrite(
350 mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
351 ConvertGattIdentifierToId(gatt_obj->GetIdentifier()), offset,
352 mojo::Array<uint8_t>::From(value));
353 }
354
295 void ArcBluetoothBridge::OnCharacteristicReadRequest( 355 void ArcBluetoothBridge::OnCharacteristicReadRequest(
296 const BluetoothDevice* device, 356 const BluetoothDevice* device,
297 const BluetoothLocalGattCharacteristic* characteristic, 357 const BluetoothLocalGattCharacteristic* characteristic,
298 int offset, 358 int offset,
299 const ValueCallback& callback, 359 const ValueCallback& callback,
300 const ErrorCallback& error_callback) {} 360 const ErrorCallback& error_callback) {
361 OnGattAttributeReadRequest<BluetoothLocalGattCharacteristic>(
362 device, characteristic, offset,
363 GattServerReadCallbacks(callback, error_callback));
364 }
301 365
302 void ArcBluetoothBridge::OnCharacteristicWriteRequest( 366 void ArcBluetoothBridge::OnCharacteristicWriteRequest(
303 const BluetoothDevice* device, 367 const BluetoothDevice* device,
304 const BluetoothLocalGattCharacteristic* characteristic, 368 const BluetoothLocalGattCharacteristic* characteristic,
305 const std::vector<uint8_t>& value, 369 const std::vector<uint8_t>& value,
306 int offset, 370 int offset,
307 const base::Closure& callback, 371 const base::Closure& callback,
308 const ErrorCallback& error_callback) {} 372 const ErrorCallback& error_callback) {
373 OnGattAttributeWriteRequest<BluetoothLocalGattCharacteristic>(
374 device, characteristic, value, offset,
375 GattServerWriteCallbacks(callback, error_callback));
376 }
309 377
310 void ArcBluetoothBridge::OnDescriptorReadRequest( 378 void ArcBluetoothBridge::OnDescriptorReadRequest(
311 const BluetoothDevice* device, 379 const BluetoothDevice* device,
312 const BluetoothLocalGattDescriptor* descriptor, 380 const BluetoothLocalGattDescriptor* descriptor,
313 int offset, 381 int offset,
314 const ValueCallback& callback, 382 const ValueCallback& callback,
315 const ErrorCallback& error_callback) {} 383 const ErrorCallback& error_callback) {
384 OnGattAttributeReadRequest<BluetoothLocalGattDescriptor>(
385 device, descriptor, offset,
386 GattServerReadCallbacks(callback, error_callback));
387 }
316 388
317 void ArcBluetoothBridge::OnDescriptorWriteRequest( 389 void ArcBluetoothBridge::OnDescriptorWriteRequest(
318 const BluetoothDevice* device, 390 const BluetoothDevice* device,
319 const BluetoothLocalGattDescriptor* descriptor, 391 const BluetoothLocalGattDescriptor* descriptor,
320 const std::vector<uint8_t>& value, 392 const std::vector<uint8_t>& value,
321 int offset, 393 int offset,
322 const base::Closure& callback, 394 const base::Closure& callback,
323 const ErrorCallback& error_callback) {} 395 const ErrorCallback& error_callback) {
396 OnGattAttributeWriteRequest<BluetoothLocalGattDescriptor>(
397 device, descriptor, value, offset,
398 GattServerWriteCallbacks(callback, error_callback));
399 }
324 400
325 void ArcBluetoothBridge::OnNotificationsStart( 401 void ArcBluetoothBridge::OnNotificationsStart(
326 const BluetoothDevice* device, 402 const BluetoothDevice* device,
327 const BluetoothLocalGattCharacteristic* characteristic) {} 403 const BluetoothLocalGattCharacteristic* characteristic) {}
328 404
329 void ArcBluetoothBridge::OnNotificationsStop( 405 void ArcBluetoothBridge::OnNotificationsStop(
330 const BluetoothDevice* device, 406 const BluetoothDevice* device,
331 const BluetoothLocalGattCharacteristic* characteristic) {} 407 const BluetoothLocalGattCharacteristic* characteristic) {}
332 408
333 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { 409 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) {
(...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after
1153 void ArcBluetoothBridge::SendIndication( 1229 void ArcBluetoothBridge::SendIndication(
1154 int32_t attribute_handle, 1230 int32_t attribute_handle,
1155 mojom::BluetoothAddressPtr address, 1231 mojom::BluetoothAddressPtr address,
1156 bool confirm, 1232 bool confirm,
1157 mojo::Array<uint8_t> value, 1233 mojo::Array<uint8_t> value,
1158 const SendIndicationCallback& callback) {} 1234 const SendIndicationCallback& callback) {}
1159 1235
1160 void ArcBluetoothBridge::SendResponse(int32_t trans_id, 1236 void ArcBluetoothBridge::SendResponse(int32_t trans_id,
1161 int32_t status, 1237 int32_t status,
1162 mojo::Array<uint8_t> value, 1238 mojo::Array<uint8_t> value,
1163 const SendResponseCallback& callback) {} 1239 const SendResponseCallback& callback) {
1240 // We used odd transaction_id for read and even id for write.
1241 if (trans_id & 1) { // read
1242 DCHECK(gatt_server_read_callbacks_.find(trans_id) !=
1243 gatt_server_read_callbacks_.end());
1244 if (status)
1245 gatt_server_read_callbacks_[trans_id].error_callback.Run();
1246 else
1247 gatt_server_read_callbacks_[trans_id].success_callback.Run(
1248 value.To<std::vector<uint8_t>>());
1249 } else { // write
1250 DCHECK(gatt_server_write_callbacks_.find(trans_id) !=
1251 gatt_server_write_callbacks_.end());
1252 if (status)
1253 gatt_server_write_callbacks_[trans_id].error_callback.Run();
1254 else
1255 gatt_server_write_callbacks_[trans_id].success_callback.Run();
1256 }
1257 OnGattOperationDone(callback);
1258 }
1164 1259
1165 void ArcBluetoothBridge::OnDiscoveryError() { 1260 void ArcBluetoothBridge::OnDiscoveryError() {
1166 LOG(WARNING) << "failed to change discovery state"; 1261 LOG(WARNING) << "failed to change discovery state";
1167 } 1262 }
1168 1263
1169 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { 1264 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const {
1170 if (!HasBluetoothInstance()) 1265 if (!HasBluetoothInstance())
1171 return; 1266 return;
1172 1267
1173 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( 1268 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged(
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
1493 uint32_t version_need) const { 1588 uint32_t version_need) const {
1494 uint32_t version = arc_bridge_service()->bluetooth()->version(); 1589 uint32_t version = arc_bridge_service()->bluetooth()->version();
1495 if (version >= version_need) 1590 if (version >= version_need)
1496 return true; 1591 return true;
1497 LOG(WARNING) << "Bluetooth instance is too old (version " << version 1592 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1498 << ") need version " << version_need; 1593 << ") need version " << version_need;
1499 return false; 1594 return false;
1500 } 1595 }
1501 1596
1502 } // namespace arc 1597 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698