| Index: components/arc/bluetooth/arc_bluetooth_bridge.cc
|
| diff --git a/components/arc/bluetooth/arc_bluetooth_bridge.cc b/components/arc/bluetooth/arc_bluetooth_bridge.cc
|
| index fdc0a7de6c04afd38d24b03c5f0e77f3e13519cc..7375e5eaeb71765ec0f3ed1da3d13ddc6aa360bc 100644
|
| --- a/components/arc/bluetooth/arc_bluetooth_bridge.cc
|
| +++ b/components/arc/bluetooth/arc_bluetooth_bridge.cc
|
| @@ -9,6 +9,7 @@
|
|
|
| #include <iomanip>
|
| #include <string>
|
| +#include <utility>
|
|
|
| #include "base/bind.h"
|
| #include "base/logging.h"
|
| @@ -292,12 +293,75 @@ void ArcBluetoothBridge::GattDescriptorValueChanged(
|
| // Placeholder for GATT client functionality
|
| }
|
|
|
| +// We will use odd transaction_id for read and even id for write to make sure
|
| +// that it does not overlap and make it easy to determine the transaction type.
|
| +int32_t ArcBluetoothBridge::CreateGattReadTransaction(
|
| + const GattServerReadCallbacks& callbacks) {
|
| + static int32_t transaction_id = 1;
|
| + transaction_id += 2;
|
| + gatt_server_read_callbacks_[transaction_id] = callbacks;
|
| + return transaction_id;
|
| +}
|
| +
|
| +int32_t ArcBluetoothBridge::CreateGattWriteTransaction(
|
| + const GattServerWriteCallbacks& callbacks) {
|
| + static int32_t transaction_id = 2;
|
| + transaction_id += 2;
|
| + gatt_server_write_callbacks_[transaction_id] = callbacks;
|
| + return transaction_id;
|
| +}
|
| +
|
| +template <class T>
|
| +void ArcBluetoothBridge::OnGattAttributeReadRequest(
|
| + const BluetoothDevice* device,
|
| + const T* gatt_obj,
|
| + int offset,
|
| + const GattServerReadCallbacks& callbacks) {
|
| + if (!HasBluetoothInstance())
|
| + return;
|
| +
|
| + if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
|
| + return;
|
| +
|
| + int32_t transaction_id = CreateGattReadTransaction(callbacks);
|
| +
|
| + arc_bridge_service()->bluetooth()->instance()->RequestGattRead(
|
| + mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
|
| + ConvertGattIdentifierToId(gatt_obj->GetIdentifier()), offset,
|
| + false /*is_long */);
|
| +}
|
| +
|
| +template <class T>
|
| +void ArcBluetoothBridge::OnGattAttributeWriteRequest(
|
| + const BluetoothDevice* device,
|
| + const T* gatt_obj,
|
| + const std::vector<uint8_t>& value,
|
| + int offset,
|
| + const GattServerWriteCallbacks& callbacks) {
|
| + if (!HasBluetoothInstance())
|
| + return;
|
| +
|
| + if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
|
| + return;
|
| +
|
| + int32_t transaction_id = CreateGattWriteTransaction(callbacks);
|
| +
|
| + arc_bridge_service()->bluetooth()->instance()->RequestGattWrite(
|
| + mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
|
| + ConvertGattIdentifierToId(gatt_obj->GetIdentifier()), offset,
|
| + mojo::Array<uint8_t>::From(value));
|
| +}
|
| +
|
| void ArcBluetoothBridge::OnCharacteristicReadRequest(
|
| const BluetoothDevice* device,
|
| const BluetoothLocalGattCharacteristic* characteristic,
|
| int offset,
|
| const ValueCallback& callback,
|
| - const ErrorCallback& error_callback) {}
|
| + const ErrorCallback& error_callback) {
|
| + OnGattAttributeReadRequest<BluetoothLocalGattCharacteristic>(
|
| + device, characteristic, offset,
|
| + GattServerReadCallbacks(callback, error_callback));
|
| +}
|
|
|
| void ArcBluetoothBridge::OnCharacteristicWriteRequest(
|
| const BluetoothDevice* device,
|
| @@ -305,14 +369,22 @@ void ArcBluetoothBridge::OnCharacteristicWriteRequest(
|
| const std::vector<uint8_t>& value,
|
| int offset,
|
| const base::Closure& callback,
|
| - const ErrorCallback& error_callback) {}
|
| + const ErrorCallback& error_callback) {
|
| + OnGattAttributeWriteRequest<BluetoothLocalGattCharacteristic>(
|
| + device, characteristic, value, offset,
|
| + GattServerWriteCallbacks(callback, error_callback));
|
| +}
|
|
|
| void ArcBluetoothBridge::OnDescriptorReadRequest(
|
| const BluetoothDevice* device,
|
| const BluetoothLocalGattDescriptor* descriptor,
|
| int offset,
|
| const ValueCallback& callback,
|
| - const ErrorCallback& error_callback) {}
|
| + const ErrorCallback& error_callback) {
|
| + OnGattAttributeReadRequest<BluetoothLocalGattDescriptor>(
|
| + device, descriptor, offset,
|
| + GattServerReadCallbacks(callback, error_callback));
|
| +}
|
|
|
| void ArcBluetoothBridge::OnDescriptorWriteRequest(
|
| const BluetoothDevice* device,
|
| @@ -320,7 +392,11 @@ void ArcBluetoothBridge::OnDescriptorWriteRequest(
|
| const std::vector<uint8_t>& value,
|
| int offset,
|
| const base::Closure& callback,
|
| - const ErrorCallback& error_callback) {}
|
| + const ErrorCallback& error_callback) {
|
| + OnGattAttributeWriteRequest<BluetoothLocalGattDescriptor>(
|
| + device, descriptor, value, offset,
|
| + GattServerWriteCallbacks(callback, error_callback));
|
| +}
|
|
|
| void ArcBluetoothBridge::OnNotificationsStart(
|
| const BluetoothDevice* device,
|
| @@ -1160,7 +1236,26 @@ void ArcBluetoothBridge::SendIndication(
|
| void ArcBluetoothBridge::SendResponse(int32_t trans_id,
|
| int32_t status,
|
| mojo::Array<uint8_t> value,
|
| - const SendResponseCallback& callback) {}
|
| + const SendResponseCallback& callback) {
|
| + // We used odd transaction_id for read and even id for write.
|
| + if (trans_id & 1) { // read
|
| + DCHECK(gatt_server_read_callbacks_.find(trans_id) !=
|
| + gatt_server_read_callbacks_.end());
|
| + if (status)
|
| + gatt_server_read_callbacks_[trans_id].error_callback.Run();
|
| + else
|
| + gatt_server_read_callbacks_[trans_id].success_callback.Run(
|
| + value.To<std::vector<uint8_t>>());
|
| + } else { // write
|
| + DCHECK(gatt_server_write_callbacks_.find(trans_id) !=
|
| + gatt_server_write_callbacks_.end());
|
| + if (status)
|
| + gatt_server_write_callbacks_[trans_id].error_callback.Run();
|
| + else
|
| + gatt_server_write_callbacks_[trans_id].success_callback.Run();
|
| + }
|
| + OnGattOperationDone(callback);
|
| +}
|
|
|
| void ArcBluetoothBridge::OnDiscoveryError() {
|
| LOG(WARNING) << "failed to change discovery state";
|
|
|