Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HidController: return QByteArrays for get[Input/Feature]Report #4521

Merged
merged 3 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions src/controllers/hid/hidcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void HidController::processInputReport(int bytesRead) {
receive(incomingData, mixxx::Time::elapsed());
}

QList<int> HidController::getInputReport(unsigned int reportID) {
QByteArray HidController::getInputReport(unsigned int reportID) {
Trace hidRead("HidController getInputReport");
int bytesRead;

Expand All @@ -185,17 +185,11 @@ QList<int> HidController::getInputReport(unsigned int reportID) {
// Otherwise minimum possible value is 1, because 1 byte is for the reportID,
// the smallest report with data is therefore 2 bytes.
DEBUG_ASSERT(bytesRead <= kReportIdSize);
return QList<int>();
return QByteArray();
}

// Convert array of bytes read in a JavaScript compatible return type
// For compatibility with the array provided by HidController::poll the reportID is contained as prefix
QList<int> dataList;
dataList.reserve(bytesRead);
for (int i = 0; i < bytesRead; i++) {
dataList.append(m_pPollData[m_pollingBufferIndex][i]);
}
return dataList;
return QByteArray::fromRawData(
reinterpret_cast<char*>(m_pPollData[m_pollingBufferIndex]), bytesRead);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reintepret_casts are more dangerous than static_casts.
would this also work?

Suggested change
reinterpret_cast<char*>(m_pPollData[m_pollingBufferIndex]), bytesRead);
static_cast<char*>(m_pPollData[m_pollingBufferIndex]), bytesRead);

or maybe

Suggested change
reinterpret_cast<char*>(m_pPollData[m_pollingBufferIndex]), bytesRead);
static_cast<char*>(&m_pPollData[m_pollingBufferIndex]), bytesRead);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, with the first:

/home/be/sw/mixxx/src/controllers/hid/hidcontroller.cpp: In member function ‘QByteArray HidController::getInputReport(unsigned int)’:
/home/be/sw/mixxx/src/controllers/hid/hidcontroller.cpp:192:13: error: invalid ‘static_cast’ from type ‘unsigned char [255]’ to type ‘char*’
  192 |             static_cast<char*>(m_pPollData[m_pollingBufferIndex]), bytesRead);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

with the second:

/home/be/sw/mixxx/src/controllers/hid/hidcontroller.cpp: In member function ‘QByteArray HidController::getInputReport(unsigned int)’:
/home/be/sw/mixxx/src/controllers/hid/hidcontroller.cpp:192:13: error: invalid ‘static_cast’ from type ‘unsigned char (*)[255]’ to type ‘char’
  192 |             static_cast<char>(&m_pPollData[m_pollingBufferIndex]), bytesRead);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I looked into the issue. The problem is not the array cast, its the unsigned-to-signed conversion.
https://godbolt.org/z/n974WvWz9

Copy link
Member

@Swiftb0y Swiftb0y Nov 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the reinterpret cast is unavoidable as far as I can tell.

}

bool HidController::poll() {
Expand Down Expand Up @@ -255,14 +249,14 @@ void HidController::sendBytesReport(QByteArray data, unsigned int reportID) {
}

void HidController::sendFeatureReport(
const QList<int>& dataList, unsigned int reportID) {
const QByteArray& reportData, unsigned int reportID) {
QByteArray dataArray;
dataArray.reserve(kReportIdSize + dataList.size());
dataArray.reserve(kReportIdSize + reportData.size());

// Append the Report ID to the beginning of dataArray[] per the API..
dataArray.append(reportID);

for (const int datum : dataList) {
for (const int datum : reportData) {
dataArray.append(datum);
}

Expand All @@ -287,7 +281,7 @@ ControllerJSProxy* HidController::jsProxy() {
return new HidControllerJSProxy(this);
}

QList<int> HidController::getFeatureReport(
QByteArray HidController::getFeatureReport(
unsigned int reportID) {
unsigned char dataRead[kReportIdSize + kBufferSize];
dataRead[0] = reportID;
Expand Down Expand Up @@ -318,10 +312,8 @@ QList<int> HidController::getFeatureReport(

// Convert array of bytes read in a JavaScript compatible return type
// For compatibility with input array HidController::sendFeatureReport, a reportID prefix is not added here
QList<int> dataList;
dataList.reserve(bytesRead - kReportIdSize);
for (int i = kReportIdSize; i < bytesRead; i++) {
dataList.append(dataRead[i]);
}
return dataList;
QByteArray byteArray;
byteArray.reserve(bytesRead - kReportIdSize);
Be-ing marked this conversation as resolved.
Show resolved Hide resolved
auto featureReportStart = reinterpret_cast<const char*>(dataRead + kReportIdSize);
return QByteArray(featureReportStart, bytesRead);
}
14 changes: 7 additions & 7 deletions src/controllers/hid/hidcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HidController final : public Controller {
// 0x0.
void sendBytes(const QByteArray& data) override;
void sendBytesReport(QByteArray data, unsigned int reportID);
void sendFeatureReport(const QList<int>& dataList, unsigned int reportID);
void sendFeatureReport(const QByteArray& reportData, unsigned int reportID);

// getInputReport receives an input report on request.
// This can be used on startup to initialize the knob positions in Mixxx
Expand All @@ -55,7 +55,7 @@ class HidController final : public Controller {
// as in the polling functionality (including ReportID in first byte).
// The returned list can be used to call the incomingData
// function of the common-hid-packet-parser.
QList<int> getInputReport(unsigned int reportID);
QByteArray getInputReport(unsigned int reportID);

// getFeatureReport receives a feature reports on request.
// HID doesn't support polling feature reports, therefore this is the
Expand All @@ -64,7 +64,7 @@ class HidController final : public Controller {
// changing the other bits. The returned list matches the input
// format of sendFeatureReport, allowing it to be read, modified
// and sent it back to the controller.
QList<int> getFeatureReport(unsigned int reportID);
QByteArray getFeatureReport(unsigned int reportID);

const mixxx::hid::DeviceInfo m_deviceInfo;

Expand Down Expand Up @@ -96,17 +96,17 @@ class HidControllerJSProxy : public ControllerJSProxy {
m_pHidController->sendReport(data, length, reportID);
}

Q_INVOKABLE QList<int> getInputReport(
Q_INVOKABLE QByteArray getInputReport(
unsigned int reportID) {
return m_pHidController->getInputReport(reportID);
}

Q_INVOKABLE void sendFeatureReport(
const QList<int>& dataList, unsigned int reportID) {
m_pHidController->sendFeatureReport(dataList, reportID);
const QByteArray& reportData, unsigned int reportID) {
m_pHidController->sendFeatureReport(reportData, reportID);
}

Q_INVOKABLE QList<int> getFeatureReport(
Q_INVOKABLE QByteArray getFeatureReport(
unsigned int reportID) {
return m_pHidController->getFeatureReport(reportID);
}
Expand Down