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");
35 "Found chip PN5%02X\n"
36 "Firmware ver. %d.%d",
37 version_data[0], version_data[1], version_data[2]);
40 PN532_COMMAND_SAMCONFIGURATION,
45 ESP_LOGE(TAG,
"No wakeup ack");
50 std::vector<uint8_t> wakeup_result;
51 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, wakeup_result)) {
60 PN532_COMMAND_SAMCONFIGURATION,
70 std::vector<uint8_t> sam_result;
71 if (!this->
read_response(PN532_COMMAND_SAMCONFIGURATION, sam_result)) {
72 ESP_LOGV(TAG,
"Invalid SAM result: (%u)", sam_result.size());
73 for (uint8_t dat : sam_result) {
74 ESP_LOGV(TAG,
" 0x%02X", dat);
87 ESP_LOGI(TAG,
"Powering down PN532");
88 if (!this->
write_command_({PN532_COMMAND_POWERDOWN, 0b10100000})) {
89 ESP_LOGE(TAG,
"Error writing powerdown command to PN532");
92 std::vector<uint8_t> response;
93 if (!this->
read_response(PN532_COMMAND_POWERDOWN, response)) {
94 ESP_LOGE(TAG,
"Error reading PN532 powerdown response");
97 if (response[0] != 0x00) {
98 ESP_LOGE(TAG,
"Error on PN532 powerdown: %02x", response[0]);
101 ESP_LOGV(TAG,
"Powerdown successful");
114 PN532_COMMAND_INLISTPASSIVETARGET,
118 ESP_LOGW(TAG,
"Requesting tag read failed!");
134 bool success =
false;
135 std::vector<uint8_t> read;
137 if (ready ==
READY) {
138 success = this->
read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
148 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
150 trigger->process(tag);
157 uint8_t num_targets = read[0];
158 if (num_targets != 1) {
161 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
163 trigger->process(tag);
170 uint8_t nfcid_length = read[5];
171 std::vector<uint8_t> nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length);
172 if (read.size() < 6U + nfcid_length) {
179 if (bin_sens->process(nfcid)) {
184 if (nfcid.size() == this->current_uid_.size()) {
185 bool same_uid =
true;
186 for (
size_t i = 0; i < nfcid.size(); i++)
194 if (next_task_ ==
READ) {
197 trigger->process(tag);
200 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
202 if (tag->has_ndef_message()) {
203 const auto &
message = tag->get_ndef_message();
204 const auto &records =
message->get_records();
205 ESP_LOGD(TAG,
" NDEF formatted records:");
206 for (
const auto &record : records) {
207 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
211 }
else if (next_task_ ==
CLEAN) {
212 ESP_LOGD(TAG,
" Tag cleaning");
214 ESP_LOGE(TAG,
" Tag was not fully cleaned successfully");
216 ESP_LOGD(TAG,
" Tag cleaned!");
217 }
else if (next_task_ ==
FORMAT) {
218 ESP_LOGD(TAG,
" Tag formatting");
220 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
222 ESP_LOGD(TAG,
" Tag formatted!");
223 }
else if (next_task_ ==
WRITE) {
225 ESP_LOGD(TAG,
" Tag writing");
226 ESP_LOGD(TAG,
" Tag formatting");
228 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
230 ESP_LOGD(TAG,
" Writing NDEF data");
232 ESP_LOGE(TAG,
" Failed to write message to tag");
234 ESP_LOGD(TAG,
" Finished writing NDEF data");
257 const uint8_t real_length = data.size() + 1;
269 for (uint8_t dat : data) {
285 ESP_LOGV(TAG,
"Reading ACK");
287 std::vector<uint8_t> data;
292 bool matches = (data[1] == 0x00 &&
294 data[3] == 0xFF && data[4] == 0x00 &&
295 data[5] == 0xFF && data[6] == 0x00);
296 ESP_LOGV(TAG,
"ACK valid: %s", YESNO(matches));
301 ESP_LOGV(TAG,
"Sending ACK for abort");
302 this->
write_data({0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00});
306 ESP_LOGV(TAG,
"Sending NACK for retransmit");
307 this->
write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
312 if (this->rd_ready_ ==
READY) {
326 this->rd_ready_ =
READY;
331 ESP_LOGV(TAG,
"Timed out waiting for readiness from PN532!");
344 auto rdy = this->rd_ready_;
353 ESP_LOGV(TAG,
"Turning RF field OFF");
355 PN532_COMMAND_RFCONFIGURATION,
364 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
365 ESP_LOGD(TAG,
"Mifare classic");
367 }
else if (
type == nfc::TAG_TYPE_2) {
368 ESP_LOGD(TAG,
"Mifare ultralight");
370 }
else if (
type == nfc::TAG_TYPE_UNKNOWN) {
371 ESP_LOGV(TAG,
"Cannot determine tag type");
372 return make_unique<nfc::NfcTag>(uid);
374 return make_unique<nfc::NfcTag>(uid);
379 this->next_task_ =
READ;
380 ESP_LOGD(TAG,
"Waiting to read next tag");
383 this->next_task_ =
CLEAN;
384 ESP_LOGD(TAG,
"Waiting to clean next tag");
387 this->next_task_ =
FORMAT;
388 ESP_LOGD(TAG,
"Waiting to format next tag");
391 this->next_task_ =
WRITE;
393 ESP_LOGD(TAG,
"Waiting to write next tag");
398 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
400 }
else if (
type == nfc::TAG_TYPE_2) {
403 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
409 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
411 }
else if (
type == nfc::TAG_TYPE_2) {
414 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
420 if (
type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
422 }
else if (
type == nfc::TAG_TYPE_2) {
425 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
432 ESP_LOGCONFIG(TAG,
"PN532:");
433 switch (this->error_code_) {
437 ESP_LOGE(TAG,
"Wake Up command failed!");
440 ESP_LOGE(TAG,
"SAM command failed!");
444 LOG_UPDATE_INTERVAL(
this);
447 LOG_BINARY_SENSOR(
" ",
"Tag", child);
452 if (data.size() != this->uid_.size())
455 for (
size_t i = 0; i < data.size(); i++) {
456 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_
char * format_uid_to(char *buffer, const std::vector< 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)
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()