USB updates, remove iostream usage

This commit is contained in:
2023-08-01 18:19:50 +02:00
parent c3f200fee4
commit 458806c6af
8 changed files with 75 additions and 67 deletions

View File

@@ -2,10 +2,13 @@
#include <spdlog/spdlog.h>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <optional>
#include "UsbTypes.hpp"
namespace ptprnt::driver {
Usb::Usb() {
@@ -18,37 +21,31 @@ Usb::~Usb() {
}
std::optional<std::vector<UsbDevice>> Usb::getDevices() {
// TODO: wrap libUsbDevs in a smart pointer with custom destructor
// calling libusb_free_device_list
libusb_device** libUsbDevs;
libusb_device* libUsbDev;
struct libusb_device_descriptor libUsbDesc;
int ret, i = 0;
mDevices.clear();
if (0 == (ret = libusb_get_device_list(NULL, &libUsbDevs))) {
if (0 == (ret = libusb_get_device_list(NULL, &mLibUsbDevs))) {
spdlog::error("Could not find any USB devices: {}", libusb_error_name(ret));
return std::nullopt;
}
while ((libUsbDev = libUsbDevs[i++]) != NULL) {
UsbDevice newDev;
while ((libUsbDev = mLibUsbDevs[i++]) != NULL) {
if ((ret = libusb_get_device_descriptor(libUsbDev, &libUsbDesc)) < 0) {
spdlog::error("failed to open device");
libusb_free_device_list(libUsbDevs, 1);
spdlog::error("failed to get device");
return std::nullopt;
}
newDev.vendorId = libUsbDesc.idVendor;
newDev.productId = libUsbDesc.idProduct;
newDev.device = libUsbDev;
newDev.hndl = nullptr; // handle is only available after we opened the dev
UsbDevice newDev{.vendorId = libUsbDesc.idVendor,
.productId = libUsbDesc.idProduct,
.device = libUsbDev,
.hndl = nullptr}; // handle is only available after we opened the dev
mDevices.push_back(newDev);
}
libusb_free_device_list(libUsbDevs, 1);
return mDevices;
}
@@ -95,7 +92,7 @@ bool Usb::close(UsbDevice dev) {
dev.vendorId, dev.productId, libusb_error_name(ret));
}
std::cout << "test" << std::endl;
spdlog::debug("test");
libusb_close(dev.hndl);
return true;
}

View File

@@ -26,7 +26,7 @@ int main(int argc, char** argv) {
}
auto driver = std::make_shared<driver::P700Driver>(usb);
auto printer = std::make_unique<printer::P700Printer>(std::move(driver));
auto printer = std::make_shared<printer::P700Printer>(std::move(driver));
//printer::info info = printer->getInfo();
return 0;