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, Firmware v%d.%d", version_data[0], version_data[1], version_data[2]);
37 PN532_COMMAND_SAMCONFIGURATION,
42 ESP_LOGE(TAG,
"No wakeup ack");
47 std::vector<uint8_t> wakeup_result;
48 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, wakeup_result)) {
57 PN532_COMMAND_SAMCONFIGURATION,
67 std::vector<uint8_t> sam_result;
68 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, sam_result)) {
69 ESP_LOGV(TAG,
"Invalid SAM result: (%u)", sam_result.size());
70 for (uint8_t dat : sam_result) {
71 ESP_LOGV(TAG,
" 0x%02X", dat);
84 ESP_LOGI(TAG,
"Powering down PN532");
85 if (!this->
write_command_({PN532_COMMAND_POWERDOWN, 0b10100000})) {
86 ESP_LOGE(TAG,
"Error writing powerdown command to PN532");
89 std::vector<uint8_t> response;
90 if (!this->
read_response(PN532_COMMAND_POWERDOWN, response)) {
91 ESP_LOGE(TAG,
"Error reading PN532 powerdown response");
94 if (response[0] != 0x00) {
95 ESP_LOGE(TAG,
"Error on PN532 powerdown: %02x", response[0]);
98 ESP_LOGV(TAG,
"Powerdown successful");
111 PN532_COMMAND_INLISTPASSIVETARGET,
115 ESP_LOGW(TAG,
"Requesting tag read failed!");
131 bool success =
false;
132 std::vector<uint8_t> read;
134 if (ready ==
READY) {
135 success = this->
read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
145 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
147 trigger->process(tag);
154 uint8_t num_targets = read[0];
155 if (num_targets != 1) {
158 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
160 trigger->process(tag);
167 uint8_t nfcid_length = read[5];
168 if (nfcid_length > nfc::NFC_UID_MAX_LENGTH || read.size() < 6U + nfcid_length) {
172 nfc::NfcTagUid nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length);
176 if (bin_sens->process(nfcid)) {
181 if (nfcid.
size() == this->current_uid_.size()) {
182 bool same_uid =
true;
183 for (
size_t i = 0; i < nfcid.
size(); i++)
191 if (next_task_ ==
READ) {
194 trigger->process(tag);
197 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
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");
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(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.
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)
Providing packet encoding functions for exchanging data with a remote host.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()