ESPHome  2024.6.1
ndef_record_uri.cpp
Go to the documentation of this file.
1 #include "ndef_record_uri.h"
2 
3 namespace esphome {
4 namespace nfc {
5 
6 static const char *const TAG = "nfc.ndef_record_uri";
7 
8 NdefRecordUri::NdefRecordUri(const std::vector<uint8_t> &payload) {
9  if (payload.empty()) {
10  ESP_LOGE(TAG, "Record payload too short");
11  return;
12  }
13 
14  uint8_t payload_identifier = payload[0]; // First byte of payload is prefix code
15 
16  std::string uri(payload.begin() + 1, payload.end());
17 
18  if (payload_identifier > 0x00 && payload_identifier <= PAYLOAD_IDENTIFIERS_COUNT) {
19  uri.insert(0, PAYLOAD_IDENTIFIERS[payload_identifier]);
20  }
21 
22  this->tnf_ = TNF_WELL_KNOWN;
23  this->type_ = "U";
24  this->set_uri(uri);
25 }
26 
27 std::vector<uint8_t> NdefRecordUri::get_encoded_payload() {
28  std::vector<uint8_t> data;
29 
30  uint8_t payload_prefix = 0x00;
31  uint8_t payload_prefix_length = 0x00;
32  for (uint8_t i = 1; i < PAYLOAD_IDENTIFIERS_COUNT; i++) {
33  std::string prefix = PAYLOAD_IDENTIFIERS[i];
34  if (this->uri_.substr(0, prefix.length()).find(prefix) != std::string::npos) {
35  payload_prefix = i;
36  payload_prefix_length = prefix.length();
37  break;
38  }
39  }
40 
41  data.push_back(payload_prefix);
42 
43  data.insert(data.end(), this->uri_.begin() + payload_prefix_length, this->uri_.end());
44  return data;
45 }
46 
47 } // namespace nfc
48 } // namespace esphome
std::vector< uint8_t > get_encoded_payload() override
void set_uri(const std::string &uri)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7