diff --git a/.clang-format b/.clang-format index de67ec2..a043404 100644 --- a/.clang-format +++ b/.clang-format @@ -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 diff --git a/inc/libusbwrap/LibUsbTypes.hpp b/inc/libusbwrap/LibUsbTypes.hpp new file mode 100644 index 0000000..a1ff0ff --- /dev/null +++ b/inc/libusbwrap/LibUsbTypes.hpp @@ -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 +}; +} \ No newline at end of file diff --git a/inc/libusbwrap/UsbDeviceFactory.hpp b/inc/libusbwrap/UsbDeviceFactory.hpp new file mode 100644 index 0000000..678e29c --- /dev/null +++ b/inc/libusbwrap/UsbDeviceFactory.hpp @@ -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> Vector of all detected USB devices + */ + std::vector> 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> Vector of detected USB devices based on vid/pid + */ + std::vector> findDevices(uint16_t vid, uint16_t pid) override; + + private: + // methods + int refreshDeviceList(); + // members + libusb_context* mLibusbCtx{nullptr}; + libusb_device** mLibusbDeviceList; +}; +} // namespace libusbwrap \ No newline at end of file diff --git a/inc/libusbwrap/interface/IUsbDevice.hpp b/inc/libusbwrap/interface/IUsbDevice.hpp new file mode 100644 index 0000000..0deb8af --- /dev/null +++ b/inc/libusbwrap/interface/IUsbDevice.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include + +#include + +#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 \ No newline at end of file diff --git a/inc/libusbwrap/interface/IUsbDeviceFactory.hpp b/inc/libusbwrap/interface/IUsbDeviceFactory.hpp new file mode 100644 index 0000000..5e11ee6 --- /dev/null +++ b/inc/libusbwrap/interface/IUsbDeviceFactory.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include + +#include "libusbwrap/interface/IUsbDevice.hpp" + +namespace libusbwrap { +class IUsbDeviceFactory { + public: + virtual std::vector> findAllDevices() = 0; + virtual std::vector> findDevices(uint16_t vid, uint16_t pid) = 0; +}; +} // namespace libusbwrap \ No newline at end of file diff --git a/meson.build b/meson.build index 55f448e..be43404 100644 --- a/meson.build +++ b/meson.build @@ -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( diff --git a/src/Usb.cpp b/src/Usb.cpp index 0cc454e..af4fff1 100644 --- a/src/Usb.cpp +++ b/src/Usb.cpp @@ -8,6 +8,7 @@ #include #include "UsbTypes.hpp" +#include "libusb.h" namespace ptprnt::driver { @@ -44,7 +45,7 @@ std::optional> Usb::getDevices() { .hndl = nullptr}; // handle is only available after we opened the dev mDevices.push_back(newDev); - } + } return mDevices; } diff --git a/src/UsbDeviceFactory.cpp b/src/UsbDeviceFactory.cpp new file mode 100644 index 0000000..5e17612 --- /dev/null +++ b/src/UsbDeviceFactory.cpp @@ -0,0 +1,51 @@ + +#include "libusbwrap/UsbDeviceFactory.hpp" + +#include + +#include + +#include "libusb.h" +#include "libusbwrap/interface/IUsbDevice.hpp" + +namespace libusbwrap { +UsbDeviceFactory::UsbDeviceFactory() {} + +UsbDeviceFactory::~UsbDeviceFactory() { + libusb_free_device_list(mLibusbDeviceList, 1); +} + +std::vector> UsbDeviceFactory::findAllDevices() { + refreshDeviceList(); + return std::vector>(); +} + +std::vector> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) { + refreshDeviceList(); + return std::vector>(); +} + +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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f876ea4..0123f56 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,10 @@ #include -#include #include -#include "IPrinter.hpp" -#include "P700Printer.hpp" -#include "Usb.hpp" +#include "libusbwrap/UsbDeviceFactory.hpp" #include -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(); - auto maybeDevs = usb->getDevices(); - if (!maybeDevs.has_value()) { - spdlog::error("No USB devices found"); - return -1; - } - - auto driver = std::make_shared(usb); - auto printer = std::make_shared(std::move(driver)); - //printer::info info = printer->getInfo(); + auto usbFactory = libusbwrap::UsbDeviceFactory(); + usbFactory.init(); + usbFactory.findAllDevices(); return 0; }