11 #include "../Common.h" 14 #define ISO14443A_UID_SIZE_SINGLE 4 15 #define ISO14443A_UID_SIZE_DOUBLE 7 16 #define ISO14443A_UID_SIZE_TRIPLE 10 18 #define ISO14443A_CMD_REQA 0x26 19 #define ISO14443A_CMD_WUPA 0x52 20 #define ISO14443A_CMD_SELECT_CL1 0x93 21 #define ISO14443A_CMD_SELECT_CL2 0x95 22 #define ISO14443A_CMD_SELECT_CL3 0x97 23 #define ISO14443A_CMD_HLTA 0x50 25 #define ISO14443A_NVB_AC_START 0x20 26 #define ISO14443A_NVB_AC_END 0x70 28 #define ISO14443A_CL_UID_OFFSET 0 29 #define ISO14443A_CL_UID_SIZE 4 30 #define ISO14443A_CL_BCC_OFFSET 4 31 #define ISO14443A_CL_BCC_SIZE 1 32 #define ISO14443A_CL_FRAME_SIZE ((ISO14443A_CL_UID_SIZE + ISO14443A_CL_BCC_SIZE) * 8) 33 #define ISO14443A_SAK_INCOMPLETE 0x04 34 #define ISO14443A_SAK_COMPLETE_COMPLIANT 0x20 35 #define ISO14443A_SAK_COMPLETE_NOT_COMPLIANT 0x00 37 #define ISO14443A_ATQA_FRAME_SIZE (2 * 8) 38 #define ISO14443A_SAK_FRAME_SIZE (3 * 8) 40 #define ISO14443A_UID0_RANDOM 0x08 41 #define ISO14443A_UID0_CT 0x88 43 #define ISO14443A_CRCA_SIZE 2 45 #define ISO14443A_CALC_BCC(ByteBuffer) \ 46 ( ByteBuffer[0] ^ ByteBuffer[1] ^ ByteBuffer[2] ^ ByteBuffer[3] ) 48 void ISO14443AAppendCRCA(
void* Buffer, uint16_t ByteCount);
49 bool ISO14443ACheckCRCA(
const void* Buffer, uint16_t ByteCount);
51 INLINE
bool ISO14443ASelect(
void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue);
52 INLINE
bool ISO14443AWakeUp(
void* Buffer, uint16_t* BitCount, uint16_t ATQAValue,
bool FromHalt);
55 bool ISO14443ASelect(
void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue)
57 uint8_t* DataPtr = (uint8_t*) Buffer;
58 uint8_t NVB = DataPtr[1];
63 case ISO14443A_NVB_AC_START:
66 DataPtr[0] = UidCL[0];
67 DataPtr[1] = UidCL[1];
68 DataPtr[2] = UidCL[2];
69 DataPtr[3] = UidCL[3];
70 DataPtr[4] = ISO14443A_CALC_BCC(DataPtr);
72 *BitCount = ISO14443A_CL_FRAME_SIZE;
76 case ISO14443A_NVB_AC_END:
79 if ( (DataPtr[2] == UidCL[0]) &&
80 (DataPtr[3] == UidCL[1]) &&
81 (DataPtr[4] == UidCL[2]) &&
82 (DataPtr[5] == UidCL[3]) ) {
84 DataPtr[0] = SAKValue;
85 ISO14443AAppendCRCA(Buffer, 1);
87 *BitCount = ISO14443A_SAK_FRAME_SIZE;
96 uint8_t CollisionByteCount = ((NVB >> 4) & 0x0f) - 2;
97 uint8_t CollisionBitCount = (NVB >> 0) & 0x0f;
98 uint8_t mask = 0xFF >> (8 - CollisionBitCount);
101 ((CollisionByteCount == 5 || (CollisionByteCount == 4 && CollisionBitCount > 0)) && memcmp(UidCL, &DataPtr[2], 4) == 0 && (ISO14443A_CALC_BCC(UidCL) & mask) == (DataPtr[6] & mask))
103 (CollisionByteCount == 4 && CollisionBitCount == 0 && memcmp(UidCL, &DataPtr[2], 4) == 0)
105 (CollisionByteCount < 4 && memcmp(UidCL, &DataPtr[2], CollisionByteCount) == 0 && (UidCL[CollisionByteCount] & mask) == (DataPtr[CollisionByteCount + 2] & mask))
108 DataPtr[0] = UidCL[0];
109 DataPtr[1] = UidCL[1];
110 DataPtr[2] = UidCL[2];
111 DataPtr[3] = UidCL[3];
112 DataPtr[4] = ISO14443A_CALC_BCC(DataPtr);
114 *BitCount = ISO14443A_CL_FRAME_SIZE;
127 bool ISO14443AWakeUp(
void* Buffer, uint16_t* BitCount, uint16_t ATQAValue,
bool FromHalt)
129 uint8_t* DataPtr = (uint8_t*) Buffer;
131 if ( ((! FromHalt) && (DataPtr[0] == ISO14443A_CMD_REQA)) ||
132 (DataPtr[0] == ISO14443A_CMD_WUPA) ){
133 DataPtr[0] = (ATQAValue >> 0) & 0x00FF;
134 DataPtr[1] = (ATQAValue >> 8) & 0x00FF;
136 *BitCount = ISO14443A_ATQA_FRAME_SIZE;