1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/include/villas/usb.hpp

113 lines
2.3 KiB
C++
Raw Permalink Normal View History

/** Helpers for USB node-types
*
* @file
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
*********************************************************************************/
#pragma once
#include <villas/exceptions.hpp>
#include <libusb-1.0/libusb.h>
namespace villas {
namespace usb {
struct Filter {
int bus;
int port;
int vendor_id;
int product_id;
};
class Error : public std::runtime_error {
protected:
enum libusb_error err;
char *msg;
public:
Error(enum libusb_error e, const std::string &what) :
std::runtime_error(what),
err(e)
{
int ret __attribute__((unused));
ret = asprintf(&msg, "%s: %s", std::runtime_error::what(), libusb_strerror(err));
}
template<typename... Args>
Error(enum libusb_error e, const std::string &what, Args&&... args) :
usb::Error(e, fmt::format(what, std::forward<Args>(args)...))
{ }
/* Same as above but with int */
Error(int e, const std::string &what) :
usb::Error((enum libusb_error) e, what)
{ }
template<typename... Args>
Error(int e, const std::string &what, Args&&... args) :
usb::Error((enum libusb_error) e, what, std::forward<Args>(args)...)
{ }
~Error()
{
if (msg)
free(msg);
}
virtual const char * what() const noexcept
{
return msg;
}
};
class Device {
protected:
struct libusb_device *device;
struct libusb_device_handle *handle;
struct libusb_device_descriptor desc;
std::string getStringDescriptor(uint8_t desc_id) const;
public:
Device(struct libusb_device *dev) :
device(dev),
handle(nullptr)
{ }
const struct libusb_device_descriptor & getDescriptor() const
{ return desc; }
int getBus() const
{ return libusb_get_bus_number(device); }
int getPort() const
{ return libusb_get_port_number(device); }
std::string getManufacturer() const
{ return getStringDescriptor(desc.iManufacturer); }
std::string getProduct() const
{ return getStringDescriptor(desc.iProduct); }
std::string getSerial() const
{ return getStringDescriptor(desc.iSerialNumber); }
bool match(const Filter *flt) const;
};
struct libusb_context * init();
void deinit_context(struct libusb_context *ctx);
struct libusb_context * get_context();
void detach(struct libusb_device_handle *hdl, int iface);
} /* namespace usb */
} /* namespace villas */