Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to achieve android support #126

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fakelibusb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ func (f *fakeLibusb) getDevices(*libusbContext) ([]*libusbDevice, error) {
return ret, nil
}

func (f *fakeLibusb) wrapSysDevice(ctx *libusbContext, systemDeviceHandle int) (*libusbDevHandle, error) {
//TODO should we do something for this
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a new field to the fakelibusb, "fileDescriptors", which should map int descriptor numbers to *libusbDevice (then the device can be pulled from f.fakeDevices map) or to *fakeDevice directly.
You can populate the keys of fileDescriptors by reusing slice indices from fakeDevices (from fakelibusb_devices.go), pointing to the same list of devices.

return nil, nil
}

func (f *fakeLibusb) getDevice(d *libusbDevHandle) (*libusbDevice, error) {
//TODO should we do something for this
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return f.handles[d]

return nil, nil
}

func (f *fakeLibusb) exit(*libusbContext) error {
close(f.submitted)
if got := len(f.ts); got > 0 {
Expand Down
20 changes: 20 additions & 0 deletions libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type libusbIntf interface {
dereference(*libusbDevice)
getDeviceDesc(*libusbDevice) (*DeviceDesc, error)
open(*libusbDevice) (*libusbDevHandle, error)
wrapSysDevice(*libusbContext, int) (*libusbDevHandle, error)

close(*libusbDevHandle)
reset(*libusbDevHandle) error
Expand All @@ -152,6 +153,7 @@ type libusbIntf interface {
getStringDesc(*libusbDevHandle, int) (string, error)
setAutoDetach(*libusbDevHandle, int) error
detachKernelDriver(*libusbDevHandle, uint8) error
getDevice(*libusbDevHandle) (*libusbDevice, error)

// interface
claim(*libusbDevHandle, uint8) error
Expand Down Expand Up @@ -217,6 +219,24 @@ func (libusbImpl) getDevices(ctx *libusbContext) ([]*libusbDevice, error) {
return ret, nil
}

func (libusbImpl) wrapSysDevice(ctx *libusbContext, systemDeviceHandle int) (*libusbDevHandle, error) {
var handle *C.libusb_device_handle
if ret := C.libusb_wrap_sys_device((*C.libusb_context)(ctx), C.intptr_t(systemDeviceHandle), &handle); ret < 0 {
return nil, fromErrNo(C.int(ret))
}

return (*libusbDevHandle)(handle), nil
}

func (libusbImpl) getDevice(d *libusbDevHandle) (*libusbDevice, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libusb_get_device doesn't return errors, so this should probably be just a trivial wrapper:

func (...) getDevice(*d libusbDevHandle) *libusbDevice {
  return C.lbusb_get_device(...)
}

device := C.libusb_get_device((*C.libusb_device_handle)(d))
if device == nil {
return nil, fmt.Errorf("libusb_get_device failed")
}
Comment on lines +248 to +250
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to handle this case here. As far as I can tell, the only thing libusb_get_device does is return the device that is already stored in the handle. I don't think it can be nil, if obtaining the handle didn't return an error.


return (*libusbDevice)(device), nil
}

func (libusbImpl) exit(c *libusbContext) error {
C.libusb_exit((*C.libusb_context)(c))
return nil
Expand Down
36 changes: 36 additions & 0 deletions usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import (
"errors"
"fmt"
"sync"
"syscall"
)

// Context manages all resources related to USB device handling.
Expand Down Expand Up @@ -210,6 +211,41 @@ func (c *Context) OpenDevices(opener func(desc *DeviceDesc) bool) ([]*Device, er
return ret, reterr
}

func (c *Context) OpenDeviceWithFileDescriptor(fileDescriptor string) (*Device, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a descriptor, it's a device path, right?

OpenDevicePath(path string)

may be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually a file descriptor! It's a bit 'funny', but what you get from the Android API is this: https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#getFileDescriptor()

fd, err := syscall.Open(fileDescriptor, syscall.O_RDWR, 0)
if err != nil {
return nil, err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annotate this error to indicate that it came from Open(path)

}

handle, err := c.libusb.wrapSysDevice(c.ctx, fd)
if err != nil {
return nil, err
}

dev, err := c.libusb.getDevice(handle)
if err != nil {
return nil, err
}

desc, err := c.libusb.getDeviceDesc(dev)
defer c.libusb.dereference(dev)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed - according to the docs, libusb_get_device doesn't increase reference count and dereferencing is not required.

if err != nil {
return nil, err
}

handle, err = c.libusb.open(dev)
if err != nil {
return nil, err
}

o := &Device{handle: handle, ctx: c, Desc: desc}
c.mu.Lock()
c.devices[o] = true
c.mu.Unlock()

return o, nil
}

// OpenDeviceWithVIDPID opens Device from specific VendorId and ProductId.
// If none is found, it returns nil and nil error. If there are multiple devices
// with the same VID/PID, it will return one of them, picked arbitrarily.
Expand Down
18 changes: 17 additions & 1 deletion usb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

package gousb

import "testing"
import (
"testing"
)

func TestOPenDevices(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -94,3 +96,17 @@ func TestOpenDeviceWithVIDPID(t *testing.T) {
}
}
}

func TestOpenDeviceWithFileDescriptor(t *testing.T) {
ctx := NewContext()
defer ctx.Close()

descriptor := "/dev/bus/usb/001/002"
device, err := ctx.OpenDeviceWithFileDescriptor(descriptor)
if err != nil {
t.Errorf("OpenDeviceWithFileDescriptor: failed opening device %s", descriptor)
}

device.Close()

}