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