From 7f455af9053894241291f094865aee8808a1d3df Mon Sep 17 00:00:00 2001 From: Marcin Bukat Date: Wed, 4 Oct 2023 10:51:54 +0200 Subject: hwstub lib: Introduce framework for device filtering The rationale behind this was ability to filter usb device by bus numer and device address. This allows to connect with selected device in case there is more then one connected device implementing hwstub interface. For now only USB backend makes use of this but the foundation is generic and can be easily extended to other backends. Change-Id: I618cfdeeb09162d5fa1002db00e40ea17c43e727 --- utils/hwstub/lib/hwstub_usb.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'utils/hwstub/lib/hwstub_usb.cpp') diff --git a/utils/hwstub/lib/hwstub_usb.cpp b/utils/hwstub/lib/hwstub_usb.cpp index e8b8e7bc3d..e6679ff780 100644 --- a/utils/hwstub/lib/hwstub_usb.cpp +++ b/utils/hwstub/lib/hwstub_usb.cpp @@ -41,20 +41,28 @@ context::context(libusb_context *ctx, bool cleanup_ctx) { } +context::context(libusb_context *ctx, bool cleanup_ctx, std::string *error, device_filter_t f) + :m_usb_ctx(ctx), m_cleanup_ctx(cleanup_ctx) +{ + (void)error; + // NOTE: can't use initializer list since this member is from parent class + // and parent's class constructor is NOT called when initializer list is built + device_filter = f; +} + context::~context() { if(m_cleanup_ctx) libusb_exit(m_usb_ctx); } -std::shared_ptr context::create(libusb_context *ctx, bool cleanup_ctx, - std::string *error) +std::shared_ptr context::create(libusb_context *ctx, bool cleanup_ctx, std::string *error, device_filter_t f) { (void) error; if(ctx == nullptr) libusb_init(nullptr); // NOTE: can't use make_shared() because of the protected ctor */ - return std::shared_ptr(new context(ctx, cleanup_ctx)); + return std::shared_ptr(new context(ctx, cleanup_ctx, nullptr, f)); } libusb_context *context::native_context() @@ -81,7 +89,11 @@ error context::fetch_device_list(std::vector& list, void*& ptr) ptr = (void *)usb_list; list.clear(); for(int i = 0; i < ret; i++) - if(device::is_hwstub_dev(usb_list[i])) + /* filter devices by hwstub interface and by other filtering criteria + * if provided + */ + if(device::is_hwstub_dev(usb_list[i]) && + context::device_filter(usb_list[i])) list.push_back(to_ctx_dev(usb_list[i])); return error::SUCCESS; } @@ -231,6 +243,12 @@ uint16_t device::get_pid() return dev_desc.idProduct; } +bool device::is_bus_addr_device(libusb_device *dev, uint8_t bus, uint8_t addr) +{ + return ((libusb_get_bus_number(dev) == bus) && + (libusb_get_device_address(dev) == addr)); +} + /** * USB handle */ -- cgit v1.2.3