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

View file

@ -30,14 +30,14 @@ class BleIO(
* @return a byte array with the received data
*/
@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) {
if (state != IOState.IDLE) {
throw BleIOBusyException()
}
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()
synchronized(state) { state = IOState.IDLE }
return ret
@ -129,6 +129,6 @@ class BleIO(
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)
}
fun peekForNack(index: Int, packets: List<BlePacket>) {
bleIO.peekCommand()?.let {
if (it.isEmpty()) {
throw UnexpectedCommandException(BleCommand(it))
private fun peekForNack(index: Int, packets: List<BlePacket>) {
val peekCmd = bleIO.peekCommand() ?: return
if (peekCmd.isEmpty()) {
throw UnexpectedCommandException(BleCommand(peekCmd))
}
when (BleCommandType.byValue(it[0])) {
when (BleCommandType.byValue(peekCmd[0])) {
BleCommandType.NACK -> {
if (it.size < 2) {
throw UnexpectedCommandException(BleCommand(it))
if (peekCmd.size < 2) {
throw UnexpectedCommandException(BleCommand(peekCmd))
}
val missingIdx = it[1]
val missingIdx = peekCmd[1]
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())
}
BleCommandType.SUCCESS -> {
if (index != packets.size - 1) {
throw UnexpectedCommandException(BleCommand(it))
throw UnexpectedCommandException(BleCommand(peekCmd))
}
}
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())
}
// TODO: use higher timeout when receiving the first packet in a message
fun receiveMessage(firstCmd: ByteArray? = null): MessagePacket {
var expectRTS = firstCmd
if (expectRTS == null) {
expectRTS = bleIO.receivePacket(CharacteristicType.CMD)
}
fun receiveMessage(): MessagePacket {
val expectRTS = bleIO.receivePacket(CharacteristicType.CMD, MESSAGE_READ_TIMEOUT_MS)
expectCommandType(BleCommand(expectRTS), BleCommandRTS())
bleIO.sendAndConfirmPacket(CharacteristicType.CMD, BleCommandCTS().data)
try {
@ -108,4 +105,8 @@ class MessageIO(private val aapsLogger: AAPSLogger, private val bleIO: BleIO) {
throw MessageIOException(cause = e)
}
}
companion object {
private const val MESSAGE_READ_TIMEOUT_MS = 4000.toLong()
}
}