Updated on 2026-08-14

This commit is contained in:
Tangem 2020-03-02 14:23:52 +00:00
commit 2a43a9ac6b
5 changed files with 30 additions and 24 deletions

View file

@ -4,7 +4,6 @@ import com.tangem.common.CardEnvironment
import com.tangem.common.apdu.CommandApdu
import com.tangem.common.apdu.Instruction
import com.tangem.common.apdu.ResponseApdu
import com.tangem.common.extensions.calculateSha256
import com.tangem.common.tlv.Tlv
import com.tangem.common.tlv.TlvBuilder
import com.tangem.common.tlv.TlvMapper
@ -64,14 +63,19 @@ enum class CardStatus(val code: Int) {
}
}
enum class ProductMask(val code: Byte) {
Note(0x01),
Tag(0x02),
Card(0x04);
/**
* Mask of products enabled on card
* @property rawValue Products mask values,
* while flags definitions and values are in [ProductMask.Companion] as constants.
*/
data class ProductMask(val rawValue: Int) {
fun contains(value: Int): Boolean = (rawValue and value) != 0
companion object {
private val values = values()
fun byCode(code: Byte): ProductMask? = values.find { it.code == code }
const val note = 0x01
const val tag = 0x02
const val idCard = 0x04
}
}

View file

@ -74,12 +74,12 @@ class TlvEncoder {
TlvValueType.ProductMask -> {
typeCheck<T, ProductMask>(tag)
byteArrayOf(
(value as ProductMask).code
(value as ProductMask).rawValue.toByte()
)
}
TlvValueType.SettingsMask -> {
typeCheck<T, SettingsMask>(tag)
(value as SettingsMask).rawValue.toByteArray()
(value as SettingsMask).rawValue.toByteArray(2)
}
TlvValueType.CardStatus -> {
typeCheck<T, CardStatus>(tag)
@ -97,7 +97,7 @@ class TlvEncoder {
}
private inline fun <reified T, reified ExpectedT> typeCheck(tag: TlvTag) {
if (T::class != ExpectedT::class){
if (T::class != ExpectedT::class) {
Log.e(this::class.simpleName!!,
"Mapping error. Type for tag: $tag must be ${tag.valueType()}. It is ${T::class}")
throw TaskError.WrongType()

View file

@ -101,12 +101,7 @@ class TlvMapper(val tlvList: List<Tlv>) {
}
TlvValueType.ProductMask -> {
typeCheck<T, ProductMask>(tag)
try {
ProductMask.byCode(tlvValue.first()) as T
} catch (exception: Exception) {
logException(tag, tlvValue.first().toString(), exception)
throw TaskError.ConvertError()
}
ProductMask(tlvValue.toInt()) as T
}
TlvValueType.SettingsMask -> {
typeCheck<T, SettingsMask>(tag)

View file

@ -3,7 +3,6 @@ package com.tangem.tasks
import com.tangem.commands.*
import com.tangem.common.CardEnvironment
import com.tangem.common.CompletionResult
import com.tangem.crypto.CryptoUtils
/**
* Events that [ScanTask] returns on completion of its commands.
@ -38,7 +37,7 @@ internal class ScanTask : Task<ScanEvent>() {
completeNfcSession(TaskError.MissingPreflightRead())
callback(TaskEvent.Completion(TaskError.MissingPreflightRead()))
} else if (currentCard.cardData?.productMask == ProductMask.Tag) {
} else if (currentCard.cardData?.productMask?.contains(ProductMask.tag) != false) {
completeNfcSession()
callback(TaskEvent.Completion())

View file

@ -116,18 +116,26 @@ class TlvMapperTest {
}
@Test
fun `map ProductMask returns correct value`() {
val localMapper = TlvMapper(listOf(Tlv(TlvTag.ProductMask, byteArrayOf(4))))
fun `map ProductMask with raw value 5 returns correct value`() {
val localMapper = TlvMapper(listOf(Tlv(TlvTag.ProductMask, byteArrayOf(5))))
val productMask: ProductMask = localMapper.map(TlvTag.ProductMask)
assertThat(productMask)
.isEqualTo(ProductMask.Card)
assertThat(productMask.contains(ProductMask.note) && productMask.contains(ProductMask.idCard))
.isTrue()
}
@Test
fun `map ProductMask with raw value 1 returns correct value`() {
val localMapper = TlvMapper(listOf(Tlv(TlvTag.ProductMask, byteArrayOf(1))))
val productMask: ProductMask = localMapper.map(TlvTag.ProductMask)
assertThat(productMask.contains(ProductMask.note))
.isTrue()
}
@Test
fun `map Enum with unknown code throws ConversionException error`() {
val localMapper = TlvMapper(listOf(Tlv(TlvTag.ProductMask, byteArrayOf(5))))
val localMapper = TlvMapper(listOf(Tlv(TlvTag.CurveId, "test".toByteArray())))
assertThrows<TaskError.ConvertError> {
localMapper.map<ProductMask>(TlvTag.ProductMask)
localMapper.map<EllipticCurve>(TlvTag.CurveId)
}
}