#ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
#include <memory>
#include <optional>
#include <string>
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_discovery_manager_mac.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/bluetooth_low_energy_adapter_apple.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
@class BluetoothDevicesConnectListener;
@class IOBluetoothDevice;
namespace device {
class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterMac
: public BluetoothLowEnergyAdapterApple,
public BluetoothDiscoveryManagerMac::Observer {
public:
static scoped_refptr<BluetoothAdapterMac> CreateAdapter();
static scoped_refptr<BluetoothAdapterMac> CreateAdapterForTest(
std::string name,
std::string address,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
BluetoothAdapterMac(const BluetoothAdapterMac&) = delete;
BluetoothAdapterMac& operator=(const BluetoothAdapterMac&) = delete;
std::string GetAddress() const override;
std::string GetName() const override;
void SetName(const std::string& name,
base::OnceClosure callback,
ErrorCallback error_callback) override;
bool IsPresent() const override;
bool IsPowered() const override;
bool IsDiscoverable() const override;
void SetDiscoverable(bool discoverable,
base::OnceClosure callback,
ErrorCallback error_callback) override;
bool IsDiscovering() const override;
void CreateRfcommService(const BluetoothUUID& uuid,
const ServiceOptions& options,
CreateServiceCallback callback,
CreateServiceErrorCallback error_callback) override;
void CreateL2capService(const BluetoothUUID& uuid,
const ServiceOptions& options,
CreateServiceCallback callback,
CreateServiceErrorCallback error_callback) override;
void ClassicDeviceFound(IOBluetoothDevice* device) override;
void ClassicDiscoveryStopped(bool unexpected) override;
void OnConnectNotification(IOBluetoothDevice* device);
void DeviceConnected(std::unique_ptr<BluetoothDevice> device);
protected:
base::WeakPtr<BluetoothAdapter> GetWeakPtr() override;
bool SetPoweredImpl(bool powered) override;
base::WeakPtr<BluetoothLowEnergyAdapterApple> GetLowEnergyWeakPtr() override;
void TriggerSystemPermissionPrompt() override;
private:
struct HostControllerState {
bool is_present = false;
bool classic_powered = false;
std::string address;
};
using HostControllerStateFunction =
base::RepeatingCallback<HostControllerState()>;
using SetControllerPowerStateFunction = base::RepeatingCallback<void(int)>;
void LazyInitialize() override;
void InitForTest(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) override;
BluetoothLowEnergyAdapterApple::GetDevicePairedStatusCallback
GetDevicePairedStatus() const override;
HostControllerState GetHostControllerState();
void SetPresentForTesting(bool present);
void SetHostControllerStateFunctionForTesting(
HostControllerStateFunction controller_state_function);
void SetPowerStateFunctionForTesting(
SetControllerPowerStateFunction power_state_function);
void SetGetDevicePairedStatusCallbackForTesting(
BluetoothLowEnergyAdapterApple::GetDevicePairedStatusCallback callback);
const static NSTimeInterval kDiscoveryTimeoutSec;
friend class BluetoothTestMac;
friend class BluetoothAdapterMacTest;
friend class BluetoothLowEnergyAdapterAppleTest;
BluetoothAdapterMac();
~BluetoothAdapterMac() override;
void StartScanWithFilter(
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
DiscoverySessionResultCallback callback) override;
void StopScan(DiscoverySessionResultCallback callback) override;
void PollAdapter();
void ClassicDeviceAdded(std::unique_ptr<BluetoothDevice> device);
void AddPairedDevices();
std::string address_;
bool classic_powered_ = false;
std::optional<bool> is_present_for_testing_;
HostControllerStateFunction controller_state_function_;
SetControllerPowerStateFunction power_state_function_;
std::unique_ptr<SetPoweredCallbacks> set_powered_callbacks_;
mutable std::string name_;
mutable bool should_update_name_ = true;
std::unique_ptr<BluetoothDiscoveryManagerMac> classic_discovery_manager_;
BluetoothLowEnergyAdapterApple::GetDevicePairedStatusCallback
device_paired_status_callback_;
std::optional<uint32_t> paired_count_;
BluetoothDevicesConnectListener* __strong connect_listener_;
base::WeakPtrFactory<BluetoothAdapterMac> weak_ptr_factory_{this};
};
}
#endif