net-predictable/usb.go

50 lines
923 B
Go

package main
import (
"fmt"
"strings"
)
func (i *iface) ProcUSB(devpath string) (seg string, err error) {
var (
bus, conf, iface int
port_path []int
work string
)
res := strings.Split(devpath, ":")
if len(res) != 2 {
err = ErrInvalidUSBFormat
return
}
// Bus and ports
if _, err = fmt.Sscanf(res[0], "%d-%s", &bus, &work); err != nil {
err = ErrInvalidUSBFormat
return
}
for _, v := range strings.Split(work, ".") {
var cp int
if _, err = fmt.Sscanf(v, "%d", &cp); err != nil {
err = ErrInvalidUSBFormat
return
}
port_path = append(port_path, cp)
}
// Configuration and interface
if _, err = fmt.Sscanf(res[1], "%d.%d", &conf, &iface); err != nil {
err = ErrInvalidUSBFormat
return
}
var b strings.Builder
for _, v := range port_path {
b.WriteString(fmt.Sprintf("u%d", v))
}
b.WriteString(fmt.Sprintf("i%d", iface))
seg = b.String()
return
}