korean rs fix

This commit is contained in:
Milos Kozak 2020-08-26 15:42:28 +02:00
parent f2d89ac428
commit 8619e4156e

View file

@ -51,6 +51,7 @@ class BLEComm @Inject internal constructor(
) { ) {
companion object { companion object {
private const val WRITE_DELAY_MILLIS: Long = 50 private const val WRITE_DELAY_MILLIS: Long = 50
private const val UART_READ_UUID = "0000fff1-0000-1000-8000-00805f9b34fb" private const val UART_READ_UUID = "0000fff1-0000-1000-8000-00805f9b34fb"
private const val UART_WRITE_UUID = "0000fff2-0000-1000-8000-00805f9b34fb" private const val UART_WRITE_UUID = "0000fff2-0000-1000-8000-00805f9b34fb"
@ -375,22 +376,22 @@ class BLEComm @Inject internal constructor(
if (decryptedBuffer[0] == BleEncryption.DANAR_PACKET__TYPE_ENCRYPTION_RESPONSE.toByte()) { if (decryptedBuffer[0] == BleEncryption.DANAR_PACKET__TYPE_ENCRYPTION_RESPONSE.toByte()) {
when (decryptedBuffer[1]) { when (decryptedBuffer[1]) {
// 1st packet exchange // 1st packet exchange
BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__PUMP_CHECK.toByte() -> BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__PUMP_CHECK.toByte() ->
processConnectResponse(decryptedBuffer) processConnectResponse(decryptedBuffer)
BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__TIME_INFORMATION.toByte() -> BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__TIME_INFORMATION.toByte() ->
processEncryptionResponse(decryptedBuffer) processEncryptionResponse(decryptedBuffer)
BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__CHECK_PASSKEY.toByte() -> BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__CHECK_PASSKEY.toByte() ->
processPasskeyCheck(decryptedBuffer) processPasskeyCheck(decryptedBuffer)
BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__PASSKEY_REQUEST.toByte() -> BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__PASSKEY_REQUEST.toByte() ->
processPairingRequest(decryptedBuffer) processPairingRequest(decryptedBuffer)
BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__PASSKEY_RETURN.toByte() -> BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__PASSKEY_RETURN.toByte() ->
processPairingRequest2(decryptedBuffer) processPairingRequest2(decryptedBuffer)
BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__GET_PUMP_CHECK.toByte() -> { BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__GET_PUMP_CHECK.toByte() -> {
// not easy mode, request time info // not easy mode, request time info
if (decryptedBuffer[2] == 0x05.toByte()) sendTimeInfo() if (decryptedBuffer[2] == 0x05.toByte()) sendTimeInfo()
// easy mode // easy mode
@ -453,32 +454,19 @@ class BLEComm @Inject internal constructor(
// v3 2nd layer encryption // v3 2nd layer encryption
v3Encryption = true v3Encryption = true
danaPump.v3RSPump = true danaPump.v3RSPump = true
val model = decryptedBuffer[5] danaPump.hwModel = decryptedBuffer[5].toInt()
// val protocol = decryptedBuffer[7] danaPump.protocol = decryptedBuffer[7].toInt()
if (model == 0x05.toByte()) { // grab randomSyncKey
sp.putString(resourceHelper.gs(R.string.key_danars_v3_randomsynckey) + danaRSPlugin.mDeviceName, String.format("%02x", decryptedBuffer[decryptedBuffer.size - 1]))
if (danaPump.hwModel == 0x05) {
aapsLogger.debug(LTag.PUMPBTCOMM, "<<<<< " + "ENCRYPTION__PUMP_CHECK V3 (OK)" + " " + DanaRS_Packet.toHexString(decryptedBuffer)) aapsLogger.debug(LTag.PUMPBTCOMM, "<<<<< " + "ENCRYPTION__PUMP_CHECK V3 (OK)" + " " + DanaRS_Packet.toHexString(decryptedBuffer))
// Dana RS Pump // Dana RS Pump
val randomPairingKey = sp.getString(resourceHelper.gs(R.string.key_danars_v3_randompairingkey) + danaRSPlugin.mDeviceName, "") sendV3PairingInformation()
val pairingKey = sp.getString(resourceHelper.gs(R.string.key_danars_v3_pairingkey) + danaRSPlugin.mDeviceName, "") } else if (danaPump.hwModel == 0x06) {
if (randomPairingKey.isNotEmpty() && pairingKey.isNotEmpty()) {
val randomSyncKey = String.format("%02x", decryptedBuffer[decryptedBuffer.size - 1])
sp.putString(resourceHelper.gs(R.string.key_danars_v3_randomsynckey) + danaRSPlugin.mDeviceName, randomSyncKey)
val tPairingKey = Base64.decode(pairingKey, Base64.DEFAULT)
val tRandomPairingKey = Base64.decode(randomPairingKey, Base64.DEFAULT)
var tRandomSyncKey: Byte = 0
if (randomSyncKey.isNotEmpty()) {
tRandomSyncKey = randomSyncKey.toInt(16).toByte()
}
bleEncryption.setPairingKeys(tPairingKey, tRandomPairingKey, tRandomSyncKey)
sendV3PairingInformation(0)
} else {
sendV3PairingInformation(1)
}
} else if (model == 0x06.toByte()) {
aapsLogger.debug(LTag.PUMPBTCOMM, "<<<<< " + "ENCRYPTION__PUMP_CHECK V3 EASY (OK)" + " " + DanaRS_Packet.toHexString(decryptedBuffer)) aapsLogger.debug(LTag.PUMPBTCOMM, "<<<<< " + "ENCRYPTION__PUMP_CHECK V3 EASY (OK)" + " " + DanaRS_Packet.toHexString(decryptedBuffer))
// Dana RS Easy // Dana RS Easy
val bytes: ByteArray = bleEncryption.getEncryptedPacket(BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__GET_EASYMENU_CHECK, null, null) sendEasyMenuCheck()
writeCharacteristicNoResponse(uartWriteBTGattChar, bytes)
} }
// response PUMP : error status // response PUMP : error status
} else if (decryptedBuffer.size == 6 && decryptedBuffer[2] == 'P'.toByte() && decryptedBuffer[3] == 'U'.toByte() && decryptedBuffer[4] == 'M'.toByte() && decryptedBuffer[5] == 'P'.toByte()) { } else if (decryptedBuffer.size == 6 && decryptedBuffer[2] == 'P'.toByte() && decryptedBuffer[3] == 'U'.toByte() && decryptedBuffer[4] == 'M'.toByte() && decryptedBuffer[5] == 'P'.toByte()) {
@ -523,6 +511,24 @@ class BLEComm @Inject internal constructor(
// 2nd packet v3 // 2nd packet v3
// 0x00 Start encryption, 0x01 Request pairing // 0x00 Start encryption, 0x01 Request pairing
private fun sendV3PairingInformation() {
val randomPairingKey = sp.getString(resourceHelper.gs(R.string.key_danars_v3_randompairingkey) + danaRSPlugin.mDeviceName, "")
val pairingKey = sp.getString(resourceHelper.gs(R.string.key_danars_v3_pairingkey) + danaRSPlugin.mDeviceName, "")
if (randomPairingKey.isNotEmpty() && pairingKey.isNotEmpty()) {
val tPairingKey = Base64.decode(pairingKey, Base64.DEFAULT)
val tRandomPairingKey = Base64.decode(randomPairingKey, Base64.DEFAULT)
var tRandomSyncKey: Byte = 0
val randomSyncKey = sp.getString(resourceHelper.gs(R.string.key_danars_v3_randomsynckey) + danaRSPlugin.mDeviceName, "")
if (randomSyncKey.isNotEmpty()) {
tRandomSyncKey = randomSyncKey.toInt(16).toByte()
}
bleEncryption.setPairingKeys(tPairingKey, tRandomPairingKey, tRandomSyncKey)
sendV3PairingInformation(0)
} else {
sendV3PairingInformation(1)
}
}
private fun sendV3PairingInformation(requestNewPairing: Int) { private fun sendV3PairingInformation(requestNewPairing: Int) {
val params = byteArrayOf(requestNewPairing.toByte()) val params = byteArrayOf(requestNewPairing.toByte())
val bytes: ByteArray = bleEncryption.getEncryptedPacket(BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__TIME_INFORMATION, params, null) val bytes: ByteArray = bleEncryption.getEncryptedPacket(BleEncryption.DANAR_PACKET__OPCODE_ENCRYPTION__TIME_INFORMATION, params, null)
@ -635,7 +641,7 @@ class BLEComm @Inject internal constructor(
isUnitUD = decryptedBuffer[3] == 0x01.toByte() isUnitUD = decryptedBuffer[3] == 0x01.toByte()
// request time information // request time information
if (v3Encryption) sendV3PairingInformation(1) if (v3Encryption) sendV3PairingInformation()
else sendTimeInfo() else sendTimeInfo()
} }