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

@@ -13,7 +13,7 @@ AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Inline AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None AlwaysBreakAfterReturnType: None
@@ -41,11 +41,11 @@ BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon BreakInheritanceList: BeforeColon
ColumnLimit: 120 ColumnLimit: 100
CompactNamespaces: false CompactNamespaces: false
ContinuationIndentWidth: 4 ContinuationIndentWidth: 4
Cpp11BracedListStyle: true Cpp11BracedListStyle: true
DerivePointerAlignment: false DerivePointerAlignment: false
EmptyLineBeforeAccessModifier: LogicalBlock EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true FixNamespaceComments: true
IndentCaseLabels: true IndentCaseLabels: true
@@ -58,7 +58,7 @@ ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left PointerAlignment: Left
ReflowComments: false ReflowComments: false
SeparateDefinitionBlocks: Always SeparateDefinitionBlocks: Always
SpaceAfterCStyleCast: false SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true SpaceAfterTemplateKeyword: true
@@ -85,11 +85,11 @@ IncludeBlocks: Regroup
IncludeCategories: IncludeCategories:
# Classes own header is always priority 0 # Classes own header is always priority 0
# System C/C++ headers in <> with extension. # System C/C++ headers in <> with extension.
- Regex: '<([A-Za-z0-9.\Q/-_\E])+.h.*>' - Regex: '<([A-Za-z0-9.\Q/-_\E])+.h.*>'
Priority: 1 Priority: 1
# System Headers in <> without extension. # System Headers in <> without extension.
- Regex: '<([A-Za-z0-9.\Q/-_\E])+>' - Regex: '<([A-Za-z0-9.\Q/-_\E])+>'
Priority: 2 Priority: 2
# c Headers in "" with extension. # c Headers in "" with extension.
- Regex: '"([A-Za-z0-9.\Q/-_\E])+"' - Regex: '"([A-Za-z0-9.\Q/-_\E])+"'
Priority: 3 Priority: 3

View File

@@ -1,15 +1,15 @@
{ {
"configurations": [ "configurations": [
{ {
"name": "Linux", "name": "Linux",
"compilerPath": "/usr/bin/clang", "compilerPath": "/usr/bin/clang",
"cStandard": "c11", "cStandard": "c11",
"cppStandard": "c++17", "cppStandard": "c++17",
"compileCommands": "${workspaceFolder}/builddir/compile_commands.json", "compileCommands": "${workspaceFolder}/builddir/compile_commands.json",
"browse": { "browse": {
"path": ["${workspaceFolder}"] "path": ["${workspaceFolder}"]
}
} }
], }
"version": 4 ],
} "version": 4
}

48
.vscode/launch.json vendored
View File

@@ -1,27 +1,27 @@
{ {
// Verwendet IntelliSense zum Ermitteln möglicher Attribute. // Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
"name": "(gdb) Starten",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/builddir/ptprnt",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{ {
"name": "(gdb) Starten", "description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"type": "cppdbg", "text": "-enable-pretty-printing",
"request": "launch", "ignoreFailures": true
"program": "${workspaceFolder}/builddir/ptprnt",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
} }
] ]
} }
]
}

View File

@@ -1,6 +1,5 @@
{ {
"clangd.arguments": ["-background-index", "-compile-commands-dir=builddir"], "clangd.arguments": ["-background-index", "-compile-commands-dir=builddir/"],
"C_Cpp.codeAnalysis.clangTidy.args": ["-extra-arg=-std=c++17"],
"editor.formatOnType": false, "editor.formatOnType": false,
"editor.formatOnSave": true, "editor.formatOnSave": true,
"files.associations": { "files.associations": {
@@ -84,5 +83,7 @@
"charconv": "cpp", "charconv": "cpp",
"*.ipp": "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; bool close(UsbDevice) override;
private: private:
libusb_device** mLibUsbDevs;
// TODO: This should be a std::map with handles & locking on access // TODO: This should be a std::map with handles & locking on access
std::vector<UsbDevice> mDevices{}; std::vector<UsbDevice> mDevices{};
}; };

View File

@@ -1,3 +1,4 @@
#include <memory>
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
@@ -5,6 +6,14 @@
namespace ptprnt::driver { 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 { struct UsbDevice {
uint16_t vendorId{0}; uint16_t vendorId{0};
uint16_t productId{0}; uint16_t productId{0};

View File

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

View File

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