Use consistent code style

Only files that are currently used in logid are changed.
This commit is contained in:
pixl
2020-06-23 22:35:37 -04:00
parent bd080e7ef6
commit dd75df8c18
34 changed files with 560 additions and 537 deletions

View File

@@ -13,16 +13,15 @@ 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";
case Asleep:
return "Device asleep";
default:
return "Invalid device";
switch(_reason) {
case NoHIDPPReport:
return "Invalid HID++ device";
case InvalidRawDevice:
return "Invalid raw device";
case Asleep:
return "Device asleep";
default:
return "Invalid device";
}
}
@@ -58,10 +57,25 @@ Device::Device(std::shared_ptr<dj::Receiver> receiver,
_init();
}
std::string Device::devicePath() const
{
return _path;
}
DeviceIndex Device::deviceIndex() const
{
return _index;
}
std::tuple<uint8_t, uint8_t> Device::version() const
{
return _version;
}
void Device::_init()
{
supported_reports = getSupportedReports(_raw_device->reportDescriptor());
if(!supported_reports)
_supported_reports = getSupportedReports(_raw_device->reportDescriptor());
if(!_supported_reports)
throw InvalidDevice(InvalidDevice::NoHIDPPReport);
try {
@@ -101,20 +115,20 @@ Device::~Device()
void Device::addEventHandler(const std::string& nickname,
const std::shared_ptr<EventHandler>& handler)
{
auto it = event_handlers.find(nickname);
assert(it == event_handlers.end());
auto it = _event_handlers.find(nickname);
assert(it == _event_handlers.end());
event_handlers.emplace(nickname, handler);
_event_handlers.emplace(nickname, handler);
}
void Device::removeEventHandler(const std::string& nickname)
{
event_handlers.erase(nickname);
_event_handlers.erase(nickname);
}
void Device::handleEvent(Report& report)
{
for(auto& handler : event_handlers)
for(auto& handler : _event_handlers)
if(handler.second->condition(report))
handler.second->callback(report);
}
@@ -123,26 +137,26 @@ Report Device::sendReport(Report& report)
{
switch(report.type())
{
case Report::Type::Short:
if(!(supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
report.setType(Report::Type::Long);
break;
case Report::Type::Long:
/* Report can be truncated, but that isn't a good idea. */
assert(supported_reports & HIDPP_REPORT_LONG_SUPPORTED);
case Report::Type::Short:
if(!(_supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
report.setType(Report::Type::Long);
break;
case Report::Type::Long:
/* 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());
Report response(raw_response);
Report::hidpp10_error hidpp10Error{};
if(response.isError10(&hidpp10Error))
throw hidpp10::Error(hidpp10Error.error_code);
Report::Hidpp10Error hidpp10_error{};
if(response.isError10(&hidpp10_error))
throw hidpp10::Error(hidpp10_error.error_code);
Report::hidpp20_error hidpp20Error{};
if(response.isError20(&hidpp20Error))
throw hidpp10::Error(hidpp20Error.error_code);
Report::Hidpp20Error hidpp20_error{};
if(response.isError20(&hidpp20_error))
throw hidpp10::Error(hidpp20_error.error_code);
return response;
}
@@ -153,20 +167,20 @@ void Device::listen()
std::thread{[=]() { _raw_device->listen(); }}.detach();
// Pass all HID++ events with device index to this device.
std::shared_ptr<RawEventHandler> rawEventHandler;
rawEventHandler->condition = [this](std::vector<uint8_t>& report)->bool
std::shared_ptr<raw::RawEventHandler> handler;
handler->condition = [this](std::vector<uint8_t>& report)->bool
{
return (report[Offset::Type] == Report::Type::Short ||
report[Offset::Type] == Report::Type::Long) &&
(report[Offset::DeviceIndex] == this->_index);
};
rawEventHandler->callback = [this](std::vector<uint8_t>& report)->void
handler->callback = [this](std::vector<uint8_t>& report)->void
{
Report _report(report);
this->handleEvent(_report);
};
_raw_device->addEventHandler("DEV_" + std::to_string(_index), rawEventHandler);
_raw_device->addEventHandler("DEV_" + std::to_string(_index), handler);
}
void Device::stopListening()

View File

@@ -1,5 +1,5 @@
#ifndef LOGID_HIDPP_DEVICE_H
#define LOGID_HIDPP_DEVICE_H
#ifndef LOGID_BACKEND_HIDPP_DEVICE_H
#define LOGID_BACKEND_HIDPP_DEVICE_H
#include <string>
#include <memory>
@@ -44,14 +44,15 @@ namespace hidpp
};
explicit Device(const std::string& path, DeviceIndex index);
explicit Device(std::shared_ptr<raw::RawDevice> raw_device, DeviceIndex index);
explicit Device(std::shared_ptr<raw::RawDevice> raw_device,
DeviceIndex index);
explicit Device(std::shared_ptr<dj::Receiver> receiver,
hidpp::DeviceConnectionEvent event);
~Device();
std::string devicePath() const { return _path; }
DeviceIndex deviceIndex() const { return _index; }
std::tuple<uint8_t, uint8_t> version() const { return _version; }
std::string devicePath() const;
DeviceIndex deviceIndex() const;
std::tuple<uint8_t, uint8_t> version() const;
void listen(); // Runs asynchronously
void stopListening();
@@ -70,14 +71,14 @@ namespace hidpp
std::shared_ptr<dj::Receiver> _receiver;
std::string _path;
DeviceIndex _index;
uint8_t supported_reports;
uint8_t _supported_reports;
std::tuple<uint8_t, uint8_t> _version;
uint16_t _pid;
std::string _name;
std::map<std::string, std::shared_ptr<EventHandler>> event_handlers;
std::map<std::string, std::shared_ptr<EventHandler>> _event_handlers;
};
} } }
#endif //LOGID_HIDPP_DEVICE_H
#endif //LOGID_BACKEND_HIDPP_DEVICE_H

View File

@@ -70,15 +70,19 @@ uint8_t hidpp::getSupportedReports(std::vector<uint8_t>&& rdesc)
{
uint8_t ret = 0;
auto it = std::search(rdesc.begin(), rdesc.end(), ShortReportDesc.begin(), ShortReportDesc.end());
auto it = std::search(rdesc.begin(), rdesc.end(),
ShortReportDesc.begin(), ShortReportDesc.end());
if(it == rdesc.end())
it = std::search(rdesc.begin(), rdesc.end(), ShortReportDesc2.begin(), ShortReportDesc2.end());
it = std::search(rdesc.begin(), rdesc.end(),
ShortReportDesc2.begin(), ShortReportDesc2.end());
if(it != rdesc.end())
ret |= HIDPP_REPORT_SHORT_SUPPORTED;
it = std::search(rdesc.begin(), rdesc.end(), LongReportDesc.begin(), LongReportDesc.end());
it = std::search(rdesc.begin(), rdesc.end(),
LongReportDesc.begin(), LongReportDesc.end());
if(it == rdesc.end())
it = std::search(rdesc.begin(), rdesc.end(), LongReportDesc2.begin(), LongReportDesc2.end());
it = std::search(rdesc.begin(), rdesc.end(),
LongReportDesc2.begin(), LongReportDesc2.end());
if(it != rdesc.end())
ret |= HIDPP_REPORT_LONG_SUPPORTED;
@@ -98,16 +102,15 @@ const char *Report::InvalidReportLength::what() const noexcept
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();
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;
@@ -119,25 +122,25 @@ Report::Report(Report::Type type, DeviceIndex device_index,
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 <= 0x0f);
assert(sw_id <= 0x0f);
switch(type)
{
case Type::Short:
_data.resize(HeaderLength + ShortParamLength);
break;
case Type::Long:
_data.resize(HeaderLength + LongParamLength);
break;
default:
throw InvalidReportID();
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::Feature] = feature_index;
_data[Offset::Function] = (function & functionMask) << 4 | (sw_id & swIdMask);
_data[Offset::Function] = (function & 0x0f) << 4 |
(sw_id & 0x0f);
}
Report::Report(const std::vector<uint8_t>& data)
@@ -145,16 +148,15 @@ Report::Report(const std::vector<uint8_t>& data)
_data = data;
// Truncating data is entirely valid here.
switch(_data[Offset::Type])
{
case Type::Short:
_data.resize(HeaderLength + ShortParamLength);
break;
case Type::Long:
_data.resize(HeaderLength + LongParamLength);
break;
default:
throw InvalidReportID();
switch(_data[Offset::Type]) {
case Type::Short:
_data.resize(HeaderLength + ShortParamLength);
break;
case Type::Long:
_data.resize(HeaderLength + LongParamLength);
break;
default:
throw InvalidReportID();
}
}
@@ -165,16 +167,15 @@ Report::Type Report::type() const
void Report::setType(Report::Type type)
{
switch(type)
{
case Type::Short:
_data.resize(HeaderLength + ShortParamLength);
break;
case Type::Long:
_data.resize(HeaderLength + LongParamLength);
break;
default:
throw InvalidReportID();
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;
@@ -218,7 +219,7 @@ uint8_t Report::function() const
void Report::setFunction(uint8_t function)
{
_data[Offset::Function] &= 0x0f;
_data[Offset::Function] |= (function << 4) & 0x0f;
_data[Offset::Function] |= (function & 0x0f) << 4;
}
uint8_t Report::swId() const
@@ -250,7 +251,7 @@ void Report::setParams(const std::vector<uint8_t>& _params)
_data[Offset::Parameters + i] = _params[i];
}
bool Report::isError10(Report::hidpp10_error *error)
bool Report::isError10(Report::Hidpp10Error *error)
{
assert(error != nullptr);
@@ -265,8 +266,10 @@ bool Report::isError10(Report::hidpp10_error *error)
return true;
}
bool Report::isError20(Report::hidpp20_error* error)
bool Report::isError20(Report::Hidpp20Error* error)
{
assert(error != nullptr);
if(_data[Offset::Type] != Type::Long ||
_data[Offset::Feature] != hidpp20::ErrorID)
return false;

View File

@@ -10,7 +10,9 @@
#define HIDPP_REPORT_LONG_SUPPORTED 1U<<1U
/* Very long reports exist, however they have not been encountered so far */
namespace logid::backend::hidpp
namespace logid {
namespace backend {
namespace hidpp
{
uint8_t getSupportedReports(std::vector<uint8_t>&& rdesc);
@@ -34,19 +36,17 @@ namespace logid::backend::hidpp
{
public:
InvalidReportID() = default;
virtual const char* what() const noexcept;
const char* what() const noexcept override;
};
class InvalidReportLength: public std::exception
{
public:
InvalidReportLength() = default;;
virtual const char* what() const noexcept;
const char* what() const noexcept override;
};
static constexpr std::size_t MaxDataLength = 20;
static constexpr uint8_t swIdMask = 0x0f;
static constexpr uint8_t functionMask = 0x0f;
Report(Report::Type type, DeviceIndex device_index,
uint8_t sub_id,
@@ -85,19 +85,17 @@ namespace logid::backend::hidpp
std::vector<uint8_t>::iterator paramEnd() { return _data.end(); }
void setParams(const std::vector<uint8_t>& _params);
struct hidpp10_error
struct Hidpp10Error
{
uint8_t sub_id, address, error_code;
};
bool isError10(Hidpp10Error* error);
bool isError10(hidpp10_error* error);
struct hidpp20_error
struct Hidpp20Error
{
uint8_t feature_index, function, software_id, error_code;
};
bool isError20(hidpp20_error* error);
bool isError20(Hidpp20Error* error);
std::vector<uint8_t> rawReport () const { return _data; }
@@ -105,6 +103,6 @@ namespace logid::backend::hidpp
private:
std::vector<uint8_t> _data;
};
}
}}}
#endif //LOGID_BACKEND_HIDPP_REPORT_H

View File

@@ -3,7 +3,9 @@
#define LOGID_HIDPP_SOFTWARE_ID 0
namespace logid::backend::hidpp
namespace logid {
namespace backend {
namespace hidpp
{
namespace ReportType
{
@@ -28,6 +30,6 @@ namespace logid::backend::hidpp
static constexpr std::size_t ShortParamLength = 3;
static constexpr std::size_t LongParamLength = 16;
}
} } }
#endif //LOGID_HIDPP_DEFS_H