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))
}
when (BleCommandType.byValue(it[0])) {
BleCommandType.NACK -> {
if (it.size < 2) {
throw UnexpectedCommandException(BleCommand(it))
}
val missingIdx = it[1]
if (missingIdx > packets.size) {
throw UnexpectedCommandException(BleCommand(it))
} if (peekCmd.isEmpty()) {
bleIO.sendAndConfirmPacket(CharacteristicType.DATA, packets[missingIdx.toInt()].toByteArray()) throw UnexpectedCommandException(BleCommand(peekCmd))
}
when (BleCommandType.byValue(peekCmd[0])) {
BleCommandType.NACK -> {
if (peekCmd.size < 2) {
throw UnexpectedCommandException(BleCommand(peekCmd))
} }
val missingIdx = peekCmd[1]
if (missingIdx > packets.size) {
throw UnexpectedCommandException(BleCommand(peekCmd))
BleCommandType.SUCCESS -> {
if (index != packets.size - 1) {
throw UnexpectedCommandException(BleCommand(it))
}
} }
bleIO.receivePacket(CharacteristicType.CMD) //consume NACK
else -> bleIO.sendAndConfirmPacket(CharacteristicType.DATA, packets[missingIdx.toInt()].toByteArray())
throw UnexpectedCommandException(BleCommand(it))
} }
BleCommandType.SUCCESS -> {
if (index != packets.size - 1) {
throw UnexpectedCommandException(BleCommand(peekCmd))
}
}
else ->
throw UnexpectedCommandException(BleCommand(peekCmd))
} }
} }
@ -67,24 +68,20 @@ class MessageIO(private val aapsLogger: AAPSLogger, private val bleIO: BleIO) {
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending DATA: ${packet.toByteArray().toHex()}") aapsLogger.debug(LTag.PUMPBTCOMM, "Sending DATA: ${packet.toByteArray().toHex()}")
bleIO.sendAndConfirmPacket(CharacteristicType.DATA, packet.toByteArray()) bleIO.sendAndConfirmPacket(CharacteristicType.DATA, packet.toByteArray())
peekForNack(index, packets) peekForNack(index, packets)
// This is implementing the same logic as the PDM. // This is implementing the same logic as the PDM.
// I think it wil not work in case of packet lost. // I think it wil not work in case of packet lost.
// This is because each lost packet, we will receive a NACK on the next packet. // This is because each lost packet, we will receive a NACK on the next packet.
// At the end, we will still be missing the last packet(s). // At the end, we will still be missing the last packet(s).
// I don't worry too much about this because for commands we have retries implemented at MessagePacket level anyway // I don't worry too much about this because for commands we have retries implemented at MessagePacket level anyway
// If this will be a problem in the future, the fix might be(pending testing with a real pod) to move back the index // If this will be a problem in the future, the fix might be(pending testing with a real pod) to move back the index
// at the value received in the NACK and make sure don't retry forever. // at the value received in the NACK and make sure don't retry forever.
} }
val expectSuccess = bleIO.receivePacket(CharacteristicType.CMD) val expectSuccess = bleIO.receivePacket(CharacteristicType.CMD)
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()
}
} }