Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Path to DeviceDesc #101

Merged
merged 7 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ 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
// The physical path of connected parent port numbers, starting at the root hub device
willmcenaney marked this conversation as resolved.
Show resolved Hide resolved
// A path length of 1 indicates that this device is connected directly to a root hub,
// A path length of 2 or more are connected to intermediate devices,
willmcenaney marked this conversation as resolved.
Show resolved Hide resolved
// Root hub devices have an empty path
// i.e. [1,2,3] represents a device connected to port 3 of a device connected to port 2 of a device attached to port 1 of a root hub.
willmcenaney marked this conversation as resolved.
Show resolved Hide resolved
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
}
if pathLen := int(C.libusb_get_port_numbers((*C.libusb_device)(d), (*C.uint8_t)(&pathData[0]), 8)); pathLen != 0 {
willmcenaney marked this conversation as resolved.
Show resolved Hide resolved
for _, nPort := range pathData[:pathLen] {
port = int(nPort)
path = append(path, port)
}
} // else default 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