dash ble: detekt reduce number of arguments

This commit is contained in:
Andrei Vereha 2021-04-04 17:57:17 +02:00
parent 513007f6ae
commit 40ac1d189a
8 changed files with 51 additions and 44 deletions

View file

@ -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(

View file

@ -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)

View file

@ -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
)
}
}
}

View file

@ -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<out Response>): Observable<PodEvent> =
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()}")
}

View file

@ -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))

View file

@ -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
}
}

View file

@ -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
)

View file

@ -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()
)
}