2023-09-04 12:21:37 +02:00
|
|
|
/* Helpers for USB node-types.
|
2021-06-18 11:27:02 -04:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2021-06-18 11:27:02 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
|
|
|
|
#include <libusb-1.0/libusb.h>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace usb {
|
|
|
|
|
|
|
|
struct Filter {
|
2023-09-07 11:46:39 +02:00
|
|
|
int bus;
|
|
|
|
int port;
|
|
|
|
int vendor_id;
|
|
|
|
int product_id;
|
2021-06-18 11:27:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class Error : public std::runtime_error {
|
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
enum libusb_error err;
|
|
|
|
char *msg;
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
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; }
|
2021-06-18 11:27:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class Device {
|
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
struct libusb_device *device;
|
|
|
|
struct libusb_device_handle *handle;
|
|
|
|
struct libusb_device_descriptor desc;
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string getStringDescriptor(uint8_t desc_id) const;
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
Device(struct libusb_device *dev) : device(dev), handle(nullptr) {}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
const struct libusb_device_descriptor &getDescriptor() const { return desc; }
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int getBus() const { return libusb_get_bus_number(device); }
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int getPort() const { return libusb_get_port_number(device); }
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string getManufacturer() const {
|
|
|
|
return getStringDescriptor(desc.iManufacturer);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string getProduct() const { return getStringDescriptor(desc.iProduct); }
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string getSerial() const {
|
|
|
|
return getStringDescriptor(desc.iSerialNumber);
|
|
|
|
}
|
2021-06-18 11:27:02 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
bool match(const Filter *flt) const;
|
2021-06-18 11:27:02 -04:00
|
|
|
};
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
struct libusb_context *init();
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
void deinit_context(struct libusb_context *ctx);
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
struct libusb_context *get_context();
|
2021-06-18 11:27:02 -04:00
|
|
|
|
|
|
|
void detach(struct libusb_device_handle *hdl, int iface);
|
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace usb
|
|
|
|
} // namespace villas
|