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.
This commit is contained in:
Sebastian Zagrodzki
2017-04-09 20:25:16 +02:00
parent a5c4de3a29
commit 56162d0105
6 changed files with 45 additions and 32 deletions

View File

@@ -76,6 +76,7 @@ var errorString = map[Error]string{
// TransferStatus contains information about the result of a transfer. // TransferStatus contains information about the result of a transfer.
type TransferStatus uint8 type TransferStatus uint8
// Transfer status values provided by libusb.
const ( const (
TransferCompleted TransferStatus = C.LIBUSB_TRANSFER_COMPLETED TransferCompleted TransferStatus = C.LIBUSB_TRANSFER_COMPLETED
TransferError TransferStatus = C.LIBUSB_TRANSFER_ERROR TransferError TransferStatus = C.LIBUSB_TRANSFER_ERROR

View File

@@ -30,8 +30,8 @@ var (
&Descriptor{ &Descriptor{
Bus: 1, Bus: 1,
Address: 1, Address: 1,
Spec: USB_2_0, Spec: Version(2, 0),
Device: BCD(0x0100), // 1.00 Device: Version(1, 0),
Vendor: ID(0x9999), Vendor: ID(0x9999),
Product: ID(0x0001), Product: ID(0x0001),
Protocol: 255, Protocol: 255,
@@ -66,8 +66,8 @@ var (
&Descriptor{ &Descriptor{
Bus: 1, Bus: 1,
Address: 2, Address: 2,
Spec: USB_2_0, Spec: Version(2, 0),
Device: BCD(0x0103), // 1.03 Device: Version(1, 3),
Vendor: ID(0x8888), Vendor: ID(0x8888),
Product: ID(0x0002), Product: ID(0x0002),
Protocol: 255, Protocol: 255,

View File

@@ -420,8 +420,8 @@ var (
libusbIsoOffset = unsafe.Offsetof(C.struct_libusb_transfer{}.iso_packet_desc) libusbIsoOffset = unsafe.Offsetof(C.struct_libusb_transfer{}.iso_packet_desc)
) )
//export xfer_callback //export xferCallback
func xfer_callback(cptr unsafe.Pointer) { func xferCallback(cptr unsafe.Pointer) {
ch := *(*chan struct{})(cptr) ch := *(*chan struct{})(cptr)
close(ch) close(ch)
} }

View File

@@ -19,30 +19,39 @@ import (
"fmt" "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 type BCD uint16
const ( // Major is the major number of the BCD.
USB_2_0 BCD = 0x0200 func (s BCD) Major() uint8 {
USB_1_1 BCD = 0x0110 maj := uint8(s >> 8)
USB_1_0 BCD = 0x0100 return 10*(maj>>4) + maj&0x0f
)
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
} }
func (d BCD) String() string { // Minor is the minor number of the BCD.
return fmt.Sprintf("%02x.%02x", int(d>>8), int(d&0xFF)) 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 type ID uint16
// String returns a hexadecimal ID.
func (id ID) String() string { func (id ID) String() string {
return fmt.Sprintf("%04x", int(id)) return fmt.Sprintf("%04x", int(id))
} }

View File

@@ -21,19 +21,22 @@ import (
func TestBCD(t *testing.T) { func TestBCD(t *testing.T) {
tests := []struct { tests := []struct {
BCD major, minor uint8
Int int bcd BCD
Str string str string
}{ }{
{0x1234, 1234, "12.34"}, {1, 1, 0x0101, "1.01"},
{12, 34, 0x1234, "12.34"},
} }
for _, test := range tests { for _, test := range tests {
if got, want := test.BCD.Int(), test.Int; got != want { bcd := Version(test.major, test.minor)
t.Errorf("Int(%x) = %d, want %d", test.BCD, got, want) 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 { if got, want := bcd.String(), test.str; got != want {
t.Errorf("String(%x) = %q, want %q", test.BCD, got, want) t.Errorf("String(%04x) = %q, want %q", uint16(test.bcd), got, want)
} }
} }
} }

View File

@@ -18,10 +18,10 @@
#include <string.h> #include <string.h>
void print_xfer(struct libusb_transfer *xfer); void print_xfer(struct libusb_transfer *xfer);
void xfer_callback(void *); void xferCallback(void *);
void callback(struct libusb_transfer *xfer) { void callback(struct libusb_transfer *xfer) {
xfer_callback(xfer->user_data); xferCallback(xfer->user_data);
} }
int submit(struct libusb_transfer *xfer) { int submit(struct libusb_transfer *xfer) {