From 458806c6af384fa855ba758269babdf5dd80ba7e Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Tue, 1 Aug 2023 18:19:50 +0200 Subject: [PATCH] USB updates, remove iostream usage --- .clang-format | 20 +++++++-------- .vscode/c_cpp_properties.json | 26 +++++++++---------- .vscode/launch.json | 48 +++++++++++++++++------------------ .vscode/settings.json | 7 ++--- inc/Usb.hpp | 1 + inc/UsbTypes.hpp | 9 +++++++ src/Usb.cpp | 29 ++++++++++----------- src/main.cpp | 2 +- 8 files changed, 75 insertions(+), 67 deletions(-) diff --git a/.clang-format b/.clang-format index fddab64..de67ec2 100644 --- a/.clang-format +++ b/.clang-format @@ -13,7 +13,7 @@ AllowAllParametersOfDeclarationOnNextLine: false AllowShortBlocksOnASingleLine: Empty AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: Inline -AllowShortIfStatementsOnASingleLine: Never +AllowShortIfStatementsOnASingleLine: Never AllowShortLambdasOnASingleLine: Inline AllowShortLoopsOnASingleLine: false AlwaysBreakAfterReturnType: None @@ -41,11 +41,11 @@ BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon -ColumnLimit: 120 +ColumnLimit: 100 CompactNamespaces: false ContinuationIndentWidth: 4 Cpp11BracedListStyle: true -DerivePointerAlignment: false +DerivePointerAlignment: false EmptyLineBeforeAccessModifier: LogicalBlock FixNamespaceComments: true IndentCaseLabels: true @@ -58,7 +58,7 @@ ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: true PointerAlignment: Left ReflowComments: false -SeparateDefinitionBlocks: Always +SeparateDefinitionBlocks: Always SpaceAfterCStyleCast: false SpaceAfterLogicalNot: false SpaceAfterTemplateKeyword: true @@ -85,11 +85,11 @@ IncludeBlocks: Regroup IncludeCategories: # Classes own header is always priority 0 # System C/C++ headers in <> with extension. - - Regex: '<([A-Za-z0-9.\Q/-_\E])+.h.*>' - Priority: 1 + - Regex: '<([A-Za-z0-9.\Q/-_\E])+.h.*>' + Priority: 1 # System Headers in <> without extension. - - Regex: '<([A-Za-z0-9.\Q/-_\E])+>' - Priority: 2 + - Regex: '<([A-Za-z0-9.\Q/-_\E])+>' + Priority: 2 # c Headers in "" with extension. - - Regex: '"([A-Za-z0-9.\Q/-_\E])+"' - Priority: 3 + - Regex: '"([A-Za-z0-9.\Q/-_\E])+"' + Priority: 3 diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index a99ab15..84aeb11 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,15 +1,15 @@ { - "configurations": [ - { - "name": "Linux", - "compilerPath": "/usr/bin/clang", - "cStandard": "c11", - "cppStandard": "c++17", - "compileCommands": "${workspaceFolder}/builddir/compile_commands.json", - "browse": { - "path": ["${workspaceFolder}"] - } + "configurations": [ + { + "name": "Linux", + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "compileCommands": "${workspaceFolder}/builddir/compile_commands.json", + "browse": { + "path": ["${workspaceFolder}"] } - ], - "version": 4 -} \ No newline at end of file + } + ], + "version": 4 +} diff --git a/.vscode/launch.json b/.vscode/launch.json index 91bbc21..53ce6ff 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,27 +1,27 @@ { - // Verwendet IntelliSense zum Ermitteln möglicher Attribute. - // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. - // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ + // Verwendet IntelliSense zum Ermitteln möglicher Attribute. + // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. + // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "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", - "type": "cppdbg", - "request": "launch", - "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 - } - ] + "description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren", + "text": "-enable-pretty-printing", + "ignoreFailures": true } - ] -} \ No newline at end of file + ] + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index f79f6ca..914b500 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } diff --git a/inc/Usb.hpp b/inc/Usb.hpp index e8f3709..5c5151c 100644 --- a/inc/Usb.hpp +++ b/inc/Usb.hpp @@ -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 mDevices{}; }; diff --git a/inc/UsbTypes.hpp b/inc/UsbTypes.hpp index 24513fd..e7e28a9 100644 --- a/inc/UsbTypes.hpp +++ b/inc/UsbTypes.hpp @@ -1,3 +1,4 @@ +#include #include @@ -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_list_ptr; + struct UsbDevice { uint16_t vendorId{0}; uint16_t productId{0}; diff --git a/src/Usb.cpp b/src/Usb.cpp index c36de03..0cc454e 100644 --- a/src/Usb.cpp +++ b/src/Usb.cpp @@ -2,10 +2,13 @@ #include +#include #include #include #include +#include "UsbTypes.hpp" + namespace ptprnt::driver { Usb::Usb() { @@ -18,37 +21,31 @@ Usb::~Usb() { } std::optional> 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; } diff --git a/src/main.cpp b/src/main.cpp index 01f21c9..f876ea4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,7 +26,7 @@ int main(int argc, char** argv) { } auto driver = std::make_shared(usb); - auto printer = std::make_unique(std::move(driver)); + auto printer = std::make_shared(std::move(driver)); //printer::info info = printer->getInfo(); return 0;