ESPHome 2025.9.0-dev
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 {
7namespace nfc {
8
9static const char *const TAG = "nfc";
10
11std::string format_uid(const std::vector<uint8_t> &uid) { return format_hex_pretty(uid, '-', false); }
12
13std::string format_bytes(const std::vector<uint8_t> &bytes) { return format_hex_pretty(bytes, ' ', false); }
14
15uint8_t guess_tag_type(uint8_t uid_length) {
16 if (uid_length == 4) {
17 return TAG_TYPE_MIFARE_CLASSIC;
18 } else {
19 return TAG_TYPE_2;
20 }
21}
22
23uint8_t get_mifare_classic_ndef_start_index(std::vector<uint8_t> &data) {
24 for (uint8_t i = 0; i < MIFARE_CLASSIC_BLOCK_SIZE; i++) {
25 if (data[i] == 0x00) {
26 // Do nothing, skip
27 } else if (data[i] == 0x03) {
28 return i;
29 } else {
30 return -2;
31 }
32 }
33 return -1;
34}
35
36bool decode_mifare_classic_tlv(std::vector<uint8_t> &data, uint32_t &message_length, uint8_t &message_start_index) {
38 if (data[i] != 0x03) {
39 ESP_LOGE(TAG, "Error, Can't decode message length.");
40 return false;
41 }
42 if (data[i + 1] == 0xFF) {
43 message_length = ((0xFF & data[i + 2]) << 8) | (0xFF & data[i + 3]);
44 message_start_index = i + MIFARE_CLASSIC_LONG_TLV_SIZE;
45 } else {
46 message_length = data[i + 1];
47 message_start_index = i + MIFARE_CLASSIC_SHORT_TLV_SIZE;
48 }
49 return true;
50}
51
52uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length) {
53 uint32_t buffer_size = message_length + 2 + 1;
54 if (buffer_size % MIFARE_ULTRALIGHT_READ_SIZE != 0)
55 buffer_size = ((buffer_size / MIFARE_ULTRALIGHT_READ_SIZE) + 1) * MIFARE_ULTRALIGHT_READ_SIZE;
56 return buffer_size;
57}
58
59uint32_t get_mifare_classic_buffer_size(uint32_t message_length) {
60 uint32_t buffer_size = message_length;
61 if (message_length < 255) {
62 buffer_size += MIFARE_CLASSIC_SHORT_TLV_SIZE + 1;
63 } else {
64 buffer_size += MIFARE_CLASSIC_LONG_TLV_SIZE + 1;
65 }
66 if (buffer_size % MIFARE_CLASSIC_BLOCK_SIZE != 0) {
67 buffer_size = ((buffer_size / MIFARE_CLASSIC_BLOCK_SIZE) + 1) * MIFARE_CLASSIC_BLOCK_SIZE;
68 }
69 return buffer_size;
70}
71
72bool mifare_classic_is_first_block(uint8_t block_num) {
73 if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
74 return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
75 } else {
76 return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
77 }
78}
79
80bool mifare_classic_is_trailer_block(uint8_t block_num) {
81 if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
82 return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
83 } else {
84 return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
85 }
86}
87
88} // namespace nfc
89} // namespace esphome
bool decode_mifare_classic_tlv(std::vector< uint8_t > &data, uint32_t &message_length, uint8_t &message_start_index)
Definition nfc.cpp:36
std::string format_bytes(const std::vector< uint8_t > &bytes)
Definition nfc.cpp:13
uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length)
Definition nfc.cpp:52
uint8_t get_mifare_classic_ndef_start_index(std::vector< uint8_t > &data)
Definition nfc.cpp:23
std::string format_uid(const std::vector< uint8_t > &uid)
Definition nfc.cpp:11
uint8_t guess_tag_type(uint8_t uid_length)
Definition nfc.cpp:15
bool mifare_classic_is_trailer_block(uint8_t block_num)
Definition nfc.cpp:80
uint32_t get_mifare_classic_buffer_size(uint32_t message_length)
Definition nfc.cpp:59
bool mifare_classic_is_first_block(uint8_t block_num)
Definition nfc.cpp:72
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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.
Definition helpers.cpp:280