Replace EndpointDirection with a bool

This commit is contained in:
Sebastian Zagrodzki
2017-04-09 18:51:51 +02:00
parent 168b501d53
commit 3d62929e05
3 changed files with 23 additions and 8 deletions

View File

@@ -46,8 +46,12 @@ type EndpointInfo struct {
// String returns the human-readable description of the endpoint.
func (e EndpointInfo) String() string {
addr := e.Number
if e.Direction == EndpointDirectionIn {
addr |= 0x80
}
ret := make([]string, 0, 3)
ret = append(ret, fmt.Sprintf("Endpoint #%d %s (address 0x%02x) %s", e.Number, e.Direction, uint8(e.Number)|uint8(e.Direction), e.TransferType))
ret = append(ret, fmt.Sprintf("Endpoint #%d %s (address 0x%02x) %s", e.Number, e.Direction, addr, e.TransferType))
switch e.TransferType {
case TransferTypeIsochronous:
ret = append(ret, fmt.Sprintf("- %s %s", e.IsoSyncType, e.UsageType))

View File

@@ -22,6 +22,7 @@ import "strconv"
// Class represents a USB-IF class or subclass code.
type Class uint8
// Standard classes defined by USB spec.
const (
ClassPerInterface Class = C.LIBUSB_CLASS_PER_INTERFACE
ClassAudio Class = C.LIBUSB_CLASS_AUDIO
@@ -67,8 +68,10 @@ func (p Protocol) String() string {
return strconv.Itoa(int(p))
}
// DescriptorType identifies the type of a USB descriptor.
type DescriptorType uint8
// Descriptor types defined by the USB spec.
const (
DescriptorTypeDevice DescriptorType = C.LIBUSB_DT_DEVICE
DescriptorTypeConfig DescriptorType = C.LIBUSB_DT_CONFIG
@@ -97,13 +100,17 @@ func (dt DescriptorType) String() string {
return descriptorTypeDescription[dt]
}
type EndpointDirection uint8
// EndpointDirection defines the direction of data flow - IN (device to host)
// or OUT (host to device).
type EndpointDirection bool
const (
EndpointNumMask = 0x0f
EndpointDirectionMask = 0x80
EndpointDirectionIn EndpointDirection = C.LIBUSB_ENDPOINT_IN
EndpointDirectionOut EndpointDirection = C.LIBUSB_ENDPOINT_OUT
EndpointNumMask = 0x0f
EndpointDirectionMask = 0x80
// EndpointDirectionIn marks data flowing from device to host.
EndpointDirectionIn EndpointDirection = true
// EndpointDirectionOut marks data flowing from host to device.
EndpointDirectionOut EndpointDirection = false
)
var endpointDirectionDescription = map[EndpointDirection]string{

View File

@@ -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),
Direction: EndpointDirection(ep.bEndpointAddress & EndpointDirectionMask),
Direction: EndpointDirection((ep.bEndpointAddress & EndpointDirectionMask) != 0),
TransferType: TransferType(ep.bmAttributes & TransferTypeMask),
MaxPacketSize: uint32(ep.wMaxPacketSize),
}
@@ -374,7 +374,11 @@ func (libusbImpl) alloc(d *libusbDevHandle, ep *EndpointInfo, timeout time.Durat
return nil, fmt.Errorf("libusb_alloc_transfer(%d) failed", isoPackets)
}
xfer.dev_handle = (*C.libusb_device_handle)(d)
xfer.endpoint = C.uchar(uint8(ep.Number&EndpointNumMask) | uint8(ep.Direction&EndpointDirectionMask))
addr := ep.Number & EndpointNumMask
if ep.Direction == EndpointDirectionIn {
addr |= EndpointDirectionMask
}
xfer.endpoint = C.uchar(addr)
xfer.timeout = C.uint(timeout / time.Millisecond)
xfer._type = C.uchar(ep.TransferType)
xfer.num_iso_packets = C.int(isoPackets)