From 5045aca7aeadeba7b4fe33f3428b919686cb0da9 Mon Sep 17 00:00:00 2001 From: Abhishek Pandit-Subedi Date: Thu, 29 Jul 2021 03:39:18 +0000 Subject: [PATCH] [floss]: Add stub device class Add a stub device class for Floss. Currently, it's only really useful for displaying address and name (which is provided by the DeviceFound callback in the adapter client). BUG=b:189499077 TEST=autoninja chrome on entire chain Change-Id: I2e34589161c6153def0aca3c217edad6b80d31d5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3027302 Commit-Queue: Abhishek Pandit-Subedi Reviewed-by: Kyle Horimoto Reviewed-by: Chris Mumford Reviewed-by: Sonny Sasaka Cr-Commit-Position: refs/heads/master@{#906554} --- device/bluetooth/BUILD.gn | 2 + .../bluetooth/floss/bluetooth_device_floss.cc | 322 ++++++++++++++++++ .../bluetooth/floss/bluetooth_device_floss.h | 137 ++++++++ 3 files changed, 461 insertions(+) create mode 100644 device/bluetooth/floss/bluetooth_device_floss.cc create mode 100644 device/bluetooth/floss/bluetooth_device_floss.h diff --git a/device/bluetooth/BUILD.gn b/device/bluetooth/BUILD.gn index 1c3bc93fc8835..168f7eed6764e 100644 --- a/device/bluetooth/BUILD.gn +++ b/device/bluetooth/BUILD.gn @@ -429,6 +429,8 @@ component("bluetooth") { "dbus/dbus_bluez_manager_wrapper_linux.h", "floss/bluetooth_adapter_floss.cc", "floss/bluetooth_adapter_floss.h", + "floss/bluetooth_device_floss.cc", + "floss/bluetooth_device_floss.h", "floss/floss_adapter_client.cc", "floss/floss_adapter_client.h", "floss/floss_dbus_client.cc", diff --git a/device/bluetooth/floss/bluetooth_device_floss.cc b/device/bluetooth/floss/bluetooth_device_floss.cc new file mode 100644 index 0000000000000..b9c7f60f9be18 --- /dev/null +++ b/device/bluetooth/floss/bluetooth_device_floss.cc @@ -0,0 +1,322 @@ +// Copyright 2021 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "device/bluetooth/floss/bluetooth_device_floss.h" + +#include + +#include "base/bind.h" +#include "base/notreached.h" +#include "dbus/bus.h" +#include "device/bluetooth/bluetooth_device.h" +#include "device/bluetooth/bluetooth_gatt_connection.h" +#include "device/bluetooth/floss/bluetooth_adapter_floss.h" +#include "device/bluetooth/floss/floss_dbus_client.h" + +namespace floss { + +using AddressType = device::BluetoothDevice::AddressType; +using VendorIDSource = device::BluetoothDevice::VendorIDSource; + +BluetoothDeviceFloss::~BluetoothDeviceFloss() = default; + +uint32_t BluetoothDeviceFloss::GetBluetoothClass() const { + NOTIMPLEMENTED(); + + return 0; +} + +device::BluetoothTransport BluetoothDeviceFloss::GetType() const { + NOTIMPLEMENTED(); + + return device::BluetoothTransport::BLUETOOTH_TRANSPORT_INVALID; +} + +std::string BluetoothDeviceFloss::GetAddress() const { + return address_; +} + +AddressType BluetoothDeviceFloss::GetAddressType() const { + NOTIMPLEMENTED(); + + return AddressType::ADDR_TYPE_UNKNOWN; +} + +VendorIDSource BluetoothDeviceFloss::GetVendorIDSource() const { + NOTIMPLEMENTED(); + + return VendorIDSource::VENDOR_ID_UNKNOWN; +} + +uint16_t BluetoothDeviceFloss::GetVendorID() const { + NOTIMPLEMENTED(); + + return 0; +} + +uint16_t BluetoothDeviceFloss::GetProductID() const { + NOTIMPLEMENTED(); + + return 0; +} + +uint16_t BluetoothDeviceFloss::GetDeviceID() const { + NOTIMPLEMENTED(); + + return 0; +} + +uint16_t BluetoothDeviceFloss::GetAppearance() const { + NOTIMPLEMENTED(); + + return 0; +} + +absl::optional BluetoothDeviceFloss::GetName() const { + if (name_.length() == 0) + return absl::nullopt; + + return name_; +} + +bool BluetoothDeviceFloss::IsPaired() const { + NOTIMPLEMENTED(); + + return false; +} + +bool BluetoothDeviceFloss::IsConnected() const { + NOTIMPLEMENTED(); + + return false; +} + +bool BluetoothDeviceFloss::IsGattConnected() const { + NOTIMPLEMENTED(); + + return false; +} + +bool BluetoothDeviceFloss::IsConnectable() const { + NOTIMPLEMENTED(); + + return false; +} + +bool BluetoothDeviceFloss::IsConnecting() const { + NOTIMPLEMENTED(); + + return false; +} + +#if defined(OS_CHROMEOS) +bool BluetoothDeviceFloss::IsBlockedByPolicy() const { + NOTIMPLEMENTED(); + + return false; +} +#endif + +device::BluetoothDevice::UUIDSet BluetoothDeviceFloss::GetUUIDs() const { + NOTIMPLEMENTED(); + + return {}; +} + +absl::optional BluetoothDeviceFloss::GetInquiryRSSI() const { + NOTIMPLEMENTED(); + + return absl::nullopt; +} + +absl::optional BluetoothDeviceFloss::GetInquiryTxPower() const { + NOTIMPLEMENTED(); + + return absl::nullopt; +} + +bool BluetoothDeviceFloss::ExpectingPinCode() const { + NOTIMPLEMENTED(); + + return false; +} + +bool BluetoothDeviceFloss::ExpectingPasskey() const { + NOTIMPLEMENTED(); + + return false; +} + +bool BluetoothDeviceFloss::ExpectingConfirmation() const { + NOTIMPLEMENTED(); + + return false; +} + +void BluetoothDeviceFloss::GetConnectionInfo(ConnectionInfoCallback callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::SetConnectionLatency( + ConnectionLatency connection_latency, + base::OnceClosure callback, + ErrorCallback error_callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::Connect( + device::BluetoothDevice::PairingDelegate* pairing_delegate, + ConnectCallback callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::SetPinCode(const std::string& pincode) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::SetPasskey(uint32_t passkey) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::ConfirmPairing() { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::RejectPairing() { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::CancelPairing() { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::Disconnect(base::OnceClosure callback, + ErrorCallback error_callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::Forget(base::OnceClosure callback, + ErrorCallback error_callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::ConnectToService( + const device::BluetoothUUID& uuid, + ConnectToServiceCallback callback, + ConnectToServiceErrorCallback error_callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::ConnectToServiceInsecurely( + const device::BluetoothUUID& uuid, + ConnectToServiceCallback callback, + ConnectToServiceErrorCallback error_callback) { + NOTIMPLEMENTED(); +} + +std::unique_ptr +BluetoothDeviceFloss::CreateBluetoothGattConnectionObject() { + NOTIMPLEMENTED(); + + return nullptr; +} + +void BluetoothDeviceFloss::SetGattServicesDiscoveryComplete(bool complete) { + NOTIMPLEMENTED(); +} + +bool BluetoothDeviceFloss::IsGattServicesDiscoveryComplete() const { + NOTIMPLEMENTED(); + + return false; +} + +void BluetoothDeviceFloss::Pair( + device::BluetoothDevice::PairingDelegate* pairing_delegate, + ConnectCallback callback) { + NOTIMPLEMENTED(); +} + +#if BUILDFLAG(IS_CHROMEOS_ASH) +void BluetoothDeviceFloss::ExecuteWrite( + base::OnceClosure callback, + ExecuteWriteErrorCallback error_callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::AbortWrite(base::OnceClosure callback, + AbortWriteErrorCallback error_callback) { + NOTIMPLEMENTED(); +} +#endif + +void BluetoothDeviceFloss::SetName(const std::string& name) { + name_ = name; +} + +void BluetoothDeviceFloss::CreateGattConnectionImpl( + absl::optional service_uuid) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::DisconnectGatt() { + NOTIMPLEMENTED(); +} + +BluetoothDeviceFloss::BluetoothDeviceFloss(BluetoothAdapterFloss* adapter, + const FlossDeviceId& device) + : BluetoothDevice(adapter), address_(device.address), name_(device.name) { + // TODO(abps): Add observers and cache data here. +} + +void BluetoothDeviceFloss::ConnectInternal(ConnectCallback callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnConnect(ConnectCallback callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnConnectError(ConnectCallback callback, + const Error& error) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnPairDuringConnect(ConnectCallback callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnPairDuringConnectError(ConnectCallback callback, + const Error& error) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnDisconnect(base::OnceClosure callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnDisconnectError(ErrorCallback error_callback, + const Error& error) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnPair(ConnectCallback callback) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnPairError(ConnectCallback callback, + const Error& error) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnCancelPairingError(const Error& error) { + NOTIMPLEMENTED(); +} + +void BluetoothDeviceFloss::OnForgetError(ErrorCallback error_callback, + const Error& error) { + NOTIMPLEMENTED(); +} + +} // namespace floss diff --git a/device/bluetooth/floss/bluetooth_device_floss.h b/device/bluetooth/floss/bluetooth_device_floss.h new file mode 100644 index 0000000000000..079ead04db584 --- /dev/null +++ b/device/bluetooth/floss/bluetooth_device_floss.h @@ -0,0 +1,137 @@ +// Copyright 2021 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef DEVICE_BLUETOOTH_FLOSS_BLUETOOTH_DEVICE_FLOSS_H_ +#define DEVICE_BLUETOOTH_FLOSS_BLUETOOTH_DEVICE_FLOSS_H_ + +#include + +#include "base/memory/weak_ptr.h" +#include "build/build_config.h" +#include "build/chromeos_buildflags.h" +#include "device/bluetooth/bluetooth_common.h" +#include "device/bluetooth/bluetooth_device.h" +#include "device/bluetooth/bluetooth_export.h" + +namespace floss { + +class BluetoothAdapterFloss; +struct Error; +struct FlossDeviceId; + +// BluetoothDeviceFloss implements device::BluetoothDevice for platforms using +// Floss (Linux front-end for Fluoride). Objects of this type should be managed +// by BluetoothAdapterFloss, which also manages device lifetimes. +// +// This class is not thread-safe but is only called from the UI thread. +class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceFloss + : public device::BluetoothDevice { + public: + BluetoothDeviceFloss(const BluetoothDeviceFloss&) = delete; + BluetoothDeviceFloss& operator=(const BluetoothDeviceFloss&) = delete; + + BluetoothDeviceFloss(BluetoothAdapterFloss* adapter, + const FlossDeviceId& device); + ~BluetoothDeviceFloss() override; + + // BluetoothDevice override + uint32_t GetBluetoothClass() const override; + device::BluetoothTransport GetType() const override; + std::string GetAddress() const override; + AddressType GetAddressType() const override; + VendorIDSource GetVendorIDSource() const override; + uint16_t GetVendorID() const override; + uint16_t GetProductID() const override; + uint16_t GetDeviceID() const override; + uint16_t GetAppearance() const override; + absl::optional GetName() const override; + bool IsPaired() const override; + bool IsConnected() const override; + bool IsGattConnected() const override; + bool IsConnectable() const override; + bool IsConnecting() const override; +#if defined(OS_CHROMEOS) + bool IsBlockedByPolicy() const override; +#endif + UUIDSet GetUUIDs() const override; + absl::optional GetInquiryRSSI() const override; + absl::optional GetInquiryTxPower() const override; + bool ExpectingPinCode() const override; + bool ExpectingPasskey() const override; + bool ExpectingConfirmation() const override; + void GetConnectionInfo(ConnectionInfoCallback callback) override; + void SetConnectionLatency(ConnectionLatency connection_latency, + base::OnceClosure callback, + ErrorCallback error_callback) override; + void Connect(device::BluetoothDevice::PairingDelegate* pairing_delegate, + ConnectCallback callback) override; + void SetPinCode(const std::string& pincode) override; + void SetPasskey(uint32_t passkey) override; + void ConfirmPairing() override; + void RejectPairing() override; + void CancelPairing() override; + void Disconnect(base::OnceClosure callback, + ErrorCallback error_callback) override; + void Forget(base::OnceClosure callback, + ErrorCallback error_callback) override; + void ConnectToService(const device::BluetoothUUID& uuid, + ConnectToServiceCallback callback, + ConnectToServiceErrorCallback error_callback) override; + void ConnectToServiceInsecurely( + const device::BluetoothUUID& uuid, + ConnectToServiceCallback callback, + ConnectToServiceErrorCallback error_callback) override; + std::unique_ptr + CreateBluetoothGattConnectionObject() override; + void SetGattServicesDiscoveryComplete(bool complete) override; + bool IsGattServicesDiscoveryComplete() const override; + void Pair(device::BluetoothDevice::PairingDelegate* pairing_delegate, + ConnectCallback callback) override; +#if BUILDFLAG(IS_CHROMEOS_ASH) + void ExecuteWrite(base::OnceClosure callback, + ExecuteWriteErrorCallback error_callback) override; + void AbortWrite(base::OnceClosure callback, + AbortWriteErrorCallback error_callback) override; +#endif + + void SetName(const std::string& name); + + protected: + // BluetoothDevice override + void CreateGattConnectionImpl( + absl::optional service_uuid) override; + void DisconnectGatt() override; + + private: + // Method for connecting to this device. + void ConnectInternal(ConnectCallback callback); + void OnConnect(ConnectCallback callback); + void OnConnectError(ConnectCallback callback, const Error& error); + + // Used if a Connect() is called but requires Pairing. + void OnPairDuringConnect(ConnectCallback callback); + void OnPairDuringConnectError(ConnectCallback callback, const Error& error); + + void OnDisconnect(base::OnceClosure callback); + void OnDisconnectError(ErrorCallback error_callback, const Error& error); + + void OnPair(ConnectCallback callback); + void OnPairError(ConnectCallback callback, const Error& error); + + void OnCancelPairingError(const Error& error); + void OnForgetError(ErrorCallback error_callback, const Error& error); + + // Address of this device. Changing this should necessitate creating a new + // BluetoothDeviceFloss object. + const std::string address_; + + // Name of this device. Can be queried later and isn't mandatory for creation. + std::string name_; + + base::WeakPtrFactory weak_ptr_factory_{this}; +}; + +} // namespace floss + +#endif // DEVICE_BLUETOOTH_FLOSS_BLUETOOTH_DEVICE_FLOSS_H_