Print version number of device 1 on each raw dev.
Only works on HID++ >=2.0 so far. Also solves a race condition where the wrong response can be sent to a request.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <assert.h>
|
||||
#include "Device.h"
|
||||
#include "Report.h"
|
||||
#include "../hidpp20/features/Root.h"
|
||||
|
||||
using namespace logid::backend;
|
||||
using namespace logid::backend::hidpp;
|
||||
@@ -29,12 +30,20 @@ Device::Device(const std::string& path, DeviceIndex index):
|
||||
if(!supported_reports)
|
||||
throw InvalidDevice(InvalidDevice::NoHIDPPReport);
|
||||
|
||||
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]);
|
||||
|
||||
// Pass all HID++ events with device index to this device.
|
||||
RawEventHandler rawEventHandler;
|
||||
rawEventHandler.condition = [index](std::vector<uint8_t>& report)->bool
|
||||
{
|
||||
return (report[Offset::Type] == Report::Short ||
|
||||
report[Offset::Type] == Report::Long) && (report[Offset::DeviceIndex] == index);
|
||||
return (report[Offset::Type] == Report::Type::Short ||
|
||||
report[Offset::Type] == Report::Type::Long) && (report[Offset::DeviceIndex] == index);
|
||||
};
|
||||
rawEventHandler.callback = [this](std::vector<uint8_t>& report)->void
|
||||
{
|
||||
@@ -69,11 +78,11 @@ Report Device::sendReport(Report& report)
|
||||
{
|
||||
switch(report.type())
|
||||
{
|
||||
case Report::Short:
|
||||
case Report::Type::Short:
|
||||
if(!(supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
|
||||
report.setType(Report::Long);
|
||||
report.setType(Report::Type::Long);
|
||||
break;
|
||||
case Report::Long:
|
||||
case Report::Type::Long:
|
||||
/* Report can be truncated, but that isn't a good idea. */
|
||||
assert(supported_reports & HIDPP_REPORT_LONG_SUPPORTED);
|
||||
}
|
||||
|
@@ -40,6 +40,7 @@ namespace hidpp
|
||||
|
||||
std::string devicePath() const { return path; }
|
||||
DeviceIndex deviceIndex() const { return index; }
|
||||
std::tuple<uint8_t, uint8_t> version() const { return _version; }
|
||||
|
||||
void listen(); // Runs asynchronously
|
||||
void stopListening();
|
||||
@@ -56,6 +57,8 @@ namespace hidpp
|
||||
DeviceIndex index;
|
||||
uint8_t supported_reports;
|
||||
|
||||
std::tuple<uint8_t, uint8_t> _version;
|
||||
|
||||
std::map<std::string, std::shared_ptr<EventHandler>> event_handlers;
|
||||
};
|
||||
} } }
|
||||
|
@@ -96,15 +96,15 @@ const char *Report::InvalidReportLength::what() const noexcept
|
||||
Report::Report(Report::Type type, DeviceIndex device_index,
|
||||
uint8_t feature_index, uint8_t function, uint8_t sw_id)
|
||||
{
|
||||
assert(!(function & functionMask));
|
||||
assert(!(sw_id & swIdMask));
|
||||
assert(function <= functionMask);
|
||||
assert(sw_id <= swIdMask);
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case Short:
|
||||
case Type::Short:
|
||||
_data.resize(HeaderLength + ShortParamLength);
|
||||
break;
|
||||
case Long:
|
||||
case Type::Long:
|
||||
_data.resize(HeaderLength + LongParamLength);
|
||||
break;
|
||||
default:
|
||||
@@ -123,10 +123,10 @@ Report::Report(const std::vector<uint8_t>& data)
|
||||
|
||||
switch(_data[Offset::Type])
|
||||
{
|
||||
case Short:
|
||||
case Type::Short:
|
||||
_data.resize(HeaderLength + ShortParamLength);
|
||||
break;
|
||||
case Long:
|
||||
case Type::Long:
|
||||
_data.resize(HeaderLength + LongParamLength);
|
||||
break;
|
||||
default:
|
||||
@@ -138,10 +138,10 @@ void Report::setType(Report::Type type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case Short:
|
||||
case Type::Short:
|
||||
_data.resize(HeaderLength + ShortParamLength);
|
||||
break;
|
||||
case Long:
|
||||
case Type::Long:
|
||||
_data.resize(HeaderLength + LongParamLength);
|
||||
break;
|
||||
default:
|
||||
@@ -158,3 +158,10 @@ void Report::setParams(const std::vector<uint8_t>& _params)
|
||||
for(std::size_t i = 0; i < _params.size(); i++)
|
||||
_data[Offset::Parameters + i] = _params[i];
|
||||
}
|
||||
|
||||
bool Report::isError20(Report::hidpp20_error* error)
|
||||
{
|
||||
if(_data[Offset::Type] != Type::Long ||
|
||||
_data[Offset::Feature] != 0xff)
|
||||
return false;
|
||||
}
|
@@ -26,11 +26,7 @@ namespace logid::backend::hidpp
|
||||
class Report
|
||||
{
|
||||
public:
|
||||
enum Type: uint8_t
|
||||
{
|
||||
Short = 0x10,
|
||||
Long = 0x11
|
||||
};
|
||||
typedef ReportType::ReportType Type;
|
||||
|
||||
class InvalidReportID: public std::exception
|
||||
{
|
||||
@@ -50,19 +46,26 @@ namespace logid::backend::hidpp
|
||||
static constexpr uint8_t swIdMask = 0x0f;
|
||||
static constexpr uint8_t functionMask = 0x0f;
|
||||
|
||||
Report(Type type, DeviceIndex device_index,
|
||||
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);
|
||||
|
||||
Type type() const { return static_cast<Type>(_data[Offset::Type]); };
|
||||
Report::Type type() const { return static_cast<Report::Type>(_data[Offset::Type]); };
|
||||
void setType(Report::Type type);
|
||||
|
||||
std::vector<uint8_t>::const_iterator paramBegin() const { return _data.begin() + Offset::Parameters; }
|
||||
std::vector<uint8_t>::const_iterator paramEnd() const { return _data.end(); }
|
||||
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);
|
||||
|
||||
struct hidpp20_error
|
||||
{
|
||||
uint8_t feature_index, function, software_id, error_code;
|
||||
};
|
||||
|
||||
bool isError20(hidpp20_error* error);
|
||||
|
||||
logid::backend::hidpp::DeviceIndex deviceIndex()
|
||||
{
|
||||
return static_cast<DeviceIndex>(_data[Offset::DeviceIndex]);
|
||||
|
@@ -5,6 +5,15 @@
|
||||
|
||||
namespace logid::backend::hidpp
|
||||
{
|
||||
namespace ReportType
|
||||
{
|
||||
enum ReportType : uint8_t
|
||||
{
|
||||
Short = 0x10,
|
||||
Long = 0x11
|
||||
};
|
||||
}
|
||||
|
||||
enum DeviceIndex: uint8_t
|
||||
{
|
||||
DefaultDevice = 0,
|
||||
|
Reference in New Issue
Block a user