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