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

Unified 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: Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
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 25d84afd842c48ab8119d8a54f3f584372cce00d..0d40462d74ae242c4aa1d54238fd2bcae51eee68 100644
--- a/components/arc/bluetooth/arc_bluetooth_bridge.cc
+++ b/components/arc/bluetooth/arc_bluetooth_bridge.cc
@@ -298,12 +298,81 @@ void ArcBluetoothBridge::GattDescriptorValueChanged(
// Placeholder for GATT client functionality
}
+// We will use odd transaction_id for read and even id for write.
rkc 2016/07/01 22:51:32 Why?
puthik_chromium 2016/07/01 22:56:09 1. To make sure that it does not repeat same numbe
rkc 2016/07/01 23:04:02 You got me wrong :) I was saying that the comment
puthik_chromium 2016/07/14 22:45:11 Done.
+int32_t ArcBluetoothBridge::CreateGattReadTransaction(
+ const ValueCallback& successCallback,
+ const ErrorCallback& errorCallback) {
+ static int32_t transaction_id = 1;
+ transaction_id += 2;
+ gatt_read_callbacks_.emplace(
rkc 2016/07/01 22:51:32 Can't use map::emplace. https://chromium-cpp.appsp
puthik_chromium 2016/07/14 22:45:12 Done.
+ transaction_id,
+ std::pair<ValueCallback, ErrorCallback>(successCallback, errorCallback));
Eric Caruso 2016/06/30 23:46:20 Instead of using std::pair here and pair::first an
puthik_chromium 2016/07/14 22:45:12 Done.
+ return transaction_id;
+}
+
+int32_t ArcBluetoothBridge::CreateGattWriteTransaction(
+ const base::Closure& successCallback,
+ const ErrorCallback& errorCallback) {
+ static int32_t transaction_id = 2;
+ transaction_id += 2;
+ gatt_write_callbacks_.emplace(
+ transaction_id,
+ std::pair<base::Closure, ErrorCallback>(successCallback, errorCallback));
+ return transaction_id;
+}
+
+template <class T>
+void ArcBluetoothBridge::OnGattAttributeReadRequest(
+ const BluetoothDevice* device,
+ const T* gatt_obj,
+ int offset,
+ const ValueCallback& callback,
+ const ErrorCallback& error_callback) {
+ if (!HasBluetoothInstance())
+ return;
+
+ if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
+ return;
+
+ int32_t transaction_id = CreateGattReadTransaction(callback, error_callback);
+
+ 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 base::Closure& callback,
+ const ErrorCallback& error_callback) {
+ if (!HasBluetoothInstance())
+ return;
+
+ if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
+ return;
+
+ int32_t transaction_id = CreateGattWriteTransaction(callback, error_callback);
+
+ 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, callback, error_callback);
+}
void ArcBluetoothBridge::OnCharacteristicWriteRequest(
const BluetoothDevice* device,
@@ -311,14 +380,20 @@ 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, 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, callback, error_callback);
+}
void ArcBluetoothBridge::OnDescriptorWriteRequest(
const BluetoothDevice* device,
@@ -326,7 +401,10 @@ 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, callback, error_callback);
+}
void ArcBluetoothBridge::OnNotificationsStart(
const BluetoothDevice* device,
@@ -1145,7 +1223,24 @@ 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 will use odd transaction_id for read and even id for write.
+ if (trans_id & 1) { // read
+ DCHECK(gatt_read_callbacks_.find(trans_id) != gatt_read_callbacks_.end());
+ if (status)
+ gatt_read_callbacks_[trans_id].second.Run();
+ else
+ gatt_read_callbacks_[trans_id].first.Run(
+ value.To<std::vector<uint8_t>>());
+ } else { // write
+ DCHECK(gatt_write_callbacks_.find(trans_id) != gatt_write_callbacks_.end());
+ if (status)
+ gatt_write_callbacks_[trans_id].second.Run();
+ else
+ gatt_write_callbacks_[trans_id].first.Run();
+ }
+ OnGattOperationDone(callback);
+}
void ArcBluetoothBridge::OnDiscoveryError() {
LOG(WARNING) << "failed to change discovery state";

Powered by Google App Engine
This is Rietveld 408576698