ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
pn7160.cpp
Go to the documentation of this file.
1#include <utility>
2
3#include "automation.h"
4#include "pn7160.h"
5
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9
10namespace esphome {
11namespace pn7160 {
12
13static const char *const TAG = "pn7160";
14
16 this->irq_pin_->setup();
17 this->ven_pin_->setup();
18 if (this->dwl_req_pin_ != nullptr) {
19 this->dwl_req_pin_->setup();
20 }
21 if (this->wkup_req_pin_ != nullptr) {
22 this->wkup_req_pin_->setup();
23 }
24
25 this->nci_fsm_transition_(); // kick off reset & init processes
26}
27
29 ESP_LOGCONFIG(TAG, "PN7160:");
30 if (this->dwl_req_pin_ != nullptr) {
31 LOG_PIN(" DWL_REQ pin: ", this->dwl_req_pin_);
32 }
33 LOG_PIN(" IRQ pin: ", this->irq_pin_);
34 LOG_PIN(" VEN pin: ", this->ven_pin_);
35 if (this->wkup_req_pin_ != nullptr) {
36 LOG_PIN(" WKUP_REQ pin: ", this->wkup_req_pin_);
37 }
38}
39
41 this->nci_fsm_transition_();
42 this->purge_old_tags_();
43}
44
45void PN7160::set_tag_emulation_message(std::shared_ptr<nfc::NdefMessage> message) {
46 this->card_emulation_message_ = std::move(message);
47 ESP_LOGD(TAG, "Tag emulation message set");
48}
49
51 const optional<bool> include_android_app_record) {
52 if (!message.has_value()) {
53 return;
54 }
55
56 auto ndef_message = make_unique<nfc::NdefMessage>();
57
58 ndef_message->add_uri_record(message.value());
59
60 if (!include_android_app_record.has_value() || include_android_app_record.value()) {
61 auto ext_record = make_unique<nfc::NdefRecord>();
62 ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
63 ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
64 ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
65 ndef_message->add_record(std::move(ext_record));
66 }
67
68 this->card_emulation_message_ = std::move(ndef_message);
69 ESP_LOGD(TAG, "Tag emulation message set");
70}
71
72void PN7160::set_tag_emulation_message(const char *message, const bool include_android_app_record) {
73 this->set_tag_emulation_message(std::string(message), include_android_app_record);
74}
75
77 if (this->listening_enabled_) {
78 this->listening_enabled_ = false;
79 this->config_refresh_pending_ = true;
80 }
81 ESP_LOGD(TAG, "Tag emulation disabled");
82}
83
85 if (this->card_emulation_message_ == nullptr) {
86 ESP_LOGE(TAG, "No NDEF message is set; tag emulation cannot be enabled");
87 return;
88 }
89 if (!this->listening_enabled_) {
90 this->listening_enabled_ = true;
91 this->config_refresh_pending_ = true;
92 }
93 ESP_LOGD(TAG, "Tag emulation enabled");
94}
95
97 if (this->polling_enabled_) {
98 this->polling_enabled_ = false;
99 this->config_refresh_pending_ = true;
100 }
101 ESP_LOGD(TAG, "Tag polling disabled");
102}
103
105 if (!this->polling_enabled_) {
106 this->polling_enabled_ = true;
107 this->config_refresh_pending_ = true;
108 }
109 ESP_LOGD(TAG, "Tag polling enabled");
110}
111
113 this->next_task_ = EP_READ;
114 ESP_LOGD(TAG, "Waiting to read next tag");
115}
116
118 this->next_task_ = EP_CLEAN;
119 ESP_LOGD(TAG, "Waiting to clean next tag");
120}
121
123 this->next_task_ = EP_FORMAT;
124 ESP_LOGD(TAG, "Waiting to format next tag");
125}
126
128 if (this->next_task_message_to_write_ == nullptr) {
129 ESP_LOGW(TAG, "Message to write must be set before setting write mode");
130 return;
131 }
132
133 this->next_task_ = EP_WRITE;
134 ESP_LOGD(TAG, "Waiting to write next tag");
135}
136
137void PN7160::set_tag_write_message(std::shared_ptr<nfc::NdefMessage> message) {
138 this->next_task_message_to_write_ = std::move(message);
139 ESP_LOGD(TAG, "Message to write has been set");
140}
141
143 if (!message.has_value()) {
144 return;
145 }
146
147 auto ndef_message = make_unique<nfc::NdefMessage>();
148
149 ndef_message->add_uri_record(message.value());
150
151 if (!include_android_app_record.has_value() || include_android_app_record.value()) {
152 auto ext_record = make_unique<nfc::NdefRecord>();
153 ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
154 ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
155 ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
156 ndef_message->add_record(std::move(ext_record));
157 }
158
159 this->next_task_message_to_write_ = std::move(ndef_message);
160 ESP_LOGD(TAG, "Message to write has been set");
161}
162
163uint8_t PN7160::set_test_mode(const TestMode test_mode, const std::vector<uint8_t> &data,
164 std::vector<uint8_t> &result) {
165 auto test_oid = TEST_PRBS_OID;
166
167 switch (test_mode) {
169 // test_oid = TEST_PRBS_OID;
170 break;
171
173 test_oid = TEST_ANTENNA_OID;
174 break;
175
177 test_oid = TEST_GET_REGISTER_OID;
178 break;
179
181 default:
182 ESP_LOGD(TAG, "Exiting test mode");
184 return nfc::STATUS_OK;
185 }
186
187 if (this->reset_core_(true, true) != nfc::STATUS_OK) {
188 ESP_LOGE(TAG, "Failed to reset NCI core");
190 result.clear();
191 return nfc::STATUS_FAILED;
192 } else {
194 }
195 if (this->init_core_() != nfc::STATUS_OK) {
196 ESP_LOGE(TAG, "Failed to initialise NCI core");
198 result.clear();
199 return nfc::STATUS_FAILED;
200 } else {
202 }
203
205 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, test_oid, data);
206
207 ESP_LOGW(TAG, "Starting test mode, OID 0x%02X", test_oid);
208 auto status = this->transceive_(tx, rx, NFCC_INIT_TIMEOUT);
209
210 if (status != nfc::STATUS_OK) {
211 ESP_LOGE(TAG, "Failed to start test mode, OID 0x%02X", test_oid);
213 result.clear();
214 } else {
215 result = rx.get_message();
216 result.erase(result.begin(), result.begin() + 4); // remove NCI header
217 if (!result.empty()) {
218 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
219 ESP_LOGW(TAG, "Test results: %s", nfc::format_bytes_to(buf, result));
220 }
221 }
222 return status;
223}
224
225uint8_t PN7160::reset_core_(const bool reset_config, const bool power) {
226 if (this->dwl_req_pin_ != nullptr) {
227 this->dwl_req_pin_->digital_write(false);
228 delay(NFCC_DEFAULT_TIMEOUT);
229 }
230
231 if (power) {
232 this->ven_pin_->digital_write(true);
233 delay(NFCC_DEFAULT_TIMEOUT);
234 this->ven_pin_->digital_write(false);
235 delay(NFCC_DEFAULT_TIMEOUT);
236 this->ven_pin_->digital_write(true);
237 delay(NFCC_INIT_TIMEOUT);
238 }
239
241 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_RESET_OID,
242 {(uint8_t) reset_config});
243
244 if (this->transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
245 ESP_LOGE(TAG, "Error sending reset command");
246 return nfc::STATUS_FAILED;
247 }
248
249 if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
250 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
251 ESP_LOGE(TAG, "Invalid reset response: %s", nfc::format_bytes_to(buf, rx.get_message()));
252 return rx.get_simple_status_response();
253 }
254 // read reset notification
255 if (this->read_nfcc(rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
256 ESP_LOGE(TAG, "Reset notification was not received");
257 return nfc::STATUS_FAILED;
258 }
259 // verify reset notification
260 if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.message_length_is(9)) ||
261 (rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET] != 0x02) ||
262 (rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != (uint8_t) reset_config)) {
263 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
264 ESP_LOGE(TAG, "Reset notification was malformed: %s", nfc::format_bytes_to(buf, rx.get_message()));
265 return nfc::STATUS_FAILED;
266 }
267
268 ESP_LOGD(TAG, "Configuration %s, NCI version: %s, Manufacturer ID: 0x%02X",
269 rx.get_message()[4] ? "reset" : "retained", rx.get_message()[5] == 0x20 ? "2.0" : "1.0",
270 rx.get_message()[6]);
271 rx.get_message().erase(rx.get_message().begin(), rx.get_message().begin() + 8);
272 char mfr_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
273 ESP_LOGD(TAG, "Manufacturer info: %s", nfc::format_bytes_to(mfr_buf, rx.get_message()));
274
275 return nfc::STATUS_OK;
276}
277
280 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
281
282 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
283 ESP_LOGE(TAG, "Error sending initialise command");
284 return nfc::STATUS_FAILED;
285 }
286
287 if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
288 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
289 ESP_LOGE(TAG, "Invalid initialise response: %s", nfc::format_bytes_to(buf, rx.get_message()));
290 return nfc::STATUS_FAILED;
291 }
292
293 uint8_t hw_version = rx.get_message()[17 + rx.get_message()[8]];
294 uint8_t rom_code_version = rx.get_message()[18 + rx.get_message()[8]];
295 uint8_t flash_major_version = rx.get_message()[19 + rx.get_message()[8]];
296 uint8_t flash_minor_version = rx.get_message()[20 + rx.get_message()[8]];
297 std::vector<uint8_t> features(rx.get_message().begin() + 4, rx.get_message().begin() + 8);
298
299 char feat_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
300 ESP_LOGD(TAG,
301 "PN7160 chip info:\n"
302 " Hardware version: %u\n"
303 " ROM code version: %u\n"
304 " FLASH major version: %u\n"
305 " FLASH minor version: %u\n"
306 " Features: %s",
307 hw_version, rom_code_version, flash_major_version, flash_minor_version,
308 nfc::format_bytes_to(feat_buf, features));
309
310 return rx.get_simple_status_response();
311}
312
315 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
316
317 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
318 ESP_LOGE(TAG, "Error enabling proprietary extensions");
319 return nfc::STATUS_FAILED;
320 }
321
322 tx.set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
323 std::vector<uint8_t>(std::begin(PMU_CFG), std::end(PMU_CFG)));
324
325 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
326 ESP_LOGE(TAG, "Error sending PMU config");
327 return nfc::STATUS_FAILED;
328 }
329
330 return this->send_core_config_();
331}
332
334 const auto *core_config_begin = std::begin(CORE_CONFIG_SOLO);
335 const auto *core_config_end = std::end(CORE_CONFIG_SOLO);
336 this->core_config_is_solo_ = true;
337
338 if (this->listening_enabled_ && this->polling_enabled_) {
339 core_config_begin = std::begin(CORE_CONFIG_RW_CE);
340 core_config_end = std::end(CORE_CONFIG_RW_CE);
341 this->core_config_is_solo_ = false;
342 }
343
345 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
346 std::vector<uint8_t>(core_config_begin, core_config_end));
347
348 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
349 ESP_LOGW(TAG, "Error sending core config");
350 return nfc::STATUS_FAILED;
351 }
352
353 return nfc::STATUS_OK;
354}
355
357 bool core_config_should_be_solo = !(this->listening_enabled_ && this->polling_enabled_);
358
360 if (this->stop_discovery_() != nfc::STATUS_OK) {
362 return nfc::STATUS_FAILED;
363 }
365 }
366
367 if (this->core_config_is_solo_ != core_config_should_be_solo) {
368 if (this->send_core_config_() != nfc::STATUS_OK) {
369 ESP_LOGV(TAG, "Failed to refresh core config");
370 return nfc::STATUS_FAILED;
371 }
372 }
373 this->config_refresh_pending_ = false;
374 return nfc::STATUS_OK;
375}
376
378 std::vector<uint8_t> discover_map = {sizeof(RF_DISCOVER_MAP_CONFIG) / 3};
379 discover_map.insert(discover_map.end(), std::begin(RF_DISCOVER_MAP_CONFIG), std::end(RF_DISCOVER_MAP_CONFIG));
380
382 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID, discover_map);
383
384 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
385 ESP_LOGE(TAG, "Error sending discover map poll config");
386 return nfc::STATUS_FAILED;
387 }
388 return nfc::STATUS_OK;
389}
390
394 nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
395 std::vector<uint8_t>(std::begin(RF_LISTEN_MODE_ROUTING_CONFIG), std::end(RF_LISTEN_MODE_ROUTING_CONFIG)));
396
397 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
398 ESP_LOGE(TAG, "Error setting listen mode routing config");
399 return nfc::STATUS_FAILED;
400 }
401 return nfc::STATUS_OK;
402}
403
405 const uint8_t *rf_discovery_config = RF_DISCOVERY_CONFIG;
406 uint8_t length = sizeof(RF_DISCOVERY_CONFIG);
407
408 if (!this->listening_enabled_) {
409 length = sizeof(RF_DISCOVERY_POLL_CONFIG);
410 rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
411 } else if (!this->polling_enabled_) {
412 length = sizeof(RF_DISCOVERY_LISTEN_CONFIG);
413 rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
414 }
415
416 std::vector<uint8_t> discover_config = std::vector<uint8_t>((length * 2) + 1);
417
418 discover_config[0] = length;
419 for (uint8_t i = 0; i < length; i++) {
420 discover_config[(i * 2) + 1] = rf_discovery_config[i];
421 discover_config[(i * 2) + 2] = 0x01; // RF Technology and Mode will be executed in every discovery period
422 }
423
425 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID, discover_config);
426
427 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
428 switch (rx.get_simple_status_response()) {
429 // in any of these cases, we are either already in or will remain in discovery, which satisfies the function call
430 case nfc::STATUS_OK:
431 case nfc::DISCOVERY_ALREADY_STARTED:
432 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
433 case nfc::DISCOVERY_TEAR_DOWN:
434 return nfc::STATUS_OK;
435
436 default:
437 ESP_LOGE(TAG, "Error starting discovery");
438 return nfc::STATUS_FAILED;
439 }
440 }
441
442 return nfc::STATUS_OK;
443}
444
445uint8_t PN7160::stop_discovery_() { return this->deactivate_(nfc::DEACTIVATION_TYPE_IDLE, NFCC_TAG_WRITE_TIMEOUT); }
446
447uint8_t PN7160::deactivate_(const uint8_t type, const uint16_t timeout) {
449 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {type});
450
451 auto status = this->transceive_(tx, rx, timeout);
452 // if (status != nfc::STATUS_OK) {
453 // ESP_LOGE(TAG, "Error sending deactivate type %u", type);
454 // return nfc::STATUS_FAILED;
455 // }
456 return status;
457}
458
460 if (this->discovered_endpoint_.empty()) {
461 ESP_LOGW(TAG, "No cached tags to select");
462 this->stop_discovery_();
464 return;
465 }
466 std::vector<uint8_t> endpoint_data = {this->discovered_endpoint_[0].id, this->discovered_endpoint_[0].protocol,
467 0x01}; // that last byte is the interface ID
468 for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
469 if (!this->discovered_endpoint_[i].trig_called) {
470 endpoint_data = {this->discovered_endpoint_[i].id, this->discovered_endpoint_[i].protocol,
471 0x01}; // that last byte is the interface ID
472 this->selecting_endpoint_ = i;
473 break;
474 }
475 }
476
478 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID, endpoint_data);
479
480 if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
481 ESP_LOGE(TAG, "Error selecting endpoint");
482 } else {
484 }
485}
486
488 uint8_t type = nfc::guess_tag_type(tag.get_uid().size());
489
490 switch (type) {
491 case nfc::TAG_TYPE_MIFARE_CLASSIC:
492 ESP_LOGV(TAG, "Reading Mifare classic");
493 return this->read_mifare_classic_tag_(tag);
494
495 case nfc::TAG_TYPE_2:
496 ESP_LOGV(TAG, "Reading Mifare ultralight");
497 return this->read_mifare_ultralight_tag_(tag);
498
499 case nfc::TAG_TYPE_UNKNOWN:
500 default:
501 ESP_LOGV(TAG, "Cannot determine tag type");
502 break;
503 }
504 return nfc::STATUS_FAILED;
505}
506
508 uint8_t type = nfc::guess_tag_type(uid.size());
509 switch (type) {
510 case nfc::TAG_TYPE_MIFARE_CLASSIC:
511 return this->format_mifare_classic_mifare_();
512
513 case nfc::TAG_TYPE_2:
514 return this->clean_mifare_ultralight_();
515
516 default:
517 ESP_LOGE(TAG, "Unsupported tag for cleaning");
518 break;
519 }
520 return nfc::STATUS_FAILED;
521}
522
524 uint8_t type = nfc::guess_tag_type(uid.size());
525 switch (type) {
526 case nfc::TAG_TYPE_MIFARE_CLASSIC:
527 return this->format_mifare_classic_ndef_();
528
529 case nfc::TAG_TYPE_2:
530 return this->clean_mifare_ultralight_();
531
532 default:
533 ESP_LOGE(TAG, "Unsupported tag for formatting");
534 break;
535 }
536 return nfc::STATUS_FAILED;
537}
538
539uint8_t PN7160::write_endpoint_(nfc::NfcTagUid &uid, std::shared_ptr<nfc::NdefMessage> &message) {
540 uint8_t type = nfc::guess_tag_type(uid.size());
541 switch (type) {
542 case nfc::TAG_TYPE_MIFARE_CLASSIC:
543 return this->write_mifare_classic_tag_(message);
544
545 case nfc::TAG_TYPE_2:
546 return this->write_mifare_ultralight_tag_(uid, message);
547
548 default:
549 ESP_LOGE(TAG, "Unsupported tag for writing");
550 break;
551 }
552 return nfc::STATUS_FAILED;
553}
554
555std::unique_ptr<nfc::NfcTag> PN7160::build_tag_(const uint8_t mode_tech, const std::vector<uint8_t> &data) {
556 switch (mode_tech) {
557 case (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA): {
558 uint8_t uid_length = data[2];
559 if (!uid_length) {
560 ESP_LOGE(TAG, "UID length cannot be zero");
561 return nullptr;
562 }
563 nfc::NfcTagUid uid(data.begin() + 3, data.begin() + 3 + uid_length);
564 const auto *tag_type_str =
565 nfc::guess_tag_type(uid_length) == nfc::TAG_TYPE_MIFARE_CLASSIC ? nfc::MIFARE_CLASSIC : nfc::NFC_FORUM_TYPE_2;
566 return make_unique<nfc::NfcTag>(uid, tag_type_str);
567 }
568 }
569 return nullptr;
570}
571
573 if (!this->discovered_endpoint_.empty()) {
574 for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
575 auto existing_tag_uid = this->discovered_endpoint_[i].tag->get_uid();
576 bool uid_match = (uid.size() == existing_tag_uid.size());
577
578 if (uid_match) {
579 for (size_t i = 0; i < uid.size(); i++) {
580 uid_match &= (uid[i] == existing_tag_uid[i]);
581 }
582 if (uid_match) {
583 return i;
584 }
585 }
586 }
587 }
588 return nullopt;
589}
590
592 for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
593 if (millis() - this->discovered_endpoint_[i].last_seen > this->tag_ttl_) {
594 this->erase_tag_(i);
595 }
596 }
597}
598
599void PN7160::erase_tag_(const uint8_t tag_index) {
600 if (tag_index < this->discovered_endpoint_.size()) {
601 for (auto *trigger : this->triggers_ontagremoved_) {
602 trigger->process(this->discovered_endpoint_[tag_index].tag);
603 }
604 for (auto *listener : this->tag_listeners_) {
605 listener->tag_off(*this->discovered_endpoint_[tag_index].tag);
606 }
607 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
608 ESP_LOGI(TAG, "Tag %s removed", nfc::format_uid_to(uid_buf, this->discovered_endpoint_[tag_index].tag->get_uid()));
609 this->discovered_endpoint_.erase(this->discovered_endpoint_.begin() + tag_index);
610 }
611}
612
614 switch (this->nci_state_) {
616 if (this->reset_core_(true, true) != nfc::STATUS_OK) {
617 ESP_LOGE(TAG, "Failed to reset NCI core");
619 return;
620 } else {
622 }
623 [[fallthrough]];
624
626 if (this->init_core_() != nfc::STATUS_OK) {
627 ESP_LOGE(TAG, "Failed to initialise NCI core");
629 return;
630 } else {
632 }
633 [[fallthrough]];
634
636 if (this->send_init_config_() != nfc::STATUS_OK) {
637 ESP_LOGE(TAG, "Failed to send initial config");
639 return;
640 } else {
641 this->config_refresh_pending_ = false;
643 }
644 [[fallthrough]];
645
647 if (this->set_discover_map_() != nfc::STATUS_OK) {
648 ESP_LOGE(TAG, "Failed to set discover map");
650 return;
651 } else {
653 }
654 [[fallthrough]];
655
657 if (this->set_listen_mode_routing_() != nfc::STATUS_OK) {
658 ESP_LOGE(TAG, "Failed to set listen mode routing");
660 return;
661 } else {
663 }
664 [[fallthrough]];
665
668 this->stop_discovery_();
669 }
670
671 if (this->config_refresh_pending_) {
672 this->refresh_core_config_();
673 }
674
675 if (!this->listening_enabled_ && !this->polling_enabled_) {
676 return;
677 }
678
679 if (this->start_discovery_() != nfc::STATUS_OK) {
680 ESP_LOGV(TAG, "Failed to start discovery");
682 } else {
684 }
685 return;
686
689 [[fallthrough]];
690
691 // All cases below are waiting for NOTIFICATION messages
693 if (this->config_refresh_pending_) {
694 this->refresh_core_config_();
695 }
696 [[fallthrough]];
697
703 if (this->irq_pin_->digital_read()) {
704 this->process_message_();
705 }
706 break;
707
708 case NCIState::FAILED:
709 case NCIState::NONE:
710 default:
711 return;
712 }
713}
714
716 ESP_LOGVV(TAG, "nci_fsm_set_state_(%u)", (uint8_t) new_state);
717 this->nci_state_ = new_state;
719 this->error_count_ = 0;
721}
722
724 ESP_LOGVV(TAG, "nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->error_count_);
725 this->nci_state_error_ = new_state;
726 if (this->error_count_++ > NFCC_MAX_ERROR_COUNT) {
729 ESP_LOGE(TAG, "Too many initialization failures -- check device connections");
730 this->mark_failed();
732 } else {
733 ESP_LOGW(TAG, "Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->nci_state_error_);
735 }
736 }
737 return this->error_count_ > NFCC_MAX_ERROR_COUNT;
738}
739
742 if (this->read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
743 return; // No data
744 }
745
746 switch (rx.get_message_type()) {
747 case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
748 if (rx.get_gid() == nfc::RF_GID) {
749 switch (rx.get_oid()) {
750 case nfc::RF_INTF_ACTIVATED_OID:
751 ESP_LOGVV(TAG, "RF_INTF_ACTIVATED_OID");
753 return;
754
755 case nfc::RF_DISCOVER_OID:
756 ESP_LOGVV(TAG, "RF_DISCOVER_OID");
757 this->process_rf_discover_oid_(rx);
758 return;
759
760 case nfc::RF_DEACTIVATE_OID:
761 ESP_LOGVV(TAG, "RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.get_message()[3], rx.get_message()[4]);
763 return;
764
765 default:
766 ESP_LOGV(TAG, "Unimplemented RF OID received: 0x%02X", rx.get_oid());
767 }
768 } else if (rx.get_gid() == nfc::NCI_CORE_GID) {
769 switch (rx.get_oid()) {
770 case nfc::NCI_CORE_GENERIC_ERROR_OID:
771 ESP_LOGV(TAG, "NCI_CORE_GENERIC_ERROR_OID:");
772 switch (rx.get_simple_status_response()) {
773 case nfc::DISCOVERY_ALREADY_STARTED:
774 ESP_LOGV(TAG, " DISCOVERY_ALREADY_STARTED");
775 break;
776
777 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
778 // Tag removed too soon
779 ESP_LOGV(TAG, " DISCOVERY_TARGET_ACTIVATION_FAILED");
780 if (this->nci_state_ == NCIState::EP_SELECTING) {
782 if (!this->discovered_endpoint_.empty()) {
783 this->erase_tag_(this->selecting_endpoint_);
784 }
785 } else {
786 this->stop_discovery_();
788 }
789 break;
790
791 case nfc::DISCOVERY_TEAR_DOWN:
792 ESP_LOGV(TAG, " DISCOVERY_TEAR_DOWN");
793 break;
794
795 default:
796 ESP_LOGW(TAG, "Unknown error: 0x%02X", rx.get_simple_status_response());
797 break;
798 }
799 break;
800
801 default:
802 ESP_LOGV(TAG, "Unimplemented NCI Core OID received: 0x%02X", rx.get_oid());
803 }
804 } else {
805 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
806 ESP_LOGV(TAG, "Unimplemented notification: %s", nfc::format_bytes_to(buf, rx.get_message()));
807 }
808 break;
809
810 case nfc::NCI_PKT_MT_CTRL_RESPONSE: {
811 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
812 ESP_LOGV(TAG, "Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.get_gid(), rx.get_oid(),
814 break;
815 }
816
817 case nfc::NCI_PKT_MT_CTRL_COMMAND: {
818 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
819 ESP_LOGV(TAG, "Unimplemented command: %s", nfc::format_bytes_to(buf, rx.get_message()));
820 break;
821 }
822
823 case nfc::NCI_PKT_MT_DATA:
824 this->process_data_message_(rx);
825 break;
826
827 default: {
828 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
829 ESP_LOGV(TAG, "Unimplemented message type: %s", nfc::format_bytes_to(buf, rx.get_message()));
830 break;
831 }
832 }
833}
834
835void PN7160::process_rf_intf_activated_oid_(nfc::NciMessage &rx) { // an endpoint was activated
836 uint8_t discovery_id = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
837 uint8_t interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
838 uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
839 uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
840 uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
841
842 ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
843 interface, protocol, mode_tech, max_size);
844
845 if (mode_tech & nfc::MODE_LISTEN_MASK) {
846 ESP_LOGVV(TAG, "Tag activated in listen mode");
847 this->nci_fsm_set_state_(NCIState::RFST_LISTEN_ACTIVE);
848 return;
849 }
850
852 auto incoming_tag =
853 this->build_tag_(mode_tech, std::vector<uint8_t>(rx.get_message().begin() + 10, rx.get_message().end()));
854
855 if (incoming_tag == nullptr) {
856 ESP_LOGE(TAG, "Could not build tag");
857 } else {
858 auto tag_loc = this->find_tag_uid_(incoming_tag->get_uid());
859 if (tag_loc.has_value()) {
860 this->discovered_endpoint_[tag_loc.value()].id = discovery_id;
861 this->discovered_endpoint_[tag_loc.value()].protocol = protocol;
862 this->discovered_endpoint_[tag_loc.value()].last_seen = millis();
863 ESP_LOGVV(TAG, "Tag cache updated");
864 } else {
865 this->discovered_endpoint_.emplace_back(
866 DiscoveredEndpoint{discovery_id, protocol, millis(), std::move(incoming_tag), false});
867 tag_loc = this->discovered_endpoint_.size() - 1;
868 ESP_LOGVV(TAG, "Tag added to cache");
869 }
870
871 auto &working_endpoint = this->discovered_endpoint_[tag_loc.value()];
872
873 switch (this->next_task_) {
874 case EP_CLEAN:
875 ESP_LOGD(TAG, " Tag cleaning");
876 if (this->clean_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
877 ESP_LOGE(TAG, " Tag cleaning incomplete");
878 }
879 ESP_LOGD(TAG, " Tag cleaned!");
880 break;
881
882 case EP_FORMAT:
883 ESP_LOGD(TAG, " Tag formatting");
884 if (this->format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
885 ESP_LOGE(TAG, "Error formatting tag as NDEF");
886 }
887 ESP_LOGD(TAG, " Tag formatted!");
888 break;
889
890 case EP_WRITE:
891 if (this->next_task_message_to_write_ != nullptr) {
892 ESP_LOGD(TAG, " Tag writing\n"
893 " Tag formatting");
894 if (this->format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
895 ESP_LOGE(TAG, " Tag could not be formatted for writing");
896 } else {
897 ESP_LOGD(TAG, " Writing NDEF data");
898 if (this->write_endpoint_(working_endpoint.tag->get_uid(), this->next_task_message_to_write_) !=
899 nfc::STATUS_OK) {
900 ESP_LOGE(TAG, " Failed to write message to tag");
901 }
902 ESP_LOGD(TAG, " Finished writing NDEF data");
903 this->next_task_message_to_write_ = nullptr;
904 this->on_finished_write_callback_.call();
905 }
906 }
907 break;
908
909 case EP_READ:
910 default:
911 if (!working_endpoint.trig_called) {
912 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
913 ESP_LOGI(TAG, "Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(),
914 nfc::format_uid_to(uid_buf, working_endpoint.tag->get_uid()));
915 if (this->read_endpoint_data_(*working_endpoint.tag) != nfc::STATUS_OK) {
916 ESP_LOGW(TAG, " Unable to read NDEF record(s)");
917 } else if (working_endpoint.tag->has_ndef_message()) {
918 const auto message = working_endpoint.tag->get_ndef_message();
919 const auto records = message->get_records();
920 ESP_LOGD(TAG, " NDEF record(s):");
921 for (const auto &record : records) {
922 ESP_LOGD(TAG, " %s - %s", record->get_type().c_str(), record->get_payload().c_str());
923 }
924 } else {
925 ESP_LOGW(TAG, " No NDEF records found");
926 }
927 for (auto *trigger : this->triggers_ontag_) {
928 trigger->process(working_endpoint.tag);
929 }
930 for (auto *listener : this->tag_listeners_) {
931 listener->tag_on(*working_endpoint.tag);
932 }
933 working_endpoint.trig_called = true;
934 break;
935 }
936 }
937 if (working_endpoint.tag->get_tag_type() == nfc::MIFARE_CLASSIC) {
939 }
940 }
941 if (this->next_task_ != EP_READ) {
942 this->read_mode();
943 }
944
945 this->stop_discovery_();
947}
948
950 auto incoming_tag = this->build_tag_(rx.get_message_byte(nfc::RF_DISCOVER_NTF_MODE_TECH),
951 std::vector<uint8_t>(rx.get_message().begin() + 7, rx.get_message().end()));
952
953 if (incoming_tag == nullptr) {
954 ESP_LOGE(TAG, "Could not build tag!");
955 } else {
956 auto tag_loc = this->find_tag_uid_(incoming_tag->get_uid());
957 if (tag_loc.has_value()) {
958 this->discovered_endpoint_[tag_loc.value()].id = rx.get_message_byte(nfc::RF_DISCOVER_NTF_DISCOVERY_ID);
959 this->discovered_endpoint_[tag_loc.value()].protocol = rx.get_message_byte(nfc::RF_DISCOVER_NTF_PROTOCOL);
960 this->discovered_endpoint_[tag_loc.value()].last_seen = millis();
961 ESP_LOGVV(TAG, "Tag found & updated");
962 } else {
963 this->discovered_endpoint_.emplace_back(DiscoveredEndpoint{rx.get_message_byte(nfc::RF_DISCOVER_NTF_DISCOVERY_ID),
964 rx.get_message_byte(nfc::RF_DISCOVER_NTF_PROTOCOL),
965 millis(), std::move(incoming_tag), false});
966 ESP_LOGVV(TAG, "Tag saved");
967 }
968 }
969
970 if (rx.get_message().back() != nfc::RF_DISCOVER_NTF_NT_MORE) {
972 ESP_LOGVV(TAG, "Discovered %u endpoints", this->discovered_endpoint_.size());
973 }
974}
975
978
979 switch (rx.get_simple_status_response()) {
980 case nfc::DEACTIVATION_TYPE_DISCOVERY:
982 break;
983
984 case nfc::DEACTIVATION_TYPE_IDLE:
986 break;
987
988 case nfc::DEACTIVATION_TYPE_SLEEP:
989 case nfc::DEACTIVATION_TYPE_SLEEP_AF:
992 } else if (this->nci_state_ == NCIState::RFST_POLL_ACTIVE) {
994 } else {
996 }
997 break;
998
999 default:
1000 break;
1001 }
1002}
1003
1005 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1006 ESP_LOGVV(TAG, "Received data message: %s", nfc::format_bytes_to(buf, rx.get_message()));
1007
1008 std::vector<uint8_t> ndef_response;
1009 this->card_emu_t4t_get_response_(rx.get_message(), ndef_response);
1010
1011 uint16_t ndef_response_size = ndef_response.size();
1012 if (!ndef_response_size) {
1013 return; // no message returned, we cannot respond
1014 }
1015
1016 std::vector<uint8_t> tx_msg = {nfc::NCI_PKT_MT_DATA, uint8_t((ndef_response_size & 0xFF00) >> 8),
1017 uint8_t(ndef_response_size & 0x00FF)};
1018 tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end());
1019 nfc::NciMessage tx(tx_msg);
1020 ESP_LOGVV(TAG, "Sending data message: %s", nfc::format_bytes_to(buf, tx.get_message()));
1021 if (this->transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
1022 ESP_LOGE(TAG, "Sending reply for card emulation failed");
1023 }
1024}
1025
1026void PN7160::card_emu_t4t_get_response_(std::vector<uint8_t> &response, std::vector<uint8_t> &ndef_response) {
1027 if (this->card_emulation_message_ == nullptr) {
1028 ESP_LOGE(TAG, "No NDEF message is set; tag emulation not possible");
1029 ndef_response.clear();
1030 return;
1031 }
1032
1033 if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_APP_SELECT))) {
1034 // CARD_EMU_T4T_APP_SELECT
1035 ESP_LOGVV(TAG, "CARD_EMU_NDEF_APP_SELECTED");
1037 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1038 } else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_CC_SELECT))) {
1039 // CARD_EMU_T4T_CC_SELECT
1041 ESP_LOGVV(TAG, "CARD_EMU_CC_SELECTED");
1043 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1044 }
1045 } else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_NDEF_SELECT))) {
1046 // CARD_EMU_T4T_NDEF_SELECT
1047 ESP_LOGVV(TAG, "CARD_EMU_NDEF_SELECTED");
1049 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1050 } else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1051 response.begin() + nfc::NCI_PKT_HEADER_SIZE + sizeof(CARD_EMU_T4T_READ),
1052 std::begin(CARD_EMU_T4T_READ))) {
1053 // CARD_EMU_T4T_READ
1055 // CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED
1056 ESP_LOGVV(TAG, "CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
1057 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1058 uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1059
1060 if (length <= (sizeof(CARD_EMU_T4T_CC) + offset + 2)) {
1061 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_CC) + offset,
1062 std::begin(CARD_EMU_T4T_CC) + offset + length);
1063 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1064 }
1066 // CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED
1067 ESP_LOGVV(TAG, "CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
1068 auto ndef_message = this->card_emulation_message_->encode();
1069 uint16_t ndef_msg_size = ndef_message.size();
1070 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1071 uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1072
1073 char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1074 ESP_LOGVV(TAG, "Encoded NDEF message: %s", nfc::format_bytes_to(ndef_buf, ndef_message));
1075
1076 if (length <= (ndef_msg_size + offset + 2)) {
1077 if (offset == 0) {
1078 ndef_response.resize(2);
1079 ndef_response[0] = (ndef_msg_size & 0xFF00) >> 8;
1080 ndef_response[1] = (ndef_msg_size & 0x00FF);
1081 if (length > 2) {
1082 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 2);
1083 }
1084 } else if (offset == 1) {
1085 ndef_response.resize(1);
1086 ndef_response[0] = (ndef_msg_size & 0x00FF);
1087 if (length > 1) {
1088 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 1);
1089 }
1090 } else {
1091 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length);
1092 }
1093
1094 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1095
1096 if ((offset + length) >= (ndef_msg_size + 2)) {
1097 ESP_LOGD(TAG, "NDEF message sent");
1098 this->on_emulated_tag_scan_callback_.call();
1099 }
1100 }
1101 }
1102 } else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1103 response.begin() + nfc::NCI_PKT_HEADER_SIZE + sizeof(CARD_EMU_T4T_WRITE),
1104 std::begin(CARD_EMU_T4T_WRITE))) {
1105 // CARD_EMU_T4T_WRITE
1107 ESP_LOGVV(TAG, "CARD_EMU_T4T_WRITE");
1108 uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1109 std::vector<uint8_t> ndef_msg_written;
1110
1111 ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5,
1112 response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 + length);
1113 char write_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1114 ESP_LOGD(TAG, "Received %u-byte NDEF message: %s", length, nfc::format_bytes_to(write_buf, ndef_msg_written));
1115 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1116 }
1117 }
1118}
1119
1120uint8_t PN7160::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint16_t timeout,
1121 const bool expect_notification) {
1122 uint8_t retries = NFCC_MAX_COMM_FAILS;
1123 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1124
1125 while (retries) {
1126 // first, send the message we need to send
1127 if (this->write_nfcc(tx) != nfc::STATUS_OK) {
1128 ESP_LOGE(TAG, "Error sending message");
1129 return nfc::STATUS_FAILED;
1130 }
1131 ESP_LOGVV(TAG, "Wrote: %s", nfc::format_bytes_to(buf, tx.get_message()));
1132 // next, the NFCC should send back a response
1133 if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1134 ESP_LOGW(TAG, "Error receiving message");
1135 if (!retries--) {
1136 ESP_LOGE(TAG, " ...giving up");
1137 return nfc::STATUS_FAILED;
1138 }
1139 } else {
1140 break;
1141 }
1142 }
1143 ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes_to(buf, rx.get_message()));
1144 // validate the response based on the message type that was sent (command vs. data)
1145 if (!tx.message_type_is(nfc::NCI_PKT_MT_DATA)) {
1146 // for commands, the GID and OID should match and the status should be OK
1147 if ((rx.get_gid() != tx.get_gid()) || (rx.get_oid()) != tx.get_oid()) {
1148 ESP_LOGE(TAG, "Incorrect response to command: %s", nfc::format_bytes_to(buf, rx.get_message()));
1149 return nfc::STATUS_FAILED;
1150 }
1151
1152 if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
1153 ESP_LOGE(TAG, "Error in response to command: %s", nfc::format_bytes_to(buf, rx.get_message()));
1154 }
1155 return rx.get_simple_status_response();
1156 } else {
1157 // when requesting data from the endpoint, the first response is from the NFCC; we must validate this, first
1158 if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.gid_is(nfc::NCI_CORE_GID)) ||
1159 (!rx.oid_is(nfc::NCI_CORE_CONN_CREDITS_OID)) || (!rx.message_length_is(3))) {
1160 ESP_LOGE(TAG, "Incorrect response to data message: %s", nfc::format_bytes_to(buf, rx.get_message()));
1161 return nfc::STATUS_FAILED;
1162 }
1163
1164 if (expect_notification) {
1165 // if the NFCC said "OK", there will be additional data to read; this comes back in a notification message
1166 if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1167 ESP_LOGE(TAG, "Error receiving data from endpoint");
1168 return nfc::STATUS_FAILED;
1169 }
1170 ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes_to(buf, rx.get_message()));
1171 }
1172
1173 return nfc::STATUS_OK;
1174 }
1175}
1176
1177uint8_t PN7160::wait_for_irq_(uint16_t timeout, bool pin_state) {
1178 auto start_time = millis();
1179
1180 while (millis() - start_time < timeout) {
1181 if (this->irq_pin_->digital_read() == pin_state) {
1182 return nfc::STATUS_OK;
1183 }
1184 }
1185 ESP_LOGW(TAG, "Timed out waiting for IRQ state");
1186 return nfc::STATUS_FAILED;
1187}
1188
1189} // namespace pn7160
1190} // namespace esphome
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool digital_read()=0
size_t size() const
Definition helpers.h:269
bool message_type_is(uint8_t message_type) const
uint8_t get_oid() const
bool simple_status_response_is(uint8_t response) const
uint8_t get_message_byte(uint8_t offset) const
bool gid_is(uint8_t gid) const
std::vector< uint8_t > & get_message()
void set_message(uint8_t message_type, const std::vector< uint8_t > &payload)
bool message_length_is(uint8_t message_length, bool recompute=false)
uint8_t get_gid() const
uint8_t get_simple_status_response() const
bool oid_is(uint8_t oid) const
uint8_t get_message_type() const
NfcTagUid & get_uid()
Definition nfc_tag.h:45
std::vector< NfcTagListener * > tag_listeners_
Definition nfc.h:95
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
virtual uint8_t write_nfcc(nfc::NciMessage &tx)=0
void process_rf_deactivate_oid_(nfc::NciMessage &rx)
Definition pn7160.cpp:976
uint8_t send_init_config_()
Definition pn7160.cpp:313
uint8_t selecting_endpoint_
Definition pn7160.h:290
void process_rf_discover_oid_(nfc::NciMessage &rx)
Definition pn7160.cpp:949
uint8_t write_mifare_ultralight_tag_(nfc::NfcTagUid &uid, const std::shared_ptr< nfc::NdefMessage > &message)
void loop() override
Definition pn7160.cpp:40
CallbackManager< void()> on_finished_write_callback_
Definition pn7160.h:299
void nci_fsm_set_state_(NCIState new_state)
set new controller state
Definition pn7160.cpp:715
void set_tag_write_message(std::shared_ptr< nfc::NdefMessage > message)
Definition pn7160.cpp:137
uint8_t read_mifare_classic_tag_(nfc::NfcTag &tag)
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
Definition pn7160.h:311
void process_data_message_(nfc::NciMessage &rx)
Definition pn7160.cpp:1004
void dump_config() override
Definition pn7160.cpp:28
void erase_tag_(uint8_t tag_index)
Definition pn7160.cpp:599
CardEmulationState ce_state_
Definition pn7160.h:303
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
Definition pn7160.cpp:1177
uint8_t start_discovery_()
Definition pn7160.cpp:404
void nci_fsm_transition_()
advance controller state as required
Definition pn7160.cpp:613
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag)
optional< size_t > find_tag_uid_(const nfc::NfcTagUid &uid)
Definition pn7160.cpp:572
uint8_t read_endpoint_data_(nfc::NfcTag &tag)
Definition pn7160.cpp:487
uint8_t transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool expect_notification=true)
Definition pn7160.cpp:1120
uint8_t stop_discovery_()
Definition pn7160.cpp:445
bool nci_fsm_set_error_state_(NCIState new_state)
setting controller to this state caused an error; returns true if too many errors/failures
Definition pn7160.cpp:723
void process_rf_intf_activated_oid_(nfc::NciMessage &rx)
Definition pn7160.cpp:835
uint8_t set_discover_map_()
Definition pn7160.cpp:377
std::unique_ptr< nfc::NfcTag > build_tag_(uint8_t mode_tech, const std::vector< uint8_t > &data)
Definition pn7160.cpp:555
uint32_t last_nci_state_change_
Definition pn7160.h:289
uint8_t clean_endpoint_(nfc::NfcTagUid &uid)
Definition pn7160.cpp:507
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout)=0
NCIState nci_state_error_
Definition pn7160.h:305
std::shared_ptr< nfc::NdefMessage > next_task_message_to_write_
Definition pn7160.h:308
enum esphome::pn7160::PN7160::NfcTask EP_READ
void setup() override
Definition pn7160.cpp:15
CallbackManager< void()> on_emulated_tag_scan_callback_
Definition pn7160.h:298
uint8_t deactivate_(uint8_t type, uint16_t timeout=NFCC_DEFAULT_TIMEOUT)
Definition pn7160.cpp:447
GPIOPin * wkup_req_pin_
Definition pn7160.h:296
std::vector< DiscoveredEndpoint > discovered_endpoint_
Definition pn7160.h:301
uint8_t send_core_config_()
Definition pn7160.cpp:333
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
Definition pn7160.h:310
void set_tag_emulation_message(std::shared_ptr< nfc::NdefMessage > message)
Definition pn7160.cpp:45
uint8_t set_listen_mode_routing_()
Definition pn7160.cpp:391
uint8_t refresh_core_config_()
Definition pn7160.cpp:356
void card_emu_t4t_get_response_(std::vector< uint8_t > &response, std::vector< uint8_t > &ndef_response)
Definition pn7160.cpp:1026
uint8_t format_endpoint_(nfc::NfcTagUid &uid)
Definition pn7160.cpp:523
std::shared_ptr< nfc::NdefMessage > card_emulation_message_
Definition pn7160.h:307
uint8_t write_endpoint_(nfc::NfcTagUid &uid, std::shared_ptr< nfc::NdefMessage > &message)
Definition pn7160.cpp:539
GPIOPin * dwl_req_pin_
Definition pn7160.h:293
uint8_t reset_core_(bool reset_config, bool power)
Definition pn7160.cpp:225
uint8_t write_mifare_classic_tag_(const std::shared_ptr< nfc::NdefMessage > &message)
void process_message_()
parse & process incoming messages from the NFCC
Definition pn7160.cpp:740
uint8_t set_test_mode(TestMode test_mode, const std::vector< uint8_t > &data, std::vector< uint8_t > &result)
Definition pn7160.cpp:163
const char * message
Definition component.cpp:38
uint16_t type
char * format_bytes_to(char *buffer, std::span< const uint8_t > bytes)
Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use.
Definition nfc.cpp:15
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.
Definition nfc.cpp:11
uint8_t guess_tag_type(uint8_t uid_length)
Definition nfc.cpp:30
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
if(written< 0)
Definition helpers.h:863
void HOT delay(uint32_t ms)
Definition core.cpp:27
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
const nullopt_t nullopt((nullopt_t::init()))
uint16_t length
Definition tt21100.cpp:0