restore USB functionality with altered implementation

This commit is contained in:
2023-08-03 19:06:21 +02:00
parent 458806c6af
commit c3915336dd
9 changed files with 165 additions and 21 deletions

View File

@@ -8,6 +8,7 @@
#include <optional>
#include "UsbTypes.hpp"
#include "libusb.h"
namespace ptprnt::driver {
@@ -44,7 +45,7 @@ std::optional<std::vector<UsbDevice>> Usb::getDevices() {
.hndl = nullptr}; // handle is only available after we opened the dev
mDevices.push_back(newDev);
}
}
return mDevices;
}

51
src/UsbDeviceFactory.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include "libusbwrap/UsbDeviceFactory.hpp"
#include <spdlog/spdlog.h>
#include <memory>
#include "libusb.h"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
UsbDeviceFactory::UsbDeviceFactory() {}
UsbDeviceFactory::~UsbDeviceFactory() {
libusb_free_device_list(mLibusbDeviceList, 1);
}
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
refreshDeviceList();
return std::vector<std::shared_ptr<IUsbDevice>>();
}
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
refreshDeviceList();
return std::vector<std::shared_ptr<IUsbDevice>>();
}
int UsbDeviceFactory::refreshDeviceList() {
int ret = libusb_get_device_list(mLibusbCtx, &mLibusbDeviceList);
if (ret < 0) {
spdlog::error("Error enumarating USB devices");
} else if (ret == 0) {
spdlog::warn("No USB devices found");
} else {
spdlog::debug("Found {} USB devices", ret);
}
return ret;
}
bool UsbDeviceFactory::init() {
auto err = libusb_init(&mLibusbCtx);
if (err != (int)Error::SUCCESS) {
spdlog::error("Could not intialize libusb");
return false;
}
return true;
}
} // namespace libusbwrap

View File

@@ -1,15 +1,10 @@
#include <spdlog/spdlog.h>
#include <iostream>
#include <memory>
#include "IPrinter.hpp"
#include "P700Printer.hpp"
#include "Usb.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
#include <libusb-1.0/libusb.h>
using namespace ptprnt;
void setupLogger() {
spdlog::set_level(spdlog::level::debug);
spdlog::info("Starting ptprnt {}", PROJ_VERSION);
@@ -18,16 +13,9 @@ void setupLogger() {
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_shared<driver::P700Driver>(usb);
auto printer = std::make_shared<printer::P700Printer>(std::move(driver));
//printer::info info = printer->getInfo();
auto usbFactory = libusbwrap::UsbDeviceFactory();
usbFactory.init();
usbFactory.findAllDevices();
return 0;
}