2021-06-18 11:27:02 -04:00
|
|
|
/** Helpers for USB node-types
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <villas/exceptions.hpp>
|
2021-07-09 19:10:37 +02:00
|
|
|
#include <villas/log.hpp>
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2021-07-09 19:10:37 +02:00
|
|
|
Logger logger;
|
|
|
|
|
2021-06-18 11:27:02 -04:00
|
|
|
public:
|
|
|
|
Device(struct libusb_device *dev) :
|
|
|
|
device(dev),
|
|
|
|
handle(nullptr)
|
2021-07-09 19:10:37 +02:00
|
|
|
{
|
|
|
|
int ret = libusb_get_device_descriptor(dev, &desc);
|
|
|
|
if (ret != LIBUSB_SUCCESS)
|
|
|
|
throw Error((enum libusb_error) ret, "Failed to get device descriptor");
|
|
|
|
|
|
|
|
auto ln = fmt::format("usb:dev:{}:{}", libusb_get_bus_number(device), libusb_get_port_number(device));
|
|
|
|
logger = logging.get(ln);
|
|
|
|
|
|
|
|
ret = libusb_open(device, &handle);
|
|
|
|
if (ret < 0)
|
|
|
|
throw Error((enum libusb_error) ret, "Failed to open device");
|
|
|
|
|
|
|
|
libusb_ref_device(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Device()
|
|
|
|
{
|
|
|
|
libusb_close(handle);
|
|
|
|
|
|
|
|
libusb_unref_device(device);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
const struct libusb_device_descriptor & getDescriptor() const
|
2021-07-09 19:07:55 +02:00
|
|
|
{
|
|
|
|
return desc;
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
int getBus() const
|
2021-07-09 19:07:55 +02:00
|
|
|
{
|
|
|
|
return libusb_get_bus_number(device);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
int getPort() const
|
2021-07-09 19:07:55 +02:00
|
|
|
{
|
|
|
|
return libusb_get_port_number(device);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
std::string getManufacturer() const
|
2021-07-09 19:07:55 +02:00
|
|
|
{
|
|
|
|
return getStringDescriptor(desc.iManufacturer);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
std::string getProduct() const
|
2021-07-09 19:07:55 +02:00
|
|
|
{
|
|
|
|
return getStringDescriptor(desc.iProduct);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
std::string getSerial() const
|
2021-07-09 19:07:55 +02:00
|
|
|
{
|
|
|
|
return getStringDescriptor(desc.iSerialNumber);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
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 */
|