ESPHome 2026.1.4
Loading...
Searching...
No Matches
pn532_mifare_classic.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_classic";
10
11std::unique_ptr<nfc::NfcTag> PN532::read_mifare_classic_tag_(std::vector<uint8_t> &uid) {
12 uint8_t current_block = 4;
13 uint8_t message_start_index = 0;
14 uint32_t message_length = 0;
15
16 if (this->auth_mifare_classic_block_(uid, current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY)) {
17 std::vector<uint8_t> data;
18 if (this->read_mifare_classic_block_(current_block, data)) {
19 if (!nfc::decode_mifare_classic_tlv(data, message_length, message_start_index)) {
20 return make_unique<nfc::NfcTag>(uid, nfc::ERROR);
21 }
22 } else {
23 ESP_LOGE(TAG, "Failed to read block %d", current_block);
24 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
25 }
26 } else {
27 ESP_LOGV(TAG, "Tag is not NDEF formatted");
28 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
29 }
30
31 uint32_t index = 0;
32 uint32_t buffer_size = nfc::get_mifare_classic_buffer_size(message_length);
33 std::vector<uint8_t> buffer;
34
35 while (index < buffer_size) {
36 if (nfc::mifare_classic_is_first_block(current_block)) {
37 if (!this->auth_mifare_classic_block_(uid, current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY)) {
38 ESP_LOGE(TAG, "Error, Block authentication failed for %d", current_block);
39 }
40 }
41 std::vector<uint8_t> block_data;
42 if (this->read_mifare_classic_block_(current_block, block_data)) {
43 buffer.insert(buffer.end(), block_data.begin(), block_data.end());
44 } else {
45 ESP_LOGE(TAG, "Error reading block %d", current_block);
46 }
47
48 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
49 current_block++;
50
51 if (nfc::mifare_classic_is_trailer_block(current_block)) {
52 current_block++;
53 }
54 }
55
56 if (buffer.begin() + message_start_index < buffer.end()) {
57 buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
58 } else {
59 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
60 }
61
62 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
63}
64
65bool PN532::read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data) {
66 if (!this->write_command_({
67 PN532_COMMAND_INDATAEXCHANGE,
68 0x01, // One card
69 nfc::MIFARE_CMD_READ,
70 block_num,
71 })) {
72 return false;
73 }
74
75 if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, data) || data[0] != 0x00) {
76 return false;
77 }
78 data.erase(data.begin());
79
80 char data_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
81 ESP_LOGVV(TAG, " Block %d: %s", block_num, nfc::format_bytes_to(data_buf, data));
82 return true;
83}
84
85bool PN532::auth_mifare_classic_block_(std::vector<uint8_t> &uid, uint8_t block_num, uint8_t key_num,
86 const uint8_t *key) {
87 std::vector<uint8_t> data({
88 PN532_COMMAND_INDATAEXCHANGE,
89 0x01, // One card
90 key_num, // Mifare Key slot
91 block_num, // Block number
92 });
93 data.insert(data.end(), key, key + 6);
94 data.insert(data.end(), uid.begin(), uid.end());
95 if (!this->write_command_(data)) {
96 ESP_LOGE(TAG, "Authentication failed - Block %d", block_num);
97 return false;
98 }
99
100 std::vector<uint8_t> response;
101 if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) {
102 ESP_LOGE(TAG, "Authentication failed - Block 0x%02x", block_num);
103 return false;
104 }
105
106 return true;
107}
108
109bool PN532::format_mifare_classic_mifare_(std::vector<uint8_t> &uid) {
110 std::vector<uint8_t> blank_buffer(
111 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
112 std::vector<uint8_t> trailer_buffer(
113 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
114
115 bool error = false;
116
117 for (int block = 0; block < 64; block += 4) {
118 if (!this->auth_mifare_classic_block_(uid, block + 3, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY)) {
119 continue;
120 }
121 if (block != 0) {
122 if (!this->write_mifare_classic_block_(block, blank_buffer)) {
123 ESP_LOGE(TAG, "Unable to write block %d", block);
124 error = true;
125 }
126 }
127 if (!this->write_mifare_classic_block_(block + 1, blank_buffer)) {
128 ESP_LOGE(TAG, "Unable to write block %d", block + 1);
129 error = true;
130 }
131 if (!this->write_mifare_classic_block_(block + 2, blank_buffer)) {
132 ESP_LOGE(TAG, "Unable to write block %d", block + 2);
133 error = true;
134 }
135 if (!this->write_mifare_classic_block_(block + 3, trailer_buffer)) {
136 ESP_LOGE(TAG, "Unable to write block %d", block + 3);
137 error = true;
138 }
139 }
140
141 return !error;
142}
143
144bool PN532::format_mifare_classic_ndef_(std::vector<uint8_t> &uid) {
145 std::vector<uint8_t> empty_ndef_message(
146 {0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
147 std::vector<uint8_t> blank_block(
148 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
149 std::vector<uint8_t> block_1_data(
150 {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
151 std::vector<uint8_t> block_2_data(
152 {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
153 std::vector<uint8_t> block_3_trailer(
154 {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
155 std::vector<uint8_t> ndef_trailer(
156 {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
157
158 if (!this->auth_mifare_classic_block_(uid, 0, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY)) {
159 ESP_LOGE(TAG, "Unable to authenticate block 0 for formatting!");
160 return false;
161 }
162 if (!this->write_mifare_classic_block_(1, block_1_data))
163 return false;
164 if (!this->write_mifare_classic_block_(2, block_2_data))
165 return false;
166 if (!this->write_mifare_classic_block_(3, block_3_trailer))
167 return false;
168
169 ESP_LOGD(TAG, "Sector 0 formatted to NDEF");
170
171 for (int block = 4; block < 64; block += 4) {
172 if (!this->auth_mifare_classic_block_(uid, block + 3, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY)) {
173 return false;
174 }
175 if (block == 4) {
176 if (!this->write_mifare_classic_block_(block, empty_ndef_message)) {
177 ESP_LOGE(TAG, "Unable to write block %d", block);
178 }
179 } else {
180 if (!this->write_mifare_classic_block_(block, blank_block)) {
181 ESP_LOGE(TAG, "Unable to write block %d", block);
182 }
183 }
184 if (!this->write_mifare_classic_block_(block + 1, blank_block)) {
185 ESP_LOGE(TAG, "Unable to write block %d", block + 1);
186 }
187 if (!this->write_mifare_classic_block_(block + 2, blank_block)) {
188 ESP_LOGE(TAG, "Unable to write block %d", block + 2);
189 }
190 if (!this->write_mifare_classic_block_(block + 3, ndef_trailer)) {
191 ESP_LOGE(TAG, "Unable to write trailer block %d", block + 3);
192 }
193 }
194 return true;
195}
196
197bool PN532::write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &write_data) {
198 std::vector<uint8_t> data({
199 PN532_COMMAND_INDATAEXCHANGE,
200 0x01, // One card
201 nfc::MIFARE_CMD_WRITE,
202 block_num,
203 });
204 data.insert(data.end(), write_data.begin(), write_data.end());
205 if (!this->write_command_(data)) {
206 ESP_LOGE(TAG, "Error writing block %d", block_num);
207 return false;
208 }
209
210 std::vector<uint8_t> response;
211 if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
212 ESP_LOGE(TAG, "Error writing block %d", block_num);
213 return false;
214 }
215
216 return true;
217}
218
220 auto encoded = message->encode();
221
222 uint32_t message_length = encoded.size();
223 uint32_t buffer_length = nfc::get_mifare_classic_buffer_size(message_length);
224
225 encoded.insert(encoded.begin(), 0x03);
226 if (message_length < 255) {
227 encoded.insert(encoded.begin() + 1, message_length);
228 } else {
229 encoded.insert(encoded.begin() + 1, 0xFF);
230 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
231 encoded.insert(encoded.begin() + 3, message_length & 0xFF);
232 }
233 encoded.push_back(0xFE);
234
235 encoded.resize(buffer_length, 0);
236
237 uint32_t index = 0;
238 uint8_t current_block = 4;
239
240 while (index < buffer_length) {
241 if (nfc::mifare_classic_is_first_block(current_block)) {
242 if (!this->auth_mifare_classic_block_(uid, current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY)) {
243 return false;
244 }
245 }
246
247 std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_CLASSIC_BLOCK_SIZE);
248 if (!this->write_mifare_classic_block_(current_block, data)) {
249 return false;
250 }
251 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
252 current_block++;
253
254 if (nfc::mifare_classic_is_trailer_block(current_block)) {
255 // Skipping as cannot write to trailer
256 current_block++;
257 }
258 }
259 return true;
260}
261
262} // namespace pn532
263} // namespace esphome
virtual bool write_data(const std::vector< uint8_t > &data)=0
bool write_mifare_classic_block_(uint8_t block_num, std::vector< uint8_t > &data)
bool format_mifare_classic_ndef_(std::vector< uint8_t > &uid)
bool format_mifare_classic_mifare_(std::vector< uint8_t > &uid)
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
std::unique_ptr< nfc::NfcTag > read_mifare_classic_tag_(std::vector< uint8_t > &uid)
bool read_mifare_classic_block_(uint8_t block_num, std::vector< uint8_t > &data)
bool write_mifare_classic_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
bool write_command_(const std::vector< uint8_t > &data)
Definition pn532.cpp:247
bool auth_mifare_classic_block_(std::vector< uint8_t > &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key)
const char * message
Definition component.cpp:38
char * format_bytes_to(char *buffer, const std::vector< uint8_t > &bytes)
Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use.
Definition nfc.cpp:15
bool decode_mifare_classic_tlv(std::vector< uint8_t > &data, uint32_t &message_length, uint8_t &message_start_index)
Definition nfc.cpp:47
bool mifare_classic_is_trailer_block(uint8_t block_num)
Definition nfc.cpp:91
uint32_t get_mifare_classic_buffer_size(uint32_t message_length)
Definition nfc.cpp:70
bool mifare_classic_is_first_block(uint8_t block_num)
Definition nfc.cpp:83
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7