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

Report Interface Number #125

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions enumerator/enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type PortDetails struct {
IsUSB bool
VID string
PID string
MI string
SerialNumber string

// Manufacturer string
Expand Down
15 changes: 11 additions & 4 deletions enumerator/usb_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"
"path/filepath"

"go.bug.st/serial"
"github.com/ben-qnimble/go-serial"
)

func nativeGetDetailedPortsList() ([]*PortDetails, error) {
Expand Down Expand Up @@ -50,20 +50,25 @@ func nativeGetPortDetails(portPath string) (*PortDetails, error) {
subSystem := filepath.Base(subSystemPath)

result := &PortDetails{Name: portPath}
mi, err := readLine(filepath.Join(realDevicePath, "bInterfaceNumber"))
if err != nil {
return nil, fmt.Errorf("Can't determine interface number of %s: %s", filepath.Join(realDevicePath, "bInterfaceNumber"), err.Error())
}

switch subSystem {
case "usb-serial":
err := parseUSBSysFS(filepath.Dir(filepath.Dir(realDevicePath)), result)
err := parseUSBSysFS(filepath.Dir(filepath.Dir(realDevicePath)), result, mi)
return result, err
case "usb":
err := parseUSBSysFS(filepath.Dir(realDevicePath), result)
err := parseUSBSysFS(filepath.Dir(realDevicePath), result, mi)
return result, err
// TODO: other cases?
default:
return result, nil
}
}

func parseUSBSysFS(usbDevicePath string, details *PortDetails) error {
func parseUSBSysFS(usbDevicePath string, details *PortDetails, mi string) error {
vid, err := readLine(filepath.Join(usbDevicePath, "idVendor"))
if err != nil {
return err
Expand All @@ -72,6 +77,7 @@ func parseUSBSysFS(usbDevicePath string, details *PortDetails) error {
if err != nil {
return err
}

serial, err := readLine(filepath.Join(usbDevicePath, "serial"))
if err != nil {
return err
Expand All @@ -88,6 +94,7 @@ func parseUSBSysFS(usbDevicePath string, details *PortDetails) error {
details.IsUSB = true
details.VID = vid
details.PID = pid
details.MI = mi
details.SerialNumber = serial
//details.Manufacturer = manufacturer
//details.Product = product
Expand Down
25 changes: 17 additions & 8 deletions enumerator/usb_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,41 @@ import (
func parseDeviceID(deviceID string, details *PortDetails) {
// Windows stock USB-CDC driver
if len(deviceID) >= 3 && deviceID[:3] == "USB" {
re := regexp.MustCompile("VID_(....)&PID_(....)(\\\\(\\w+)$)?").FindAllStringSubmatch(deviceID, -1)
if re == nil || len(re[0]) < 2 {
re := regexp.MustCompile("VID_(....)&PID_(....)(?:&MI_(..)){0,1}(\\\\(\\w+)$)?").FindAllStringSubmatch(deviceID, -1)
if re == nil || len(re[0]) < 3 {
// Silently ignore unparsable strings
return
}
details.IsUSB = true
details.VID = re[0][1]
details.PID = re[0][2]
if len(re[0]) >= 4 {
details.SerialNumber = re[0][4]
if len(re[0]) > 4 {
details.MI = re[0][3]
}

if len(re[0]) >= 6 {
details.SerialNumber = re[0][5]
}
return
}

// FTDI driver
if len(deviceID) >= 7 && deviceID[:7] == "FTDIBUS" {
re := regexp.MustCompile("VID_(....)\\+PID_(....)(\\+(\\w+))?").FindAllStringSubmatch(deviceID, -1)
if re == nil || len(re[0]) < 2 {
re := regexp.MustCompile("VID_(....)\\+PID_(....)(?:+MI_(..)){0,1}(\\+(\\w+))?").FindAllStringSubmatch(deviceID, -1)
if re == nil || len(re[0]) < 3 {
// Silently ignore unparsable strings
return
}
details.IsUSB = true
details.VID = re[0][1]
details.PID = re[0][2]
if len(re[0]) >= 4 {
details.SerialNumber = re[0][4]

if len(re[0]) > 4 {
details.MI = re[0][3]
}

if len(re[0]) >= 6 {
details.SerialNumber = re[0][5]
}
return
}
Expand Down