ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ndef_message.cpp
Go to the documentation of this file.
1#include "ndef_message.h"
2#include <cinttypes>
3
4namespace esphome::nfc {
5
6static const char *const TAG = "nfc.ndef_message";
7
8NdefMessage::NdefMessage(std::vector<uint8_t> &data) {
9 ESP_LOGV(TAG, "Building NdefMessage with %zu bytes", data.size());
10 size_t index = 0;
11 while (index < data.size()) {
12 // Minimum record: TNF byte + type length byte + payload length (1 or 4 bytes)
13 if (index + 2 >= data.size()) {
14 ESP_LOGE(TAG, "Truncated record header; aborting");
15 break;
16 }
17
18 uint8_t tnf_byte = data[index++];
19 bool me = tnf_byte & 0x40; // Message End bit (is set if this is the last record of the message)
20 bool sr = tnf_byte & 0x10; // Short record bit (is set if payload size is less or equal to 255 bytes)
21 bool il = tnf_byte & 0x08; // ID length bit (is set if ID Length field exists)
22 uint8_t tnf = tnf_byte & 0x07; // Type Name Format
23
24 ESP_LOGVV(TAG, "me=%s, sr=%s, il=%s, tnf=%d", YESNO(me), YESNO(sr), YESNO(il), tnf);
25
26 uint8_t type_length = data[index++];
27 uint32_t payload_length = 0;
28 if (sr) {
29 payload_length = data[index++];
30 } else {
31 if (index + 4 > data.size()) {
32 ESP_LOGE(TAG, "Truncated payload length; aborting");
33 break;
34 }
35 payload_length = (static_cast<uint32_t>(data[index]) << 24) | (static_cast<uint32_t>(data[index + 1]) << 16) |
36 (static_cast<uint32_t>(data[index + 2]) << 8) | static_cast<uint32_t>(data[index + 3]);
37 index += 4;
38 }
39
40 uint8_t id_length = 0;
41 if (il) {
42 if (index >= data.size()) {
43 ESP_LOGE(TAG, "Truncated ID length; aborting");
44 break;
45 }
46 id_length = data[index++];
47 }
48
49 ESP_LOGVV(TAG, "Lengths: type=%d, payload=%" PRIu32 ", id=%d", type_length, payload_length, id_length);
50
51 std::string type_str(data.begin() + index, data.begin() + index + type_length);
52
53 index += type_length;
54
55 std::string id_str;
56 if (il) {
57 id_str = std::string(data.begin() + index, data.begin() + index + id_length);
58 index += id_length;
59 }
60
61 if ((data.begin() + index > data.end()) || (data.begin() + index + payload_length > data.end())) {
62 ESP_LOGE(TAG, "Corrupt record encountered; NdefMessage constructor aborting");
63 break;
64 }
65
66 std::vector<uint8_t> payload_data(data.begin() + index, data.begin() + index + payload_length);
67
68 std::unique_ptr<NdefRecord> record;
69
70 // Based on tnf and type, create a more specific NdefRecord object
71 // constructed from the payload data
72 if (tnf == TNF_WELL_KNOWN && type_str == "U") {
73 record = make_unique<NdefRecordUri>(payload_data);
74 } else if (tnf == TNF_WELL_KNOWN && type_str == "T") {
75 record = make_unique<NdefRecordText>(payload_data);
76 } else {
77 // Could not recognize the record, so store as generic one.
78 record = make_unique<NdefRecord>(payload_data);
79 record->set_tnf(tnf);
80 record->set_type(type_str);
81 }
82
83 record->set_id(id_str);
84
85 index += payload_length;
86
87 ESP_LOGV(TAG, "Adding record type %s = %s", record->get_type().c_str(), record->get_payload().c_str());
88 this->add_record(std::move(record));
89
90 if (me)
91 break;
92 }
93}
94
95bool NdefMessage::add_record(std::unique_ptr<NdefRecord> record) {
96 if (this->records_.size() >= MAX_NDEF_RECORDS) {
97 ESP_LOGE(TAG, "Too many records. Max: %d", MAX_NDEF_RECORDS);
98 return false;
99 }
100 this->records_.emplace_back(std::move(record));
101 return true;
102}
103
104bool NdefMessage::add_text_record(const std::string &text) { return this->add_text_record(text, "en"); };
105
106bool NdefMessage::add_text_record(const std::string &text, const std::string &encoding) {
107 return this->add_record(make_unique<NdefRecordText>(encoding, text));
108}
109
110bool NdefMessage::add_uri_record(const std::string &uri) { return this->add_record(make_unique<NdefRecordUri>(uri)); }
111
112std::vector<uint8_t> NdefMessage::encode() {
113 std::vector<uint8_t> data;
114
115 for (size_t i = 0; i < this->records_.size(); i++) {
116 auto encoded_record = this->records_[i]->encode(i == 0, (i + 1) == this->records_.size());
117 data.insert(data.end(), encoded_record.begin(), encoded_record.end());
118 }
119 return data;
120}
121
122} // namespace esphome::nfc
bool add_uri_record(const std::string &uri)
std::vector< std::shared_ptr< NdefRecord > > records_
bool add_text_record(const std::string &text)
bool add_record(std::unique_ptr< NdefRecord > record)
std::vector< uint8_t > encode()
static void uint32_t