diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index f7e5a86ae4..ca291751d2 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -104,7 +104,6 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun getPumpStatus(reason: String) { // TODO history - omnipodManager.getStatus(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE).blockingSubscribeBy( onNext = { podEvent -> aapsLogger.debug( diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt index e330434faf..cccb42a989 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm import info.nightscout.androidaps.utils.extensions.toHex import java.nio.ByteBuffer + data class Id(val address: ByteArray) { init { require(address.size == 4) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Ids.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Ids.kt new file mode 100644 index 0000000000..ac3e2e8406 --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Ids.kt @@ -0,0 +1,20 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm + +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.scan.PodScanner +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager + +class Ids(podState: OmnipodDashPodStateManager) { + val myId = Id.fromInt(OmnipodDashBleManagerImpl.CONTROLLER_ID) + private val uniqueId = podState.uniqueId + val podId = uniqueId?.let(Id::fromLong) + ?: myId.increment() // pod not activated + + companion object { + fun notActivated(): Id { + return Id.fromLong( + PodScanner + .POD_ID_NOT_ACTIVATED + ) + } + } +} \ No newline at end of file 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 2967fc8646..55bddace08 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 @@ -35,10 +35,7 @@ class OmnipodDashBleManagerImpl @Inject constructor( private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter private var connection: Connection? = null private var status: ConnectionStatus = ConnectionStatus.IDLE - private val myId = Id.fromInt(CONTROLLER_ID) - private val uniqueId = podState.uniqueId - private val podId = uniqueId?.let(Id::fromLong) - ?: myId.increment() // pod not activated + private val ids = Ids(podState) override fun sendCommand(cmd: Command, responseType: KClass): Observable = Observable.create { emitter -> @@ -143,18 +140,14 @@ class OmnipodDashBleManagerImpl @Inject constructor( val ltk = assertPaired() - val uniqueId = podState.uniqueId - val podId = uniqueId?.let { Id.fromLong(uniqueId) } - ?: myId.increment() // pod not activated - var eapSqn = podState.increaseEapAkaSequenceNumber() - var newSqn = conn.establishSession(ltk, msgSeq, myId, podId, eapSqn) + var newSqn = conn.establishSession(ltk, msgSeq, ids, eapSqn) if (newSqn != null) { aapsLogger.info(LTag.PUMPBTCOMM, "Updating EAP SQN to: $newSqn") podState.eapAkaSequenceNumber = newSqn.toLong() - newSqn = conn.establishSession(ltk, msgSeq, myId, podId, podState.increaseEapAkaSequenceNumber()) + newSqn = conn.establishSession(ltk, msgSeq, ids, podState.increaseEapAkaSequenceNumber()) if (newSqn != null) { throw SessionEstablishmentException("Received resynchronization SQN for the second time") } @@ -204,16 +197,11 @@ class OmnipodDashBleManagerImpl @Inject constructor( val ltkExchanger = LTKExchanger( aapsLogger, conn.msgIO, - myId, - podId, - Id.fromLong( - PodScanner - .POD_ID_NOT_ACTIVATED - ) + ids, ) val pairResult = ltkExchanger.negotiateLTK() - emitter.onNext(PodEvent.Paired(podId)) - podState.updateFromPairing(podId, pairResult) + emitter.onNext(PodEvent.Paired(ids.podId)) + podState.updateFromPairing(ids.podId, pairResult) if (BuildConfig.DEBUG) { aapsLogger.info(LTag.PUMPCOMM, "Got LTK: ${pairResult.ltk.toHex()}") } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/pair/LTKExchanger.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/pair/LTKExchanger.kt index 0b60d6bb52..ef64c71ea7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/pair/LTKExchanger.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/pair/LTKExchanger.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.pair import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Ids import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.MessageIOException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.PairingException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO @@ -19,11 +20,9 @@ import info.nightscout.androidaps.utils.extensions.toHex internal class LTKExchanger( private val aapsLogger: AAPSLogger, private val msgIO: MessageIO, - val myId: Id, - val podId: Id, - val podAddress: Id + private val ids: Ids, ) { - + private val podAddress = Ids.notActivated() private val keyExchange = KeyExchange(aapsLogger, X25519KeyGenerator(), RandomByteGenerator()) private var seq: Byte = 1 @@ -31,17 +30,17 @@ internal class LTKExchanger( fun negotiateLTK(): PairResult { val sp1sp2 = PairMessage( sequenceNumber = seq, - source = myId, + source = ids.myId, destination = podAddress, keys = arrayOf(SP1, SP2), - payloads = arrayOf(podId.address, sp2()) + payloads = arrayOf(ids.podId.address, sp2()) ) throwOnSendError(sp1sp2.messagePacket, SP1+SP2) seq++ val sps1 = PairMessage( sequenceNumber = seq, - source = myId, + source = ids.myId, destination = podAddress, keys = arrayOf(SPS1), payloads = arrayOf(keyExchange.pdmPublic + keyExchange.pdmNonce) @@ -55,7 +54,7 @@ internal class LTKExchanger( seq++ val sps2 = PairMessage( sequenceNumber = seq, - source = myId, + source = ids.myId, destination = podAddress, keys = arrayOf(SPS2), payloads = arrayOf(keyExchange.pdmConf) @@ -70,7 +69,7 @@ internal class LTKExchanger( // send SP0GP0 val sp0gp0 = PairMessage ( sequenceNumber = seq, - source = myId, + source = ids.myId, destination = podAddress, keys = arrayOf(SP0GP0), payloads = arrayOf(ByteArray(0)) 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 d332154094..f71e625f4a 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 @@ -8,7 +8,7 @@ import android.content.Context import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.BuildConfig -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Ids import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.ServiceDiscoverer import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.callbacks.BleCommCallbacks import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.EnDecrypt @@ -132,8 +132,8 @@ class Connection( return Connected } - fun establishSession(ltk: ByteArray, msgSeq: Byte, myId: Id, podID: Id, eapSqn: ByteArray): EapSqn? { - val eapAkaExchanger = SessionEstablisher(aapsLogger, msgIO, ltk, eapSqn, myId, podID, msgSeq) + fun establishSession(ltk: ByteArray, msgSeq: Byte, ids: Ids, eapSqn: ByteArray): EapSqn? { + val eapAkaExchanger = SessionEstablisher(aapsLogger, msgIO, ltk, eapSqn, ids, msgSeq) return when (val keys = eapAkaExchanger.negotiateSessionKeys()) { is SessionNegotiationResynchronization -> { if (BuildConfig.DEBUG) { @@ -153,7 +153,7 @@ class Connection( keys.nonce, keys.ck ) - session = Session(aapsLogger, msgIO, myId, podID, sessionKeys = keys, enDecrypt = enDecrypt) + session = Session(aapsLogger, msgIO, ids, sessionKeys = keys, enDecrypt = enDecrypt) null } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt index 59b7fd825d..377e625dc8 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Ids import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.EnDecrypt import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotParseResponseException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.IllegalResponseException @@ -33,8 +34,7 @@ data class CommandAckError(val result: Response, val msg: String) : CommandRecei class Session( private val aapsLogger: AAPSLogger, private val msgIO: MessageIO, - private val myId: Id, - private val podId: Id, + private val ids: Ids, val sessionKeys: SessionKeys, val enDecrypt: EnDecrypt ) { @@ -130,8 +130,8 @@ class Session( val msg = MessagePacket( type = MessageType.ENCRYPTED, sequenceNumber = sessionKeys.msgSequenceNumber, - source = myId, - destination = podId, + source = ids.myId, + destination = ids.podId, payload = ByteArray(0), eqos = 0, ack = true, @@ -151,8 +151,8 @@ class Session( val msg = MessagePacket( type = MessageType.ENCRYPTED, sequenceNumber = sessionKeys.msgSequenceNumber, - source = myId, - destination = podId, + source = ids.myId, + destination = ids.podId, payload = wrapped, eqos = 1 ) 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 9f4ab8e347..355cab8daf 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 @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Ids import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.Nonce import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.SessionEstablishmentException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO @@ -18,8 +19,7 @@ class SessionEstablisher( private val msgIO: MessageIO, private val ltk: ByteArray, private val eapSqn: ByteArray, - private val myId: Id, - private val podId: Id, + private val ids: Ids, private var msgSeq: Byte ) { @@ -84,8 +84,8 @@ class SessionEstablisher( return MessagePacket( type = MessageType.SESSION_ESTABLISHMENT, sequenceNumber = msgSeq, - source = myId, - destination = podId, + source = ids.myId, + destination = ids.podId, payload = eapMsg.toByteArray() ) } @@ -190,8 +190,8 @@ class SessionEstablisher( return MessagePacket( type = MessageType.SESSION_ESTABLISHMENT, sequenceNumber = msgSeq, - source = myId, - destination = podId, + source = ids.myId, + destination = ids.podId, payload = eapMsg.toByteArray() ) }