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;
246 ESP_LOGD(TAG,
"Configuration %s, NCI version: %s",
247 rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] ?
"reset" :
"retained",
248 rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] == 0x20 ?
"2.0" :
"1.0");
250 return nfc::STATUS_OK;
255 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
258 ESP_LOGE(TAG,
"Error sending initialise command");
259 return nfc::STATUS_FAILED;
263 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
265 return nfc::STATUS_FAILED;
275 "PN7150 chip info:\n"
276 " Manufacturer ID: 0x%02X\n"
277 " Hardware version: 0x%02X\n"
278 " ROM code version: 0x%02X\n"
279 " FLASH major version: 0x%02X\n"
280 " FLASH minor version: 0x%02X",
281 manf_id, hw_version, rom_code_version, flash_major_version, flash_minor_version);
288 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
291 ESP_LOGE(TAG,
"Error enabling proprietary extensions");
292 return nfc::STATUS_FAILED;
295 tx.
set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
296 std::vector<uint8_t>(std::begin(PMU_CFG), std::end(PMU_CFG)));
299 ESP_LOGE(TAG,
"Error sending PMU config");
300 return nfc::STATUS_FAILED;
307 const auto *core_config_begin = std::begin(CORE_CONFIG_SOLO);
308 const auto *core_config_end = std::end(CORE_CONFIG_SOLO);
312 core_config_begin = std::begin(CORE_CONFIG_RW_CE);
313 core_config_end = std::end(CORE_CONFIG_RW_CE);
318 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
319 std::vector<uint8_t>(core_config_begin, core_config_end));
322 ESP_LOGW(TAG,
"Error sending core config");
323 return nfc::STATUS_FAILED;
326 return nfc::STATUS_OK;
335 return nfc::STATUS_FAILED;
342 ESP_LOGV(TAG,
"Failed to refresh core config");
343 return nfc::STATUS_FAILED;
347 return nfc::STATUS_OK;
351 std::vector<uint8_t> discover_map = {
sizeof(RF_DISCOVER_MAP_CONFIG) / 3};
352 discover_map.insert(discover_map.end(), std::begin(RF_DISCOVER_MAP_CONFIG), std::end(RF_DISCOVER_MAP_CONFIG));
355 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID, discover_map);
357 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
358 ESP_LOGE(TAG,
"Error sending discover map poll config");
359 return nfc::STATUS_FAILED;
361 return nfc::STATUS_OK;
367 nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
368 std::vector<uint8_t>(std::begin(RF_LISTEN_MODE_ROUTING_CONFIG), std::end(RF_LISTEN_MODE_ROUTING_CONFIG)));
370 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
371 ESP_LOGE(TAG,
"Error setting listen mode routing config");
372 return nfc::STATUS_FAILED;
374 return nfc::STATUS_OK;
378 const uint8_t *rf_discovery_config = RF_DISCOVERY_CONFIG;
379 uint8_t
length =
sizeof(RF_DISCOVERY_CONFIG);
382 length =
sizeof(RF_DISCOVERY_POLL_CONFIG);
383 rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
385 length =
sizeof(RF_DISCOVERY_LISTEN_CONFIG);
386 rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
389 std::vector<uint8_t> discover_config = std::vector<uint8_t>((
length * 2) + 1);
391 discover_config[0] =
length;
392 for (uint8_t i = 0; i <
length; i++) {
393 discover_config[(i * 2) + 1] = rf_discovery_config[i];
394 discover_config[(i * 2) + 2] = 0x01;
398 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID, discover_config);
404 case nfc::DISCOVERY_ALREADY_STARTED:
405 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
406 case nfc::DISCOVERY_TEAR_DOWN:
407 return nfc::STATUS_OK;
410 ESP_LOGE(TAG,
"Error starting discovery");
411 return nfc::STATUS_FAILED;
415 return nfc::STATUS_OK;
422 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {
type});
434 ESP_LOGW(TAG,
"No cached tags to select");
451 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID, endpoint_data);
454 ESP_LOGE(TAG,
"Error selecting endpoint");
464 case nfc::TAG_TYPE_MIFARE_CLASSIC:
465 ESP_LOGV(TAG,
"Reading Mifare classic");
468 case nfc::TAG_TYPE_2:
469 ESP_LOGV(TAG,
"Reading Mifare ultralight");
472 case nfc::TAG_TYPE_UNKNOWN:
474 ESP_LOGV(TAG,
"Cannot determine tag type");
477 return nfc::STATUS_FAILED;
483 case nfc::TAG_TYPE_MIFARE_CLASSIC:
486 case nfc::TAG_TYPE_2:
490 ESP_LOGE(TAG,
"Unsupported tag for cleaning");
493 return nfc::STATUS_FAILED;
499 case nfc::TAG_TYPE_MIFARE_CLASSIC:
502 case nfc::TAG_TYPE_2:
506 ESP_LOGE(TAG,
"Unsupported tag for formatting");
509 return nfc::STATUS_FAILED;
515 case nfc::TAG_TYPE_MIFARE_CLASSIC:
518 case nfc::TAG_TYPE_2:
522 ESP_LOGE(TAG,
"Unsupported tag for writing");
525 return nfc::STATUS_FAILED;
528std::unique_ptr<nfc::NfcTag>
PN7150::build_tag_(
const uint8_t mode_tech,
const std::vector<uint8_t> &data) {
530 case (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA): {
531 uint8_t uid_length = data[2];
533 ESP_LOGE(TAG,
"UID length cannot be zero");
536 nfc::NfcTagUid uid(data.begin() + 3, data.begin() + 3 + uid_length);
537 const auto *tag_type_str =
538 nfc::guess_tag_type(uid_length) == nfc::TAG_TYPE_MIFARE_CLASSIC ? nfc::MIFARE_CLASSIC : nfc::NFC_FORUM_TYPE_2;
539 return make_unique<nfc::NfcTag>(uid, tag_type_str);
549 bool uid_match = (uid.
size() == existing_tag_uid.size());
552 for (
size_t i = 0; i < uid.
size(); i++) {
553 uid_match &= (uid[i] == existing_tag_uid[i]);
580 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
589 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
590 ESP_LOGE(TAG,
"Failed to reset NCI core");
600 ESP_LOGE(TAG,
"Failed to initialise NCI core");
610 ESP_LOGE(TAG,
"Failed to send initial config");
621 ESP_LOGE(TAG,
"Failed to set discover map");
631 ESP_LOGE(TAG,
"Failed to set listen mode routing");
653 ESP_LOGV(TAG,
"Failed to start discovery");
690 ESP_LOGVV(TAG,
"nci_fsm_set_state_(%u)", (uint8_t) new_state);
698 ESP_LOGVV(TAG,
"nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->
error_count_);
703 ESP_LOGE(TAG,
"Too many initialization failures -- check device connections");
707 ESP_LOGW(TAG,
"Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->
nci_state_error_);
716 if (this->
read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
721 case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
722 if (rx.
get_gid() == nfc::RF_GID) {
724 case nfc::RF_INTF_ACTIVATED_OID:
725 ESP_LOGVV(TAG,
"RF_INTF_ACTIVATED_OID");
729 case nfc::RF_DISCOVER_OID:
730 ESP_LOGVV(TAG,
"RF_DISCOVER_OID");
734 case nfc::RF_DEACTIVATE_OID:
735 ESP_LOGVV(TAG,
"RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.
get_message()[3], rx.
get_message()[4]);
740 ESP_LOGV(TAG,
"Unimplemented RF OID received: 0x%02X", rx.
get_oid());
742 }
else if (rx.
get_gid() == nfc::NCI_CORE_GID) {
744 case nfc::NCI_CORE_GENERIC_ERROR_OID:
745 ESP_LOGV(TAG,
"NCI_CORE_GENERIC_ERROR_OID:");
747 case nfc::DISCOVERY_ALREADY_STARTED:
748 ESP_LOGV(TAG,
" DISCOVERY_ALREADY_STARTED");
751 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
753 ESP_LOGV(TAG,
" DISCOVERY_TARGET_ACTIVATION_FAILED");
765 case nfc::DISCOVERY_TEAR_DOWN:
766 ESP_LOGV(TAG,
" DISCOVERY_TEAR_DOWN");
776 ESP_LOGV(TAG,
"Unimplemented NCI Core OID received: 0x%02X", rx.
get_oid());
779 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
784 case nfc::NCI_PKT_MT_CTRL_RESPONSE: {
785 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
786 ESP_LOGV(TAG,
"Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.
get_gid(), rx.
get_oid(),
791 case nfc::NCI_PKT_MT_CTRL_COMMAND: {
792 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
797 case nfc::NCI_PKT_MT_DATA:
802 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
810 uint8_t discovery_id = rx.
get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
811 uint8_t
interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
812 uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
813 uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
814 uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
816 ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
817 interface, protocol, mode_tech, max_size);
819 if (mode_tech & nfc::MODE_LISTEN_MASK) {
820 ESP_LOGVV(TAG,
"Tag activated in listen mode");
821 this->nci_fsm_set_state_(NCIState::RFST_LISTEN_ACTIVE);
829 if (incoming_tag ==
nullptr) {
830 ESP_LOGE(TAG,
"Could not build tag");
833 if (tag_loc.has_value()) {
837 ESP_LOGVV(TAG,
"Tag cache updated");
842 ESP_LOGVV(TAG,
"Tag added to cache");
847 switch (this->next_task_) {
849 ESP_LOGD(TAG,
" Tag cleaning");
850 if (this->
clean_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
851 ESP_LOGE(TAG,
" Tag cleaning incomplete");
853 ESP_LOGD(TAG,
" Tag cleaned!");
857 ESP_LOGD(TAG,
" Tag formatting");
858 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
859 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
861 ESP_LOGD(TAG,
" Tag formatted!");
866 ESP_LOGD(TAG,
" Tag writing\n"
868 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
869 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
871 ESP_LOGD(TAG,
" Writing NDEF data");
872 if (this->
write_endpoint_(working_endpoint.tag->get_uid(), this->next_task_message_to_write_) !=
874 ESP_LOGE(TAG,
" Failed to write message to tag");
876 ESP_LOGD(TAG,
" Finished writing NDEF data");
885 if (!working_endpoint.trig_called) {
886 char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE];
887 ESP_LOGI(TAG,
"Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(),
890 ESP_LOGW(TAG,
" Unable to read NDEF record(s)");
891 }
else if (working_endpoint.tag->has_ndef_message()) {
892 const auto message = working_endpoint.tag->get_ndef_message();
893 const auto records =
message->get_records();
894 ESP_LOGD(TAG,
" NDEF record(s):");
895 for (
const auto &record : records) {
896 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
899 ESP_LOGW(TAG,
" No NDEF records found");
902 trigger->process(working_endpoint.tag);
905 listener->tag_on(*working_endpoint.tag);
907 working_endpoint.trig_called =
true;
911 if (working_endpoint.tag->get_tag_type() == nfc::MIFARE_CLASSIC) {
915 if (this->next_task_ !=
EP_READ) {
927 if (incoming_tag ==
nullptr) {
928 ESP_LOGE(TAG,
"Could not build tag!");
931 if (tag_loc.has_value()) {
935 ESP_LOGVV(TAG,
"Tag found & updated");
939 millis(), std::move(incoming_tag),
false});
940 ESP_LOGVV(TAG,
"Tag saved");
944 if (rx.
get_message().back() != nfc::RF_DISCOVER_NTF_NT_MORE) {
954 case nfc::DEACTIVATION_TYPE_DISCOVERY:
958 case nfc::DEACTIVATION_TYPE_IDLE:
962 case nfc::DEACTIVATION_TYPE_SLEEP:
963 case nfc::DEACTIVATION_TYPE_SLEEP_AF:
979 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
982 std::vector<uint8_t> ndef_response;
985 uint16_t ndef_response_size = ndef_response.size();
986 if (!ndef_response_size) {
990 std::vector<uint8_t> tx_msg = {nfc::NCI_PKT_MT_DATA, uint8_t((ndef_response_size & 0xFF00) >> 8),
991 uint8_t(ndef_response_size & 0x00FF)};
992 tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end());
995 if (this->
transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT,
false) != nfc::STATUS_OK) {
996 ESP_LOGE(TAG,
"Sending reply for card emulation failed");
1002 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation not possible");
1003 ndef_response.clear();
1007 if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_APP_SELECT))) {
1009 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_APP_SELECTED");
1011 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1012 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_CC_SELECT))) {
1015 ESP_LOGVV(TAG,
"CARD_EMU_CC_SELECTED");
1017 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1019 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_NDEF_SELECT))) {
1021 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_SELECTED");
1023 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1024 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1025 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_READ),
1026 std::begin(CARD_EMU_T4T_READ))) {
1030 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
1031 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1032 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1034 if (
length <= (
sizeof(CARD_EMU_T4T_CC) + offset + 2)) {
1035 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_CC) + offset,
1036 std::begin(CARD_EMU_T4T_CC) + offset +
length);
1037 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1041 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
1043 uint16_t ndef_msg_size = ndef_message.size();
1044 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1045 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1047 char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1050 if (
length <= (ndef_msg_size + offset + 2)) {
1052 ndef_response.resize(2);
1053 ndef_response[0] = (ndef_msg_size & 0xFF00) >> 8;
1054 ndef_response[1] = (ndef_msg_size & 0x00FF);
1056 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length - 2);
1058 }
else if (offset == 1) {
1059 ndef_response.resize(1);
1060 ndef_response[0] = (ndef_msg_size & 0x00FF);
1062 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length - 1);
1065 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length);
1068 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1070 if ((offset +
length) >= (ndef_msg_size + 2)) {
1071 ESP_LOGD(TAG,
"NDEF message sent");
1076 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1077 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_WRITE),
1078 std::begin(CARD_EMU_T4T_WRITE))) {
1081 ESP_LOGVV(TAG,
"CARD_EMU_T4T_WRITE");
1082 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1083 std::vector<uint8_t> ndef_msg_written;
1085 ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5,
1086 response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 +
length);
1087 char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1089 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
1095 const bool expect_notification) {
1096 uint8_t retries = NFCC_MAX_COMM_FAILS;
1097 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
1101 if (this->
write_nfcc(tx) != nfc::STATUS_OK) {
1102 ESP_LOGE(TAG,
"Error sending message");
1103 return nfc::STATUS_FAILED;
1107 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1108 ESP_LOGW(TAG,
"Error receiving message");
1110 ESP_LOGE(TAG,
" ...giving up");
1111 return nfc::STATUS_FAILED;
1123 return nfc::STATUS_FAILED;
1135 return nfc::STATUS_FAILED;
1138 if (expect_notification) {
1140 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1141 ESP_LOGE(TAG,
"Error receiving data from endpoint");
1142 return nfc::STATUS_FAILED;
1147 return nfc::STATUS_OK;
1152 auto start_time =
millis();
1154 while (
millis() - start_time < timeout) {
1156 return nfc::STATUS_OK;
1159 ESP_LOGW(TAG,
"Timed out waiting for IRQ state");
1160 return nfc::STATUS_FAILED;
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< 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_
uint8_t format_endpoint_(nfc::NfcTagUid &uid)
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
optional< size_t > find_tag_uid_(const nfc::NfcTagUid &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 write_endpoint_(nfc::NfcTagUid &uid, 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_
void process_rf_deactivate_oid_(nfc::NciMessage &rx)
uint8_t refresh_core_config_()
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)
uint8_t clean_endpoint_(nfc::NfcTagUid &uid)
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_()
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_
uint8_t write_mifare_ultralight_tag_(nfc::NfcTagUid &uid, const std::shared_ptr< nfc::NdefMessage > &message)
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, 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.
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 HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
const nullopt_t nullopt((nullopt_t::init()))