Re-structured project
This commit is contained in:
52
src/libusbwrap/UsbDeviceFactory.cpp
Normal file
52
src/libusbwrap/UsbDeviceFactory.cpp
Normal 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
|
Reference in New Issue
Block a user