Added Usb Detection using libUsb

This commit is contained in:
2022-11-12 18:29:04 +01:00
parent 815e67bdfb
commit d7dfdc4739
8 changed files with 171 additions and 106 deletions

162
.vscode/settings.json vendored
View File

@@ -2,86 +2,86 @@
"clangd.arguments": [ "-log=verbose", "clangd.arguments": [ "-log=verbose",
"-pretty", "-pretty",
"--background-index", "--background-index",
"--compile-commands-dir=${workspaceFolder}/builddir", "--compile-commands-dir=${workspaceFolder}/builddir"
"-std=c++17"
], ],
"files.associations": { "files.associations": {
"cctype": "cpp", "cctype": "cpp",
"clocale": "cpp", "clocale": "cpp",
"cmath": "cpp", "cmath": "cpp",
"cstdarg": "cpp", "cstdarg": "cpp",
"cstddef": "cpp", "cstddef": "cpp",
"cstdio": "cpp", "cstdio": "cpp",
"cstdlib": "cpp", "cstdlib": "cpp",
"cstring": "cpp", "cstring": "cpp",
"ctime": "cpp", "ctime": "cpp",
"cwchar": "cpp", "cwchar": "cpp",
"cwctype": "cpp", "cwctype": "cpp",
"array": "cpp", "array": "cpp",
"atomic": "cpp", "atomic": "cpp",
"strstream": "cpp", "strstream": "cpp",
"bit": "cpp", "bit": "cpp",
"*.tcc": "cpp", "*.tcc": "cpp",
"bitset": "cpp", "bitset": "cpp",
"cfenv": "cpp", "cfenv": "cpp",
"chrono": "cpp", "chrono": "cpp",
"codecvt": "cpp", "codecvt": "cpp",
"compare": "cpp", "compare": "cpp",
"complex": "cpp", "complex": "cpp",
"concepts": "cpp", "concepts": "cpp",
"condition_variable": "cpp", "condition_variable": "cpp",
"cstdint": "cpp", "cstdint": "cpp",
"deque": "cpp", "deque": "cpp",
"forward_list": "cpp", "forward_list": "cpp",
"list": "cpp", "list": "cpp",
"map": "cpp", "map": "cpp",
"set": "cpp", "set": "cpp",
"string": "cpp", "string": "cpp",
"unordered_map": "cpp", "unordered_map": "cpp",
"unordered_set": "cpp", "unordered_set": "cpp",
"vector": "cpp", "vector": "cpp",
"exception": "cpp", "exception": "cpp",
"algorithm": "cpp", "algorithm": "cpp",
"functional": "cpp", "functional": "cpp",
"iterator": "cpp", "iterator": "cpp",
"memory": "cpp", "memory": "cpp",
"memory_resource": "cpp", "memory_resource": "cpp",
"numeric": "cpp", "numeric": "cpp",
"optional": "cpp", "optional": "cpp",
"random": "cpp", "random": "cpp",
"ratio": "cpp", "ratio": "cpp",
"source_location": "cpp", "source_location": "cpp",
"string_view": "cpp", "string_view": "cpp",
"system_error": "cpp", "system_error": "cpp",
"tuple": "cpp", "tuple": "cpp",
"type_traits": "cpp", "type_traits": "cpp",
"utility": "cpp", "utility": "cpp",
"fstream": "cpp", "fstream": "cpp",
"initializer_list": "cpp", "initializer_list": "cpp",
"iomanip": "cpp", "iomanip": "cpp",
"iosfwd": "cpp", "iosfwd": "cpp",
"iostream": "cpp", "iostream": "cpp",
"istream": "cpp", "istream": "cpp",
"limits": "cpp", "limits": "cpp",
"mutex": "cpp", "mutex": "cpp",
"new": "cpp", "new": "cpp",
"numbers": "cpp", "numbers": "cpp",
"ostream": "cpp", "ostream": "cpp",
"semaphore": "cpp", "semaphore": "cpp",
"shared_mutex": "cpp", "shared_mutex": "cpp",
"sstream": "cpp", "sstream": "cpp",
"stdexcept": "cpp", "stdexcept": "cpp",
"stop_token": "cpp", "stop_token": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"thread": "cpp", "thread": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeindex": "cpp", "typeindex": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"valarray": "cpp", "valarray": "cpp",
"variant": "cpp", "variant": "cpp",
"csignal": "cpp", "csignal": "cpp",
"any": "cpp", "any": "cpp",
"regex": "cpp", "regex": "cpp",
"future": "cpp" "future": "cpp",
} "charconv": "cpp"
}
} }

View File

@@ -1,6 +1,7 @@
#include "UsbTypes.hpp" #include "UsbTypes.hpp"
#include <vector> #include <vector>
#include <optional>
#pragma once #pragma once
@@ -8,9 +9,9 @@ namespace ptprnt::driver {
class IUsb { class IUsb {
public: public:
virtual ~IUsb(){}; virtual ~IUsb() = default;
virtual std::vector<UsbDevice> listDevices() = 0; virtual std::optional<std::vector<UsbDevice>> getDevices() = 0;
}; };
} // namespace ptprnt::driver } // namespace ptprnt::driver

View File

