ESPHome  2024.10.3
pn532.h
Go to the documentation of this file.
1 #pragma once
2 
9 
10 #include <cinttypes>
11 #include <vector>
12 
13 namespace esphome {
14 namespace pn532 {
15 
16 static const uint8_t PN532_COMMAND_VERSION_DATA = 0x02;
17 static const uint8_t PN532_COMMAND_SAMCONFIGURATION = 0x14;
18 static const uint8_t PN532_COMMAND_RFCONFIGURATION = 0x32;
19 static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
20 static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
21 static const uint8_t PN532_COMMAND_POWERDOWN = 0x16;
22 
27 };
28 
29 class PN532BinarySensor;
30 
31 class PN532 : public PollingComponent {
32  public:
33  void setup() override;
34 
35  void dump_config() override;
36 
37  void update() override;
38  float get_setup_priority() const override;
39 
40  void loop() override;
41  void on_shutdown() override { powerdown(); }
42 
43  void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); }
44  void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); }
46 
47  void add_on_finished_write_callback(std::function<void()> callback) {
48  this->on_finished_write_callback_.add(std::move(callback));
49  }
50 
51  bool is_writing() { return this->next_task_ != READ; };
52 
53  void read_mode();
54  void clean_mode();
55  void format_mode();
56  void write_mode(nfc::NdefMessage *message);
57  bool powerdown();
58 
59  protected:
60  void turn_off_rf_();
61  bool write_command_(const std::vector<uint8_t> &data);
62  bool read_ack_();
63  void send_ack_();
64  void send_nack_();
65 
66  enum PN532ReadReady read_ready_(bool block);
67  virtual bool is_read_ready() = 0;
68  virtual bool write_data(const std::vector<uint8_t> &data) = 0;
69  virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0;
70  virtual bool read_response(uint8_t command, std::vector<uint8_t> &data) = 0;
71 
72  std::unique_ptr<nfc::NfcTag> read_tag_(std::vector<uint8_t> &uid);
73 
74  bool format_tag_(std::vector<uint8_t> &uid);
75  bool clean_tag_(std::vector<uint8_t> &uid);
76  bool write_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message);
77 
78  std::unique_ptr<nfc::NfcTag> read_mifare_classic_tag_(std::vector<uint8_t> &uid);
79  bool read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
80  bool write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
81  bool auth_mifare_classic_block_(std::vector<uint8_t> &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key);
82  bool format_mifare_classic_mifare_(std::vector<uint8_t> &uid);
83  bool format_mifare_classic_ndef_(std::vector<uint8_t> &uid);
84  bool write_mifare_classic_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message);
85 
86  std::unique_ptr<nfc::NfcTag> read_mifare_ultralight_tag_(std::vector<uint8_t> &uid);
87  bool read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector<uint8_t> &data);
88  bool is_mifare_ultralight_formatted_(const std::vector<uint8_t> &page_3_to_6);
90  bool find_mifare_ultralight_ndef_(const std::vector<uint8_t> &page_3_to_6, uint8_t &message_length,
91  uint8_t &message_start_index);
92  bool write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data);
93  bool write_mifare_ultralight_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message);
95 
96  bool updates_enabled_{true};
97  bool requested_read_{false};
98  std::vector<PN532BinarySensor *> binary_sensors_;
99  std::vector<nfc::NfcOnTagTrigger *> triggers_ontag_;
100  std::vector<nfc::NfcOnTagTrigger *> triggers_ontagremoved_;
101  std::vector<uint8_t> current_uid_;
103  uint32_t rd_start_time_{0};
104  enum PN532ReadReady rd_ready_ { WOULDBLOCK };
105  enum NfcTask {
106  READ = 0,
110  } next_task_{READ};
111  enum PN532Error {
112  NONE = 0,
115  } error_code_{NONE};
117 };
118 
120  public:
121  void set_uid(const std::vector<uint8_t> &uid) { uid_ = uid; }
122 
123  bool process(std::vector<uint8_t> &data);
124 
125  void on_scan_end() {
126  if (!this->found_) {
127  this->publish_state(false);
128  }
129  this->found_ = false;
130  }
131 
132  protected:
133  std::vector<uint8_t> uid_;
134  bool found_{false};
135 };
136 
138  public:
139  explicit PN532OnFinishedWriteTrigger(PN532 *parent) {
140  parent->add_on_finished_write_callback([this]() { this->trigger(); });
141  }
142 };
143 
144 template<typename... Ts> class PN532IsWritingCondition : public Condition<Ts...>, public Parented<PN532> {
145  public:
146  bool check(Ts... x) override { return this->parent_->is_writing(); }
147 };
148 
149 } // namespace pn532
150 } // namespace esphome
bool format_mifare_classic_ndef_(std::vector< uint8_t > &uid)
enum esphome::pn532::PN532::NfcTask READ
virtual bool write_data(const std::vector< uint8_t > &data)=0
void update() override
Definition: pn532.cpp:106
std::vector< PN532BinarySensor * > binary_sensors_
Definition: pn532.h:98
bool is_mifare_ultralight_formatted_(const std::vector< uint8_t > &page_3_to_6)
bool updates_enabled_
Definition: pn532.h:96
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
bool is_writing()
Definition: pn532.h:51
void dump_config() override
Definition: pn532.cpp:430
virtual bool is_read_ready()=0
uint16_t x
Definition: tt21100.cpp:17
bool check(Ts... x) override
Definition: pn532.h:146
bool write_mifare_classic_block_(uint8_t block_num, std::vector< uint8_t > &data)
bool read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector< uint8_t > &data)
void set_uid(const std::vector< uint8_t > &uid)
Definition: pn532.h:121
void register_ontagremoved_trigger(nfc::NfcOnTagTrigger *trig)
Definition: pn532.h:45
This class simplifies creating components that periodically check a state.
Definition: component.h:283
std::unique_ptr< nfc::NfcTag > read_tag_(std::vector< uint8_t > &uid)
Definition: pn532.cpp:360
enum PN532ReadReady read_ready_(bool block)
Definition: pn532.cpp:310
void on_shutdown() override
Definition: pn532.h:41
nfc::NdefMessage * next_task_message_to_write_
Definition: pn532.h:102
bool clean_tag_(std::vector< uint8_t > &uid)
Definition: pn532.cpp:395
std::vector< uint8_t > current_uid_
Definition: pn532.h:101
void add_on_finished_write_callback(std::function< void()> callback)
Definition: pn532.h:47
bool requested_read_
Definition: pn532.h:97
bool format_mifare_classic_mifare_(std::vector< uint8_t > &uid)
Base class for all automation conditions.
Definition: automation.h:74
bool find_mifare_ultralight_ndef_(const std::vector< uint8_t > &page_3_to_6, uint8_t &message_length, uint8_t &message_start_index)
CallbackManager< void()> on_finished_write_callback_
Definition: pn532.h:116
std::unique_ptr< nfc::NfcTag > read_mifare_ultralight_tag_(std::vector< uint8_t > &uid)
enum esphome::pn532::PN532::PN532Error NONE
void setup() override
Definition: pn532.cpp:17
std::vector< uint8_t > uid_
Definition: pn532.h:133
void register_ontag_trigger(nfc::NfcOnTagTrigger *trig)
Definition: pn532.h:44
virtual bool read_data(std::vector< uint8_t > &data, uint8_t len)=0
bool auth_mifare_classic_block_(std::vector< uint8_t > &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key)
bool write_mifare_classic_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
std::string size_t len
Definition: helpers.h:292
void register_tag(PN532BinarySensor *tag)
Definition: pn532.h:43
float get_setup_priority() const override
Definition: pn532.cpp:428
void loop() override
Definition: pn532.cpp:126
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
bool write_mifare_ultralight_page_(uint8_t page_num, std::vector< uint8_t > &write_data)
uint32_t rd_start_time_
Definition: pn532.h:103
bool write_mifare_ultralight_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
std::unique_ptr< nfc::NfcTag > read_mifare_classic_tag_(std::vector< uint8_t > &uid)
bool write_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
Definition: pn532.cpp:417
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
Definition: pn532.h:100
bool format_tag_(std::vector< uint8_t > &uid)
Definition: pn532.cpp:406
bool write_command_(const std::vector< uint8_t > &data)
Definition: pn532.cpp:246
bool read_mifare_classic_block_(uint8_t block_num, std::vector< uint8_t > &data)
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521
void write_mode(nfc::NdefMessage *message)
Definition: pn532.cpp:389
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
Definition: pn532.h:99