1
1
package me.jahnen.libaums.libusbcommunication
2
2
3
- import android.hardware.usb.*
3
+ import android.hardware.usb.UsbDevice
4
+ import android.hardware.usb.UsbDeviceConnection
5
+ import android.hardware.usb.UsbEndpoint
6
+ import android.hardware.usb.UsbInterface
7
+ import android.hardware.usb.UsbManager
4
8
import android.util.Log
5
9
import me.jahnen.libaums.core.ErrNo
6
10
import me.jahnen.libaums.core.usb.PipeException
@@ -12,11 +16,11 @@ import java.nio.ByteBuffer
12
16
13
17
14
18
class LibusbCommunication (
15
- usbManager : UsbManager ,
16
- usbDevice : UsbDevice ,
17
- override val usbInterface : UsbInterface ,
18
- override val outEndpoint : UsbEndpoint ,
19
- override val inEndpoint : UsbEndpoint
19
+ usbManager : UsbManager ,
20
+ usbDevice : UsbDevice ,
21
+ override val usbInterface : UsbInterface ,
22
+ override val outEndpoint : UsbEndpoint ,
23
+ override val inEndpoint : UsbEndpoint
20
24
) : UsbCommunication {
21
25
22
26
// used to save heap address of libusb device handle
@@ -29,23 +33,24 @@ class LibusbCommunication(
29
33
System .loadLibrary(" libusbcom" )
30
34
31
35
deviceConnection = usbManager.openDevice(usbDevice)
32
- ? : throw IOException (" deviceConnection is null!" )
36
+ ? : throw IOException (" deviceConnection is null!" )
33
37
34
- if (! nativeInit(deviceConnection!! .fileDescriptor, libUsbHandleArray)) {
35
- throw IOException (" libusb init failed" )
38
+ val res = nativeInit(deviceConnection!! .fileDescriptor, libUsbHandleArray)
39
+ if (res != 0 ) {
40
+ throw LibusbException (" libusb init failed" , LibusbError .fromCode(res))
36
41
}
37
42
38
43
val claim = deviceConnection!! .claimInterface(usbInterface, true )
39
44
if (! claim) {
40
- throw IOException (" could not claim interface!" )
45
+ throw ErrNoIOException (" could not claim interface!" )
41
46
}
42
47
// val ret = nativeClaimInterface(libUsbHandle, usbInterface.id)
43
48
// if (ret < 0) {
44
49
// throw IOException("libusb returned $ret in claim interface")
45
50
// }
46
51
}
47
52
48
- private external fun nativeInit (fd : Int , handle : LongArray ): Boolean
53
+ private external fun nativeInit (fd : Int , handle : LongArray ): Int
49
54
private external fun nativeClaimInterface (handle : Long , interfaceNumber : Int ): Int
50
55
private external fun nativeClose (handle : Long , interfaceNumber : Int )
51
56
private external fun nativeReset (handle : Long ): Int
@@ -54,20 +59,30 @@ class LibusbCommunication(
54
59
private external fun nativeControlTransfer (handle : Long , requestType : Int , request : Int , value : Int , index : Int , buffer : ByteArray , length : Int , timeout : Int ): Int
55
60
56
61
override fun bulkOutTransfer (src : ByteBuffer ): Int {
57
- val transferred = nativeBulkTransfer(libUsbHandle, outEndpoint.address, src.array(), src.position(), src.remaining(), TRANSFER_TIMEOUT )
62
+ val transferred = nativeBulkTransfer(
63
+ libUsbHandle, outEndpoint.address, src.array(), src.position(), src.remaining(),
64
+ TRANSFER_TIMEOUT
65
+ )
58
66
when {
59
- transferred == LIBUSB_EPIPE -> throw PipeException ()
60
- transferred < 0 -> throw IOException (" libusb returned $transferred in control transfer" )
67
+ transferred == LibusbError .PIPE .code -> throw PipeException ()
68
+ transferred < 0 -> throw LibusbException (
69
+ " libusb control transfer failed" , LibusbError .fromCode(transferred)
70
+ )
61
71
}
62
72
src.position(src.position() + transferred)
63
73
return transferred
64
74
}
65
75
66
76
override fun bulkInTransfer (dest : ByteBuffer ): Int {
67
- val transferred = nativeBulkTransfer(libUsbHandle, inEndpoint.address, dest.array(), dest.position(), dest.remaining(), TRANSFER_TIMEOUT )
77
+ val transferred = nativeBulkTransfer(
78
+ libUsbHandle, inEndpoint.address, dest.array(), dest.position(), dest.remaining(),
79
+ TRANSFER_TIMEOUT
80
+ )
68
81
when {
69
- transferred == LIBUSB_EPIPE -> throw PipeException ()
70
- transferred < 0 -> throw IOException (" libusb returned $transferred in control transfer" )
82
+ transferred == LibusbError .PIPE .code -> throw PipeException ()
83
+ transferred < 0 -> throw LibusbException (
84
+ " libusb control transfer failed" , LibusbError .fromCode(transferred)
85
+ )
71
86
}
72
87
dest.position(dest.position() + transferred)
73
88
return transferred
@@ -76,7 +91,7 @@ class LibusbCommunication(
76
91
override fun controlTransfer (requestType : Int , request : Int , value : Int , index : Int , buffer : ByteArray , length : Int ): Int {
77
92
val ret = nativeControlTransfer(libUsbHandle, requestType, request, value, index, buffer, length, TRANSFER_TIMEOUT )
78
93
if (ret < 0 ) {
79
- throw IOException (" libusb returned $ret in control transfer" )
94
+ throw LibusbException (" libusb control transfer failed " , LibusbError .fromCode(ret) )
80
95
}
81
96
return ret
82
97
}
@@ -88,12 +103,14 @@ class LibusbCommunication(
88
103
89
104
val ret = nativeReset(libUsbHandle)
90
105
// if LIBUSB_ERROR_NOT_FOUND might need reenumeration
91
- Log .d(TAG , " libusb reset returned $ret " )
106
+ Log .d(TAG , " libusb reset returned $ret : ${ LibusbError .fromCode(ret).message} " )
92
107
93
108
var counter = 3
94
- while (! deviceConnection!! .claimInterface(usbInterface, true ) && counter >= 0 ) {
109
+ while (! deviceConnection!! .claimInterface(usbInterface, true ) && counter >= 0 ) {
95
110
if (counter == 0 ) {
96
- throw IOException (" Could not claim interface, errno: ${ErrNo .errno} ${ErrNo .errstr} " )
111
+ throw ErrNoIOException (
112
+ " Could not claim interface, errno: ${ErrNo .errno} ${ErrNo .errstr} "
113
+ )
97
114
}
98
115
Thread .sleep(800 )
99
116
counter--
@@ -102,7 +119,7 @@ class LibusbCommunication(
102
119
103
120
override fun clearFeatureHalt (endpoint : UsbEndpoint ) {
104
121
val ret = nativeClearHalt(libUsbHandle, endpoint.address)
105
- Log .d(TAG , " libusb clearFeatureHalt returned $ret " )
122
+ Log .d(TAG , " libusb clearFeatureHalt returned $ret : ${ LibusbError .fromCode(ret).message} " )
106
123
}
107
124
108
125
override fun close () {
@@ -113,7 +130,6 @@ class LibusbCommunication(
113
130
114
131
companion object {
115
132
private val TAG = LibusbCommunication ::class .java.simpleName
116
- private val LIBUSB_EPIPE = - 9
117
133
}
118
134
}
119
135
0 commit comments