#include "device/fido/cable/fido_cable_discovery.h"
#include <memory>
#include <utility>
#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/device_event_log/device_event_log.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_advertisement.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "device/fido/cable/fido_ble_connection.h"
#include "device/fido/cable/fido_ble_uuids.h"
#include "device/fido/cable/fido_cable_device.h"
#include "device/fido/cable/fido_cable_handshake_handler.h"
#include "device/fido/cable/fido_tunnel_device.h"
#include "device/fido/features.h"
#include "device/fido/fido_parsing_utils.h"
#if BUILDFLAG(IS_MAC)
#include "device/fido/mac/util.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "device/bluetooth/bluetooth_low_energy_scan_filter.h"
#include "device/bluetooth/floss/floss_features.h"
#endif
namespace device {
namespace {
constexpr char kScanClientName[] = …;
std::unique_ptr<BluetoothAdvertisement::Data> ConstructAdvertisementData(
base::span<const uint8_t, kCableEphemeralIdSize> client_eid) { … }
}
FidoCableDiscovery::ObservedDeviceData::ObservedDeviceData() = default;
FidoCableDiscovery::ObservedDeviceData::~ObservedDeviceData() = default;
FidoCableDiscovery::FidoCableDiscovery(
std::vector<CableDiscoveryData> discovery_data)
: … { … }
FidoCableDiscovery::~FidoCableDiscovery() { … }
std::unique_ptr<FidoDiscoveryBase::EventStream<base::span<const uint8_t, 20>>>
FidoCableDiscovery::GetV2AdvertStream() { … }
std::unique_ptr<FidoCableHandshakeHandler>
FidoCableDiscovery::CreateV1HandshakeHandler(
FidoCableDevice* device,
const CableDiscoveryData& discovery_data,
const CableEidArray& authenticator_eid) { … }
const BluetoothUUID& FidoCableDiscovery::GoogleCableUUID() { … }
const BluetoothUUID& FidoCableDiscovery::FIDOCableUUID() { … }
bool FidoCableDiscovery::IsCableDevice(const BluetoothDevice* device) { … }
void FidoCableDiscovery::OnGetAdapter(scoped_refptr<BluetoothAdapter> adapter) { … }
void FidoCableDiscovery::OnSetPowered() { … }
void FidoCableDiscovery::SetDiscoverySession(
std::unique_ptr<BluetoothDiscoverySession> discovery_session) { … }
void FidoCableDiscovery::DeviceAdded(BluetoothAdapter* adapter,
BluetoothDevice* device) { … }
void FidoCableDiscovery::DeviceChanged(BluetoothAdapter* adapter,
BluetoothDevice* device) { … }
void FidoCableDiscovery::DeviceRemoved(BluetoothAdapter* adapter,
BluetoothDevice* device) { … }
void FidoCableDiscovery::AdapterPoweredChanged(BluetoothAdapter* adapter,
bool powered) { … }
void FidoCableDiscovery::AdapterDiscoveringChanged(BluetoothAdapter* adapter,
bool is_scanning) { … }
#if BUILDFLAG(IS_CHROMEOS)
void FidoCableDiscovery::OnDeviceFound(
device::BluetoothLowEnergyScanSession* scan_session,
device::BluetoothDevice* device) {
DeviceAdded(adapter_.get(), device);
}
void FidoCableDiscovery::OnDeviceLost(
device::BluetoothLowEnergyScanSession* scan_session,
device::BluetoothDevice* device) {
DeviceRemoved(adapter_.get(), device);
}
void FidoCableDiscovery::OnSessionStarted(
device::BluetoothLowEnergyScanSession* scan_session,
std::optional<device::BluetoothLowEnergyScanSession::ErrorCode>
error_code) {
if (error_code) {
FIDO_LOG(ERROR) << "Failed to start caBLE LE scan session, error_code = "
<< static_cast<int>(error_code.value());
le_scan_session_.reset();
return;
}
FIDO_LOG(DEBUG) << "LE scan session started.";
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&FidoCableDiscovery::StartAdvertisement,
weak_factory_.GetWeakPtr()),
base::Milliseconds(500));
}
void FidoCableDiscovery::OnSessionInvalidated(
device::BluetoothLowEnergyScanSession* scan_session) {
FIDO_LOG(EVENT) << "LE scan session invalidated";
le_scan_session_.reset();
}
#endif
void FidoCableDiscovery::StartCableDiscovery() { … }
void FidoCableDiscovery::OnStartDiscoverySession(
std::unique_ptr<BluetoothDiscoverySession> session) { … }
void FidoCableDiscovery::OnStartDiscoverySessionError() { … }
void FidoCableDiscovery::StartAdvertisement() { … }
void FidoCableDiscovery::StopAdvertisements(base::OnceClosure callback) { … }
void FidoCableDiscovery::OnAdvertisementsStopped(base::OnceClosure callback) { … }
void FidoCableDiscovery::OnAdvertisementRegistered(
const CableEidArray& client_eid,
scoped_refptr<BluetoothAdvertisement> advertisement) { … }
void FidoCableDiscovery::CableDeviceFound(BluetoothAdapter* adapter,
BluetoothDevice* device) { … }
void FidoCableDiscovery::ConductEncryptionHandshake(
FidoCableHandshakeHandler* handshake_handler,
CableDiscoveryData::Version cable_version) { … }
void FidoCableDiscovery::ValidateAuthenticatorHandshakeMessage(
CableDiscoveryData::Version cable_version,
FidoCableHandshakeHandler* handshake_handler,
std::optional<std::vector<uint8_t>> handshake_response) { … }
std::optional<FidoCableDiscovery::V1DiscoveryDataAndEID>
FidoCableDiscovery::GetCableDiscoveryData(const BluetoothDevice* device) { … }
std::optional<CableEidArray> FidoCableDiscovery::MaybeGetEidFromServiceData(
const BluetoothDevice* device) { … }
std::vector<CableEidArray> FidoCableDiscovery::GetUUIDs(
const BluetoothDevice* device) { … }
std::optional<FidoCableDiscovery::V1DiscoveryDataAndEID>
FidoCableDiscovery::GetCableDiscoveryDataFromAuthenticatorEid(
CableEidArray authenticator_eid) { … }
void FidoCableDiscovery::StartInternal() { … }
std::string FidoCableDiscovery::ResultDebugString(
const CableEidArray& eid,
const std::optional<FidoCableDiscovery::V1DiscoveryDataAndEID>& result) { … }
void FidoCableDiscovery::Stop() { … }
}