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

@@ -41,7 +41,7 @@ BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 120
ColumnLimit: 100
CompactNamespaces: false
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true

View File

@@ -1,6 +1,5 @@
{
"clangd.arguments": ["-background-index", "-compile-commands-dir=builddir"],
"C_Cpp.codeAnalysis.clangTidy.args": ["-extra-arg=-std=c++17"],
"clangd.arguments": ["-background-index", "-compile-commands-dir=builddir/"],
"editor.formatOnType": false,
"editor.formatOnSave": true,
"files.associations": {
@@ -84,5 +83,7 @@
"charconv": "cpp",
"*.ipp": "cpp"
},
"clang-tidy.buildPath": "builddir/"
"clang-tidy.buildPath": "builddir/",
"clangd.onConfigChanged": "restart",
"C_Cpp.default.compileCommands": "builddir/compile_commands.json"
}

View File

@@ -15,6 +15,7 @@ class Usb : public IUsb {
bool close(UsbDevice) override;
private:
libusb_device** mLibUsbDevs;
// TODO: This should be a std::map with handles & locking on access
std::vector<UsbDevice> mDevices{};
};

View File

@@ -1,3 +1,4 @@
#include <memory>
#include <libusb-1.0/libusb.h>
@@ -5,6 +6,14 @@
namespace ptprnt::driver {
struct libusb_device_list_ptr_deleter {
void operator()(libusb_device** usbdevicelistptr) {
libusb_free_device_list(usbdevicelistptr, 1);
}
};
typedef std::unique_ptr<libusb_device**, libusb_device_list_ptr_deleter> libusb_device_list_ptr;
struct UsbDevice {
uint16_t vendorId{0};
uint16_t productId{0};

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;