Extracted calculateLuhnChecksum() and calculateXorChecksum()
This commit is contained in:
parent
2c5f14df6a
commit
c7e71fcc84
|
@ -21,18 +21,7 @@ open class FlickercodeDecoder {
|
||||||
val de2 = parseDatenelement(challengeHHD_UC, de1.endIndex)
|
val de2 = parseDatenelement(challengeHHD_UC, de1.endIndex)
|
||||||
val de3 = parseDatenelement(challengeHHD_UC, de2.endIndex)
|
val de3 = parseDatenelement(challengeHHD_UC, de2.endIndex)
|
||||||
|
|
||||||
val luhnData = controlByte + startCode + de1.data + de2.data + de3.data
|
val luhnChecksum = calculateLuhnChecksum(startCode, controlByte, de1, de2, de3)
|
||||||
|
|
||||||
val luhnSum = luhnData.mapIndexed { index, char ->
|
|
||||||
val asNumber = char.toString().toInt(16)
|
|
||||||
|
|
||||||
if (index % 2 == 1) {
|
|
||||||
val doubled = asNumber * 2
|
|
||||||
return@mapIndexed (doubled / 10) + (doubled % 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
asNumber
|
|
||||||
}.sum()
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// können im HHDUC-Protokoll Datenelemente ausgelassen werden, indem als Länge LDE1, LDE2 oder LDE3 = ‘00‘ angegeben wird.
|
// können im HHDUC-Protokoll Datenelemente ausgelassen werden, indem als Länge LDE1, LDE2 oder LDE3 = ‘00‘ angegeben wird.
|
||||||
|
@ -43,11 +32,7 @@ open class FlickercodeDecoder {
|
||||||
val dataLength = (dataWithoutLengthAndChecksum.length + 2) / 2 // + 2 for checksum
|
val dataLength = (dataWithoutLengthAndChecksum.length + 2) / 2 // + 2 for checksum
|
||||||
val dataWithoutChecksum = toHex(dataLength, 2) + dataWithoutLengthAndChecksum
|
val dataWithoutChecksum = toHex(dataLength, 2) + dataWithoutLengthAndChecksum
|
||||||
|
|
||||||
var xorChecksum = 0
|
val xorChecksumString = calculateXorChecksum(dataWithoutChecksum)
|
||||||
val xorByteData = dataWithoutChecksum.map { parseIntToHex(it) }
|
|
||||||
xorByteData.forEach { xorChecksum = xorChecksum xor it }
|
|
||||||
|
|
||||||
val xorChecksumString = toHex(xorChecksum, 1)
|
|
||||||
|
|
||||||
val parsedDataSet = dataWithoutChecksum + luhnChecksum + xorChecksumString
|
val parsedDataSet = dataWithoutChecksum + luhnChecksum + xorChecksumString
|
||||||
|
|
||||||
|
@ -121,6 +106,36 @@ open class FlickercodeDecoder {
|
||||||
return lengthByte and 0b00011111
|
return lengthByte and 0b00011111
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected open fun calculateLuhnChecksum(startCode: FlickercodeDatenelement, controlByte: String,
|
||||||
|
de1: FlickercodeDatenelement, de2: FlickercodeDatenelement, de3: FlickercodeDatenelement): Int {
|
||||||
|
|
||||||
|
val luhnData = controlByte + startCode.data + de1.data + de2.data + de3.data
|
||||||
|
|
||||||
|
val luhnSum = luhnData.mapIndexed { index, char ->
|
||||||
|
val asNumber = char.toString().toInt(16)
|
||||||
|
|
||||||
|
if (index % 2 == 1) {
|
||||||
|
val doubled = asNumber * 2
|
||||||
|
return@mapIndexed (doubled / 10) + (doubled % 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
asNumber
|
||||||
|
}.sum()
|
||||||
|
|
||||||
|
return 10 - (luhnSum % 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun calculateXorChecksum(dataWithoutChecksum: String): String {
|
||||||
|
var xorChecksum = 0
|
||||||
|
val xorByteData = dataWithoutChecksum.map { parseIntToHex(it) }
|
||||||
|
|
||||||
|
xorByteData.forEach { xorChecksum = xorChecksum xor it }
|
||||||
|
|
||||||
|
return toHex(xorChecksum, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected open fun toHex(number: Int, minLength: Int): String {
|
protected open fun toHex(number: Int, minLength: Int): String {
|
||||||
var result = number.toString (16).toUpperCase()
|
var result = number.toString (16).toUpperCase()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue