#ifndef DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_SOCKET_BLUEZ_H_
#define DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_SOCKET_BLUEZ_H_
#include <memory>
#include <string>
#include "base/containers/queue.h"
#include "base/memory/raw_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "dbus/object_path.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/bluetooth_socket_net.h"
#include "device/bluetooth/dbus/bluetooth_profile_manager_client.h"
#include "device/bluetooth/dbus/bluetooth_profile_service_provider.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
namespace bluez {
class BluetoothDeviceBlueZ;
class BluetoothAdapterBlueZ;
class BluetoothAdapterProfileBlueZ;
class DEVICE_BLUETOOTH_EXPORT BluetoothSocketBlueZ
: public device::BluetoothSocketNet,
public device::BluetoothAdapter::Observer,
public bluez::BluetoothProfileServiceProvider::Delegate {
public:
enum SecurityLevel { SECURITY_LEVEL_LOW, SECURITY_LEVEL_MEDIUM };
static scoped_refptr<BluetoothSocketBlueZ> CreateBluetoothSocket(
scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
scoped_refptr<device::BluetoothSocketThread> socket_thread);
BluetoothSocketBlueZ(const BluetoothSocketBlueZ&) = delete;
BluetoothSocketBlueZ& operator=(const BluetoothSocketBlueZ&) = delete;
virtual void Connect(const BluetoothDeviceBlueZ* device,
const device::BluetoothUUID& uuid,
SecurityLevel security_level,
base::OnceClosure success_callback,
ErrorCompletionCallback error_callback);
enum SocketType { kRfcomm, kL2cap };
virtual void Listen(
scoped_refptr<device::BluetoothAdapter> adapter,
SocketType socket_type,
const device::BluetoothUUID& uuid,
const device::BluetoothAdapter::ServiceOptions& service_options,
base::OnceClosure success_callback,
ErrorCompletionCallback error_callback);
void Disconnect(base::OnceClosure callback) override;
void Accept(AcceptCompletionCallback success_callback,
ErrorCompletionCallback error_callback) override;
protected:
~BluetoothSocketBlueZ() override;
private:
BluetoothSocketBlueZ(
scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
scoped_refptr<device::BluetoothSocketThread> socket_thread);
void RegisterProfile(BluetoothAdapterBlueZ* adapter,
base::OnceClosure success_callback,
ErrorCompletionCallback error_callback);
void OnRegisterProfile(base::OnceClosure success_callback,
ErrorCompletionCallback error_callback,
BluetoothAdapterProfileBlueZ* profile);
void OnRegisterProfileError(ErrorCompletionCallback error_callback,
const std::string& error_message);
void OnConnectProfile(base::OnceClosure success_callback);
void OnConnectProfileError(ErrorCompletionCallback error_callback,
const std::string& error_name,
const std::string& error_message);
void AdapterPresentChanged(device::BluetoothAdapter* adapter,
bool present) override;
void OnInternalRegisterProfile(BluetoothAdapterProfileBlueZ* profile);
void OnInternalRegisterProfileError(const std::string& error_message);
void Released() override;
void NewConnection(
const dbus::ObjectPath& device_path,
base::ScopedFD fd,
const bluez::BluetoothProfileServiceProvider::Delegate::Options& options,
ConfirmationCallback callback) override;
void RequestDisconnection(const dbus::ObjectPath& device_path,
ConfirmationCallback callback) override;
void Cancel() override;
void AcceptConnectionRequest();
void DoNewConnection(
const dbus::ObjectPath& device_path,
base::ScopedFD fd,
const bluez::BluetoothProfileServiceProvider::Delegate::Options& options,
ConfirmationCallback callback);
void OnNewConnection(scoped_refptr<BluetoothSocket> socket,
ConfirmationCallback callback,
Status status);
void UnregisterProfile();
scoped_refptr<device::BluetoothAdapter> adapter_;
std::string device_address_;
dbus::ObjectPath device_path_;
device::BluetoothUUID uuid_;
std::unique_ptr<bluez::BluetoothProfileManagerClient::Options> options_;
raw_ptr<BluetoothAdapterProfileBlueZ> profile_;
struct AcceptRequest {
AcceptRequest();
~AcceptRequest();
AcceptCompletionCallback success_callback;
ErrorCompletionCallback error_callback;
};
std::unique_ptr<AcceptRequest> accept_request_;
struct ConnectionRequest {
ConnectionRequest();
~ConnectionRequest();
dbus::ObjectPath device_path;
base::ScopedFD fd;
bluez::BluetoothProfileServiceProvider::Delegate::Options options;
ConfirmationCallback callback;
bool accepting;
bool cancelled;
};
base::queue<std::unique_ptr<ConnectionRequest>> connection_request_queue_;
};
}
#endif