16static std::unique_ptr<nfc::NdefMessage> build_uri_message(
const std::string &uri,
17 const bool include_android_app_record) {
18 auto ndef_message = make_unique<nfc::NdefMessage>();
20 ndef_message->add_uri_record(uri);
22 if (include_android_app_record) {
23 auto ext_record = make_unique<nfc::NdefRecord>();
24 ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
25 ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
26 ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
27 ndef_message->add_record(std::move(ext_record));
40 LOG_PIN(
" IRQ pin: ", this->
irq_pin_);
41 LOG_PIN(
" VEN pin: ", this->
ven_pin_);
54 auto encoded =
message->encode();
55 if (encoded.size() > CARD_EMU_T4T_MAX_NDEF_SIZE) {
56 ESP_LOGE(TAG,
"Tag emulation message too long: %zu > %u bytes", encoded.size(), CARD_EMU_T4T_MAX_NDEF_SIZE);
60 for (
const uint8_t
byte : encoded) {
63 ESP_LOGD(TAG,
"Tag emulation message set");
79 ESP_LOGD(TAG,
"Tag emulation disabled");
84 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation cannot be enabled");
91 ESP_LOGD(TAG,
"Tag emulation enabled");
99 ESP_LOGD(TAG,
"Tag polling disabled");
107 ESP_LOGD(TAG,
"Tag polling enabled");
112 ESP_LOGD(TAG,
"Waiting to read next tag");
117 ESP_LOGD(TAG,
"Waiting to clean next tag");
122 ESP_LOGD(TAG,
"Waiting to format next tag");
127 ESP_LOGW(TAG,
"Message to write must be set before setting write mode");
132 ESP_LOGD(TAG,
"Waiting to write next tag");
137 ESP_LOGD(TAG,
"Message to write has been set");
145 std::vector<uint8_t> &
result) {
146 auto test_oid = TEST_PRBS_OID;
154 test_oid = TEST_ANTENNA_OID;
158 test_oid = TEST_GET_REGISTER_OID;
163 ESP_LOGD(TAG,
"Exiting test mode");
165 return nfc::STATUS_OK;
168 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
169 ESP_LOGE(TAG,
"Failed to reset NCI core");
172 return nfc::STATUS_FAILED;
177 ESP_LOGE(TAG,
"Failed to initialise NCI core");
180 return nfc::STATUS_FAILED;
186 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, test_oid, data);
188 ESP_LOGW(TAG,
"Starting test mode, OID 0x%02X", test_oid);
191 if (
status != nfc::STATUS_OK) {
192 ESP_LOGE(TAG,
"Failed to start test mode, OID 0x%02X", test_oid);
198 result.assign(payload.begin() + std::min<size_t>(1, payload.size()), payload.end());
200 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
212 {.high_before_ms = NFCC_RESET_DELAY, .low_ms = NFCC_RESET_DELAY, .high_after_ms = NFCC_INIT_TIMEOUT});
216 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_RESET_OID,
217 {(uint8_t) reset_config});
219 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
220 ESP_LOGE(TAG,
"Error sending reset command");
221 return nfc::STATUS_FAILED;
225 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
234 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
237 ESP_LOGE(TAG,
"Error sending initialise command");
238 return nfc::STATUS_FAILED;
242 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
244 return nfc::STATUS_FAILED;
252 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
255 ESP_LOGE(TAG,
"Error enabling proprietary extensions");
256 return nfc::STATUS_FAILED;
259 tx.
set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID, this->
pmu_config());
262 ESP_LOGE(TAG,
"Error sending PMU config");
263 return nfc::STATUS_FAILED;
270 std::span<const uint8_t> core_config = CORE_CONFIG_SOLO;
274 core_config = CORE_CONFIG_RW_CE;
279 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID, core_config);
282 ESP_LOGW(TAG,
"Error sending core config");
283 return nfc::STATUS_FAILED;
286 return nfc::STATUS_OK;
295 return nfc::STATUS_FAILED;
302 ESP_LOGV(TAG,
"Failed to refresh core config");
303 return nfc::STATUS_FAILED;
307 return nfc::STATUS_OK;
312 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID,
313 {
sizeof(RF_DISCOVER_MAP_CONFIG) / 3});
314 tx.
append(RF_DISCOVER_MAP_CONFIG);
316 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
317 ESP_LOGE(TAG,
"Error sending discover map poll config");
318 return nfc::STATUS_FAILED;
320 return nfc::STATUS_OK;
325 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
328 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
329 ESP_LOGE(TAG,
"Error setting listen mode routing config");
330 return nfc::STATUS_FAILED;
332 return nfc::STATUS_OK;
336 std::span<const uint8_t> rf_discovery_config = RF_DISCOVERY_CONFIG;
339 rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
341 rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
345 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID,
346 {
static_cast<uint8_t
>(rf_discovery_config.size())});
347 for (
const uint8_t mode_tech : rf_discovery_config) {
348 tx.append({mode_tech, 0x01});
355 case nfc::DISCOVERY_ALREADY_STARTED:
356 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
357 case nfc::DISCOVERY_TEAR_DOWN:
358 return nfc::STATUS_OK;
361 ESP_LOGE(TAG,
"Error starting discovery");
362 return nfc::STATUS_FAILED;
366 return nfc::STATUS_OK;
373 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {
type});
385 ESP_LOGW(TAG,
"No cached tags to select");
399 uint8_t
interface = nfc::INTF_FRAME;
400 if (endpoint.protocol == nfc::PROT_ISODEP) {
401 interface = nfc::INTF_ISODEP;
402 }
else if (endpoint.protocol == nfc::PROT_MIFARE) {
403 interface = nfc::INTF_TAGCMD;
406 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID,
407 {endpoint.id, endpoint.protocol, interface});
410 ESP_LOGE(TAG,
"Error selecting endpoint");
418 case nfc::PROT_MIFARE:
419 ESP_LOGV(TAG,
"Reading Mifare classic");
423 ESP_LOGV(TAG,
"Reading Mifare ultralight");
427 ESP_LOGV(TAG,
"Reading protocol 0x%02X is not supported", protocol);
430 return nfc::STATUS_FAILED;
435 case nfc::PROT_MIFARE:
442 ESP_LOGE(TAG,
"Unsupported tag for cleaning");
445 return nfc::STATUS_FAILED;
450 case nfc::PROT_MIFARE:
457 ESP_LOGE(TAG,
"Unsupported tag for formatting");
460 return nfc::STATUS_FAILED;
464 std::shared_ptr<nfc::NdefMessage> &
message) {
466 case nfc::PROT_MIFARE:
473 ESP_LOGE(TAG,
"Unsupported tag for writing");
476 return nfc::STATUS_FAILED;
480 if (mode_tech != (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA)) {
484 if (rf_tech_params.size() < 3) {
485 ESP_LOGE(TAG,
"NFC-A parameters too short");
488 const uint8_t uid_length = rf_tech_params[2];
489 if (uid_length == 0 || uid_length > nfc::NFC_UID_MAX_LENGTH || rf_tech_params.size() < 3u + uid_length) {
490 ESP_LOGE(TAG,
"Invalid UID length: %u", uid_length);
493 uid.
assign(rf_tech_params.begin() + 3, rf_tech_params.begin() + 3 + uid_length);
498 if (protocol == nfc::PROT_MIFARE) {
499 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
501 if (protocol == nfc::PROT_T2T) {
502 return make_unique<nfc::NfcTag>(uid, nfc::NFC_FORUM_TYPE_2);
504 return make_unique<nfc::NfcTag>(uid);
509 if (tag_loc.has_value()) {
510 ESP_LOGVV(TAG,
"Tag cache updated");
511 return tag_loc.value();
520 ESP_LOGW(TAG,
"Tag cache full; dropping the tag seen longest ago");
526 .protocol = protocol,
527 .trig_called =
false};
528 ESP_LOGVV(TAG,
"Tag added to cache");
536 bool uid_match = (uid.
size() == existing_tag_uid.size());
539 for (
size_t i = 0; i < uid.
size(); i++) {
540 uid_match &= (uid[i] == existing_tag_uid[i]);
563 for (
auto *trigger : this->triggers_ontagremoved_) {
566#ifdef NFC_TAG_LISTENER_COUNT
571 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
586 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
587 ESP_LOGE(TAG,
"Failed to reset NCI core");
597 ESP_LOGE(TAG,
"Failed to initialise NCI core");
607 ESP_LOGE(TAG,
"Failed to send initial config");
618 ESP_LOGE(TAG,
"Failed to set discover map");
628 ESP_LOGE(TAG,
"Failed to set listen mode routing");
650 ESP_LOGV(TAG,
"Failed to start discovery");
677 ESP_LOGW(TAG,
"Timed out waiting for notification in state %u; resetting NFCC", (uint8_t) this->
nci_state_);
695 ESP_LOGVV(TAG,
"nci_fsm_set_state_(%u)", (uint8_t) new_state);
703 ESP_LOGVV(TAG,
"nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->
error_count_);
708 ESP_LOGE(TAG,
"Too many initialization failures -- check device connections");
712 ESP_LOGW(TAG,
"Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->
nci_state_error_);
721 if (this->
read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
726 case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
727 if (rx.
get_gid() == nfc::RF_GID) {
729 case nfc::RF_INTF_ACTIVATED_OID:
730 ESP_LOGVV(TAG,
"RF_INTF_ACTIVATED_OID");
734 case nfc::RF_DISCOVER_OID:
735 ESP_LOGVV(TAG,
"RF_DISCOVER_OID");
739 case nfc::RF_DEACTIVATE_OID:
740 ESP_LOGVV(TAG,
"RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.
get_message()[3], rx.
get_message()[4]);
745 ESP_LOGV(TAG,
"Unimplemented RF OID received: 0x%02X", rx.
get_oid());
747 }
else if (rx.
get_gid() == nfc::NCI_CORE_GID) {
749 case nfc::NCI_CORE_GENERIC_ERROR_OID:
750 ESP_LOGV(TAG,
"NCI_CORE_GENERIC_ERROR_OID:");
752 case nfc::DISCOVERY_ALREADY_STARTED:
753 ESP_LOGV(TAG,
" DISCOVERY_ALREADY_STARTED");
756 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
758 ESP_LOGV(TAG,
" DISCOVERY_TARGET_ACTIVATION_FAILED");
770 case nfc::DISCOVERY_TEAR_DOWN:
771 ESP_LOGV(TAG,
" DISCOVERY_TEAR_DOWN");
781 ESP_LOGV(TAG,
"Unimplemented NCI Core OID received: 0x%02X", rx.
get_oid());
784 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
789 case nfc::NCI_PKT_MT_CTRL_RESPONSE: {
790 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
791 ESP_LOGV(TAG,
"Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.
get_gid(), rx.
get_oid(),
796 case nfc::NCI_PKT_MT_CTRL_COMMAND: {
797 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
802 case nfc::NCI_PKT_MT_DATA:
807 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
815 uint8_t discovery_id = rx.
get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
816 uint8_t
interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
817 uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
818 uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
819 uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
821 ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
822 interface, protocol, mode_tech, max_size);
824 if (mode_tech & nfc::MODE_LISTEN_MASK) {
825 ESP_LOGVV(TAG,
"Tag activated in listen mode");
826 this->nci_fsm_set_state_(NCIState::RFST_LISTEN_ACTIVE);
831 if (rx.
get_message().
size() < nfc::RF_INTF_ACTIVATED_NTF_RF_TECH_PARAMS) {
832 ESP_LOGE(TAG,
"RF_INTF_ACTIVATED_NTF too short");
839 std::span<const uint8_t>(rx.
get_message()).subspan(nfc::RF_INTF_ACTIVATED_NTF_RF_TECH_PARAMS),
841 ESP_LOGE(TAG,
"Could not build tag");
844 working_endpoint.id = discovery_id;
845 working_endpoint.protocol = protocol;
850 ESP_LOGD(TAG,
" Tag cleaning");
851 if (this->
clean_endpoint_(working_endpoint.protocol) != nfc::STATUS_OK) {
852 ESP_LOGE(TAG,
" Tag cleaning incomplete");
854 ESP_LOGD(TAG,
" Tag cleaned!");
858 ESP_LOGD(TAG,
" Tag formatting");
860 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
862 ESP_LOGD(TAG,
" Tag formatted!");
867 ESP_LOGD(TAG,
" Tag writing\n"
870 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
872 ESP_LOGD(TAG,
" Writing NDEF data");
873 if (this->
write_endpoint_(working_endpoint.protocol, working_endpoint.tag->get_uid(),
874 this->next_task_message_to_write_) != nfc::STATUS_OK) {
875 ESP_LOGE(TAG,
" Failed to write message to tag");
877 ESP_LOGD(TAG,
" Finished writing NDEF data");
886 if (!working_endpoint.trig_called) {
887 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
888 ESP_LOGI(TAG,
"Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(),
890 if (this->
read_endpoint_data_(working_endpoint.protocol, *working_endpoint.tag) != nfc::STATUS_OK) {
891 ESP_LOGW(TAG,
" Unable to read NDEF record(s)");
892 }
else if (working_endpoint.tag->has_ndef_message()) {
893 const auto &
message = working_endpoint.tag->get_ndef_message();
894 const auto &records =
message->get_records();
895 ESP_LOGD(TAG,
" NDEF record(s):");
896 for (
const auto &record : records) {
897 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
900 ESP_LOGW(TAG,
" No NDEF records found");
902 for (
auto *trigger : this->triggers_ontag_) {
903 trigger->process(working_endpoint.tag);
905#ifdef NFC_TAG_LISTENER_COUNT
907 listener->tag_on(*working_endpoint.tag);
910 working_endpoint.trig_called =
true;
915 working_endpoint.last_seen =
millis();
916 if (working_endpoint.protocol == nfc::PROT_MIFARE) {
930 ESP_LOGE(TAG,
"RF_DISCOVER_NTF too short");
933 const uint8_t protocol = rx.
get_message_byte(nfc::RF_DISCOVER_NTF_PROTOCOL);
936 std::span<const uint8_t>(rx.
get_message()).subspan(nfc::RF_DISCOVER_NTF_RF_TECH_PARAMS), uid)) {
937 ESP_LOGE(TAG,
"Could not build tag!");
941 endpoint.protocol = protocol;
946 if (ntf[ntf.size() - 1] != nfc::RF_DISCOVER_NTF_NT_MORE) {
956 case nfc::DEACTIVATION_TYPE_DISCOVERY:
960 case nfc::DEACTIVATION_TYPE_IDLE:
964 case nfc::DEACTIVATION_TYPE_SLEEP:
965 case nfc::DEACTIVATION_TYPE_SLEEP_AF:
981 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
987 if (ndef_response.
empty()) {
993 if (this->
transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT,
false) != nfc::STATUS_OK) {
994 ESP_LOGE(TAG,
"Sending reply for card emulation failed");
1001 const uint16_t ndef_msg_size = ndef_message.
size();
1002 const uint32_t file_size = ndef_msg_size + 2;
1008 for (
uint32_t i = offset; i < offset + static_cast<uint32_t>(
length); i++) {
1010 ndef_response.
push_back(ndef_msg_size >> 8);
1011 }
else if (i == 1) {
1012 ndef_response.
push_back(ndef_msg_size & 0xFF);
1014 ndef_response.
push_back(ndef_message[i - 2]);
1018 ESP_LOGD(TAG,
"NDEF message sent");
1025 ndef_response.
clear();
1027 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation not possible");
1030 if (response.size() < nfc::NCI_PKT_HEADER_SIZE) {
1034 const auto apdu_begin = response.begin() + nfc::NCI_PKT_HEADER_SIZE;
1035 const size_t apdu_size = response.size() - nfc::NCI_PKT_HEADER_SIZE;
1036 auto apdu_is = [&](
const uint8_t *cmd,
size_t cmd_size) {
1037 return apdu_size == cmd_size && std::equal(apdu_begin, response.end(), cmd);
1039 auto apdu_starts_with = [&](
const uint8_t *cmd,
size_t cmd_size) {
1040 return apdu_size >= cmd_size && std::equal(cmd, cmd + cmd_size, apdu_begin);
1042 auto append = [&](std::span<const uint8_t> bytes) {
1043 for (
const uint8_t
byte : bytes) {
1049 if (apdu_is(CARD_EMU_T4T_APP_SELECT,
sizeof(CARD_EMU_T4T_APP_SELECT)) ||
1050 apdu_is(CARD_EMU_T4T_APP_SELECT,
sizeof(CARD_EMU_T4T_APP_SELECT) - 1)) {
1051 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_APP_SELECTED");
1054 }
else if (apdu_is(CARD_EMU_T4T_CC_SELECT,
sizeof(CARD_EMU_T4T_CC_SELECT))) {
1056 ESP_LOGVV(TAG,
"CARD_EMU_CC_SELECTED");
1060 }
else if (apdu_is(CARD_EMU_T4T_NDEF_SELECT,
sizeof(CARD_EMU_T4T_NDEF_SELECT))) {
1061 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_SELECTED");
1064 }
else if (apdu_starts_with(CARD_EMU_T4T_READ,
sizeof(CARD_EMU_T4T_READ)) && apdu_size == 5) {
1066 const uint16_t offset =
encode_uint16(apdu_begin[2], apdu_begin[3]);
1067 const uint8_t
length = apdu_begin[4];
1069 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
1070 if (offset +
static_cast<uint32_t>(
length) <=
sizeof(CARD_EMU_T4T_CC)) {
1071 append(std::span<const uint8_t>(CARD_EMU_T4T_CC).subspan(offset,
length));
1075 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
1078 }
else if (apdu_starts_with(CARD_EMU_T4T_WRITE,
sizeof(CARD_EMU_T4T_WRITE)) && apdu_size >= 5) {
1080 const uint8_t
length = apdu_begin[4];
1082 ESP_LOGVV(TAG,
"CARD_EMU_T4T_WRITE");
1083 char write_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1084 ESP_LOGD(TAG,
"Received %u-byte NDEF message: %s",
length,
1091 append(CARD_EMU_T4T_OK);
1093 ndef_response.
clear();
1094 append(CARD_EMU_T4T_NOK);
1100 const bool expect_notification) {
1101 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1105 uint8_t attempts = 0;
1106 while (this->
write_nfcc(tx) != nfc::STATUS_OK) {
1107 if (++attempts >= NFCC_MAX_COMM_FAILS) {
1108 ESP_LOGE(TAG,
"Error sending message");
1109 return nfc::STATUS_FAILED;
1111 delay(NFCC_WRITE_RETRY_DELAY);
1119 for (uint8_t i = 0; i < NFCC_MAX_COMM_FAILS; i++) {
1120 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1121 ESP_LOGW(TAG,
"Error receiving response");
1122 return nfc::STATUS_FAILED;
1129 ESP_LOGW(TAG,
"Discarding message received while waiting for response: %s",
1136 return nfc::STATUS_FAILED;
1146 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1147 ESP_LOGW(TAG,
"Error receiving credit notification");
1148 return nfc::STATUS_FAILED;
1154 return nfc::STATUS_FAILED;
1157 if (expect_notification) {
1159 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1160 ESP_LOGE(TAG,
"Error receiving data from endpoint");
1161 return nfc::STATUS_FAILED;
1166 return nfc::STATUS_OK;
1170 auto start_time =
millis();
1172 while (
millis() - start_time < timeout) {
1174 return nfc::STATUS_OK;
1177 return nfc::STATUS_FAILED;
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
virtual bool digital_read()=0
void push_back(const T &value)
static constexpr size_t capacity()
void assign(InputIt first, InputIt last)
One NCI packet, held in a fixed buffer so building and reading messages never allocates.
bool message_type_is(uint8_t message_type) 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
bool message_length_is(uint8_t message_length, bool recompute=false)
std::span< const uint8_t > get_payload() const
The payload bytes that follow the header.
void set_message(uint8_t message_type, std::span< const uint8_t > payload)
uint8_t get_simple_status_response() const
bool oid_is(uint8_t oid) const
uint8_t get_message_type() const
void append(std::span< const uint8_t > data)
Appends bytes to the payload; bytes that do not fit are dropped.
StaticVector< NfcTagListener *, NFC_TAG_LISTENER_COUNT > tag_listeners_
uint8_t write_mifare_classic_tag_(const std::shared_ptr< nfc::NdefMessage > &message)
void set_tag_emulation_off()
StaticVector< DiscoveredEndpoint, MAX_DISCOVERED_ENDPOINTS > discovered_endpoint_
uint8_t set_test_mode(TestMode test_mode, const std::vector< uint8_t > &data, std::vector< uint8_t > &result)
bool core_config_is_solo_
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
virtual void prepare_reset()
Chip hooks Called before the NFCC is reset, e.g.
uint32_t last_nci_state_change_
uint8_t write_mifare_ultralight_tag_(nfc::NfcTagUid &uid, const std::shared_ptr< nfc::NdefMessage > &message)
uint8_t send_core_config_()
uint8_t stop_discovery_()
void process_message_()
parse & process incoming messages from the NFCC
size_t find_or_add_tag_(uint8_t protocol, const nfc::NfcTagUid &uid)
Finds a cached endpoint by UID, or caches a new one, evicting the entry seen longest ago if the cache...
uint8_t transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool expect_notification=true)
std::unique_ptr< nfc::NfcTag > build_tag_(uint8_t protocol, const nfc::NfcTagUid &uid)
uint8_t deactivate_(uint8_t type, uint16_t timeout=NFCC_DEFAULT_TIMEOUT)
void set_tag_emulation_message(const std::shared_ptr< nfc::NdefMessage > &message)
uint8_t set_discover_map_()
void nci_fsm_set_state_(NCIState new_state)
set new controller state
uint8_t clean_mifare_ultralight_()
void nci_fsm_transition_()
advance controller state as required
optional< size_t > find_tag_uid_(const nfc::NfcTagUid &uid)
uint8_t send_init_config_()
uint8_t refresh_core_config_()
uint8_t read_mifare_classic_tag_(nfc::NfcTag &tag)
bool card_emu_t4t_read_ndef_(uint16_t offset, uint8_t length, CardEmuResponse &ndef_response)
uint8_t format_endpoint_(uint8_t protocol)
void process_rf_deactivate_oid_(nfc::NciMessage &rx)
virtual uint8_t verify_reset(nfc::NciMessage &rx, bool reset_config)=0
Validates what follows a successful CORE_RESET_RSP (in rx); may read more messages from the NFCC.
uint8_t read_endpoint_data_(uint8_t protocol, nfc::NfcTag &tag)
virtual std::span< const uint8_t > listen_mode_routing_config() const =0
Payload of the RF_SET_LISTEN_MODE_ROUTING_CMD.
void set_tag_write_message(std::shared_ptr< nfc::NdefMessage > message)
uint8_t clean_endpoint_(uint8_t protocol)
uint8_t format_mifare_classic_ndef_()
void process_data_message_(nfc::NciMessage &rx)
std::shared_ptr< nfc::NdefMessage > next_task_message_to_write_
LazyCallbackManager< void()> on_emulated_tag_scan_callback_
uint8_t halt_mifare_classic_tag_()
virtual uint8_t process_init_response(nfc::NciMessage &rx)=0
Logs chip information from a successful CORE_INIT_RSP.
virtual std::span< const uint8_t > pmu_config() const =0
Parameters for the CORE_SET_CONFIG_CMD that configures the power management unit.
void process_rf_discover_oid_(nfc::NciMessage &rx)
uint8_t reset_core_(bool reset_config, bool power)
uint8_t write_endpoint_(uint8_t protocol, nfc::NfcTagUid &uid, std::shared_ptr< nfc::NdefMessage > &message)
uint8_t set_listen_mode_routing_()
void set_tag_emulation_on()
void dump_config() override
LazyCallbackManager< void()> on_finished_write_callback_
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag)
void process_rf_intf_activated_oid_(nfc::NciMessage &rx)
FixedVector< uint8_t > card_emulation_ndef_
virtual uint8_t write_nfcc(nfc::NciMessage &tx)=0
bool nci_fsm_set_error_state_(NCIState new_state)
setting controller to this state caused an error; returns true if too many errors/failures
uint8_t selecting_endpoint_
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout)=0
CardEmulationState ce_state_
void card_emu_t4t_get_response_(std::span< const uint8_t > response, CardEmuResponse &ndef_response)
uint8_t start_discovery_()
void erase_tag_(uint8_t tag_index)
uint8_t format_mifare_classic_mifare_()
bool parse_uid_(uint8_t mode_tech, std::span< const uint8_t > rf_tech_params, nfc::NfcTagUid &uid)
Reads the UID from the RF technology parameters of a discovery or activation notification.
bool config_refresh_pending_
NCIState nci_state_error_
const LogString * message
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.
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.
@ NFCC_SET_LISTEN_MODE_ROUTING
ESPHOME_LOG_TAG(TAG, "pn71xx")
@ CARD_EMU_NDEF_APP_SELECTED
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.