ESPHome 2026.5.1
Loading...
Searching...
No Matches
nfc_helpers.cpp
Go to the documentation of this file.
1#include "nfc_helpers.h"
2
3namespace esphome::nfc {
4
5static const char *const TAG = "nfc.helpers";
6
7bool has_ha_tag_ndef(NfcTag &tag) { return !get_ha_tag_ndef(tag).empty(); }
8
9std::string get_ha_tag_ndef(NfcTag &tag) {
10 if (!tag.has_ndef_message()) {
11 return std::string();
12 }
13 auto message = tag.get_ndef_message();
14 auto records = message->get_records();
15 for (const auto &record : records) {
16 std::string payload = record->get_payload();
17 size_t pos = payload.find(HA_TAG_ID_PREFIX);
18 if (pos != std::string::npos) {
19 return payload.substr(pos + sizeof(HA_TAG_ID_PREFIX) - 1);
20 }
21 }
22 return std::string();
23}
24
26 static const char ALPHANUM[] = "0123456789abcdef";
27 std::string uri = HA_TAG_ID_PREFIX;
28 for (int i = 0; i < 8; i++) {
29 uri += ALPHANUM[random_uint32() % (sizeof(ALPHANUM) - 1)];
30 }
31 uri += "-";
32 for (int j = 0; j < 3; j++) {
33 for (int i = 0; i < 4; i++) {
34 uri += ALPHANUM[random_uint32() % (sizeof(ALPHANUM) - 1)];
35 }
36 uri += "-";
37 }
38 for (int i = 0; i < 12; i++) {
39 uri += ALPHANUM[random_uint32() % (sizeof(ALPHANUM) - 1)];
40 }
41 ESP_LOGD(TAG, "Payload to be written: %s", uri.c_str());
42 return uri;
43}
44
45} // namespace esphome::nfc
const char * message
Definition component.cpp:35
std::string get_ha_tag_ndef(NfcTag &tag)
std::string get_random_ha_tag_ndef()
bool has_ha_tag_ndef(NfcTag &tag)
const char * tag
Definition log.h:74
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:12
size_t size_t pos
Definition helpers.h:1038