ESPHome
2026.5.1
Loading...
Searching...
No Matches
esphome
components
nfc
nfc_helpers.cpp
Go to the documentation of this file.
1
#include "
nfc_helpers.h
"
2
3
namespace
esphome::nfc
{
4
5
static
const
char
*
const
TAG =
"nfc.helpers"
;
6
7
bool
has_ha_tag_ndef
(
NfcTag
&
tag
) {
return
!
get_ha_tag_ndef
(
tag
).empty(); }
8
9
std::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
25
std::string
get_random_ha_tag_ndef
() {
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
esphome::nfc::NfcTag
Definition
nfc_tag.h:16
message
const char * message
Definition
component.cpp:35
esphome::nfc
Definition
automation.cpp:4
esphome::nfc::get_ha_tag_ndef
std::string get_ha_tag_ndef(NfcTag &tag)
Definition
nfc_helpers.cpp:9
esphome::nfc::get_random_ha_tag_ndef
std::string get_random_ha_tag_ndef()
Definition
nfc_helpers.cpp:25
esphome::nfc::has_ha_tag_ndef
bool has_ha_tag_ndef(NfcTag &tag)
Definition
nfc_helpers.cpp:7
esphome::tag
const char * tag
Definition
log.h:74
esphome::random_uint32
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition
helpers.cpp:12
esphome::pos
size_t size_t pos
Definition
helpers.h:1038
nfc_helpers.h
Generated by
1.12.0