Skip to content

Commit b5a4143

Browse files
committed
Windows reference code: improvements for composite devices
1 parent 3c4e69c commit b5a4143

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

reference/windows/USB/usb_device.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#include <regex>
2323
#include <thread>
2424

25-
usb_device::usb_device(usb_registry* registry, std::wstring&& device_path, int vendor_id, int product_id, const std::vector<uint8_t>& config_desc)
26-
: registry_(registry), vendor_id_(vendor_id), product_id_(product_id), is_open_(false), device_path_(std::move(device_path)) {
25+
usb_device::usb_device(usb_registry* registry, std::wstring&& device_path, int vendor_id, int product_id, const std::vector<uint8_t>& config_desc, bool is_composite)
26+
: registry_(registry), vendor_id_(vendor_id), product_id_(product_id), is_open_(false), device_path_(std::move(device_path)), is_composite_(is_composite) {
2727

2828
config_parser parser{};
2929
parser.parse(config_desc.data(), static_cast<int>(config_desc.size()));
@@ -458,7 +458,7 @@ void usb_device::cancel_transfer(usb_direction direction, int endpoint_number, O
458458
}
459459

460460
std::wstring usb_device::get_interface_device_path(int interface_num) {
461-
if (interface_num == 0)
461+
if (!is_composite_)
462462
return device_path_;
463463

464464
auto it = interface_device_paths_.find(interface_num);
@@ -471,9 +471,6 @@ std::wstring usb_device::get_interface_device_path(int interface_num) {
471471

472472
auto dev_info_set = device_info_set::of_path(device_path_);
473473

474-
if (!dev_info_set.is_composite_device())
475-
throw usb_error("internal error: interface belongs to a separate function but device is not composite");
476-
477474
auto children_instance_ids = dev_info_set.get_device_property_string_list(DEVPKEY_Device_Children);
478475
if (children_instance_ids.empty()) {
479476
std::wcerr << "missing children IDs for device " << device_path_ << std::endl;

reference/windows/USB/usb_device.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class usb_device {
248248
interface_handle(int intf_num, int first_num, std::wstring&& path);
249249
};
250250

251-
usb_device(usb_registry* registry, std::wstring&& device_path, int vendor_id, int product_id, const std::vector<uint8_t>& config_desc);
251+
usb_device(usb_registry* registry, std::wstring&& device_path, int vendor_id, int product_id, const std::vector<uint8_t>& config_desc, bool is_composite);
252252
void set_product_names(const std::string& manufacturer, const std::string& product, const std::string& serial_number);
253253
void build_handles(const std::wstring& device_path);
254254
std::wstring get_interface_device_path(int interface_num);
@@ -285,6 +285,7 @@ class usb_device {
285285
std::string product_;
286286
std::string serial_number_;
287287

288+
bool is_composite_;
288289
std::vector<usb_interface> interfaces_;
289290
std::vector<usb_composite_function> functions_;
290291
std::vector<interface_handle> interface_handles_;

reference/windows/USB/usb_registry.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ std::shared_ptr<usb_device> usb_registry::create_device_from_device_info(device_
167167
hub_handles[hub_path] = hub_handle;
168168
}
169169

170-
return create_device(std::move(device_path), hub_handle, usb_port_num);
170+
return create_device(std::move(device_path), dev_info_set.is_composite_device(), hub_handle, usb_port_num);
171171
}
172172

173-
std::shared_ptr<usb_device> usb_registry::create_device(std::wstring&& device_path, HANDLE hub_handle, DWORD usb_port_num) {
173+
std::shared_ptr<usb_device> usb_registry::create_device(std::wstring&& device_path, bool is_composite, HANDLE hub_handle, DWORD usb_port_num) {
174174

175175
// get device descriptor
176176
USB_NODE_CONNECTION_INFORMATION_EX conn_info = { 0 };
@@ -186,7 +186,7 @@ std::shared_ptr<usb_device> usb_registry::create_device(std::wstring&& device_pa
186186
auto config_desc = get_descriptor(hub_handle, usb_port_num, USB_CONFIGURATION_DESCRIPTOR_TYPE, 0, 0);
187187

188188
// Create new device
189-
std::shared_ptr<usb_device> device(new usb_device(this, std::move(device_path), vendorId, productId, config_desc));
189+
std::shared_ptr<usb_device> device(new usb_device(this, std::move(device_path), vendorId, productId, config_desc, is_composite));
190190
device->set_product_names(
191191
get_string(hub_handle, usb_port_num, conn_info.DeviceDescriptor.iManufacturer),
192192
get_string(hub_handle, usb_port_num, conn_info.DeviceDescriptor.iProduct),

reference/windows/USB/usb_registry.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class usb_registry {
5353

5454
void detect_present_devices();
5555
std::shared_ptr<usb_device> create_device_from_device_info(device_info_set& dev_info_set, std::wstring&& device_path, std::map<std::wstring, HANDLE>& hub_handles);
56-
std::shared_ptr<usb_device> create_device(std::wstring&& device_path, HANDLE hub_handle, DWORD usb_port_num);
56+
std::shared_ptr<usb_device> create_device(std::wstring&& device_path, bool is_composite, HANDLE hub_handle, DWORD usb_port_num);
5757

5858
static std::string get_string(HANDLE hub_handle, ULONG usb_port_num, int index);
5959
static std::vector<uint8_t> get_descriptor(HANDLE hub_handle, ULONG usb_port_num, uint16_t descriptor_type, int index, int language_id, int request_size = 0);

0 commit comments

Comments
 (0)