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

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: run error_callback if offset < 0 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 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 57ae9914b3815ce532d1324765a8f088a851456e..631c3cf4f974e2f7548716ca41d324010bfdaad3 100644
--- a/components/arc/bluetooth/arc_bluetooth_bridge.cc
+++ b/components/arc/bluetooth/arc_bluetooth_bridge.cc
@@ -293,12 +293,74 @@ 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 LocalGattAttribute>
+void ArcBluetoothBridge::OnGattAttributeReadRequest(
+ const BluetoothDevice* device,
+ const LocalGattAttribute* gatt_obj,
+ int offset,
+ const GattServerReadCallbacks& callbacks) {
+ if (!HasBluetoothInstance() ||
+ !CheckBluetoothInstanceVersion(kMinBtleNotifyVersion) || offset < 0) {
+ callbacks.error_callback.Run();
+ return;
+ }
+
+ int32_t transaction_id = CreateGattReadTransaction(callbacks);
+
+ arc_bridge_service()->bluetooth()->instance()->RequestGattRead(
+ mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
+ gatt_handle_[gatt_obj->GetIdentifier()], offset, false /*is_long */);
Luis Héctor Chávez 2016/07/19 01:49:17 What happens if |gatt_obj->GetIdentifier()| is inv
puthik_chromium 2016/07/19 22:21:47 It should valid all the time because we are delega
+}
+
+template <class LocalGattAttribute>
+void ArcBluetoothBridge::OnGattAttributeWriteRequest(
+ const BluetoothDevice* device,
+ const LocalGattAttribute* gatt_obj,
+ const std::vector<uint8_t>& value,
+ int offset,
+ const GattServerWriteCallbacks& callbacks) {
+ if (!HasBluetoothInstance() ||
+ !CheckBluetoothInstanceVersion(kMinBtleNotifyVersion) || offset < 0) {
+ callbacks.error_callback.Run();
+ return;
+ }
+
+ int32_t transaction_id = CreateGattWriteTransaction(callbacks);
+
+ arc_bridge_service()->bluetooth()->instance()->RequestGattWrite(
Luis Héctor Chávez 2016/07/19 01:49:16 Instead of doing this transaction magic, is it pos
puthik_chromium 2016/07/19 22:21:47 This require 3 calls. Chrome to Android: RequestGa
Luis Héctor Chávez 2016/07/19 22:38:00 What do you need the callback of SendResponse for?
puthik_chromium 2016/07/19 23:05:43 Look like the response of SendResponse is optional
puthik_chromium 2016/07/20 01:42:09 Done. SendResponse removed
+ mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
+ gatt_handle_[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,
@@ -306,14 +368,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,
@@ -321,7 +391,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,
@@ -1064,6 +1138,7 @@ int32_t ArcBluetoothBridge::CreateGattAttributeHandle(
gatt_server_attr_handle++;
std::string identifier = gatt_attr->GetIdentifier();
gatt_identifier_[gatt_server_attr_handle] = identifier;
+ gatt_handle_[identifier] = gatt_server_attr_handle;
return gatt_server_attr_handle;
}
@@ -1150,8 +1225,9 @@ void ArcBluetoothBridge::DeleteService(int32_t service_handle,
const DeleteServiceCallback& callback) {
BluetoothLocalGattService* service =
bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
- service->Delete();
gatt_identifier_.erase(service_handle);
+ gatt_handle_.erase(service->GetIdentifier());
+ service->Delete();
OnGattOperationDone(callback);
}
@@ -1165,7 +1241,27 @@ 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";

Powered by Google App Engine
This is Rietveld 408576698