13static const char *
const TAG =
"pn7150";
23 ESP_LOGCONFIG(TAG,
"PN7150:");
24 LOG_PIN(
" IRQ pin: ", this->
irq_pin_);
25 LOG_PIN(
" VEN pin: ", this->
ven_pin_);
35 ESP_LOGD(TAG,
"Tag emulation message set");
44 auto ndef_message = make_unique<nfc::NdefMessage>();
46 ndef_message->add_uri_record(
message.value());
48 if (!include_android_app_record.
has_value() || include_android_app_record.
value()) {
49 auto ext_record = make_unique<nfc::NdefRecord>();
50 ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
51 ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
52 ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
53 ndef_message->add_record(std::move(ext_record));
57 ESP_LOGD(TAG,
"Tag emulation message set");
69 ESP_LOGD(TAG,
"Tag emulation disabled");
74 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation cannot be enabled");
81 ESP_LOGD(TAG,
"Tag emulation enabled");
89 ESP_LOGD(TAG,
"Tag polling disabled");
97 ESP_LOGD(TAG,
"Tag polling enabled");
102 ESP_LOGD(TAG,
"Waiting to read next tag");
107 ESP_LOGD(TAG,
"Waiting to clean next tag");
112 ESP_LOGD(TAG,
"Waiting to format next tag");
117 ESP_LOGW(TAG,
"Message to write must be set before setting write mode");
122 ESP_LOGD(TAG,
"Waiting to write next tag");
127 ESP_LOGD(TAG,
"Message to write has been set");
135 auto ndef_message = make_unique<nfc::NdefMessage>();
137 ndef_message->add_uri_record(
message.value());
139 if (!include_android_app_record.
has_value() || include_android_app_record.
value()) {
140 auto ext_record = make_unique<nfc::NdefRecord>();
141 ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
142 ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
143 ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
144 ndef_message->add_record(std::move(ext_record));
148 ESP_LOGD(TAG,
"Message to write has been set");
152 std::vector<uint8_t> &result) {
153 auto test_oid = TEST_PRBS_OID;
161 test_oid = TEST_ANTENNA_OID;
165 test_oid = TEST_GET_REGISTER_OID;
170 ESP_LOGD(TAG,
"Exiting test mode");
172 return nfc::STATUS_OK;
175 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
176 ESP_LOGE(TAG,
"Failed to reset NCI core");
179 return nfc::STATUS_FAILED;
184 ESP_LOGE(TAG,
"Failed to initialise NCI core");
187 return nfc::STATUS_FAILED;
193 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, test_oid, data);
195 ESP_LOGW(TAG,
"Starting test mode, OID 0x%02X", test_oid);
198 if (
status != nfc::STATUS_OK) {
199 ESP_LOGE(TAG,
"Failed to start test mode, OID 0x%02X", test_oid);
204 result.erase(result.begin(), result.begin() + 4);
205 if (!result.empty()) {
206 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
216 delay(NFCC_DEFAULT_TIMEOUT);
218 delay(NFCC_DEFAULT_TIMEOUT);
220 delay(NFCC_INIT_TIMEOUT);
224 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_RESET_OID,
225 {(uint8_t) reset_config});
227 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
228 ESP_LOGE(TAG,
"Error sending reset command");
229 return nfc::STATUS_FAILED;
233 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
239 (rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != 0x11) ||
240 (rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] != (uint8_t) reset_config)) {
241 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
243 return nfc::STATUS_FAILED;
249 rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] ?
"reset" :
"retained",
250 rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] == 0x20 ?
"2.0" :
"1.0");
252 return nfc::STATUS_OK;
257 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
260 ESP_LOGE(TAG,
"Error sending initialise command");
261 return nfc::STATUS_FAILED;
265 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
267 return nfc::STATUS_FAILED;
277 "Manufacturer ID: 0x%02X\n"
278 "Hardware version: 0x%02X\n"
279 "ROM code version: 0x%02X\n"
280 "FLASH major version: 0x%02X\n"
281 "FLASH minor version: 0x%02X",
282 manf_id, hw_version, rom_code_version, flash_major_version, flash_minor_version);
289 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
292 ESP_LOGE(TAG,
"Error enabling proprietary extensions");
293 return nfc::STATUS_FAILED;
296 tx.
set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
297 std::vector<uint8_t>(std::begin(PMU_CFG), std::end(PMU_CFG)));
300 ESP_LOGE(TAG,
"Error sending PMU config");
301 return nfc::STATUS_FAILED;
308 const auto *core_config_begin = std::begin(CORE_CONFIG_SOLO);
309 const auto *core_config_end = std::end(CORE_CONFIG_SOLO);
313 core_config_begin = std::begin(CORE_CONFIG_RW_CE);
314 core_config_end = std::end(CORE_CONFIG_RW_CE);
319 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
320 std::vector<uint8_t>(core_config_begin, core_config_end));
323 ESP_LOGW(TAG,
"Error sending core config");
324 return nfc::STATUS_FAILED;
327 return nfc::STATUS_OK;
336 return nfc::STATUS_FAILED;
343 ESP_LOGV(TAG,
"Failed to refresh core config");
344 return nfc::STATUS_FAILED;
348 return nfc::STATUS_OK;
352 std::vector<uint8_t> discover_map = {
sizeof(RF_DISCOVER_MAP_CONFIG) / 3};
353 discover_map.insert(discover_map.end(), std::begin(RF_DISCOVER_MAP_CONFIG), std::end(RF_DISCOVER_MAP_CONFIG));
356 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID, discover_map);
358 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
359 ESP_LOGE(TAG,
"Error sending discover map poll config");
360 return nfc::STATUS_FAILED;
362 return nfc::STATUS_OK;
368 nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
369 std::vector<uint8_t>(std::begin(RF_LISTEN_MODE_ROUTING_CONFIG), std::end(RF_LISTEN_MODE_ROUTING_CONFIG)));
371 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
372 ESP_LOGE(TAG,
"Error setting listen mode routing config");
373 return nfc::STATUS_FAILED;
375 return nfc::STATUS_OK;
379 const uint8_t *rf_discovery_config = RF_DISCOVERY_CONFIG;
380 uint8_t
length =
sizeof(RF_DISCOVERY_CONFIG);
383 length =
sizeof(RF_DISCOVERY_POLL_CONFIG);
384 rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
386 length =
sizeof(RF_DISCOVERY_LISTEN_CONFIG);
387 rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
390 std::vector<uint8_t> discover_config = std::vector<uint8_t>((
length * 2) + 1);
392 discover_config[0] =
length;
393 for (uint8_t i = 0; i <
length; i++) {
394 discover_config[(i * 2) + 1] = rf_discovery_config[i];
395 discover_config[(i * 2) + 2] = 0x01;
399 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID, discover_config);
405 case nfc::DISCOVERY_ALREADY_STARTED:
406 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
407 case nfc::DISCOVERY_TEAR_DOWN:
408 return nfc::STATUS_OK;
411 ESP_LOGE(TAG,
"Error starting discovery");
412 return nfc::STATUS_FAILED;
416 return nfc::STATUS_OK;
423 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {
type});
435 ESP_LOGW(TAG,
"No cached tags to select");
452 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID, endpoint_data);
455 ESP_LOGE(TAG,
"Error selecting endpoint");
465 case nfc::TAG_TYPE_MIFARE_CLASSIC:
466 ESP_LOGV(TAG,
"Reading Mifare classic");
469 case nfc::TAG_TYPE_2:
470 ESP_LOGV(TAG,
"Reading Mifare ultralight");
473 case nfc::TAG_TYPE_UNKNOWN:
475 ESP_LOGV(TAG,
"Cannot determine tag type");
478 return nfc::STATUS_FAILED;
484 case nfc::TAG_TYPE_MIFARE_CLASSIC:
487 case nfc::TAG_TYPE_2:
491 ESP_LOGE(TAG,
"Unsupported tag for cleaning");
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 formatting");
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 writing");
526 return nfc::STATUS_FAILED;
529std::unique_ptr<nfc::NfcTag>
PN7150::build_tag_(
const uint8_t mode_tech,
const std::vector<uint8_t> &data) {
531 case (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA): {
532 uint8_t uid_length = data[2];
534 ESP_LOGE(TAG,
"UID length cannot be zero");
537 std::vector<uint8_t> uid(data.begin() + 3, data.begin() + 3 + uid_length);
538 const auto *tag_type_str =
539 nfc::guess_tag_type(uid_length) == nfc::TAG_TYPE_MIFARE_CLASSIC ? nfc::MIFARE_CLASSIC : nfc::NFC_FORUM_TYPE_2;
540 return make_unique<nfc::NfcTag>(uid, tag_type_str);
550 bool uid_match = (uid.size() == existing_tag_uid.size());
553 for (
size_t i = 0; i < uid.size(); i++) {
554 uid_match &= (uid[i] == existing_tag_uid[i]);
581 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
590 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
591 ESP_LOGE(TAG,
"Failed to reset NCI core");
601 ESP_LOGE(TAG,
"Failed to initialise NCI core");
611 ESP_LOGE(TAG,
"Failed to send initial config");
622 ESP_LOGE(TAG,
"Failed to set discover map");
632 ESP_LOGE(TAG,
"Failed to set listen mode routing");
654 ESP_LOGV(TAG,
"Failed to start discovery");
691 ESP_LOGVV(TAG,
"nci_fsm_set_state_(%u)", (uint8_t) new_state);
699 ESP_LOGVV(TAG,
"nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->
error_count_);
704 ESP_LOGE(TAG,
"Too many initialization failures -- check device connections");
708 ESP_LOGW(TAG,
"Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->
nci_state_error_);
717 if (this->
read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
722 case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
723 if (rx.
get_gid() == nfc::RF_GID) {
725 case nfc::RF_INTF_ACTIVATED_OID:
726 ESP_LOGVV(TAG,
"RF_INTF_ACTIVATED_OID");
730 case nfc::RF_DISCOVER_OID:
731 ESP_LOGVV(TAG,
"RF_DISCOVER_OID");
735 case nfc::RF_DEACTIVATE_OID:
736 ESP_LOGVV(TAG,
"RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.
get_message()[3], rx.
get_message()[4]);
741 ESP_LOGV(TAG,
"Unimplemented RF OID received: 0x%02X", rx.
get_oid());
743 }
else if (rx.
get_gid() == nfc::NCI_CORE_GID) {
745 case nfc::NCI_CORE_GENERIC_ERROR_OID:
746 ESP_LOGV(TAG,
"NCI_CORE_GENERIC_ERROR_OID:");
748 case nfc::DISCOVERY_ALREADY_STARTED:
749 ESP_LOGV(TAG,
" DISCOVERY_ALREADY_STARTED");
752 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
754 ESP_LOGV(TAG,
" DISCOVERY_TARGET_ACTIVATION_FAILED");
766 case nfc::DISCOVERY_TEAR_DOWN:
767 ESP_LOGV(TAG,
" DISCOVERY_TEAR_DOWN");
777 ESP_LOGV(TAG,
"Unimplemented NCI Core OID received: 0x%02X", rx.
get_oid());
780 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
785 case nfc::NCI_PKT_MT_CTRL_RESPONSE: {
786 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
787 ESP_LOGV(TAG,
"Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.
get_gid(), rx.
get_oid(),
792 case nfc::NCI_PKT_MT_CTRL_COMMAND: {
793 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
798 case nfc::NCI_PKT_MT_DATA:
803 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
811 uint8_t discovery_id = rx.
get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
812 uint8_t
interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
813 uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
814 uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
815 uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
817 ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
818 interface, protocol, mode_tech, max_size);
820 if (mode_tech & nfc::MODE_LISTEN_MASK) {
821 ESP_LOGVV(TAG,
"Tag activated in listen mode");
822 this->nci_fsm_set_state_(NCIState::RFST_LISTEN_ACTIVE);
830 if (incoming_tag ==
nullptr) {
831 ESP_LOGE(TAG,
"Could not build tag");
834 if (tag_loc.has_value()) {
838 ESP_LOGVV(TAG,
"Tag cache updated");
843 ESP_LOGVV(TAG,
"Tag added to cache");
848 switch (this->next_task_) {
850 ESP_LOGD(TAG,
" Tag cleaning");
851 if (this->
clean_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
852 ESP_LOGE(TAG,
" Tag cleaning incomplete");
854 ESP_LOGD(TAG,
" Tag cleaned!");
858 ESP_LOGD(TAG,
" Tag formatting");
859 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
860 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
862 ESP_LOGD(TAG,
" Tag formatted!");
867 ESP_LOGD(TAG,
" Tag writing\n"
869 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
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.tag->get_uid(), this->next_task_message_to_write_) !=
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(),
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");
903 trigger->process(working_endpoint.tag);
906 listener->tag_on(*working_endpoint.tag);
908 working_endpoint.trig_called =
true;
912 if (working_endpoint.tag->get_tag_type() == nfc::MIFARE_CLASSIC) {
916 if (this->next_task_ !=
EP_READ) {
928 if (incoming_tag ==
nullptr) {
929 ESP_LOGE(TAG,
"Could not build tag!");
932 if (tag_loc.has_value()) {
936 ESP_LOGVV(TAG,
"Tag found & updated");
940 millis(), std::move(incoming_tag),
false});
941 ESP_LOGVV(TAG,
"Tag saved");
945 if (rx.
get_message().back() != nfc::RF_DISCOVER_NTF_NT_MORE) {
955 case nfc::DEACTIVATION_TYPE_DISCOVERY:
959 case nfc::DEACTIVATION_TYPE_IDLE:
963 case nfc::DEACTIVATION_TYPE_SLEEP:
964 case nfc::DEACTIVATION_TYPE_SLEEP_AF:
980 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
983 std::vector<uint8_t> ndef_response;
986 uint16_t ndef_response_size = ndef_response.size();
987 if (!ndef_response_size) {
991 std::vector<uint8_t> tx_msg = {nfc::NCI_PKT_MT_DATA, uint8_t((ndef_response_size & 0xFF00) >> 8),
992 uint8_t(ndef_response_size & 0x00FF)};
993 tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end());
996 if (this->
transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT,
false) != nfc::STATUS_OK) {
997 ESP_LOGE(TAG,
"Sending reply for card emulation failed");
1003 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation not possible");
1004 ndef_response.clear();
1008 if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_APP_SELECT))) {
1010 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_APP_SELECTED");
1012 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1013 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_CC_SELECT))) {
1016 ESP_LOGVV(TAG,
"CARD_EMU_CC_SELECTED");
1018 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1020 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_NDEF_SELECT))) {
1022 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_SELECTED");
1024 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,
1026 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_READ),
1027 std::begin(CARD_EMU_T4T_READ))) {
1031 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
1032 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1033 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1035 if (
length <= (
sizeof(CARD_EMU_T4T_CC) + offset + 2)) {
1036 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_CC) + offset,
1037 std::begin(CARD_EMU_T4T_CC) + offset +
length);
1038 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1042 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
1044 uint16_t ndef_msg_size = ndef_message.size();
1045 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1046 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1048 char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1051 if (
length <= (ndef_msg_size + offset + 2)) {
1053 ndef_response.resize(2);
1054 ndef_response[0] = (ndef_msg_size & 0xFF00) >> 8;
1055 ndef_response[1] = (ndef_msg_size & 0x00FF);
1057 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length - 2);
1059 }
else if (offset == 1) {
1060 ndef_response.resize(1);
1061 ndef_response[0] = (ndef_msg_size & 0x00FF);
1063 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length - 1);
1066 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length);
1069 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1071 if ((offset +
length) >= (ndef_msg_size + 2)) {
1072 ESP_LOGD(TAG,
"NDEF message sent");
1077 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1078 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_WRITE),
1079 std::begin(CARD_EMU_T4T_WRITE))) {
1082 ESP_LOGVV(TAG,
"CARD_EMU_T4T_WRITE");
1083 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1084 std::vector<uint8_t> ndef_msg_written;
1086 ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5,
1087 response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 +
length);
1088 char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1090 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1096 const bool expect_notification) {
1097 uint8_t retries = NFCC_MAX_COMM_FAILS;
1098 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1102 if (this->
write_nfcc(tx) != nfc::STATUS_OK) {
1103 ESP_LOGE(TAG,
"Error sending message");
1104 return nfc::STATUS_FAILED;
1108 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1109 ESP_LOGW(TAG,
"Error receiving message");
1111 ESP_LOGE(TAG,
" ...giving up");
1112 return nfc::STATUS_FAILED;
1124 return nfc::STATUS_FAILED;
1136 return nfc::STATUS_FAILED;
1139 if (expect_notification) {
1141 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1142 ESP_LOGE(TAG,
"Error receiving data from endpoint");
1143 return nfc::STATUS_FAILED;
1148 return nfc::STATUS_OK;
1153 auto start_time =
millis();
1155 while (
millis() - start_time < timeout) {
1157 return nfc::STATUS_OK;
1160 ESP_LOGW(TAG,
"Timed out waiting for IRQ state");
1161 return nfc::STATUS_FAILED;
virtual void mark_failed()
Mark this component as failed.
virtual void digital_write(bool value)=0
virtual bool digital_read()=0
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
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_simple_status_response() const
bool oid_is(uint8_t oid) const
uint8_t get_message_type() const
std::vector< uint8_t > & get_uid()
std::vector< NfcTagListener * > tag_listeners_
value_type const & value() const
void process_message_()
parse & process incoming messages from the NFCC
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
bool core_config_is_solo_
std::shared_ptr< nfc::NdefMessage > next_task_message_to_write_
uint8_t write_mifare_classic_tag_(const std::shared_ptr< nfc::NdefMessage > &message)
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 mode_tech, const std::vector< uint8_t > &data)
void nci_fsm_transition_()
advance controller state as required
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag)
CardEmulationState ce_state_
std::vector< DiscoveredEndpoint > discovered_endpoint_
void process_rf_intf_activated_oid_(nfc::NciMessage &rx)
uint8_t format_mifare_classic_mifare_()
void card_emu_t4t_get_response_(std::vector< uint8_t > &response, std::vector< uint8_t > &ndef_response)
uint8_t deactivate_(uint8_t type, uint16_t timeout=NFCC_DEFAULT_TIMEOUT)
uint8_t halt_mifare_classic_tag_()
void process_data_message_(nfc::NciMessage &rx)
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout)=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 clean_endpoint_(std::vector< uint8_t > &uid)
uint8_t write_mifare_ultralight_tag_(std::vector< uint8_t > &uid, const std::shared_ptr< nfc::NdefMessage > &message)
void set_tag_emulation_message(std::shared_ptr< nfc::NdefMessage > message)
void set_tag_write_message(std::shared_ptr< nfc::NdefMessage > message)
void set_tag_emulation_on()
uint8_t set_listen_mode_routing_()
NCIState nci_state_error_
uint8_t format_endpoint_(std::vector< uint8_t > &uid)
void process_rf_deactivate_oid_(nfc::NciMessage &rx)
uint8_t refresh_core_config_()
uint8_t write_endpoint_(std::vector< uint8_t > &uid, std::shared_ptr< nfc::NdefMessage > &message)
uint8_t selecting_endpoint_
uint8_t send_init_config_()
void erase_tag_(uint8_t tag_index)
uint8_t clean_mifare_ultralight_()
uint8_t set_test_mode(TestMode test_mode, const std::vector< uint8_t > &data, std::vector< uint8_t > &result)
CallbackManager< void()> on_finished_write_callback_
uint8_t format_mifare_classic_ndef_()
bool config_refresh_pending_
uint8_t read_mifare_classic_tag_(nfc::NfcTag &tag)
uint8_t reset_core_(bool reset_config, bool power)
uint8_t send_core_config_()
optional< size_t > find_tag_uid_(const std::vector< uint8_t > &uid)
void set_tag_emulation_off()
void nci_fsm_set_state_(NCIState new_state)
set new controller state
uint8_t stop_discovery_()
uint8_t read_endpoint_data_(nfc::NfcTag &tag)
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
void dump_config() override
uint8_t set_discover_map_()
CallbackManager< void()> on_emulated_tag_scan_callback_
uint32_t last_nci_state_change_
uint8_t start_discovery_()
void process_rf_discover_oid_(nfc::NciMessage &rx)
std::shared_ptr< nfc::NdefMessage > card_emulation_message_
enum esphome::pn7150::PN7150::NfcTask EP_READ
virtual uint8_t write_nfcc(nfc::NciMessage &tx)=0
char * format_bytes_to(char *buffer, const std::vector< 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, const std::vector< uint8_t > &uid)
Format UID to buffer with '-' separator (e.g., "04-11-22-33"). Returns buffer for inline use.
uint8_t guess_tag_type(uint8_t uid_length)
@ CARD_EMU_NDEF_APP_SELECTED
@ NFCC_SET_LISTEN_MODE_ROUTING
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
const nullopt_t nullopt((nullopt_t::init()))