#ifndef DEVICE_BLUETOOTH_EMULATION_FAKE_PERIPHERAL_H_
#define DEVICE_BLUETOOTH_EMULATION_FAKE_PERIPHERAL_H_
#include <optional>
#include <string>
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/emulation/fake_central.h"
#include "device/bluetooth/emulation/fake_remote_gatt_service.h"
namespace device {
class BluetoothUUID;
}
namespace bluetooth {
class FakePeripheral : public device::BluetoothDevice {
public:
FakePeripheral(FakeCentral* fake_central, const std::string& address);
FakePeripheral(const FakePeripheral&) = delete;
FakePeripheral& operator=(const FakePeripheral&) = delete;
~FakePeripheral() override;
void SetName(std::optional<std::string> name);
void SetSystemConnected(bool gatt_connected);
void SetServiceUUIDs(UUIDSet service_uuids);
void SetManufacturerData(ManufacturerDataMap manufacturer_data);
void SetNextGATTConnectionResponse(uint16_t code);
void SetNextGATTDiscoveryResponse(uint16_t code);
void SimulateGATTConnectionResponse(uint16_t code);
void SimulateGATTDiscoveryResponse(uint16_t code);
bool AllResponsesConsumed();
void SimulateGATTDisconnection();
std::string AddFakeService(const device::BluetoothUUID& service_uuid);
bool RemoveFakeService(const std::string& identifier);
FakeCentral& fake_central() const { return fake_central_.get(); }
uint32_t GetBluetoothClass() const override;
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID)
device::BluetoothTransport GetType() const override;
#endif
std::string GetIdentifier() 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;
std::optional<std::string> GetName() const override;
std::u16string GetNameForDisplay() const override;
bool IsPaired() const override;
#if BUILDFLAG(IS_CHROMEOS)
bool IsBonded() const override;
#endif
bool IsConnected() const override;
bool IsGattConnected() const override;
bool IsConnectable() const override;
bool IsConnecting() 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(PairingDelegate* pairing_delegate,
ConnectCallback callback) override;
#if BUILDFLAG(IS_CHROMEOS)
void ConnectClassic(PairingDelegate* pairing_delegate,
ConnectCallback callback) override;
#endif
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;
void CreateGattConnection(
GattConnectionCallback callback,
std::optional<device::BluetoothUUID> service_uuid) override;
bool IsGattServicesDiscoveryComplete() const override;
#if BUILDFLAG(IS_APPLE)
bool IsLowEnergyDevice() override;
#endif
#if BUILDFLAG(IS_CHROMEOS)
void ExecuteWrite(base::OnceClosure callback,
ExecuteWriteErrorCallback error_callback) override;
void AbortWrite(base::OnceClosure callback,
AbortWriteErrorCallback error_callback) override;
#endif
protected:
void CreateGattConnectionImpl(std::optional<device::BluetoothUUID>) override;
void DisconnectGatt() override;
private:
void DispatchConnectionEvent();
void DispatchDiscoveryEvent();
const std::string address_;
std::optional<std::string> name_;
bool system_connected_;
bool gatt_connected_;
size_t last_service_id_;
mutable bool pending_gatt_discovery_;
std::optional<uint16_t> next_connection_response_;
std::optional<uint16_t> next_discovery_response_;
const raw_ref<FakeCentral> fake_central_;
mutable base::WeakPtrFactory<FakePeripheral> weak_ptr_factory_{this};
};
}
#endif