ESPHome  2024.10.2
ndef_record.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/log.h"
4 #include "esphome/core/helpers.h"
5 
6 #include <vector>
7 
8 namespace esphome {
9 namespace nfc {
10 
11 static const uint8_t TNF_EMPTY = 0x00;
12 static const uint8_t TNF_WELL_KNOWN = 0x01;
13 static const uint8_t TNF_MIME_MEDIA = 0x02;
14 static const uint8_t TNF_ABSOLUTE_URI = 0x03;
15 static const uint8_t TNF_EXTERNAL_TYPE = 0x04;
16 static const uint8_t TNF_UNKNOWN = 0x05;
17 static const uint8_t TNF_UNCHANGED = 0x06;
18 static const uint8_t TNF_RESERVED = 0x07;
19 
20 class NdefRecord {
21  public:
23  NdefRecord(std::vector<uint8_t> payload_data);
24  void set_tnf(uint8_t tnf) { this->tnf_ = tnf; };
25  void set_type(const std::string &type) { this->type_ = type; };
26  void set_payload(const std::string &payload) { this->payload_ = payload; };
27  void set_id(const std::string &id) { this->id_ = id; };
28  NdefRecord(const NdefRecord &) = default;
29  virtual ~NdefRecord() {}
30  virtual std::unique_ptr<NdefRecord> clone() const { // To allow copying polymorphic classes
31  return make_unique<NdefRecord>(*this);
32  };
33 
34  uint32_t get_encoded_size();
35 
36  std::vector<uint8_t> encode(bool first, bool last);
37 
38  uint8_t create_flag_byte(bool first, bool last, size_t payload_size);
39 
40  const std::string &get_type() const { return this->type_; };
41  const std::string &get_id() const { return this->id_; };
42  virtual const std::string &get_payload() const { return this->payload_; };
43 
44  virtual std::vector<uint8_t> get_encoded_payload() {
45  std::vector<uint8_t> payload(this->payload_.begin(), this->payload_.end());
46  return payload;
47  };
48 
49  protected:
50  uint8_t tnf_;
51  std::string type_;
52  std::string id_;
53  std::string payload_;
54 };
55 
56 } // namespace nfc
57 } // namespace esphome
void set_id(const std::string &id)
Definition: ndef_record.h:27
void set_tnf(uint8_t tnf)
Definition: ndef_record.h:24
virtual std::vector< uint8_t > get_encoded_payload()
Definition: ndef_record.h:44
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition: helpers.h:703
std::vector< uint8_t > encode(bool first, bool last)
Definition: ndef_record.cpp:12
const std::string & get_id() const
Definition: ndef_record.h:41
virtual std::unique_ptr< NdefRecord > clone() const
Definition: ndef_record.h:30
virtual const std::string & get_payload() const
Definition: ndef_record.h:42
uint8_t type
void set_type(const std::string &type)
Definition: ndef_record.h:25
uint8_t create_flag_byte(bool first, bool last, size_t payload_size)
Definition: ndef_record.cpp:47
void set_payload(const std::string &payload)
Definition: ndef_record.h:26
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const std::string & get_type() const
Definition: ndef_record.h:40