#pragma once
#include <stdint.h>
#include <vector>
#include "HciSocket.h"
#include "Utils.h"
class HciAdapter
{
public:
struct Header
{
uint16_t code;
uint16_t controllerId;
uint16_t dataSize;
void toNetwork()
{
code = Utils::endianToHci(code);
controllerId = Utils::endianToHci(controllerId);
dataSize = Utils::endianToHci(dataSize);
}
void toHost()
{
code = Utils::endianToHost(code);
controllerId = Utils::endianToHost(controllerId);
dataSize = Utils::endianToHost(dataSize);
}
} __attribute__((packed));
struct ResponseEvent
{
Header header;
uint16_t commandCode;
uint8_t status;
void toNetwork()
{
header.toNetwork();
commandCode = Utils::endianToHci(commandCode);
}
void toHost()
{
header.toHost();
commandCode = Utils::endianToHost(commandCode);
}
} __attribute__((packed));
bool connect();
bool isConnected() const;
void disconnect();
bool sendCommand(Header &request, ResponseEvent &response, int responseLen);
private:
bool readResponse(uint16_t commandCode, ResponseEvent &response, size_t responseLen) const;
bool filterAndValidateEvents(uint16_t commandCode, std::vector<uint8_t> &buffer) const;
HciSocket hciSocket;
};