Use consistent code style
Only files that are currently used in logid are changed.
This commit is contained in:
@@ -8,14 +8,17 @@ Error::Error(uint8_t code) : _code (code)
|
||||
|
||||
const char* Error::what() const noexcept
|
||||
{
|
||||
switch(_code)
|
||||
{
|
||||
case Unknown:
|
||||
return "Unknown";
|
||||
case KeepAliveTimeout:
|
||||
return "Keep-alive timeout";
|
||||
default:
|
||||
return std::string("Reserved error code " + std::to_string(_code))
|
||||
.c_str();
|
||||
switch(_code) {
|
||||
case Unknown:
|
||||
return "Unknown";
|
||||
case KeepAliveTimeout:
|
||||
return "Keep-alive timeout";
|
||||
default:
|
||||
return "Reserved";
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t Error::code() const noexcept
|
||||
{
|
||||
return _code;
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
#ifndef LOGID_HIDPP_BACKEND_DJ_ERROR_H
|
||||
#define LOGID_HIDPP_BACKEND_DJ_ERROR_H
|
||||
#ifndef LOGID_BACKEND_DJ_ERROR_H
|
||||
#define LOGID_BACKEND_DJ_ERROR_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
@@ -17,9 +17,9 @@ namespace dj
|
||||
KeepAliveTimeout = 0x01
|
||||
};
|
||||
|
||||
Error(uint8_t code);
|
||||
explicit Error(uint8_t code);
|
||||
|
||||
virtual const char* what() const noexcept;
|
||||
const char* what() const noexcept override;
|
||||
uint8_t code() const noexcept;
|
||||
|
||||
private:
|
||||
@@ -27,4 +27,4 @@ namespace dj
|
||||
};
|
||||
}}}
|
||||
|
||||
#endif //LOGID_HIDPP_BACKEND_DJ_ERROR_H
|
||||
#endif //LOGID_BACKEND_DJ_ERROR_H
|
@@ -12,12 +12,11 @@ InvalidReceiver::InvalidReceiver(Reason reason) : _reason (reason)
|
||||
|
||||
const char* InvalidReceiver::what() const noexcept
|
||||
{
|
||||
switch(_reason)
|
||||
{
|
||||
case NoDJReports:
|
||||
return "No DJ reports";
|
||||
default:
|
||||
return "Invalid receiver";
|
||||
switch(_reason) {
|
||||
case NoDJReports:
|
||||
return "No DJ reports";
|
||||
default:
|
||||
return "Invalid receiver";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,10 +26,10 @@ InvalidReceiver::Reason InvalidReceiver::code() const noexcept
|
||||
}
|
||||
|
||||
Receiver::Receiver(std::string path) :
|
||||
raw_device (std::make_shared<raw::RawDevice>(path)),
|
||||
_hidpp10_device (raw_device, hidpp::DefaultDevice)
|
||||
_raw_device (std::make_shared<raw::RawDevice>(std::move(path))),
|
||||
_hidpp10_device (_raw_device, hidpp::DefaultDevice)
|
||||
{
|
||||
if(!supportsDjReports(raw_device->reportDescriptor()))
|
||||
if(!supportsDjReports(_raw_device->reportDescriptor()))
|
||||
throw InvalidReceiver(InvalidReceiver::NoDJReports);
|
||||
}
|
||||
|
||||
@@ -39,28 +38,28 @@ void Receiver::enumerateDj()
|
||||
sendDjRequest(hidpp::DefaultDevice, GetPairedDevices,{});
|
||||
}
|
||||
|
||||
Receiver::notification_flags Receiver::getHidppNotifications()
|
||||
Receiver::NotificationFlags Receiver::getHidppNotifications()
|
||||
{
|
||||
auto response = _hidpp10_device.getRegister(EnableHidppNotifications, {},
|
||||
hidpp::ReportType::Short);
|
||||
|
||||
notification_flags flags{};
|
||||
flags.device_battery_status = response[0] & (1 << 4);
|
||||
flags.receiver_wireless_notifications = response[1] & (1 << 0);
|
||||
flags.receiver_software_present = response[1] & (1 << 3);
|
||||
NotificationFlags flags{};
|
||||
flags.deviceBatteryStatus = response[0] & (1 << 4);
|
||||
flags.receiverWirelessNotifications = response[1] & (1 << 0);
|
||||
flags.receiverSoftwarePresent = response[1] & (1 << 3);
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
void Receiver::enableHidppNotifications(notification_flags flags)
|
||||
void Receiver::enableHidppNotifications(NotificationFlags flags)
|
||||
{
|
||||
std::vector<uint8_t> request(3);
|
||||
|
||||
if(flags.device_battery_status)
|
||||
if(flags.deviceBatteryStatus)
|
||||
request[0] |= (1 << 4);
|
||||
if(flags.receiver_wireless_notifications)
|
||||
if(flags.receiverWirelessNotifications)
|
||||
request[1] |= 1;
|
||||
if(flags.receiver_software_present)
|
||||
if(flags.receiverSoftwarePresent)
|
||||
request[1] |= (1 << 3);
|
||||
|
||||
_hidpp10_device.setRegister(EnableHidppNotifications, request,
|
||||
@@ -228,14 +227,14 @@ hidpp::DeviceConnectionEvent Receiver::deviceConnectionEvent(
|
||||
|
||||
void Receiver::handleDjEvent(Report& report)
|
||||
{
|
||||
for(auto& handler : dj_event_handlers)
|
||||
for(auto& handler : _dj_event_handlers)
|
||||
if(handler.second->condition(report))
|
||||
handler.second->callback(report);
|
||||
}
|
||||
|
||||
void Receiver::handleHidppEvent(hidpp::Report &report)
|
||||
{
|
||||
for(auto& handler : hidpp_event_handlers)
|
||||
for(auto& handler : _hidpp_event_handlers)
|
||||
if(handler.second->condition(report))
|
||||
handler.second->callback(report);
|
||||
}
|
||||
@@ -243,39 +242,39 @@ void Receiver::handleHidppEvent(hidpp::Report &report)
|
||||
void Receiver::addDjEventHandler(const std::string& nickname,
|
||||
const std::shared_ptr<EventHandler>& handler)
|
||||
{
|
||||
auto it = dj_event_handlers.find(nickname);
|
||||
assert(it == dj_event_handlers.end());
|
||||
dj_event_handlers.emplace(nickname, handler);
|
||||
auto it = _dj_event_handlers.find(nickname);
|
||||
assert(it == _dj_event_handlers.end());
|
||||
_dj_event_handlers.emplace(nickname, handler);
|
||||
}
|
||||
|
||||
void Receiver::removeDjEventHandler(const std::string &nickname)
|
||||
{
|
||||
dj_event_handlers.erase(nickname);
|
||||
_dj_event_handlers.erase(nickname);
|
||||
}
|
||||
|
||||
const std::map<std::string, std::shared_ptr<EventHandler>>&
|
||||
Receiver::djEventHandlers()
|
||||
{
|
||||
return dj_event_handlers;
|
||||
return _dj_event_handlers;
|
||||
}
|
||||
|
||||
void Receiver::addHidppEventHandler(const std::string& nickname,
|
||||
const std::shared_ptr<hidpp::EventHandler>& handler)
|
||||
{
|
||||
auto it = hidpp_event_handlers.find(nickname);
|
||||
assert(it == hidpp_event_handlers.end());
|
||||
hidpp_event_handlers.emplace(nickname, handler);
|
||||
auto it = _hidpp_event_handlers.find(nickname);
|
||||
assert(it == _hidpp_event_handlers.end());
|
||||
_hidpp_event_handlers.emplace(nickname, handler);
|
||||
}
|
||||
|
||||
void Receiver::removeHidppEventHandler(const std::string &nickname)
|
||||
{
|
||||
hidpp_event_handlers.erase(nickname);
|
||||
_hidpp_event_handlers.erase(nickname);
|
||||
}
|
||||
|
||||
const std::map<std::string, std::shared_ptr<hidpp::EventHandler>>&
|
||||
Receiver::hidppEventHandlers()
|
||||
{
|
||||
return hidpp_event_handlers;
|
||||
return _hidpp_event_handlers;
|
||||
}
|
||||
|
||||
void Receiver::sendDjRequest(hidpp::DeviceIndex index, uint8_t function,
|
||||
@@ -290,61 +289,61 @@ void Receiver::sendDjRequest(hidpp::DeviceIndex index, uint8_t function,
|
||||
|
||||
std::copy(params.begin(), params.end(), request.paramBegin());
|
||||
|
||||
raw_device->sendReportNoResponse(request.rawData());
|
||||
_raw_device->sendReportNoResponse(request.rawData());
|
||||
}
|
||||
|
||||
void Receiver::listen()
|
||||
{
|
||||
if(!raw_device->isListening())
|
||||
std::thread{[=]() { raw_device->listen(); }}.detach();
|
||||
if(!_raw_device->isListening())
|
||||
std::thread{[=]() { _raw_device->listen(); }}.detach();
|
||||
|
||||
if(raw_device->eventHandlers().find("RECV_HIDPP") ==
|
||||
raw_device->eventHandlers().end()) {
|
||||
if(_raw_device->eventHandlers().find("RECV_HIDPP") ==
|
||||
_raw_device->eventHandlers().end()) {
|
||||
// Pass all HID++ events on DefaultDevice to handleHidppEvent
|
||||
std::shared_ptr<RawEventHandler> hidppRawEventHandler =
|
||||
std::make_shared<RawEventHandler>();
|
||||
hidppRawEventHandler->condition = [](std::vector<uint8_t>& report)->bool
|
||||
std::shared_ptr<raw::RawEventHandler> hidpp_handler =
|
||||
std::make_shared<raw::RawEventHandler>();
|
||||
hidpp_handler->condition = [](std::vector<uint8_t>& report)->bool
|
||||
{
|
||||
return (report[hidpp::Offset::Type] == hidpp::Report::Type::Short ||
|
||||
report[hidpp::Offset::Type] == hidpp::Report::Type::Long);
|
||||
};
|
||||
hidppRawEventHandler->callback = [this](std::vector<uint8_t>& report)->void
|
||||
{
|
||||
hidpp_handler->callback = [this](std::vector<uint8_t>& report)
|
||||
->void {
|
||||
hidpp::Report _report(report);
|
||||
this->handleHidppEvent(_report);
|
||||
};
|
||||
raw_device->addEventHandler("RECV_HIDPP", hidppRawEventHandler);
|
||||
_raw_device->addEventHandler("RECV_HIDPP", hidpp_handler);
|
||||
}
|
||||
|
||||
if(raw_device->eventHandlers().find("RECV_DJ") ==
|
||||
raw_device->eventHandlers().end()) {
|
||||
// Pass all DJ events with device index to handleHidppEvent
|
||||
std::shared_ptr<RawEventHandler> djRawEventHandler =
|
||||
std::make_shared<RawEventHandler>();
|
||||
djRawEventHandler->condition = [](std::vector<uint8_t>& report)->bool
|
||||
if(_raw_device->eventHandlers().find("RECV_DJ") ==
|
||||
_raw_device->eventHandlers().end()) {
|
||||
// Pass all DJ events with device index to handleDjEvent
|
||||
std::shared_ptr<raw::RawEventHandler> dj_handler =
|
||||
std::make_shared<raw::RawEventHandler>();
|
||||
dj_handler->condition = [](std::vector<uint8_t>& report)->bool
|
||||
{
|
||||
return (report[Offset::Type] == Report::Type::Short ||
|
||||
report[Offset::Type] == Report::Type::Long);
|
||||
};
|
||||
djRawEventHandler->callback = [this](std::vector<uint8_t>& report)->void
|
||||
dj_handler->callback = [this](std::vector<uint8_t>& report)->void
|
||||
{
|
||||
Report _report(report);
|
||||
this->handleDjEvent(_report);
|
||||
};
|
||||
raw_device->addEventHandler("RECV_DJ", djRawEventHandler);
|
||||
_raw_device->addEventHandler("RECV_DJ", dj_handler);
|
||||
}
|
||||
}
|
||||
|
||||
void Receiver::stopListening()
|
||||
{
|
||||
raw_device->removeEventHandler("RECV_HIDPP");
|
||||
raw_device->removeEventHandler("RECV_DJ");
|
||||
_raw_device->removeEventHandler("RECV_HIDPP");
|
||||
_raw_device->removeEventHandler("RECV_DJ");
|
||||
|
||||
if(raw_device->eventHandlers().empty())
|
||||
raw_device->stopListener();
|
||||
if(_raw_device->eventHandlers().empty())
|
||||
_raw_device->stopListener();
|
||||
}
|
||||
|
||||
std::shared_ptr<raw::RawDevice> Receiver::rawDevice() const
|
||||
{
|
||||
return raw_device;
|
||||
return _raw_device;
|
||||
}
|
@@ -25,7 +25,7 @@ namespace dj
|
||||
NoDJReports
|
||||
};
|
||||
explicit InvalidReceiver(Reason reason);
|
||||
virtual const char* what() const noexcept;
|
||||
const char* what() const noexcept override;
|
||||
Reason code() const noexcept;
|
||||
private:
|
||||
Reason _reason;
|
||||
@@ -73,15 +73,15 @@ namespace dj
|
||||
PairingInfo = 0xb5
|
||||
};
|
||||
|
||||
struct notification_flags
|
||||
struct NotificationFlags
|
||||
{
|
||||
bool device_battery_status;
|
||||
bool receiver_wireless_notifications;
|
||||
bool receiver_software_present;
|
||||
bool deviceBatteryStatus;
|
||||
bool receiverWirelessNotifications;
|
||||
bool receiverSoftwarePresent;
|
||||
};
|
||||
|
||||
notification_flags getHidppNotifications();
|
||||
void enableHidppNotifications(notification_flags flags);
|
||||
NotificationFlags getHidppNotifications();
|
||||
void enableHidppNotifications(NotificationFlags flags);
|
||||
|
||||
void enumerateHidpp();
|
||||
uint8_t getConnectionState(hidpp::DeviceIndex index);
|
||||
@@ -142,11 +142,11 @@ namespace dj
|
||||
void handleHidppEvent(hidpp::Report& report);
|
||||
|
||||
std::map<std::string, std::shared_ptr<EventHandler>>
|
||||
dj_event_handlers;
|
||||
_dj_event_handlers;
|
||||
std::map<std::string, std::shared_ptr<hidpp::EventHandler>>
|
||||
hidpp_event_handlers;
|
||||
_hidpp_event_handlers;
|
||||
|
||||
std::shared_ptr<raw::RawDevice> raw_device;
|
||||
std::shared_ptr<raw::RawDevice> _raw_device;
|
||||
hidpp10::Device _hidpp10_device;
|
||||
};
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ ReceiverMonitor::ReceiverMonitor(std::string path) : _receiver (
|
||||
assert(_receiver->djEventHandlers().find("RECVMON") ==
|
||||
_receiver->djEventHandlers().end());
|
||||
|
||||
Receiver::notification_flags notification_flags{
|
||||
Receiver::NotificationFlags notification_flags{
|
||||
true,
|
||||
true,
|
||||
true};
|
||||
@@ -25,16 +25,15 @@ void ReceiverMonitor::run()
|
||||
_receiver->listen();
|
||||
|
||||
if(_receiver->hidppEventHandlers().find("RECVMON") ==
|
||||
_receiver->hidppEventHandlers().end())
|
||||
{
|
||||
std::shared_ptr<hidpp::EventHandler> eventHandler =
|
||||
_receiver->hidppEventHandlers().end()) {
|
||||
std::shared_ptr<hidpp::EventHandler> event_handler =
|
||||
std::make_shared<hidpp::EventHandler>();
|
||||
eventHandler->condition = [](hidpp::Report &report) -> bool {
|
||||
event_handler->condition = [](hidpp::Report &report) -> bool {
|
||||
return (report.subId() == Receiver::DeviceConnection ||
|
||||
report.subId() == Receiver::DeviceDisconnection);
|
||||
};
|
||||
|
||||
eventHandler->callback = [this](hidpp::Report &report) -> void {
|
||||
event_handler->callback = [this](hidpp::Report &report) -> void {
|
||||
/* Running in a new thread prevents deadlocks since the
|
||||
* receiver may be enumerating.
|
||||
*/
|
||||
@@ -48,7 +47,7 @@ void ReceiverMonitor::run()
|
||||
}, report}.detach();
|
||||
};
|
||||
|
||||
_receiver->addHidppEventHandler("RECVMON", eventHandler);
|
||||
_receiver->addHidppEventHandler("RECVMON", event_handler);
|
||||
}
|
||||
|
||||
enumerate();
|
||||
|
@@ -28,37 +28,36 @@ static const std::array<uint8_t, 34> DJReportDesc = {
|
||||
|
||||
bool dj::supportsDjReports(std::vector<uint8_t>&& rdesc)
|
||||
{
|
||||
auto it = std::search(rdesc.begin(), rdesc.end(), DJReportDesc.begin(), DJReportDesc.end());
|
||||
auto it = std::search(rdesc.begin(), rdesc.end(),
|
||||
DJReportDesc.begin(), DJReportDesc.end());
|
||||
return it != rdesc.end();
|
||||
}
|
||||
|
||||
Report::Report(std::vector<uint8_t>& data) : _data (data)
|
||||
{
|
||||
switch(data[Offset::Type])
|
||||
{
|
||||
case ReportType::Short:
|
||||
_data.resize(HeaderLength+ShortParamLength);
|
||||
break;
|
||||
case ReportType::Long:
|
||||
_data.resize(HeaderLength+LongParamLength);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
switch(data[Offset::Type]) {
|
||||
case ReportType::Short:
|
||||
_data.resize(HeaderLength+ShortParamLength);
|
||||
break;
|
||||
case ReportType::Long:
|
||||
_data.resize(HeaderLength+LongParamLength);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
Report::Report(Report::Type type, hidpp::DeviceIndex index, uint8_t feature)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case ReportType::Short:
|
||||
_data.resize(HeaderLength+ShortParamLength);
|
||||
break;
|
||||
case ReportType::Long:
|
||||
_data.resize(HeaderLength+LongParamLength);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
switch(type) {
|
||||
case ReportType::Short:
|
||||
_data.resize(HeaderLength+ShortParamLength);
|
||||
break;
|
||||
case ReportType::Long:
|
||||
_data.resize(HeaderLength+LongParamLength);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
_data[Offset::Type] = type;
|
||||
|
@@ -6,7 +6,9 @@
|
||||
#include "defs.h"
|
||||
#include "../hidpp/defs.h"
|
||||
|
||||
namespace logid::backend::dj
|
||||
namespace logid {
|
||||
namespace backend {
|
||||
namespace dj
|
||||
{
|
||||
namespace Offset
|
||||
{
|
||||
@@ -33,6 +35,6 @@ namespace logid::backend::dj
|
||||
private:
|
||||
std::vector<uint8_t> _data;
|
||||
};
|
||||
}
|
||||
}}}
|
||||
|
||||
#endif //LOGID_BACKEND_DJ_REPORT_H
|
||||
|
Reference in New Issue
Block a user