Skip to content

Commit

Permalink
HID: Avoid repeated error messages from hid_write()/hid_read()
Browse files Browse the repository at this point in the history
  • Loading branch information
daschuer committed Sep 24, 2024
1 parent f5890a8 commit a8190f0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/controllers/hid/hidiooutputreport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ constexpr size_t kMaxHidErrorMessageSize = 512;
HidIoOutputReport::HidIoOutputReport(
const quint8& reportId, const unsigned int& reportDataSize)
: m_reportId(reportId),
m_hidWriteErrorLogged(false),
m_possiblyUnsentDataCached(false),
m_lastCachedDataSize(0) {
// First byte must always contain the ReportID - also after swapping, therefore initialize both arrays
Expand Down Expand Up @@ -121,10 +122,15 @@ bool HidIoOutputReport::sendCachedData(QMutex* pHidDeviceAndPollMutex,
reinterpret_cast<const unsigned char*>(m_lastSentData.constData()),
m_lastSentData.size());
if (result == -1) {
qCWarning(logOutput) << "Unable to send data to device :"
<< mixxx::convertWCStringToQString(
hid_error(pHidDevice),
kMaxHidErrorMessageSize);
if (!m_hidWriteErrorLogged) {
qCWarning(logOutput) << "Unable to send data to device :"
<< mixxx::convertWCStringToQString(
hid_error(pHidDevice),
kMaxHidErrorMessageSize);
m_hidWriteErrorLogged = true; // Don't log the same error again
}
} else {
m_hidWriteErrorLogged = false;
}

hidDeviceLock.unlock();
Expand Down
1 change: 1 addition & 0 deletions src/controllers/hid/hidiooutputreport.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class HidIoOutputReport {
private:
const quint8 m_reportId;
QByteArray m_lastSentData;
bool m_hidWriteErrorLogged;

/// Mutex must be locked when reading/writing m_cachedData
/// or m_possiblyUnsentDataCached
Expand Down
23 changes: 15 additions & 8 deletions src/controllers/hid/hidiothread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ HidIoThread::HidIoThread(
m_pHidDevice(pHidDevice),
m_lastPollSize(0),
m_pollingBufferIndex(0),
m_hidReadErrorLogged(false),
m_globalOutputReportFifo(),
m_runLoopSemaphore(1) {
// Initializing isn't strictly necessary but is good practice.
Expand Down Expand Up @@ -99,16 +100,22 @@ void HidIoThread::pollBufferedInputReports() {
int bytesRead = hid_read(m_pHidDevice, m_pPollData[m_pollingBufferIndex], kBufferSize);
if (bytesRead < 0) {
// -1 is the only error value according to hidapi documentation.
qCWarning(m_logOutput) << "Unable to read buffered HID InputReports from"
<< m_deviceInfo.formatName() << ":"
<< mixxx::convertWCStringToQString(
hid_error(m_pHidDevice),
kMaxHidErrorMessageSize);
DEBUG_ASSERT(bytesRead == -1);
if (!m_hidReadErrorLogged) {
qCWarning(m_logOutput) << "Unable to read buffered HID InputReports from"
<< m_deviceInfo.formatName() << ":"
<< mixxx::convertWCStringToQString(
hid_error(m_pHidDevice),
kMaxHidErrorMessageSize);
m_hidReadErrorLogged = true; // Don't log the same error again
}
break;
} else if (bytesRead == 0) {
// No InputReports left to be read
break;
} else {
m_hidReadErrorLogged = false; // Allow to log new errors
if (bytesRead == 0) {
// No InputReports left to be read
break;
}
}
processInputReport(bytesRead);
}
Expand Down
1 change: 1 addition & 0 deletions src/controllers/hid/hidiothread.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class HidIoThread : public QThread {
unsigned char m_pPollData[kNumBuffers][kBufferSize];
int m_lastPollSize;
int m_pollingBufferIndex;
bool m_hidReadErrorLogged;

/// Must be locked when a operation changes the size of the m_outputReports map,
/// or when modify the m_outputReportIterator
Expand Down

0 comments on commit a8190f0

Please sign in to comment.