14static const char *
const TAG =
"pn532";
19 ESP_LOGW(TAG,
"Error sending version command, trying again");
21 ESP_LOGE(TAG,
"Error sending version command");
27 std::vector<uint8_t> version_data;
28 if (!this->
read_response(PN532_COMMAND_VERSION_DATA, version_data)) {
29 ESP_LOGE(TAG,
"Error getting version");
33 ESP_LOGD(TAG,
"Found chip PN5%02X, Firmware v%d.%d", version_data[0], version_data[1], version_data[2]);
36 PN532_COMMAND_SAMCONFIGURATION,
41 ESP_LOGE(TAG,
"No wakeup ack");
46 std::vector<uint8_t> wakeup_result;
47 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, wakeup_result)) {
56 PN532_COMMAND_SAMCONFIGURATION,
66 std::vector<uint8_t> sam_result;
67 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, sam_result)) {
68 ESP_LOGV(TAG,
"Invalid SAM result: (%u)", sam_result.size());
69 for (uint8_t dat : sam_result) {
70 ESP_LOGV(TAG,
" 0x%02X", dat);
83 ESP_LOGI(TAG,
"Powering down PN532");
84 if (!this->
write_command_({PN532_COMMAND_POWERDOWN, 0b10100000})) {
85 ESP_LOGE(TAG,
"Error writing powerdown command to PN532");
88 std::vector<uint8_t> response;
89 if (!this->
read_response(PN532_COMMAND_POWERDOWN, response)) {
90 ESP_LOGE(TAG,
"Error reading PN532 powerdown response");
93 if (response[0] != 0x00) {
94 ESP_LOGE(TAG,
"Error on PN532 powerdown: %02x", response[0]);
97 ESP_LOGV(TAG,
"Powerdown successful");
110 PN532_COMMAND_INLISTPASSIVETARGET,
114 ESP_LOGW(TAG,
"Requesting tag read failed!");
130 bool success =
false;
131 std::vector<uint8_t> read;
133 if (ready ==
READY) {
134 success = this->
read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
146 trigger->process(
tag);
153 uint8_t num_targets = read[0];
154 if (num_targets != 1) {
159 trigger->process(
tag);
166 uint8_t nfcid_length = read[5];
167 if (nfcid_length > nfc::NFC_UID_MAX_LENGTH || read.size() < 6U + nfcid_length) {
171 nfc::NfcTagUid nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length);
175 if (bin_sens->process(nfcid)) {
180 if (nfcid.
size() == this->current_uid_.size()) {
181 bool same_uid =
true;
182 for (
size_t i = 0; i < nfcid.
size(); i++)
190 if (next_task_ ==
READ) {
193 trigger->process(
tag);
196 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
198 if (
tag->has_ndef_message()) {
199 const auto &
message =
tag->get_ndef_message();
200 const auto &records =
message->get_records();
201 ESP_LOGD(TAG,
" NDEF formatted records:");
202 for (
const auto &record : records) {
203 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
207 }
else if (next_task_ ==
CLEAN) {
208 ESP_LOGD(TAG,
" Tag cleaning");
210 ESP_LOGE(TAG,
" Tag was not fully cleaned successfully");
212 ESP_LOGD(TAG,
" Tag cleaned!");
213 }
else if (next_task_ ==
FORMAT) {
214 ESP_LOGD(TAG,
" Tag formatting");
216 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
218 ESP_LOGD(TAG,
" Tag formatted!");
219 }
else if (next_task_ ==
WRITE) {
221 ESP_LOGD(TAG,
" Tag writing");
222 ESP_LOGD(TAG,
" Tag formatting");
224 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
226 ESP_LOGD(TAG,
" Writing NDEF data");
228 ESP_LOGE(TAG,
" Failed to write message to tag");
230 ESP_LOGD(TAG,
" Finished writing NDEF data");
253 const uint8_t real_length = data.size() + 1;
265 for (uint8_t dat : data) {
281 ESP_LOGV(TAG,
"Reading ACK");
283 std::vector<uint8_t> data;
288 bool matches = (data[1] == 0x00 &&
290 data[3] == 0xFF && data[4] == 0x00 &&
291 data[5] == 0xFF && data[6] == 0x00);
292 ESP_LOGV(TAG,
"ACK valid: %s", YESNO(matches));
297 ESP_LOGV(TAG,
"Sending ACK for abort");
298 this->
write_data({0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00});
302 ESP_LOGV(TAG,
"Sending NACK for retransmit");
303 this->
write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
308 if (this->rd_ready_ ==
READY) {
323 this->rd_ready_ =
READY;
327 if (
millis() - rd_start_time > 100) {
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");
427 ESP_LOGCONFIG(TAG,
"PN532:");
428 switch (this->error_code_) {
432 ESP_LOGE(TAG,
"Wake Up command failed!");
435 ESP_LOGE(TAG,
"SAM command failed!");
439 LOG_UPDATE_INTERVAL(
this);
442 LOG_BINARY_SENSOR(
" ",
"Tag", child);
447 if (data.size() != this->uid_.size())
450 for (
size_t i = 0; i < data.size(); i++) {
451 if (data[i] != this->
uid_[i])
void mark_failed()
Mark this component as failed.
void status_set_warning()
void status_clear_warning()
uint32_t update_interval_
void publish_state(bool new_state)
Publish a new state to the front-end.
bool process(const nfc::NfcTagUid &data)
virtual bool write_data(const std::vector< uint8_t > &data)=0
nfc::NfcTagUid current_uid_
enum PN532ReadReady read_ready_(bool block)
virtual bool is_read_ready()=0
optional< uint32_t > rd_start_time_
bool format_mifare_classic_mifare_(nfc::NfcTagUid &uid)
std::unique_ptr< nfc::NfcTag > read_mifare_ultralight_tag_(nfc::NfcTagUid &uid)
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_
bool format_mifare_classic_ndef_(nfc::NfcTagUid &uid)
void dump_config() override
bool write_mifare_classic_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message)
std::unique_ptr< nfc::NfcTag > read_mifare_classic_tag_(nfc::NfcTagUid &uid)
enum esphome::pn532::PN532::NfcTask READ
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
bool clean_mifare_ultralight_()
CallbackManager< void()> on_finished_write_callback_
void write_mode(nfc::NdefMessage *message)
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
bool write_mifare_ultralight_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message)
enum esphome::pn532::PN532::PN532Error NONE
bool clean_tag_(nfc::NfcTagUid &uid)
bool write_command_(const std::vector< uint8_t > &data)
bool format_tag_(nfc::NfcTagUid &uid)
nfc::NdefMessage * next_task_message_to_write_
bool write_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message)
std::unique_ptr< nfc::NfcTag > read_tag_(nfc::NfcTagUid &uid)
char * format_uid_to(char *buffer, std::span< const uint8_t > uid)
Format UID to buffer with '-' separator (e.g., "04-11-22-33"). Returns buffer for inline use.
uint8_t guess_tag_type(uint8_t uid_length)
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()