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

@@ -5,7 +5,7 @@ Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignConsecutiveAssignments: Consecutive
AlignOperands: Align
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
@@ -50,6 +50,7 @@ EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IndentCaseLabels: true
IndentPPDirectives: None
IndentAccessModifiers: false
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1

View File

@@ -0,0 +1,22 @@
#pragma once
#include "libusb.h"
namespace libusbwrap {
enum class Error {
SUCCESS = LIBUSB_SUCCESS,
IO = LIBUSB_ERROR_IO,
INVALID_PARAM = LIBUSB_ERROR_INVALID_PARAM,
ACCESS = LIBUSB_ERROR_ACCESS,
NO_DEVICE = LIBUSB_ERROR_NO_DEVICE,
NOT_FOUND = LIBUSB_ERROR_NOT_FOUND,
BUSY = LIBUSB_ERROR_BUSY,
TIMEOUT = LIBUSB_ERROR_TIMEOUT,
OVERFLOW = LIBUSB_ERROR_OVERFLOW,
PIPE = LIBUSB_ERROR_PIPE,
INTERRUPTED = LIBUSB_ERROR_INTERRUPTED,
NO_MEM = LIBUSB_ERROR_NO_MEM,
NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED,
OTHER = LIBUSB_ERROR_OTHER
};
}

View File

@@ -0,0 +1,34 @@
#pragma once
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
namespace libusbwrap {
class UsbDeviceFactory : public IUsbDeviceFactory {
public:
UsbDeviceFactory();
~UsbDeviceFactory();
bool init();
/**
* @brief Gets all devices that are detected by libusb. Will allocate a shared_ptr for each Device
*
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of all detected USB devices
*/
std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() override;
/**
* @brief Gets all devices with certain vid/pid combination.
* If only one device of certain type is connected, vector is usually only one element
*
* @param vid VendorId of the devices to find
* @param pid ProductId of the devices to find
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of detected USB devices based on vid/pid
*/
std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
private:
// methods
int refreshDeviceList();
// members
libusb_context* mLibusbCtx{nullptr};
libusb_device** mLibusbDeviceList;
};
} // namespace libusbwrap

View File

@@ -0,0 +1,34 @@
#pragma once
#include <sys/types.h>
#include <cstdint>
#include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
namespace libusbwrap {
namespace device {
enum class Speed {
UNKNOWN = LIBUSB_SPEED_UNKNOWN,
LOW = LIBUSB_SPEED_LOW,
FULL = LIBUSB_SPEED_FULL,
HIGH = LIBUSB_SPEED_HIGH,
SUPER = LIBUSB_SPEED_SUPER,
SUPER_PLUS = LIBUSB_SPEED_SUPER_PLUS
};
} // namespace device
class IUsbDevice {
public:
virtual Error open() = 0;
virtual void close() = 0;
// getters
virtual uint16_t getVid() = 0;
virtual uint16_t getPid() = 0;
virtual device::Speed getSpeed() = 0;
virtual uint8_t getBusNumber() = 0;
virtual uint8_t getPortNumber() = 0;
};
} // namespace libusbwrap

View File

@@ -0,0 +1,15 @@
#pragma once
#include <cstdint>
#include <memory>
#include <vector>
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
class IUsbDeviceFactory {
public:
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0;
virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
};
} // namespace libusbwrap

View File

@@ -11,9 +11,7 @@ incdir = include_directories('inc')
srcs = [
'src/main.cpp',
'src/P700Driver.cpp',
'src/P700Printer.cpp',
'src/Usb.cpp'
'src/UsbDeviceFactory.cpp'
]
executable(

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;
}