From 56162d01057cbf0ec91e6e8706cacd265270630f Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sun, 9 Apr 2017 20:25:16 +0200 Subject: [PATCH] Remove BCD.Int, add BCD.Major/Minor. Remove USB spec versions - I don't expect them to get much use, and reuse of BCD for USB spec version and device revision makes it somewhat confusing. --- usb/error.go | 1 + usb/fakelibusb_test.go | 8 ++++---- usb/libusb.go | 4 ++-- usb/misc.go | 41 +++++++++++++++++++++++++---------------- usb/misc_test.go | 19 +++++++++++-------- usb/transfer.c | 4 ++-- 6 files changed, 45 insertions(+), 32 deletions(-) diff --git a/usb/error.go b/usb/error.go index f0f5f5a..f5577d3 100644 --- a/usb/error.go +++ b/usb/error.go @@ -76,6 +76,7 @@ var errorString = map[Error]string{ // TransferStatus contains information about the result of a transfer. type TransferStatus uint8 +// Transfer status values provided by libusb. const ( TransferCompleted TransferStatus = C.LIBUSB_TRANSFER_COMPLETED TransferError TransferStatus = C.LIBUSB_TRANSFER_ERROR diff --git a/usb/fakelibusb_test.go b/usb/fakelibusb_test.go index bcfd512..e3eba6b 100644 --- a/usb/fakelibusb_test.go +++ b/usb/fakelibusb_test.go @@ -30,8 +30,8 @@ var ( &Descriptor{ Bus: 1, Address: 1, - Spec: USB_2_0, - Device: BCD(0x0100), // 1.00 + Spec: Version(2, 0), + Device: Version(1, 0), Vendor: ID(0x9999), Product: ID(0x0001), Protocol: 255, @@ -66,8 +66,8 @@ var ( &Descriptor{ Bus: 1, Address: 2, - Spec: USB_2_0, - Device: BCD(0x0103), // 1.03 + Spec: Version(2, 0), + Device: Version(1, 3), Vendor: ID(0x8888), Product: ID(0x0002), Protocol: 255, diff --git a/usb/libusb.go b/usb/libusb.go index 189ec39..7e3644e 100644 --- a/usb/libusb.go +++ b/usb/libusb.go @@ -420,8 +420,8 @@ var ( libusbIsoOffset = unsafe.Offsetof(C.struct_libusb_transfer{}.iso_packet_desc) ) -//export xfer_callback -func xfer_callback(cptr unsafe.Pointer) { +//export xferCallback +func xferCallback(cptr unsafe.Pointer) { ch := *(*chan struct{})(cptr) close(ch) } diff --git a/usb/misc.go b/usb/misc.go index 1650e10..f3f80bc 100644 --- a/usb/misc.go +++ b/usb/misc.go @@ -19,30 +19,39 @@ import ( "fmt" ) +// BCD is a binary-coded decimal version number, with first 8 bits representing +// the major version number, an latter 8 bits the minor version number. +// Major and minor are composed of 4+4 bits, where each 4 bits represents +// a decimal digit. +// Example: BCD(0x1234) means major 12 (decimal) and minor 34 (decimal). type BCD uint16 -const ( - USB_2_0 BCD = 0x0200 - USB_1_1 BCD = 0x0110 - USB_1_0 BCD = 0x0100 -) - -func (d BCD) Int() (i int) { - ten := 1 - for o := uint(0); o < 4; o++ { - n := ((0xF << (o * 4)) & d) >> (o * 4) - i += int(n) * ten - ten *= 10 - } - return +// Major is the major number of the BCD. +func (s BCD) Major() uint8 { + maj := uint8(s >> 8) + return 10*(maj>>4) + maj&0x0f } -func (d BCD) String() string { - return fmt.Sprintf("%02x.%02x", int(d>>8), int(d&0xFF)) +// Minor is the minor number of the BCD. +func (s BCD) Minor() uint8 { + min := uint8(s & 0xff) + return 10*(min>>4) + min&0x0f } +// String returns a dotted representation of the BCD (major.minor). +func (s BCD) String() string { + return fmt.Sprintf("%d.%02d", s.Major(), s.Minor()) +} + +// Version returns a BCD version number with given major/minor. +func Version(major, minor uint8) BCD { + return (BCD(major)/10)<<12 | (BCD(major)%10)<<8 | (BCD(minor)/10)<<4 | BCD(minor)%10 +} + +// ID represents a vendor or product ID. type ID uint16 +// String returns a hexadecimal ID. func (id ID) String() string { return fmt.Sprintf("%04x", int(id)) } diff --git a/usb/misc_test.go b/usb/misc_test.go index 106e5d5..95b5c6e 100644 --- a/usb/misc_test.go +++ b/usb/misc_test.go @@ -21,19 +21,22 @@ import ( func TestBCD(t *testing.T) { tests := []struct { - BCD - Int int - Str string + major, minor uint8 + bcd BCD + str string }{ - {0x1234, 1234, "12.34"}, + {1, 1, 0x0101, "1.01"}, + {12, 34, 0x1234, "12.34"}, } for _, test := range tests { - if got, want := test.BCD.Int(), test.Int; got != want { - t.Errorf("Int(%x) = %d, want %d", test.BCD, got, want) + bcd := Version(test.major, test.minor) + if bcd != test.bcd { + t.Errorf("Version(%d, %d): got BCD %04x, want %04x", test.major, test.minor, uint16(bcd), uint16(test.bcd)) + continue } - if got, want := test.BCD.String(), test.Str; got != want { - t.Errorf("String(%x) = %q, want %q", test.BCD, got, want) + if got, want := bcd.String(), test.str; got != want { + t.Errorf("String(%04x) = %q, want %q", uint16(test.bcd), got, want) } } } diff --git a/usb/transfer.c b/usb/transfer.c index 9965019..ca099db 100644 --- a/usb/transfer.c +++ b/usb/transfer.c @@ -18,10 +18,10 @@ #include void print_xfer(struct libusb_transfer *xfer); -void xfer_callback(void *); +void xferCallback(void *); void callback(struct libusb_transfer *xfer) { - xfer_callback(xfer->user_data); + xferCallback(xfer->user_data); } int submit(struct libusb_transfer *xfer) {