Skip to content

Commit

Permalink
usbDevice: Fixed our violating the API contract for `libusb_interrupt…
Browse files Browse the repository at this point in the history
…_transfer()` on FreeBSD because they implement an older version of it
  • Loading branch information
dragonmux authored and esden committed Jan 6, 2024
1 parent df8d79c commit 33192d1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/include/usbDevice.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,19 @@ private:
[[nodiscard]] bool interruptTransfer(const uint8_t endpoint, const void *const bufferPtr,
const int32_t bufferLen, const milliseconds_t timeout) const noexcept
{
// libusb versions prior to v1.0.21 cannot take `nullptr` as their `transfer` parameter.
// FreeBSD's custom libusb implements API version v1.0.13 at time of writing, so we cannot make
// use of this ability to ignore the transfer byte count result.
int bytesTransferred{};
// The const-cast here is required becasue libusb is not const-correct. It is UB, but we cannot avoid it.
const auto result
{
libusb_interrupt_transfer(device, endpoint,
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
const_cast<uint8_t *>(static_cast<const uint8_t *>(bufferPtr)), bufferLen, nullptr,
const_cast<uint8_t *>(static_cast<const uint8_t *>(bufferPtr)), bufferLen, &bytesTransferred,
static_cast<uint32_t>(timeout.count()))
};

// Check for outright failures and report them
if (result && result != LIBUSB_ERROR_TIMEOUT)
{
const auto endpointNumber{uint8_t(endpoint & 0x7FU)};
Expand All @@ -123,6 +127,9 @@ private:
direction == endpointDir_t::controllerIn ? "IN"sv : "OUT"sv,
", reason: "sv, libusb_error_name(result));
}
// Check if we moved less data than we expected, and warn if in debug mode if we did
if (result == 0 && bytesTransferred < bufferLen && console.showDebug())
console.warn("Interrupt transfer received "sv, bytesTransferred, " instead of the expected "sv, bufferLen);
return !result;
}

Expand Down

0 comments on commit 33192d1

Please sign in to comment.