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!");
130 std::vector<uint8_t> read;
131 bool success = this->
read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
138 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
140 trigger->process(tag);
147 uint8_t num_targets = read[0];
148 if (num_targets != 1) {
151 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
153 trigger->process(tag);
160 uint8_t nfcid_length = read[5];
161 std::vector<uint8_t> nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length);
162 if (read.size() < 6U + nfcid_length) {
169 if (bin_sens->process(nfcid)) {
175 bool same_uid =
true;
176 for (
size_t i = 0; i < nfcid.size(); i++)
184 if (next_task_ ==
READ) {
187 trigger->process(tag);
191 if (tag->has_ndef_message()) {
192 const auto &message = tag->get_ndef_message();
193 const auto &records = message->get_records();
194 ESP_LOGD(TAG,
" NDEF formatted records:");
195 for (
const auto &record : records) {
196 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
200 }
else if (next_task_ ==
CLEAN) {
201 ESP_LOGD(TAG,
" Tag cleaning...");
203 ESP_LOGE(TAG,
" Tag was not fully cleaned successfully");
205 ESP_LOGD(TAG,
" Tag cleaned!");
206 }
else if (next_task_ ==
FORMAT) {
207 ESP_LOGD(TAG,
" Tag formatting...");
209 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
211 ESP_LOGD(TAG,
" Tag formatted!");
212 }
else if (next_task_ ==
WRITE) {
214 ESP_LOGD(TAG,
" Tag writing...");
215 ESP_LOGD(TAG,
" Tag formatting...");
217 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
219 ESP_LOGD(TAG,
" Writing NDEF data");
221 ESP_LOGE(TAG,
" Failed to write message to tag");
223 ESP_LOGD(TAG,
" Finished writing NDEF data");
239 write_data.push_back(0x00);
242 write_data.push_back(0x00);
243 write_data.push_back(0xFF);
246 const uint8_t real_length = data.size() + 1;
248 write_data.push_back(real_length);
250 write_data.push_back(~real_length + 1);
253 write_data.push_back(0xD4);
258 for (uint8_t dat : data) {
259 write_data.push_back(dat);
264 write_data.push_back(~checksum + 1);
266 write_data.push_back(0x00);
274 ESP_LOGV(TAG,
"Reading ACK...");
276 std::vector<uint8_t> data;
281 bool matches = (data[1] == 0x00 &&
283 data[3] == 0xFF && data[4] == 0x00 &&
284 data[5] == 0xFF && data[6] == 0x00);
285 ESP_LOGV(TAG,
"ACK valid: %s", YESNO(matches));
290 ESP_LOGV(TAG,
"Sending NACK for retransmit");
291 this->
write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
296 ESP_LOGV(TAG,
"Turning RF field OFF");
298 PN532_COMMAND_RFCONFIGURATION,
307 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
308 ESP_LOGD(TAG,
"Mifare classic");
310 }
else if (type == nfc::TAG_TYPE_2) {
311 ESP_LOGD(TAG,
"Mifare ultralight");
313 }
else if (type == nfc::TAG_TYPE_UNKNOWN) {
314 ESP_LOGV(TAG,
"Cannot determine tag type");
315 return make_unique<nfc::NfcTag>(uid);
317 return make_unique<nfc::NfcTag>(uid);
322 this->next_task_ =
READ;
323 ESP_LOGD(TAG,
"Waiting to read next tag");
326 this->next_task_ =
CLEAN;
327 ESP_LOGD(TAG,
"Waiting to clean next tag");
330 this->next_task_ =
FORMAT;
331 ESP_LOGD(TAG,
"Waiting to format next tag");
334 this->next_task_ =
WRITE;
336 ESP_LOGD(TAG,
"Waiting to write next tag");
341 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
343 }
else if (type == nfc::TAG_TYPE_2) {
346 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
352 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
354 }
else if (type == nfc::TAG_TYPE_2) {
357 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
363 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
365 }
else if (type == nfc::TAG_TYPE_2) {
368 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
375 ESP_LOGCONFIG(TAG,
"PN532:");
376 switch (this->error_code_) {
380 ESP_LOGE(TAG,
"Wake Up command failed!");
383 ESP_LOGE(TAG,
"SAM command failed!");
387 LOG_UPDATE_INTERVAL(
this);
390 LOG_BINARY_SENSOR(
" ",
"Tag", child);
395 if (data.size() != this->uid_.size())
398 for (
size_t i = 0; i < data.size(); i++) {
399 if (data[i] != this->uid_[i])
403 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 dump_config() override
std::string format_uid(std::vector< uint8_t > &uid)
std::unique_ptr< nfc::NfcTag > read_tag_(std::vector< uint8_t > &uid)
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)
void status_set_warning()
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)
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)