Fix UsbSerialTransport.read() to throw IOException on error
This commit is contained in:
parent
5fbe0712e8
commit
2e08f9a340
1 changed files with 41 additions and 0 deletions
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.eddy.atomrelay
|
||||||
|
|
||||||
|
import android.hardware.usb.UsbConstants
|
||||||
|
import android.hardware.usb.UsbDeviceConnection
|
||||||
|
import android.hardware.usb.UsbEndpoint
|
||||||
|
import org.dkaukov.esp32.io.SerialTransport
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class UsbSerialTransport(
|
||||||
|
private val connection: UsbDeviceConnection,
|
||||||
|
private val epOut: UsbEndpoint,
|
||||||
|
private val epIn: UsbEndpoint
|
||||||
|
) : SerialTransport {
|
||||||
|
|
||||||
|
override fun read(buffer: ByteArray, length: Int): Int {
|
||||||
|
val n = connection.bulkTransfer(epIn, buffer, length, 5000)
|
||||||
|
if (n < 0) throw IOException("USB read failed")
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun write(buffer: ByteArray, length: Int) {
|
||||||
|
val n = connection.bulkTransfer(epOut, buffer, length, 5000)
|
||||||
|
if (n < 0) throw IOException("USB write failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setControlLines(dtr: Boolean, rts: Boolean) {
|
||||||
|
connection.controlTransfer(
|
||||||
|
UsbConstants.USB_DIR_OUT or UsbConstants.USB_TYPE_CLASS or 0x01,
|
||||||
|
0x22,
|
||||||
|
(if (dtr) 0x01 else 0x00) or (if (rts) 0x02 else 0x00),
|
||||||
|
0,
|
||||||
|
null,
|
||||||
|
0,
|
||||||
|
1000
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun readBufferSize(): Int {
|
||||||
|
return epIn.maxPacketSize.coerceAtLeast(64)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue