From 3d62929e058bcf7408d0f661cce2ec5f45fdaa74 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sun, 9 Apr 2017 18:51:51 +0200 Subject: [PATCH] Replace EndpointDirection with a bool --- usb/config.go | 6 +++++- usb/constants.go | 17 ++++++++++++----- usb/libusb.go | 8 ++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/usb/config.go b/usb/config.go index 43624c0..b492e26 100644 --- a/usb/config.go +++ b/usb/config.go @@ -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)) diff --git a/usb/constants.go b/usb/constants.go index 6ecbfe2..aec3a0d 100644 --- a/usb/constants.go +++ b/usb/constants.go @@ -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{ diff --git a/usb/libusb.go b/usb/libusb.go index 5950618..db6f8e3 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), - 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)