15static const char *
const TAG =
"pn532";
20 ESP_LOGW(TAG,
"Error sending version command, trying again");
22 ESP_LOGE(TAG,
"Error sending version command");
28 std::vector<uint8_t> version_data;
29 if (!this->
read_response(PN532_COMMAND_VERSION_DATA, version_data)) {
30 ESP_LOGE(TAG,
"Error getting version");
34 ESP_LOGD(TAG,
"Found chip PN5%02X", version_data[0]);
35 ESP_LOGD(TAG,
"Firmware ver. %d.%d", version_data[1], version_data[2]);
38 PN532_COMMAND_SAMCONFIGURATION,
43 ESP_LOGE(TAG,
"No wakeup ack");
48 std::vector<uint8_t> wakeup_result;
49 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, wakeup_result)) {
58 PN532_COMMAND_SAMCONFIGURATION,
68 std::vector<uint8_t> sam_result;
69 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, sam_result)) {
70 ESP_LOGV(TAG,
"Invalid SAM result: (%u)", sam_result.size());
71 for (uint8_t dat : sam_result) {
72 ESP_LOGV(TAG,
" 0x%02X", dat);
85 ESP_LOGI(TAG,
"Powering down PN532");
86 if (!this->
write_command_({PN532_COMMAND_POWERDOWN, 0b10100000})) {
87 ESP_LOGE(TAG,
"Error writing powerdown command to PN532");
90 std::vector<uint8_t> response;
91 if (!this->
read_response(PN532_COMMAND_POWERDOWN, response)) {
92 ESP_LOGE(TAG,
"Error reading PN532 powerdown response");
95 if (response[0] != 0x00) {
96 ESP_LOGE(TAG,
"Error on PN532 powerdown: %02x", response[0]);
99 ESP_LOGV(TAG,
"Powerdown successful");
112 PN532_COMMAND_INLISTPASSIVETARGET,
116 ESP_LOGW(TAG,
"Requesting tag read failed!");
132 bool success =
false;
133 std::vector<uint8_t> read;
135 if (ready ==
READY) {
136 success = this->
read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
146 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
148 trigger->process(tag);
155 uint8_t num_targets = read[0];
156 if (num_targets != 1) {
159 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
161 trigger->process(tag);
168 uint8_t nfcid_length = read[5];
169 std::vector<uint8_t> nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length);
170 if (read.size() < 6U + nfcid_length) {
177 if (bin_sens->process(nfcid)) {
182 if (nfcid.size() == this->current_uid_.size()) {
183 bool same_uid =
true;
184 for (
size_t i = 0; i < nfcid.size(); i++)
192 if (next_task_ ==
READ) {
195 trigger->process(tag);
199 if (tag->has_ndef_message()) {
200 const auto &message = tag->get_ndef_message();
201 const auto &records = message->get_records();
202 ESP_LOGD(TAG,
" NDEF formatted records:");
203 for (
const auto &record : records) {
204 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
208 }
else if (next_task_ ==
CLEAN) {
209 ESP_LOGD(TAG,
" Tag cleaning");
211 ESP_LOGE(TAG,
" Tag was not fully cleaned successfully");
213 ESP_LOGD(TAG,
" Tag cleaned!");
214 }
else if (next_task_ ==
FORMAT) {
215 ESP_LOGD(TAG,
" Tag formatting");
217 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
219 ESP_LOGD(TAG,
" Tag formatted!");
220 }
else if (next_task_ ==
WRITE) {
222 ESP_LOGD(TAG,
" Tag writing");
223 ESP_LOGD(TAG,
" Tag formatting");
225 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
227 ESP_LOGD(TAG,
" Writing NDEF data");
229 ESP_LOGE(TAG,
" Failed to write message to tag");
231 ESP_LOGD(TAG,
" Finished writing NDEF data");
254 const uint8_t real_length = data.size() + 1;
266 for (uint8_t dat : data) {
282 ESP_LOGV(TAG,
"Reading ACK");
284 std::vector<uint8_t> data;
289 bool matches = (data[1] == 0x00 &&
291 data[3] == 0xFF && data[4] == 0x00 &&
292 data[5] == 0xFF && data[6] == 0x00);
293 ESP_LOGV(TAG,
"ACK valid: %s", YESNO(matches));
298 ESP_LOGV(TAG,
"Sending ACK for abort");
299 this->
write_data({0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00});
303 ESP_LOGV(TAG,
"Sending NACK for retransmit");
304 this->
write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
309 if (this->rd_ready_ ==
READY) {
323 this->rd_ready_ =
READY;
328 ESP_LOGV(TAG,
"Timed out waiting for readiness from PN532!");
341 auto rdy = this->rd_ready_;
350 ESP_LOGV(TAG,
"Turning RF field OFF");
352 PN532_COMMAND_RFCONFIGURATION,
361 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
362 ESP_LOGD(TAG,
"Mifare classic");
364 }
else if (
type == nfc::TAG_TYPE_2) {
365 ESP_LOGD(TAG,
"Mifare ultralight");
367 }
else if (
type == nfc::TAG_TYPE_UNKNOWN) {
368 ESP_LOGV(TAG,
"Cannot determine tag type");
369 return make_unique<nfc::NfcTag>(uid);
371 return make_unique<nfc::NfcTag>(uid);
376 this->next_task_ =
READ;
377 ESP_LOGD(TAG,
"Waiting to read next tag");
380 this->next_task_ =
CLEAN;
381 ESP_LOGD(TAG,
"Waiting to clean next tag");
384 this->next_task_ =
FORMAT;
385 ESP_LOGD(TAG,
"Waiting to format next tag");
388 this->next_task_ =
WRITE;
390 ESP_LOGD(TAG,
"Waiting to write next tag");
395 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
397 }
else if (
type == nfc::TAG_TYPE_2) {
400 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
406 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
408 }
else if (
type == nfc::TAG_TYPE_2) {
411 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
417 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
419 }
else if (
type == nfc::TAG_TYPE_2) {
422 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
429 ESP_LOGCONFIG(TAG,
"PN532:");
430 switch (this->error_code_) {
434 ESP_LOGE(TAG,
"Wake Up command failed!");
437 ESP_LOGE(TAG,
"SAM command failed!");
441 LOG_UPDATE_INTERVAL(
this);
444 LOG_BINARY_SENSOR(
" ",
"Tag", child);
449 if (data.size() != this->uid_.size())
452 for (
size_t i = 0; i < data.size(); i++) {
453 if (data[i] != this->
uid_[i])
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
uint32_t update_interval_
void publish_state(bool new_state)
Publish a new state to the front-end.
std::vector< uint8_t > uid_
bool process(std::vector< uint8_t > &data)
virtual bool write_data(const std::vector< uint8_t > &data)=0
enum PN532ReadReady read_ready_(bool block)
virtual bool is_read_ready()=0
bool format_mifare_classic_ndef_(std::vector< uint8_t > &uid)
bool format_mifare_classic_mifare_(std::vector< uint8_t > &uid)
bool write_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
virtual bool read_data(std::vector< uint8_t > &data, uint8_t len)=0
std::vector< PN532BinarySensor * > binary_sensors_
void dump_config() override
std::unique_ptr< nfc::NfcTag > read_mifare_classic_tag_(std::vector< uint8_t > &uid)
enum esphome::pn532::PN532::NfcTask READ
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
bool format_tag_(std::vector< uint8_t > &uid)
float get_setup_priority() const override
std::unique_ptr< nfc::NfcTag > read_tag_(std::vector< uint8_t > &uid)
bool write_mifare_ultralight_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
bool clean_mifare_ultralight_()
CallbackManager< void()> on_finished_write_callback_
void write_mode(nfc::NdefMessage *message)
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
enum esphome::pn532::PN532::PN532Error NONE
bool clean_tag_(std::vector< uint8_t > &uid)
std::unique_ptr< nfc::NfcTag > read_mifare_ultralight_tag_(std::vector< uint8_t > &uid)
bool write_mifare_classic_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
bool write_command_(const std::vector< uint8_t > &data)
nfc::NdefMessage * next_task_message_to_write_
std::vector< uint8_t > current_uid_
std::string format_uid(const std::vector< uint8_t > &uid)
uint8_t guess_tag_type(uint8_t uid_length)
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT yield()
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()