3 #include "automation.h" 13 static const char *
const TAG =
"pn7160";
29 ESP_LOGCONFIG(TAG,
"PN7160:");
33 LOG_PIN(
" IRQ pin: ", this->
irq_pin_);
34 LOG_PIN(
" VEN pin: ", this->
ven_pin_);
47 ESP_LOGD(TAG,
"Tag emulation message set");
56 auto ndef_message = make_unique<nfc::NdefMessage>();
58 ndef_message->add_uri_record(message.
value());
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));
69 ESP_LOGD(TAG,
"Tag emulation message set");
81 ESP_LOGD(TAG,
"Tag emulation disabled");
86 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation cannot be enabled");
93 ESP_LOGD(TAG,
"Tag emulation enabled");
101 ESP_LOGD(TAG,
"Tag polling disabled");
109 ESP_LOGD(TAG,
"Tag polling enabled");
114 ESP_LOGD(TAG,
"Waiting to read next tag");
119 ESP_LOGD(TAG,
"Waiting to clean next tag");
124 ESP_LOGD(TAG,
"Waiting to format next tag");
129 ESP_LOGW(TAG,
"Message to write must be set before setting write mode");
134 ESP_LOGD(TAG,
"Waiting to write next tag");
139 ESP_LOGD(TAG,
"Message to write has been set");
147 auto ndef_message = make_unique<nfc::NdefMessage>();
149 ndef_message->add_uri_record(message.
value());
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));
160 ESP_LOGD(TAG,
"Message to write has been set");
164 std::vector<uint8_t> &result) {
165 auto test_oid = TEST_PRBS_OID;
173 test_oid = TEST_ANTENNA_OID;
177 test_oid = TEST_GET_REGISTER_OID;
182 ESP_LOGD(TAG,
"Exiting test mode");
184 return nfc::STATUS_OK;
187 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
188 ESP_LOGE(TAG,
"Failed to reset NCI core");
191 return nfc::STATUS_FAILED;
196 ESP_LOGE(TAG,
"Failed to initialise NCI core");
199 return nfc::STATUS_FAILED;
205 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, test_oid, data);
207 ESP_LOGW(TAG,
"Starting test mode, OID 0x%02X", test_oid);
210 if (
status != nfc::STATUS_OK) {
211 ESP_LOGE(TAG,
"Failed to start test mode, OID 0x%02X", test_oid);
216 result.erase(result.begin(), result.begin() + 4);
217 if (!result.empty()) {
227 delay(NFCC_DEFAULT_TIMEOUT);
232 delay(NFCC_DEFAULT_TIMEOUT);
234 delay(NFCC_DEFAULT_TIMEOUT);
236 delay(NFCC_INIT_TIMEOUT);
240 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_RESET_OID,
241 {(uint8_t) reset_config});
243 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
244 ESP_LOGE(TAG,
"Error sending reset command");
245 return nfc::STATUS_FAILED;
253 if (this->
read_nfcc(rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
254 ESP_LOGE(TAG,
"Reset notification was not received");
255 return nfc::STATUS_FAILED;
259 (rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET] != 0x02) ||
260 (rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != (uint8_t) reset_config)) {
262 return nfc::STATUS_FAILED;
265 ESP_LOGD(TAG,
"Configuration %s", rx.
get_message()[4] ?
"reset" :
"retained");
266 ESP_LOGD(TAG,
"NCI version: %s", rx.
get_message()[5] == 0x20 ?
"2.0" :
"1.0");
267 ESP_LOGD(TAG,
"Manufacturer ID: 0x%02X", rx.
get_message()[6]);
271 return nfc::STATUS_OK;
276 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
279 ESP_LOGE(TAG,
"Error sending initialise command");
280 return nfc::STATUS_FAILED;
285 return nfc::STATUS_FAILED;
294 ESP_LOGD(TAG,
"Hardware version: %u", hw_version);
295 ESP_LOGD(TAG,
"ROM code version: %u", rom_code_version);
296 ESP_LOGD(TAG,
"FLASH major version: %u", flash_major_version);
297 ESP_LOGD(TAG,
"FLASH minor version: %u", flash_minor_version);
305 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
308 ESP_LOGE(TAG,
"Error enabling proprietary extensions");
309 return nfc::STATUS_FAILED;
312 tx.
set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
313 std::vector<uint8_t>(std::begin(PMU_CFG),
std::end(PMU_CFG)));
316 ESP_LOGE(TAG,
"Error sending PMU config");
317 return nfc::STATUS_FAILED;
324 const auto *core_config_begin = std::begin(CORE_CONFIG_SOLO);
325 const auto *core_config_end =
std::end(CORE_CONFIG_SOLO);
329 core_config_begin = std::begin(CORE_CONFIG_RW_CE);
330 core_config_end =
std::end(CORE_CONFIG_RW_CE);
335 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
336 std::vector<uint8_t>(core_config_begin, core_config_end));
339 ESP_LOGW(TAG,
"Error sending core config");
340 return nfc::STATUS_FAILED;
343 return nfc::STATUS_OK;
352 return nfc::STATUS_FAILED;
359 ESP_LOGV(TAG,
"Failed to refresh core config");
360 return nfc::STATUS_FAILED;
364 return nfc::STATUS_OK;
368 std::vector<uint8_t> discover_map = {
sizeof(RF_DISCOVER_MAP_CONFIG) / 3};
369 discover_map.insert(discover_map.end(), std::begin(RF_DISCOVER_MAP_CONFIG),
std::end(RF_DISCOVER_MAP_CONFIG));
372 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID, discover_map);
375 ESP_LOGE(TAG,
"Error sending discover map poll config");
376 return nfc::STATUS_FAILED;
378 return nfc::STATUS_OK;
384 nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
385 std::vector<uint8_t>(std::begin(RF_LISTEN_MODE_ROUTING_CONFIG),
std::end(RF_LISTEN_MODE_ROUTING_CONFIG)));
388 ESP_LOGE(TAG,
"Error setting listen mode routing config");
389 return nfc::STATUS_FAILED;
391 return nfc::STATUS_OK;
395 const uint8_t *rf_discovery_config = RF_DISCOVERY_CONFIG;
396 uint8_t
length =
sizeof(RF_DISCOVERY_CONFIG);
399 length =
sizeof(RF_DISCOVERY_POLL_CONFIG);
400 rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
402 length =
sizeof(RF_DISCOVERY_LISTEN_CONFIG);
403 rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
406 std::vector<uint8_t> discover_config = std::vector<uint8_t>((length * 2) + 1);
408 discover_config[0] =
length;
409 for (uint8_t i = 0; i <
length; i++) {
410 discover_config[(i * 2) + 1] = rf_discovery_config[i];
411 discover_config[(i * 2) + 2] = 0x01;
415 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID, discover_config);
418 switch (rx.get_simple_status_response()) {
421 case nfc::DISCOVERY_ALREADY_STARTED:
422 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
423 case nfc::DISCOVERY_TEAR_DOWN:
424 return nfc::STATUS_OK;
427 ESP_LOGE(TAG,
"Error starting discovery");
428 return nfc::STATUS_FAILED;
432 return nfc::STATUS_OK;
439 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {type});
451 ESP_LOGW(TAG,
"No cached tags to select");
468 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID, endpoint_data);
471 ESP_LOGE(TAG,
"Error selecting endpoint");
481 case nfc::TAG_TYPE_MIFARE_CLASSIC:
482 ESP_LOGV(TAG,
"Reading Mifare classic");
485 case nfc::TAG_TYPE_2:
486 ESP_LOGV(TAG,
"Reading Mifare ultralight");
489 case nfc::TAG_TYPE_UNKNOWN:
491 ESP_LOGV(TAG,
"Cannot determine tag type");
494 return nfc::STATUS_FAILED;
500 case nfc::TAG_TYPE_MIFARE_CLASSIC:
503 case nfc::TAG_TYPE_2:
507 ESP_LOGE(TAG,
"Unsupported tag for cleaning");
510 return nfc::STATUS_FAILED;
516 case nfc::TAG_TYPE_MIFARE_CLASSIC:
519 case nfc::TAG_TYPE_2:
523 ESP_LOGE(TAG,
"Unsupported tag for formatting");
526 return nfc::STATUS_FAILED;
532 case nfc::TAG_TYPE_MIFARE_CLASSIC:
535 case nfc::TAG_TYPE_2:
539 ESP_LOGE(TAG,
"Unsupported tag for writing");
542 return nfc::STATUS_FAILED;
545 std::unique_ptr<nfc::NfcTag>
PN7160::build_tag_(
const uint8_t mode_tech,
const std::vector<uint8_t> &data) {
547 case (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA): {
548 uint8_t uid_length = data[2];
550 ESP_LOGE(TAG,
"UID length cannot be zero");
553 std::vector<uint8_t> uid(data.begin() + 3, data.begin() + 3 + uid_length);
554 const auto *tag_type_str =
555 nfc::guess_tag_type(uid_length) == nfc::TAG_TYPE_MIFARE_CLASSIC ? nfc::MIFARE_CLASSIC : nfc::NFC_FORUM_TYPE_2;
556 return make_unique<nfc::NfcTag>(uid, tag_type_str);
566 bool uid_match = (uid.size() == existing_tag_uid.size());
569 for (
size_t i = 0; i < uid.size(); i++) {
570 uid_match &= (uid[i] == existing_tag_uid[i]);
605 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
606 ESP_LOGE(TAG,
"Failed to reset NCI core");
616 ESP_LOGE(TAG,
"Failed to initialise NCI core");
626 ESP_LOGE(TAG,
"Failed to send initial config");
637 ESP_LOGE(TAG,
"Failed to set discover map");
647 ESP_LOGE(TAG,
"Failed to set listen mode routing");
669 ESP_LOGV(TAG,
"Failed to start discovery");
705 ESP_LOGVV(TAG,
"nci_fsm_set_state_(%u)", (uint8_t) new_state);
713 ESP_LOGVV(TAG,
"nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->
error_count_);
718 ESP_LOGE(TAG,
"Too many initialization failures -- check device connections");
722 ESP_LOGW(TAG,
"Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->
nci_state_error_);
731 if (this->
read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
736 case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
737 if (rx.
get_gid() == nfc::RF_GID) {
739 case nfc::RF_INTF_ACTIVATED_OID:
740 ESP_LOGVV(TAG,
"RF_INTF_ACTIVATED_OID");
744 case nfc::RF_DISCOVER_OID:
745 ESP_LOGVV(TAG,
"RF_DISCOVER_OID");
749 case nfc::RF_DEACTIVATE_OID:
750 ESP_LOGVV(TAG,
"RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.
get_message()[3], rx.
get_message()[4]);
755 ESP_LOGV(TAG,
"Unimplemented RF OID received: 0x%02X", rx.
get_oid());
757 }
else if (rx.
get_gid() == nfc::NCI_CORE_GID) {
759 case nfc::NCI_CORE_GENERIC_ERROR_OID:
760 ESP_LOGV(TAG,
"NCI_CORE_GENERIC_ERROR_OID:");
762 case nfc::DISCOVERY_ALREADY_STARTED:
763 ESP_LOGV(TAG,
" DISCOVERY_ALREADY_STARTED");
766 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
768 ESP_LOGV(TAG,
" DISCOVERY_TARGET_ACTIVATION_FAILED");
780 case nfc::DISCOVERY_TEAR_DOWN:
781 ESP_LOGV(TAG,
" DISCOVERY_TEAR_DOWN");
791 ESP_LOGV(TAG,
"Unimplemented NCI Core OID received: 0x%02X", rx.
get_oid());
798 case nfc::NCI_PKT_MT_CTRL_RESPONSE:
799 ESP_LOGV(TAG,
"Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.
get_gid(), rx.
get_oid(),
803 case nfc::NCI_PKT_MT_CTRL_COMMAND:
807 case nfc::NCI_PKT_MT_DATA:
818 uint8_t discovery_id = rx.
get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
819 uint8_t
interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
820 uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
821 uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
822 uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
824 ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
825 interface, protocol, mode_tech, max_size);
827 if (mode_tech & nfc::MODE_LISTEN_MASK) {
828 ESP_LOGVV(TAG,
"Tag activated in listen mode");
837 if (incoming_tag ==
nullptr) {
838 ESP_LOGE(TAG,
"Could not build tag");
841 if (tag_loc.has_value()) {
845 ESP_LOGVV(TAG,
"Tag cache updated");
850 ESP_LOGVV(TAG,
"Tag added to cache");
855 switch (this->next_task_) {
857 ESP_LOGD(TAG,
" Tag cleaning...");
858 if (this->
clean_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
859 ESP_LOGE(TAG,
" Tag cleaning incomplete");
861 ESP_LOGD(TAG,
" Tag cleaned!");
865 ESP_LOGD(TAG,
" Tag formatting...");
866 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
867 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
869 ESP_LOGD(TAG,
" Tag formatted!");
874 ESP_LOGD(TAG,
" Tag writing...");
875 ESP_LOGD(TAG,
" Tag formatting...");
876 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
877 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
879 ESP_LOGD(TAG,
" Writing NDEF data");
882 ESP_LOGE(TAG,
" Failed to write message to tag");
884 ESP_LOGD(TAG,
" Finished writing NDEF data");
893 if (!working_endpoint.trig_called) {
894 ESP_LOGI(TAG,
"Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(),
897 ESP_LOGW(TAG,
" Unable to read NDEF record(s)");
898 }
else if (working_endpoint.tag->has_ndef_message()) {
899 const auto message = working_endpoint.tag->get_ndef_message();
900 const auto records = message->get_records();
901 ESP_LOGD(TAG,
" NDEF record(s):");
902 for (
const auto &record : records) {
903 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
906 ESP_LOGW(TAG,
" No NDEF records found");
909 trigger->process(working_endpoint.tag);
912 listener->tag_on(*working_endpoint.tag);
914 working_endpoint.trig_called =
true;
918 if (working_endpoint.tag->get_tag_type() == nfc::MIFARE_CLASSIC) {
922 if (this->next_task_ !=
EP_READ) {
934 if (incoming_tag ==
nullptr) {
935 ESP_LOGE(TAG,
"Could not build tag!");
938 if (tag_loc.has_value()) {
942 ESP_LOGVV(TAG,
"Tag found & updated");
946 millis(), std::move(incoming_tag),
false});
947 ESP_LOGVV(TAG,
"Tag saved");
951 if (rx.
get_message().back() != nfc::RF_DISCOVER_NTF_NT_MORE) {
961 case nfc::DEACTIVATION_TYPE_DISCOVERY:
965 case nfc::DEACTIVATION_TYPE_IDLE:
969 case nfc::DEACTIVATION_TYPE_SLEEP:
970 case nfc::DEACTIVATION_TYPE_SLEEP_AF:
988 std::vector<uint8_t> ndef_response;
991 uint16_t ndef_response_size = ndef_response.size();
992 if (!ndef_response_size) {
996 std::vector<uint8_t> tx_msg = {nfc::NCI_PKT_MT_DATA, uint8_t((ndef_response_size & 0xFF00) >> 8),
997 uint8_t(ndef_response_size & 0x00FF)};
998 tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end());
1001 if (this->
transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT,
false) != nfc::STATUS_OK) {
1002 ESP_LOGE(TAG,
"Sending reply for card emulation failed");
1008 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation not possible");
1009 ndef_response.clear();
1013 if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_APP_SELECT))) {
1015 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_APP_SELECTED");
1017 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1018 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_CC_SELECT))) {
1021 ESP_LOGVV(TAG,
"CARD_EMU_CC_SELECTED");
1023 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1025 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_NDEF_SELECT))) {
1027 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_SELECTED");
1029 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1030 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1031 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_READ),
1032 std::begin(CARD_EMU_T4T_READ))) {
1036 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
1037 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1038 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1040 if (length <= (
sizeof(CARD_EMU_T4T_CC) + offset + 2)) {
1041 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_CC) + offset,
1042 std::begin(CARD_EMU_T4T_CC) + offset +
length);
1043 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1047 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
1049 uint16_t ndef_msg_size = ndef_message.size();
1050 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1051 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1053 ESP_LOGVV(TAG,
"Encoded NDEF message: %s",
nfc::format_bytes(ndef_message).c_str());
1055 if (length <= (ndef_msg_size + offset + 2)) {
1057 ndef_response.resize(2);
1058 ndef_response[0] = (ndef_msg_size & 0xFF00) >> 8;
1059 ndef_response[1] = (ndef_msg_size & 0x00FF);
1061 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 2);
1063 }
else if (offset == 1) {
1064 ndef_response.resize(1);
1065 ndef_response[0] = (ndef_msg_size & 0x00FF);
1067 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 1);
1070 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length);
1073 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1075 if ((offset + length) >= (ndef_msg_size + 2)) {
1076 ESP_LOGD(TAG,
"NDEF message sent");
1081 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1082 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_WRITE),
1083 std::begin(CARD_EMU_T4T_WRITE))) {
1086 ESP_LOGVV(TAG,
"CARD_EMU_T4T_WRITE");
1087 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1088 std::vector<uint8_t> ndef_msg_written;
1090 ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5,
1091 response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 +
length);
1092 ESP_LOGD(TAG,
"Received %u-byte NDEF message: %s", length,
nfc::format_bytes(ndef_msg_written).c_str());
1093 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1099 const bool expect_notification) {
1100 uint8_t retries = NFCC_MAX_COMM_FAILS;
1104 if (this->
write_nfcc(tx) != nfc::STATUS_OK) {
1105 ESP_LOGE(TAG,
"Error sending message");
1106 return nfc::STATUS_FAILED;
1110 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1111 ESP_LOGW(TAG,
"Error receiving message");
1113 ESP_LOGE(TAG,
" ...giving up");
1114 return nfc::STATUS_FAILED;
1126 return nfc::STATUS_FAILED;
1138 return nfc::STATUS_FAILED;
1141 if (expect_notification) {
1143 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1144 ESP_LOGE(TAG,
"Error receiving data from endpoint");
1145 return nfc::STATUS_FAILED;
1150 return nfc::STATUS_OK;
1155 auto start_time =
millis();
1157 while (
millis() - start_time < timeout) {
1159 return nfc::STATUS_OK;
1162 ESP_LOGW(TAG,
"Timed out waiting for IRQ state");
1163 return nfc::STATUS_FAILED;
CardEmulationState ce_state_
virtual void digital_write(bool value)=0
value_type const & value() const
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout)=0
uint8_t stop_discovery_()
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
std::vector< NfcTagListener * > tag_listeners_
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag)
uint8_t get_message_type() const
void set_tag_emulation_message(std::shared_ptr< nfc::NdefMessage > message)
void set_tag_emulation_off()
std::string format_uid(std::vector< uint8_t > &uid)
std::shared_ptr< nfc::NdefMessage > next_task_message_to_write_
void card_emu_t4t_get_response_(std::vector< uint8_t > &response, std::vector< uint8_t > &ndef_response)
void set_tag_emulation_on()
CallbackManager< void()> on_finished_write_callback_
bool message_type_is(uint8_t message_type) const
uint8_t halt_mifare_classic_tag_()
std::vector< DiscoveredEndpoint > discovered_endpoint_
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
uint8_t start_discovery_()
uint32_t IRAM_ATTR HOT millis()
uint8_t write_mifare_classic_tag_(const std::shared_ptr< nfc::NdefMessage > &message)
std::shared_ptr< nfc::NdefMessage > card_emulation_message_
uint8_t get_message_byte(uint8_t offset) const
uint8_t set_test_mode(TestMode test_mode, const std::vector< uint8_t > &data, std::vector< uint8_t > &result)
void process_rf_discover_oid_(nfc::NciMessage &rx)
uint8_t read_mifare_classic_tag_(nfc::NfcTag &tag)
NCIState nci_state_error_
const nullopt_t nullopt((nullopt_t::init()))
uint32_t last_nci_state_change_
virtual uint8_t write_nfcc(nfc::NciMessage &tx)=0
std::vector< uint8_t > & get_message()
uint8_t format_endpoint_(std::vector< uint8_t > &uid)
void nci_fsm_set_state_(NCIState new_state)
set new controller state
uint8_t selecting_endpoint_
uint8_t guess_tag_type(uint8_t uid_length)
void process_rf_intf_activated_oid_(nfc::NciMessage &rx)
uint8_t set_discover_map_()
void process_message_()
parse & process incoming messages from the NFCC
bool core_config_is_solo_
void process_data_message_(nfc::NciMessage &rx)
std::vector< uint8_t > & get_uid()
uint8_t send_core_config_()
bool gid_is(uint8_t gid) const
virtual bool digital_read()=0
void erase_tag_(uint8_t tag_index)
bool simple_status_response_is(uint8_t response) const
void process_rf_deactivate_oid_(nfc::NciMessage &rx)
uint8_t deactivate_(uint8_t type, uint16_t timeout=NFCC_DEFAULT_TIMEOUT)
void dump_config() override
uint8_t write_mifare_ultralight_tag_(std::vector< uint8_t > &uid, const std::shared_ptr< nfc::NdefMessage > &message)
uint8_t format_mifare_classic_mifare_()
virtual void mark_failed()
Mark this component as failed.
void set_message(uint8_t message_type, const std::vector< uint8_t > &payload)
bool oid_is(uint8_t oid) const
void set_tag_write_message(std::shared_ptr< nfc::NdefMessage > message)
uint8_t read_endpoint_data_(nfc::NfcTag &tag)
void nci_fsm_transition_()
advance controller state as required
uint8_t transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool expect_notification=true)
uint8_t write_endpoint_(std::vector< uint8_t > &uid, std::shared_ptr< nfc::NdefMessage > &message)
bool message_length_is(uint8_t message_length, bool recompute=false)
Implementation of SPI Controller mode.
uint8_t refresh_core_config_()
optional< size_t > find_tag_uid_(const std::vector< uint8_t > &uid)
std::unique_ptr< nfc::NfcTag > build_tag_(uint8_t mode_tech, const std::vector< uint8_t > &data)
uint8_t format_mifare_classic_ndef_()
uint8_t send_init_config_()
uint8_t get_simple_status_response() const
uint8_t clean_mifare_ultralight_()
uint8_t clean_endpoint_(std::vector< uint8_t > &uid)
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 reset_core_(bool reset_config, bool power)
enum esphome::pn7160::PN7160::NfcTask EP_READ
bool config_refresh_pending_
std::string format_bytes(std::vector< uint8_t > &bytes)
CallbackManager< void()> on_emulated_tag_scan_callback_
uint8_t set_listen_mode_routing_()
void IRAM_ATTR HOT delay(uint32_t ms)