ESPHome 2026.5.1
Loading...
Searching...
No Matches
nfc.cpp
Go to the documentation of this file.
1#include "nfc.h"
2#include <cstdio>
4#include "esphome/core/log.h"
5
6namespace esphome::nfc {
7
8static const char *const TAG = "nfc";
9
10char *format_uid_to(char *buffer, std::span<const uint8_t> uid) {
11 return format_hex_pretty_to(buffer, FORMAT_UID_BUFFER_SIZE, uid.data(), uid.size(), '-');
12}
13
14char *format_bytes_to(char *buffer, std::span<const uint8_t> bytes) {
15 return format_hex_pretty_to(buffer, FORMAT_BYTES_BUFFER_SIZE, bytes.data(), bytes.size(), ' ');
16}
17
18#pragma GCC diagnostic push
19#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
20// Deprecated wrappers intentionally use heap-allocating version for backward compatibility
21std::string format_uid(std::span<const uint8_t> uid) {
22 return format_hex_pretty(uid.data(), uid.size(), '-', false); // NOLINT
23}
24std::string format_bytes(std::span<const uint8_t> bytes) {
25 return format_hex_pretty(bytes.data(), bytes.size(), ' ', false); // NOLINT
26}
27#pragma GCC diagnostic pop
28
29uint8_t guess_tag_type(uint8_t uid_length) {
30 if (uid_length == 4) {
31 return TAG_TYPE_MIFARE_CLASSIC;
32 } else {
33 return TAG_TYPE_2;
34 }
35}
36
37int8_t get_mifare_classic_ndef_start_index(std::vector<uint8_t> &data) {
38 for (uint8_t i = 0; i < MIFARE_CLASSIC_BLOCK_SIZE; i++) {
39 if (data[i] == 0x00) {
40 // Do nothing, skip
41 } else if (data[i] == 0x03) {
42 return i;
43 } else {
44 return -2;
45 }
46 }
47 return -1;
48}
49
50bool decode_mifare_classic_tlv(std::vector<uint8_t> &data, uint32_t &message_length, uint8_t &message_start_index) {
51 if (data.size() < MIFARE_CLASSIC_BLOCK_SIZE) {
52 ESP_LOGE(TAG, "Error, data too short for NDEF detection.");
53 return false;
54 }
56 if (i < 0 || data[i] != 0x03) {
57 ESP_LOGE(TAG, "Error, Can't decode message length.");
58 return false;
59 }
60 uint8_t idx = static_cast<uint8_t>(i);
61 if (idx + 4 <= data.size() && data[idx + 1] == 0xFF) {
62 message_length = ((0xFF & data[idx + 2]) << 8) | (0xFF & data[idx + 3]);
63 message_start_index = idx + MIFARE_CLASSIC_LONG_TLV_SIZE;
64 } else if (idx + 2 <= data.size()) {
65 message_length = data[idx + 1];
66 message_start_index = idx + MIFARE_CLASSIC_SHORT_TLV_SIZE;
67 } else {
68 ESP_LOGE(TAG, "Error, TLV data too short.");
69 return false;
70 }
71 return true;
72}
73
75 uint32_t buffer_size = message_length + 2 + 1;
76 if (buffer_size % MIFARE_ULTRALIGHT_READ_SIZE != 0)
77 buffer_size = ((buffer_size / MIFARE_ULTRALIGHT_READ_SIZE) + 1) * MIFARE_ULTRALIGHT_READ_SIZE;
78 return buffer_size;
79}
80
82 uint32_t buffer_size = message_length;
83 if (message_length < 255) {
84 buffer_size += MIFARE_CLASSIC_SHORT_TLV_SIZE + 1;
85 } else {
86 buffer_size += MIFARE_CLASSIC_LONG_TLV_SIZE + 1;
87 }
88 if (buffer_size % MIFARE_CLASSIC_BLOCK_SIZE != 0) {
89 buffer_size = ((buffer_size / MIFARE_CLASSIC_BLOCK_SIZE) + 1) * MIFARE_CLASSIC_BLOCK_SIZE;
90 }
91 return buffer_size;
92}
93
94bool mifare_classic_is_first_block(uint8_t block_num) {
95 if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
96 return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
97 } else {
98 return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
99 }
100}
101
102bool mifare_classic_is_trailer_block(uint8_t block_num) {
103 if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
104 return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
105 } else {
106 return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
107 }
108}
109
110} // namespace esphome::nfc
bool decode_mifare_classic_tlv(std::vector< uint8_t > &data, uint32_t &message_length, uint8_t &message_start_index)
Definition nfc.cpp:50
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.
Definition nfc.cpp:14
uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length)
Definition nfc.cpp:74
std::string format_uid(std::span< const uint8_t > uid)
Definition nfc.cpp:21
std::string format_bytes(std::span< const uint8_t > bytes)
Definition nfc.cpp:24
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.
Definition nfc.cpp:10
uint8_t guess_tag_type(uint8_t uid_length)
Definition nfc.cpp:29
bool mifare_classic_is_trailer_block(uint8_t block_num)
Definition nfc.cpp:102
uint32_t get_mifare_classic_buffer_size(uint32_t message_length)
Definition nfc.cpp:81
int8_t get_mifare_classic_ndef_start_index(std::vector< uint8_t > &data)
Definition nfc.cpp:37
bool mifare_classic_is_first_block(uint8_t block_num)
Definition nfc.cpp:94
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).
Definition helpers.cpp:341
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
static void uint32_t