Medtrum: Improve failure modes;
- ReadDataPacket: Add crc check - BLEComm: Differentiate between retryable and non-retryable error - setBolus: Use readStatus to wait for bolus if any
This commit is contained in:
parent
41afd7cb42
commit
100be23cf4
8 changed files with 234 additions and 48 deletions
|
@ -1,12 +1,32 @@
|
||||||
package info.nightscout.pump.medtrum.comm
|
package info.nightscout.pump.medtrum.comm
|
||||||
|
|
||||||
|
import CrcUtils.calcCrc8
|
||||||
|
|
||||||
class ReadDataPacket(data: ByteArray) {
|
class ReadDataPacket(data: ByteArray) {
|
||||||
|
|
||||||
private var totalData = data.copyOfRange(0, data.size - 1) // Strip crc
|
private var totalData = data.copyOfRange(0, data.size - 1) // Strip crc
|
||||||
|
private var failed = false
|
||||||
private var dataSize: Byte = data[0]
|
private var dataSize: Byte = data[0]
|
||||||
|
private var sequenceNumber: Byte = data[3]
|
||||||
|
|
||||||
|
init {
|
||||||
|
val crcInitialChunk = calcCrc8(data.copyOfRange(0, data.size - 1), data.size - 1)
|
||||||
|
|
||||||
|
if (crcInitialChunk != data[data.size - 1]) {
|
||||||
|
failed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun addData(newData: ByteArray) {
|
fun addData(newData: ByteArray) {
|
||||||
totalData += newData.copyOfRange(4, newData.size - 1) // Strip header and crc
|
totalData += newData.copyOfRange(4, newData.size - 1) // Strip header and crc
|
||||||
|
sequenceNumber++
|
||||||
|
val crcNewChunk = calcCrc8(newData.copyOfRange(0, newData.size - 1), newData.size - 1)
|
||||||
|
if (crcNewChunk != newData[newData.size - 1]) {
|
||||||
|
failed = true
|
||||||
|
}
|
||||||
|
if (sequenceNumber != newData[3]) {
|
||||||
|
failed = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun allDataReceived(): Boolean {
|
fun allDataReceived(): Boolean {
|
||||||
|
@ -16,4 +36,8 @@ class ReadDataPacket(data: ByteArray) {
|
||||||
fun getData(): ByteArray {
|
fun getData(): ByteArray {
|
||||||
return totalData
|
return totalData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun failed(): Boolean {
|
||||||
|
return failed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package info.nightscout.pump.medtrum.comm
|
package info.nightscout.pump.medtrum.comm
|
||||||
|
|
||||||
class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) {
|
import CrcUtils.calcCrc8
|
||||||
|
|
||||||
private val CRC_8_TABLE: IntArray = intArrayOf(0, 155, 173, 54, 193, 90, 108, 247, 25, 130, 180, 47, 216, 67, 117, 238, 50, 169, 159, 4, 243, 104, 94, 197, 43, 176, 134, 29, 234, 113, 71, 220, 100, 255, 201, 82, 165, 62, 8, 147, 125, 230, 208, 75, 188, 39, 17, 138, 86, 205, 251, 96, 151, 12, 58, 161, 79, 212, 226, 121, 142, 21, 35, 184, 200, 83, 101, 254, 9, 146, 164, 63, 209, 74, 124, 231, 16, 139, 189, 38, 250, 97, 87, 204, 59, 160, 150, 13, 227, 120, 78, 213, 34, 185, 143, 20, 172, 55, 1, 154, 109, 246, 192, 91, 181, 46, 24, 131, 116, 239, 217, 66, 158, 5, 51, 168, 95, 196, 242, 105, 135, 28, 42, 177, 70, 221, 235, 112, 11, 144, 166, 61, 202, 81, 103, 252, 18, 137, 191, 36, 211, 72, 126, 229, 57, 162, 148, 15, 248, 99, 85, 206, 32, 187, 141, 22, 225, 122, 76, 215, 111, 244, 194, 89, 174, 53, 3, 152, 118, 237, 219, 64, 183, 44, 26, 129, 93, 198, 240, 107, 156, 7, 49, 170, 68, 223, 233, 114, 133, 30, 40, 179, 195, 88, 110, 245, 2, 153, 175, 52, 218, 65, 119, 236, 27, 128, 182, 45, 241, 106, 92, 199, 48, 171, 157, 6, 232, 115, 69, 222, 41, 178, 132, 31, 167, 60, 10, 145, 102, 253, 203, 80, 190, 37, 19, 136, 127, 228, 210, 73, 149, 14, 56, 163, 84, 207, 249, 98, 140, 23, 33, 186, 77, 214, 224, 123)
|
class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) {
|
||||||
|
|
||||||
private val packages = mutableListOf<ByteArray>()
|
private val packages = mutableListOf<ByteArray>()
|
||||||
private var index = 0
|
private var index = 0
|
||||||
|
@ -17,7 +17,7 @@ class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) {
|
||||||
)
|
)
|
||||||
|
|
||||||
var tmp: ByteArray = header + data.copyOfRange(1, data.size)
|
var tmp: ByteArray = header + data.copyOfRange(1, data.size)
|
||||||
val totalCommand: ByteArray = tmp + calcCrc8(tmp, tmp.size).toByte()
|
val totalCommand: ByteArray = tmp + calcCrc8(tmp, tmp.size)
|
||||||
|
|
||||||
if ((totalCommand.size - header.size) <= 15) {
|
if ((totalCommand.size - header.size) <= 15) {
|
||||||
packages.add(totalCommand + 0.toByte())
|
packages.add(totalCommand + 0.toByte())
|
||||||
|
@ -28,7 +28,7 @@ class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) {
|
||||||
while (remainingCommand.size > 15) {
|
while (remainingCommand.size > 15) {
|
||||||
header[3] = pkgIndex.toByte()
|
header[3] = pkgIndex.toByte()
|
||||||
tmp = header + remainingCommand.copyOfRange(0, 15)
|
tmp = header + remainingCommand.copyOfRange(0, 15)
|
||||||
packages.add(tmp + calcCrc8(tmp, tmp.size).toByte())
|
packages.add(tmp + calcCrc8(tmp, tmp.size))
|
||||||
|
|
||||||
remainingCommand = remainingCommand.copyOfRange(15, remainingCommand.size)
|
remainingCommand = remainingCommand.copyOfRange(15, remainingCommand.size)
|
||||||
pkgIndex = (pkgIndex + 1) % 256
|
pkgIndex = (pkgIndex + 1) % 256
|
||||||
|
@ -37,7 +37,7 @@ class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) {
|
||||||
// Add last package
|
// Add last package
|
||||||
header[3] = pkgIndex.toByte()
|
header[3] = pkgIndex.toByte()
|
||||||
tmp = header + remainingCommand
|
tmp = header + remainingCommand
|
||||||
packages.add(tmp + calcCrc8(tmp, tmp.size).toByte())
|
packages.add(tmp + calcCrc8(tmp, tmp.size))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,12 +53,4 @@ class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) {
|
||||||
fun allPacketsConsumed(): Boolean {
|
fun allPacketsConsumed(): Boolean {
|
||||||
return index >= packages.size
|
return index >= packages.size
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calcCrc8(value: ByteArray, size: Int): Int {
|
|
||||||
var crc8 = 0
|
|
||||||
for (i in 0 until size) {
|
|
||||||
crc8 = CRC_8_TABLE[(value[i].toInt() and 255) xor (crc8 and 255)] and 255
|
|
||||||
}
|
|
||||||
return crc8
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,6 @@ import info.nightscout.pump.medtrum.extension.toLong
|
||||||
import info.nightscout.pump.medtrum.util.MedtrumTimeUtil
|
import info.nightscout.pump.medtrum.util.MedtrumTimeUtil
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
typealias MaskHandler = (ByteArray, Int) -> Int
|
|
||||||
|
|
||||||
class NotificationPacket(val injector: HasAndroidInjector) {
|
class NotificationPacket(val injector: HasAndroidInjector) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,7 +79,7 @@ class NotificationPacket(val injector: HasAndroidInjector) {
|
||||||
private const val SIZE_UNUSED_LEGACY = 2
|
private const val SIZE_UNUSED_LEGACY = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
val maskHandlers: Map<Int, MaskHandler> = mapOf(
|
val maskHandlers: Map<Int, (ByteArray, Int) -> Int> = mapOf(
|
||||||
MASK_SUSPEND to ::handleSuspend,
|
MASK_SUSPEND to ::handleSuspend,
|
||||||
MASK_NORMAL_BOLUS to ::handleNormalBolus,
|
MASK_NORMAL_BOLUS to ::handleNormalBolus,
|
||||||
MASK_EXTENDED_BOLUS to ::handleExtendedBolus,
|
MASK_EXTENDED_BOLUS to ::handleExtendedBolus,
|
||||||
|
|
|
@ -40,7 +40,7 @@ interface BLECommCallback {
|
||||||
fun onBLEDisconnected()
|
fun onBLEDisconnected()
|
||||||
fun onNotification(notification: ByteArray)
|
fun onNotification(notification: ByteArray)
|
||||||
fun onIndication(indication: ByteArray)
|
fun onIndication(indication: ByteArray)
|
||||||
fun onSendMessageError(reason: String)
|
fun onSendMessageError(reason: String, isRetryAble: Boolean)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
@ -258,7 +258,11 @@ class BLEComm @Inject internal constructor(
|
||||||
mReadPacket?.addData(value)
|
mReadPacket?.addData(value)
|
||||||
}
|
}
|
||||||
if (mReadPacket?.allDataReceived() == true) {
|
if (mReadPacket?.allDataReceived() == true) {
|
||||||
mReadPacket?.getData()?.let { mCallback?.onIndication(it) }
|
if (mReadPacket?.failed() == true) {
|
||||||
|
mCallback?.onSendMessageError("ReadDataPacket failed", false)
|
||||||
|
} else {
|
||||||
|
mReadPacket?.getData()?.let { mCallback?.onIndication(it) }
|
||||||
|
}
|
||||||
mReadPacket = null
|
mReadPacket = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,7 +283,7 @@ class BLEComm @Inject internal constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mCallback?.onSendMessageError("onCharacteristicWrite failure")
|
mCallback?.onSendMessageError("onCharacteristicWrite failure", true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,7 +408,7 @@ class BLEComm @Inject internal constructor(
|
||||||
writeCharacteristic(uartWriteBTGattChar, value)
|
writeCharacteristic(uartWriteBTGattChar, value)
|
||||||
} else {
|
} else {
|
||||||
aapsLogger.error(LTag.PUMPBTCOMM, "sendMessage error in writePacket!")
|
aapsLogger.error(LTag.PUMPBTCOMM, "sendMessage error in writePacket!")
|
||||||
mCallback?.onSendMessageError("error in writePacket!")
|
mCallback?.onSendMessageError("error in writePacket!", false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -430,7 +434,7 @@ class BLEComm @Inject internal constructor(
|
||||||
aapsLogger.debug(LTag.PUMPBTCOMM, "writeCharacteristic: ${Arrays.toString(data)}")
|
aapsLogger.debug(LTag.PUMPBTCOMM, "writeCharacteristic: ${Arrays.toString(data)}")
|
||||||
val success = mBluetoothGatt?.writeCharacteristic(characteristic)
|
val success = mBluetoothGatt?.writeCharacteristic(characteristic)
|
||||||
if (success != true) {
|
if (success != true) {
|
||||||
mCallback?.onSendMessageError("Failed to write characteristic")
|
mCallback?.onSendMessageError("Failed to write characteristic", true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, WRITE_DELAY_MILLIS)
|
}, WRITE_DELAY_MILLIS)
|
||||||
|
|
|
@ -339,7 +339,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
|
|
||||||
if (!sendBolusCommand(insulin)) {
|
if (!sendBolusCommand(insulin)) {
|
||||||
aapsLogger.error(LTag.PUMPCOMM, "Failed to set bolus")
|
aapsLogger.error(LTag.PUMPCOMM, "Failed to set bolus")
|
||||||
commandQueue.loadEvents(null) // make sure if anything is delivered (which is highly unlikely at this point) we get it
|
commandQueue.readStatus(rh.gs(R.string.bolus_error), null) // make sure if anything is delivered (which is highly unlikely at this point) we get it
|
||||||
t.insulin = 0.0
|
t.insulin = 0.0
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -735,9 +735,9 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
currentState.onIndication(indication)
|
currentState.onIndication(indication)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSendMessageError(reason: String) {
|
override fun onSendMessageError(reason: String, isRetryAble: Boolean) {
|
||||||
aapsLogger.debug(LTag.PUMPCOMM, "<<<<< error during send message $reason")
|
aapsLogger.debug(LTag.PUMPCOMM, "<<<<< error during send message $reason")
|
||||||
currentState.onSendMessageError(reason)
|
currentState.onSendMessageError(reason, isRetryAble)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Service stuff */
|
/** Service stuff */
|
||||||
|
@ -822,10 +822,10 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
return responseSuccess
|
return responseSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onSendMessageError(reason: String) {
|
fun onSendMessageError(reason: String, isRetryAble: Boolean) {
|
||||||
aapsLogger.warn(LTag.PUMPCOMM, "onSendMessageError: " + this.toString() + "reason: $reason")
|
aapsLogger.warn(LTag.PUMPCOMM, "onSendMessageError: " + this.toString() + "reason: $reason")
|
||||||
// Retry 3 times
|
// Retry 3 times
|
||||||
if (sendRetryCounter < 3) {
|
if (sendRetryCounter < 3 && isRetryAble) {
|
||||||
sendRetryCounter++
|
sendRetryCounter++
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
object CrcUtils {
|
||||||
|
|
||||||
|
private val lookupTable: UByteArray = ubyteArrayOf(0u, 155u, 173u, 54u, 193u, 90u, 108u, 247u, 25u, 130u, 180u, 47u, 216u, 67u, 117u, 238u, 50u, 169u, 159u, 4u, 243u, 104u, 94u, 197u, 43u, 176u, 134u, 29u, 234u, 113u, 71u, 220u, 100u, 255u, 201u, 82u, 165u, 62u, 8u, 147u, 125u, 230u, 208u, 75u, 188u, 39u, 17u, 138u, 86u, 205u, 251u, 96u, 151u, 12u, 58u, 161u, 79u, 212u, 226u, 121u, 142u, 21u, 35u, 184u, 200u, 83u, 101u, 254u, 9u, 146u, 164u, 63u, 209u, 74u, 124u, 231u, 16u, 139u, 189u, 38u, 250u, 97u, 87u, 204u, 59u, 160u, 150u, 13u, 227u, 120u, 78u, 213u, 34u, 185u, 143u, 20u, 172u, 55u, 1u, 154u, 109u, 246u, 192u, 91u, 181u, 46u, 24u, 131u, 116u, 239u, 217u, 66u, 158u, 5u, 51u, 168u, 95u, 196u, 242u, 105u, 135u, 28u, 42u, 177u, 70u, 221u, 235u, 112u, 11u, 144u, 166u, 61u, 202u, 81u, 103u, 252u, 18u, 137u, 191u, 36u, 211u, 72u, 126u, 229u, 57u, 162u, 148u, 15u, 248u, 99u, 85u, 206u, 32u, 187u, 141u, 22u, 225u, 122u, 76u, 215u, 111u, 244u, 194u, 89u, 174u, 53u, 3u, 152u, 118u, 237u, 219u, 64u, 183u, 44u, 26u, 129u, 93u, 198u, 240u, 107u, 156u, 7u, 49u, 170u, 68u, 223u, 233u, 114u, 133u, 30u, 40u, 179u, 195u, 88u, 110u, 245u, 2u, 153u, 175u, 52u, 218u, 65u, 119u, 236u, 27u, 128u, 182u, 45u, 241u, 106u, 92u, 199u, 48u, 171u, 157u, 6u, 232u, 115u, 69u, 222u, 41u, 178u, 132u, 31u, 167u, 60u, 10u, 145u, 102u, 253u, 203u, 80u, 190u, 37u, 19u, 136u, 127u, 228u, 210u, 73u, 149u, 14u, 56u, 163u, 84u, 207u, 249u, 98u, 140u, 23u, 33u, 186u, 77u, 214u, 224u, 123u)
|
||||||
|
|
||||||
|
fun calcCrc8(value: ByteArray, size: Int): Byte {
|
||||||
|
var crc8: UByte = 0u
|
||||||
|
for (i in 0 until size) {
|
||||||
|
val tableIndex: UByte = (value[i].toUByte() xor crc8)
|
||||||
|
crc8 = lookupTable[tableIndex.toInt()]
|
||||||
|
}
|
||||||
|
return crc8.toByte()
|
||||||
|
}
|
||||||
|
}
|
|
@ -80,6 +80,7 @@
|
||||||
<string name="alarm_battery_out">Battery out</string>
|
<string name="alarm_battery_out">Battery out</string>
|
||||||
<string name="alarm_no_calibration">No calibration</string>
|
<string name="alarm_no_calibration">No calibration</string>
|
||||||
<string name="pump_time_update_failed">Failed to update pump timezone, snooze message and refresh manually.</string>
|
<string name="pump_time_update_failed">Failed to update pump timezone, snooze message and refresh manually.</string>
|
||||||
|
<string name="bolus_error">Bolus error</string>
|
||||||
|
|
||||||
<!-- wizard-->
|
<!-- wizard-->
|
||||||
<string name="retry">Retry</string>
|
<string name="retry">Retry</string>
|
||||||
|
|
|
@ -0,0 +1,154 @@
|
||||||
|
package info.nightscout.pump.medtrum.comm
|
||||||
|
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class ReadDataPacketTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenCorrectBytesExpectPacketNotFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(51, 99, 10, 1, 0, 0, -86, 44, 1, -1, -85, 21, -108, -62, 1, 0, 22, 0, 1, 75)
|
||||||
|
val chunk2 = byteArrayOf(51, 99, 10, 2, 0, 0, 0, -80, -116, 84, 18, 10, 0, 10, 0, 0, 0, 0, 0, -10)
|
||||||
|
val chunk3 = byteArrayOf(51, 99, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -61, -59, -120, 5)
|
||||||
|
val chunk4 = byteArrayOf(51, 99, 10, 4, 19, -82, -80)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
packet.addData(chunk2)
|
||||||
|
packet.addData(chunk3)
|
||||||
|
packet.addData(chunk4)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.getData()).isEqualTo(
|
||||||
|
byteArrayOf(
|
||||||
|
51, 99, 10, 1, 0, 0, -86, 44, 1, -1, -85, 21, -108, -62, 1, 0, 22, 0, 1, 0, 0, 0, -80,
|
||||||
|
-116, 84, 18, 10, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -61, -59, -120, 19, -82
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertThat(packet.failed()).isFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenIncorrectCRCInFirstChunkExpectPacketFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(51, 99, 10, 1, 0, 0, -86, 44, 1, -1, -85, 21, -1, -62, -1, -1, 22, 0, 1, 75)
|
||||||
|
val chunk2 = byteArrayOf(51, 99, 10, 2, 0, 0, 0, -80, -116, 84, 18, 10, 0, 10, 0, 0, 0, 0, 0, -10)
|
||||||
|
val chunk3 = byteArrayOf(51, 99, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -61, -59, -120, 5)
|
||||||
|
val chunk4 = byteArrayOf(51, 99, 10, 4, 19, -82, -80)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
packet.addData(chunk2)
|
||||||
|
packet.addData(chunk3)
|
||||||
|
packet.addData(chunk4)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.failed()).isTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenIncorrectCRCInSecondChunkExpectPacketFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(51, 99, 10, 1, 0, 0, -86, 44, 1, -1, -85, 21, -108, -62, 1, 0, 22, 0, 1, 75)
|
||||||
|
val chunk2 = byteArrayOf(51, 99, 10, 2, 0, 0, 0, -80, -1, 84, 18, 10, 0, 10, -1, -1, 0, 0, 0, -10)
|
||||||
|
val chunk3 = byteArrayOf(51, 99, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -61, -59, -120, 5)
|
||||||
|
val chunk4 = byteArrayOf(51, 99, 10, 4, 19, -82, -80)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
packet.addData(chunk2)
|
||||||
|
packet.addData(chunk3)
|
||||||
|
packet.addData(chunk4)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.failed()).isTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenIncorrectCRCInThirdChunkExpectPacketFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(51, 99, 10, 1, 0, 0, -86, 44, 1, -1, -85, 21, -108, -62, 1, 0, 22, 0, 1, 75)
|
||||||
|
val chunk2 = byteArrayOf(51, 99, 10, 2, 0, 0, 0, -80, -116, 84, 18, 10, 0, 10, 0, 0, 0, 0, 0, -10)
|
||||||
|
val chunk3 = byteArrayOf(51, 99, 10, 3, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -61, -59, -120, 5)
|
||||||
|
val chunk4 = byteArrayOf(51, 99, 10, 4, 19, -82, -80)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
packet.addData(chunk2)
|
||||||
|
packet.addData(chunk3)
|
||||||
|
packet.addData(chunk4)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.failed()).isTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenIncorrectCRCInLastChunkExpectPacketFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(51, 99, 10, 1, 0, 0, -86, 44, 1, -1, -85, 21, -108, -62, 1, 0, 22, 0, 1, 75)
|
||||||
|
val chunk2 = byteArrayOf(51, 99, 10, 2, 0, 0, 0, -80, -116, 84, 18, 10, 0, 10, 0, 0, 0, 0, 0, -10)
|
||||||
|
val chunk3 = byteArrayOf(51, 99, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -61, -59, -120, 5)
|
||||||
|
val chunk4 = byteArrayOf(51, 99, 10, -1, -1, -82, -80)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
packet.addData(chunk2)
|
||||||
|
packet.addData(chunk3)
|
||||||
|
packet.addData(chunk4)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.failed()).isTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenIncorrectSequenceExpectPacketFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(51, 99, 10, 1, 0, 0, -86, 44, 1, -1, -85, 21, -108, -62, 1, 0, 22, 0, 1, 75)
|
||||||
|
val chunk2 = byteArrayOf(51, 99, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -61, -59, -120, 5)
|
||||||
|
val chunk3 = byteArrayOf(51, 99, 10, 2, 0, 0, 0, -80, -116, 84, 18, 10, 0, 10, 0, 0, 0, 0, 0, -10)
|
||||||
|
val chunk4 = byteArrayOf(51, 99, 10, 4, 19, -82, -80)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
packet.addData(chunk2)
|
||||||
|
packet.addData(chunk3)
|
||||||
|
packet.addData(chunk4)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.failed()).isTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenCorrectBytesOneChunkExpectPacketNotFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(14, 5, 0, 0, 0, 0, 2, 80, 1, 74, 64, 4, 0, -16, 0)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.getData()).isEqualTo(byteArrayOf(14, 5, 0, 0, 0, 0, 2, 80, 1, 74, 64, 4, 0, -16))
|
||||||
|
assertThat(packet.failed()).isFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenIncorrectBytesOneChunkExpectPacketFailed() {
|
||||||
|
// arrange
|
||||||
|
val chunk1 = byteArrayOf(14, 5, 0, -1, -1, -1, 2, 80, 1, 74, 64, 4, 0, -16, 0)
|
||||||
|
|
||||||
|
// act
|
||||||
|
val packet = ReadDataPacket(chunk1)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
assertThat(packet.allDataReceived()).isTrue()
|
||||||
|
assertThat(packet.failed()).isTrue()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue