-
Notifications
You must be signed in to change notification settings - Fork 129
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
return nil, nil | ||
} | ||
|
||
func (f *fakeLibusb) getDevice(d *libusbDevHandle) (*libusbDevice, error) { | ||
//TODO should we do something for this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"sync" | ||
"syscall" | ||
) | ||
|
||
// Context manages all resources related to USB device handling. | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not a descriptor, it's a device path, right?
may be better. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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.