Skip to content

Commit

Permalink
Add Path to DeviceDesc (#101)
Browse files Browse the repository at this point in the history
Populates information about device port path in the device descriptor.
  • Loading branch information
willmcenaney authored Nov 10, 2021
1 parent 1f6abc1 commit 6ad122d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
21 changes: 16 additions & 5 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 6ad122d

Please sign in to comment.