13 std::vector<uint8_t> data;
17 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
21 ESP_LOGW(TAG,
"Not NDEF formatted");
22 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
25 uint8_t message_length;
26 uint8_t message_start_index;
28 ESP_LOGW(TAG,
"Couldn't find NDEF message");
29 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
31 ESP_LOGVV(TAG,
"NDEF message length: %u, start: %u", message_length, message_start_index);
33 if (message_length == 0) {
34 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
37 const uint8_t read_length = message_length + message_start_index > 12 ? message_length + message_start_index - 12 : 0;
40 ESP_LOGE(TAG,
"Error reading tag data");
41 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
45 data.erase(data.begin(), data.begin() + message_start_index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
47 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2, data);
51 const uint8_t read_increment = nfc::MIFARE_ULTRALIGHT_READ_SIZE * nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
52 std::vector<uint8_t> response;
54 for (uint8_t i = 0; i * read_increment < num_bytes; i++) {
56 PN532_COMMAND_INDATAEXCHANGE,
59 uint8_t(i * nfc::MIFARE_ULTRALIGHT_READ_SIZE + start_page),
64 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) {
67 uint16_t bytes_offset = (i + 1) * read_increment;
68 auto pages_in_end_itr = bytes_offset <= num_bytes ? response.end() : response.end() - (bytes_offset - num_bytes);
70 if ((pages_in_end_itr > response.begin()) && (pages_in_end_itr <= response.end())) {
71 data.insert(data.end(), response.begin() + 1, pages_in_end_itr);
75 char data_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
82 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
84 return (page_3_to_6.size() > p4_offset + 3) &&
85 ((page_3_to_6[p4_offset + 0] != 0xFF) || (page_3_to_6[p4_offset + 1] != 0xFF) ||
86 (page_3_to_6[p4_offset + 2] != 0xFF) || (page_3_to_6[p4_offset + 3] != 0xFF));
99 uint8_t &message_start_index) {
100 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
102 if (!(page_3_to_6.size() > p4_offset + 5)) {
106 if (page_3_to_6[p4_offset + 0] == 0x03) {
107 message_length = page_3_to_6[p4_offset + 1];
108 message_start_index = 2;
110 }
else if (page_3_to_6[p4_offset + 5] == 0x03) {
111 message_length = page_3_to_6[p4_offset + 6];
112 message_start_index = 7;
121 auto encoded =
message->encode();
123 uint32_t message_length = encoded.size();
126 if (buffer_length > capacity) {
127 ESP_LOGE(TAG,
"Message length exceeds tag capacity %" PRIu32
" > %" PRIu32, buffer_length, capacity);
131 encoded.insert(encoded.begin(), 0x03);
132 if (message_length < 255) {
133 encoded.insert(encoded.begin() + 1, message_length);
135 encoded.insert(encoded.begin() + 1, 0xFF);
136 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
137 encoded.insert(encoded.begin() + 2, message_length & 0xFF);
139 encoded.push_back(0xFE);
141 encoded.resize(buffer_length, 0);
144 uint8_t current_page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
146 while (index < buffer_length) {
150 index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
158 uint8_t pages = (capacity / nfc::MIFARE_ULTRALIGHT_PAGE_SIZE) + nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
160 static constexpr std::array<uint8_t, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE> BLANK_DATA = {0x00, 0x00, 0x00, 0x00};
162 for (
int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) {
171 std::vector<uint8_t> cmd({
172 PN532_COMMAND_INDATAEXCHANGE,
174 nfc::MIFARE_CMD_WRITE_ULTRALIGHT,
179 ESP_LOGE(TAG,
"Error writing page %u", page_num);
183 std::vector<uint8_t> response;
184 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
185 ESP_LOGE(TAG,
"Error writing page %u", page_num);
char * format_bytes_to(char *buffer, std::span< const uint8_t > bytes)
Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use.