12 uint8_t current_block = 4;
13 uint8_t message_start_index = 0;
17 std::vector<uint8_t> data;
20 return make_unique<nfc::NfcTag>(uid, nfc::ERROR);
23 ESP_LOGE(TAG,
"Failed to read block %d", current_block);
24 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
27 ESP_LOGV(TAG,
"Tag is not NDEF formatted");
28 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
33 std::vector<uint8_t> buffer;
35 while (index < buffer_size) {
38 ESP_LOGE(TAG,
"Error, Block authentication failed for %d", current_block);
41 std::vector<uint8_t> block_data;
43 buffer.insert(buffer.end(), block_data.begin(), block_data.end());
45 ESP_LOGE(TAG,
"Error reading block %d", current_block);
48 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
56 if (buffer.begin() + message_start_index < buffer.end()) {
57 buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
59 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
62 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
109 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLANK_BUFFER = {
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
111 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> TRAILER_BUFFER = {
112 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
116 for (
int block = 0; block < 64; block += 4) {
122 ESP_LOGE(TAG,
"Unable to write block %d", block);
127 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
131 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
135 ESP_LOGE(TAG,
"Unable to write block %d", block + 3);
144 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> EMPTY_NDEF_MESSAGE = {
145 0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
146 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLANK_BLOCK = {
147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
148 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_1_DATA = {
149 0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
150 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_2_DATA = {
151 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
152 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_3_TRAILER = {
153 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
154 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> NDEF_TRAILER = {
155 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
158 ESP_LOGE(TAG,
"Unable to authenticate block 0 for formatting!");
168 ESP_LOGD(TAG,
"Sector 0 formatted to NDEF");
170 for (
int block = 4; block < 64; block += 4) {
176 ESP_LOGE(TAG,
"Unable to write block %d", block);
180 ESP_LOGE(TAG,
"Unable to write block %d", block);
184 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
187 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
190 ESP_LOGE(TAG,
"Unable to write trailer block %d", block + 3);
219 auto encoded =
message->encode();
221 uint32_t message_length = encoded.size();
224 encoded.insert(encoded.begin(), 0x03);
225 if (message_length < 255) {
226 encoded.insert(encoded.begin() + 1, message_length);
228 encoded.insert(encoded.begin() + 1, 0xFF);
229 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
230 encoded.insert(encoded.begin() + 3, message_length & 0xFF);
232 encoded.push_back(0xFE);
234 encoded.resize(buffer_length, 0);
237 uint8_t current_block = 4;
239 while (index < buffer_length) {
249 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;