2021-04-04 21:04:12 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <optional>
|
|
|
|
|
#include <libusb-1.0/libusb.h>
|
|
|
|
|
|
|
|
|
|
#include "src/DebugToolDrivers/DebugTool.hpp"
|
|
|
|
|
|
|
|
|
|
namespace Bloom::Usb
|
|
|
|
|
{
|
2022-10-01 21:01:37 +01:00
|
|
|
using LibusbContext = std::unique_ptr<::libusb_context, decltype(&::libusb_exit)>;
|
|
|
|
|
using LibusbDevice = std::unique_ptr<::libusb_device, decltype(&::libusb_unref_device)>;
|
|
|
|
|
using LibusbDeviceHandle = std::unique_ptr<::libusb_device_handle, decltype(&::libusb_close)>;
|
2023-01-14 03:03:10 +00:00
|
|
|
using LibusbConfigDescriptor = std::unique_ptr<::libusb_config_descriptor, decltype(&::libusb_free_config_descriptor)>;
|
2022-10-01 16:50:57 +01:00
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
class UsbDevice
|
|
|
|
|
{
|
|
|
|
|
public:
|
2022-10-01 16:50:57 +01:00
|
|
|
std::uint16_t vendorId;
|
|
|
|
|
std::uint16_t productId;
|
2022-01-11 21:12:25 +00:00
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
UsbDevice(std::uint16_t vendorId, std::uint16_t productId);
|
2022-01-11 21:12:25 +00:00
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
UsbDevice(const UsbDevice& other) = delete;
|
|
|
|
|
UsbDevice& operator = (const UsbDevice& other) = delete;
|
2022-01-11 21:12:25 +00:00
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
UsbDevice(UsbDevice&& other) = default;
|
2022-01-11 21:12:25 +00:00
|
|
|
UsbDevice& operator = (UsbDevice&& other) = default;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2021-06-22 23:52:31 +01:00
|
|
|
void init();
|
|
|
|
|
|
2021-04-07 23:30:01 +01:00
|
|
|
/**
|
|
|
|
|
* Selects a specific configuration on the device, using the configuration index.
|
|
|
|
|
*
|
2022-10-02 13:28:27 +01:00
|
|
|
* @param configurationIndex
|
2021-04-07 23:30:01 +01:00
|
|
|
*/
|
2022-10-02 13:28:27 +01:00
|
|
|
virtual void setConfiguration(std::uint8_t configurationIndex);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
virtual ~UsbDevice();
|
|
|
|
|
|
2021-10-06 21:12:31 +01:00
|
|
|
protected:
|
2022-10-01 21:01:37 +01:00
|
|
|
static inline LibusbContext libusbContext = LibusbContext(nullptr, ::libusb_exit);
|
2022-10-01 16:50:57 +01:00
|
|
|
|
2022-10-01 21:01:37 +01:00
|
|
|
LibusbDevice libusbDevice = LibusbDevice(nullptr, ::libusb_unref_device);
|
|
|
|
|
LibusbDeviceHandle libusbDeviceHandle = LibusbDeviceHandle(nullptr, ::libusb_close);
|
2022-10-01 16:50:57 +01:00
|
|
|
|
2022-10-01 21:01:37 +01:00
|
|
|
std::vector<LibusbDevice> findMatchingDevices(std::uint16_t vendorId, std::uint16_t productId);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2023-01-14 03:03:10 +00:00
|
|
|
LibusbConfigDescriptor getConfigDescriptor(std::optional<std::uint8_t> configurationIndex = std::nullopt);
|
|
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
void detachKernelDriverFromInterface(std::uint8_t interfaceNumber);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
|
|
|
|
void close();
|
2021-04-04 21:04:12 +01:00
|
|
|
};
|
|
|
|
|
}
|