Android support via fileDescriptor (#130)

Adds a new way to initialize the context, `ContextOptions`, and a new way to open devices, `OpenDeviceWithFileDescriptor`.

Combined they can be used to support Android devices, where device enumeration is not supported (can be now disabled through `ContextOptions`) and where USB devices can be opened in the Android SDK and passed over to gousb through a file descriptor.

Co-authored-by: Juan <994594+juaoose@users.noreply.github.com>
Co-authored-by: Kuba Raczkowski <kuba.raczkowski@spectricity.com>
This commit is contained in:
Kuba Raczkowski
2024-09-08 21:40:30 +02:00
committed by GitHub
parent 8c7a7eb841
commit 606016adee
5 changed files with 176 additions and 20 deletions

View File

@@ -94,3 +94,44 @@ func TestOpenDeviceWithVIDPID(t *testing.T) {
}
}
}
func TestOpenDeviceWithFileDescriptor(t *testing.T) {
ctx := newContextWithImpl(newFakeLibusb())
defer ctx.Close()
// file descriptor is an index to the FakeDevices array
for _, d := range []struct {
vid, pid ID
sysDevPtr uintptr
}{
{0x9999, 0x0001, 78},
{0x8888, 0x0002, 94},
} {
dev, err := ctx.OpenDeviceWithFileDescriptor(d.sysDevPtr)
if err != nil {
t.Fatalf("OpenDeviceWithFileDescriptor(%d): err != nil for a valid device: %v", d.sysDevPtr, err)
}
if dev == nil {
t.Fatalf("OpenDeviceWithFileDescriptor(%d): device == nil for a valid device", d.sysDevPtr)
}
if dev != nil && (dev.Desc.Vendor != ID(d.vid) || dev.Desc.Product != ID(d.pid)) {
t.Errorf("OpenDeviceWithFileDescriptor(%d): device's VID/PID %s/%s don't match expected: %s/%s", d.sysDevPtr, dev.Desc.Vendor, dev.Desc.Product, ID(d.vid), ID(d.pid))
}
}
}
func TestOpenDeviceWithFileDescriptorOnMissingDevice(t *testing.T) {
ctx := newContextWithImpl(newFakeLibusb())
defer ctx.Close()
for _, sysDevPtr := range []uintptr{
7, // set, but does not exist in the fakeDevices array
0, // unset
} {
if _, err := ctx.OpenDeviceWithFileDescriptor(sysDevPtr); err == nil {
t.Errorf("OpenDeviceWithFileDescriptor(%d): got nil error for invalid device", sysDevPtr)
}
}
}