From 83276566fc9ef808e75246804db4583f2646c707 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Wed, 26 Apr 2017 22:22:07 +0200 Subject: [PATCH] Replace uint8 with Go-idiomatic int, where it makes sense. --- usb/config.go | 8 ++++---- usb/device.go | 29 +++++++++++++++-------------- usb/endpoint.go | 4 ++-- usb/libusb.go | 8 ++++---- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/usb/config.go b/usb/config.go index 567cab7..fd45f2a 100644 --- a/usb/config.go +++ b/usb/config.go @@ -22,7 +22,7 @@ import "fmt" type InterfaceInfo struct { // Number is the number of this interface, a zero-based index in the array // of interfaces supported by the device configuration. - Number uint8 + Number int // AltSettings is a list of alternate settings supported by the interface. AltSettings []InterfaceSetting } @@ -37,9 +37,9 @@ func (i InterfaceInfo) String() string { // alternate setting, extracted from the descriptor. type InterfaceSetting struct { // Number is the number of this interface, the same as in InterfaceInfo. - Number uint8 + Number int // Alternate is the number of this alternate setting. - Alternate uint8 + Alternate int // Class is the USB-IF class code, as defined by the USB spec. Class Class // SubClass is the USB-IF subclass code, as defined by the USB spec. @@ -60,7 +60,7 @@ func (a InterfaceSetting) String() string { // ConfigInfo contains the information about a USB device configuration. type ConfigInfo struct { // Config is the configuration number. - Config uint8 + Config int // SelfPowered is true if the device is powered externally, i.e. not // drawing power from the USB bus. SelfPowered bool diff --git a/usb/device.go b/usb/device.go index dba7f77..6f74cc1 100644 --- a/usb/device.go +++ b/usb/device.go @@ -44,7 +44,7 @@ type Device struct { // Claimed interfaces lock *sync.Mutex - claimed map[uint8]int + claimed map[int]int } func newDevice(handle *libusbDevHandle, desc *Descriptor) *Device { @@ -56,7 +56,7 @@ func newDevice(handle *libusbDevHandle, desc *Descriptor) *Device { WriteTimeout: DefaultWriteTimeout, ControlTimeout: DefaultControlTimeout, lock: new(sync.Mutex), - claimed: make(map[uint8]int, ifaces), + claimed: make(map[int]int, ifaces), } return d @@ -74,15 +74,16 @@ func (d *Device) Control(rType, request uint8, val, idx uint16, data []byte) (in // ActiveConfig returns the config id (not the index) of the active configuration. // This corresponds to the ConfigInfo.Config field. -func (d *Device) ActiveConfig() (uint8, error) { - return libusb.getConfig(d.handle) +func (d *Device) ActiveConfig() (int, error) { + ret, err := libusb.getConfig(d.handle) + return int(ret), err } // SetConfig attempts to change the active configuration. // The cfg provided is the config id (not the index) of the configuration to set, // which corresponds to the ConfigInfo.Config field. -func (d *Device) SetConfig(cfg uint8) error { - return libusb.setConfig(d.handle, cfg) +func (d *Device) SetConfig(cfg int) error { + return libusb.setConfig(d.handle, uint8(cfg)) } // Close closes the device. @@ -93,14 +94,14 @@ func (d *Device) Close() error { d.lock.Lock() defer d.lock.Unlock() for iface := range d.claimed { - libusb.release(d.handle, iface) + libusb.release(d.handle, uint8(iface)) } libusb.close(d.handle) d.handle = nil return nil } -func (d *Device) openEndpoint(cfgNum, ifNum, setNum, epAddr uint8) (*endpoint, error) { +func (d *Device) openEndpoint(cfgNum, ifNum, setNum, epAddr int) (*endpoint, error) { var cfg *ConfigInfo for _, c := range d.Configs { if c.Config == cfgNum { @@ -158,14 +159,14 @@ func (d *Device) openEndpoint(cfgNum, ifNum, setNum, epAddr uint8) (*endpoint, e if err != nil { return nil, fmt.Errorf("usb: getcfg: %s", err) } - if activeConf != cfgNum { - if err := libusb.setConfig(d.handle, cfgNum); err != nil { + if activeConf != uint8(cfgNum) { + if err := libusb.setConfig(d.handle, uint8(cfgNum)); err != nil { return nil, fmt.Errorf("usb: setcfg: %s", err) } } // Claim the interface - if err := libusb.claim(d.handle, ifNum); err != nil { + if err := libusb.claim(d.handle, uint8(ifNum)); err != nil { return nil, fmt.Errorf("usb: claim: %s", err) } @@ -176,7 +177,7 @@ func (d *Device) openEndpoint(cfgNum, ifNum, setNum, epAddr uint8) (*endpoint, e // Choose the alternate if setAlternate { - if err := libusb.setAlt(d.handle, ifNum, setNum); err != nil { + if err := libusb.setAlt(d.handle, uint8(ifNum), uint8(setNum)); err != nil { return nil, fmt.Errorf("usb: setalt: %s", err) } } @@ -185,7 +186,7 @@ func (d *Device) openEndpoint(cfgNum, ifNum, setNum, epAddr uint8) (*endpoint, e } // InEndpoint prepares an IN endpoint for transfer. -func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*InEndpoint, error) { +func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum int) (*InEndpoint, error) { ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionIn)) if err != nil { return nil, err @@ -197,7 +198,7 @@ func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*InEndpoint, er } // OutEndpoint prepares an OUT endpoint for transfer. -func (d *Device) OutEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*OutEndpoint, error) { +func (d *Device) OutEndpoint(cfgNum, ifNum, setNum, epNum int) (*OutEndpoint, error) { ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionOut)) if err != nil { return nil, err diff --git a/usb/endpoint.go b/usb/endpoint.go index f13c729..a68d231 100644 --- a/usb/endpoint.go +++ b/usb/endpoint.go @@ -29,7 +29,7 @@ type EndpointInfo struct { // with endpoint direction IN. // The device can have up to two endpoints with the same number but with // different directions. - Number uint8 + Number int // Direction defines whether the data is flowing IN or OUT from the host perspective. Direction EndpointDirection // MaxPacketSize is the maximum USB packet size for a single frame/microframe. @@ -46,7 +46,7 @@ type EndpointInfo struct { UsageType UsageType } -func endpointAddr(n uint8, d EndpointDirection) uint8 { +func endpointAddr(n int, d EndpointDirection) int { addr := n if d == EndpointDirectionIn { addr |= 0x80 diff --git a/usb/libusb.go b/usb/libusb.go index 9249896..93ac407 100644 --- a/usb/libusb.go +++ b/usb/libusb.go @@ -41,7 +41,7 @@ type libusbEndpoint C.struct_libusb_endpoint_descriptor func (ep libusbEndpoint) endpointInfo(dev *Descriptor) EndpointInfo { ei := EndpointInfo{ - Number: uint8(ep.bEndpointAddress & endpointNumMask), + Number: int(ep.bEndpointAddress & endpointNumMask), Direction: EndpointDirection((ep.bEndpointAddress & endpointDirectionMask) != 0), TransferType: TransferType(ep.bmAttributes & transferTypeMask), MaxPacketSize: uint32(ep.wMaxPacketSize), @@ -214,7 +214,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) { return nil, err } c := ConfigInfo{ - Config: uint8(cfg.bConfigurationValue), + Config: int(cfg.bConfigurationValue), SelfPowered: (cfg.bmAttributes & selfPoweredMask) != 0, RemoteWakeup: (cfg.bmAttributes & remoteWakeupMask) != 0, // TODO(sebek): at GenX speeds MaxPower is expressed in units of 8mA, not 2mA. @@ -242,8 +242,8 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) { descs := make([]InterfaceSetting, 0, len(alts)) for _, alt := range alts { i := InterfaceSetting{ - Number: uint8(alt.bInterfaceNumber), - Alternate: uint8(alt.bAlternateSetting), + Number: int(alt.bInterfaceNumber), + Alternate: int(alt.bAlternateSetting), Class: Class(alt.bInterfaceClass), SubClass: Class(alt.bInterfaceSubClass), Protocol: Protocol(alt.bInterfaceProtocol),