restore USB functionality with altered implementation
This commit is contained in:
@@ -5,7 +5,7 @@ Language: Cpp
|
|||||||
BasedOnStyle: Google
|
BasedOnStyle: Google
|
||||||
AccessModifierOffset: -1
|
AccessModifierOffset: -1
|
||||||
AlignAfterOpenBracket: Align
|
AlignAfterOpenBracket: Align
|
||||||
AlignConsecutiveAssignments: None
|
AlignConsecutiveAssignments: Consecutive
|
||||||
AlignOperands: Align
|
AlignOperands: Align
|
||||||
AllowAllArgumentsOnNextLine: true
|
AllowAllArgumentsOnNextLine: true
|
||||||
AllowAllConstructorInitializersOnNextLine: true
|
AllowAllConstructorInitializersOnNextLine: true
|
||||||
@@ -50,6 +50,7 @@ EmptyLineBeforeAccessModifier: LogicalBlock
|
|||||||
FixNamespaceComments: true
|
FixNamespaceComments: true
|
||||||
IndentCaseLabels: true
|
IndentCaseLabels: true
|
||||||
IndentPPDirectives: None
|
IndentPPDirectives: None
|
||||||
|
IndentAccessModifiers: false
|
||||||
IndentWidth: 4
|
IndentWidth: 4
|
||||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
MaxEmptyLinesToKeep: 1
|
MaxEmptyLinesToKeep: 1
|
||||||
|
22
inc/libusbwrap/LibUsbTypes.hpp
Normal file
22
inc/libusbwrap/LibUsbTypes.hpp
Normal 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
|
||||||
|
};
|
||||||
|
}
|
34
inc/libusbwrap/UsbDeviceFactory.hpp
Normal file
34
inc/libusbwrap/UsbDeviceFactory.hpp
Normal 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
|
34
inc/libusbwrap/interface/IUsbDevice.hpp
Normal file
34
inc/libusbwrap/interface/IUsbDevice.hpp
Normal 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
|
15
inc/libusbwrap/interface/IUsbDeviceFactory.hpp
Normal file
15
inc/libusbwrap/interface/IUsbDeviceFactory.hpp
Normal 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
|
@@ -11,9 +11,7 @@ incdir = include_directories('inc')
|
|||||||
|
|
||||||
srcs = [
|
srcs = [
|
||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
'src/P700Driver.cpp',
|
'src/UsbDeviceFactory.cpp'
|
||||||
'src/P700Printer.cpp',
|
|
||||||
'src/Usb.cpp'
|
|
||||||
]
|
]
|
||||||
|
|
||||||
executable(
|
executable(
|
||||||
|
@@ -8,6 +8,7 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
#include "UsbTypes.hpp"
|
#include "UsbTypes.hpp"
|
||||||
|
#include "libusb.h"
|
||||||
|
|
||||||
namespace ptprnt::driver {
|
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
|
.hndl = nullptr}; // handle is only available after we opened the dev
|
||||||
|
|
||||||
mDevices.push_back(newDev);
|
mDevices.push_back(newDev);
|
||||||
}
|
}
|
||||||
|
|
||||||
return mDevices;
|
return mDevices;
|
||||||
}
|
}
|
||||||
|
51
src/UsbDeviceFactory.cpp
Normal file
51
src/UsbDeviceFactory.cpp
Normal 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
|
20
src/main.cpp
20
src/main.cpp
@@ -1,15 +1,10 @@
|
|||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "IPrinter.hpp"
|
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||||
#include "P700Printer.hpp"
|
|
||||||
#include "Usb.hpp"
|
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
using namespace ptprnt;
|
|
||||||
|
|
||||||
void setupLogger() {
|
void setupLogger() {
|
||||||
spdlog::set_level(spdlog::level::debug);
|
spdlog::set_level(spdlog::level::debug);
|
||||||
spdlog::info("Starting ptprnt {}", PROJ_VERSION);
|
spdlog::info("Starting ptprnt {}", PROJ_VERSION);
|
||||||
@@ -18,16 +13,9 @@ void setupLogger() {
|
|||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
setupLogger();
|
setupLogger();
|
||||||
|
|
||||||
auto usb = std::make_shared<driver::Usb>();
|
auto usbFactory = libusbwrap::UsbDeviceFactory();
|
||||||
auto maybeDevs = usb->getDevices();
|
usbFactory.init();
|
||||||
if (!maybeDevs.has_value()) {
|
usbFactory.findAllDevices();
|
||||||
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();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user