diff options
author | Marcin Bukat <marcin.bukat@gmail.com> | 2023-10-04 10:51:54 +0200 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2023-11-09 02:06:18 -0500 |
commit | 7f455af9053894241291f094865aee8808a1d3df (patch) | |
tree | 6ad266f617473f01e3de2f6e36cb47da10212166 /utils/hwstub/lib/hwstub_usb.cpp | |
parent | 154f10c2d2f32b24e51c1b3a232e2886cc834ff4 (diff) | |
download | rockbox-7f455af9053894241291f094865aee8808a1d3df.tar.gz rockbox-7f455af9053894241291f094865aee8808a1d3df.zip |
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
Diffstat (limited to 'utils/hwstub/lib/hwstub_usb.cpp')
-rw-r--r-- | utils/hwstub/lib/hwstub_usb.cpp | 26 |
1 files changed, 22 insertions, 4 deletions
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) | |||
41 | { | 41 | { |
42 | } | 42 | } |
43 | 43 | ||
44 | context::context(libusb_context *ctx, bool cleanup_ctx, std::string *error, device_filter_t f) | ||
45 | :m_usb_ctx(ctx), m_cleanup_ctx(cleanup_ctx) | ||
46 | { | ||
47 | (void)error; | ||
48 | // NOTE: can't use initializer list since this member is from parent class | ||
49 | // and parent's class constructor is NOT called when initializer list is built | ||
50 | device_filter = f; | ||
51 | } | ||
52 | |||
44 | context::~context() | 53 | context::~context() |
45 | { | 54 | { |
46 | if(m_cleanup_ctx) | 55 | if(m_cleanup_ctx) |
47 | libusb_exit(m_usb_ctx); | 56 | libusb_exit(m_usb_ctx); |
48 | } | 57 | } |
49 | 58 | ||
50 | std::shared_ptr<context> context::create(libusb_context *ctx, bool cleanup_ctx, | 59 | std::shared_ptr<context> context::create(libusb_context *ctx, bool cleanup_ctx, std::string *error, device_filter_t f) |
51 | std::string *error) | ||
52 | { | 60 | { |
53 | (void) error; | 61 | (void) error; |
54 | if(ctx == nullptr) | 62 | if(ctx == nullptr) |
55 | libusb_init(nullptr); | 63 | libusb_init(nullptr); |
56 | // NOTE: can't use make_shared() because of the protected ctor */ | 64 | // NOTE: can't use make_shared() because of the protected ctor */ |
57 | return std::shared_ptr<context>(new context(ctx, cleanup_ctx)); | 65 | return std::shared_ptr<context>(new context(ctx, cleanup_ctx, nullptr, f)); |
58 | } | 66 | } |
59 | 67 | ||
60 | libusb_context *context::native_context() | 68 | libusb_context *context::native_context() |
@@ -81,7 +89,11 @@ error context::fetch_device_list(std::vector<ctx_dev_t>& list, void*& ptr) | |||
81 | ptr = (void *)usb_list; | 89 | ptr = (void *)usb_list; |
82 | list.clear(); | 90 | list.clear(); |
83 | for(int i = 0; i < ret; i++) | 91 | for(int i = 0; i < ret; i++) |
84 | if(device::is_hwstub_dev(usb_list[i])) | 92 | /* filter devices by hwstub interface and by other filtering criteria |
93 | * if provided | ||
94 | */ | ||
95 | if(device::is_hwstub_dev(usb_list[i]) && | ||
96 | context::device_filter(usb_list[i])) | ||
85 | list.push_back(to_ctx_dev(usb_list[i])); | 97 | list.push_back(to_ctx_dev(usb_list[i])); |
86 | return error::SUCCESS; | 98 | return error::SUCCESS; |
87 | } | 99 | } |
@@ -231,6 +243,12 @@ uint16_t device::get_pid() | |||
231 | return dev_desc.idProduct; | 243 | return dev_desc.idProduct; |
232 | } | 244 | } |
233 | 245 | ||
246 | bool device::is_bus_addr_device(libusb_device *dev, uint8_t bus, uint8_t addr) | ||
247 | { | ||
248 | return ((libusb_get_bus_number(dev) == bus) && | ||
249 | (libusb_get_device_address(dev) == addr)); | ||
250 | } | ||
251 | |||
234 | /** | 252 | /** |
235 | * USB handle | 253 | * USB handle |
236 | */ | 254 | */ |