Re-implemented USB device enumeration with new structure

This commit is contained in:
2023-08-26 13:16:09 +02:00
parent 61e2352a3c
commit 779911e97e
8 changed files with 76 additions and 98 deletions

View File

@@ -1,76 +0,0 @@
#include "P700Driver.hpp"
#include <spdlog/spdlog.h>
#include <iostream>
#include <stdexcept>
namespace ptprnt::driver {
P700Driver::P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor, uint16_t usbProductId)
: mUsbDriver{std::move(usbDriver)},
mUsbDev{.vendorId = usbDevVendor, .productId = usbProductId}
{
spdlog::debug("P700Driver constructing");
if (!init()) {
spdlog::error("Could not find any P700 Printers.");
} else {
spdlog::info("Found P700 Printer :-).");
}
}
P700Driver::~P700Driver() {
spdlog::debug("P700Driver destructing");
}
driver::info P700Driver::getInfo() {
return mInfo;
}
bool P700Driver::open() {
auto maybeUsbDev = mUsbDriver->open(mUsbDev);
if (!maybeUsbDev.has_value()) {
spdlog::error("Can't open P700 printer");
return false;
}
mUsbDev = maybeUsbDev.value();
return true;
}
bool P700Driver::close() {
if (!mUsbDriver->close(mUsbDev)) {
return false;
}
return true;
}
bool P700Driver::command() {
return false;
}
bool P700Driver::init() {
auto devs = mUsbDriver->getDevices();
if (!devs.has_value()) {
spdlog::error("No USB devices found");
return false;
}
// TODO: There is the possibility, that two printers with the same ID are connected
// We have to take that into account
auto devIt = std::find_if(devs.value().begin(), devs.value().end(), [=](auto dev) {
return mUsbDev.vendorId == dev.vendorId && mUsbDev.productId == dev.productId;
});
if (devIt == devs.value().end()) {
spdlog::error("No device with {0:04X}:{1:04X}", mUsbDev.vendorId, mUsbDev.productId);
return false;
}
mUsbDev = *devIt;
return true;
}
} // namespace ptprnt::driver

View File

@@ -1,5 +1,7 @@
#pragma once
#include <memory>
#include "libusb.h"
namespace libusbwrap {

View File

@@ -1,35 +1,52 @@
#include "libusbwrap/UsbDevice.hpp"
#include <stdexcept>
#include "libusb.h"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
UsbDevice::UsbDevice() {}
UsbDevice::~UsbDevice() {}
Error UsbDevice::open() {
return Error::SUCCESS;
UsbDevice::UsbDevice(libusb_context* ctx, libusb_device* dev) : mLibusbCtx(ctx), mLibusbDev(dev) {
if (mLibusbCtx == nullptr || mLibusbDev == nullptr) {
throw std::invalid_argument("ctx or device are nullptr");
}
void UsbDevice::close() {}
libusb_get_device_descriptor(dev, &mLibusbDevDesc);
}
UsbDevice::~UsbDevice() {
// only free the device, not the context, which is shared between every device
// the UsbDeviceFactory will take care of that
if (mIsOpen) {
close();
}
}
Error UsbDevice::open() {
return static_cast<Error>(libusb_open(mLibusbDev, &mLibusbDevHandle));
}
void UsbDevice::close() {
libusb_close(mLibusbDevHandle);
}
const uint16_t UsbDevice::getVid() {
return 0x0000;
return mLibusbDevDesc.idVendor;
}
const uint16_t UsbDevice::getPid() {
return 0x0000;
return mLibusbDevDesc.idProduct;
}
const device::Speed UsbDevice::getSpeed() {
return device::Speed::FULL;
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev));
}
const uint8_t UsbDevice::getBusNumber() {
return 0;
return libusb_get_bus_number(mLibusbDev);
}
const uint8_t UsbDevice::getPortNumber() {
return 0;
return libusb_get_port_number(mLibusbDev);
}
} // namespace libusbwrap

View File

@@ -1,11 +1,15 @@
#pragma once
#include <atomic>
#include <cstddef>
#include "libusb.h"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
class UsbDevice : public IUsbDevice {
public:
UsbDevice();
UsbDevice(libusb_context* ctx, libusb_device* dev);
~UsbDevice();
// delete copy ctor and assignment
@@ -21,5 +25,12 @@ class UsbDevice : public IUsbDevice {
const device::Speed getSpeed() override;
const uint8_t getBusNumber() override;
const uint8_t getPortNumber() override;
private:
libusb_context* mLibusbCtx{nullptr};
libusb_device* mLibusbDev{nullptr};
libusb_device_handle* mLibusbDevHandle{nullptr};
libusb_device_descriptor mLibusbDevDesc;
std::atomic<bool> mIsOpen = false;
};
} // namespace libusbwrap

View File

@@ -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);

View File

@@ -32,6 +32,9 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
private:
// methods
int refreshDeviceList();
std::vector<std::shared_ptr<IUsbDevice>> buildMaskedDeviceVector(uint16_t vidMask,
uint16_t pidMask, uint16_t vid,
uint16_t pid);
// members
libusb_context* mLibusbCtx{nullptr};
libusb_device** mLibusbDeviceList;

View File

@@ -3,6 +3,7 @@
#include <sys/types.h>
#include <cstdint>
#include <optional>
#include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
@@ -25,10 +26,10 @@ class IUsbDevice {
virtual void close() = 0;
// getters
const virtual uint16_t getVid() = 0;
const virtual uint16_t getPid() = 0;
const virtual device::Speed getSpeed() = 0;
const virtual uint8_t getBusNumber() = 0;
const virtual uint8_t getPortNumber() = 0;
virtual const uint16_t getVid() = 0;
virtual const uint16_t getPid() = 0;
virtual const device::Speed getSpeed() = 0;
virtual const uint8_t getBusNumber() = 0;
virtual const uint8_t getPortNumber() = 0;
};
} // namespace libusbwrap

View File

@@ -25,9 +25,6 @@ int main(int argc, char** argv) {
auto usbDevs = usbFactory.findAllDevices();
for (auto usbDev : usbDevs) {
auto vid = usbDev->getVid();
auto pid = usbDev->getPid();
spdlog::info("UsbDev {}:{}", vid, pid);
auto foundPrinterIt =
std::find_if(compatiblePrinters.begin(), compatiblePrinters.end(),
[usbDev](const std::shared_ptr<ptprnt::IPrinterInfo>& printer) {