@@ -1,20 +1,29 @@
#include "IDriver.hpp" #include "IDriver.hpp"
#include <cstdint> #include <cstdint>
#include <memory>
#include "Usb.hpp"
#pragma once #pragma once
namespace ptprnt::driver { namespace ptprnt::driver {
class P700Driver : public IDriver { class P700Driver : public IDriver {
public: public:
P700Driver(uint16_t UsbDevVendor = 0x04f9, uint16_t UsbDevId = 0x2061); P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor = 0x04f9, uint16_t usbDevId = 0x2061);
~P700Driver() override; ~P700Driver() override;
driver::info getInfo() override; driver::info getInfo() override;
bool open() override; bool open() override;
bool close() override; bool close() override;
bool command() override; bool command() override;
private:
std::shared_ptr<Usb> mUsbDriver{0};
UsbDevice mUsbDev{0};
bool init();
}; };
} // namespace ptprnt::driver } // namespace ptprnt::driver

View File

@@ -1,4 +1,5 @@
#include "IUsb.hpp" #include "IUsb.hpp"
#include "UsbTypes.hpp"
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
@@ -8,13 +9,13 @@ namespace ptprnt::driver {
class Usb : public IUsb { class Usb : public IUsb {
public: public:
Usb(); Usb();
~Usb() override; ~Usb() override;
std::vector<UsbDevice> listDevices() override; std::optional<std::vector<UsbDevice>> getDevices() override;
private: private:
std::vector<UsbDevice> mDevices{};
}; };
} // namespace ptprnt::driver } // namespace ptprnt::driver

View File

@@ -6,6 +6,8 @@
namespace ptprnt::driver { namespace ptprnt::driver {
struct UsbDevice { struct UsbDevice {
uint16_t vendorId{0};
uint16_t productId{0};
libusb_device* dev{nullptr}; libusb_device* dev{nullptr};
libusb_device_handle* hndl{nullptr}; libusb_device_handle* hndl{nullptr};
}; };

View File

@@ -3,12 +3,26 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <spdlog/spdlog.h>
namespace ptprnt::driver { namespace ptprnt::driver {
P700Driver::P700Driver(uint16_t UsbDevVendor, uint16_t UsbDevId) {} P700Driver::P700Driver(std::shared_ptr<Usb> 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() { P700Driver::~P700Driver() {
spdlog::debug("P700Driver destructing");
} }
driver::info P700Driver::getInfo() { driver::info P700Driver::getInfo() {
@@ -27,4 +41,26 @@ bool P700Driver::command() {
return false; 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 } // namespace ptprnt::driver

View File

@@ -2,36 +2,45 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <optional>
#include <spdlog/spdlog.h>
namespace ptprnt::driver { namespace ptprnt::driver {
Usb::Usb() { Usb::Usb() {
std::cout << "Usb starting" << std::endl; spdlog::debug("Usb constructing");
libusb_init(NULL); libusb_init(NULL);
} }
Usb::~Usb() { Usb::~Usb() {
std::cout << "Usb stopping" << std::endl; spdlog::debug("Usb destructing");
} }
std::vector<UsbDevice> Usb::listDevices() { std::optional<std::vector<UsbDevice>> Usb::getDevices() {
libusb_device ** devs; libusb_device ** devs;
libusb_device *dev; libusb_device *dev;
struct libusb_device_descriptor desc; struct libusb_device_descriptor desc;
int r,i=0; int r,i=0;
mDevices.clear();
libusb_get_device_list(NULL, &devs); libusb_get_device_list(NULL, &devs);
while ((dev=devs[i++]) != NULL) { while ((dev=devs[i++]) != NULL) {
UsbDevice newDev;
if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) { 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); libusb_free_device_list(devs, 1);
return std::nullopt;
} }
std::cout << std::hex << std::setw(4) << desc.idVendor << ":" << desc.idProduct; newDev.vendorId = desc.idVendor;
std::cout << std::endl; newDev.productId = desc.idProduct;
mDevices.push_back(newDev);
} }
return std::vector<UsbDevice>(); return mDevices;
} }
} // namespace ptprnt::driver } // namespace ptprnt::driver

View File

@@ -10,21 +10,28 @@
using namespace ptprnt; using namespace ptprnt;
int main(int argc, char** argv) { void setupLogger() {
spdlog::set_level(spdlog::level::debug);
spdlog::info("Starting ptprnt {}", PROJ_VERSION); spdlog::info("Starting ptprnt {}", PROJ_VERSION);
auto usb = std::make_unique<driver::Usb>(); }
auto devs = usb->listDevices();
for (auto dev : devs) { int main(int argc, char** argv) {
setupLogger();
auto usb = std::make_shared<driver::Usb>();
auto maybeDevs = usb->getDevices();
if(!maybeDevs.has_value()) {
spdlog::error("No USB devices found");
return -1;
} }
/*auto driver = std::make_unique<driver::P700Driver>(); auto driver = std::make_unique<driver::P700Driver>(usb);
auto printer = std::make_unique<printer::P700Printer>(std::move(driver)); //auto printer = std::make_unique<printer::P700Printer>(std::move(driver));
printer::info info = printer->getInfo();*/ //printer::info info = printer->getInfo();
return 0; return 0;
} }