From d7dfdc4739bd3a3ed63f5ca7f501425742ecf937 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Sat, 12 Nov 2022 18:29:04 +0100 Subject: [PATCH] Added Usb Detection using libUsb --- .vscode/settings.json | 162 +++++++++++++++++++++--------------------- inc/IUsb.hpp | 5 +- inc/P700Driver.hpp | 13 +++- inc/Usb.hpp | 9 +-- inc/UsbTypes.hpp | 2 + src/P700Driver.cpp | 40 ++++++++++- src/Usb.cpp | 23 ++++-- src/main.cpp | 23 +++--- 8 files changed, 171 insertions(+), 106 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9e3b11c..256586b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,86 +2,86 @@ "clangd.arguments": [ "-log=verbose", "-pretty", "--background-index", - "--compile-commands-dir=${workspaceFolder}/builddir", - "-std=c++17" + "--compile-commands-dir=${workspaceFolder}/builddir" ], - "files.associations": { - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "array": "cpp", - "atomic": "cpp", - "strstream": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "bitset": "cpp", - "cfenv": "cpp", - "chrono": "cpp", - "codecvt": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdint": "cpp", - "deque": "cpp", - "forward_list": "cpp", - "list": "cpp", - "map": "cpp", - "set": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "unordered_set": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "source_location": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "semaphore": "cpp", - "shared_mutex": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "cinttypes": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "valarray": "cpp", - "variant": "cpp", - "csignal": "cpp", - "any": "cpp", - "regex": "cpp", - "future": "cpp" - } + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "strstream": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cfenv": "cpp", + "chrono": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "source_location": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "valarray": "cpp", + "variant": "cpp", + "csignal": "cpp", + "any": "cpp", + "regex": "cpp", + "future": "cpp", + "charconv": "cpp" + } } \ No newline at end of file diff --git a/inc/IUsb.hpp b/inc/IUsb.hpp index a964425..1c8ca92 100644 --- a/inc/IUsb.hpp +++ b/inc/IUsb.hpp @@ -1,6 +1,7 @@ #include "UsbTypes.hpp" #include +#include #pragma once @@ -8,9 +9,9 @@ namespace ptprnt::driver { class IUsb { public: - virtual ~IUsb(){}; + virtual ~IUsb() = default; - virtual std::vector listDevices() = 0; + virtual std::optional> getDevices() = 0; }; } // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/P700Driver.hpp b/inc/P700Driver.hpp index c5a9723..2c0ed36 100644 --- a/inc/P700Driver.hpp +++ b/inc/P700Driver.hpp @@ -1,20 +1,29 @@ #include "IDriver.hpp" #include +#include + +#include "Usb.hpp" #pragma once namespace ptprnt::driver { class P700Driver : public IDriver { - public: - P700Driver(uint16_t UsbDevVendor = 0x04f9, uint16_t UsbDevId = 0x2061); + public: + P700Driver(std::shared_ptr usbDriver, uint16_t usbDevVendor = 0x04f9, uint16_t usbDevId = 0x2061); ~P700Driver() override; driver::info getInfo() override; bool open() override; bool close() override; bool command() override; + + private: + std::shared_ptr mUsbDriver{0}; + UsbDevice mUsbDev{0}; + + bool init(); }; } // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/Usb.hpp b/inc/Usb.hpp index 9249086..8b72f27 100644 --- a/inc/Usb.hpp +++ b/inc/Usb.hpp @@ -1,4 +1,5 @@ #include "IUsb.hpp" +#include "UsbTypes.hpp" #include @@ -8,13 +9,13 @@ namespace ptprnt::driver { class Usb : public IUsb { public: - Usb(); - ~Usb() override; + Usb(); + ~Usb() override; - std::vector listDevices() override; + std::optional> getDevices() override; private: - + std::vector mDevices{}; }; } // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/UsbTypes.hpp b/inc/UsbTypes.hpp index 6403602..e1e4737 100644 --- a/inc/UsbTypes.hpp +++ b/inc/UsbTypes.hpp @@ -6,6 +6,8 @@ namespace ptprnt::driver { struct UsbDevice { + uint16_t vendorId{0}; + uint16_t productId{0}; libusb_device* dev{nullptr}; libusb_device_handle* hndl{nullptr}; }; diff --git a/src/P700Driver.cpp b/src/P700Driver.cpp index 163b0d6..4d0da59 100644 --- a/src/P700Driver.cpp +++ b/src/P700Driver.cpp @@ -3,12 +3,26 @@ #include #include +#include + namespace ptprnt::driver { -P700Driver::P700Driver(uint16_t UsbDevVendor, uint16_t UsbDevId) {} +P700Driver::P700Driver(std::shared_ptr 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() { @@ -27,4 +41,26 @@ bool P700Driver::command() { return false; } +bool P700Driver::init() { + auto devs = mUsbDriver->getDevices(); + + if(!devs.has_value()) { + spdlog::error("No USB devices found"); + return false; + } + + 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 \ No newline at end of file diff --git a/src/Usb.cpp b/src/Usb.cpp index 645c40c..1adc5fb 100644 --- a/src/Usb.cpp +++ b/src/Usb.cpp @@ -2,36 +2,45 @@ #include #include +#include +#include namespace ptprnt::driver { Usb::Usb() { - std::cout << "Usb starting" << std::endl; + spdlog::debug("Usb constructing"); libusb_init(NULL); } Usb::~Usb() { - std::cout << "Usb stopping" << std::endl; + spdlog::debug("Usb destructing"); } -std::vector Usb::listDevices() { +std::optional> Usb::getDevices() { libusb_device ** devs; libusb_device *dev; struct libusb_device_descriptor desc; int r,i=0; + mDevices.clear(); + libusb_get_device_list(NULL, &devs); while ((dev=devs[i++]) != NULL) { + UsbDevice newDev; + if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) { - std::cerr << "failed to open device" << std::endl; + spdlog::error("failed to open device"); libusb_free_device_list(devs, 1); + return std::nullopt; } - std::cout << std::hex << std::setw(4) << desc.idVendor << ":" << desc.idProduct; - std::cout << std::endl; + newDev.vendorId = desc.idVendor; + newDev.productId = desc.idProduct; + + mDevices.push_back(newDev); } - return std::vector(); + return mDevices; } } // namespace ptprnt::driver \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c3c80e7..fcd7d9b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,21 +10,28 @@ using namespace ptprnt; -int main(int argc, char** argv) { - +void setupLogger() { + spdlog::set_level(spdlog::level::debug); spdlog::info("Starting ptprnt {}", PROJ_VERSION); - auto usb = std::make_unique(); - auto devs = usb->listDevices(); +} - for (auto dev : devs) { +int main(int argc, char** argv) { + setupLogger(); + auto usb = std::make_shared(); + auto maybeDevs = usb->getDevices(); + if(!maybeDevs.has_value()) { + spdlog::error("No USB devices found"); + return -1; } - /*auto driver = std::make_unique(); - auto printer = std::make_unique(std::move(driver)); - printer::info info = printer->getInfo();*/ + auto driver = std::make_unique(usb); + //auto printer = std::make_unique(std::move(driver)); + //printer::info info = printer->getInfo(); return 0; } + +