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

Unified Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2104043002: arc: bluetooth: Implement Gatt Server add/delete/start/stop service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gs1
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 0b27315f36d6e2fee2825021d1380c9ae498a431..25d84afd842c48ab8119d8a54f3f584372cce00d 100644
--- a/components/arc/bluetooth/arc_bluetooth_bridge.cc
+++ b/components/arc/bluetooth/arc_bluetooth_bridge.cc
@@ -53,6 +53,7 @@ using device::BluetoothUUID;
namespace {
constexpr int32_t kMinBtleVersion = 1;
constexpr int32_t kMinBtleNotifyVersion = 2;
+constexpr int32_t kMinGattServerVersion = 3;
constexpr uint32_t kGattReadPermission =
BluetoothGattCharacteristic::Permission::PERMISSION_READ |
BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED |
@@ -63,6 +64,7 @@ constexpr uint32_t kGattWritePermission =
BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED |
BluetoothGattCharacteristic::Permission::
PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED;
+constexpr int32_t kInvalidGattAttributeHandle = -1;
} // namespace
namespace arc {
@@ -867,6 +869,19 @@ BluetoothRemoteGattDescriptor* ArcBluetoothBridge::FindGattDescriptor(
characteristic->GetDescriptors(), desc_id->uuid.To<BluetoothUUID>());
}
+// Common callback function for Gatt operation that only need to report
+// Gatt status back to Android.
+void ArcBluetoothBridge::OnGattOperationDone(
+ const GattStatusCallback& callback) const {
+ callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
+}
+
+void ArcBluetoothBridge::OnGattOperationError(
+ const GattStatusCallback& callback,
+ BluetoothGattService::GattErrorCode error_code) const {
+ callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code));
+}
+
// Same callback for both ReadGattCharacteristic and ReadGattDescriptor
void ArcBluetoothBridge::OnGattReadDone(
const GattReadCallback& callback,
@@ -887,18 +902,6 @@ void ArcBluetoothBridge::OnGattReadError(
callback.Run(std::move(gattValue));
}
-// Same callback for both WriteGattCharacteristic and WriteGattDescriptor
-void ArcBluetoothBridge::OnGattWriteDone(
- const GattWriteCallback& callback) const {
- callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
-}
-
-void ArcBluetoothBridge::OnGattWriteError(
- const GattWriteCallback& callback,
- BluetoothGattService::GattErrorCode error_code) const {
- callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code));
-}
-
void ArcBluetoothBridge::ReadGattCharacteristic(
mojom::BluetoothAddressPtr remote_addr,
mojom::BluetoothGattServiceIDPtr service_id,
@@ -929,9 +932,9 @@ void ArcBluetoothBridge::WriteGattCharacteristic(
characteristic->WriteRemoteCharacteristic(
value->value.To<std::vector<uint8_t>>(),
- base::Bind(&ArcBluetoothBridge::OnGattWriteDone,
+ base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
weak_factory_.GetWeakPtr(), callback),
- base::Bind(&ArcBluetoothBridge::OnGattWriteError,
+ base::Bind(&ArcBluetoothBridge::OnGattOperationError,
weak_factory_.GetWeakPtr(), callback));
}
@@ -976,15 +979,15 @@ void ArcBluetoothBridge::WriteGattDescriptor(
// TODO(http://crbug.com/622832)
if (descriptor->GetUUID() ==
BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) {
- OnGattWriteDone(callback);
+ OnGattOperationDone(callback);
return;
}
descriptor->WriteRemoteDescriptor(
value->value.To<std::vector<uint8_t>>(),
- base::Bind(&ArcBluetoothBridge::OnGattWriteDone,
+ base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
weak_factory_.GetWeakPtr(), callback),
- base::Bind(&ArcBluetoothBridge::OnGattWriteError,
+ base::Bind(&ArcBluetoothBridge::OnGattOperationError,
weak_factory_.GetWeakPtr(), callback));
}
@@ -996,17 +999,6 @@ void ArcBluetoothBridge::OnGattNotifyStartDone(
callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
}
-void ArcBluetoothBridge::OnGattNotifyStartError(
- const RegisterForGattNotificationCallback& callback,
- BluetoothGattService::GattErrorCode error_code) const {
- callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code));
-}
-
-void ArcBluetoothBridge::OnGattNotifyStopDone(
- const DeregisterForGattNotificationCallback& callback) const {
- callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
-}
-
void ArcBluetoothBridge::RegisterForGattNotification(
mojom::BluetoothAddressPtr remote_addr,
mojom::BluetoothGattServiceIDPtr service_id,
@@ -1029,7 +1021,7 @@ void ArcBluetoothBridge::RegisterForGattNotification(
base::Bind(&ArcBluetoothBridge::OnGattNotifyStartDone,
weak_factory_.GetWeakPtr(), callback,
characteristic->GetIdentifier()),
- base::Bind(&ArcBluetoothBridge::OnGattNotifyStartError,
+ base::Bind(&ArcBluetoothBridge::OnGattOperationError,
weak_factory_.GetWeakPtr(), callback));
}
@@ -1055,7 +1047,7 @@ void ArcBluetoothBridge::DeregisterForGattNotification(
std::unique_ptr<BluetoothGattNotifySession> notify =
std::move(notification_session_[char_id_str]);
notification_session_.erase(char_id_str);
- notify->Stop(base::Bind(&ArcBluetoothBridge::OnGattNotifyStopDone,
+ notify->Stop(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
weak_factory_.GetWeakPtr(), callback));
}
@@ -1068,31 +1060,80 @@ void ArcBluetoothBridge::ReadRemoteRssi(
callback.Run(rssi);
}
+template <class T>
+int32_t ArcBluetoothBridge::CreateGattAttributeHandle(T* gatt_obj) {
+ if (!gatt_obj)
+ return kInvalidGattAttributeHandle;
+ std::string identifier = gatt_obj->GetIdentifier();
+ int32_t id = ConvertGattIdentifierToId(identifier);
rkc 2016/07/01 22:21:46 This function won't work with IDs returned by gatt
puthik_chromium 2016/07/14 19:01:47 I added the incremental handle count.
+ DCHECK(gatt_identifier_.find(id) == gatt_identifier_.end());
+ gatt_identifier_[id] = identifier;
+ return id;
+}
+
void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id,
int32_t num_handles,
- const AddServiceCallback& callback) {}
+ const AddServiceCallback& callback) {
+ base::WeakPtr<BluetoothLocalGattService> service =
+ BluetoothLocalGattService::Create(
+ bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(),
+ service_id->is_primary, nullptr /*included_service */,
+ this /* delegate*/);
+ callback.Run(
+ CreateGattAttributeHandle<BluetoothLocalGattService>(service.get()));
+}
void ArcBluetoothBridge::AddCharacteristic(
int32_t service_handle,
mojom::BluetoothUUIDPtr uuid,
int32_t properties,
int32_t permissions,
- const AddCharacteristicCallback& callback) {}
+ const AddCharacteristicCallback& callback) {
+ base::WeakPtr<BluetoothLocalGattCharacteristic> characteristic =
+ BluetoothLocalGattCharacteristic::Create(
+ uuid.To<BluetoothUUID>(), properties, permissions,
+ bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]));
+ callback.Run(CreateGattAttributeHandle<BluetoothLocalGattCharacteristic>(
+ characteristic.get()));
+}
void ArcBluetoothBridge::AddDescriptor(int32_t service_handle,
mojom::BluetoothUUIDPtr uuid,
int32_t permissions,
- const AddDescriptorCallback& callback) {}
+ const AddDescriptorCallback& callback) {
+ // TODO(next patch version) Android send service handle not characteristic.
rkc 2016/07/01 22:21:45 TODO(puthik)? So this is because on Android, you
puthik_chromium 2016/07/14 19:01:47 I add the descriptor to the last added characteris
rkc 2016/07/14 23:15:25 Have you read through the Android code to verify t
+ // That's not make sense.
+}
void ArcBluetoothBridge::StartService(int32_t service_handle,
int32_t transport,
- const StartServiceCallback& callback) {}
+ const StartServiceCallback& callback) {
+ BluetoothLocalGattService* service =
+ bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
+ service->Register(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
+ weak_factory_.GetWeakPtr(), callback),
+ base::Bind(&ArcBluetoothBridge::OnGattOperationError,
+ weak_factory_.GetWeakPtr(), callback));
+}
void ArcBluetoothBridge::StopService(int32_t service_handle,
- const StopServiceCallback& callback) {}
+ const StopServiceCallback& callback) {
+ BluetoothLocalGattService* service =
+ bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
+ service->Unregister(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
+ weak_factory_.GetWeakPtr(), callback),
+ base::Bind(&ArcBluetoothBridge::OnGattOperationError,
+ weak_factory_.GetWeakPtr(), callback));
+}
void ArcBluetoothBridge::DeleteService(int32_t service_handle,
- const DeleteServiceCallback& callback) {}
+ const DeleteServiceCallback& callback) {
+ BluetoothLocalGattService* service =
+ bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
+ service->Delete();
+ gatt_identifier_.erase(service_handle);
+ OnGattOperationDone(callback);
+}
void ArcBluetoothBridge::SendIndication(
int32_t attribute_handle,

Powered by Google App Engine
This is Rietveld 408576698