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