Replace uint8 with Go-idiomatic int, where it makes sense.

This commit is contained in:
Sebastian Zagrodzki
2017-04-26 22:22:07 +02:00
parent a55abe9076
commit 83276566fc
4 changed files with 25 additions and 24 deletions

View File

@@ -22,7 +22,7 @@ import "fmt"
type InterfaceInfo struct { type InterfaceInfo struct {
// Number is the number of this interface, a zero-based index in the array // Number is the number of this interface, a zero-based index in the array
// of interfaces supported by the device configuration. // of interfaces supported by the device configuration.
Number uint8 Number int
// AltSettings is a list of alternate settings supported by the interface. // AltSettings is a list of alternate settings supported by the interface.
AltSettings []InterfaceSetting AltSettings []InterfaceSetting
} }
@@ -37,9 +37,9 @@ func (i InterfaceInfo) String() string {
// alternate setting, extracted from the descriptor. // alternate setting, extracted from the descriptor.
type InterfaceSetting struct { type InterfaceSetting struct {
// Number is the number of this interface, the same as in InterfaceInfo. // 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 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 is the USB-IF class code, as defined by the USB spec.
Class Class Class Class
// SubClass is the USB-IF subclass code, as defined by the USB spec. // 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. // ConfigInfo contains the information about a USB device configuration.
type ConfigInfo struct { type ConfigInfo struct {
// Config is the configuration number. // Config is the configuration number.
Config uint8 Config int
// SelfPowered is true if the device is powered externally, i.e. not // SelfPowered is true if the device is powered externally, i.e. not
// drawing power from the USB bus. // drawing power from the USB bus.
SelfPowered bool SelfPowered bool

View File

@@ -44,7 +44,7 @@ type Device struct {
// Claimed interfaces // Claimed interfaces
lock *sync.Mutex lock *sync.Mutex
claimed map[uint8]int claimed map[int]int
} }
func newDevice(handle *libusbDevHandle, desc *Descriptor) *Device { func newDevice(handle *libusbDevHandle, desc *Descriptor) *Device {
@@ -56,7 +56,7 @@ func newDevice(handle *libusbDevHandle, desc *Descriptor) *Device {
WriteTimeout: DefaultWriteTimeout, WriteTimeout: DefaultWriteTimeout,
ControlTimeout: DefaultControlTimeout, ControlTimeout: DefaultControlTimeout,
lock: new(sync.Mutex), lock: new(sync.Mutex),
claimed: make(map[uint8]int, ifaces), claimed: make(map[int]int, ifaces),
} }
return d 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. // ActiveConfig returns the config id (not the index) of the active configuration.
// This corresponds to the ConfigInfo.Config field. // This corresponds to the ConfigInfo.Config field.
func (d *Device) ActiveConfig() (uint8, error) { func (d *Device) ActiveConfig() (int, error) {
return libusb.getConfig(d.handle) ret, err := libusb.getConfig(d.handle)
return int(ret), err
} }
// SetConfig attempts to change the active configuration. // SetConfig attempts to change the active configuration.
// The cfg provided is the config id (not the index) of the configuration to set, // The cfg provided is the config id (not the index) of the configuration to set,
// which corresponds to the ConfigInfo.Config field. // which corresponds to the ConfigInfo.Config field.
func (d *Device) SetConfig(cfg uint8) error { func (d *Device) SetConfig(cfg int) error {
return libusb.setConfig(d.handle, cfg) return libusb.setConfig(d.handle, uint8(cfg))
} }
// Close closes the device. // Close closes the device.
@@ -93,14 +94,14 @@ func (d *Device) Close() error {
d.lock.Lock() d.lock.Lock()
defer d.lock.Unlock() defer d.lock.Unlock()
for iface := range d.claimed { for iface := range d.claimed {
libusb.release(d.handle, iface) libusb.release(d.handle, uint8(iface))
} }
libusb.close(d.handle) libusb.close(d.handle)
d.handle = nil d.handle = nil
return 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 var cfg *ConfigInfo
for _, c := range d.Configs { for _, c := range d.Configs {
if c.Config == cfgNum { if c.Config == cfgNum {
@@ -158,14 +159,14 @@ func (d *Device) openEndpoint(cfgNum, ifNum, setNum, epAddr uint8) (*endpoint, e
if err != nil { if err != nil {
return nil, fmt.Errorf("usb: getcfg: %s", err) return nil, fmt.Errorf("usb: getcfg: %s", err)
} }
if activeConf != cfgNum { if activeConf != uint8(cfgNum) {
if err := libusb.setConfig(d.handle, cfgNum); err != nil { if err := libusb.setConfig(d.handle, uint8(cfgNum)); err != nil {
return nil, fmt.Errorf("usb: setcfg: %s", err) return nil, fmt.Errorf("usb: setcfg: %s", err)
} }
} }
// Claim the interface // 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) 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 // Choose the alternate
if setAlternate { 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) 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. // 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)) ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionIn))
if err != nil { if err != nil {
return nil, err 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. // 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)) ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionOut))
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -29,7 +29,7 @@ type EndpointInfo struct {
// with endpoint direction IN. // with endpoint direction IN.
// The device can have up to two endpoints with the same number but with // The device can have up to two endpoints with the same number but with
// different directions. // different directions.
Number uint8 Number int
// Direction defines whether the data is flowing IN or OUT from the host perspective. // Direction defines whether the data is flowing IN or OUT from the host perspective.
Direction EndpointDirection Direction EndpointDirection
// MaxPacketSize is the maximum USB packet size for a single frame/microframe. // MaxPacketSize is the maximum USB packet size for a single frame/microframe.
@@ -46,7 +46,7 @@ type EndpointInfo struct {
UsageType UsageType UsageType UsageType
} }
func endpointAddr(n uint8, d EndpointDirection) uint8 { func endpointAddr(n int, d EndpointDirection) int {
addr := n addr := n
if d == EndpointDirectionIn { if d == EndpointDirectionIn {
addr |= 0x80 addr |= 0x80

View File

@@ -41,7 +41,7 @@ type libusbEndpoint C.struct_libusb_endpoint_descriptor
func (ep libusbEndpoint) endpointInfo(dev *Descriptor) EndpointInfo { func (ep libusbEndpoint) endpointInfo(dev *Descriptor) EndpointInfo {
ei := EndpointInfo{ ei := EndpointInfo{
Number: uint8(ep.bEndpointAddress & endpointNumMask), Number: int(ep.bEndpointAddress & endpointNumMask),
Direction: EndpointDirection((ep.bEndpointAddress & endpointDirectionMask) != 0), Direction: EndpointDirection((ep.bEndpointAddress & endpointDirectionMask) != 0),
TransferType: TransferType(ep.bmAttributes & transferTypeMask), TransferType: TransferType(ep.bmAttributes & transferTypeMask),
MaxPacketSize: uint32(ep.wMaxPacketSize), MaxPacketSize: uint32(ep.wMaxPacketSize),
@@ -214,7 +214,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
return nil, err return nil, err
} }
c := ConfigInfo{ c := ConfigInfo{
Config: uint8(cfg.bConfigurationValue), Config: int(cfg.bConfigurationValue),
SelfPowered: (cfg.bmAttributes & selfPoweredMask) != 0, SelfPowered: (cfg.bmAttributes & selfPoweredMask) != 0,
RemoteWakeup: (cfg.bmAttributes & remoteWakeupMask) != 0, RemoteWakeup: (cfg.bmAttributes & remoteWakeupMask) != 0,
// TODO(sebek): at GenX speeds MaxPower is expressed in units of 8mA, not 2mA. // 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)) descs := make([]InterfaceSetting, 0, len(alts))
for _, alt := range alts { for _, alt := range alts {
i := InterfaceSetting{ i := InterfaceSetting{
Number: uint8(alt.bInterfaceNumber), Number: int(alt.bInterfaceNumber),
Alternate: uint8(alt.bAlternateSetting), Alternate: int(alt.bAlternateSetting),
Class: Class(alt.bInterfaceClass), Class: Class(alt.bInterfaceClass),
SubClass: Class(alt.bInterfaceSubClass), SubClass: Class(alt.bInterfaceSubClass),
Protocol: Protocol(alt.bInterfaceProtocol), Protocol: Protocol(alt.bInterfaceProtocol),