diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt index 9712f43fac..ad09b225ff 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt @@ -17,7 +17,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response. import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager import info.nightscout.androidaps.utils.extensions.toHex import io.reactivex.Observable -import java.nio.ByteBuffer import java.util.concurrent.atomic.AtomicBoolean import javax.inject.Inject import javax.inject.Singleton @@ -85,7 +84,6 @@ class OmnipodDashBleManagerImpl @Inject constructor( } } - override fun getStatus(): ConnectionStatus { // TODO is this used? var s: ConnectionStatus diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt index 8bdd998020..f11be039c4 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt @@ -121,7 +121,7 @@ class BleCommCallbacks( val insertResult = incomingPackets.byCharacteristicType(characteristicType).add(payload) if (!insertResult) { - aapsLogger.warn(LTag.PUMPBTCOMM, "Could not insert read data to the incoming queue: ${characteristicType}") + aapsLogger.warn(LTag.PUMPBTCOMM, "Could not insert read data to the incoming queue: $characteristicType") } } 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 5daeeb6fc7..c544db8001 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 @@ -107,7 +107,7 @@ class MessageIO( maxMessageReadTries = joiner.fullFragments * 2 + 2 for (i in 1 until joiner.fullFragments + 1) { expected++ - val nackOnTimeout = !joiner.oneExtraPacket && i==joiner.fullFragments // last packet + val nackOnTimeout = !joiner.oneExtraPacket && i == joiner.fullFragments // last packet val packet = expectBlePacket(expected, nackOnTimeout) if (packet !is PacketReceiveSuccess) { aapsLogger.warn(LTag.PUMPBTCOMM, "Error reading packet:$packet") @@ -191,8 +191,11 @@ class MessageIO( if (received == null || received.isEmpty()) { if (nackOnTimeout) cmdBleIO.sendAndConfirmPacket(BleCommandNack(index).data) - aapsLogger.info(LTag.PUMPBTCOMM, "Error reading index: $index. Received: $received. NackOnTimeout: " + - "$nackOnTimeout") + aapsLogger.info( + LTag.PUMPBTCOMM, + "Error reading index: $index. Received: $received. NackOnTimeout: " + + "$nackOnTimeout" + ) continue } if (received[0] == index) { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt index ee2a98abc7..d332154094 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt @@ -5,7 +5,6 @@ import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothProfile import android.content.Context -import android.provider.ContactsContract import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.BuildConfig @@ -27,8 +26,11 @@ sealed class ConnectionState object Connected : ConnectionState() object NotConnected : ConnectionState() -class Connection(private val podDevice: BluetoothDevice, private val aapsLogger: AAPSLogger, context: Context) - : DisconnectHandler { +class Connection( + private val podDevice: BluetoothDevice, + private val aapsLogger: AAPSLogger, + context: Context +) : DisconnectHandler { private val incomingPackets = IncomingPackets() private val bleCommCallbacks = BleCommCallbacks(aapsLogger, incomingPackets, this) @@ -36,6 +38,7 @@ class Connection(private val podDevice: BluetoothDevice, private val aapsLogger: private val bluetoothManager: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager + // The session is Synchronized because we can lose the connection right when establishing it var session: Session? = null @Synchronized get @@ -138,6 +141,7 @@ class Connection(private val podDevice: BluetoothDevice, private val aapsLogger: } keys.synchronizedEapSqn } + is SessionKeys -> { if (BuildConfig.DEBUG) { aapsLogger.info(LTag.PUMPCOMM, "CK: ${keys.ck.toHex()}") diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/DisconnectHandler.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/DisconnectHandler.kt index e307b2e224..9b86447123 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/DisconnectHandler.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/DisconnectHandler.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session interface DisconnectHandler { + fun onConnectionLost(status: Int) -} \ No newline at end of file +} diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapAkaAttribute.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapAkaAttribute.kt index ea0d1f69b9..0f2fbe73d3 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapAkaAttribute.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapAkaAttribute.kt @@ -128,7 +128,7 @@ data class EapAkaAttributeAuts(val payload: ByteArray) : EapAkaAttribute() { companion object { fun parse(payload: ByteArray): EapAkaAttribute { - if (payload.size < SIZE-2) { + if (payload.size < SIZE - 2) { throw MessageIOException("Could not parse AUTS attribute: ${payload.toHex()}") } return EapAkaAttributeAuts(payload) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapSqn.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapSqn.kt index b2e4f6769e..8190f7ebb9 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapSqn.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapSqn.kt @@ -1,13 +1,12 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id import java.nio.ByteBuffer class EapSqn(val value: ByteArray) { - constructor(v: Long): this(fromLong(v)) + constructor(v: Long) : this(fromLong(v)) init { - require(value.size == SIZE) {"Eap SQN is $SIZE bytes long"} + require(value.size == SIZE) { "Eap SQN is $SIZE bytes long" } } fun increment(): EapSqn { @@ -17,7 +16,7 @@ class EapSqn(val value: ByteArray) { fun toLong(): Long { return ByteBuffer.wrap( byteArrayOf(0x00, 0x00) + - value + value ).long } @@ -26,10 +25,10 @@ class EapSqn(val value: ByteArray) { } companion object { + private const val SIZE = 6 private fun fromLong(v: Long): ByteArray { return ByteBuffer.allocate(8).putLong(v).array().copyOfRange(2, 8) } - } -} \ No newline at end of file +} diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Milenage.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Milenage.kt index dbab6ae991..a428165a8d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Milenage.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Milenage.kt @@ -20,9 +20,11 @@ class Milenage( init { require(k.size == KEY_SIZE) { "Milenage key has to be $KEY_SIZE bytes long. Received: ${k.toHex()}" } require(sqn.size == SQN) { "Milenage SQN has to be $SQN long. Received: ${sqn.toHex()}" } - require(auts.size == AUTS_SIZE) { "Milenage AUTS has to be $AUTS_SIZE long. Received: ${auts.toHex()}"} - require(amf.size == MILENAGE_AMF.size) { "Milenage AMF has to be ${MILENAGE_AMF.size} long." + - "Received: ${amf.toHex()}"} + require(auts.size == AUTS_SIZE) { "Milenage AUTS has to be $AUTS_SIZE long. Received: ${auts.toHex()}" } + require(amf.size == MILENAGE_AMF.size) { + "Milenage AMF has to be ${MILENAGE_AMF.size} long." + + "Received: ${amf.toHex()}" + } } private val secretKeySpec = SecretKeySpec(k, "AES") @@ -84,12 +86,14 @@ class Milenage( // Used for re-synchronisation AUTS = SQN^AK || MAC-S private val akStarInput = ByteArray(KEY_SIZE) + init { for (i in 0..15) { akStarInput[(i + 4) % 16] = randOpcEncryptedXorOpc[i] } akStarInput[15] = (akStarInput[15].toInt() xor 8).toByte() } + private val akStarFull = cipher.doFinal(akStarInput) xor opc private val akStar = akStarFull.copyOfRange(0, 6) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt index ab425b9dcf..bf5b78c316 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt @@ -137,7 +137,7 @@ class SessionEstablisher( if (eapMsg.attributes.size == 1 && eapMsg.attributes[0] is EapAkaAttributeClientErrorCode) { throw SessionEstablishmentException( "Received CLIENT_ERROR_CODE for EAP-AKA challenge: ${ - eapMsg.attributes[0].toByteArray().toHex() + eapMsg.attributes[0].toByteArray().toHex() }" ) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionKeys.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionKeys.kt index 4aa1a4b481..cd6ca1d2f1 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionKeys.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionKeys.kt @@ -4,11 +4,18 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecryp sealed class SessionNegotiationResponse -data class SessionKeys(val ck: ByteArray, val nonce: Nonce, var msgSequenceNumber: Byte):SessionNegotiationResponse() { +data class SessionKeys( + val ck: ByteArray, + val nonce: Nonce, + var msgSequenceNumber: Byte +) : SessionNegotiationResponse() { + init { require(ck.size == 16) { "CK has to be 16 bytes long" } } } -data class SessionNegotiationResynchronization(val synchronizedEapSqn: EapSqn, val msgSequenceNumber: Byte) - : SessionNegotiationResponse() \ No newline at end of file +data class SessionNegotiationResynchronization( + val synchronizedEapSqn: EapSqn, + val msgSequenceNumber: Byte +) : SessionNegotiationResponse() diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index dd073c8c50..8fcd23b213 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -16,7 +16,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response. import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.VersionResponse import info.nightscout.androidaps.utils.sharedPreferences.SP import java.io.Serializable -import java.nio.ByteBuffer import java.util.* import javax.inject.Inject import javax.inject.Singleton