RS allow more tries before resetting pairing

This commit is contained in:
Milos Kozak 2020-10-26 09:27:37 +01:00
parent 809652a89e
commit 4d4733a58a
2 changed files with 27 additions and 16 deletions

View file

@ -11,6 +11,7 @@
<string name="key_danars_v3_randomsynckey" translatable="false">danars_v3_randomsync_key_</string>
<string name="key_rs_logcanulachange" translatable="false">rs_logcanulachange</string>
<string name="key_rs_loginsulinchange" translatable="false">rs_loginsulinchange</string>
<string name="key_rs_last_clear_key_request" translatable="false">rs_last_clear_key_request</string>
<string name="key_danar_bt_name" translatable="false">danar_bt_name</string>
<string name="danars_pairing">Pairing</string>

View file

@ -6,7 +6,16 @@ import android.content.Intent
import android.os.SystemClock
import android.util.Base64
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.dana.DanaPump
import info.nightscout.androidaps.danars.DanaRSPlugin
import info.nightscout.androidaps.danars.R
import info.nightscout.androidaps.danars.activities.EnterPinActivity
import info.nightscout.androidaps.danars.activities.PairingHelperActivity
import info.nightscout.androidaps.danars.comm.DanaRSMessageHashTable
import info.nightscout.androidaps.danars.comm.DanaRS_Packet
import info.nightscout.androidaps.danars.comm.DanaRS_Packet_Etc_Keep_Connection
import info.nightscout.androidaps.danars.encryption.BleEncryption
import info.nightscout.androidaps.danars.events.EventDanaRSPairingSuccess
import info.nightscout.androidaps.events.EventPumpStatusChanged
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
@ -15,15 +24,7 @@ import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification
import info.nightscout.androidaps.dana.DanaPump
import info.nightscout.androidaps.danars.DanaRSPlugin
import info.nightscout.androidaps.danars.activities.EnterPinActivity
import info.nightscout.androidaps.danars.activities.PairingHelperActivity
import info.nightscout.androidaps.danars.comm.DanaRSMessageHashTable
import info.nightscout.androidaps.danars.comm.DanaRS_Packet
import info.nightscout.androidaps.danars.comm.DanaRS_Packet_Etc_Keep_Connection
import info.nightscout.androidaps.danars.encryption.BleEncryption
import info.nightscout.androidaps.danars.events.EventDanaRSPairingSuccess
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.extensions.notify
@ -133,12 +134,18 @@ class BLEComm @Inject internal constructor(
if (!encryptedDataRead && encryptedCommandSent && v3Encryption) {
// there was no response from pump after started encryption
// assume pairing keys are invalid
aapsLogger.error("Clearing pairing keys !!!")
sp.remove(resourceHelper.gs(R.string.key_danars_v3_randompairingkey) + danaRSPlugin.mDeviceName)
sp.remove(resourceHelper.gs(R.string.key_danars_v3_pairingkey) + danaRSPlugin.mDeviceName)
sp.remove(resourceHelper.gs(R.string.key_danars_v3_randomsynckey) + danaRSPlugin.mDeviceName)
ToastUtils.showToastInUiThread(context, R.string.invalidpairing)
danaRSPlugin.changePump()
val lastClearRequest = sp.getLong(R.string.key_rs_last_clear_key_request, 0)
if (lastClearRequest != 0L && DateUtil.isOlderThan(lastClearRequest, 5)) {
aapsLogger.error("Clearing pairing keys !!!")
sp.remove(resourceHelper.gs(R.string.key_danars_v3_randompairingkey) + danaRSPlugin.mDeviceName)
sp.remove(resourceHelper.gs(R.string.key_danars_v3_pairingkey) + danaRSPlugin.mDeviceName)
sp.remove(resourceHelper.gs(R.string.key_danars_v3_randomsynckey) + danaRSPlugin.mDeviceName)
ToastUtils.showToastInUiThread(context, R.string.invalidpairing)
danaRSPlugin.changePump()
} else if (lastClearRequest == 0L) {
aapsLogger.error("Clearing pairing keys postponed")
sp.putLong(R.string.key_rs_last_clear_key_request, DateUtil.now())
}
}
// cancel previous scheduled disconnection to prevent closing upcoming connection
scheduledDisconnection?.cancel(false)
@ -320,7 +327,10 @@ class BLEComm @Inject internal constructor(
// decrypt 2nd level after successful connection
val incomingBuffer = if (v3Encryption && isConnected)
bleEncryption.decryptSecondLevelPacket(receivedData).also { encryptedDataRead = true }
bleEncryption.decryptSecondLevelPacket(receivedData).also {
encryptedDataRead = true
sp.putLong(R.string.key_rs_last_clear_key_request, 0L)
}
else receivedData
addToReadBuffer(incomingBuffer)