12 uint8_t current_block = 4;
13 uint8_t message_start_index = 0;
14 uint32_t message_length = 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);
87 std::vector<uint8_t> data({
88 PN532_COMMAND_INDATAEXCHANGE,
93 data.insert(data.end(), key, key + 6);
94 data.insert(data.end(), uid.begin(), uid.end());
96 ESP_LOGE(TAG,
"Authentication failed - Block %d", block_num);
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);
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});
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 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});
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) {
247 std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_CLASSIC_BLOCK_SIZE);
251 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;