Implement dj::Receiver class

Again, many things were done here.
This commit is contained in:
pixl
2020-06-19 03:58:00 -04:00
parent 47db60fad8
commit b41649b0de
18 changed files with 819 additions and 26 deletions

View File

@@ -26,7 +26,19 @@ Device::InvalidDevice::Reason Device::InvalidDevice::code() const noexcept
/// TODO: Initialize a single RawDevice for each path.
Device::Device(const std::string& path, DeviceIndex index):
raw_device (std::make_shared<raw::RawDevice>(path)), path (path), index (index)
raw_device (std::make_shared<raw::RawDevice>(path)), path (path),
_index (index)
{
_init();
}
Device::Device(std::shared_ptr<raw::RawDevice> raw_device, DeviceIndex index) :
raw_device (raw_device), _index (index)
{
_init();
}
void Device::_init()
{
supported_reports = getSupportedReports(raw_device->reportDescriptor());
if(!supported_reports)
@@ -34,8 +46,9 @@ Device::Device(const std::string& path, DeviceIndex index):
try
{
Report versionRequest(Report::Type::Short, index, hidpp20::FeatureID::ROOT,
hidpp20::Root::Ping, LOGID_HIDPP_SOFTWARE_ID);
Report versionRequest(Report::Type::Short, _index,
hidpp20::FeatureID::ROOT,hidpp20::Root::Ping,
LOGID_HIDPP_SOFTWARE_ID);
auto versionResponse = sendReport(versionRequest);
auto versionResponse_params = versionResponse.paramBegin();
@@ -53,10 +66,11 @@ Device::Device(const std::string& path, DeviceIndex index):
// Pass all HID++ events with device index to this device.
RawEventHandler rawEventHandler;
rawEventHandler.condition = [index](std::vector<uint8_t>& report)->bool
rawEventHandler.condition = [this](std::vector<uint8_t>& report)->bool
{
return (report[Offset::Type] == Report::Type::Short ||
report[Offset::Type] == Report::Type::Long) && (report[Offset::DeviceIndex] == index);
report[Offset::Type] == Report::Type::Long) &&
(report[Offset::DeviceIndex] == this->_index);
};
rawEventHandler.callback = [this](std::vector<uint8_t>& report)->void
{
@@ -64,7 +78,15 @@ Device::Device(const std::string& path, DeviceIndex index):
this->handleEvent(_report);
};
raw_device->addEventHandler("DEV_" + std::to_string(index), rawEventHandler);
raw_device->addEventHandler("DEV_" + std::to_string(_index), rawEventHandler);
}
Device::~Device()
{
raw_device->removeEventHandler("DEV_" + std::to_string(_index));
///TODO: tmp
raw_device->stopListener();
raw_device.reset();
}
void Device::addEventHandler(const std::string& nickname, const std::shared_ptr<EventHandler>& handler)

View File

@@ -37,9 +37,11 @@ namespace hidpp
};
Device(const std::string& path, DeviceIndex index);
Device(std::shared_ptr<raw::RawDevice> raw_device, DeviceIndex index);
~Device();
std::string devicePath() const { return path; }
DeviceIndex deviceIndex() const { return index; }
DeviceIndex deviceIndex() const { return _index; }
std::tuple<uint8_t, uint8_t> version() const { return _version; }
void listen(); // Runs asynchronously
@@ -52,9 +54,11 @@ namespace hidpp
void handleEvent(Report& report);
private:
void _init();
std::shared_ptr<raw::RawDevice> raw_device;
std::string path;
DeviceIndex index;
DeviceIndex _index;
uint8_t supported_reports;
std::tuple<uint8_t, uint8_t> _version;

View File

