From 261d82390bbab1f706625b5306d4add563c26682 Mon Sep 17 00:00:00 2001 From: pixl Date: Sun, 21 Jun 2020 15:42:44 -0400 Subject: [PATCH] Assert report ID and length before sending --- src/logid/backend/hidpp/Report.h | 3 ++- src/logid/backend/raw/RawDevice.cpp | 23 +++++++++++++++++++---- src/logid/backend/raw/RawDevice.h | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/logid/backend/hidpp/Report.h b/src/logid/backend/hidpp/Report.h index 82292c5..c3ab1d6 100644 --- a/src/logid/backend/hidpp/Report.h +++ b/src/logid/backend/hidpp/Report.h @@ -100,8 +100,9 @@ namespace logid::backend::hidpp bool isError20(hidpp20_error* error); std::vector rawReport () const { return _data; } - private: + static constexpr std::size_t HeaderLength = 4; + private: std::vector _data; }; } diff --git a/src/logid/backend/raw/RawDevice.cpp b/src/logid/backend/raw/RawDevice.cpp index 00582d2..be5ece5 100644 --- a/src/logid/backend/raw/RawDevice.cpp +++ b/src/logid/backend/raw/RawDevice.cpp @@ -3,6 +3,7 @@ #include "../hidpp/defs.h" #include "../dj/defs.h" #include "../../util.h" +#include "../hidpp/Report.h" #include #include @@ -22,8 +23,24 @@ using namespace logid::backend::raw; using namespace logid::backend; using namespace std::chrono; -bool RawDevice::supportedReportID(uint8_t id) +bool RawDevice::supportedReport(uint8_t id, uint8_t length) { + switch(id) + { + case hidpp::ReportType::Short: + return length == (hidpp::ShortParamLength + + hidpp::Report::HeaderLength); + case hidpp::ReportType::Long: + return length == (hidpp::LongParamLength + + hidpp::Report::HeaderLength); + case dj::ReportType::Short: + return length == (dj::ShortParamLength + dj::HeaderLength); + case dj::ReportType::Long: + return length == (dj::LongParamLength + dj::HeaderLength); + default: + return false; + } + return (hidpp::ReportType::Short == id) || (hidpp::ReportType::Long == id) || (dj::ReportType::Short == id) || (dj::ReportType::Long == id); } @@ -116,8 +133,6 @@ std::vector RawDevice::sendReport(const std::vector& report) // DJ commands are not systematically acknowledged, do not expect a result. void RawDevice::sendReportNoResponse(const std::vector& report) { - assert(supportedReportID(report[0])); - /* If the listener will stop, handle I/O manually. * Otherwise, push to queue and wait for result. */ if(continue_listen) @@ -200,7 +215,7 @@ int RawDevice::_sendReport(const std::vector& report) printf("\n"); } - assert(supportedReportID(report[0])); + assert(supportedReport(report[0], report.size())); std::lock_guard lock(dev_io); int ret = ::write(fd, report.data(), report.size()); diff --git a/src/logid/backend/raw/RawDevice.h b/src/logid/backend/raw/RawDevice.h index e03b800..4cb99c1 100644 --- a/src/logid/backend/raw/RawDevice.h +++ b/src/logid/backend/raw/RawDevice.h @@ -21,7 +21,7 @@ namespace raw class RawDevice { public: - static bool supportedReportID(uint8_t id); + static bool supportedReport(uint8_t id, uint8_t length); explicit RawDevice(std::string path); ~RawDevice();