dash ble: renames and fixes after testing

This commit is contained in:
Andrei Vereha 2021-03-11 21:27:18 +01:00
parent 8154e16c94
commit 2aed2005ef
8 changed files with 46 additions and 35 deletions

View file

@ -53,9 +53,9 @@ class OmnipodDashManagerImpl @Inject constructor(
} // TODO are these reasonable values? } // TODO are these reasonable values?
private val observeScanAndActivateNewPod: Observable<PodEvent> private val observePairNewPod: Observable<PodEvent>
get() = Observable.defer { get() = Observable.defer {
bleManager.activateNewPod() bleManager.pairNewPod()
} // TODO are these reasonable values? } // TODO are these reasonable values?
private fun observeSendProgramBolusCommand( private fun observeSendProgramBolusCommand(
@ -177,7 +177,7 @@ class OmnipodDashManagerImpl @Inject constructor(
override fun activatePodPart1(lowReservoirAlertTrigger: AlertTrigger.ReservoirVolumeTrigger?): Observable<PodEvent> { override fun activatePodPart1(lowReservoirAlertTrigger: AlertTrigger.ReservoirVolumeTrigger?): Observable<PodEvent> {
return Observable.concat( return Observable.concat(
observePodReadyForActivationPart1, observePodReadyForActivationPart1,
observeScanAndActivateNewPod, observePairNewPod,
observeActivationPart1Commands(lowReservoirAlertTrigger) observeActivationPart1Commands(lowReservoirAlertTrigger)
).doOnComplete(ActivationProgressUpdater(ActivationProgress.PHASE_1_COMPLETED)) ).doOnComplete(ActivationProgressUpdater(ActivationProgress.PHASE_1_COMPLETED))
// TODO these would be common for any observable returned in a public function in this class // 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 -> { is PodEvent.BluetoothConnected -> {
podStateManager.bluetoothAddress = event.bluetoothAddress
} }
is PodEvent.Connected -> { is PodEvent.Connected -> {

View file

@ -13,7 +13,7 @@ interface OmnipodDashBleManager {
fun connect(): Observable<PodEvent> fun connect(): Observable<PodEvent>
fun activateNewPod(): Observable<PodEvent> fun pairNewPod(): Observable<PodEvent>
fun disconnect() fun disconnect()
} }

View file

@ -47,6 +47,10 @@ class OmnipodDashBleManagerImpl @Inject constructor(
private var msgIO: MessageIO? = null private var msgIO: MessageIO? = null
private var gatt: BluetoothGatt? = null private var gatt: BluetoothGatt? = null
private var status: ConnectionStatus = ConnectionStatus.IDLE 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( @Throws(
FailedToConnectException::class, FailedToConnectException::class,
@ -106,8 +110,8 @@ class OmnipodDashBleManagerImpl @Inject constructor(
val session = Session( val session = Session(
aapsLogger = aapsLogger, aapsLogger = aapsLogger,
msgIO = mIO, msgIO = mIO,
myId = Id.fromInt(CONTROLLER_ID), myId = myId,
podId = Id.fromInt(CONTROLLER_ID).increment(), podId = podId,
sessionKeys = keys, sessionKeys = keys,
enDecrypt = enDecrypt enDecrypt = enDecrypt
) )
@ -144,7 +148,9 @@ class OmnipodDashBleManagerImpl @Inject constructor(
override fun connect(): Observable<PodEvent> = Observable.create { emitter -> override fun connect(): Observable<PodEvent> = Observable.create { emitter ->
try { 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 // check if already connected
val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) val podDevice = bluetoothAdapter.getRemoteDevice(podAddress)
val connectionState = bluetoothManager.getConnectionState(podDevice, BluetoothProfile.GATT) val connectionState = bluetoothManager.getConnectionState(podDevice, BluetoothProfile.GATT)
@ -154,6 +160,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(
emitter.onComplete() emitter.onComplete()
return@create return@create
} }
emitter.onNext(PodEvent.BluetoothConnecting) emitter.onNext(PodEvent.BluetoothConnecting)
if (msgIO != null) { if (msgIO != null) {
disconnect() disconnect()
@ -161,12 +168,10 @@ class OmnipodDashBleManagerImpl @Inject constructor(
val bleIO = connect(podDevice) val bleIO = connect(podDevice)
val mIO = MessageIO(aapsLogger, bleIO) val mIO = MessageIO(aapsLogger, bleIO)
msgIO = mIO msgIO = mIO
emitter.onNext(PodEvent.BluetoothConnected) emitter.onNext(PodEvent.BluetoothConnected(podAddress))
emitter.onNext(PodEvent.EstablishingSession) emitter.onNext(PodEvent.EstablishingSession)
establishSession(1.toByte()) establishSession(1.toByte())
emitter.onNext(PodEvent.Connected) emitter.onNext(PodEvent.Connected)
emitter.onComplete() emitter.onComplete()
@ -179,7 +184,6 @@ class OmnipodDashBleManagerImpl @Inject constructor(
private fun establishSession(msgSeq: Byte) { private fun establishSession(msgSeq: Byte) {
val mIO = msgIO ?: throw FailedToConnectException("connection lost") val mIO = msgIO ?: throw FailedToConnectException("connection lost")
val ltk: ByteArray = podState.ltk ?: throw FailedToConnectException("Missing LTK, activate the pod first") val ltk: ByteArray = podState.ltk ?: throw FailedToConnectException("Missing LTK, activate the pod first")
val myId = Id.fromInt(CONTROLLER_ID)
val uniqueId = podState.uniqueId val uniqueId = podState.uniqueId
val podId = uniqueId?.let { Id.fromLong(uniqueId) } val podId = uniqueId?.let { Id.fromLong(uniqueId) }
?: myId.increment() // pod not activated ?: myId.increment() // pod not activated
@ -197,46 +201,43 @@ class OmnipodDashBleManagerImpl @Inject constructor(
sessionKeys = keys sessionKeys = keys
} }
override fun activateNewPod(): Observable<PodEvent> = Observable.create { emitter -> override fun pairNewPod(): Observable<PodEvent> = Observable.create { emitter ->
try { try {
if (podState.ltk != null) { if (podState.ltk != null) {
throw PodAlreadyActivatedException() emitter.onNext(PodEvent.AlreadyPaired)
emitter.onComplete()
return@create
} }
aapsLogger.info(LTag.PUMPBTCOMM, "starting new pod activation") aapsLogger.info(LTag.PUMPBTCOMM, "starting new pod activation")
val podScanner = PodScanner(aapsLogger, bluetoothAdapter)
emitter.onNext(PodEvent.Scanning) emitter.onNext(PodEvent.Scanning)
val podScanner = PodScanner(aapsLogger, bluetoothAdapter)
val podAddress = podScanner.scanForPod( val podAddress = podScanner.scanForPod(
PodScanner.SCAN_FOR_SERVICE_UUID, PodScanner.SCAN_FOR_SERVICE_UUID,
PodScanner.POD_ID_NOT_ACTIVATED PodScanner.POD_ID_NOT_ACTIVATED
).scanResult.device.address ).scanResult.device.address
// For tests: this.podAddress = "B8:27:EB:1D:7E:BB";
podState.bluetoothAddress = podAddress podState.bluetoothAddress = podAddress
emitter.onNext(PodEvent.BluetoothConnecting) emitter.onNext(PodEvent.BluetoothConnecting)
val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) val podDevice = bluetoothAdapter.getRemoteDevice(podAddress)
val bleIO = connect(podDevice) val bleIO = connect(podDevice)
emitter.onNext(PodEvent.BluetoothConnected)
val mIO = MessageIO(aapsLogger, bleIO) val mIO = MessageIO(aapsLogger, bleIO)
val myId = Id.fromInt(CONTROLLER_ID) msgIO = mIO
val podId = myId.increment() emitter.onNext(PodEvent.BluetoothConnected(podAddress))
emitter.onNext(PodEvent.Pairing)
emitter.onNext(PodEvent.Pairing)
val ltkExchanger = LTKExchanger(aapsLogger, mIO, myId, podId, Id.fromLong(PodScanner.POD_ID_NOT_ACTIVATED)) val ltkExchanger = LTKExchanger(aapsLogger, mIO, myId, podId, Id.fromLong(PodScanner.POD_ID_NOT_ACTIVATED))
val pairResult = ltkExchanger.negotiateLTK() val pairResult = ltkExchanger.negotiateLTK()
podState.ltk = pairResult.ltk
podState.eapAkaSequenceNumber = 1
emitter.onNext(PodEvent.Paired(podId)) emitter.onNext(PodEvent.Paired(podId))
podState.uniqueId = podId.toLong() podState.updateFromPairing(podId, pairResult)
val msgSeq = pairResult.msgSeq
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
aapsLogger.info(LTag.PUMPCOMM, "Got LTK: ${pairResult.ltk.toHex()}") 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() emitter.onComplete()
} catch (ex: Exception) { } catch (ex: Exception) {
disconnect() disconnect()

View file

@ -1,4 +0,0 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
class PodAlreadyActivatedException: Exception("The pod is already activated") {
}

View file

@ -8,9 +8,10 @@ sealed class PodEvent {
/* BT connection events */ /* BT connection events */
class AlreadyConnected(val bluetoothAddress: String) : PodEvent() class AlreadyConnected(val bluetoothAddress: String) : PodEvent()
object AlreadyPaired : PodEvent()
object Scanning : PodEvent() object Scanning : PodEvent()
object BluetoothConnecting : PodEvent() object BluetoothConnecting : PodEvent()
object BluetoothConnected : PodEvent() class BluetoothConnected(val bluetoothAddress: String) : PodEvent()
object Pairing : PodEvent() object Pairing : PodEvent()
class Paired(val uniqueId: Id) : PodEvent() class Paired(val uniqueId: Id) : PodEvent()
object EstablishingSession : PodEvent() object EstablishingSession : PodEvent()

View file

@ -1,5 +1,7 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state 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.definition.*
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.DefaultStatusResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.DefaultStatusResponse
@ -54,6 +56,7 @@ interface OmnipodDashPodStateManager {
fun updateFromVersionResponse(response: VersionResponse) fun updateFromVersionResponse(response: VersionResponse)
fun updateFromSetUniqueIdResponse(response: SetUniqueIdResponse) fun updateFromSetUniqueIdResponse(response: SetUniqueIdResponse)
fun updateFromAlarmStatusResponse(response: AlarmStatusResponse) fun updateFromAlarmStatusResponse(response: AlarmStatusResponse)
fun updateFromPairing(uniqueId: Id, pairResult: PairResult)
fun reset() fun reset()
data class TempBasal(val startTime: Long, val rate: Double, val durationInMinutes: Short) : Serializable data class TempBasal(val startTime: Long, val rate: Double, val durationInMinutes: Short) : Serializable

View file

@ -6,6 +6,8 @@ import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.pump.omnipod.dash.EventOmnipodDashPumpValuesChanged 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.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.definition.*
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.DefaultStatusResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.DefaultStatusResponse
@ -248,6 +250,13 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
rxBus.send(EventOmnipodDashPumpValuesChanged()) rxBus.send(EventOmnipodDashPumpValuesChanged())
} }
override fun updateFromPairing(uniqueId: Id, pairResult: PairResult){
podState.eapAkaSequenceNumber = 1
podState.ltk = pairResult.ltk
podState.uniqueId = uniqueId.toLong()
}
override fun reset() { override fun reset() {
podState = PodState() podState = PodState()
store() store()

View file

@ -20,7 +20,7 @@ class EnDecryptTest {
Hex.decode("dda23c090a0a0a0a"), Hex.decode("dda23c090a0a0a0a"),
0 0
), ),
Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e"), Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e")
) )
val encryptedMessage = Hex.decode( val encryptedMessage = Hex.decode(
"54571101070003400242000002420001" + "54571101070003400242000002420001" +
@ -43,7 +43,7 @@ class EnDecryptTest {
Hex.decode("dda23c090a0a0a0a"), Hex.decode("dda23c090a0a0a0a"),
0 0
), ),
Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e"), Hex.decode("ba1283744b6de9fab6d9b77d95a71d6e")
) )
val encryptedMessage = Hex.decode( val encryptedMessage = Hex.decode(
"54571101070003400242000002420001" + "54571101070003400242000002420001" +