Added UsbInterface class to access non-HID USB interfaces

This commit is contained in:
Nav
2023-11-18 22:53:06 +00:00
parent e271590f70
commit 68f1ba35ff
3 changed files with 165 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
#pragma once
#include <cstdint>
#include <vector>
#include <optional>
#include <chrono>
#include <libusb-1.0/libusb.h>
namespace Usb
{
/**
* The UsbInterface provides access to a particular USB interface.
*/
class UsbInterface
{
public:
std::uint8_t interfaceNumber = 0;
UsbInterface(
std::uint8_t interfaceNumber,
::libusb_device_handle* deviceHandle
);
UsbInterface(const UsbInterface& other) = delete;
UsbInterface& operator = (const UsbInterface& other) = delete;
UsbInterface(UsbInterface&& other) = default;
UsbInterface& operator = (UsbInterface&& other) = default;
/**
* Attempts to claim the interface
*/
void init();
/**
* Releases the claimed interface
*/
void close();
std::vector<unsigned char> readBulk(
std::uint8_t endpointAddress,
std::optional<std::chrono::milliseconds> timeout = std::nullopt
);
void writeBulk(std::uint8_t endpointAddress, std::vector<unsigned char>&& buffer);
private:
::libusb_device_handle* deviceHandle;
bool claimed = false;
};
}