ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ndef_record_uri.cpp
Go to the documentation of this file.
1#include "ndef_record_uri.h"
3
4namespace esphome::nfc {
5
6ESPHOME_LOG_TAG(TAG, "nfc.ndef_record_uri");
7
8// URI identifier codes from the NFC Forum URI record type definition
9PROGMEM_STRING_TABLE(UriPrefixStrings, "", "http://www.", "https://www.", "http://", "https://",
10 "tel:", "mailto:", "ftp://anonymous:anonymous@", "ftp://ftp.", "ftps://", "sftp://", "smb://",
11 "nfs://", "ftp://", "dav://", "news:", "telnet://", "imap:", "rtsp://",
12 "urn:", "pop:", "sip:", "sips:", "tftp:", "btspp://", "btl2cap://", "btgoep://", "tcpobex://",
13 "irdaobex://", "file://",
14 "urn:epc:id:", "urn:epc:tag:", "urn:epc:pat:", "urn:epc:raw:", "urn:epc:", "urn:nfc:");
15
16NdefRecordUri::NdefRecordUri(const std::vector<uint8_t> &payload) {
17 if (payload.empty()) {
18 ESP_LOGE(TAG, "Record payload too short");
19 return;
20 }
21
22 uint8_t payload_identifier = payload[0]; // First byte of payload is prefix code
23
24 std::string uri(payload.begin() + 1, payload.end());
25
26 if (payload_identifier > 0x00 && payload_identifier <= UriPrefixStrings::LAST_INDEX) {
27 char prefix[UriPrefixStrings::MAX_LENGTH + 1];
28 ESPHOME_strncpy_P(prefix, reinterpret_cast<ESPHOME_PGM_P>(UriPrefixStrings::get_progmem_str(payload_identifier, 0)),
29 sizeof(prefix));
30 uri.insert(0, prefix);
31 }
32
33 this->tnf_ = TNF_WELL_KNOWN;
34 this->type_ = "U";
35 this->set_uri(uri);
36}
37
38std::vector<uint8_t> NdefRecordUri::get_encoded_payload() {
39 std::vector<uint8_t> data;
40
41 uint8_t payload_prefix = 0x00;
42 uint8_t payload_prefix_length = 0x00;
43 for (uint8_t i = 1; i < UriPrefixStrings::LAST_INDEX; i++) {
44 const auto *prefix = reinterpret_cast<ESPHOME_PGM_P>(UriPrefixStrings::get_progmem_str(i, 0));
45 const size_t length = ESPHOME_strlen_P(prefix);
46 if (this->uri_.size() >= length && ESPHOME_strncmp_P(this->uri_.c_str(), prefix, length) == 0) {
47 payload_prefix = i;
48 payload_prefix_length = length;
49 break;
50 }
51 }
52
53 data.push_back(payload_prefix);
54
55 data.insert(data.end(), this->uri_.begin() + payload_prefix_length, this->uri_.end());
56 return data;
57}
58
59} // namespace esphome::nfc
std::vector< uint8_t > get_encoded_payload() override
void set_uri(const std::string &uri)
ESPHOME_LOG_TAG(TAG, "nfc.binary_sensor")
PROGMEM_STRING_TABLE(UriPrefixStrings, "", "http://www.", "https://www.", "http://", "https://", "tel:", "mailto:", "ftp://anonymous:anonymous@", "ftp://ftp.", "ftps://", "sftp://", "smb://", "nfs://", "ftp://", "dav://", "news:", "telnet://", "imap:", "rtsp://", "urn:", "pop:", "sip:", "sips:", "tftp:", "btspp://", "btl2cap://", "btgoep://", "tcpobex://", "irdaobex://", "file://", "urn:epc:id:", "urn:epc:tag:", "urn:epc:pat:", "urn:epc:raw:", "urn:epc:", "urn:nfc:")
uint16_t length
Definition tt21100.cpp:0