ESPHome  2024.11.0
ndef_record_text.cpp
Go to the documentation of this file.
1 #include "ndef_record_text.h"
2 #include "ndef_record.h"
3 
4 namespace esphome {
5 namespace nfc {
6 
7 static const char *const TAG = "nfc.ndef_record_text";
8 
9 NdefRecordText::NdefRecordText(const std::vector<uint8_t> &payload) {
10  if (payload.empty()) {
11  ESP_LOGE(TAG, "Record payload too short");
12  return;
13  }
14 
15  uint8_t language_code_length = payload[0] & 0b00111111; // Todo, make use of encoding bit?
16 
17  this->language_code_ = std::string(payload.begin() + 1, payload.begin() + 1 + language_code_length);
18 
19  this->text_ = std::string(payload.begin() + 1 + language_code_length, payload.end());
20 
21  this->tnf_ = TNF_WELL_KNOWN;
22 
23  this->type_ = "T";
24 }
25 
26 std::vector<uint8_t> NdefRecordText::get_encoded_payload() {
27  std::vector<uint8_t> data;
28 
29  uint8_t flag_byte = this->language_code_.length() & 0b00111111; // UTF8 assumed
30 
31  data.push_back(flag_byte);
32 
33  data.insert(data.end(), this->language_code_.begin(), this->language_code_.end());
34 
35  data.insert(data.end(), this->text_.begin(), this->text_.end());
36  return data;
37 }
38 
39 } // namespace nfc
40 } // namespace esphome
std::vector< uint8_t > get_encoded_payload() override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7