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

View File

@@ -3,12 +3,26 @@
#include <iostream>
#include <stdexcept>
#include <spdlog/spdlog.h>
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() {
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

View File

@@ -2,36 +2,45 @@
#include <iostream>
#include <iomanip>
#include <optional>
#include <spdlog/spdlog.h>
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<UsbDevice> Usb::listDevices() {
std::optional<std::vector<UsbDevice>> 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<UsbDevice>();
return mDevices;
}
} // namespace ptprnt::driver

View File

@@ -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<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 printer = std::make_unique<printer::P700Printer>(std::move(driver));
printer::info info = printer->getInfo();*/
auto driver = std::make_unique<driver::P700Driver>(usb);
//auto printer = std::make_unique<printer::P700Printer>(std::move(driver));
//printer::info info = printer->getInfo();
return 0;
}