ESPHome 2026.1.5
Loading...
Searching...
No Matches
pn7150_mifare_ultralight.cpp
Go to the documentation of this file.
1#include <cinttypes>
2#include <memory>
3
4#include "pn7150.h"
5#include "esphome/core/log.h"
6
7namespace esphome {
8namespace pn7150 {
9
10static const char *const TAG = "pn7150.mifare_ultralight";
11
13 std::vector<uint8_t> data;
14 // pages 3 to 6 contain various info we are interested in -- do one read to grab it all
15 if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE * nfc::MIFARE_ULTRALIGHT_READ_SIZE,
16 data) != nfc::STATUS_OK) {
17 return nfc::STATUS_FAILED;
18 }
19
20 if (!this->is_mifare_ultralight_formatted_(data)) {
21 ESP_LOGW(TAG, "Not NDEF formatted");
22 return nfc::STATUS_FAILED;
23 }
24
25 uint8_t message_length;
26 uint8_t message_start_index;
27 if (this->find_mifare_ultralight_ndef_(data, message_length, message_start_index) != nfc::STATUS_OK) {
28 ESP_LOGW(TAG, "Couldn't find NDEF message");
29 return nfc::STATUS_FAILED;
30 }
31 ESP_LOGVV(TAG, "NDEF message length: %u, start: %u", message_length, message_start_index);
32
33 if (message_length == 0) {
34 return nfc::STATUS_FAILED;
35 }
36 // we already read pages 3-6 earlier -- pick up where we left off so we're not re-reading pages
37 const uint8_t read_length = message_length + message_start_index > 12 ? message_length + message_start_index - 12 : 0;
38 if (read_length) {
39 if (read_mifare_ultralight_bytes_(nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE + 3, read_length, data) !=
40 nfc::STATUS_OK) {
41 ESP_LOGE(TAG, "Error reading tag data");
42 return nfc::STATUS_FAILED;
43 }
44 }
45 // we need to trim off page 3 as well as any bytes ahead of message_start_index
46 data.erase(data.begin(), data.begin() + message_start_index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
47
48 tag.set_ndef_message(make_unique<nfc::NdefMessage>(data));
49
50 return nfc::STATUS_OK;
51}
52
53uint8_t PN7150::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector<uint8_t> &data) {
54 const uint8_t read_increment = nfc::MIFARE_ULTRALIGHT_READ_SIZE * nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
56 nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {nfc::MIFARE_CMD_READ, start_page});
57
58 for (size_t i = 0; i * read_increment < num_bytes; i++) {
59 tx.get_message().back() = i * nfc::MIFARE_ULTRALIGHT_READ_SIZE + start_page;
60 do { // loop because sometimes we struggle here...???...
61 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
62 ESP_LOGE(TAG, "Error reading tag data");
63 return nfc::STATUS_FAILED;
64 }
65 } while (rx.get_payload_size() < read_increment);
66 uint16_t bytes_offset = (i + 1) * read_increment;
67 auto pages_in_end_itr = bytes_offset <= num_bytes ? rx.get_message().end() - 1
68 : rx.get_message().end() - (bytes_offset - num_bytes + 1);
69
70 if ((pages_in_end_itr > rx.get_message().begin()) && (pages_in_end_itr < rx.get_message().end())) {
71 data.insert(data.end(), rx.get_message().begin() + nfc::NCI_PKT_HEADER_SIZE, pages_in_end_itr);
72 }
73 }
74
75 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
76 ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes_to(buf, data));
77
78 return nfc::STATUS_OK;
79}
80
81bool PN7150::is_mifare_ultralight_formatted_(const std::vector<uint8_t> &page_3_to_6) {
82 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
83
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));
87}
88
90 std::vector<uint8_t> data;
91 if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE, data) == nfc::STATUS_OK) {
92 ESP_LOGV(TAG, "Tag capacity is %u bytes", data[2] * 8U);
93 return data[2] * 8U;
94 }
95 return 0;
96}
97
98uint8_t PN7150::find_mifare_ultralight_ndef_(const std::vector<uint8_t> &page_3_to_6, uint8_t &message_length,
99 uint8_t &message_start_index) {
100 const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
101
102 if (!(page_3_to_6.size() > p4_offset + 5)) {
103 return nfc::STATUS_FAILED;
104 }
105
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;
109 return nfc::STATUS_OK;
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;
113 return nfc::STATUS_OK;
114 }
115 return nfc::STATUS_FAILED;
116}
117
118uint8_t PN7150::write_mifare_ultralight_tag_(std::vector<uint8_t> &uid,
119 const std::shared_ptr<nfc::NdefMessage> &message) {
120 uint32_t capacity = this->read_mifare_ultralight_capacity_();
121
122 auto encoded = message->encode();
123
124 uint32_t message_length = encoded.size();
125 uint32_t buffer_length = nfc::get_mifare_ultralight_buffer_size(message_length);
126
127 if (buffer_length > capacity) {
128 ESP_LOGE(TAG, "Message length exceeds tag capacity %" PRIu32 " > %" PRIu32, buffer_length, capacity);
129 return nfc::STATUS_FAILED;
130 }
131
132 encoded.insert(encoded.begin(), 0x03);
133 if (message_length < 255) {
134 encoded.insert(encoded.begin() + 1, message_length);
135 } else {
136 encoded.insert(encoded.begin() + 1, 0xFF);
137 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
138 encoded.insert(encoded.begin() + 2, message_length & 0xFF);
139 }
140 encoded.push_back(0xFE);
141
142 encoded.resize(buffer_length, 0);
143
144 uint32_t index = 0;
145 uint8_t current_page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
146
147 while (index < buffer_length) {
148 std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
149 if (this->write_mifare_ultralight_page_(current_page, data) != nfc::STATUS_OK) {
150 return nfc::STATUS_FAILED;
151 }
152 index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
153 current_page++;
154 }
155 return nfc::STATUS_OK;
156}
157
159 uint32_t capacity = this->read_mifare_ultralight_capacity_();
160 uint8_t pages = (capacity / nfc::MIFARE_ULTRALIGHT_PAGE_SIZE) + nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
161
162 std::vector<uint8_t> blank_data = {0x00, 0x00, 0x00, 0x00};
163
164 for (int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) {
165 if (this->write_mifare_ultralight_page_(i, blank_data) != nfc::STATUS_OK) {
166 return nfc::STATUS_FAILED;
167 }
168 }
169 return nfc::STATUS_OK;
170}
171
172uint8_t PN7150::write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data) {
173 std::vector<uint8_t> payload = {nfc::MIFARE_CMD_WRITE_ULTRALIGHT, page_num};
174 payload.insert(payload.end(), write_data.begin(), write_data.end());
175
177 nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, payload);
178
179 if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
180 ESP_LOGE(TAG, "Error writing page %u", page_num);
181 return nfc::STATUS_FAILED;
182 }
183 return nfc::STATUS_OK;
184}
185
186} // namespace pn7150
187} // namespace esphome
uint8_t get_payload_size(bool recompute=false)
std::vector< uint8_t > & get_message()
void set_ndef_message(std::unique_ptr< NdefMessage > ndef_message)
Definition nfc_tag.h:48
uint8_t transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool expect_notification=true)
Definition pn7150.cpp:1095
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag)
uint8_t read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector< uint8_t > &data)
uint8_t find_mifare_ultralight_ndef_(const std::vector< uint8_t > &page_3_to_6, uint8_t &message_length, uint8_t &message_start_index)
uint8_t write_mifare_ultralight_page_(uint8_t page_num, std::vector< uint8_t > &write_data)
uint8_t write_mifare_ultralight_tag_(std::vector< uint8_t > &uid, const std::shared_ptr< nfc::NdefMessage > &message)
bool is_mifare_ultralight_formatted_(const std::vector< uint8_t > &page_3_to_6)
const char * message
Definition component.cpp:38
char * format_bytes_to(char *buffer, const std::vector< uint8_t > &bytes)
Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use.
Definition nfc.cpp:15
uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length)
Definition nfc.cpp:63
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7