8static const char *
const TAG =
"nfc";
19 if (uid_length == 4) {
20 return TAG_TYPE_MIFARE_CLASSIC;
27 for (uint8_t i = 0; i < MIFARE_CLASSIC_BLOCK_SIZE; i++) {
28 if (data[i] == 0x00) {
30 }
else if (data[i] == 0x03) {
40 if (data.size() < MIFARE_CLASSIC_BLOCK_SIZE) {
41 ESP_LOGE(TAG,
"Error, data too short for NDEF detection.");
45 if (i < 0 || data[i] != 0x03) {
46 ESP_LOGE(TAG,
"Error, Can't decode message length.");
49 uint8_t idx =
static_cast<uint8_t
>(i);
50 if (idx + 4 <= data.size() && data[idx + 1] == 0xFF) {
51 message_length = ((0xFF & data[idx + 2]) << 8) | (0xFF & data[idx + 3]);
52 message_start_index = idx + MIFARE_CLASSIC_LONG_TLV_SIZE;
53 }
else if (idx + 2 <= data.size()) {
54 message_length = data[idx + 1];
55 message_start_index = idx + MIFARE_CLASSIC_SHORT_TLV_SIZE;
57 ESP_LOGE(TAG,
"Error, TLV data too short.");
64 uint32_t buffer_size = message_length + 2 + 1;
65 if (buffer_size % MIFARE_ULTRALIGHT_READ_SIZE != 0)
66 buffer_size = ((buffer_size / MIFARE_ULTRALIGHT_READ_SIZE) + 1) * MIFARE_ULTRALIGHT_READ_SIZE;
71 uint32_t buffer_size = message_length;
72 if (message_length < 255) {
73 buffer_size += MIFARE_CLASSIC_SHORT_TLV_SIZE + 1;
75 buffer_size += MIFARE_CLASSIC_LONG_TLV_SIZE + 1;
77 if (buffer_size % MIFARE_CLASSIC_BLOCK_SIZE != 0) {
78 buffer_size = ((buffer_size / MIFARE_CLASSIC_BLOCK_SIZE) + 1) * MIFARE_CLASSIC_BLOCK_SIZE;
84 if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
85 return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
87 return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
92 if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
93 return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
95 return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
bool decode_mifare_classic_tlv(std::vector< uint8_t > &data, uint32_t &message_length, uint8_t &message_start_index)
char * format_bytes_to(char *buffer, std::span< const uint8_t > bytes)
Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use.
uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length)
char * format_uid_to(char *buffer, std::span< const uint8_t > uid)
Format UID to buffer with '-' separator (e.g., "04-11-22-33"). Returns buffer for inline use.
uint8_t guess_tag_type(uint8_t uid_length)
bool mifare_classic_is_trailer_block(uint8_t block_num)
uint32_t get_mifare_classic_buffer_size(uint32_t message_length)
int8_t get_mifare_classic_ndef_start_index(std::vector< uint8_t > &data)
bool mifare_classic_is_first_block(uint8_t block_num)
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).