14 uint8_t current_block = 4;
15 uint8_t message_start_index = 0;
22 return make_unique<nfc::NfcTag>(uid, nfc::ERROR);
25 ESP_LOGE(TAG,
"Failed to read block %d", current_block);
26 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
29 ESP_LOGV(TAG,
"Tag is not NDEF formatted");
30 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
32 if (message_length > MIFARE_CLASSIC_MAX_NDEF_SIZE) {
33 ESP_LOGE(TAG,
"NDEF message too long: %" PRIu32
" bytes", message_length);
34 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
39 if (!
buffer.try_init(buffer_size)) {
40 ESP_LOGE(TAG,
"Out of memory reading NDEF message of %" PRIu32
" bytes", buffer_size);
41 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
44 while (
buffer.size() < buffer_size) {
47 ESP_LOGE(TAG,
"Error, Block authentication failed for %d", current_block);
48 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
52 ESP_LOGE(TAG,
"Error reading block %d", current_block);
53 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
55 for (
const uint8_t
byte : block_data) {
65 if (message_start_index >=
buffer.size()) {
66 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
69 return make_unique<nfc::NfcTag>(
70 uid, nfc::MIFARE_CLASSIC,
71 make_unique<nfc::NdefMessage>(std::span<const uint8_t>(
buffer).subspan(message_start_index)));
113 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLANK_BUFFER = {
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
115 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> TRAILER_BUFFER = {
116 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
120 for (
int block = 0; block < 64; block += 4) {
126 ESP_LOGE(TAG,
"Unable to write block %d", block);
131 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
135 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
139 ESP_LOGE(TAG,
"Unable to write block %d", block + 3);
148 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> EMPTY_NDEF_MESSAGE = {
149 0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
150 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLANK_BLOCK = {
151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
152 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_1_DATA = {
153 0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
154 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_2_DATA = {
155 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
156 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_3_TRAILER = {
157 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
158 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> NDEF_TRAILER = {
159 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
162 ESP_LOGE(TAG,
"Unable to authenticate block 0 for formatting!");
172 ESP_LOGD(TAG,
"Sector 0 formatted to NDEF");
176 for (
int block = 4; block < 64; block += 4) {
182 ESP_LOGE(TAG,
"Unable to write block %d", block);
187 ESP_LOGE(TAG,
"Unable to write block %d", block);
192 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
196 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
200 ESP_LOGE(TAG,
"Unable to write trailer block %d", block + 3);