Added Usb Detection using libUsb
This commit is contained in:
23
src/Usb.cpp
23
src/Usb.cpp
@@ -2,36 +2,45 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <optional>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace ptprnt::driver {
|
||||
|
||||
Usb::Usb() {
|
||||
std::cout << "Usb starting" << std::endl;
|
||||
spdlog::debug("Usb constructing");
|
||||
libusb_init(NULL);
|
||||
}
|
||||
Usb::~Usb() {
|
||||
std::cout << "Usb stopping" << std::endl;
|
||||
spdlog::debug("Usb destructing");
|
||||
}
|
||||
|
||||
std::vector<UsbDevice> Usb::listDevices() {
|
||||
std::optional<std::vector<UsbDevice>> Usb::getDevices() {
|
||||
libusb_device ** devs;
|
||||
libusb_device *dev;
|
||||
struct libusb_device_descriptor desc;
|
||||
int r,i=0;
|
||||
|
||||
mDevices.clear();
|
||||
|
||||
libusb_get_device_list(NULL, &devs);
|
||||
|
||||
while ((dev=devs[i++]) != NULL) {
|
||||
UsbDevice newDev;
|
||||
|
||||
if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
|
||||
std::cerr << "failed to open device" << std::endl;
|
||||
spdlog::error("failed to open device");
|
||||
libusb_free_device_list(devs, 1);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::cout << std::hex << std::setw(4) << desc.idVendor << ":" << desc.idProduct;
|
||||
std::cout << std::endl;
|
||||
newDev.vendorId = desc.idVendor;
|
||||
newDev.productId = desc.idProduct;
|
||||
|
||||
mDevices.push_back(newDev);
|
||||
}
|
||||
|
||||
return std::vector<UsbDevice>();
|
||||
return mDevices;
|
||||
}
|
||||
|
||||
} // namespace ptprnt::driver
|
Reference in New Issue
Block a user