ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
nfc_tag.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <vector>
5
7#include "esphome/core/log.h"
8#include "ndef_message.h"
9
10namespace esphome::nfc {
11
12// NFC UIDs are 4, 7, or 10 bytes depending on tag type
13static constexpr size_t NFC_UID_MAX_LENGTH = 10;
15
16class NfcTag {
17 public:
18 NfcTag() { this->tag_type_ = "Unknown"; };
19 NfcTag(const NfcTagUid &uid) {
20 this->uid_ = uid;
21 this->tag_type_ = "Unknown";
22 };
23 NfcTag(const NfcTagUid &uid, const std::string &tag_type) {
24 this->uid_ = uid;
25 this->tag_type_ = tag_type;
26 };
27 NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::unique_ptr<nfc::NdefMessage> ndef_message) {
28 this->uid_ = uid;
29 this->tag_type_ = tag_type;
30 this->ndef_message_ = std::move(ndef_message);
31 };
32 NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::vector<uint8_t> &ndef_data) {
33 this->uid_ = uid;
34 this->tag_type_ = tag_type;
35 this->ndef_message_ = make_unique<NdefMessage>(ndef_data);
36 };
37 NfcTag(const NfcTag &rhs) {
38 uid_ = rhs.uid_;
39 tag_type_ = rhs.tag_type_;
40 if (rhs.ndef_message_ != nullptr)
41 ndef_message_ = make_unique<NdefMessage>(*rhs.ndef_message_);
42 }
43
44 NfcTagUid &get_uid() { return this->uid_; };
45 const std::string &get_tag_type() { return this->tag_type_; };
46 bool has_ndef_message() { return this->ndef_message_ != nullptr; };
47 const std::shared_ptr<NdefMessage> &get_ndef_message() { return this->ndef_message_; };
48 void set_ndef_message(std::unique_ptr<NdefMessage> ndef_message) { this->ndef_message_ = std::move(ndef_message); };
49
50 protected:
52 std::string tag_type_;
53 std::shared_ptr<NdefMessage> ndef_message_;
54};
55
56} // namespace esphome::nfc
NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::vector< uint8_t > &ndef_data)
Definition nfc_tag.h:32
NfcTag(const NfcTagUid &uid)
Definition nfc_tag.h:19
void set_ndef_message(std::unique_ptr< NdefMessage > ndef_message)
Definition nfc_tag.h:48
std::string tag_type_
Definition nfc_tag.h:52
NfcTag(const NfcTagUid &uid, const std::string &tag_type)
Definition nfc_tag.h:23
NfcTag(const NfcTag &rhs)
Definition nfc_tag.h:37
const std::shared_ptr< NdefMessage > & get_ndef_message()
Definition nfc_tag.h:47
const std::string & get_tag_type()
Definition nfc_tag.h:45
std::shared_ptr< NdefMessage > ndef_message_
Definition nfc_tag.h:53
NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::unique_ptr< nfc::NdefMessage > ndef_message)
Definition nfc_tag.h:27
bool has_ndef_message()
Definition nfc_tag.h:46
NfcTagUid & get_uid()
Definition nfc_tag.h:44