ESPHome 2026.3.0-dev
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 {
11namespace nfc {
12
13// NFC UIDs are 4, 7, or 10 bytes depending on tag type
14static constexpr size_t NFC_UID_MAX_LENGTH = 10;
16
17class NfcTag {
18 public:
19 NfcTag() { this->tag_type_ = "Unknown"; };
20 NfcTag(const NfcTagUid &uid) {
21 this->uid_ = uid;
22 this->tag_type_ = "Unknown";
23 };
24 NfcTag(const NfcTagUid &uid, const std::string &tag_type) {
25 this->uid_ = uid;
26 this->tag_type_ = tag_type;
27 };
28 NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::unique_ptr<nfc::NdefMessage> ndef_message) {
29 this->uid_ = uid;
30 this->tag_type_ = tag_type;
31 this->ndef_message_ = std::move(ndef_message);
32 };
33 NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::vector<uint8_t> &ndef_data) {
34 this->uid_ = uid;
35 this->tag_type_ = tag_type;
36 this->ndef_message_ = make_unique<NdefMessage>(ndef_data);
37 };
38 NfcTag(const NfcTag &rhs) {
39 uid_ = rhs.uid_;
40 tag_type_ = rhs.tag_type_;
41 if (rhs.ndef_message_ != nullptr)
42 ndef_message_ = make_unique<NdefMessage>(*rhs.ndef_message_);
43 }
44
45 NfcTagUid &get_uid() { return this->uid_; };
46 const std::string &get_tag_type() { return this->tag_type_; };
47 bool has_ndef_message() { return this->ndef_message_ != nullptr; };
48 const std::shared_ptr<NdefMessage> &get_ndef_message() { return this->ndef_message_; };
49 void set_ndef_message(std::unique_ptr<NdefMessage> ndef_message) { this->ndef_message_ = std::move(ndef_message); };
50
51 protected:
53 std::string tag_type_;
54 std::shared_ptr<NdefMessage> ndef_message_;
55};
56
57} // namespace nfc
58} // namespace esphome
NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::vector< uint8_t > &ndef_data)
Definition nfc_tag.h:33
NfcTag(const NfcTagUid &uid)
Definition nfc_tag.h:20
void set_ndef_message(std::unique_ptr< NdefMessage > ndef_message)
Definition nfc_tag.h:49
std::string tag_type_
Definition nfc_tag.h:53
NfcTag(const NfcTagUid &uid, const std::string &tag_type)
Definition nfc_tag.h:24
NfcTag(const NfcTag &rhs)
Definition nfc_tag.h:38
const std::shared_ptr< NdefMessage > & get_ndef_message()
Definition nfc_tag.h:48
const std::string & get_tag_type()
Definition nfc_tag.h:46
std::shared_ptr< NdefMessage > ndef_message_
Definition nfc_tag.h:54
NfcTag(const NfcTagUid &uid, const std::string &tag_type, std::unique_ptr< nfc::NdefMessage > ndef_message)
Definition nfc_tag.h:28
bool has_ndef_message()
Definition nfc_tag.h:47
NfcTagUid & get_uid()
Definition nfc_tag.h:45
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7