15 static const char *
const TAG =
"pn532";
18 ESP_LOGCONFIG(TAG,
"Setting up PN532...");
22 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", version_data[0]);
34 ESP_LOGD(TAG,
"Firmware ver. %d.%d", 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);
86 PN532_COMMAND_INLISTPASSIVETARGET,
90 ESP_LOGW(TAG,
"Requesting tag read failed!");
102 std::vector<uint8_t> read;
103 bool success = this->
read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
110 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
112 trigger->process(tag);
119 uint8_t num_targets = read[0];
120 if (num_targets != 1) {
123 auto tag = make_unique<nfc::NfcTag>(this->
current_uid_);
125 trigger->process(tag);
132 uint8_t nfcid_length = read[5];
133 std::vector<uint8_t> nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length);
134 if (read.size() < 6U + nfcid_length) {
141 if (bin_sens->process(nfcid)) {
147 bool same_uid =
true;
148 for (
size_t i = 0; i < nfcid.size(); i++)
156 if (next_task_ ==
READ) {
159 trigger->process(tag);
163 if (tag->has_ndef_message()) {
164 const auto &message = tag->get_ndef_message();
165 const auto &records = message->get_records();
166 ESP_LOGD(TAG,
" NDEF formatted records:");
167 for (
const auto &record : records) {
168 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
172 }
else if (next_task_ ==
CLEAN) {
173 ESP_LOGD(TAG,
" Tag cleaning...");
175 ESP_LOGE(TAG,
" Tag was not fully cleaned successfully");
177 ESP_LOGD(TAG,
" Tag cleaned!");
178 }
else if (next_task_ ==
FORMAT) {
179 ESP_LOGD(TAG,
" Tag formatting...");
181 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
183 ESP_LOGD(TAG,
" Tag formatted!");
184 }
else if (next_task_ ==
WRITE) {
186 ESP_LOGD(TAG,
" Tag writing...");
187 ESP_LOGD(TAG,
" Tag formatting...");
189 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
191 ESP_LOGD(TAG,
" Writing NDEF data");
193 ESP_LOGE(TAG,
" Failed to write message to tag");
195 ESP_LOGD(TAG,
" Finished writing NDEF data");
211 write_data.push_back(0x00);
214 write_data.push_back(0x00);
215 write_data.push_back(0xFF);
218 const uint8_t real_length = data.size() + 1;
220 write_data.push_back(real_length);
222 write_data.push_back(~real_length + 1);
225 write_data.push_back(0xD4);
230 for (uint8_t dat : data) {
231 write_data.push_back(dat);
236 write_data.push_back(~checksum + 1);
238 write_data.push_back(0x00);
246 ESP_LOGV(TAG,
"Reading ACK...");
248 std::vector<uint8_t> data;
253 bool matches = (data[1] == 0x00 &&
255 data[3] == 0xFF && data[4] == 0x00 &&
256 data[5] == 0xFF && data[6] == 0x00);
257 ESP_LOGV(TAG,
"ACK valid: %s", YESNO(matches));
262 ESP_LOGV(TAG,
"Sending NACK for retransmit");
263 this->
write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
268 ESP_LOGV(TAG,
"Turning RF field OFF");
270 PN532_COMMAND_RFCONFIGURATION,
279 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
280 ESP_LOGD(TAG,
"Mifare classic");
282 }
else if (type == nfc::TAG_TYPE_2) {
283 ESP_LOGD(TAG,
"Mifare ultralight");
285 }
else if (type == nfc::TAG_TYPE_UNKNOWN) {
286 ESP_LOGV(TAG,
"Cannot determine tag type");
287 return make_unique<nfc::NfcTag>(uid);
289 return make_unique<nfc::NfcTag>(uid);
294 this->next_task_ =
READ;
295 ESP_LOGD(TAG,
"Waiting to read next tag");
298 this->next_task_ =
CLEAN;
299 ESP_LOGD(TAG,
"Waiting to clean next tag");
302 this->next_task_ =
FORMAT;
303 ESP_LOGD(TAG,
"Waiting to format next tag");
306 this->next_task_ =
WRITE;
308 ESP_LOGD(TAG,
"Waiting to write next tag");
313 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
315 }
else if (type == nfc::TAG_TYPE_2) {
318 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
324 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
326 }
else if (type == nfc::TAG_TYPE_2) {
329 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
335 if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) {
337 }
else if (type == nfc::TAG_TYPE_2) {
340 ESP_LOGE(TAG,
"Unsupported Tag for formatting");
347 ESP_LOGCONFIG(TAG,
"PN532:");
348 switch (this->error_code_) {
352 ESP_LOGE(TAG,
"Wake Up command failed!");
355 ESP_LOGE(TAG,
"SAM command failed!");
359 LOG_UPDATE_INTERVAL(
this);
362 LOG_BINARY_SENSOR(
" ",
"Tag", child);
367 if (data.size() != this->uid_.size())
370 for (
size_t i = 0; i < data.size(); i++) {
371 if (data[i] != this->uid_[i])
375 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
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)