From 8d65947835a8c96249cd75f32c1e6298b82ae770 Mon Sep 17 00:00:00 2001 From: JoergAtGithub Date: Thu, 6 Jan 2022 00:26:04 +0100 Subject: [PATCH] Separate class HidIoReport into dedicated files --- src/controllers/hid/hidio.cpp | 55 ----------------------------- src/controllers/hid/hidio.h | 17 +-------- src/controllers/hid/hidioreport.cpp | 55 +++++++++++++++++++++++++++++ src/controllers/hid/hidioreport.h | 30 ++++++++++++++++ 4 files changed, 86 insertions(+), 71 deletions(-) create mode 100644 src/controllers/hid/hidioreport.cpp create mode 100644 src/controllers/hid/hidioreport.h diff --git a/src/controllers/hid/hidio.cpp b/src/controllers/hid/hidio.cpp index 94bbf830669e..9ca938de5634 100644 --- a/src/controllers/hid/hidio.cpp +++ b/src/controllers/hid/hidio.cpp @@ -11,61 +11,6 @@ #include "util/time.h" #include "util/trace.h" -namespace { -constexpr int kReportIdSize = 1; -constexpr int kMaxHidErrorMessageSize = 512; -QString loggingCategoryPrefix(const QString& deviceName) { - return QStringLiteral("controller.") + - RuntimeLoggingCategory::removeInvalidCharsFromCategory(deviceName.toLower()); -} -} // namespace - -HidIoReport::HidIoReport(const unsigned char& reportId, - hid_device* device, - const mixxx::hid::DeviceInfo&& deviceInfo) - : m_reportId(reportId), - m_logOutput(loggingCategoryPrefix(deviceInfo.formatName()) + QStringLiteral(".output")), - m_pHidDevice(device), - m_deviceInfo(std::move(deviceInfo)), - m_lastSentOutputReport() { -} - -void HidIoReport::sendOutputReport(QByteArray data) { - auto startOfHidWrite = mixxx::Time::elapsed(); - if (!m_lastSentOutputReport.compare(data)) { - qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit() - << " Skipped identical Output Report for" << m_deviceInfo.formatName() - << "serial #" << m_deviceInfo.serialNumberRaw() - << "(Report ID" << m_reportId << ")"; - return; // Same data sent last time - } - - // hid_write requires the first byte to be the Report ID, followed by the data[] to be send - QByteArray outputReport; - outputReport.reserve(data.size() + kReportIdSize); - outputReport.append(m_reportId); - outputReport.append(data); - - // hid_write can take several milliseconds, because hidapi synchronizes the asyncron HID communication from the OS - int result = hid_write(m_pHidDevice, - (unsigned char*)outputReport.constData(), - outputReport.size()); - if (result == -1) { - qCWarning(m_logOutput) << "Unable to send data to" << m_deviceInfo.formatName() << ":" - << mixxx::convertWCStringToQString( - hid_error(m_pHidDevice), - kMaxHidErrorMessageSize); - } else { - qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit() << " " - << result << "bytes sent to" << m_deviceInfo.formatName() - << "serial #" << m_deviceInfo.serialNumberRaw() - << "(including report ID of" << m_reportId << ") - Needed: " - << (mixxx::Time::elapsed() - startOfHidWrite).formatMicrosWithUnit(); - - m_lastSentOutputReport = std::move(data); - } -} - HidIoThread::HidIoThread( hid_device* device, const mixxx::hid::DeviceInfo&& deviceInfo) : QThread(), diff --git a/src/controllers/hid/hidio.h b/src/controllers/hid/hidio.h index fb56d1855eda..cd95675eddfc 100644 --- a/src/controllers/hid/hidio.h +++ b/src/controllers/hid/hidio.h @@ -5,26 +5,11 @@ #include "controllers/controller.h" #include "controllers/hid/hiddevice.h" +#include "controllers/hid/hidioreport.h" #include "util/compatibility/qatomic.h" #include "util/compatibility/qmutex.h" #include "util/duration.h" -class HidIoReport { - public: - HidIoReport(const unsigned char& reportId, - hid_device* device, - const mixxx::hid::DeviceInfo&& deviceInfo); - void sendOutputReport(QByteArray data); - - private: - const unsigned char m_reportId; - const RuntimeLoggingCategory m_logOutput; - hid_device* const - m_pHidDevice; // const pointer to the C data structure, which hidapi uses for communication between functions - const mixxx::hid::DeviceInfo m_deviceInfo; - QByteArray m_lastSentOutputReport; -}; - class HidIoThread : public QThread { Q_OBJECT public: diff --git a/src/controllers/hid/hidioreport.cpp b/src/controllers/hid/hidioreport.cpp new file mode 100644 index 000000000000..7c82470dc563 --- /dev/null +++ b/src/controllers/hid/hidioreport.cpp @@ -0,0 +1,55 @@ +#include "controllers/hid/hidioreport.h" + +#include + +#include "controllers/defs_controllers.h" +#include "controllers/hid/legacyhidcontrollermappingfilehandler.h" +#include "util/string.h" +#include "util/time.h" +#include "util/trace.h" + +HidIoReport::HidIoReport(const unsigned char& reportId, + hid_device* device, + const mixxx::hid::DeviceInfo&& deviceInfo) + : m_reportId(reportId), + m_logOutput(loggingCategoryPrefix(deviceInfo.formatName()) + QStringLiteral(".output")), + m_pHidDevice(device), + m_deviceInfo(std::move(deviceInfo)), + m_lastSentOutputReport() { +} + +void HidIoReport::sendOutputReport(QByteArray data) { + auto startOfHidWrite = mixxx::Time::elapsed(); + if (!m_lastSentOutputReport.compare(data)) { + qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit() + << " Skipped identical Output Report for" << m_deviceInfo.formatName() + << "serial #" << m_deviceInfo.serialNumberRaw() + << "(Report ID" << m_reportId << ")"; + return; // Same data sent last time + } + + // hid_write requires the first byte to be the Report ID, followed by the data[] to be send + QByteArray outputReport; + outputReport.reserve(data.size() + kReportIdSize); + outputReport.append(m_reportId); + outputReport.append(data); + + // hid_write can take several milliseconds, because hidapi synchronizes the asyncron HID communication from the OS + int result = hid_write(m_pHidDevice, + (unsigned char*)outputReport.constData(), + outputReport.size()); + if (result == -1) { + qCWarning(m_logOutput) << "Unable to send data to" << m_deviceInfo.formatName() << ":" + << mixxx::convertWCStringToQString( + hid_error(m_pHidDevice), + kMaxHidErrorMessageSize); + } else { + qCDebug(m_logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit() << " " + << result << "bytes sent to" << m_deviceInfo.formatName() + << "serial #" << m_deviceInfo.serialNumberRaw() + << "(including report ID of" << m_reportId << ") - Needed: " + << (mixxx::Time::elapsed() - startOfHidWrite).formatMicrosWithUnit(); + + m_lastSentOutputReport = std::move(data); + } +} diff --git a/src/controllers/hid/hidioreport.h b/src/controllers/hid/hidioreport.h new file mode 100644 index 000000000000..8457ebe6ffb5 --- /dev/null +++ b/src/controllers/hid/hidioreport.h @@ -0,0 +1,30 @@ +#pragma once + +#include "controllers/controller.h" +#include "controllers/hid/hiddevice.h" +#include "util/duration.h" + +namespace { +constexpr int kReportIdSize = 1; +constexpr int kMaxHidErrorMessageSize = 512; +QString loggingCategoryPrefix(const QString& deviceName) { + return QStringLiteral("controller.") + + RuntimeLoggingCategory::removeInvalidCharsFromCategory(deviceName.toLower()); +} +} // namespace + +class HidIoReport { + public: + HidIoReport(const unsigned char& reportId, + hid_device* device, + const mixxx::hid::DeviceInfo&& deviceInfo); + void sendOutputReport(QByteArray data); + + private: + const unsigned char m_reportId; + const RuntimeLoggingCategory m_logOutput; + hid_device* const + m_pHidDevice; // const pointer to the C data structure, which hidapi uses for communication between functions + const mixxx::hid::DeviceInfo m_deviceInfo; + QByteArray m_lastSentOutputReport; +};