diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTK.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTK.kt index 3784ae9ffa..1f83940f2e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTK.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTK.kt @@ -4,7 +4,7 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.BleIO data class LTK(val ltk: ByteArray, val noncePrefix: ByteArray) { - init{ + init { require(ltk.size == 16) require(noncePrefix.size == 16) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTKExchanger.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTKExchanger.kt index 0d7e255543..526d9cfc42 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTKExchanger.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/LTKExchanger.kt @@ -1,13 +1,11 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.ltk import info.nightscout.androidaps.logging.AAPSLogger -import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.OmnipodDashBleManagerImpl import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.Id import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.StringLengthPrefixEncoding import info.nightscout.androidaps.utils.extensions.hexStringToByteArray -import info.nightscout.androidaps.utils.extensions.toHex -import java.nio.ByteBuffer internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgIO: MessageIO) { @@ -48,8 +46,10 @@ internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgI } fun sp1sp2(sp1: ByteArray, sp2: ByteArray, seq: Byte, controllerId: Id, nodeId: Id): PairMessage { - val payload = "SP1=".toByteArray() + sp1 - ",SP2=".toByteArray() + sp2 + val payload = StringLengthPrefixEncoding.formatKeys( + arrayOf("SP1=", ",SP2="), + arrayOf(sp1, sp2), + ) return PairMessage( sequenceNumber = seq, source = controllerId, diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/PairMessage.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/PairMessage.kt index bb88c60831..99466e3b4e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/PairMessage.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ltk/PairMessage.kt @@ -13,7 +13,6 @@ data class PairMessage( type=MessageType.PAIRING, source=source, destination = destination, - encryptedPayload=false, payload=payload, sequenceNumber=sequenceNumber, sas=true, // TODO: understand why this is true for PairMessages diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt index 73f31a7f62..d64d693edc 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt @@ -26,7 +26,7 @@ class MessageIO(private val aapsLogger: AAPSLogger, private val bleIO: BleIO) { } // TODO: peek for NACKs val expectSuccess = bleIO.receivePacket(CharacteristicType.CMD) - if (BleCommand(expectSuccess) != BleCommandCTS()) { + if (BleCommand(expectSuccess) != BleCommandSuccess()) { throw UnexpectedCommandException(BleCommand(expectSuccess)) } // TODO: handle NACKS/FAILS/etc diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacket.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacket.kt index a918a2b551..f18cbc9369 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacket.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacket.kt @@ -1,34 +1,10 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message -import java.io.DataOutput import java.nio.ByteBuffer /*** * MessagePacket contains header and raw payload for a message */ - -class Flag:Byte{ - fun set(idx: Byte, val: Boolean) { - val mask = 1 ): List { TODO("not implemented") } - fun formatKeys(keys: List, payload: List, addLength: Boolean): ByteArray { - TODO("not implemented") + fun formatKeys(keys: Array, payloads: Array): ByteArray { + val payloadTotalSize = payloads.fold(0) { acc, i -> acc + i.size } + val keyTotalSize = keys.fold(0) { acc, i -> acc + i.length } + + val bb = ByteBuffer.allocate(2 * keys.size + keyTotalSize + payloadTotalSize) + for (idx in keys.indices) { + val k = keys[idx] + val payload = payloads[idx] + bb.put(k.toByteArray()) + bb.putShort(payload.size.toShort()) + bb.put(payload) + } + + val ret = ByteArray(bb.position()) + bb.flip() + bb.get(ret) + + return ret } } } \ No newline at end of file