1 #include "automation.h" 13 static 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()) {
215 delay(NFCC_DEFAULT_TIMEOUT);
217 delay(NFCC_DEFAULT_TIMEOUT);
219 delay(NFCC_INIT_TIMEOUT);
223 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_RESET_OID,
224 {(uint8_t) reset_config});
226 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
227 ESP_LOGE(TAG,
"Error sending reset command");
228 return nfc::STATUS_FAILED;
237 (rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != 0x11) ||
238 (rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] != (uint8_t) reset_config)) {
240 return nfc::STATUS_FAILED;
243 ESP_LOGD(TAG,
"Configuration %s", rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] ?
"reset" :
"retained");
244 ESP_LOGD(TAG,
"NCI version: %s", rx.
get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] == 0x20 ?
"2.0" :
"1.0");
246 return nfc::STATUS_OK;
251 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
254 ESP_LOGE(TAG,
"Error sending initialise command");
255 return nfc::STATUS_FAILED;
260 return nfc::STATUS_FAILED;
269 ESP_LOGD(TAG,
"Manufacturer ID: 0x%02X", manf_id);
270 ESP_LOGD(TAG,
"Hardware version: 0x%02X", hw_version);
271 ESP_LOGD(TAG,
"ROM code version: 0x%02X", rom_code_version);
272 ESP_LOGD(TAG,
"FLASH major version: 0x%02X", flash_major_version);
273 ESP_LOGD(TAG,
"FLASH minor version: 0x%02X", flash_minor_version);
280 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
283 ESP_LOGE(TAG,
"Error enabling proprietary extensions");
284 return nfc::STATUS_FAILED;
287 tx.
set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
288 std::vector<uint8_t>(std::begin(PMU_CFG),
std::end(PMU_CFG)));
291 ESP_LOGE(TAG,
"Error sending PMU config");
292 return nfc::STATUS_FAILED;
299 const auto *core_config_begin = std::begin(CORE_CONFIG_SOLO);
300 const auto *core_config_end =
std::end(CORE_CONFIG_SOLO);
304 core_config_begin = std::begin(CORE_CONFIG_RW_CE);
305 core_config_end =
std::end(CORE_CONFIG_RW_CE);
310 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
311 std::vector<uint8_t>(core_config_begin, core_config_end));
314 ESP_LOGW(TAG,
"Error sending core config");
315 return nfc::STATUS_FAILED;
318 return nfc::STATUS_OK;
327 return nfc::STATUS_FAILED;
334 ESP_LOGV(TAG,
"Failed to refresh core config");
335 return nfc::STATUS_FAILED;
339 return nfc::STATUS_OK;
343 std::vector<uint8_t> discover_map = {
sizeof(RF_DISCOVER_MAP_CONFIG) / 3};
344 discover_map.insert(discover_map.end(), std::begin(RF_DISCOVER_MAP_CONFIG),
std::end(RF_DISCOVER_MAP_CONFIG));
347 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID, discover_map);
349 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
350 ESP_LOGE(TAG,
"Error sending discover map poll config");
351 return nfc::STATUS_FAILED;
353 return nfc::STATUS_OK;
359 nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
360 std::vector<uint8_t>(std::begin(RF_LISTEN_MODE_ROUTING_CONFIG),
std::end(RF_LISTEN_MODE_ROUTING_CONFIG)));
362 if (this->
transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
363 ESP_LOGE(TAG,
"Error setting listen mode routing config");
364 return nfc::STATUS_FAILED;
366 return nfc::STATUS_OK;
370 const uint8_t *rf_discovery_config = RF_DISCOVERY_CONFIG;
371 uint8_t
length =
sizeof(RF_DISCOVERY_CONFIG);
374 length =
sizeof(RF_DISCOVERY_POLL_CONFIG);
375 rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
377 length =
sizeof(RF_DISCOVERY_LISTEN_CONFIG);
378 rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
381 std::vector<uint8_t> discover_config = std::vector<uint8_t>((length * 2) + 1);
383 discover_config[0] =
length;
384 for (uint8_t i = 0; i <
length; i++) {
385 discover_config[(i * 2) + 1] = rf_discovery_config[i];
386 discover_config[(i * 2) + 2] = 0x01;
390 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID, discover_config);
393 switch (rx.get_simple_status_response()) {
396 case nfc::DISCOVERY_ALREADY_STARTED:
397 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
398 case nfc::DISCOVERY_TEAR_DOWN:
399 return nfc::STATUS_OK;
402 ESP_LOGE(TAG,
"Error starting discovery");
403 return nfc::STATUS_FAILED;
407 return nfc::STATUS_OK;
414 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {type});
426 ESP_LOGW(TAG,
"No cached tags to select");
443 nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID, endpoint_data);
446 ESP_LOGE(TAG,
"Error selecting endpoint");
456 case nfc::TAG_TYPE_MIFARE_CLASSIC:
457 ESP_LOGV(TAG,
"Reading Mifare classic");
460 case nfc::TAG_TYPE_2:
461 ESP_LOGV(TAG,
"Reading Mifare ultralight");
464 case nfc::TAG_TYPE_UNKNOWN:
466 ESP_LOGV(TAG,
"Cannot determine tag type");
469 return nfc::STATUS_FAILED;
475 case nfc::TAG_TYPE_MIFARE_CLASSIC:
478 case nfc::TAG_TYPE_2:
482 ESP_LOGE(TAG,
"Unsupported tag for cleaning");
485 return nfc::STATUS_FAILED;
491 case nfc::TAG_TYPE_MIFARE_CLASSIC:
494 case nfc::TAG_TYPE_2:
498 ESP_LOGE(TAG,
"Unsupported tag for formatting");
501 return nfc::STATUS_FAILED;
507 case nfc::TAG_TYPE_MIFARE_CLASSIC:
510 case nfc::TAG_TYPE_2:
514 ESP_LOGE(TAG,
"Unsupported tag for writing");
517 return nfc::STATUS_FAILED;
520 std::unique_ptr<nfc::NfcTag>
PN7150::build_tag_(
const uint8_t mode_tech,
const std::vector<uint8_t> &data) {
522 case (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA): {
523 uint8_t uid_length = data[2];
525 ESP_LOGE(TAG,
"UID length cannot be zero");
528 std::vector<uint8_t> uid(data.begin() + 3, data.begin() + 3 + uid_length);
529 const auto *tag_type_str =
530 nfc::guess_tag_type(uid_length) == nfc::TAG_TYPE_MIFARE_CLASSIC ? nfc::MIFARE_CLASSIC : nfc::NFC_FORUM_TYPE_2;
531 return make_unique<nfc::NfcTag>(uid, tag_type_str);
541 bool uid_match = (uid.size() == existing_tag_uid.size());
544 for (
size_t i = 0; i < uid.size(); i++) {
545 uid_match &= (uid[i] == existing_tag_uid[i]);
580 if (this->
reset_core_(
true,
true) != nfc::STATUS_OK) {
581 ESP_LOGE(TAG,
"Failed to reset NCI core");
591 ESP_LOGE(TAG,
"Failed to initialise NCI core");
601 ESP_LOGE(TAG,
"Failed to send initial config");
612 ESP_LOGE(TAG,
"Failed to set discover map");
622 ESP_LOGE(TAG,
"Failed to set listen mode routing");
644 ESP_LOGV(TAG,
"Failed to start discovery");
681 ESP_LOGVV(TAG,
"nci_fsm_set_state_(%u)", (uint8_t) new_state);
689 ESP_LOGVV(TAG,
"nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->
error_count_);
694 ESP_LOGE(TAG,
"Too many initialization failures -- check device connections");
698 ESP_LOGW(TAG,
"Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->
nci_state_error_);
707 if (this->
read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
712 case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
713 if (rx.
get_gid() == nfc::RF_GID) {
715 case nfc::RF_INTF_ACTIVATED_OID:
716 ESP_LOGVV(TAG,
"RF_INTF_ACTIVATED_OID");
720 case nfc::RF_DISCOVER_OID:
721 ESP_LOGVV(TAG,
"RF_DISCOVER_OID");
725 case nfc::RF_DEACTIVATE_OID:
726 ESP_LOGVV(TAG,
"RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.
get_message()[3], rx.
get_message()[4]);
731 ESP_LOGV(TAG,
"Unimplemented RF OID received: 0x%02X", rx.
get_oid());
733 }
else if (rx.
get_gid() == nfc::NCI_CORE_GID) {
735 case nfc::NCI_CORE_GENERIC_ERROR_OID:
736 ESP_LOGV(TAG,
"NCI_CORE_GENERIC_ERROR_OID:");
738 case nfc::DISCOVERY_ALREADY_STARTED:
739 ESP_LOGV(TAG,
" DISCOVERY_ALREADY_STARTED");
742 case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
744 ESP_LOGV(TAG,
" DISCOVERY_TARGET_ACTIVATION_FAILED");
756 case nfc::DISCOVERY_TEAR_DOWN:
757 ESP_LOGV(TAG,
" DISCOVERY_TEAR_DOWN");
767 ESP_LOGV(TAG,
"Unimplemented NCI Core OID received: 0x%02X", rx.
get_oid());
774 case nfc::NCI_PKT_MT_CTRL_RESPONSE:
775 ESP_LOGV(TAG,
"Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.
get_gid(), rx.
get_oid(),
779 case nfc::NCI_PKT_MT_CTRL_COMMAND:
783 case nfc::NCI_PKT_MT_DATA:
794 uint8_t discovery_id = rx.
get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
795 uint8_t
interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
796 uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
797 uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
798 uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
800 ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
801 interface, protocol, mode_tech, max_size);
803 if (mode_tech & nfc::MODE_LISTEN_MASK) {
804 ESP_LOGVV(TAG,
"Tag activated in listen mode");
813 if (incoming_tag ==
nullptr) {
814 ESP_LOGE(TAG,
"Could not build tag");
817 if (tag_loc.has_value()) {
821 ESP_LOGVV(TAG,
"Tag cache updated");
826 ESP_LOGVV(TAG,
"Tag added to cache");
831 switch (this->next_task_) {
833 ESP_LOGD(TAG,
" Tag cleaning...");
834 if (this->
clean_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
835 ESP_LOGE(TAG,
" Tag cleaning incomplete");
837 ESP_LOGD(TAG,
" Tag cleaned!");
841 ESP_LOGD(TAG,
" Tag formatting...");
842 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
843 ESP_LOGE(TAG,
"Error formatting tag as NDEF");
845 ESP_LOGD(TAG,
" Tag formatted!");
850 ESP_LOGD(TAG,
" Tag writing...");
851 ESP_LOGD(TAG,
" Tag formatting...");
852 if (this->
format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
853 ESP_LOGE(TAG,
" Tag could not be formatted for writing");
855 ESP_LOGD(TAG,
" Writing NDEF data");
858 ESP_LOGE(TAG,
" Failed to write message to tag");
860 ESP_LOGD(TAG,
" Finished writing NDEF data");
869 if (!working_endpoint.trig_called) {
870 ESP_LOGI(TAG,
"Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(),
873 ESP_LOGW(TAG,
" Unable to read NDEF record(s)");
874 }
else if (working_endpoint.tag->has_ndef_message()) {
875 const auto message = working_endpoint.tag->get_ndef_message();
876 const auto records = message->get_records();
877 ESP_LOGD(TAG,
" NDEF record(s):");
878 for (
const auto &record : records) {
879 ESP_LOGD(TAG,
" %s - %s", record->get_type().c_str(), record->get_payload().c_str());
882 ESP_LOGW(TAG,
" No NDEF records found");
885 trigger->process(working_endpoint.tag);
888 listener->tag_on(*working_endpoint.tag);
890 working_endpoint.trig_called =
true;
894 if (working_endpoint.tag->get_tag_type() == nfc::MIFARE_CLASSIC) {
898 if (this->next_task_ !=
EP_READ) {
910 if (incoming_tag ==
nullptr) {
911 ESP_LOGE(TAG,
"Could not build tag!");
914 if (tag_loc.has_value()) {
918 ESP_LOGVV(TAG,
"Tag found & updated");
922 millis(), std::move(incoming_tag),
false});
923 ESP_LOGVV(TAG,
"Tag saved");
927 if (rx.
get_message().back() != nfc::RF_DISCOVER_NTF_NT_MORE) {
937 case nfc::DEACTIVATION_TYPE_DISCOVERY:
941 case nfc::DEACTIVATION_TYPE_IDLE:
945 case nfc::DEACTIVATION_TYPE_SLEEP:
946 case nfc::DEACTIVATION_TYPE_SLEEP_AF:
964 std::vector<uint8_t> ndef_response;
967 uint16_t ndef_response_size = ndef_response.size();
968 if (!ndef_response_size) {
972 std::vector<uint8_t> tx_msg = {nfc::NCI_PKT_MT_DATA, uint8_t((ndef_response_size & 0xFF00) >> 8),
973 uint8_t(ndef_response_size & 0x00FF)};
974 tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end());
977 if (this->
transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT,
false) != nfc::STATUS_OK) {
978 ESP_LOGE(TAG,
"Sending reply for card emulation failed");
984 ESP_LOGE(TAG,
"No NDEF message is set; tag emulation not possible");
985 ndef_response.clear();
989 if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_APP_SELECT))) {
991 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_APP_SELECTED");
993 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
994 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_CC_SELECT))) {
997 ESP_LOGVV(TAG,
"CARD_EMU_CC_SELECTED");
999 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1001 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_NDEF_SELECT))) {
1003 ESP_LOGVV(TAG,
"CARD_EMU_NDEF_SELECTED");
1005 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1006 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1007 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_READ),
1008 std::begin(CARD_EMU_T4T_READ))) {
1012 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
1013 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1014 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1016 if (length <= (
sizeof(CARD_EMU_T4T_CC) + offset + 2)) {
1017 ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_CC) + offset,
1018 std::begin(CARD_EMU_T4T_CC) + offset +
length);
1019 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1023 ESP_LOGVV(TAG,
"CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
1025 uint16_t ndef_msg_size = ndef_message.size();
1026 uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
1027 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1029 ESP_LOGVV(TAG,
"Encoded NDEF message: %s",
nfc::format_bytes(ndef_message).c_str());
1031 if (length <= (ndef_msg_size + offset + 2)) {
1033 ndef_response.resize(2);
1034 ndef_response[0] = (ndef_msg_size & 0xFF00) >> 8;
1035 ndef_response[1] = (ndef_msg_size & 0x00FF);
1037 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 2);
1039 }
else if (offset == 1) {
1040 ndef_response.resize(1);
1041 ndef_response[0] = (ndef_msg_size & 0x00FF);
1043 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 1);
1046 ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() +
length);
1049 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1051 if ((offset + length) >= (ndef_msg_size + 2)) {
1052 ESP_LOGD(TAG,
"NDEF message sent");
1057 }
else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
1058 response.begin() + nfc::NCI_PKT_HEADER_SIZE +
sizeof(CARD_EMU_T4T_WRITE),
1059 std::begin(CARD_EMU_T4T_WRITE))) {
1062 ESP_LOGVV(TAG,
"CARD_EMU_T4T_WRITE");
1063 uint8_t
length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
1064 std::vector<uint8_t> ndef_msg_written;
1066 ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5,
1067 response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 +
length);
1068 ESP_LOGD(TAG,
"Received %u-byte NDEF message: %s", length,
nfc::format_bytes(ndef_msg_written).c_str());
1069 ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK),
std::end(CARD_EMU_T4T_OK));
1075 const bool expect_notification) {
1076 uint8_t retries = NFCC_MAX_COMM_FAILS;
1080 if (this->
write_nfcc(tx) != nfc::STATUS_OK) {
1081 ESP_LOGE(TAG,
"Error sending message");
1082 return nfc::STATUS_FAILED;
1086 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1087 ESP_LOGW(TAG,
"Error receiving message");
1089 ESP_LOGE(TAG,
" ...giving up");
1090 return nfc::STATUS_FAILED;
1102 return nfc::STATUS_FAILED;
1114 return nfc::STATUS_FAILED;
1117 if (expect_notification) {
1119 if (this->
read_nfcc(rx, timeout) != nfc::STATUS_OK) {
1120 ESP_LOGE(TAG,
"Error receiving data from endpoint");
1121 return nfc::STATUS_FAILED;
1126 return nfc::STATUS_OK;
1131 auto start_time =
millis();
1133 while (
millis() - start_time < timeout) {
1135 return nfc::STATUS_OK;
1138 ESP_LOGW(TAG,
"Timed out waiting for IRQ state");
1139 return nfc::STATUS_FAILED;
virtual void digital_write(bool value)=0
uint32_t last_nci_state_change_
value_type const & value() const
std::unique_ptr< nfc::NfcTag > build_tag_(uint8_t mode_tech, const std::vector< uint8_t > &data)
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout)=0
uint8_t reset_core_(bool reset_config, bool power)
uint8_t write_mifare_ultralight_tag_(std::vector< uint8_t > &uid, const std::shared_ptr< nfc::NdefMessage > &message)
CallbackManager< void()> on_finished_write_callback_
void erase_tag_(uint8_t tag_index)
void set_tag_emulation_message(std::shared_ptr< nfc::NdefMessage > message)
std::vector< NfcTagListener * > tag_listeners_
void process_rf_deactivate_oid_(nfc::NciMessage &rx)
uint8_t read_endpoint_data_(nfc::NfcTag &tag)
void process_rf_discover_oid_(nfc::NciMessage &rx)
CallbackManager< void()> on_emulated_tag_scan_callback_
void dump_config() override
uint8_t get_message_type() const
void process_message_()
parse & process incoming messages from the NFCC
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 format_mifare_classic_ndef_()
std::string format_uid(std::vector< uint8_t > &uid)
uint8_t set_discover_map_()
bool message_type_is(uint8_t message_type) const
std::vector< nfc::NfcOnTagTrigger * > triggers_ontag_
void process_rf_intf_activated_oid_(nfc::NciMessage &rx)
uint8_t set_test_mode(TestMode test_mode, const std::vector< uint8_t > &data, std::vector< uint8_t > &result)
uint8_t send_core_config_()
uint8_t transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool expect_notification=true)
void process_data_message_(nfc::NciMessage &rx)
uint32_t IRAM_ATTR HOT millis()
bool config_refresh_pending_
uint8_t write_endpoint_(std::vector< uint8_t > &uid, std::shared_ptr< nfc::NdefMessage > &message)
uint8_t format_mifare_classic_mifare_()
void nci_fsm_transition_()
advance controller state as required
uint8_t get_message_byte(uint8_t offset) const
bool core_config_is_solo_
void card_emu_t4t_get_response_(std::vector< uint8_t > &response, std::vector< uint8_t > &ndef_response)
const nullopt_t nullopt((nullopt_t::init()))
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag)
void set_tag_emulation_off()
std::vector< uint8_t > & get_message()
enum esphome::pn7150::PN7150::NfcTask EP_READ
uint8_t selecting_endpoint_
optional< size_t > find_tag_uid_(const std::vector< uint8_t > &uid)
uint8_t guess_tag_type(uint8_t uid_length)
std::shared_ptr< nfc::NdefMessage > next_task_message_to_write_
NCIState nci_state_error_
std::vector< nfc::NfcOnTagTrigger * > triggers_ontagremoved_
uint8_t start_discovery_()
void set_tag_write_message(std::shared_ptr< nfc::NdefMessage > message)
std::vector< uint8_t > & get_uid()
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
bool gid_is(uint8_t gid) const
virtual bool digital_read()=0
uint8_t deactivate_(uint8_t type, uint16_t timeout=NFCC_DEFAULT_TIMEOUT)
void set_tag_emulation_on()
std::vector< DiscoveredEndpoint > discovered_endpoint_
bool simple_status_response_is(uint8_t response) const
uint8_t send_init_config_()
virtual void mark_failed()
Mark this component as failed.
uint8_t read_mifare_classic_tag_(nfc::NfcTag &tag)
void set_message(uint8_t message_type, const std::vector< uint8_t > &payload)
bool oid_is(uint8_t oid) const
bool message_length_is(uint8_t message_length, bool recompute=false)
Implementation of SPI Controller mode.
virtual uint8_t write_nfcc(nfc::NciMessage &tx)=0
uint8_t write_mifare_classic_tag_(const std::shared_ptr< nfc::NdefMessage > &message)
uint8_t refresh_core_config_()
uint8_t set_listen_mode_routing_()
uint8_t clean_endpoint_(std::vector< uint8_t > &uid)
uint8_t format_endpoint_(std::vector< uint8_t > &uid)
uint8_t get_simple_status_response() const
void nci_fsm_set_state_(NCIState new_state)
set new controller state
uint8_t stop_discovery_()
std::shared_ptr< nfc::NdefMessage > card_emulation_message_
std::string format_bytes(std::vector< uint8_t > &bytes)
uint8_t halt_mifare_classic_tag_()
CardEmulationState ce_state_
uint8_t clean_mifare_ultralight_()
void IRAM_ATTR HOT delay(uint32_t ms)