#include "device/bluetooth/emulation/fake_central.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/ranges/algorithm.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_filter.h"
#include "device/bluetooth/bluetooth_discovery_session_outcome.h"
#include "device/bluetooth/emulation/fake_peripheral.h"
#include "device/bluetooth/emulation/fake_remote_gatt_characteristic.h"
#include "device/bluetooth/emulation/fake_remote_gatt_service.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "device/bluetooth/public/mojom/emulation/fake_bluetooth.mojom.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "device/bluetooth/bluetooth_low_energy_scan_filter.h"
#endif
namespace bluetooth {
namespace {
template <typename Optional, typename T = typename Optional::value_type>
T ValueOrDefault(Optional&& opt) { … }
device::BluetoothDevice::ManufacturerDataMap ToManufacturerDataMap(
base::flat_map<uint16_t, std::vector<uint8_t>>&& map) { … }
}
FakeCentral::FakeCentral(mojom::CentralState state,
mojo::PendingReceiver<mojom::FakeCentral> receiver)
: … { … }
void FakeCentral::SimulatePreconnectedPeripheral(
const std::string& address,
const std::string& name,
const base::flat_map<uint16_t, std::vector<uint8_t>>& manufacturer_data,
const std::vector<device::BluetoothUUID>& known_service_uuids,
SimulatePreconnectedPeripheralCallback callback) { … }
void FakeCentral::SimulateAdvertisementReceived(
mojom::ScanResultPtr scan_result_ptr,
SimulateAdvertisementReceivedCallback callback) { … }
void FakeCentral::SetState(mojom::CentralState new_state,
SetStateCallback callback) { … }
void FakeCentral::SetNextGATTConnectionResponse(
const std::string& address,
uint16_t code,
SetNextGATTConnectionResponseCallback callback) { … }
void FakeCentral::SetNextGATTDiscoveryResponse(
const std::string& address,
uint16_t code,
SetNextGATTDiscoveryResponseCallback callback) { … }
bool FakeCentral::AllResponsesConsumed() { … }
void FakeCentral::SimulateGATTDisconnection(
const std::string& address,
SimulateGATTDisconnectionCallback callback) { … }
void FakeCentral::SimulateGATTServicesChanged(
const std::string& address,
SimulateGATTServicesChangedCallback callback) { … }
void FakeCentral::AddFakeService(const std::string& peripheral_address,
const device::BluetoothUUID& service_uuid,
AddFakeServiceCallback callback) { … }
void FakeCentral::RemoveFakeService(const std::string& identifier,
const std::string& peripheral_address,
RemoveFakeServiceCallback callback) { … }
void FakeCentral::AddFakeCharacteristic(
const device::BluetoothUUID& characteristic_uuid,
mojom::CharacteristicPropertiesPtr properties,
const std::string& service_id,
const std::string& peripheral_address,
AddFakeCharacteristicCallback callback) { … }
void FakeCentral::RemoveFakeCharacteristic(
const std::string& identifier,
const std::string& service_id,
const std::string& peripheral_address,
RemoveFakeCharacteristicCallback callback) { … }
void FakeCentral::AddFakeDescriptor(
const device::BluetoothUUID& descriptor_uuid,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
AddFakeDescriptorCallback callback) { … }
void FakeCentral::RemoveFakeDescriptor(const std::string& descriptor_id,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
RemoveFakeDescriptorCallback callback) { … }
void FakeCentral::SetNextReadCharacteristicResponse(
uint16_t gatt_code,
const std::optional<std::vector<uint8_t>>& value,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
SetNextReadCharacteristicResponseCallback callback) { … }
void FakeCentral::SetNextWriteCharacteristicResponse(
uint16_t gatt_code,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
SetNextWriteCharacteristicResponseCallback callback) { … }
void FakeCentral::SetNextSubscribeToNotificationsResponse(
uint16_t gatt_code,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
SetNextSubscribeToNotificationsResponseCallback callback) { … }
void FakeCentral::SetNextUnsubscribeFromNotificationsResponse(
uint16_t gatt_code,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
SetNextUnsubscribeFromNotificationsResponseCallback callback) { … }
void FakeCentral::IsNotifying(const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
IsNotifyingCallback callback) { … }
void FakeCentral::GetLastWrittenCharacteristicValue(
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
GetLastWrittenCharacteristicValueCallback callback) { … }
void FakeCentral::SetNextReadDescriptorResponse(
uint16_t gatt_code,
const std::optional<std::vector<uint8_t>>& value,
const std::string& descriptor_id,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
SetNextReadDescriptorResponseCallback callback) { … }
void FakeCentral::SetNextWriteDescriptorResponse(
uint16_t gatt_code,
const std::string& descriptor_id,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
SetNextWriteDescriptorResponseCallback callback) { … }
void FakeCentral::GetLastWrittenDescriptorValue(
const std::string& descriptor_id,
const std::string& characteristic_id,
const std::string& service_id,
const std::string& peripheral_address,
GetLastWrittenDescriptorValueCallback callback) { … }
void FakeCentral::Initialize(base::OnceClosure callback) { … }
std::string FakeCentral::GetAddress() const { … }
std::string FakeCentral::GetName() const { … }
void FakeCentral::SetName(const std::string& name,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
bool FakeCentral::IsInitialized() const { … }
bool FakeCentral::IsPresent() const { … }
bool FakeCentral::IsPowered() const { … }
void FakeCentral::SetPowered(bool powered,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
bool FakeCentral::IsDiscoverable() const { … }
void FakeCentral::SetDiscoverable(bool discoverable,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
base::TimeDelta FakeCentral::GetDiscoverableTimeout() const { … }
#endif
bool FakeCentral::IsDiscovering() const { … }
FakeCentral::UUIDList FakeCentral::GetUUIDs() const { … }
void FakeCentral::CreateRfcommService(
const device::BluetoothUUID& uuid,
const ServiceOptions& options,
CreateServiceCallback callback,
CreateServiceErrorCallback error_callback) { … }
void FakeCentral::CreateL2capService(
const device::BluetoothUUID& uuid,
const ServiceOptions& options,
CreateServiceCallback callback,
CreateServiceErrorCallback error_callback) { … }
void FakeCentral::RegisterAdvertisement(
std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement_data,
CreateAdvertisementCallback callback,
AdvertisementErrorCallback error_callback) { … }
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
void FakeCentral::SetAdvertisingInterval(
const base::TimeDelta& min,
const base::TimeDelta& max,
base::OnceClosure callback,
AdvertisementErrorCallback error_callback) { … }
void FakeCentral::ResetAdvertising(base::OnceClosure callback,
AdvertisementErrorCallback error_callback) { … }
void FakeCentral::ConnectDevice(
const std::string& address,
const std::optional<device::BluetoothDevice::AddressType>& address_type,
ConnectDeviceCallback callback,
ConnectDeviceErrorCallback error_callback) { … }
#endif
device::BluetoothLocalGattService* FakeCentral::GetGattService(
const std::string& identifier) const { … }
#if BUILDFLAG(IS_CHROMEOS)
void FakeCentral::SetServiceAllowList(const UUIDList& uuids,
base::OnceClosure callback,
ErrorCallback error_callback) {
NOTREACHED_IN_MIGRATION();
}
std::unique_ptr<device::BluetoothLowEnergyScanSession>
FakeCentral::StartLowEnergyScanSession(
std::unique_ptr<device::BluetoothLowEnergyScanFilter> filter,
base::WeakPtr<device::BluetoothLowEnergyScanSession::Delegate> delegate) {
NOTREACHED_IN_MIGRATION();
return nullptr;
}
device::BluetoothAdapter::LowEnergyScanSessionHardwareOffloadingStatus
FakeCentral::GetLowEnergyScanSessionHardwareOffloadingStatus() {
return LowEnergyScanSessionHardwareOffloadingStatus::kNotSupported;
}
std::vector<device::BluetoothAdapter::BluetoothRole>
FakeCentral::GetSupportedRoles() {
NOTREACHED_IN_MIGRATION();
return std::vector<device::BluetoothAdapter::BluetoothRole>{};
}
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
void FakeCentral::SetStandardChromeOSAdapterName() {
NOTREACHED_IN_MIGRATION();
}
#endif
base::WeakPtr<device::BluetoothAdapter> FakeCentral::GetWeakPtr() { … }
bool FakeCentral::SetPoweredImpl(bool powered) { … }
void FakeCentral::UpdateFilter(
std::unique_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
DiscoverySessionResultCallback callback) { … }
void FakeCentral::StartScanWithFilter(
std::unique_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
DiscoverySessionResultCallback callback) { … }
void FakeCentral::StopScan(DiscoverySessionResultCallback callback) { … }
void FakeCentral::RemovePairingDelegateInternal(
device::BluetoothDevice::PairingDelegate* pairing_delegate) { … }
FakeCentral::~FakeCentral() = default;
FakePeripheral* FakeCentral::GetFakePeripheral(
const std::string& peripheral_address) const { … }
FakeRemoteGattService* FakeCentral::GetFakeRemoteGattService(
const std::string& peripheral_address,
const std::string& service_id) const { … }
FakeRemoteGattCharacteristic* FakeCentral::GetFakeRemoteGattCharacteristic(
const std::string& peripheral_address,
const std::string& service_id,
const std::string& characteristic_id) const { … }
FakeRemoteGattDescriptor* FakeCentral::GetFakeRemoteGattDescriptor(
const std::string& peripheral_address,
const std::string& service_id,
const std::string& characteristic_id,
const std::string& descriptor_id) const { … }
}