#ifndef DEVICE_BLUETOOTH_DEVICE_H_
#define DEVICE_BLUETOOTH_DEVICE_H_
#include <memory>
#include <string>
#include <vector>
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_gatt_connection.h"
#include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_remote_gatt_descriptor.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h"
#include "device/bluetooth/public/mojom/device.mojom.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
namespace bluetooth {
class Device : public mojom::Device, public device::BluetoothAdapter::Observer {
public:
Device(const Device&) = delete;
Device& operator=(const Device&) = delete;
~Device() override;
static void Create(
scoped_refptr<device::BluetoothAdapter> adapter,
std::unique_ptr<device::BluetoothGattConnection> connection,
mojo::PendingReceiver<mojom::Device> receiver);
static mojom::DeviceInfoPtr ConstructDeviceInfoStruct(
const device::BluetoothDevice* device);
void DeviceChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) override;
void GattServicesDiscovered(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) override;
void Disconnect() override;
void GetInfo(GetInfoCallback callback) override;
void GetServices(GetServicesCallback callback) override;
void GetCharacteristics(const std::string& service_id,
GetCharacteristicsCallback callback) override;
void ReadValueForCharacteristic(
const std::string& service_id,
const std::string& characteristic_id,
ReadValueForCharacteristicCallback callback) override;
void WriteValueForCharacteristic(
const std::string& service_id,
const std::string& characteristic_id,
const std::vector<uint8_t>& value,
WriteValueForCharacteristicCallback callback) override;
void GetDescriptors(const std::string& service_id,
const std::string& characteristic_id,
GetDescriptorsCallback callback) override;
void ReadValueForDescriptor(const std::string& service_id,
const std::string& characteristic_id,
const std::string& descriptor_id,
ReadValueForDescriptorCallback callback) override;
void WriteValueForDescriptor(
const std::string& service_id,
const std::string& characteristic_id,
const std::string& descriptor_id,
const std::vector<uint8_t>& value,
WriteValueForDescriptorCallback callback) override;
private:
Device(scoped_refptr<device::BluetoothAdapter> adapter,
std::unique_ptr<device::BluetoothGattConnection> connection);
void GetServicesImpl(GetServicesCallback callback);
mojom::ServiceInfoPtr ConstructServiceInfoStruct(
const device::BluetoothRemoteGattService& service);
void OnReadRemoteCharacteristic(
ReadValueForCharacteristicCallback callback,
absl::optional<device::BluetoothGattService::GattErrorCode> error_code,
const std::vector<uint8_t>& value);
void OnWriteRemoteCharacteristic(
WriteValueForCharacteristicCallback callback);
void OnWriteRemoteCharacteristicError(
WriteValueForCharacteristicCallback callback,
device::BluetoothGattService::GattErrorCode error_code);
void OnReadRemoteDescriptor(
ReadValueForDescriptorCallback callback,
absl::optional<device::BluetoothGattService::GattErrorCode> error_code,
const std::vector<uint8_t>& value);
void OnWriteRemoteDescriptor(WriteValueForDescriptorCallback callback);
void OnWriteRemoteDescriptorError(
WriteValueForDescriptorCallback callback,
device::BluetoothGattService::GattErrorCode error_code);
const std::string& GetAddress();
scoped_refptr<device::BluetoothAdapter> adapter_;
std::unique_ptr<device::BluetoothGattConnection> connection_;
mojo::SelfOwnedReceiverRef<mojom::Device> receiver_;
std::vector<base::OnceClosure> pending_services_requests_;
base::WeakPtrFactory<Device> weak_ptr_factory_{this};
};
}
#endif