ESPHome  2024.11.1
ndef_record.cpp
Go to the documentation of this file.
1 #include "ndef_record.h"
2 
3 namespace esphome {
4 namespace nfc {
5 
6 static const char *const TAG = "nfc.ndef_record";
7 
8 NdefRecord::NdefRecord(std::vector<uint8_t> payload_data) {
9  this->payload_ = std::string(payload_data.begin(), payload_data.end());
10 }
11 
12 std::vector<uint8_t> NdefRecord::encode(bool first, bool last) {
13  std::vector<uint8_t> data;
14 
15  // Get encoded payload, this is overridden by more specific record classes
16  std::vector<uint8_t> payload_data = get_encoded_payload();
17 
18  size_t payload_length = payload_data.size();
19 
20  data.push_back(this->create_flag_byte(first, last, payload_length));
21 
22  data.push_back(this->type_.length());
23 
24  if (payload_length <= 255) {
25  data.push_back(payload_length);
26  } else {
27  data.push_back(0);
28  data.push_back(0);
29  data.push_back((payload_length >> 8) & 0xFF);
30  data.push_back(payload_length & 0xFF);
31  }
32 
33  if (this->id_.length()) {
34  data.push_back(this->id_.length());
35  }
36 
37  data.insert(data.end(), this->type_.begin(), this->type_.end());
38 
39  if (this->id_.length()) {
40  data.insert(data.end(), this->id_.begin(), this->id_.end());
41  }
42 
43  data.insert(data.end(), payload_data.begin(), payload_data.end());
44  return data;
45 }
46 
47 uint8_t NdefRecord::create_flag_byte(bool first, bool last, size_t payload_size) {
48  uint8_t value = this->tnf_ & 0b00000111;
49  if (first) {
50  value = value | 0x80; // Set MB bit
51  }
52  if (last) {
53  value = value | 0x40; // Set ME bit
54  }
55  if (payload_size <= 255) {
56  value = value | 0x10; // Set SR bit
57  }
58  if (this->id_.length()) {
59  value = value | 0x08; // Set IL bit
60  }
61  return value;
62 };
63 
64 } // namespace nfc
65 } // namespace esphome
virtual std::vector< uint8_t > get_encoded_payload()
Definition: ndef_record.h:44
std::vector< uint8_t > encode(bool first, bool last)
Definition: ndef_record.cpp:12
uint8_t create_flag_byte(bool first, bool last, size_t payload_size)
Definition: ndef_record.cpp:47
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7