ESPHome  2024.11.0
nfc_tag.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 
6 #include "esphome/core/log.h"
7 #include "esphome/core/helpers.h"
8 #include "ndef_message.h"
9 
10 namespace esphome {
11 namespace nfc {
12 
13 class NfcTag {
14  public:
15  NfcTag() {
16  this->uid_ = {};
17  this->tag_type_ = "Unknown";
18  };
19  NfcTag(std::vector<uint8_t> &uid) {
20  this->uid_ = uid;
21  this->tag_type_ = "Unknown";
22  };
23  NfcTag(std::vector<uint8_t> &uid, const std::string &tag_type) {
24  this->uid_ = uid;
25  this->tag_type_ = tag_type;
26  };
27  NfcTag(std::vector<uint8_t> &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(std::vector<uint8_t> &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  std::vector<uint8_t> &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:
51  std::vector<uint8_t> uid_;
52  std::string tag_type_;
53  std::shared_ptr<NdefMessage> ndef_message_;
54 };
55 
56 } // namespace nfc
57 } // namespace esphome
NfcTag(const NfcTag &rhs)
Definition: nfc_tag.h:37
std::vector< uint8_t > uid_
Definition: nfc_tag.h:48
const std::shared_ptr< NdefMessage > & get_ndef_message()
Definition: nfc_tag.h:47
void set_ndef_message(std::unique_ptr< NdefMessage > ndef_message)
Definition: nfc_tag.h:48
const std::string & get_tag_type()
Definition: nfc_tag.h:45
NfcTag(std::vector< uint8_t > &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
std::shared_ptr< NdefMessage > ndef_message_
Definition: nfc_tag.h:53
NfcTag(std::vector< uint8_t > &uid, const std::string &tag_type, std::vector< uint8_t > &ndef_data)
Definition: nfc_tag.h:32
std::vector< uint8_t > & get_uid()
Definition: nfc_tag.h:44
std::string tag_type_
Definition: nfc_tag.h:52
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
NfcTag(std::vector< uint8_t > &uid, const std::string &tag_type)
Definition: nfc_tag.h:23
NfcTag(std::vector< uint8_t > &uid)
Definition: nfc_tag.h:19