9 static const char *
const TAG =
"pn532.mifare_ultralight";
12 std::vector<uint8_t> data;
16 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
20 ESP_LOGW(TAG,
"Not NDEF formatted");
21 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
24 uint8_t message_length;
25 uint8_t message_start_index;
27 ESP_LOGW(TAG,
"Couldn't find NDEF message");
28 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
30 ESP_LOGVV(TAG,
"NDEF message length: %u, start: %u", message_length, message_start_index);
32 if (message_length == 0) {
33 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
36 const uint8_t read_length = message_length + message_start_index > 12 ? message_length + message_start_index - 12 : 0;
39 ESP_LOGE(TAG,
"Error reading tag data");
40 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
44 data.erase(data.begin(), data.begin() + message_start_index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
46 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2, data);
50 const uint8_t read_increment = nfc::MIFARE_ULTRALIGHT_READ_SIZE * nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
51 std::vector<uint8_t> response;
53 for (uint8_t i = 0; i * read_increment < num_bytes; i++) {
55 PN532_COMMAND_INDATAEXCHANGE,
58 uint8_t(i * nfc::MIFARE_ULTRALIGHT_READ_SIZE + start_page),
63 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) {
66 uint16_t bytes_offset = (i + 1) * read_increment;
67 auto pages_in_end_itr = bytes_offset <= num_bytes ? response.end() : response.end() - (bytes_offset - num_bytes);
69 if ((pages_in_end_itr > response.begin()) && (pages_in_end_itr <= response.end())) {
70 data.insert(data.end(), response.begin() + 1, pages_in_end_itr);
80 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
82 return (page_3_to_6.size() > p4_offset + 3) &&
83 ((page_3_to_6[p4_offset + 0] != 0xFF) || (page_3_to_6[p4_offset + 1] != 0xFF) ||
84 (page_3_to_6[p4_offset + 2] != 0xFF) || (page_3_to_6[p4_offset + 3] != 0xFF));
88 std::vector<uint8_t> data;
90 ESP_LOGV(TAG,
"Tag capacity is %u bytes", data[2] * 8U);
97 uint8_t &message_start_index) {
98 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
100 if (!(page_3_to_6.size() > p4_offset + 5)) {
104 if (page_3_to_6[p4_offset + 0] == 0x03) {
105 message_length = page_3_to_6[p4_offset + 1];
106 message_start_index = 2;
108 }
else if (page_3_to_6[p4_offset + 5] == 0x03) {
109 message_length = page_3_to_6[p4_offset + 6];
110 message_start_index = 7;
119 auto encoded = message->
encode();
121 uint32_t message_length = encoded.size();
124 if (buffer_length > capacity) {
125 ESP_LOGE(TAG,
"Message length exceeds tag capacity %" PRIu32
" > %" PRIu32, buffer_length, capacity);
129 encoded.insert(encoded.begin(), 0x03);
130 if (message_length < 255) {
131 encoded.insert(encoded.begin() + 1, message_length);
133 encoded.insert(encoded.begin() + 1, 0xFF);
134 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
135 encoded.insert(encoded.begin() + 2, message_length & 0xFF);
137 encoded.push_back(0xFE);
139 encoded.resize(buffer_length, 0);
142 uint8_t current_page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
144 while (index < buffer_length) {
145 std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
149 index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
157 uint8_t pages = (capacity / nfc::MIFARE_ULTRALIGHT_PAGE_SIZE) + nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
159 std::vector<uint8_t> blank_data = {0x00, 0x00, 0x00, 0x00};
161 for (
int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) {
170 std::vector<uint8_t> data({
171 PN532_COMMAND_INDATAEXCHANGE,
173 nfc::MIFARE_CMD_WRITE_ULTRALIGHT,
176 data.insert(data.end(), write_data.begin(), write_data.end());
178 ESP_LOGE(TAG,
"Error writing page %u", page_num);
182 std::vector<uint8_t> response;
183 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
184 ESP_LOGE(TAG,
"Error writing page %u", page_num);
virtual bool write_data(const std::vector< uint8_t > &data)=0
bool is_mifare_ultralight_formatted_(const std::vector< uint8_t > &page_3_to_6)
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
bool read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector< uint8_t > &data)
bool find_mifare_ultralight_ndef_(const std::vector< uint8_t > &page_3_to_6, uint8_t &message_length, uint8_t &message_start_index)
bool clean_mifare_ultralight_()
std::unique_ptr< nfc::NfcTag > read_mifare_ultralight_tag_(std::vector< uint8_t > &uid)
std::vector< uint8_t > encode()
Implementation of SPI Controller mode.
bool write_mifare_ultralight_page_(uint8_t page_num, std::vector< uint8_t > &write_data)
bool write_mifare_ultralight_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length)
bool write_command_(const std::vector< uint8_t > &data)
uint16_t read_mifare_ultralight_capacity_()
std::string format_bytes(std::vector< uint8_t > &bytes)