2020-06-17 06:43:53 +00:00
|
|
|
#include <assert.h>
|
2020-06-16 23:53:38 +00:00
|
|
|
#include "Device.h"
|
|
|
|
#include "Report.h"
|
2020-06-18 05:34:25 +00:00
|
|
|
#include "../hidpp20/features/Root.h"
|
2020-06-16 23:53:38 +00:00
|
|
|
|
|
|
|
using namespace logid::backend;
|
|
|
|
using namespace logid::backend::hidpp;
|
|
|
|
|
|
|
|
const char* Device::InvalidDevice::what() const noexcept
|
|
|
|
{
|
|
|
|
switch(_reason)
|
|
|
|
{
|
|
|
|
case NoHIDPPReport:
|
|
|
|
return "Invalid HID++ device";
|
|
|
|
case InvalidRawDevice:
|
|
|
|
return "Invalid raw device";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Device::InvalidDevice::Reason Device::InvalidDevice::code() const noexcept
|
|
|
|
{
|
|
|
|
return _reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TODO: Initialize a single RawDevice for each path.
|
2020-06-17 06:43:53 +00:00
|
|
|
Device::Device(const std::string& path, DeviceIndex index):
|
2020-06-16 23:53:38 +00:00
|
|
|
raw_device (std::make_shared<raw::RawDevice>(path)), path (path), index (index)
|
|
|
|
{
|
|
|
|
supported_reports = getSupportedReports(raw_device->reportDescriptor());
|
|
|
|
if(!supported_reports)
|
|
|
|
throw InvalidDevice(InvalidDevice::NoHIDPPReport);
|
2020-06-17 06:43:53 +00:00
|
|
|
|
2020-06-18 05:34:25 +00:00
|
|
|
Report versionRequest(Report::Type::Short, index, hidpp20::FeatureID::ROOT,
|
|
|
|
hidpp20::Root::Ping, LOGID_HIDPP_SOFTWARE_ID);
|
|
|
|
|
|
|
|
///TODO: Catch error
|
|
|
|
auto versionResponse = sendReport(versionRequest);
|
|
|
|
auto versionResponse_params = versionResponse.paramBegin();
|
|
|
|
_version = std::make_tuple(versionResponse_params[0], versionResponse_params[1]);
|
|
|
|
|
2020-06-17 06:43:53 +00:00
|
|
|
// Pass all HID++ events with device index to this device.
|
|
|
|
RawEventHandler rawEventHandler;
|
|
|
|
rawEventHandler.condition = [index](std::vector<uint8_t>& report)->bool
|
|
|
|
{
|
2020-06-18 05:34:25 +00:00
|
|
|
return (report[Offset::Type] == Report::Type::Short ||
|
|
|
|
report[Offset::Type] == Report::Type::Long) && (report[Offset::DeviceIndex] == index);
|
2020-06-17 06:43:53 +00:00
|
|
|
};
|
|
|
|
rawEventHandler.callback = [this](std::vector<uint8_t>& report)->void
|
|
|
|
{
|
|
|
|
Report _report(report);
|
|
|
|
this->handleEvent(_report);
|
|
|
|
};
|
|
|
|
|
|
|
|
raw_device->addEventHandler("DEV_" + std::to_string(index), rawEventHandler);
|
|
|
|
}
|
|
|
|
|
2020-06-17 08:15:00 +00:00
|
|
|
void Device::addEventHandler(const std::string& nickname, const std::shared_ptr<EventHandler>& handler)
|
2020-06-17 06:43:53 +00:00
|
|
|
{
|
|
|
|
auto it = event_handlers.find(nickname);
|
|
|
|
assert(it == event_handlers.end());
|
|
|
|
|
|
|
|
event_handlers.emplace(nickname, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::removeEventHandler(const std::string& nickname)
|
|
|
|
{
|
|
|
|
event_handlers.erase(nickname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::handleEvent(Report& report)
|
|
|
|
{
|
|
|
|
for(auto& handler : event_handlers)
|
2020-06-17 08:15:00 +00:00
|
|
|
if(handler.second->condition(report))
|
|
|
|
handler.second->callback(report);
|
2020-06-17 06:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Report Device::sendReport(Report& report)
|
|
|
|
{
|
|
|
|
switch(report.type())
|
|
|
|
{
|
2020-06-18 05:34:25 +00:00
|
|
|
case Report::Type::Short:
|
2020-06-17 06:43:53 +00:00
|
|
|
if(!(supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
|
2020-06-18 05:34:25 +00:00
|
|
|
report.setType(Report::Type::Long);
|
2020-06-17 06:43:53 +00:00
|
|
|
break;
|
2020-06-18 05:34:25 +00:00
|
|
|
case Report::Type::Long:
|
2020-06-17 06:43:53 +00:00
|
|
|
/* Report can be truncated, but that isn't a good idea. */
|
|
|
|
assert(supported_reports & HIDPP_REPORT_LONG_SUPPORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto raw_response = raw_device->sendReport(report.rawReport());
|
|
|
|
return Report(raw_response);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::listen()
|
|
|
|
{
|
|
|
|
raw_device->listen();
|
|
|
|
}
|