diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt index 9fe2630d00..02de450881 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt @@ -53,9 +53,9 @@ class OmnipodDashManagerImpl @Inject constructor( } // TODO are these reasonable values? - private val observeScanAndActivateNewPod: Observable + private val observePairNewPod: Observable get() = Observable.defer { - bleManager.activateNewPod() + bleManager.pairNewPod() } // TODO are these reasonable values? private fun observeSendProgramBolusCommand( @@ -177,7 +177,7 @@ class OmnipodDashManagerImpl @Inject constructor( override fun activatePodPart1(lowReservoirAlertTrigger: AlertTrigger.ReservoirVolumeTrigger?): Observable { return Observable.concat( observePodReadyForActivationPart1, - observeScanAndActivateNewPod, + observePairNewPod, observeActivationPart1Commands(lowReservoirAlertTrigger) ).doOnComplete(ActivationProgressUpdater(ActivationProgress.PHASE_1_COMPLETED)) // TODO these would be common for any observable returned in a public function in this class @@ -415,6 +415,7 @@ class OmnipodDashManagerImpl @Inject constructor( } is PodEvent.BluetoothConnected -> { + podStateManager.bluetoothAddress = event.bluetoothAddress } is PodEvent.Connected -> { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt index 3dd9f9e174..814685ccd0 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt @@ -13,7 +13,7 @@ interface OmnipodDashBleManager { fun connect(): Observable - fun activateNewPod(): Observable + fun pairNewPod(): Observable fun disconnect() } 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 9b553e9146..d8fc543518 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 @@ -47,6 +47,10 @@ class OmnipodDashBleManagerImpl @Inject constructor( private var msgIO: MessageIO? = null private var gatt: BluetoothGatt? = 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(uniqueId) } + ?: myId.increment() // pod not activated @Throws( FailedToConnectException::class, @@ -106,8 +110,8 @@ class OmnipodDashBleManagerImpl @Inject constructor( val session = Session( aapsLogger = aapsLogger, msgIO = mIO, - myId = Id.fromInt(CONTROLLER_ID), - podId = Id.fromInt(CONTROLLER_ID).increment(), + myId = myId, + podId = podId, sessionKeys = keys, enDecrypt = enDecrypt ) @@ -144,7 +148,9 @@ class OmnipodDashBleManagerImpl @Inject constructor( override fun connect(): Observable = Observable.create { emitter -> try { - val podAddress = podState.bluetoothAddress ?: throw FailedToConnectException("Missing bluetoothAddress, activate the pod first") + val podAddress = + podState.bluetoothAddress + ?: throw FailedToConnectException("Missing bluetoothAddress, activate the pod first") // check if already connected val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) val connectionState = bluetoothManager.getConnectionState(podDevice, BluetoothProfile.GATT) @@ -154,6 +160,7 @@ class OmnipodDashBleManagerImpl @Inject constructor( emitter.onComplete() return@create } + emitter.onNext(PodEvent.BluetoothConnecting) if (msgIO != null) { disconnect() @@ -161,12 +168,10 @@ class OmnipodDashBleManagerImpl @Inject constructor( val bleIO = connect(podDevice) val mIO = MessageIO(aapsLogger, bleIO) msgIO = mIO - emitter.onNext(PodEvent.BluetoothConnected) + emitter.onNext(PodEvent.BluetoothConnected(podAddress)) emitter.onNext(PodEvent.EstablishingSession) - establishSession(1.toByte()) - emitter.onNext(PodEvent.Connected) emitter.onComplete() @@ -179,7 +184,6 @@ class OmnipodDashBleManagerImpl @Inject constructor( private fun establishSession(msgSeq: Byte) { val mIO = msgIO ?: throw FailedToConnectException("connection lost") val ltk: ByteArray = podState.ltk ?: throw FailedToConnectException("Missing LTK, activate the pod first") - val myId = Id.fromInt(CONTROLLER_ID) val uniqueId = podState.uniqueId val podId = uniqueId?.let { Id.fromLong(uniqueId) } ?: myId.increment() // pod not activated @@ -197,46 +201,43 @@ class OmnipodDashBleManagerImpl @Inject constructor( sessionKeys = keys } - override fun activateNewPod(): Observable = Observable.create { emitter -> + override fun pairNewPod(): Observable = Observable.create { emitter -> try { + if (podState.ltk != null) { - throw PodAlreadyActivatedException() + emitter.onNext(PodEvent.AlreadyPaired) + emitter.onComplete() + return@create } aapsLogger.info(LTag.PUMPBTCOMM, "starting new pod activation") - val podScanner = PodScanner(aapsLogger, bluetoothAdapter) emitter.onNext(PodEvent.Scanning) - + val podScanner = PodScanner(aapsLogger, bluetoothAdapter) val podAddress = podScanner.scanForPod( PodScanner.SCAN_FOR_SERVICE_UUID, PodScanner.POD_ID_NOT_ACTIVATED ).scanResult.device.address - // For tests: this.podAddress = "B8:27:EB:1D:7E:BB"; podState.bluetoothAddress = podAddress + emitter.onNext(PodEvent.BluetoothConnecting) val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) val bleIO = connect(podDevice) - emitter.onNext(PodEvent.BluetoothConnected) - val mIO = MessageIO(aapsLogger, bleIO) - val myId = Id.fromInt(CONTROLLER_ID) - val podId = myId.increment() - emitter.onNext(PodEvent.Pairing) + msgIO = mIO + emitter.onNext(PodEvent.BluetoothConnected(podAddress)) + emitter.onNext(PodEvent.Pairing) val ltkExchanger = LTKExchanger(aapsLogger, mIO, myId, podId, Id.fromLong(PodScanner.POD_ID_NOT_ACTIVATED)) val pairResult = ltkExchanger.negotiateLTK() - podState.ltk = pairResult.ltk - podState.eapAkaSequenceNumber = 1 emitter.onNext(PodEvent.Paired(podId)) - podState.uniqueId = podId.toLong() - val msgSeq = pairResult.msgSeq + podState.updateFromPairing(podId, pairResult) if (BuildConfig.DEBUG) { aapsLogger.info(LTag.PUMPCOMM, "Got LTK: ${pairResult.ltk.toHex()}") } - emitter.onNext(PodEvent.EstablishingSession) - establishSession(msgSeq) - emitter.onNext(PodEvent.Paired(podId)) + emitter.onNext(PodEvent.EstablishingSession) + establishSession(pairResult.msgSeq) + emitter.onNext(PodEvent.Connected) emitter.onComplete() } catch (ex: Exception) { disconnect() diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/PodAlreadyActivatedException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/PodAlreadyActivatedException.kt deleted file mode 100644 index 1564f85b85..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/PodAlreadyActivatedException.kt +++ /dev/null @@ -1,4 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions - -class PodAlreadyActivatedException: Exception("The pod is already activated") { -} \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt index 54720e1104..367393292e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt @@ -8,9 +8,10 @@ sealed class PodEvent { /* BT connection events */ class AlreadyConnected(val bluetoothAddress: String) : PodEvent() + object AlreadyPaired : PodEvent() object Scanning : PodEvent() object BluetoothConnecting : PodEvent() - object BluetoothConnected : PodEvent() + class BluetoothConnected(val bluetoothAddress: String) : PodEvent() object Pairing : PodEvent() class Paired(val uniqueId: Id) : PodEvent() object EstablishingSession : PodEvent() diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 75ec144593..51cab91974 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -1,5 +1,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.pair.PairResult import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.* import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.DefaultStatusResponse @@ -54,6 +56,7 @@ interface OmnipodDashPodStateManager { fun updateFromVersionResponse(response: VersionResponse) fun updateFromSetUniqueIdResponse(response: SetUniqueIdResponse) fun updateFromAlarmStatusResponse(response: AlarmStatusResponse) + fun updateFromPairing(uniqueId: Id, pairResult: PairResult) fun reset() data class TempBasal(val startTime: Long, val rate: Double, val durationInMinutes: Short) : Serializable 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 f8f05c729f..e92627b92e 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 @@ -6,6 +6,8 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.pump.omnipod.dash.EventOmnipodDashPumpValuesChanged import info.nightscout.androidaps.plugins.pump.omnipod.dash.R +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.pair.PairResult import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.* import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.DefaultStatusResponse @@ -248,6 +250,13 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( rxBus.send(EventOmnipodDashPumpValuesChanged()) } + override fun updateFromPairing(uniqueId: Id, pairResult: PairResult){ + podState.eapAkaSequenceNumber = 1 + podState.ltk = pairResult.ltk + podState.uniqueId = uniqueId.toLong() + } + + override fun reset() { podState = PodState() store() diff --git a/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/endecrypt/EnDecryptTest.kt b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/endecrypt/EnDecryptTest.kt index 04c0d38f8a..a85f9c9c1c 100644 --- a/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/endecrypt/EnDecryptTest.kt +++ b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/endecrypt/EnDecryptTest.kt @@ -20,7 +20,7 @@ class EnDecryptTest { Hex.decode("dda23c090a0a0a0a"), 0 ), - Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e"), + Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e") ) val encryptedMessage = Hex.decode( "54571101070003400242000002420001" + @@ -43,7 +43,7 @@ class EnDecryptTest { Hex.decode("dda23c090a0a0a0a"), 0 ), - Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e"), + Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e") ) val encryptedMessage = Hex.decode( "54571101070003400242000002420001" +