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) { override fun getPumpStatus(reason: String) {
// TODO history // TODO history
omnipodManager.getStatus(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE).blockingSubscribeBy( omnipodManager.getStatus(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE).blockingSubscribeBy(
onNext = { podEvent -> onNext = { podEvent ->
aapsLogger.debug( 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 info.nightscout.androidaps.utils.extensions.toHex
import java.nio.ByteBuffer import java.nio.ByteBuffer
data class Id(val address: ByteArray) { data class Id(val address: ByteArray) {
init { init {
require(address.size == 4) 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 val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
private var connection: Connection? = null private var connection: Connection? = null
private var status: ConnectionStatus = ConnectionStatus.IDLE private var status: ConnectionStatus = ConnectionStatus.IDLE
private val myId = Id.fromInt(CONTROLLER_ID) private val ids = Ids(podState)
private val uniqueId = podState.uniqueId
private val podId = uniqueId?.let(Id::fromLong)
?: myId.increment() // pod not activated
override fun sendCommand(cmd: Command, responseType: KClass<out Response>): Observable<PodEvent> = override fun sendCommand(cmd: Command, responseType: KClass<out Response>): Observable<PodEvent> =
Observable.create { emitter -> Observable.create { emitter ->
@ -143,18 +140,14 @@ class OmnipodDashBleManagerImpl @Inject constructor(
val ltk = assertPaired() val ltk = assertPaired()
val uniqueId = podState.uniqueId
val podId = uniqueId?.let { Id.fromLong(uniqueId) }
?: myId.increment() // pod not activated
var eapSqn = podState.increaseEapAkaSequenceNumber() var eapSqn = podState.increaseEapAkaSequenceNumber()
var newSqn = conn.establishSession(ltk, msgSeq, myId, podId, eapSqn) var newSqn = conn.establishSession(ltk, msgSeq, ids, eapSqn)
if (newSqn != null) { if (newSqn != null) {
aapsLogger.info(LTag.PUMPBTCOMM, "Updating EAP SQN to: $newSqn") aapsLogger.info(LTag.PUMPBTCOMM, "Updating EAP SQN to: $newSqn")
podState.eapAkaSequenceNumber = newSqn.toLong() podState.eapAkaSequenceNumber = newSqn.toLong()
newSqn = conn.establishSession(ltk, msgSeq, myId, podId, podState.increaseEapAkaSequenceNumber()) newSqn = conn.establishSession(ltk, msgSeq, ids, podState.increaseEapAkaSequenceNumber())
if (newSqn != null) { if (newSqn != null) {
throw SessionEstablishmentException("Received resynchronization SQN for the second time") throw SessionEstablishmentException("Received resynchronization SQN for the second time")
} }
@ -204,16 +197,11 @@ class OmnipodDashBleManagerImpl @Inject constructor(
val ltkExchanger = LTKExchanger( val ltkExchanger = LTKExchanger(
aapsLogger, aapsLogger,
conn.msgIO, conn.msgIO,
myId, ids,
podId,
Id.fromLong(
PodScanner
.POD_ID_NOT_ACTIVATED
)
) )
val pairResult = ltkExchanger.negotiateLTK() val pairResult = ltkExchanger.negotiateLTK()
emitter.onNext(PodEvent.Paired(podId)) emitter.onNext(PodEvent.Paired(ids.podId))
podState.updateFromPairing(podId, pairResult) podState.updateFromPairing(ids.podId, pairResult)
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
aapsLogger.info(LTag.PUMPCOMM, "Got LTK: ${pairResult.ltk.toHex()}") 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.AAPSLogger
import info.nightscout.androidaps.logging.LTag 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.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.MessageIOException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.PairingException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.PairingException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO 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( internal class LTKExchanger(
private val aapsLogger: AAPSLogger, private val aapsLogger: AAPSLogger,
private val msgIO: MessageIO, private val msgIO: MessageIO,
val myId: Id, private val ids: Ids,
val podId: Id,
val podAddress: Id
) { ) {
private val podAddress = Ids.notActivated()
private val keyExchange = KeyExchange(aapsLogger, X25519KeyGenerator(), RandomByteGenerator()) private val keyExchange = KeyExchange(aapsLogger, X25519KeyGenerator(), RandomByteGenerator())
private var seq: Byte = 1 private var seq: Byte = 1
@ -31,17 +30,17 @@ internal class LTKExchanger(
fun negotiateLTK(): PairResult { fun negotiateLTK(): PairResult {
val sp1sp2 = PairMessage( val sp1sp2 = PairMessage(
sequenceNumber = seq, sequenceNumber = seq,
source = myId, source = ids.myId,
destination = podAddress, destination = podAddress,
keys = arrayOf(SP1, SP2), keys = arrayOf(SP1, SP2),
payloads = arrayOf(podId.address, sp2()) payloads = arrayOf(ids.podId.address, sp2())
) )
throwOnSendError(sp1sp2.messagePacket, SP1+SP2) throwOnSendError(sp1sp2.messagePacket, SP1+SP2)
seq++ seq++
val sps1 = PairMessage( val sps1 = PairMessage(
sequenceNumber = seq, sequenceNumber = seq,
source = myId, source = ids.myId,
destination = podAddress, destination = podAddress,
keys = arrayOf(SPS1), keys = arrayOf(SPS1),
payloads = arrayOf(keyExchange.pdmPublic + keyExchange.pdmNonce) payloads = arrayOf(keyExchange.pdmPublic + keyExchange.pdmNonce)
@ -55,7 +54,7 @@ internal class LTKExchanger(
seq++ seq++
val sps2 = PairMessage( val sps2 = PairMessage(
sequenceNumber = seq, sequenceNumber = seq,
source = myId, source = ids.myId,
destination = podAddress, destination = podAddress,
keys = arrayOf(SPS2), keys = arrayOf(SPS2),
payloads = arrayOf(keyExchange.pdmConf) payloads = arrayOf(keyExchange.pdmConf)
@ -70,7 +69,7 @@ internal class LTKExchanger(
// send SP0GP0 // send SP0GP0
val sp0gp0 = PairMessage ( val sp0gp0 = PairMessage (
sequenceNumber = seq, sequenceNumber = seq,
source = myId, source = ids.myId,
destination = podAddress, destination = podAddress,
keys = arrayOf(SP0GP0), keys = arrayOf(SP0GP0),
payloads = arrayOf(ByteArray(0)) 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.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.pump.omnipod.dash.BuildConfig 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.ServiceDiscoverer
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.callbacks.BleCommCallbacks import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.callbacks.BleCommCallbacks
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.EnDecrypt import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.EnDecrypt
@ -132,8 +132,8 @@ class Connection(
return Connected return Connected
} }
fun establishSession(ltk: ByteArray, msgSeq: Byte, myId: Id, podID: Id, eapSqn: ByteArray): EapSqn? { fun establishSession(ltk: ByteArray, msgSeq: Byte, ids: Ids, eapSqn: ByteArray): EapSqn? {
val eapAkaExchanger = SessionEstablisher(aapsLogger, msgIO, ltk, eapSqn, myId, podID, msgSeq) val eapAkaExchanger = SessionEstablisher(aapsLogger, msgIO, ltk, eapSqn, ids, msgSeq)
return when (val keys = eapAkaExchanger.negotiateSessionKeys()) { return when (val keys = eapAkaExchanger.negotiateSessionKeys()) {
is SessionNegotiationResynchronization -> { is SessionNegotiationResynchronization -> {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
@ -153,7 +153,7 @@ class Connection(
keys.nonce, keys.nonce,
keys.ck keys.ck
) )
session = Session(aapsLogger, msgIO, myId, podID, sessionKeys = keys, enDecrypt = enDecrypt) session = Session(aapsLogger, msgIO, ids, sessionKeys = keys, enDecrypt = enDecrypt)
null 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.AAPSLogger
import info.nightscout.androidaps.logging.LTag 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.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.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.CouldNotParseResponseException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.IllegalResponseException 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( class Session(
private val aapsLogger: AAPSLogger, private val aapsLogger: AAPSLogger,
private val msgIO: MessageIO, private val msgIO: MessageIO,
private val myId: Id, private val ids: Ids,
private val podId: Id,
val sessionKeys: SessionKeys, val sessionKeys: SessionKeys,
val enDecrypt: EnDecrypt val enDecrypt: EnDecrypt
) { ) {
@ -130,8 +130,8 @@ class Session(
val msg = MessagePacket( val msg = MessagePacket(
type = MessageType.ENCRYPTED, type = MessageType.ENCRYPTED,
sequenceNumber = sessionKeys.msgSequenceNumber, sequenceNumber = sessionKeys.msgSequenceNumber,
source = myId, source = ids.myId,
destination = podId, destination = ids.podId,
payload = ByteArray(0), payload = ByteArray(0),
eqos = 0, eqos = 0,
ack = true, ack = true,
@ -151,8 +151,8 @@ class Session(
val msg = MessagePacket( val msg = MessagePacket(
type = MessageType.ENCRYPTED, type = MessageType.ENCRYPTED,
sequenceNumber = sessionKeys.msgSequenceNumber, sequenceNumber = sessionKeys.msgSequenceNumber,
source = myId, source = ids.myId,
destination = podId, destination = ids.podId,
payload = wrapped, payload = wrapped,
eqos = 1 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.AAPSLogger
import info.nightscout.androidaps.logging.LTag 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.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.endecrypt.Nonce
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.SessionEstablishmentException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.SessionEstablishmentException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO
@ -18,8 +19,7 @@ class SessionEstablisher(
private val msgIO: MessageIO, private val msgIO: MessageIO,
private val ltk: ByteArray, private val ltk: ByteArray,
private val eapSqn: ByteArray, private val eapSqn: ByteArray,
private val myId: Id, private val ids: Ids,
private val podId: Id,
private var msgSeq: Byte private var msgSeq: Byte
) { ) {
@ -84,8 +84,8 @@ class SessionEstablisher(
return MessagePacket( return MessagePacket(
type = MessageType.SESSION_ESTABLISHMENT, type = MessageType.SESSION_ESTABLISHMENT,
sequenceNumber = msgSeq, sequenceNumber = msgSeq,
source = myId, source = ids.myId,
destination = podId, destination = ids.podId,
payload = eapMsg.toByteArray() payload = eapMsg.toByteArray()
) )
} }
@ -190,8 +190,8 @@ class SessionEstablisher(
return MessagePacket( return MessagePacket(
type = MessageType.SESSION_ESTABLISHMENT, type = MessageType.SESSION_ESTABLISHMENT,
sequenceNumber = msgSeq, sequenceNumber = msgSeq,
source = myId, source = ids.myId,
destination = podId, destination = ids.podId,
payload = eapMsg.toByteArray() payload = eapMsg.toByteArray()
) )
} }