15 static const char *
const TAG =
"pn532";
18 ESP_LOGCONFIG(TAG,
"Setting up PN532...");
22 ESP_LOGW(TAG,
"Error sending version command, trying again...");
24 ESP_LOGE(TAG,
"Error sending version command");
30 std::vector<uint8_t> version_data;
31 if (!this->
read_response(PN532_COMMAND_VERSION_DATA, version_data)) {
32 ESP_LOGE(TAG,
"Error getting version");
36 ESP_LOGD(TAG,
"Found chip PN5%02X", version_data[0]);
37 ESP_LOGD(TAG,
"Firmware ver. %d.%d", 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)) {
185 bool same_uid =
true;
186 for (
size_t i = 0; i < nfcid.size(); i++)
194 if (next_task_ ==
READ) {
197 trigger->process(tag);
201 if (tag->has_ndef_message()) {
202 const auto &message = tag->get_ndef_message();
203 const auto &records = message->get_records();
204 ESP_LOGD(TAG,
" NDEF formatted records:");
205 for (
const auto &record : records) {
206 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
210 }
else if (next_task_ ==
CLEAN) {
211 ESP_LOGD(TAG,
" Tag cleaning...");
213 ESP_LOGE(TAG,
" Tag was not fully cleaned successfully");
215 ESP_LOGD(TAG,
" Tag cleaned!");
216 }
else if (next_task_ ==
FORMAT) {
217 ESP_LOGD(TAG,
" Tag formatting...");
219 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
221 ESP_LOGD(TAG,
" Tag formatted!");
222 }
else if (next_task_ ==
WRITE) {
224 ESP_LOGD(TAG,
" Tag writing...");
225 ESP_LOGD(TAG,
" Tag formatting...");
227 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
229 ESP_LOGD(TAG,
" Writing NDEF data");
231 ESP_LOGE(TAG,
" Failed to write message to tag");
233 ESP_LOGD(TAG,
" Finished writing NDEF data");
249 write_data.push_back(0x00);
252 write_data.push_back(0x00);
253 write_data.push_back(0xFF);
256 const uint8_t real_length = data.size() + 1;
258 write_data.push_back(real_length);
260 write_data.push_back(~real_length + 1);
263 write_data.push_back(0xD4);
268 for (uint8_t dat : data) {
269 write_data.push_back(dat);
274 write_data.push_back(~checksum + 1);
276 write_data.push_back(0x00);
284 ESP_LOGV(TAG,
"Reading ACK...");
286 std::vector<uint8_t> data;
291 bool matches = (data[1] == 0x00 &&
293 data[3] == 0xFF && data[4] == 0x00 &&
294 data[5] == 0xFF && data[6] == 0x00);
295 ESP_LOGV(TAG,
"ACK valid: %s", YESNO(matches));
300 ESP_LOGV(TAG,
"Sending ACK for abort");
301 this->
write_data({0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00});
305 ESP_LOGV(TAG,
"Sending NACK for retransmit");
306 this->
write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
311 if (this->rd_ready_ ==
READY) {
325 this->rd_ready_ =
READY;
330 ESP_LOGV(TAG,
"Timed out waiting for readiness from PN532!");
343 auto rdy = this->rd_ready_;
352 ESP_LOGV(TAG,
"Turning RF field OFF");
354 PN532_COMMAND_RFCONFIGURATION,
363 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
364 ESP_LOGD(TAG,
"Mifare classic");
366 }
else if (type == nfc::TAG_TYPE_2) {
367 ESP_LOGD(TAG,
"Mifare ultralight");
369 }
else if (type == nfc::TAG_TYPE_UNKNOWN) {
370 ESP_LOGV(TAG,
"Cannot determine tag type");
371 return make_unique<nfc::NfcTag>(uid);
373 return make_unique<nfc::NfcTag>(uid);
378 this->next_task_ =
READ;
379 ESP_LOGD(TAG,
"Waiting to read next tag");
382 this->next_task_ =
CLEAN;
383 ESP_LOGD(TAG,
"Waiting to clean next tag");
386 this->next_task_ =
FORMAT;
387 ESP_LOGD(TAG,
"Waiting to format next tag");
390 this->next_task_ =
WRITE;
392 ESP_LOGD(TAG,
"Waiting to write next tag");
397 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
399 }
else if (type == nfc::TAG_TYPE_2) {
402 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
408 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
410 }
else if (type == nfc::TAG_TYPE_2) {
413 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
419 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
421 }
else if (type == nfc::TAG_TYPE_2) {
424 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
431 ESP_LOGCONFIG(TAG,
"PN532:");
432 switch (this->error_code_) {
436 ESP_LOGE(TAG,
"Wake Up command failed!");
439 ESP_LOGE(TAG,
"SAM command failed!");
443 LOG_UPDATE_INTERVAL(
this);
446 LOG_BINARY_SENSOR(
" ",
"Tag", child);
451 if (data.size() != this->uid_.size())
454 for (
size_t i = 0; i < data.size(); i++) {
455 if (data[i] != this->uid_[i])
459 this->publish_state(
true);
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
uint32_t update_interval_
const float DATA
For components that import data from directly connected sensors like DHT.
std::vector< PN532BinarySensor * > binary_sensors_
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
void status_set_warning(const char *message="unspecified")
void dump_config() override
virtual bool is_read_ready()=0
std::string format_uid(std::vector< uint8_t > &uid)
std::unique_ptr< nfc::NfcTag > read_tag_(std::vector< uint8_t > &uid)
enum PN532ReadReady read_ready_(bool block)
uint32_t IRAM_ATTR HOT millis()
nfc::NdefMessage * next_task_message_to_write_
bool clean_tag_(std::vector< uint8_t > &uid)
std::vector< uint8_t > current_uid_
bool format_mifare_classic_mifare_(std::vector< uint8_t > &uid)
bool process(std::vector< uint8_t > &data)
uint8_t guess_tag_type(uint8_t uid_length)
void status_clear_warning()
bool clean_mifare_ultralight_()
CallbackManager< void()> on_finished_write_callback_
std::unique_ptr< nfc::NfcTag > read_mifare_ultralight_tag_(std::vector< uint8_t > &uid)
enum esphome::pn532::PN532::PN532Error NONE
virtual bool read_data(std::vector< uint8_t > &data, uint8_t len)=0
bool write_mifare_classic_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
void IRAM_ATTR HOT yield()
virtual void mark_failed()
Mark this component as failed.
float get_setup_priority() const override
Implementation of SPI Controller mode.
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)
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
bool format_tag_(std::vector< uint8_t > &uid)
bool write_command_(const std::vector< uint8_t > &data)
void write_mode(nfc::NdefMessage *message)
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
void IRAM_ATTR HOT delay(uint32_t ms)