13 uint8_t current_block = 4;
14 uint8_t message_start_index = 0;
15 uint32_t message_length = 0;
18 std::vector<uint8_t> data;
21 return make_unique<nfc::NfcTag>(uid, nfc::ERROR);
24 ESP_LOGE(TAG,
"Failed to read block %d", current_block);
25 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
28 ESP_LOGV(TAG,
"Tag is not NDEF formatted");
29 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
34 std::vector<uint8_t> buffer;
36 while (index < buffer_size) {
39 ESP_LOGE(TAG,
"Error, Block authentication failed for %d", current_block);
42 std::vector<uint8_t> block_data;
44 buffer.insert(buffer.end(), block_data.begin(), block_data.end());
46 ESP_LOGE(TAG,
"Error reading block %d", current_block);
49 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
57 if (buffer.begin() + message_start_index < buffer.end()) {
58 buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
60 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
63 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
110 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLANK_BUFFER = {
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
112 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> TRAILER_BUFFER = {
113 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
117 for (
int block = 0; block < 64; block += 4) {
123 ESP_LOGE(TAG,
"Unable to write block %d", block);
128 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
132 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
136 ESP_LOGE(TAG,
"Unable to write block %d", block + 3);
145 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> EMPTY_NDEF_MESSAGE = {
146 0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
147 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLANK_BLOCK = {
148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
149 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_1_DATA = {
150 0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
151 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_2_DATA = {
152 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
153 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> BLOCK_3_TRAILER = {
154 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
155 static constexpr std::array<uint8_t, nfc::MIFARE_CLASSIC_BLOCK_SIZE> NDEF_TRAILER = {
156 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
159 ESP_LOGE(TAG,
"Unable to authenticate block 0 for formatting!");
169 ESP_LOGD(TAG,
"Sector 0 formatted to NDEF");
171 for (
int block = 4; block < 64; block += 4) {
177 ESP_LOGE(TAG,
"Unable to write block %d", block);
181 ESP_LOGE(TAG,
"Unable to write block %d", block);
185 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
188 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
191 ESP_LOGE(TAG,
"Unable to write trailer block %d", block + 3);
220 auto encoded =
message->encode();
222 uint32_t message_length = encoded.size();
225 encoded.insert(encoded.begin(), 0x03);
226 if (message_length < 255) {
227 encoded.insert(encoded.begin() + 1, message_length);
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);
233 encoded.push_back(0xFE);
235 encoded.resize(buffer_length, 0);
238 uint8_t current_block = 4;
240 while (index < buffer_length) {
250 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;