From bce3d3b6b9a1f7b628163d2b07dadddc3f116e74 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 2 May 2020 00:17:39 +0200 Subject: [PATCH] Verify RSv1 password --- .../dependencyInjection/AppComponent.kt | 3 +- .../dependencyInjection/AppModule.kt | 2 +- .../plugins/pump/danaR/DanaRPump.kt | 6 +- .../plugins/pump/danaRS/DanaRSPlugin.kt | 77 ++++++++++--------- .../plugins/pump/danaRS/services/BLEComm.kt | 21 +++-- app/src/main/res/values/strings.xml | 3 +- app/src/main/res/values/validator.xml | 2 + app/src/main/res/xml/pref_danars.xml | 21 ++--- 8 files changed, 76 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt index 91153bd867..0bb89f83c1 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt @@ -67,7 +67,8 @@ import javax.inject.Singleton PreferencesModule::class, OverviewModule::class, DataClassesModule::class, - SMSModule::class + SMSModule::class, + DanaRSCommModule::class ] ) interface AppComponent : AndroidInjector { diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt index 48b599c488..871f88a503 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -74,7 +74,7 @@ import info.nightscout.androidaps.utils.wizard.BolusWizard import info.nightscout.androidaps.utils.wizard.QuickWizardEntry import javax.inject.Singleton -@Module(includes = [AppModule.AppBindings::class, PluginsModule::class, DanaRSCommModule::class]) +@Module(includes = [AppModule.AppBindings::class, PluginsModule::class]) open class AppModule { @Provides diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPump.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPump.kt index b9a5509b54..8b50a1670a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPump.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPump.kt @@ -119,6 +119,7 @@ class DanaRPump @Inject constructor( // DanaRS specific var rsPassword = "" + var v3RSPump = false; // User settings var timeDisplayType = 0 @@ -219,7 +220,10 @@ class DanaRPump @Inject constructor( } val isPasswordOK: Boolean - get() = !(password != -1 && password != sp.getInt(R.string.key_danar_password, -1)) + get() = password == sp.getInt(R.string.key_danar_password, -2) + + val isRSPasswordOK: Boolean + get() = rsPassword.equals(sp.getString(R.string.key_danars_password, ""), ignoreCase = true) fun reset() { aapsLogger.debug(LTag.PUMP, "DanaRPump reset") diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.kt index 53062a3c93..7bed59fdfa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.kt @@ -81,7 +81,15 @@ class DanaRSPlugin @Inject constructor( override fun updatePreferenceSummary(pref: Preference) { super.updatePreferenceSummary(pref) - if (pref.key == resourceHelper.gs(R.string.key_danars_name)) pref.summary = sp.getString(R.string.key_danars_name, "") + + if (pref.key == resourceHelper.gs(R.string.key_danars_name)) { + val value = sp.getStringOrNull(R.string.key_danars_name, null) + pref.summary = value + ?: resourceHelper.gs(R.string.rileylink_error_address_not_set_short) + } + if (pref.key == resourceHelper.gs(R.string.key_danars_password)) { + if (danaRPump.v3RSPump) pref + } } override fun onStart() { @@ -204,7 +212,7 @@ class DanaRSPlugin @Inject constructor( // Pump interface override fun isInitialized(): Boolean { - return danaRPump.lastConnection > 0 && danaRPump.maxBasal > 0 + return danaRPump.lastConnection > 0 && danaRPump.maxBasal > 0 && danaRPump.isRSPasswordOK } override fun isSuspended(): Boolean { @@ -402,7 +410,6 @@ class DanaRSPlugin @Inject constructor( @Synchronized override fun setTempBasalPercent(percent: Int, durationInMinutes: Int, profile: Profile, enforceNew: Boolean): PumpEnactResult { - val pump = danaRPump val result = PumpEnactResult(injector) var percentAfterConstraint = constraintChecker.applyBasalPercentConstraints(Constraint(percent), profile).value() if (percentAfterConstraint < 0) { @@ -421,8 +428,8 @@ class DanaRSPlugin @Inject constructor( result.success = true result.isTempCancel = false result.comment = resourceHelper.gs(R.string.virtualpump_resultok) - result.duration = pump.tempBasalRemainingMin - result.percent = pump.tempBasalPercent + result.duration = danaRPump.tempBasalRemainingMin + result.percent = danaRPump.tempBasalPercent result.isPercent = true aapsLogger.debug(LTag.PUMP, "setTempBasalPercent: Correct value already set") return result @@ -435,13 +442,13 @@ class DanaRSPlugin @Inject constructor( val durationInHours = max(durationInMinutes / 60, 1) danaRSService?.tempBasal(percentAfterConstraint, durationInHours) ?: false } - if (connectionOK && pump.isTempBasalInProgress && pump.tempBasalPercent == percentAfterConstraint) { + if (connectionOK && danaRPump.isTempBasalInProgress && danaRPump.tempBasalPercent == percentAfterConstraint) { result.enacted = true result.success = true result.comment = resourceHelper.gs(R.string.virtualpump_resultok) result.isTempCancel = false - result.duration = pump.tempBasalRemainingMin - result.percent = pump.tempBasalPercent + result.duration = danaRPump.tempBasalRemainingMin + result.percent = danaRPump.tempBasalPercent result.isPercent = true aapsLogger.debug(LTag.PUMP, "setTempBasalPercent: OK") return result @@ -454,16 +461,15 @@ class DanaRSPlugin @Inject constructor( } @Synchronized private fun setHighTempBasalPercent(percent: Int): PumpEnactResult { - val pump = danaRPump val result = PumpEnactResult(injector) val connectionOK = danaRSService?.highTempBasal(percent) ?: false - if (connectionOK && pump.isTempBasalInProgress && pump.tempBasalPercent == percent) { + if (connectionOK && danaRPump.isTempBasalInProgress && danaRPump.tempBasalPercent == percent) { result.enacted = true result.success = true result.comment = resourceHelper.gs(R.string.virtualpump_resultok) result.isTempCancel = false - result.duration = pump.tempBasalRemainingMin - result.percent = pump.tempBasalPercent + result.duration = danaRPump.tempBasalRemainingMin + result.percent = danaRPump.tempBasalPercent result.isPercent = true aapsLogger.debug(LTag.PUMP, "setHighTempBasalPercent: OK") return result @@ -477,7 +483,6 @@ class DanaRSPlugin @Inject constructor( @Synchronized override fun setExtendedBolus(insulin: Double, durationInMinutes: Int): PumpEnactResult { - val pump = danaRPump var insulinAfterConstraint = constraintChecker.applyExtendedBolusConstraints(Constraint(insulin)).value() // needs to be rounded val durationInHalfHours = max(durationInMinutes / 30, 1) @@ -488,23 +493,23 @@ class DanaRSPlugin @Inject constructor( result.enacted = false result.success = true result.comment = resourceHelper.gs(R.string.virtualpump_resultok) - result.duration = pump.extendedBolusRemainingMinutes - result.absolute = pump.extendedBolusAbsoluteRate + result.duration = danaRPump.extendedBolusRemainingMinutes + result.absolute = danaRPump.extendedBolusAbsoluteRate result.isPercent = false result.isTempCancel = false - aapsLogger.debug(LTag.PUMP, "setExtendedBolus: Correct extended bolus already set. Current: " + pump.extendedBolusAmount + " Asked: " + insulinAfterConstraint) + aapsLogger.debug(LTag.PUMP, "setExtendedBolus: Correct extended bolus already set. Current: " + danaRPump.extendedBolusAmount + " Asked: " + insulinAfterConstraint) return result } val connectionOK = danaRSService?.extendedBolus(insulinAfterConstraint, durationInHalfHours) ?: false - if (connectionOK && pump.isExtendedInProgress && abs(pump.extendedBolusAbsoluteRate - insulinAfterConstraint) < pumpDescription.extendedBolusStep) { + if (connectionOK && danaRPump.isExtendedInProgress && abs(danaRPump.extendedBolusAbsoluteRate - insulinAfterConstraint) < pumpDescription.extendedBolusStep) { result.enacted = true result.success = true result.comment = resourceHelper.gs(R.string.virtualpump_resultok) result.isTempCancel = false - result.duration = pump.extendedBolusRemainingMinutes - result.absolute = pump.extendedBolusAbsoluteRate - result.bolusDelivered = pump.extendedBolusAmount + result.duration = danaRPump.extendedBolusRemainingMinutes + result.absolute = danaRPump.extendedBolusAbsoluteRate + result.bolusDelivered = danaRPump.extendedBolusAmount result.isPercent = false aapsLogger.debug(LTag.PUMP, "setExtendedBolus: OK") return result @@ -562,9 +567,8 @@ class DanaRSPlugin @Inject constructor( } override fun getJSONStatus(profile: Profile, profileName: String): JSONObject { - val pump = danaRPump val now = System.currentTimeMillis() - if (pump.lastConnection + 5 * 60 * 1000L < System.currentTimeMillis()) { + if (danaRPump.lastConnection + 5 * 60 * 1000L < System.currentTimeMillis()) { return JSONObject() } val pumpJson = JSONObject() @@ -572,13 +576,13 @@ class DanaRSPlugin @Inject constructor( val status = JSONObject() val extended = JSONObject() try { - battery.put("percent", pump.batteryRemaining) - status.put("status", if (pump.pumpSuspended) "suspended" else "normal") - status.put("timestamp", DateUtil.toISOString(pump.lastConnection)) + battery.put("percent", danaRPump.batteryRemaining) + status.put("status", if (danaRPump.pumpSuspended) "suspended" else "normal") + status.put("timestamp", DateUtil.toISOString(danaRPump.lastConnection)) extended.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION) - if (pump.lastBolusTime != 0L) { - extended.put("LastBolus", DateUtil.dateAndTimeString(pump.lastBolusTime)) - extended.put("LastBolusAmount", pump.lastBolusAmount) + if (danaRPump.lastBolusTime != 0L) { + extended.put("LastBolus", DateUtil.dateAndTimeString(danaRPump.lastBolusTime)) + extended.put("LastBolusAmount", danaRPump.lastBolusAmount) } val tb = treatmentsPlugin.getTempBasalFromHistory(now) if (tb != null) { @@ -601,7 +605,7 @@ class DanaRSPlugin @Inject constructor( pumpJson.put("battery", battery) pumpJson.put("status", status) pumpJson.put("extended", extended) - pumpJson.put("reservoir", pump.reservoirRemainingUnits.toInt()) + pumpJson.put("reservoir", danaRPump.reservoirRemainingUnits.toInt()) pumpJson.put("clock", DateUtil.toISOString(now)) } catch (e: JSONException) { aapsLogger.error("Unhandled exception", e) @@ -626,15 +630,14 @@ class DanaRSPlugin @Inject constructor( } override fun shortStatus(veryShort: Boolean): String { - val pump = danaRPump var ret = "" - if (pump.lastConnection != 0L) { - val agoMillis = System.currentTimeMillis() - pump.lastConnection + if (danaRPump.lastConnection != 0L) { + val agoMillis = System.currentTimeMillis() - danaRPump.lastConnection val agoMin = (agoMillis / 60.0 / 1000.0).toInt() ret += "LastConn: $agoMin minago\n" } - if (pump.lastBolusTime != 0L) - ret += "LastBolus: ${DecimalFormatter.to2Decimal(pump.lastBolusAmount)}U @${DateFormat.format("HH:mm", pump.lastBolusTime)}" + if (danaRPump.lastBolusTime != 0L) + ret += "LastBolus: ${DecimalFormatter.to2Decimal(danaRPump.lastBolusAmount)}U @${DateFormat.format("HH:mm", danaRPump.lastBolusTime)}" val activeTemp = treatmentsPlugin.getRealTempBasalFromHistory(System.currentTimeMillis()) if (activeTemp != null) @@ -645,10 +648,10 @@ class DanaRSPlugin @Inject constructor( ret += "Extended: $activeExtendedBolus\n" if (!veryShort) { - ret += "TDD: ${DecimalFormatter.to0Decimal(pump.dailyTotalUnits)} / ${pump.maxDailyTotalUnits} U" + ret += "TDD: ${DecimalFormatter.to0Decimal(danaRPump.dailyTotalUnits)} / ${danaRPump.maxDailyTotalUnits} U" } - ret += "Reserv: ${DecimalFormatter.to0Decimal(pump.reservoirRemainingUnits)} U" - ret += "Batt: ${pump.batteryRemaining}" + ret += "Reserv: ${DecimalFormatter.to0Decimal(danaRPump.reservoirRemainingUnits)} U" + ret += "Batt: ${danaRPump.batteryRemaining}" return ret } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt index 255a86d1f3..c3a60805d2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt @@ -12,6 +12,7 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.bus.RxBusWrapper 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.plugins.pump.danaR.DanaRPump @@ -22,6 +23,7 @@ import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRSMessageHashTabl import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSPairingSuccess +import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.extensions.notify import info.nightscout.androidaps.utils.extensions.waitMillis @@ -67,6 +69,7 @@ class BLEComm @Inject internal constructor( set(newValue) { bleEncryption.setEnhancedEncryption(newValue) field = newValue + danaRPump.v3RSPump = newValue } private var isEasyMode: Boolean = false private var isUnitUD: Boolean = false @@ -550,11 +553,19 @@ class BLEComm @Inject internal constructor( var pass: Int = (decryptedBuffer[size - 1].toInt() and 0x000000FF shl 8) + (decryptedBuffer[size - 2].toInt() and 0x000000FF) pass = pass xor 3463 danaRPump.rsPassword = Integer.toHexString(pass) - aapsLogger.debug(LTag.PUMPBTCOMM, "Pump user password: " + Integer.toHexString(pass)) - rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTED)) - isConnected = true - isConnecting = false - aapsLogger.debug(LTag.PUMPBTCOMM, "RS connected and status read") + aapsLogger.debug(LTag.PUMPBTCOMM, "Pump user password: " + danaRPump.rsPassword) + if (!danaRPump.isRSPasswordOK) { + aapsLogger.error(LTag.PUMPBTCOMM, "Wrong pump password") + rxBus.send(EventNewNotification(Notification(Notification.WRONG_PUMP_PASSWORD, resourceHelper.gs(R.string.wrongpumppassword), Notification.URGENT))) + disconnect("WrongPassword") + SystemClock.sleep(T.mins(1).msecs()) + } else { + rxBus.send(EventDismissNotification(Notification.WRONG_PUMP_PASSWORD)) + rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTED)) + isConnected = true + isConnecting = false + aapsLogger.debug(LTag.PUMPBTCOMM, "RS connected and status read") + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2fbd5522ea..315b0e66a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -379,6 +379,7 @@ Refill Suspend Connecting for %1$d s + Pump password (v1 only) Pump password Wrong pump password! Pump is busy @@ -805,7 +806,6 @@ DanaRS Dana Selected pump - Pair new pump Bolus speed danars_bolusspeed Set basal step to 0.01 U/h @@ -1844,4 +1844,5 @@ statuslights_copy_ns Copy NS settings (if exists)? statuslights_overview_advanced + danars_password diff --git a/app/src/main/res/values/validator.xml b/app/src/main/res/values/validator.xml index aef7116a48..4de9c5fab1 100644 --- a/app/src/main/res/values/validator.xml +++ b/app/src/main/res/values/validator.xml @@ -18,11 +18,13 @@ Must be 6 digit number Must be 12 characters of ABCDEF0123456789 Must be 8 characters of ABCDEF0123456789 + Must be 4 characters of ABCDEF0123456789 Not a minimum length Pin should be 3 to 6 digits, not same or in series ^\\d{4} ^[A-F0-9]{12}$ ^[A-F0-9]{8}$ + ^[A-F0-9]{4}$ \ No newline at end of file diff --git a/app/src/main/res/xml/pref_danars.xml b/app/src/main/res/xml/pref_danars.xml index baf7bc4405..1e35b65ee9 100644 --- a/app/src/main/res/xml/pref_danars.xml +++ b/app/src/main/res/xml/pref_danars.xml @@ -8,24 +8,19 @@ app:initialExpandedChildrenCount="0"> + android:key="@string/key_danars_name" + android:summary="" + android:title="@string/selectedpump"> - -