#include "device/bluetooth/floss/bluetooth_device_floss.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/task/sequenced_task_runner.h"
#include "components/device_event_log/device_event_log.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/bluetooth_gatt_connection_floss.h"
#include "device/bluetooth/floss/bluetooth_remote_gatt_service_floss.h"
#include "device/bluetooth/floss/bluetooth_socket_floss.h"
#include "device/bluetooth/floss/floss_dbus_client.h"
#include "device/bluetooth/floss/floss_dbus_manager.h"
#include "device/bluetooth/floss/floss_gatt_manager_client.h"
#include "device/bluetooth/floss/floss_socket_manager.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "device/bluetooth/chromeos/bluetooth_utils.h"
#endif
namespace floss {
namespace {
const int32_t kMinConnectionIntervalLow = …;
const int32_t kMaxConnectionIntervalLow = …;
const int32_t kMinConnectionIntervalMedium = …;
const int32_t kMaxConnectionIntervalMedium = …;
const int32_t kMinConnectionIntervalHigh = …;
const int32_t kMaxConnectionIntervalHigh = …;
const int32_t kDefaultConnectionLatency = …;
const int32_t kDefaultConnectionTimeout = …;
const int32_t kMaxMtuSize = …;
constexpr base::TimeDelta kDefaultConnectTimeout = …;
void OnRemoveBond(base::OnceClosure callback, DBusResult<bool> ret) { … }
}
AddressType;
VendorIDSource;
uint32_t BluetoothDeviceFloss::GetBluetoothClass() const { … }
device::BluetoothTransport BluetoothDeviceFloss::GetType() const { … }
std::string BluetoothDeviceFloss::GetAddress() const { … }
AddressType BluetoothDeviceFloss::GetAddressType() const { … }
VendorIDSource BluetoothDeviceFloss::GetVendorIDSource() const { … }
uint16_t BluetoothDeviceFloss::GetVendorID() const { … }
uint16_t BluetoothDeviceFloss::GetProductID() const { … }
uint16_t BluetoothDeviceFloss::GetDeviceID() const { … }
uint16_t BluetoothDeviceFloss::GetAppearance() const { … }
std::optional<std::string> BluetoothDeviceFloss::GetName() const { … }
bool BluetoothDeviceFloss::IsPaired() const { … }
#if BUILDFLAG(IS_CHROMEOS)
bool BluetoothDeviceFloss::IsBonded() const {
return IsBondedImpl();
}
#endif
bool BluetoothDeviceFloss::IsConnected() const { … }
bool BluetoothDeviceFloss::IsGattConnected() const { … }
bool BluetoothDeviceFloss::IsConnectable() const { … }
bool BluetoothDeviceFloss::IsConnecting() const { … }
device::BluetoothDevice::UUIDSet BluetoothDeviceFloss::GetUUIDs() const { … }
std::optional<int8_t> BluetoothDeviceFloss::GetInquiryTxPower() const { … }
bool BluetoothDeviceFloss::ExpectingPinCode() const { … }
bool BluetoothDeviceFloss::ExpectingPasskey() const { … }
bool BluetoothDeviceFloss::ExpectingConfirmation() const { … }
void BluetoothDeviceFloss::GetConnectionInfo(ConnectionInfoCallback callback) { … }
void BluetoothDeviceFloss::SetConnectionLatency(
ConnectionLatency connection_latency,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothDeviceFloss::OnSetConnectionLatency(base::OnceClosure callback,
ErrorCallback error_callback,
DBusResult<Void> ret) { … }
void BluetoothDeviceFloss::Connect(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback) { … }
void BluetoothDeviceFloss::ConnectWithTransport(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback,
FlossAdapterClient::BluetoothTransport transport) { … }
void BluetoothDeviceFloss::OnCreateBond(DBusResult<bool> ret) { … }
void BluetoothDeviceFloss::OnCreateBond(
DBusResult<FlossDBusClient::BtifStatus> ret) { … }
void BluetoothDeviceFloss::ConnectionIncomplete() { … }
#if BUILDFLAG(IS_CHROMEOS)
void BluetoothDeviceFloss::ConnectClassic(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback) {
ConnectWithTransport(pairing_delegate, std::move(callback),
FlossAdapterClient::BluetoothTransport::kBrEdr);
}
#endif
void BluetoothDeviceFloss::SetPinCode(const std::string& pincode) { … }
void BluetoothDeviceFloss::SetPasskey(uint32_t passkey) { … }
void BluetoothDeviceFloss::ConfirmPairing() { … }
void BluetoothDeviceFloss::RejectPairing() { … }
void BluetoothDeviceFloss::CancelPairing() { … }
void BluetoothDeviceFloss::Disconnect(base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothDeviceFloss::Forget(base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothDeviceFloss::ConnectToService(
const device::BluetoothUUID& uuid,
ConnectToServiceCallback callback,
ConnectToServiceErrorCallback error_callback) { … }
void BluetoothDeviceFloss::ConnectToServiceInsecurely(
const device::BluetoothUUID& uuid,
ConnectToServiceCallback callback,
ConnectToServiceErrorCallback error_callback) { … }
std::unique_ptr<device::BluetoothGattConnection>
BluetoothDeviceFloss::CreateBluetoothGattConnectionObject() { … }
void BluetoothDeviceFloss::SetGattServicesDiscoveryComplete(bool complete) { … }
bool BluetoothDeviceFloss::IsGattServicesDiscoveryComplete() const { … }
void BluetoothDeviceFloss::Pair(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback) { … }
BluetoothPairingFloss* BluetoothDeviceFloss::BeginPairing(
BluetoothDevice::PairingDelegate* pairing_delegate) { … }
#if BUILDFLAG(IS_CHROMEOS)
void BluetoothDeviceFloss::OnExecuteWrite(
base::OnceClosure callback,
ExecuteWriteErrorCallback error_callback,
DBusResult<Void> ret) {
if (!ret.has_value()) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kFailed);
return;
}
pending_execute_write_ =
std::make_pair(std::move(callback), std::move(error_callback));
}
void BluetoothDeviceFloss::BeginReliableWrite() {
DCHECK(!using_reliable_write_);
if (!using_reliable_write_) {
using_reliable_write_ = true;
FlossDBusManager::Get()->GetGattManagerClient()->BeginReliableWrite(
base::DoNothing(), address_);
}
}
void BluetoothDeviceFloss::ExecuteWrite(
base::OnceClosure callback,
ExecuteWriteErrorCallback error_callback) {
if (pending_execute_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kInProgress);
return;
}
if (!using_reliable_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kFailed);
return;
}
FlossDBusManager::Get()->GetGattManagerClient()->EndReliableWrite(
base::BindOnce(&BluetoothDeviceFloss::OnExecuteWrite,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)),
address_, true);
}
void BluetoothDeviceFloss::AbortWrite(base::OnceClosure callback,
AbortWriteErrorCallback error_callback) {
if (pending_execute_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kInProgress);
return;
}
if (!using_reliable_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kFailed);
return;
}
FlossDBusManager::Get()->GetGattManagerClient()->EndReliableWrite(
base::BindOnce(&BluetoothDeviceFloss::OnExecuteWrite,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)),
address_, false);
}
void BluetoothDeviceFloss::GattExecuteWrite(std::string address,
GattStatus status) {
if (address != address_) {
return;
}
if (!pending_execute_write_) {
return;
}
if (status != GattStatus::kSuccess) {
std::move(pending_execute_write_->second)
.Run(
floss::BluetoothGattServiceFloss::GattStatusToServiceError(status));
} else {
std::move(pending_execute_write_->first).Run();
}
pending_execute_write_ = std::nullopt;
}
#endif
FlossDeviceId BluetoothDeviceFloss::AsFlossDeviceId() const { … }
void BluetoothDeviceFloss::SetName(const std::string& name) { … }
void BluetoothDeviceFloss::SetBondState(
FlossAdapterClient::BondState bond_state,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) { … }
void BluetoothDeviceFloss::SetIsConnected(bool is_connected) { … }
void BluetoothDeviceFloss::SetConnectionState(uint32_t connection_state) { … }
void BluetoothDeviceFloss::ConnectAllEnabledProfiles() { … }
void BluetoothDeviceFloss::ResetPairing() { … }
void BluetoothDeviceFloss::CreateGattConnectionImpl(
std::optional<device::BluetoothUUID> service_uuid) { … }
void BluetoothDeviceFloss::OnConnectGatt(DBusResult<Void> ret) { … }
void BluetoothDeviceFloss::UpgradeToFullDiscovery() { … }
void BluetoothDeviceFloss::DisconnectGatt() { … }
BluetoothDeviceFloss::BluetoothDeviceFloss(
BluetoothAdapterFloss* adapter,
const FlossDeviceId& device,
scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
scoped_refptr<device::BluetoothSocketThread> socket_thread)
: … { … }
BluetoothDeviceFloss::~BluetoothDeviceFloss() { … }
bool BluetoothDeviceFloss::IsBondedImpl() const { … }
void BluetoothDeviceFloss::OnGetRemoteType(
base::OnceClosure callback,
DBusResult<FlossAdapterClient::BluetoothDeviceType> ret) { … }
void BluetoothDeviceFloss::OnGetRemoteClass(base::OnceClosure callback,
DBusResult<uint32_t> ret) { … }
void BluetoothDeviceFloss::OnGetRemoteAppearance(base::OnceClosure callback,
DBusResult<uint16_t> ret) { … }
void BluetoothDeviceFloss::OnGetRemoteUuids(base::OnceClosure callback,
DBusResult<UUIDList> ret) { … }
void BluetoothDeviceFloss::OnGetRemoteVendorProductInfo(
base::OnceClosure callback,
DBusResult<FlossAdapterClient::VendorProductInfo> ret) { … }
void BluetoothDeviceFloss::OnGetRemoteAddressType(
base::OnceClosure callback,
DBusResult<FlossAdapterClient::BtAddressType> ret) { … }
void BluetoothDeviceFloss::OnConnectAllEnabledProfiles(DBusResult<Void> ret) { … }
void BluetoothDeviceFloss::OnConnectAllEnabledProfiles(
DBusResult<FlossDBusClient::BtifStatus> ret) { … }
void BluetoothDeviceFloss::UpdateConnectingState(
ConnectingState state,
std::optional<BluetoothDevice::ConnectErrorCode> error) { … }
void BluetoothDeviceFloss::UpdateGattConnectingState(
GattConnectingState state) { … }
void BluetoothDeviceFloss::TriggerConnectCallback(
std::optional<BluetoothDevice::ConnectErrorCode> error_code) { … }
void BluetoothDeviceFloss::OnDisconnectAllEnabledProfiles(
base::OnceClosure callback,
ErrorCallback error_callback,
DBusResult<Void> ret) { … }
void BluetoothDeviceFloss::OnConnectToServiceError(
scoped_refptr<BluetoothSocketFloss> socket,
ConnectToServiceErrorCallback error_callback,
const std::string& error_message) { … }
void BluetoothDeviceFloss::FetchRemoteType(base::OnceClosure callback) { … }
void BluetoothDeviceFloss::FetchRemoteClass(base::OnceClosure callback) { … }
void BluetoothDeviceFloss::FetchRemoteAppearance(base::OnceClosure callback) { … }
void BluetoothDeviceFloss::FetchRemoteUuids(base::OnceClosure callback) { … }
void BluetoothDeviceFloss::FetchRemoteVendorProductInfo(
base::OnceClosure callback) { … }
void BluetoothDeviceFloss::FetchRemoteAddressType(base::OnceClosure callback) { … }
void BluetoothDeviceFloss::InitializeDeviceProperties(
PropertiesState state,
base::OnceClosure callback) { … }
void BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback() { … }
void BluetoothDeviceFloss::GattClientConnectionState(GattStatus status,
int32_t client_id,
bool connected,
std::string address) { … }
void BluetoothDeviceFloss::GattSearchComplete(
std::string address,
const std::vector<GattService>& services,
GattStatus status) { … }
void BluetoothDeviceFloss::GattConnectionUpdated(std::string address,
int32_t interval,
int32_t latency,
int32_t timeout,
GattStatus status) { … }
void BluetoothDeviceFloss::GattConfigureMtu(std::string address,
int32_t mtu,
GattStatus status) { … }
#if BUILDFLAG(IS_CHROMEOS)
void BluetoothDeviceFloss::GattServiceChanged(std::string address) {
if (address != GetAddress()) {
return;
}
adapter()->NotifyGattNeedsDiscovery(this);
}
#endif
}