dash ble: use higher timeout when reading a message

This timeout(4s) is the same that the PDM uses to send messaged with
`eqos` when they are not acknowledged
This commit is contained in:
Andrei Vereha 2021-03-14 22:04:57 +01:00
parent 442a0b274f
commit 8868f29ab2
3 changed files with 43 additions and 42 deletions

View file

@ -55,8 +55,8 @@ class BleCommCallbacks(
} }
@Throws(InterruptedException::class, TimeoutException::class, CouldNotConfirmWriteException::class) @Throws(InterruptedException::class, TimeoutException::class, CouldNotConfirmWriteException::class)
fun confirmWrite(expectedPayload: ByteArray, timeoutMs: Int) { fun confirmWrite(expectedPayload: ByteArray, timeoutMs: Long) {
val received: CharacteristicWriteConfirmation = writeQueue.poll(timeoutMs.toLong(), TimeUnit.MILLISECONDS) val received: CharacteristicWriteConfirmation = writeQueue.poll(timeoutMs, TimeUnit.MILLISECONDS)
?: throw TimeoutException() ?: throw TimeoutException()
when (received) { when (received) {
@ -116,9 +116,9 @@ class BleCommCallbacks(
} }
@Throws(InterruptedException::class, CouldNotConfirmDescriptorWriteException::class) @Throws(InterruptedException::class, CouldNotConfirmDescriptorWriteException::class)
fun confirmWriteDescriptor(descriptorUUID: String, timeoutMs: Int) { fun confirmWriteDescriptor(descriptorUUID: String, timeoutMs: Long) {
val confirmed: DescriptorWriteConfirmation = descriptorWriteQueue.poll( val confirmed: DescriptorWriteConfirmation = descriptorWriteQueue.poll(
timeoutMs.toLong(), timeoutMs,
TimeUnit.MILLISECONDS TimeUnit.MILLISECONDS
) )
?: throw TimeoutException() ?: throw TimeoutException()

View file

@ -30,14 +30,14 @@ class BleIO(
* @return a byte array with the received data * @return a byte array with the received data
*/ */
@Throws(BleIOBusyException::class, InterruptedException::class, TimeoutException::class) @Throws(BleIOBusyException::class, InterruptedException::class, TimeoutException::class)
fun receivePacket(characteristic: CharacteristicType): ByteArray { fun receivePacket(characteristic: CharacteristicType, timeoutMs:Long = DEFAULT_IO_TIMEOUT_MS): ByteArray {
synchronized(state) { synchronized(state) {
if (state != IOState.IDLE) { if (state != IOState.IDLE) {
throw BleIOBusyException() throw BleIOBusyException()
} }
state = IOState.READING state = IOState.READING
} }
val ret = incomingPackets[characteristic]?.poll(DEFAULT_IO_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS) val ret = incomingPackets[characteristic]?.poll(timeoutMs.toLong(), TimeUnit.MILLISECONDS)
?: throw TimeoutException() ?: throw TimeoutException()
synchronized(state) { state = IOState.IDLE } synchronized(state) { state = IOState.IDLE }
return ret return ret
@ -129,6 +129,6 @@ class BleIO(
companion object { companion object {
private const val DEFAULT_IO_TIMEOUT_MS = 2000 private const val DEFAULT_IO_TIMEOUT_MS = 2000.toLong()
} }
} }

View file

@ -24,33 +24,34 @@ class MessageIO(private val aapsLogger: AAPSLogger, private val bleIO: BleIO) {
throw UnexpectedCommandException(actual) throw UnexpectedCommandException(actual)
} }
fun peekForNack(index: Int, packets: List<BlePacket>) { private fun peekForNack(index: Int, packets: List<BlePacket>) {
bleIO.peekCommand()?.let { val peekCmd = bleIO.peekCommand() ?: return
if (it.isEmpty()) {
throw UnexpectedCommandException(BleCommand(it)) if (peekCmd.isEmpty()) {
throw UnexpectedCommandException(BleCommand(peekCmd))
} }
when (BleCommandType.byValue(it[0])) { when (BleCommandType.byValue(peekCmd[0])) {
BleCommandType.NACK -> { BleCommandType.NACK -> {
if (it.size < 2) { if (peekCmd.size < 2) {
throw UnexpectedCommandException(BleCommand(it)) throw UnexpectedCommandException(BleCommand(peekCmd))
} }
val missingIdx = it[1] val missingIdx = peekCmd[1]
if (missingIdx > packets.size) { if (missingIdx > packets.size) {
throw UnexpectedCommandException(BleCommand(it)) throw UnexpectedCommandException(BleCommand(peekCmd))
} }
bleIO.receivePacket(CharacteristicType.CMD) //consume NACK
bleIO.sendAndConfirmPacket(CharacteristicType.DATA, packets[missingIdx.toInt()].toByteArray()) bleIO.sendAndConfirmPacket(CharacteristicType.DATA, packets[missingIdx.toInt()].toByteArray())
} }
BleCommandType.SUCCESS -> { BleCommandType.SUCCESS -> {
if (index != packets.size - 1) { if (index != packets.size - 1) {
throw UnexpectedCommandException(BleCommand(it)) throw UnexpectedCommandException(BleCommand(peekCmd))
} }
} }
else -> else ->
throw UnexpectedCommandException(BleCommand(it)) throw UnexpectedCommandException(BleCommand(peekCmd))
}
} }
} }
@ -79,12 +80,8 @@ class MessageIO(private val aapsLogger: AAPSLogger, private val bleIO: BleIO) {
expectCommandType(BleCommand(expectSuccess), BleCommandSuccess()) expectCommandType(BleCommand(expectSuccess), BleCommandSuccess())
} }
// TODO: use higher timeout when receiving the first packet in a message fun receiveMessage(): MessagePacket {
fun receiveMessage(firstCmd: ByteArray? = null): MessagePacket { val expectRTS = bleIO.receivePacket(CharacteristicType.CMD, MESSAGE_READ_TIMEOUT_MS)
var expectRTS = firstCmd
if (expectRTS == null) {
expectRTS = bleIO.receivePacket(CharacteristicType.CMD)
}
expectCommandType(BleCommand(expectRTS), BleCommandRTS()) expectCommandType(BleCommand(expectRTS), BleCommandRTS())
bleIO.sendAndConfirmPacket(CharacteristicType.CMD, BleCommandCTS().data) bleIO.sendAndConfirmPacket(CharacteristicType.CMD, BleCommandCTS().data)
try { try {
@ -108,4 +105,8 @@ class MessageIO(private val aapsLogger: AAPSLogger, private val bleIO: BleIO) {
throw MessageIOException(cause = e) throw MessageIOException(cause = e)
} }
} }
companion object {
private const val MESSAGE_READ_TIMEOUT_MS = 4000.toLong()
}
} }