Re-implemented USB device enumeration with new structure
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "libusb.h"
|
||||
#include "libusbwrap/UsbDevice.hpp"
|
||||
@@ -18,12 +19,12 @@ UsbDeviceFactory::~UsbDeviceFactory() {
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||
refreshDeviceList();
|
||||
return std::vector<std::shared_ptr<IUsbDevice>>({std::make_shared<UsbDevice>()});
|
||||
return buildMaskedDeviceVector(0x0, 0x0, 0x0, 0x0);
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
||||
refreshDeviceList();
|
||||
return std::vector<std::shared_ptr<IUsbDevice>>({std::make_shared<UsbDevice>()});
|
||||
return buildMaskedDeviceVector(0xffff, 0xffff, vid, pid);
|
||||
}
|
||||
|
||||
int UsbDeviceFactory::refreshDeviceList() {
|
||||
@@ -39,6 +40,28 @@ int UsbDeviceFactory::refreshDeviceList() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
||||
uint16_t pidMask,
|
||||
uint16_t vid,
|
||||
uint16_t pid) {
|
||||
std::vector<std::shared_ptr<IUsbDevice>> matchedDevices;
|
||||
// see libusb/examples/listdevs.c
|
||||
int i = 0;
|
||||
libusb_device* currDev = nullptr;
|
||||
while ((currDev = mLibusbDeviceList[i++]) != nullptr) {
|
||||
struct libusb_device_descriptor currDevDesc;
|
||||
int ret = libusb_get_device_descriptor(currDev, &currDevDesc);
|
||||
if (ret < 0) {
|
||||
continue;
|
||||
}
|
||||
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
||||
((currDevDesc.idProduct & pidMask) == pid)) {
|
||||
matchedDevices.push_back(std::make_shared<UsbDevice>(mLibusbCtx, currDev));
|
||||
}
|
||||
}
|
||||
return matchedDevices;
|
||||
}
|
||||
|
||||
bool UsbDeviceFactory::init() {
|
||||
auto err = libusb_init(&mLibusbCtx);
|
||||
|
||||
|
Reference in New Issue
Block a user