#ifndef DEVICE_BLUETOOTH_FLOSS_FLOSS_GATT_MANAGER_CLIENT_H_
#define DEVICE_BLUETOOTH_FLOSS_FLOSS_GATT_MANAGER_CLIENT_H_
#include <string>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/floss/exported_callback_manager.h"
#include "device/bluetooth/floss/floss_dbus_client.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
namespace floss {
enum class DEVICE_BLUETOOTH_EXPORT AuthRequired {
kNoAuth = 0,
kNoMitm,
kReqMitm,
kSignedNoMitm,
kSignedReqMitm,
};
enum class DEVICE_BLUETOOTH_EXPORT WriteType {
kInvalid = 0,
kWriteNoResponse,
kWrite,
kWritePrepare,
};
enum class DEVICE_BLUETOOTH_EXPORT LeDiscoverableMode {
kInvalid = 0,
kNonDiscoverable = 1,
kLimitedDiscoverable = 2,
kGeneralDiscoverable = 3,
};
enum class DEVICE_BLUETOOTH_EXPORT LePhy {
kInvalid = 0,
kPhy1m = 1,
kPhy2m = 2,
kPhyCoded = 3,
};
enum class DEVICE_BLUETOOTH_EXPORT GattStatus {
kSuccess = 0,
kInvalidHandle,
kReadNotPermitted,
kWriteNotPermitted,
kInvalidPdu,
kInsufficientAuthentication,
kReqNotSupported,
kInvalidOffset,
kInsufficientAuthorization,
kPrepareQueueFull,
kNotFound,
kNotLong,
kInsufficientKeySize,
kInvalidAttributeLen,
kUnlikelyError,
kInsufficientEncryption,
kUnsupportedGroupType,
kInsufficientResources,
kDatabaseOutOfSync,
kValueNotAllowed,
kTooShort = 0x7f,
kNoResources,
kInternalError,
kWrongState,
kDbFull,
kBusy,
kError,
kCommandStarted,
kIllegalParameter,
kPending,
kAuthFailed,
kMore,
kInvalidConfig,
kServiceStarted,
kEncryptedNoMitm,
kNotEncrypted,
kCongested,
kDupReg,
kAlreadyOpen,
kCancel,
kCccCfgErr = 0xFD,
kPrcInProgress = 0xFE,
kOutOfRange = 0xFF,
};
enum class DEVICE_BLUETOOTH_EXPORT GattWriteRequestStatus {
kSuccess = 0,
kFail = 1,
kBusy = 2,
};
struct DEVICE_BLUETOOTH_EXPORT GattDescriptor {
device::BluetoothUUID uuid;
int32_t instance_id;
int32_t permissions;
GattDescriptor();
~GattDescriptor();
};
struct DEVICE_BLUETOOTH_EXPORT GattCharacteristic {
enum Property {
GATT_CHAR_PROP_BIT_BROADCAST = (1 << 0),
GATT_CHAR_PROP_BIT_READ = (1 << 1),
GATT_CHAR_PROP_BIT_WRITE_NR = (1 << 2),
GATT_CHAR_PROP_BIT_WRITE = (1 << 3),
GATT_CHAR_PROP_BIT_NOTIFY = (1 << 4),
GATT_CHAR_PROP_BIT_INDICATE = (1 << 5),
GATT_CHAR_PROP_BIT_AUTH = (1 << 6),
GATT_CHAR_PROP_BIT_EXT_PROP = (1 << 7),
};
enum Permission {
GATT_PERM_READ = (1 << 0),
GATT_PERM_READ_ENCRYPTED = (1 << 1),
GATT_PERM_READ_ENC_MITM = (1 << 2),
GATT_PERM_WRITE = (1 << 4),
GATT_PERM_WRITE_ENCRYPTED = (1 << 5),
GATT_PERM_WRITE_ENC_MITM = (1 << 6),
GATT_PERM_WRITE_SIGNED = (1 << 7),
GATT_PERM_WRITE_SIGNED_MITM = (1 << 8),
};
device::BluetoothUUID uuid;
int32_t instance_id;
int32_t properties;
int32_t permissions;
int32_t key_size;
WriteType write_type;
std::vector<GattDescriptor> descriptors;
GattCharacteristic();
GattCharacteristic(const GattCharacteristic&);
~GattCharacteristic();
};
struct DEVICE_BLUETOOTH_EXPORT GattService {
enum ServiceType {
GATT_SERVICE_TYPE_PRIMARY = 0,
GATT_SERVICE_TYPE_SECONDARY = 1,
};
device::BluetoothUUID uuid;
int32_t instance_id;
int32_t service_type;
std::vector<GattCharacteristic> characteristics;
std::vector<GattService> included_services;
GattService();
GattService(const GattService&);
~GattService();
};
class DEVICE_BLUETOOTH_EXPORT FlossGattClientObserver
: public base::CheckedObserver {
public:
FlossGattClientObserver(const FlossGattClientObserver&) = delete;
FlossGattClientObserver& operator=(const FlossGattClientObserver&) = delete;
FlossGattClientObserver() = default;
~FlossGattClientObserver() override = default;
virtual void GattClientRegistered(GattStatus status, int32_t client_id) {}
virtual void GattClientConnectionState(GattStatus status,
int32_t client_id,
bool connected,
std::string address) {}
virtual void GattPhyUpdate(std::string address,
LePhy tx,
LePhy rx,
GattStatus status) {}
virtual void GattPhyRead(std::string address,
LePhy tx,
LePhy rx,
GattStatus status) {}
virtual void GattSearchComplete(std::string address,
const std::vector<GattService>& services,
GattStatus status) {}
virtual void GattCharacteristicRead(std::string address,
GattStatus status,
int32_t handle,
const std::vector<uint8_t>& data) {}
virtual void GattCharacteristicWrite(std::string address,
GattStatus status,
int32_t handle) {}
virtual void GattExecuteWrite(std::string address, GattStatus status) {}
virtual void GattDescriptorRead(std::string address,
GattStatus status,
int32_t handle,
const std::vector<uint8_t>& data) {}
virtual void GattDescriptorWrite(std::string address,
GattStatus status,
int32_t handle) {}
virtual void GattNotify(std::string address,
int32_t handle,
const std::vector<uint8_t>& data) {}
virtual void GattReadRemoteRssi(std::string address,
int32_t rssi,
GattStatus status) {}
virtual void GattConfigureMtu(std::string address,
int32_t mtu,
GattStatus status) {}
virtual void GattConnectionUpdated(std::string address,
int32_t interval,
int32_t latency,
int32_t timeout,
GattStatus status) {}
virtual void GattServiceChanged(std::string address) {}
};
class DEVICE_BLUETOOTH_EXPORT FlossGattServerObserver
: public base::CheckedObserver {
public:
FlossGattServerObserver(const FlossGattServerObserver&) = delete;
FlossGattServerObserver& operator=(const FlossGattServerObserver&) = delete;
FlossGattServerObserver() = default;
~FlossGattServerObserver() override = default;
virtual void GattServerRegistered(GattStatus status, int32_t server) {}
virtual void GattServerConnectionState(int32_t server_id,
bool connected,
std::string address) {}
virtual void GattServerServiceAdded(GattStatus status, GattService service) {}
virtual void GattServerServiceRemoved(GattStatus status, int32_t handle) {}
virtual void GattServerCharacteristicReadRequest(std::string address,
int32_t request_id,
int32_t offset,
bool is_long,
int32_t handle) {}
virtual void GattServerDescriptorReadRequest(std::string address,
int32_t request_id,
int32_t offset,
bool is_long,
int32_t handle) {}
virtual void GattServerCharacteristicWriteRequest(
std::string address,
int32_t request_id,
int32_t offset,
int32_t length,
bool is_prepared_write,
bool needs_response,
int32_t handle,
std::vector<uint8_t> value) {}
virtual void GattServerDescriptorWriteRequest(std::string address,
int32_t request_id,
int32_t offset,
int32_t length,
bool is_prepared_write,
bool needs_response,
int32_t handle,
std::vector<uint8_t> value) {}
virtual void GattServerExecuteWrite(std::string address,
int32_t request_id,
bool execute_write) {}
virtual void GattServerNotificationSent(std::string address,
GattStatus status) {}
virtual void GattServerMtuChanged(std::string address, int32_t mtu) {}
virtual void GattServerPhyUpdate(std::string address,
LePhy tx_phy,
LePhy rx_phy,
GattStatus status) {}
virtual void GattServerPhyRead(std::string address,
LePhy tx_phy,
LePhy rx_phy,
GattStatus status) {}
virtual void GattServerConnectionUpdate(std::string address,
int32_t interval,
int32_t latency,
int32_t timeout,
GattStatus status) {}
virtual void GattServerSubrateChange(std::string address,
int32_t subrate_factor,
int32_t latency,
int32_t continuation_num,
int32_t timeout,
GattStatus status) {}
};
class DEVICE_BLUETOOTH_EXPORT FlossGattManagerClient
: public FlossDBusClient,
public FlossGattClientObserver,
public FlossGattServerObserver {
public:
static const char kExportedCallbacksPath[];
static std::unique_ptr<FlossGattManagerClient> Create();
FlossGattManagerClient();
~FlossGattManagerClient() override;
FlossGattManagerClient(const FlossGattManagerClient&) = delete;
FlossGattManagerClient& operator=(const FlossGattManagerClient&) = delete;
void AddObserver(FlossGattClientObserver* observer);
void AddServerObserver(FlossGattServerObserver* observer);
void RemoveObserver(FlossGattClientObserver* observer);
void RemoveServerObserver(FlossGattServerObserver* observer);
virtual void Connect(ResponseCallback<Void> callback,
const std::string& remote_device,
const BluetoothTransport& transport,
bool is_direct);
virtual void Disconnect(ResponseCallback<Void> callback,
const std::string& remote_device);
virtual void BeginReliableWrite(ResponseCallback<Void> callback,
const std::string& remote_device);
virtual void EndReliableWrite(ResponseCallback<Void> callback,
const std::string& remote_device,
bool execute);
virtual void Refresh(ResponseCallback<Void> callback,
const std::string& remote_device);
virtual void DiscoverAllServices(ResponseCallback<Void> callback,
const std::string& remote_device);
virtual void DiscoverServiceByUuid(ResponseCallback<Void> callback,
const std::string& remote_device,
const device::BluetoothUUID& uuid);
virtual void ReadCharacteristic(ResponseCallback<Void> callback,
const std::string& remote_device,
const int32_t handle,
const AuthRequired auth_required);
virtual void ReadUsingCharacteristicUuid(ResponseCallback<Void> callback,
const std::string& remote_device,
const device::BluetoothUUID& uuid,
const int32_t start_handle,
const int32_t end_handle,
const AuthRequired auth_required);
virtual void WriteCharacteristic(
ResponseCallback<GattWriteRequestStatus> callback,
const std::string& remote_device,
const int32_t handle,
const WriteType write_type,
const AuthRequired auth_required,
base::span<const uint8_t> data);
virtual void ReadDescriptor(ResponseCallback<Void> callback,
const std::string& remote_device,
const int32_t handle,
const AuthRequired auth_required);
virtual void WriteDescriptor(ResponseCallback<Void> callback,
const std::string& remote_device,
const int32_t handle,
const AuthRequired auth_required,
base::span<const uint8_t> data);
virtual void RegisterForNotification(ResponseCallback<GattStatus> callback,
const std::string& remote_device,
const int32_t handle);
virtual void UnregisterNotification(ResponseCallback<GattStatus> callback,
const std::string& remote_device,
const int32_t handle);
virtual void ReadRemoteRssi(ResponseCallback<Void> callback,
const std::string& remote_device);
virtual void ConfigureMTU(ResponseCallback<Void> callback,
const std::string& remote_device,
const int32_t mtu);
virtual void UpdateConnectionParameters(ResponseCallback<Void> callback,
const std::string& remote_device,
const int32_t min_interval,
const int32_t max_interval,
const int32_t latency,
const int32_t timeout,
const uint16_t min_ce_len,
const uint16_t max_ce_len);
virtual void ServerConnect(ResponseCallback<Void> callback,
const std::string& remote_device,
const BluetoothTransport& transport);
virtual void ServerDisconnect(ResponseCallback<Void> callback,
const std::string& remote_device);
virtual void ServerSetPreferredPhy(ResponseCallback<Void> callback,
const std::string& remote_device,
LePhy tx_phy,
LePhy rx_phy,
int32_t phy_options);
virtual void ServerReadPhy(ResponseCallback<Void> callback,
const std::string& remote_device);
virtual void AddService(ResponseCallback<Void> callback, GattService service);
virtual void RemoveService(ResponseCallback<Void> callback, int32_t handle);
virtual void ClearServices(ResponseCallback<Void> callback);
virtual void SendResponse(ResponseCallback<Void> callback,
const std::string& remote_device,
int32_t request_id,
GattStatus status,
int32_t offset,
std::vector<uint8_t> value);
virtual void ServerSendNotification(ResponseCallback<Void> callback,
const std::string& remote_device,
int32_t handle,
bool confirm,
std::vector<uint8_t> value);
std::string ServiceName() const { return service_name_; }
bool GetMsftSupported() const { return property_msft_supported_.Get(); }
void Init(dbus::Bus* bus,
const std::string& service_name,
const int adapter_index,
base::Version version,
base::OnceClosure on_ready) override;
protected:
friend class BluetoothGattFlossTest;
void GattClientRegistered(GattStatus status, int32_t client_id) override;
void GattClientConnectionState(GattStatus status,
int32_t client_id,
bool connected,
std::string address) override;
void GattPhyUpdate(std::string address,
LePhy tx,
LePhy rx,
GattStatus status) override;
void GattPhyRead(std::string address,
LePhy tx,
LePhy rx,
GattStatus status) override;
void GattSearchComplete(std::string address,
const std::vector<GattService>& services,
GattStatus status) override;
void GattCharacteristicRead(std::string address,
GattStatus status,
int32_t handle,
const std::vector<uint8_t>& data) override;
void GattCharacteristicWrite(std::string address,
GattStatus status,
int32_t handle) override;
void GattExecuteWrite(std::string address, GattStatus status) override;
void GattDescriptorRead(std::string address,
GattStatus status,
int32_t handle,
const std::vector<uint8_t>& data) override;
void GattDescriptorWrite(std::string address,
GattStatus status,
int32_t handle) override;
void GattNotify(std::string address,
int32_t handle,
const std::vector<uint8_t>& data) override;
void GattReadRemoteRssi(std::string address,
int32_t rssi,
GattStatus status) override;
void GattConfigureMtu(std::string address,
int32_t mtu,
GattStatus status) override;
void GattConnectionUpdated(std::string address,
int32_t interval,
int32_t latency,
int32_t timeout,
GattStatus status) override;
void GattServiceChanged(std::string address) override;
void OnRegisterNotificationResponse(ResponseCallback<GattStatus> callback,
bool is_registering,
DBusResult<Void> result);
void GattServerRegistered(GattStatus status, int32_t server_id) override;
void GattServerConnectionState(int32_t server_id,
bool connected,
std::string address) override;
void GattServerServiceAdded(GattStatus status, GattService service) override;
void GattServerServiceRemoved(GattStatus status, int32_t handle) override;
void GattServerCharacteristicReadRequest(std::string address,
int32_t request_id,
int32_t offset,
bool is_long,
int32_t handle) override;
void GattServerDescriptorReadRequest(std::string address,
int32_t request_id,
int32_t offset,
bool is_long,
int32_t handle) override;
void GattServerCharacteristicWriteRequest(
std::string address,
int32_t request_id,
int32_t offset,
int32_t length,
bool is_prepared_write,
bool needs_response,
int32_t handle,
std::vector<uint8_t> value) override;
void GattServerDescriptorWriteRequest(std::string address,
int32_t request_id,
int32_t offset,
int32_t length,
bool is_prepared_write,
bool needs_response,
int32_t handle,
std::vector<uint8_t> value) override;
void GattServerExecuteWrite(std::string address,
int32_t request_id,
bool execute_write) override;
void GattServerNotificationSent(std::string address,
GattStatus status) override;
void GattServerMtuChanged(std::string address, int32_t mtu) override;
void GattServerPhyUpdate(std::string address,
LePhy tx_phy,
LePhy rx_phy,
GattStatus status) override;
void GattServerPhyRead(std::string address,
LePhy tx_phy,
LePhy rx_phy,
GattStatus status) override;
void GattServerConnectionUpdate(std::string address,
int32_t interval,
int32_t latency,
int32_t timeout,
GattStatus status) override;
void GattServerSubrateChange(std::string address,
int32_t subrate_factor,
int32_t latency,
int32_t continuation_num,
int32_t timeout,
GattStatus status) override;
raw_ptr<dbus::Bus> bus_ = nullptr;
dbus::ObjectPath gatt_adapter_path_;
base::ObserverList<FlossGattClientObserver> gatt_client_observers_;
base::ObserverList<FlossGattServerObserver> gatt_server_observers_;
std::string service_name_;
private:
friend class FlossGattClientTest;
void RegisterClient();
void RegisterServer();
void CompleteInit();
template <typename R, typename... Args>
void CallGattMethod(ResponseCallback<R> callback,
const char* member,
Args... args) {
CallMethod(std::move(callback), bus_, service_name_, kGattInterface,
gatt_adapter_path_, member, args...);
}
int32_t client_id_ = 0;
int32_t server_id_ = 0;
base::OnceClosure on_ready_;
ExportedCallbackManager<FlossGattClientObserver>
gatt_client_exported_callback_manager_{gatt::kCallbackInterface};
ExportedCallbackManager<FlossGattServerObserver>
gatt_server_exported_callback_manager_{gatt::kServerCallbackInterface};
FlossProperty<bool> property_msft_supported_{
kGattInterface, gatt::kCallbackInterface, "IsMsftSupported",
nullptr };
base::WeakPtrFactory<FlossGattManagerClient> weak_ptr_factory_{this};
};
}
#endif