diff --git a/README.md b/README.md index 7e679cc..200642e 100644 --- a/README.md +++ b/README.md @@ -60,32 +60,25 @@ Build *Note* For MinGW32, use **MinGW32/static/libusb-1.0.a** while MinGW64 use **MinGW64/static/libusb-1.0.a** for linker -- Open `$(GOPATH)/src/github.com/.../gousb/usb/usb.go`. Then inject: +- Open `$(GOPATH)/src/github.com/kylelemons/gousb/usb/usb.go`. + +Then edit `#cgo` directive, such as // #cgo CFLAGS: -ID:/lib/libusbx-1.0.xx/include // #cgo LDFLAGS: D:/lib/libusbx-1.0.xx/MinGW64/static/libusb-1.0.a -before the line: +to your `libusb-1.0` installed path before the line: // #include This flag will tell the linker the exact path of static library -- Go to `$(GOPATH)/src/github.com/.../gousb/usb/`. Run: +- Go to `$(GOPATH)/src/github.com/kylelemons/gousb/`. Run: - $ go install - - -- Go to `$(GOPATH)/src/github.com/.../gousb/usbid/`. Run: - - $ go install - -Example: lsusb --------------- -- Go to `$(GOPATH)/src/github.com/.../gousb/lsusb/`. Run: - - $ go run main.go + $ go install ./... + + `lsusb` can run under `$GOBIN/lsusb` Documentation ============= diff --git a/usb/usb.go b/usb/usb.go index b106815..dcbea62 100644 --- a/usb/usb.go +++ b/usb/usb.go @@ -15,7 +15,10 @@ // Package usb provides a wrapper around libusb-1.0. package usb -// #cgo LDFLAGS: -lusb-1.0 +// #cgo windows CFLAGS: -ID:/lib/libusb-1.0.19/include +// #cgo windows amd64 LDFLAGS: D:/lib/libusb-1.0.19/MinGW64/static/libusb-1.0.a +// #cgo windows 386 LDFLAGS: D:/lib/libusb-1.0.19/MinGW32/static/libusb-1.0.a +// #cgo !windows LDFLAGS: -lusb-1.0 // #include import "C" @@ -106,6 +109,22 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro return ret, reterr } +// OpenDeviceWithVidPid opens Device from specific VendorId and ProductId. +// If there are any errors, it'll returns at second value. +func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) { + handle := C.libusb_open_device_with_vid_pid(c.ctx, (C.uint16_t)(vid), (C.uint16_t)(pid)) + dev := C.libusb_get_device(handle) + desc, err := newDescriptor(dev) + + // return an error from nil-handle and nil-device + if err != nil { + return nil, err + } + + device := newDevice(handle, desc) + return device, nil +} + func (c *Context) Close() error { close(c.done) if c.ctx != nil { diff --git a/usb/usb_test.go b/usb/usb_test.go index 22aad4f..689f7f1 100644 --- a/usb/usb_test.go +++ b/usb/usb_test.go @@ -81,13 +81,53 @@ func TestEnum(t *testing.T) { } } +func TestOpenDeviceWithVidPid(t *testing.T) { + c := NewContext() + defer c.Close() + c.Debug(0) + + // Accept for all device + devs, err := c.ListDevices(func(desc *Descriptor) bool { + return true + }) + defer func() { + for _, d := range devs { + d.Close() + } + }() + + if err != nil { + t.Fatalf("list: %s", err) + } + + for i := range devs { + vid := devs[i].Vendor + pid := devs[i].Product + device, err := c.OpenDeviceWithVidPid((int)(vid), (int)(pid)) + + // if the context failed to open device + if err != nil { + t.Fail() + } + + // if opened device was not valid + if device.Descriptor.Bus != devs[i].Bus || + device.Descriptor.Address != devs[i].Address || + device.Vendor != devs[i].Vendor || + device.Product != devs[i].Product { + t.Fail() + } + + } +} + func TestMultipleContexts(t *testing.T) { var buf bytes.Buffer log.SetOutput(&buf) for i := 0; i < 2; i++ { ctx := NewContext() _, err := ctx.ListDevices(func(desc *Descriptor) bool { - return false + return true }) if err != nil { t.Fatal(err)