Skip to content

Commit

Permalink
Reland "arc: bluetooth: Implement sending indications in GATT server"
Browse files Browse the repository at this point in the history
This is a reland of cdafb2e

Original change's description:
> arc: bluetooth: Implement sending indications in GATT server
>
>  - Ensure that CCCD is added such that we can report the value change in
>    CCCD to Android in OnNotificationStart/Stop()
>  - Implement SendIndication() such that Android can call it
>
> Bug: b:78593133
> Test: Pass Android CtsVerifier test on server sending indication/notification
> Change-Id: I8b1a4be6596555003d6d447b4518f37632a45370
> Reviewed-on: https://chromium-review.googlesource.com/1119079
> Commit-Queue: Qiyu Hu <qiyuh@google.com>
> Reviewed-by: Luis Hector Chavez <lhchavez@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#572291}

Bug: b:78593133
Test: Build w/ is_debug = true
Change-Id: I4bd27b7c9bdf4b7c16f14e2d67a2a6647dc14ad6
Reviewed-on: https://chromium-review.googlesource.com/1125359
Reviewed-by: Luis Hector Chavez <lhchavez@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
Reviewed-by: Rahul Chaturvedi <rkc@chromium.org>
Commit-Queue: Qiyu Hu <qiyuh@google.com>
Cr-Commit-Position: refs/heads/master@{#572431}
  • Loading branch information
Qiyu Hu authored and Commit Bot committed Jul 4, 2018
1 parent 47f7073 commit 0635969
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
79 changes: 68 additions & 11 deletions chrome/browser/chromeos/arc/bluetooth/arc_bluetooth_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "device/bluetooth/bluetooth_local_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_local_gatt_descriptor.h"
#include "device/bluetooth/bluez/bluetooth_device_bluez.h"
#include "device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h"
#include "device/bluetooth/bluez/bluetooth_remote_gatt_characteristic_bluez.h"
#include "mojo/public/cpp/platform/platform_handle.h"
#include "mojo/public/cpp/system/platform_handle.h"
Expand Down Expand Up @@ -381,6 +382,24 @@ void OnRemoveServiceRecordError(
std::move(callback).Run(status);
}

const device::BluetoothLocalGattDescriptor* FindCCCD(
const device::BluetoothLocalGattCharacteristic* characteristic) {
for (const auto& descriptor :
static_cast<const bluez::BluetoothLocalGattCharacteristicBlueZ*>(
characteristic)
->GetDescriptors()) {
if (descriptor->GetUUID() ==
BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) {
return descriptor.get();
}
}
return nullptr;
}

std::vector<uint8_t> MakeCCCDValue(uint8_t value) {
return {value, 0};
}

} // namespace

namespace arc {
Expand Down Expand Up @@ -897,11 +916,30 @@ void ArcBluetoothBridge::OnDescriptorWriteRequest(
void ArcBluetoothBridge::OnNotificationsStart(
const BluetoothDevice* device,
device::BluetoothGattCharacteristic::NotificationType notification_type,
const BluetoothLocalGattCharacteristic* characteristic) {}
const BluetoothLocalGattCharacteristic* characteristic) {
const BluetoothLocalGattDescriptor* cccd = FindCCCD(characteristic);
if (cccd == nullptr)
return;
OnDescriptorWriteRequest(
device, cccd,
MakeCCCDValue(notification_type ==
device::BluetoothRemoteGattCharacteristic::
NotificationType::kNotification
? ENABLE_NOTIFICATION_VALUE
: ENABLE_INDICATION_VALUE),
0, base::DoNothing(), base::DoNothing());
}

void ArcBluetoothBridge::OnNotificationsStop(
const BluetoothDevice* device,
const BluetoothLocalGattCharacteristic* characteristic) {}
const BluetoothLocalGattCharacteristic* characteristic) {
const BluetoothLocalGattDescriptor* cccd = FindCCCD(characteristic);
if (cccd == nullptr)
return;
OnDescriptorWriteRequest(device, cccd,
MakeCCCDValue(DISABLE_NOTIFICATION_VALUE), 0,
base::DoNothing(), base::DoNothing());
}

void ArcBluetoothBridge::EnableAdapter(EnableAdapterCallback callback) {
DCHECK(bluetooth_adapter_);
Expand Down Expand Up @@ -1927,14 +1965,6 @@ void ArcBluetoothBridge::AddDescriptor(int32_t service_handle,
std::move(callback).Run(kInvalidGattAttributeHandle);
return;
}
// Chrome automatically adds a CCC Descriptor to a characteristic when needed.
// We will generate a bogus handle for Android.
if (uuid ==
BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) {
int32_t handle = GetNextGattServerAttributeHandle();
std::move(callback).Run(handle);
return;
}

DCHECK(gatt_identifier_.find(service_handle) != gatt_identifier_.end());
BluetoothLocalGattService* service =
Expand Down Expand Up @@ -2008,7 +2038,34 @@ void ArcBluetoothBridge::SendIndication(int32_t attribute_handle,
mojom::BluetoothAddressPtr address,
bool confirm,
const std::vector<uint8_t>& value,
SendIndicationCallback callback) {}
SendIndicationCallback callback) {
BluetoothDevice* device =
bluetooth_adapter_->GetDevice(address->To<std::string>());
auto identifier = gatt_identifier_.find(attribute_handle);
if (device == nullptr || identifier == gatt_identifier_.end()) {
std::move(callback).Run(mojom::BluetoothGattStatus::GATT_FAILURE);
return;
}

for (const auto& service_handle : last_characteristic_) {
auto it = gatt_identifier_.find(service_handle.first);
if (it == gatt_identifier_.end())
continue;
BluetoothLocalGattService* service =
bluetooth_adapter_->GetGattService(it->second);
if (service == nullptr)
continue;
BluetoothLocalGattCharacteristic* characteristic =
service->GetCharacteristic(identifier->second);
if (characteristic == nullptr)
continue;
characteristic->NotifyValueChanged(device, value, confirm);
std::move(callback).Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
return;
}

std::move(callback).Run(mojom::BluetoothGattStatus::GATT_FAILURE);
}

void ArcBluetoothBridge::GetSdpRecords(mojom::BluetoothAddressPtr remote_addr,
const BluetoothUUID& target_uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BluetoothLocalGattServiceBlueZ;
// The BluetoothLocalGattCharacteristicBlueZ class implements
// BluetoothLocalGattCharacteristic for local GATT characteristics for
// platforms that use BlueZ.
class BluetoothLocalGattCharacteristicBlueZ
class DEVICE_BLUETOOTH_EXPORT BluetoothLocalGattCharacteristicBlueZ
: public BluetoothGattCharacteristicBlueZ,
public device::BluetoothLocalGattCharacteristic {
public:
Expand Down

0 comments on commit 0635969

Please sign in to comment.