This commit is contained in:
Andrei Vereha 2021-03-28 23:24:12 +02:00
parent 6c9aa90679
commit 189b791c5c
4 changed files with 11 additions and 12 deletions

View file

@ -17,13 +17,14 @@ data class BleCommandNack(val idx: Byte) : BleCommand(BleCommandType.NACK, byteA
companion object {
fun parse(payload: ByteArray): BleCommand {
if (payload.size < 2) {
return BleCommandIncorrect("Incorrect NACK payload", payload)
return when {
payload.size < 2 ->
BleCommandIncorrect("Incorrect NACK payload", payload)
payload[0] != BleCommandType.NACK.value ->
BleCommandIncorrect("Incorrect NACK header", payload)
else ->
BleCommandNack(payload[1])
}
if (payload[0] != BleCommandType.NACK.value) {
return BleCommandIncorrect("Incorrect NACK header", payload)
}
return BleCommandNack(payload[1])
}
}
}

View file

@ -2,4 +2,5 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.excepti
import info.nightscout.androidaps.utils.extensions.toHex
class CouldNotParseMessageException(val payload: ByteArray) : Exception("Could not parse message payload: ${payload.toHex()}")
class CouldNotParseMessageException(val payload: ByteArray) :
Exception("Could not parse message payload: ${payload.toHex()}")

View file

@ -30,9 +30,6 @@ class CmdBleIO(
CharacteristicType.CMD
) {
init {
}
fun peekCommand(): ByteArray? {
return incomingPackets.peek()
}

View file

@ -14,7 +14,7 @@ class StringLengthPrefixEncoding private constructor() {
private const val LENGTH_BYTES = 2
fun parseKeys(keys: Array<String>, payload: ByteArray): Array<ByteArray> {
val ret = Array<ByteArray>(keys.size, { ByteArray(0) })
val ret = Array<>(keys.size, { ByteArray(0) })
var remaining = payload
for ((index, key) in keys.withIndex()) {
when {
@ -50,7 +50,7 @@ class StringLengthPrefixEncoding private constructor() {
val k = keys[idx]
val payload = payloads[idx]
bb.put(k.toByteArray())
if (payload.size > 0) {
if (payload.isNotEmpty()) {
bb.putShort(payload.size.toShort())
bb.put(payload)
}