Chromium Code Reviews| 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 4c5637a916ed6a6b7ea58c9e586cecffed959aaf..fdc0a7de6c04afd38d24b03c5f0e77f3e13519cc 100644 |
| --- a/components/arc/bluetooth/arc_bluetooth_bridge.cc |
| +++ b/components/arc/bluetooth/arc_bluetooth_bridge.cc |
| @@ -49,6 +49,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 | |
| @@ -59,6 +60,7 @@ constexpr uint32_t kGattWritePermission = |
| BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | |
| BluetoothGattCharacteristic::Permission:: |
| PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; |
| +constexpr int32_t kInvalidGattAttributeHandle = -1; |
| } // namespace |
| namespace arc { |
| @@ -861,6 +863,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, |
| @@ -881,18 +896,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, |
| @@ -923,9 +926,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)); |
| } |
| @@ -970,15 +973,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)); |
| } |
| @@ -990,17 +993,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, |
| @@ -1023,7 +1015,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)); |
| } |
| @@ -1049,7 +1041,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)); |
| } |
| @@ -1062,30 +1054,101 @@ void ArcBluetoothBridge::ReadRemoteRssi( |
| callback.Run(rssi); |
| } |
| +template <class T> |
|
rkc
2016/07/14 23:15:25
template <class Attribute>
puthik_chromium
2016/07/15 21:49:33
Done. I think LocalGattObjectT sound better as we
|
| +int32_t ArcBluetoothBridge::CreateGattAttributeHandle(T* gatt_obj) { |
| + if (!gatt_obj) |
| + return kInvalidGattAttributeHandle; |
| + gatt_server_obj_handle++; |
| + std::string identifier = gatt_obj->GetIdentifier(); |
| + gatt_identifier_[gatt_server_obj_handle] = identifier; |
| + return gatt_server_obj_handle; |
| +} |
| + |
| 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])); |
| + int32_t characteristic_handle = |
| + CreateGattAttributeHandle<BluetoothLocalGattCharacteristic>( |
| + characteristic.get()); |
| + last_characteristic_[service_handle] = characteristic_handle; |
| + callback.Run(characteristic_handle); |
| +} |
| void ArcBluetoothBridge::AddDescriptor(int32_t service_handle, |
| mojom::BluetoothUUIDPtr uuid, |
| int32_t permissions, |
| - const AddDescriptorCallback& callback) {} |
| + const AddDescriptorCallback& callback) { |
| + // Chrome automatically add CCC Descriptor to a characteristic when needed. |
| + // So we will just generate bogus handle for android. |
| + if (uuid.To<BluetoothUUID>() == |
| + BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) { |
| + gatt_server_obj_handle++; |
| + callback.Run(gatt_server_obj_handle); |
| + return; |
| + } |
| + |
| + BluetoothLocalGattService* service = |
| + bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]); |
| + // Since Android API does not give information which characteristic is the |
| + // parent of the new descriptor. We will just assume that it would be the |
| + // last characteristic that was added to the given service. This matches the |
| + BluetoothLocalGattCharacteristic* characteristic = service->GetCharacteristic( |
| + gatt_identifier_[last_characteristic_[service_handle]]); |
| + |
| + base::WeakPtr<BluetoothLocalGattDescriptor> descriptor = |
| + BluetoothLocalGattDescriptor::Create(uuid.To<BluetoothUUID>(), |
| + permissions, characteristic); |
| + callback.Run(CreateGattAttributeHandle<BluetoothLocalGattDescriptor>( |
| + descriptor.get())); |
| +} |
| void ArcBluetoothBridge::StartService(int32_t service_handle, |
| - 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, |