Moved wMaxPacketSize retrieval to Usb::UsbDevice class.

And some other bits of tidying
This commit is contained in:
Nav
2023-12-02 19:31:20 +00:00
parent eab1688b1a
commit 084eef1a30
7 changed files with 93 additions and 48 deletions

View File

@@ -4,6 +4,8 @@
#include <array>
#include "src/Logger/Logger.hpp"
#include "src/Services/StringService.hpp"
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
#include "src/TargetController/Exceptions/DeviceCommunicationFailure.hpp"
#include "src/TargetController/Exceptions/DeviceNotFound.hpp"
@@ -100,6 +102,53 @@ namespace Usb
}
}
std::uint8_t UsbDevice::getFirstEndpointAddress(
std::uint8_t interfaceNumber,
::libusb_endpoint_direction direction
) {
const auto activeConfigDescriptor = this->getConfigDescriptor();
for (auto interfaceIndex = 0; interfaceIndex < activeConfigDescriptor->bNumInterfaces; ++interfaceIndex) {
const auto* interfaceDescriptor = (activeConfigDescriptor->interface + interfaceIndex)->altsetting;
if (interfaceDescriptor->bInterfaceNumber != interfaceNumber) {
continue;
}
for (auto endpointIndex = 0; endpointIndex < interfaceDescriptor->bNumEndpoints; ++endpointIndex) {
const auto* endpointDescriptor = (interfaceDescriptor->endpoint + endpointIndex);
if ((endpointDescriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) != direction) {
return endpointDescriptor->bEndpointAddress;
}
}
}
throw DeviceInitializationFailure("Failed to obtain address of USB endpoint");
}
std::uint16_t UsbDevice::getEndpointMaxPacketSize(std::uint8_t endpointAddress) {
const auto activeConfigDescriptor = this->getConfigDescriptor();
for (auto interfaceIndex = 0; interfaceIndex < activeConfigDescriptor->bNumInterfaces; ++interfaceIndex) {
const auto* interfaceDescriptor = (activeConfigDescriptor->interface + interfaceIndex)->altsetting;
for (auto endpointIndex = 0; endpointIndex < interfaceDescriptor->bNumEndpoints; ++endpointIndex) {
const auto* endpointDescriptor = (interfaceDescriptor->endpoint + endpointIndex);
if (endpointDescriptor->bEndpointAddress == endpointAddress) {
return endpointDescriptor->wMaxPacketSize;
}
}
}
throw DeviceInitializationFailure(
"Failed to obtain maximum packet size of USB endpoint (address: 0x"
+ Services::StringService::toHex(endpointAddress) + "). Endpoint not found. Selected configuration "
"value (" + std::to_string(activeConfigDescriptor->bConfigurationValue) + ")"
);
}
std::vector<LibusbDevice> UsbDevice::findMatchingDevices(std::uint16_t vendorId, std::uint16_t productId) {
::libusb_device** devices = nullptr;
::libusb_device* device;

View File

@@ -38,6 +38,22 @@ namespace Usb
*/
std::string getSerialNumber() const;
/**
* Obtains the address of the first endpoint within a given interface and of a particular direction.
*
* @param endpointAddress
* @return
*/
std::uint8_t getFirstEndpointAddress(std::uint8_t interfaceNumber, ::libusb_endpoint_direction direction);
/**
* Obtains the maximum packet size of an endpoint.
*
* @param endpointAddress
* @return
*/
std::uint16_t getEndpointMaxPacketSize(std::uint8_t endpointAddress);
/**
* Selects a specific configuration on the device, using the configuration index.
*