ESPHome 2026.2.4
Loading...
Searching...
No Matches
pn532_mifare_ultralight.cpp
Go to the documentation of this file.
1#include <memory>
2
3#include "pn532.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace pn532 {
8
9static const char *const TAG = "pn532.mifare_ultralight";
10
11std::unique_ptr<nfc::NfcTag> PN532::read_mifare_ultralight_tag_(nfc::NfcTagUid &uid) {
12 std::vector<uint8_t> data;
13 // pages 3 to 6 contain various info we are interested in -- do one read to grab it all
14 if (!this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE * nfc::MIFARE_ULTRALIGHT_READ_SIZE,
15 data)) {
16 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
17 }
18
19 if (!this->is_mifare_ultralight_formatted_(data)) {
20 ESP_LOGW(TAG, "Not NDEF formatted");
21 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
22 }
23
24 uint8_t message_length;
25 uint8_t message_start_index;
26 if (!this->find_mifare_ultralight_ndef_(data, message_length, message_start_index)) {
27 ESP_LOGW(TAG, "Couldn't find NDEF message");
28 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
29 }
30 ESP_LOGVV(TAG, "NDEF message length: %u, start: %u", message_length, message_start_index);
31
32 if (message_length == 0) {
33 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
34 }
35 // we already read pages 3-6 earlier -- pick up where we left off so we're not re-reading pages
36 const uint8_t read_length = message_length + message_start_index > 12 ? message_length + message_start_index - 12 : 0;
37 if (read_length) {
38 if (!read_mifare_ultralight_bytes_(nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE + 3, read_length, data)) {
39 ESP_LOGE(TAG, "Error reading tag data");
40 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
41 }
42 }
43 // we need to trim off page 3 as well as any bytes ahead of message_start_index
44 data.erase(data.begin(), data.begin() + message_start_index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
45
46 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2, data);
47}
48
49bool PN532::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector<uint8_t> &data) {
50 const uint8_t read_increment = nfc::MIFARE_ULTRALIGHT_READ_SIZE * nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
51 std::vector<uint8_t> response;
52
53 for (uint8_t i = 0; i * read_increment < num_bytes; i++) {
54 if (!this->write_command_({
55 PN532_COMMAND_INDATAEXCHANGE,
56 0x01, // One card
57 nfc::MIFARE_CMD_READ,
58 uint8_t(i * nfc::MIFARE_ULTRALIGHT_READ_SIZE + start_page),
59 })) {
60 return false;
61 }
62
63 if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) {
64 return false;
65 }
66 uint16_t bytes_offset = (i + 1) * read_increment;
67 auto pages_in_end_itr = bytes_offset <= num_bytes ? response.end() : response.end() - (bytes_offset - num_bytes);
68
69 if ((pages_in_end_itr > response.begin()) && (pages_in_end_itr <= response.end())) {
70 data.insert(data.end(), response.begin() + 1, pages_in_end_itr);
71 }
72 }
73
74 char data_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
75 ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes_to(data_buf, data));
76
77 return true;
78}
79
80bool PN532::is_mifare_ultralight_formatted_(const std::vector<uint8_t> &page_3_to_6) {
81 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
82
83 return (page_3_to_6.size() > p4_offset + 3) &&
84 ((page_3_to_6[p4_offset + 0] != 0xFF) || (page_3_to_6[p4_offset + 1] != 0xFF) ||
85 (page_3_to_6[p4_offset + 2] != 0xFF) || (page_3_to_6[p4_offset + 3] != 0xFF));
86}
87
89 std::vector<uint8_t> data;
90 if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE, data)) {
91 ESP_LOGV(TAG, "Tag capacity is %u bytes", data[2] * 8U);
92 return data[2] * 8U;
93 }
94 return 0;
95}
96
97bool PN532::find_mifare_ultralight_ndef_(const std::vector<uint8_t> &page_3_to_6, uint8_t &message_length,
98 uint8_t &message_start_index) {
99 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
100
101 if (!(page_3_to_6.size() > p4_offset + 5)) {
102 return false;
103 }
104
105 if (page_3_to_6[p4_offset + 0] == 0x03) {
106 message_length = page_3_to_6[p4_offset + 1];
107 message_start_index = 2;
108 return true;
109 } else if (page_3_to_6[p4_offset + 5] == 0x03) {
110 message_length = page_3_to_6[p4_offset + 6];
111 message_start_index = 7;
112 return true;
113 }
114 return false;
115}
116
118 uint32_t capacity = this->read_mifare_ultralight_capacity_();
119
120 auto encoded = message->encode();
121
122 uint32_t message_length = encoded.size();
123 uint32_t buffer_length = nfc::get_mifare_ultralight_buffer_size(message_length);
124
125 if (buffer_length > capacity) {
126 ESP_LOGE(TAG, "Message length exceeds tag capacity %" PRIu32 " > %" PRIu32, buffer_length, capacity);
127 return false;
128 }
129
130 encoded.insert(encoded.begin(), 0x03);
131 if (message_length < 255) {
132 encoded.insert(encoded.begin() + 1, message_length);
133 } else {
134 encoded.insert(encoded.begin() + 1, 0xFF);
135 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
136 encoded.insert(encoded.begin() + 2, message_length & 0xFF);
137 }
138 encoded.push_back(0xFE);
139
140 encoded.resize(buffer_length, 0);
141
142 uint32_t index = 0;
143 uint8_t current_page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
144
145 while (index < buffer_length) {
146 std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
147 if (!this->write_mifare_ultralight_page_(current_page, data)) {
148 return false;
149 }
150 index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
151 current_page++;
152 }
153 return true;
154}
155
157 uint32_t capacity = this->read_mifare_ultralight_capacity_();
158 uint8_t pages = (capacity / nfc::MIFARE_ULTRALIGHT_PAGE_SIZE) + nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
159
160 std::vector<uint8_t> blank_data = {0x00, 0x00, 0x00, 0x00};
161
162 for (int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) {
163 if (!this->write_mifare_ultralight_page_(i, blank_data)) {
164 return false;
165 }
166 }
167 return true;
168}
169
170bool PN532::write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data) {
171 std::vector<uint8_t> data({
172 PN532_COMMAND_INDATAEXCHANGE,
173 0x01, // One card
174 nfc::MIFARE_CMD_WRITE_ULTRALIGHT,
175 page_num,
176 });
177 data.insert(data.end(), write_data.begin(), write_data.end());
178 if (!this->write_command_(data)) {
179 ESP_LOGE(TAG, "Error writing page %u", page_num);
180 return false;
181 }
182
183 std::vector<uint8_t> response;
184 if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
185 ESP_LOGE(TAG, "Error writing page %u", page_num);
186 return false;
187 }
188
189 return true;
190}
191
192} // namespace pn532
193} // namespace esphome
virtual bool write_data(const std::vector< uint8_t > &data)=0
std::unique_ptr< nfc::NfcTag > read_mifare_ultralight_tag_(nfc::NfcTagUid &uid)
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
bool find_mifare_ultralight_ndef_(const std::vector< uint8_t > &page_3_to_6, uint8_t &message_length, uint8_t &message_start_index)
bool is_mifare_ultralight_formatted_(const std::vector< uint8_t > &page_3_to_6)
bool read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector< uint8_t > &data)
bool write_mifare_ultralight_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message)
bool write_mifare_ultralight_page_(uint8_t page_num, std::vector< uint8_t > &write_data)
bool write_command_(const std::vector< uint8_t > &data)
Definition pn532.cpp:247
const char * message
Definition component.cpp:38
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:15
uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length)
Definition nfc.cpp:67
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7