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