tested for libusb-1.0.19 on windows (MinGw)
This commit is contained in:
23
README.md
23
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
|
*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 CFLAGS: -ID:/lib/libusbx-1.0.xx/include
|
||||||
// #cgo LDFLAGS: D:/lib/libusbx-1.0.xx/MinGW64/static/libusb-1.0.a
|
// #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 <libusb-1.0/libusb.h>
|
// #include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
This flag will tell the linker the exact path of static library
|
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 install ./...
|
||||||
|
|
||||||
|
`lsusb` can run under `$GOBIN/lsusb`
|
||||||
- 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
|
|
||||||
|
|
||||||
Documentation
|
Documentation
|
||||||
=============
|
=============
|
||||||
|
21
usb/usb.go
21
usb/usb.go
@@ -15,7 +15,10 @@
|
|||||||
// Package usb provides a wrapper around libusb-1.0.
|
// Package usb provides a wrapper around libusb-1.0.
|
||||||
package usb
|
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 <libusb-1.0/libusb.h>
|
// #include <libusb-1.0/libusb.h>
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
@@ -106,6 +109,22 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
|
|||||||
return ret, reterr
|
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 {
|
func (c *Context) Close() error {
|
||||||
close(c.done)
|
close(c.done)
|
||||||
if c.ctx != nil {
|
if c.ctx != nil {
|
||||||
|
@@ -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) {
|
func TestMultipleContexts(t *testing.T) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
log.SetOutput(&buf)
|
log.SetOutput(&buf)
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
ctx := NewContext()
|
ctx := NewContext()
|
||||||
_, err := ctx.ListDevices(func(desc *Descriptor) bool {
|
_, err := ctx.ListDevices(func(desc *Descriptor) bool {
|
||||||
return false
|
return true
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
Reference in New Issue
Block a user