ESPHome  2022.12.8
pn532.h
Go to the documentation of this file.
1 #pragma once
2 
9 
10 #include <vector>
11 
12 namespace esphome {
13 namespace pn532 {
14 
15 static const uint8_t PN532_COMMAND_VERSION_DATA = 0x02;
16 static const uint8_t PN532_COMMAND_SAMCONFIGURATION = 0x14;
17 static const uint8_t PN532_COMMAND_RFCONFIGURATION = 0x32;
18 static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
19 static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
20 
21 class PN532BinarySensor;
22 
23 class PN532 : public PollingComponent {
24  public:
25  void setup() override;
26 
27  void dump_config() override;
28 
29  void update() override;
30  float get_setup_priority() const override;
31 
32  void loop() override;
33 
34  void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); }
35  void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); }
37 
38  void add_on_finished_write_callback(std::function<void()> callback) {
39  this->on_finished_write_callback_.add(std::move(callback));
40  }
41 
42  bool is_writing() { return this->next_task_ != READ; };
43 
44  void read_mode();
45  void clean_mode();
46  void format_mode();
47  void write_mode(nfc::NdefMessage *message);
48 
49  protected:
50  void turn_off_rf_();
51  bool write_command_(const std::vector<uint8_t> &data);
52  bool read_ack_();
53  void send_nack_();
54 
55  virtual bool write_data(const std::vector<uint8_t> &data) = 0;
56  virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0;
57  virtual bool read_response(uint8_t command, std::vector<uint8_t> &data) = 0;
58 
59  std::unique_ptr<nfc::NfcTag> read_tag_(std::vector<uint8_t> &uid);
60 
61  bool format_tag_(std::vector<uint8_t> &uid);
62  bool clean_tag_(std::vector<uint8_t> &uid);
63  bool write_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message);
64 
65  std::unique_ptr<nfc::NfcTag> read_mifare_classic_tag_(std::vector<uint8_t> &uid);
66  bool read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
67  bool write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
68  bool auth_mifare_classic_block_(std::vector<uint8_t> &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key);
69  bool format_mifare_classic_mifare_(std::vector<uint8_t> &uid);
70  bool format_mifare_classic_ndef_(std::vector<uint8_t> &uid);
71  bool write_mifare_classic_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message);
72 
73  std::unique_ptr<nfc::NfcTag> read_mifare_ultralight_tag_(std::vector<uint8_t> &uid);
74  bool read_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &data);
77  bool find_mifare_ultralight_ndef_(uint8_t &message_length, uint8_t &message_start_index);
78  bool write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data);
79  bool write_mifare_ultralight_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message);
81 
82  bool requested_read_{false};
83  std::vector<PN532BinarySensor *> binary_sensors_;
84  std::vector<nfc::NfcOnTagTrigger *> triggers_ontag_;
85  std::vector<nfc::NfcOnTagTrigger *> triggers_ontagremoved_;
86  std::vector<uint8_t> current_uid_;
88  enum NfcTask {
89  READ = 0,
93  } next_task_{READ};
94  enum PN532Error {
95  NONE = 0,
98  } error_code_{NONE};
100 };
101 
103  public:
104  void set_uid(const std::vector<uint8_t> &uid) { uid_ = uid; }
105 
106  bool process(std::vector<uint8_t> &data);
107 
108  void on_scan_end() {
109  if (!this->found_) {
110  this->publish_state(false);
111  }
112  this->found_ = false;
113  }
114 
115  protected:
116  std::vector<uint8_t> uid_;
117  bool found_{false};
118 };
119 
121  public:
122  explicit PN532OnFinishedWriteTrigger(PN532 *parent) {
123  parent->add_on_finished_write_callback([this]() { this->trigger(); });
124  }
125 };
126 
127 template<typename... Ts> class PN532IsWritingCondition : public Condition<Ts...>, public Parented<PN532> {
128  public:
129  bool check(Ts... x) override { return this->parent_->is_writing(); }
130 };
131 
132 } // namespace pn532
133 } // 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:81
std::vector< PN532BinarySensor * > binary_sensors_
Definition: pn532.h:83
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
bool is_writing()
Definition: pn532.h:42
void dump_config() override
Definition: pn532.cpp:346
bool check(Ts... x) override
Definition: pn532.h:129
bool read_mifare_ultralight_page_(uint8_t page_num, std::vector< uint8_t > &data)
bool write_mifare_classic_block_(uint8_t block_num, std::vector< uint8_t > &data)
bool find_mifare_ultralight_ndef_(uint8_t &message_length, uint8_t &message_start_index)
void set_uid(const std::vector< uint8_t > &uid)
Definition: pn532.h:104
void register_ontagremoved_trigger(nfc::NfcOnTagTrigger *trig)
Definition: pn532.h:36
This class simplifies creating components that periodically check a state.
Definition: component.h:267
std::unique_ptr< nfc::NfcTag > read_tag_(std::vector< uint8_t > &uid)
Definition: pn532.cpp:276
nfc::NdefMessage * next_task_message_to_write_
Definition: pn532.h:87
bool clean_tag_(std::vector< uint8_t > &uid)
Definition: pn532.cpp:311
std::vector< uint8_t > current_uid_
Definition: pn532.h:86
void add_on_finished_write_callback(std::function< void()> callback)
Definition: pn532.h:38
bool requested_read_
Definition: pn532.h:82
bool format_mifare_classic_mifare_(std::vector< uint8_t > &uid)
Base class for all automation conditions.
Definition: automation.h:74
CallbackManager< void()> on_finished_write_callback_
Definition: pn532.h:99
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:116
void register_ontag_trigger(nfc::NfcOnTagTrigger *trig)
Definition: pn532.h:35
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:281
void register_tag(PN532BinarySensor *tag)
Definition: pn532.h:34
float get_setup_priority() const override
Definition: pn532.cpp:344
void loop() override
Definition: pn532.cpp:98
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:28
bool write_mifare_ultralight_page_(uint8_t page_num, std::vector< uint8_t > &write_data)
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:333
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
Definition: pn532.h:85
bool format_tag_(std::vector< uint8_t > &uid)
Definition: pn532.cpp:322
bool write_command_(const std::vector< uint8_t > &data)
Definition: pn532.cpp:208
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:500
void write_mode(nfc::NdefMessage *message)
Definition: pn532.cpp:305
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
Definition: pn532.h:84