在Android中,我使用类(UsbIso.java)从连接的USB设备以同步方式传输数据。由于Android本机不支持同步传输,因此我不得不通过JNA库使用USBFS本机Linux机制进行正确的ioctl调用。
在具有32位架构的Android设备(armeabi,armeabi-v7a)中,所有功能均可正常运行。在具有64位体系结构(arm64-v8a)的Android设备中,要获取URB的ioctl调用(reapRequest方法内部的USBDEVFS_REAPURB,请参见下面的相关代码)将返回错误14,错误的地址。我猜这是由USBDEVFS_REAPURB参数或PointerByReference参数引起的,该参数指向无效的虚拟地址,但是我不知道如何解决它。
UsbIso.java类中导致此错误的相关代码是这样的:
public Request reapRequest (boolean wait) throws IOException {
PointerByReference urbPointer = new PointerByReference();
int func = wait ? USBDEVFS_REAPURB : USBDEVFS_REAPURBNDELAY;
int rc;
try {
rc = libc.ioctl(fileDescriptor, func, urbPointer); // <-- Error 14, bad address
} catch (LastErrorException e) {
if (e.getErrorCode() == EAGAIN && !wait) {
return null;
}
}
...
}
您正在使用针对32位优化的源代码:
// Note: The layout and size of the USBFS structures matches that of Linux Kernel 3.2 and 3.14
// for ARM 32 bit. For other environments (X86, 64 bit, future Linux kernels), it might be
// necessary to adjust some values.
虽然JNA通常会针对32位和64位调整结构映射,但此代码认为JNA太慢,因此需要手动映射这些偏移量:
// This class is modeled after struct usbdevfs_urb in <linuxKernel>/include/linux/usbdevice_fs.h
// At first I implemented the URB structure directly using com.sun.jna.Structure, but that was extremely slow.
// Therefore byte offsets are now used to access the fields of the structure.
如果您查看结构映射,usbdevfs_urb
那么有3个指针字段需要从4字节偏移量调整为8字节偏移量。例如,第5个字段buffer
从4个字节更改为8个字节,因此此代码中断:
public void setBuffer (Pointer buffer) {
urbBuf.putInt(12, (int)Pointer.nativeValue(buffer)); }
public void setBufferLength (int bufferLength) {
urbBuf.putInt(16, bufferLength); }
特别是,putInt(12, (int) ...)
可能应该putLong(12, ...)
为,而下一次调用中的16应该为20(以此类推,将剩余的偏移量加4)。
最后两个字段也是8字节vs. 4字节,因此setUserContext()
和getUserContext()
需要处理long
而不是int
,并且urbBaseSize
需求从44增加到52(+为buffer
4,对于+为4 userContext
。
我看到一些其他int
变量表示需要变为long
s的内存地址。我可能还需要其他更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句