Re-structured project

This commit is contained in:
2023-08-26 11:53:44 +02:00
parent c3915336dd
commit 61e2352a3c
25 changed files with 257 additions and 257 deletions

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,35 @@
#include "libusbwrap/UsbDevice.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
UsbDevice::UsbDevice() {}
UsbDevice::~UsbDevice() {}
Error UsbDevice::open() {
return Error::SUCCESS;
}
void UsbDevice::close() {}
const uint16_t UsbDevice::getVid() {
return 0x0000;
}
const uint16_t UsbDevice::getPid() {
return 0x0000;
}
const device::Speed UsbDevice::getSpeed() {
return device::Speed::FULL;
}
const uint8_t UsbDevice::getBusNumber() {
return 0;
}
const uint8_t UsbDevice::getPortNumber() {
return 0;
}
} // namespace libusbwrap

View File

@@ -0,0 +1,25 @@
#pragma once
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
class UsbDevice : public IUsbDevice {
public:
UsbDevice();
~UsbDevice();
// delete copy ctor and assignment
UsbDevice(const UsbDevice&) = delete;
UsbDevice& operator=(UsbDevice&) = delete;
Error open() override;
void close() override;
// getters
const uint16_t getVid() override;
const uint16_t getPid() override;
const device::Speed getSpeed() override;
const uint8_t getBusNumber() override;
const uint8_t getPortNumber() override;
};
} // namespace libusbwrap

View File

@@ -0,0 +1,52 @@
#include "libusbwrap/UsbDeviceFactory.hpp"
#include <spdlog/spdlog.h>
#include <memory>
#include "libusb.h"
#include "libusbwrap/UsbDevice.hpp"
#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::make_shared<UsbDevice>()});
}
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
refreshDeviceList();
return std::vector<std::shared_ptr<IUsbDevice>>({std::make_shared<UsbDevice>()});
}
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

@@ -0,0 +1,39 @@
#pragma once
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
namespace libusbwrap {
class UsbDeviceFactory : public IUsbDeviceFactory {
public:
UsbDeviceFactory();
~UsbDeviceFactory();
// delete copy ctor and assignment
UsbDeviceFactory(const UsbDeviceFactory&) = delete;
UsbDeviceFactory& operator=(UsbDeviceFactory&) = delete;
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
const virtual uint16_t getVid() = 0;
const virtual uint16_t getPid() = 0;
const virtual device::Speed getSpeed() = 0;
const virtual uint8_t getBusNumber() = 0;
const 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