From 6ad122da6aa210da7ce8f3cc3a287ba289a53085 Mon Sep 17 00:00:00 2001 From: Will McEnaney Date: Wed, 10 Nov 2021 15:04:13 +0000 Subject: [PATCH] Add Path to DeviceDesc (#101) Populates information about device port path in the device descriptor. --- device.go | 21 ++++++++++++++++----- libusb.go | 16 ++++++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/device.go b/device.go index 88e360b..4180b11 100644 --- a/device.go +++ b/device.go @@ -24,11 +24,22 @@ import ( // DeviceDesc is a representation of a USB device descriptor. type DeviceDesc struct { - // Bus information - Bus int // The bus on which the device was detected - Address int // The address of the device on the bus - Speed Speed // The negotiated operating speed for the device - Port int // The usb port on which the device was detected + // The bus on which the device was detected + Bus int + // The address of the device on the bus + Address int + // The negotiated operating speed for the device + Speed Speed + // The pyhsical port on the parent hub on which the device is connected. + // Ports are numbered from 1, excepting root hub devices which are always 0. + Port int + // Physical path of connected parent ports, starting at the root hub device. + // A path length of 0 represents a root hub device, + // a path length of 1 represents a device directly connected to a root hub, + // a path length of 2 or more are connected to intermediate hub devices. + // e.g. [1,2,3] represents a device connected to port 3 of a hub connected + // to port 2 of a hub connected to port 1 of a root hub. + Path []int // Version information Spec BCD // USB Specification Release Number diff --git a/libusb.go b/libusb.go index 4e7ea1d..4763594 100644 --- a/libusb.go +++ b/libusb.go @@ -224,14 +224,26 @@ func (libusbImpl) setDebug(c *libusbContext, lvl int) { } func (libusbImpl) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) { - var desc C.struct_libusb_device_descriptor + var ( + desc C.struct_libusb_device_descriptor + pathData [8]uint8 + path []int + port int + ) if err := fromErrNo(C.libusb_get_device_descriptor((*C.libusb_device)(d), &desc)); err != nil { return nil, err } + pathLen := int(C.libusb_get_port_numbers((*C.libusb_device)(d), (*C.uint8_t)(&pathData[0]), 8)) + for _, nPort := range pathData[:pathLen] { + port = int(nPort) + path = append(path, port) + } + // Defaults to port = 0, path = [] for root device dev := &DeviceDesc{ Bus: int(C.libusb_get_bus_number((*C.libusb_device)(d))), Address: int(C.libusb_get_device_address((*C.libusb_device)(d))), - Port: int(C.libusb_get_port_number((*C.libusb_device)(d))), + Port: port, + Path: path, Speed: Speed(C.libusb_get_device_speed((*C.libusb_device)(d))), Spec: BCD(desc.bcdUSB), Device: BCD(desc.bcdDevice),