@@ -95,6 +95,27 @@ const char *Report::InvalidReportLength::what() const noexcept
return "Invalid report length";
}
Report::Report(Report::Type type, DeviceIndex device_index,
uint8_t sub_id, uint8_t address)
{
switch(type)
{
case Type::Short:
_data.resize(HeaderLength + ShortParamLength);
break;
case Type::Long:
_data.resize(HeaderLength + LongParamLength);
break;
default:
throw InvalidReportID();
}
_data[Offset::Type] = type;
_data[Offset::DeviceIndex] = device_index;
_data[Offset::SubID] = sub_id;
_data[Offset::Address] = address;
}
Report::Report(Report::Type type, DeviceIndex device_index,
uint8_t feature_index, uint8_t function, uint8_t sw_id)
{
@@ -123,6 +144,7 @@ Report::Report(const std::vector<uint8_t>& data)
{
_data = data;
// Truncating data is entirely valid here.
switch(_data[Offset::Type])
{
case Type::Short:
@@ -136,6 +158,11 @@ Report::Report(const std::vector<uint8_t>& data)
}
}
Report::Type Report::type() const
{
return static_cast<Report::Type>(_data[Offset::Type]);
}
void Report::setType(Report::Type type)
{
switch(type)
@@ -153,6 +180,58 @@ void Report::setType(Report::Type type)
_data[Offset::Type] = type;
}
uint8_t Report::feature() const
{
return _data[Offset::Feature];
}
void Report::setFeature(uint8_t feature)
{
_data[Offset::Parameters] = feature;
}
uint8_t Report::subId() const
{
return _data[Offset::SubID];
}
void Report::setSubId(uint8_t sub_id)
{
_data[Offset::SubID] = sub_id;
}
uint8_t Report::function() const
{
return (_data[Offset::Function] >> 4) & 0x0f;
}
void Report::setFunction(uint8_t function)
{
_data[Offset::Function] &= 0x0f;
_data[Offset::Function] |= (function << 4) & 0x0f;
}
uint8_t Report::swId() const
{
return _data[Offset::Function] & 0x0f;
}
void Report::setSwId(uint8_t sub_id)
{
_data[Offset::Function] &= 0x0f;
_data[Offset::Function] |= sub_id & 0x0f;
}
uint8_t Report::address() const
{
return _data[Offset::Address];
}
void Report::setAddress(uint8_t address)
{
_data[Offset::Address] = address;
}
void Report::setParams(const std::vector<uint8_t>& _params)
{
assert(_params.size() <= _data.size()-HeaderLength);

View File

@@ -20,6 +20,7 @@ namespace logid::backend::hidpp
static constexpr uint8_t DeviceIndex = 1;
static constexpr uint8_t SubID = 2;
static constexpr uint8_t Feature = 2;
static constexpr uint8_t Address = 3;
static constexpr uint8_t Function = 3;
static constexpr uint8_t Parameters = 4;
}
@@ -47,16 +48,37 @@ namespace logid::backend::hidpp
static constexpr uint8_t swIdMask = 0x0f;
static constexpr uint8_t functionMask = 0x0f;
Report(Report::Type type, DeviceIndex device_index,
uint8_t sub_id,
uint8_t address);
Report(Report::Type type, DeviceIndex device_index,
uint8_t feature_index,
uint8_t function,
uint8_t sw_id);
explicit Report(const std::vector<uint8_t>& data);
Report::Type type() const { return static_cast<Report::Type>(_data[Offset::Type]); };
Report::Type type() const;
void setType(Report::Type type);
std::vector<uint8_t>::iterator paramBegin() { return _data.begin() + Offset::Parameters; }
uint8_t feature() const;
void setFeature(uint8_t feature);
uint8_t subId() const;
void setSubId(uint8_t sub_id);
uint8_t function() const;
void setFunction(uint8_t function);
uint8_t swId() const;
void setSwId(uint8_t sw_id);
uint8_t address() const;
void setAddress(uint8_t address);
std::vector<uint8_t>::iterator paramBegin()
{
return _data.begin() + Offset::Parameters;
}
std::vector<uint8_t>::iterator paramEnd() { return _data.end(); }
void setParams(const std::vector<uint8_t>& _params);