From 7f0e4eaa84ada41d72af40b5bbf0564c60a945fb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 12 Jan 2026 07:38:39 -1000 Subject: [PATCH] [nfc] Use stack-based hex formatting in pn7150/pn7160 components (#13163) --- esphome/components/nfc/automation.cpp | 6 +- .../nfc/binary_sensor/nfc_binary_sensor.cpp | 4 +- esphome/components/nfc/nfc.cpp | 11 +++ esphome/components/nfc/nfc.h | 14 ++++ esphome/components/pn532/pn532.cpp | 3 +- .../components/pn532/pn532_mifare_classic.cpp | 3 +- .../pn532/pn532_mifare_ultralight.cpp | 3 +- esphome/components/pn7150/pn7150.cpp | 63 +++++++++++------ .../pn7150/pn7150_mifare_classic.cpp | 22 +++--- .../pn7150/pn7150_mifare_ultralight.cpp | 3 +- esphome/components/pn7160/pn7160.cpp | 70 ++++++++++++------- .../pn7160/pn7160_mifare_classic.cpp | 22 +++--- .../pn7160/pn7160_mifare_ultralight.cpp | 3 +- 13 files changed, 154 insertions(+), 73 deletions(-) diff --git a/esphome/components/nfc/automation.cpp b/esphome/components/nfc/automation.cpp index ff00340df0..e2956e4c12 100644 --- a/esphome/components/nfc/automation.cpp +++ b/esphome/components/nfc/automation.cpp @@ -1,9 +1,13 @@ #include "automation.h" +#include "nfc.h" namespace esphome { namespace nfc { -void NfcOnTagTrigger::process(const std::unique_ptr &tag) { this->trigger(format_uid(tag->get_uid()), *tag); } +void NfcOnTagTrigger::process(const std::unique_ptr &tag) { + char uid_buf[FORMAT_UID_BUFFER_SIZE]; + this->trigger(std::string(format_uid_to(uid_buf, tag->get_uid())), *tag); +} } // namespace nfc } // namespace esphome diff --git a/esphome/components/nfc/binary_sensor/nfc_binary_sensor.cpp b/esphome/components/nfc/binary_sensor/nfc_binary_sensor.cpp index bc19fa7213..b62b243cc6 100644 --- a/esphome/components/nfc/binary_sensor/nfc_binary_sensor.cpp +++ b/esphome/components/nfc/binary_sensor/nfc_binary_sensor.cpp @@ -1,4 +1,5 @@ #include "nfc_binary_sensor.h" +#include "../nfc.h" #include "../nfc_helpers.h" #include "esphome/core/log.h" @@ -24,7 +25,8 @@ void NfcTagBinarySensor::dump_config() { return; } if (!this->uid_.empty()) { - ESP_LOGCONFIG(TAG, " Tag UID: %s", format_bytes(this->uid_).c_str()); + char uid_buf[FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGCONFIG(TAG, " Tag UID: %s", format_bytes_to(uid_buf, this->uid_)); } } diff --git a/esphome/components/nfc/nfc.cpp b/esphome/components/nfc/nfc.cpp index d3a2481693..82e86b936a 100644 --- a/esphome/components/nfc/nfc.cpp +++ b/esphome/components/nfc/nfc.cpp @@ -8,9 +8,20 @@ namespace nfc { static const char *const TAG = "nfc"; +char *format_uid_to(char *buffer, const std::vector &uid) { + return format_hex_pretty_to(buffer, FORMAT_UID_BUFFER_SIZE, uid.data(), uid.size(), '-'); +} + +char *format_bytes_to(char *buffer, const std::vector &bytes) { + return format_hex_pretty_to(buffer, FORMAT_BYTES_BUFFER_SIZE, bytes.data(), bytes.size(), ' '); +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" std::string format_uid(const std::vector &uid) { return format_hex_pretty(uid, '-', false); } std::string format_bytes(const std::vector &bytes) { return format_hex_pretty(bytes, ' ', false); } +#pragma GCC diagnostic pop uint8_t guess_tag_type(uint8_t uid_length) { if (uid_length == 4) { diff --git a/esphome/components/nfc/nfc.h b/esphome/components/nfc/nfc.h index 9879cfdb03..6568c60a85 100644 --- a/esphome/components/nfc/nfc.h +++ b/esphome/components/nfc/nfc.h @@ -53,7 +53,21 @@ static const uint8_t DEFAULT_KEY[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; static const uint8_t NDEF_KEY[6] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7}; static const uint8_t MAD_KEY[6] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5}; +/// Max UID size is 10 bytes, formatted as "XX-XX-XX-XX-XX-XX-XX-XX-XX-XX\0" = 30 chars +static constexpr size_t FORMAT_UID_BUFFER_SIZE = 30; +/// Format UID to buffer with '-' separator (e.g., "04-11-22-33"). Returns buffer for inline use. +char *format_uid_to(char *buffer, const std::vector &uid); + +/// Buffer size for format_bytes_to (64 bytes max = 192 chars with space separator) +static constexpr size_t FORMAT_BYTES_BUFFER_SIZE = 192; +/// Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use. +char *format_bytes_to(char *buffer, const std::vector &bytes); + +// Remove before 2026.6.0 +ESPDEPRECATED("Use format_uid_to() with stack buffer instead. Removed in 2026.6.0", "2025.12.0") std::string format_uid(const std::vector &uid); +// Remove before 2026.6.0 +ESPDEPRECATED("Use format_bytes_to() with stack buffer instead. Removed in 2026.6.0", "2025.12.0") std::string format_bytes(const std::vector &bytes); uint8_t guess_tag_type(uint8_t uid_length); diff --git a/esphome/components/pn532/pn532.cpp b/esphome/components/pn532/pn532.cpp index d5e892a576..8f0c5581d4 100644 --- a/esphome/components/pn532/pn532.cpp +++ b/esphome/components/pn532/pn532.cpp @@ -197,7 +197,8 @@ void PN532::loop() { trigger->process(tag); if (report) { - ESP_LOGD(TAG, "Found new tag '%s'", nfc::format_uid(nfcid).c_str()); + char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE]; + ESP_LOGD(TAG, "Found new tag '%s'", nfc::format_uid_to(uid_buf, nfcid)); if (tag->has_ndef_message()) { const auto &message = tag->get_ndef_message(); const auto &records = message->get_records(); diff --git a/esphome/components/pn532/pn532_mifare_classic.cpp b/esphome/components/pn532/pn532_mifare_classic.cpp index 943f8c5519..28ab22e160 100644 --- a/esphome/components/pn532/pn532_mifare_classic.cpp +++ b/esphome/components/pn532/pn532_mifare_classic.cpp @@ -77,7 +77,8 @@ bool PN532::read_mifare_classic_block_(uint8_t block_num, std::vector & } data.erase(data.begin()); - ESP_LOGVV(TAG, " Block %d: %s", block_num, nfc::format_bytes(data).c_str()); + char data_buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, " Block %d: %s", block_num, nfc::format_bytes_to(data_buf, data)); return true; } diff --git a/esphome/components/pn532/pn532_mifare_ultralight.cpp b/esphome/components/pn532/pn532_mifare_ultralight.cpp index f823829a6c..0221ba31c5 100644 --- a/esphome/components/pn532/pn532_mifare_ultralight.cpp +++ b/esphome/components/pn532/pn532_mifare_ultralight.cpp @@ -71,7 +71,8 @@ bool PN532::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes } } - ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes(data).c_str()); + char data_buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes_to(data_buf, data)); return true; } diff --git a/esphome/components/pn7150/pn7150.cpp b/esphome/components/pn7150/pn7150.cpp index f6ddcb0767..e1ba3761d4 100644 --- a/esphome/components/pn7150/pn7150.cpp +++ b/esphome/components/pn7150/pn7150.cpp @@ -203,7 +203,8 @@ uint8_t PN7150::set_test_mode(const TestMode test_mode, const std::vectortag_listeners_) { listener->tag_off(*this->discovered_endpoint_[tag_index].tag); } - ESP_LOGI(TAG, "Tag %s removed", nfc::format_uid(this->discovered_endpoint_[tag_index].tag->get_uid()).c_str()); + char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE]; + ESP_LOGI(TAG, "Tag %s removed", nfc::format_uid_to(uid_buf, this->discovered_endpoint_[tag_index].tag->get_uid())); this->discovered_endpoint_.erase(this->discovered_endpoint_.begin() + tag_index); } } @@ -772,26 +777,33 @@ void PN7150::process_message_() { ESP_LOGV(TAG, "Unimplemented NCI Core OID received: 0x%02X", rx.get_oid()); } } else { - ESP_LOGV(TAG, "Unimplemented notification: %s", nfc::format_bytes(rx.get_message()).c_str()); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGV(TAG, "Unimplemented notification: %s", nfc::format_bytes_to(buf, rx.get_message())); } break; - case nfc::NCI_PKT_MT_CTRL_RESPONSE: + case nfc::NCI_PKT_MT_CTRL_RESPONSE: { + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; ESP_LOGV(TAG, "Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.get_gid(), rx.get_oid(), - nfc::format_bytes(rx.get_message()).c_str()); + nfc::format_bytes_to(buf, rx.get_message())); break; + } - case nfc::NCI_PKT_MT_CTRL_COMMAND: - ESP_LOGV(TAG, "Unimplemented command: %s", nfc::format_bytes(rx.get_message()).c_str()); + case nfc::NCI_PKT_MT_CTRL_COMMAND: { + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGV(TAG, "Unimplemented command: %s", nfc::format_bytes_to(buf, rx.get_message())); break; + } case nfc::NCI_PKT_MT_DATA: this->process_data_message_(rx); break; - default: - ESP_LOGV(TAG, "Unimplemented message type: %s", nfc::format_bytes(rx.get_message()).c_str()); + default: { + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGV(TAG, "Unimplemented message type: %s", nfc::format_bytes_to(buf, rx.get_message())); break; + } } } @@ -872,8 +884,9 @@ void PN7150::process_rf_intf_activated_oid_(nfc::NciMessage &rx) { // an endpoi case EP_READ: default: if (!working_endpoint.trig_called) { + char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE]; ESP_LOGI(TAG, "Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(), - nfc::format_uid(working_endpoint.tag->get_uid()).c_str()); + nfc::format_uid_to(uid_buf, working_endpoint.tag->get_uid())); if (this->read_endpoint_data_(*working_endpoint.tag) != nfc::STATUS_OK) { ESP_LOGW(TAG, " Unable to read NDEF record(s)"); } else if (working_endpoint.tag->has_ndef_message()) { @@ -964,7 +977,8 @@ void PN7150::process_rf_deactivate_oid_(nfc::NciMessage &rx) { } void PN7150::process_data_message_(nfc::NciMessage &rx) { - ESP_LOGVV(TAG, "Received data message: %s", nfc::format_bytes(rx.get_message()).c_str()); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, "Received data message: %s", nfc::format_bytes_to(buf, rx.get_message())); std::vector ndef_response; this->card_emu_t4t_get_response_(rx.get_message(), ndef_response); @@ -978,7 +992,7 @@ void PN7150::process_data_message_(nfc::NciMessage &rx) { uint8_t(ndef_response_size & 0x00FF)}; tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end()); nfc::NciMessage tx(tx_msg); - ESP_LOGVV(TAG, "Sending data message: %s", nfc::format_bytes(tx.get_message()).c_str()); + ESP_LOGVV(TAG, "Sending data message: %s", nfc::format_bytes_to(buf, tx.get_message())); if (this->transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending reply for card emulation failed"); } @@ -1031,7 +1045,8 @@ void PN7150::card_emu_t4t_get_response_(std::vector &response, std::vec uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3]; uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4]; - ESP_LOGVV(TAG, "Encoded NDEF message: %s", nfc::format_bytes(ndef_message).c_str()); + char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, "Encoded NDEF message: %s", nfc::format_bytes_to(ndef_buf, ndef_message)); if (length <= (ndef_msg_size + offset + 2)) { if (offset == 0) { @@ -1070,7 +1085,8 @@ void PN7150::card_emu_t4t_get_response_(std::vector &response, std::vec ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5, response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 + length); - ESP_LOGD(TAG, "Received %u-byte NDEF message: %s", length, nfc::format_bytes(ndef_msg_written).c_str()); + char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGD(TAG, "Received %u-byte NDEF message: %s", length, nfc::format_bytes_to(ndef_buf, ndef_msg_written)); ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK)); } } @@ -1079,6 +1095,7 @@ void PN7150::card_emu_t4t_get_response_(std::vector &response, std::vec uint8_t PN7150::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint16_t timeout, const bool expect_notification) { uint8_t retries = NFCC_MAX_COMM_FAILS; + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; while (retries) { // first, send the message we need to send @@ -1086,7 +1103,7 @@ uint8_t PN7150::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint ESP_LOGE(TAG, "Error sending message"); return nfc::STATUS_FAILED; } - ESP_LOGVV(TAG, "Wrote: %s", nfc::format_bytes(tx.get_message()).c_str()); + ESP_LOGVV(TAG, "Wrote: %s", nfc::format_bytes_to(buf, tx.get_message())); // next, the NFCC should send back a response if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) { ESP_LOGW(TAG, "Error receiving message"); @@ -1098,24 +1115,24 @@ uint8_t PN7150::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint break; } } - ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes_to(buf, rx.get_message())); // validate the response based on the message type that was sent (command vs. data) if (!tx.message_type_is(nfc::NCI_PKT_MT_DATA)) { // for commands, the GID and OID should match and the status should be OK if ((rx.get_gid() != tx.get_gid()) || (rx.get_oid()) != tx.get_oid()) { - ESP_LOGE(TAG, "Incorrect response to command: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGE(TAG, "Incorrect response to command: %s", nfc::format_bytes_to(buf, rx.get_message())); return nfc::STATUS_FAILED; } if (!rx.simple_status_response_is(nfc::STATUS_OK)) { - ESP_LOGE(TAG, "Error in response to command: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGE(TAG, "Error in response to command: %s", nfc::format_bytes_to(buf, rx.get_message())); } return rx.get_simple_status_response(); } else { // when requesting data from the endpoint, the first response is from the NFCC; we must validate this, first if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.gid_is(nfc::NCI_CORE_GID)) || (!rx.oid_is(nfc::NCI_CORE_CONN_CREDITS_OID)) || (!rx.message_length_is(3))) { - ESP_LOGE(TAG, "Incorrect response to data message: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGE(TAG, "Incorrect response to data message: %s", nfc::format_bytes_to(buf, rx.get_message())); return nfc::STATUS_FAILED; } @@ -1125,7 +1142,7 @@ uint8_t PN7150::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint ESP_LOGE(TAG, "Error receiving data from endpoint"); return nfc::STATUS_FAILED; } - ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes_to(buf, rx.get_message())); } return nfc::STATUS_OK; diff --git a/esphome/components/pn7150/pn7150_mifare_classic.cpp b/esphome/components/pn7150/pn7150_mifare_classic.cpp index 0443929f69..dee81b610a 100644 --- a/esphome/components/pn7150/pn7150_mifare_classic.cpp +++ b/esphome/components/pn7150/pn7150_mifare_classic.cpp @@ -70,7 +70,8 @@ uint8_t PN7150::read_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Timeout reading tag data"); return nfc::STATUS_FAILED; @@ -79,13 +80,13 @@ uint8_t PN7150::read_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending MFC_AUTHENTICATE_REQ failed"); return nfc::STATUS_FAILED; @@ -119,7 +121,7 @@ uint8_t PN7150::auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, c if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(MFC_AUTHENTICATE_OID)) || (rx.get_message()[4] != nfc::STATUS_OK)) { ESP_LOGE(TAG, "MFC authentication failed - block 0x%02x", block_num); - ESP_LOGVV(TAG, "MFC_AUTHENTICATE_RSP: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGVV(TAG, "MFC_AUTHENTICATE_RSP: %s", nfc::format_bytes_to(buf, rx.get_message())); return nfc::STATUS_FAILED; } @@ -238,7 +240,8 @@ uint8_t PN7150::write_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending XCHG_DATA_REQ failed"); return nfc::STATUS_FAILED; @@ -247,7 +250,7 @@ uint8_t PN7150::write_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) { ESP_LOGE(TAG, "MFC XCHG_DATA timed out waiting for XCHG_DATA_RSP during block write"); return nfc::STATUS_FAILED; @@ -256,7 +259,7 @@ uint8_t PN7150::write_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending halt XCHG_DATA_REQ failed"); return nfc::STATUS_FAILED; diff --git a/esphome/components/pn7150/pn7150_mifare_ultralight.cpp b/esphome/components/pn7150/pn7150_mifare_ultralight.cpp index b107f6f79e..ac15475bad 100644 --- a/esphome/components/pn7150/pn7150_mifare_ultralight.cpp +++ b/esphome/components/pn7150/pn7150_mifare_ultralight.cpp @@ -72,7 +72,8 @@ uint8_t PN7150::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_b } } - ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes(data).c_str()); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes_to(buf, data)); return nfc::STATUS_OK; } diff --git a/esphome/components/pn7160/pn7160.cpp b/esphome/components/pn7160/pn7160.cpp index 8c8028b04a..1a38dce5fd 100644 --- a/esphome/components/pn7160/pn7160.cpp +++ b/esphome/components/pn7160/pn7160.cpp @@ -215,7 +215,8 @@ uint8_t PN7160::set_test_mode(const TestMode test_mode, const std::vector features(rx.get_message().begin() + 4, rx.get_message().begin() + 8); + char feat_buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; ESP_LOGD(TAG, "Hardware version: %u\n" "ROM code version: %u\n" "FLASH major version: %u\n" "FLASH minor version: %u\n" "Features: %s", - hw_version, rom_code_version, flash_major_version, flash_minor_version, nfc::format_bytes(features).c_str()); + hw_version, rom_code_version, flash_major_version, flash_minor_version, + nfc::format_bytes_to(feat_buf, features)); return rx.get_simple_status_response(); } @@ -599,7 +606,8 @@ void PN7160::erase_tag_(const uint8_t tag_index) { for (auto *listener : this->tag_listeners_) { listener->tag_off(*this->discovered_endpoint_[tag_index].tag); } - ESP_LOGI(TAG, "Tag %s removed", nfc::format_uid(this->discovered_endpoint_[tag_index].tag->get_uid()).c_str()); + char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE]; + ESP_LOGI(TAG, "Tag %s removed", nfc::format_uid_to(uid_buf, this->discovered_endpoint_[tag_index].tag->get_uid())); this->discovered_endpoint_.erase(this->discovered_endpoint_.begin() + tag_index); } } @@ -796,26 +804,33 @@ void PN7160::process_message_() { ESP_LOGV(TAG, "Unimplemented NCI Core OID received: 0x%02X", rx.get_oid()); } } else { - ESP_LOGV(TAG, "Unimplemented notification: %s", nfc::format_bytes(rx.get_message()).c_str()); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGV(TAG, "Unimplemented notification: %s", nfc::format_bytes_to(buf, rx.get_message())); } break; - case nfc::NCI_PKT_MT_CTRL_RESPONSE: + case nfc::NCI_PKT_MT_CTRL_RESPONSE: { + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; ESP_LOGV(TAG, "Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.get_gid(), rx.get_oid(), - nfc::format_bytes(rx.get_message()).c_str()); + nfc::format_bytes_to(buf, rx.get_message())); break; + } - case nfc::NCI_PKT_MT_CTRL_COMMAND: - ESP_LOGV(TAG, "Unimplemented command: %s", nfc::format_bytes(rx.get_message()).c_str()); + case nfc::NCI_PKT_MT_CTRL_COMMAND: { + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGV(TAG, "Unimplemented command: %s", nfc::format_bytes_to(buf, rx.get_message())); break; + } case nfc::NCI_PKT_MT_DATA: this->process_data_message_(rx); break; - default: - ESP_LOGV(TAG, "Unimplemented message type: %s", nfc::format_bytes(rx.get_message()).c_str()); + default: { + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGV(TAG, "Unimplemented message type: %s", nfc::format_bytes_to(buf, rx.get_message())); break; + } } } @@ -896,8 +911,9 @@ void PN7160::process_rf_intf_activated_oid_(nfc::NciMessage &rx) { // an endpoi case EP_READ: default: if (!working_endpoint.trig_called) { + char uid_buf[nfc::FORMAT_UID_BUFFER_SIZE]; ESP_LOGI(TAG, "Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(), - nfc::format_uid(working_endpoint.tag->get_uid()).c_str()); + nfc::format_uid_to(uid_buf, working_endpoint.tag->get_uid())); if (this->read_endpoint_data_(*working_endpoint.tag) != nfc::STATUS_OK) { ESP_LOGW(TAG, " Unable to read NDEF record(s)"); } else if (working_endpoint.tag->has_ndef_message()) { @@ -988,7 +1004,8 @@ void PN7160::process_rf_deactivate_oid_(nfc::NciMessage &rx) { } void PN7160::process_data_message_(nfc::NciMessage &rx) { - ESP_LOGVV(TAG, "Received data message: %s", nfc::format_bytes(rx.get_message()).c_str()); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, "Received data message: %s", nfc::format_bytes_to(buf, rx.get_message())); std::vector ndef_response; this->card_emu_t4t_get_response_(rx.get_message(), ndef_response); @@ -1002,7 +1019,7 @@ void PN7160::process_data_message_(nfc::NciMessage &rx) { uint8_t(ndef_response_size & 0x00FF)}; tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end()); nfc::NciMessage tx(tx_msg); - ESP_LOGVV(TAG, "Sending data message: %s", nfc::format_bytes(tx.get_message()).c_str()); + ESP_LOGVV(TAG, "Sending data message: %s", nfc::format_bytes_to(buf, tx.get_message())); if (this->transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending reply for card emulation failed"); } @@ -1055,7 +1072,8 @@ void PN7160::card_emu_t4t_get_response_(std::vector &response, std::vec uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3]; uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4]; - ESP_LOGVV(TAG, "Encoded NDEF message: %s", nfc::format_bytes(ndef_message).c_str()); + char ndef_buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, "Encoded NDEF message: %s", nfc::format_bytes_to(ndef_buf, ndef_message)); if (length <= (ndef_msg_size + offset + 2)) { if (offset == 0) { @@ -1094,7 +1112,8 @@ void PN7160::card_emu_t4t_get_response_(std::vector &response, std::vec ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5, response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 + length); - ESP_LOGD(TAG, "Received %u-byte NDEF message: %s", length, nfc::format_bytes(ndef_msg_written).c_str()); + char write_buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGD(TAG, "Received %u-byte NDEF message: %s", length, nfc::format_bytes_to(write_buf, ndef_msg_written)); ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK)); } } @@ -1103,6 +1122,7 @@ void PN7160::card_emu_t4t_get_response_(std::vector &response, std::vec uint8_t PN7160::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint16_t timeout, const bool expect_notification) { uint8_t retries = NFCC_MAX_COMM_FAILS; + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; while (retries) { // first, send the message we need to send @@ -1110,7 +1130,7 @@ uint8_t PN7160::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint ESP_LOGE(TAG, "Error sending message"); return nfc::STATUS_FAILED; } - ESP_LOGVV(TAG, "Wrote: %s", nfc::format_bytes(tx.get_message()).c_str()); + ESP_LOGVV(TAG, "Wrote: %s", nfc::format_bytes_to(buf, tx.get_message())); // next, the NFCC should send back a response if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) { ESP_LOGW(TAG, "Error receiving message"); @@ -1122,24 +1142,24 @@ uint8_t PN7160::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint break; } } - ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes_to(buf, rx.get_message())); // validate the response based on the message type that was sent (command vs. data) if (!tx.message_type_is(nfc::NCI_PKT_MT_DATA)) { // for commands, the GID and OID should match and the status should be OK if ((rx.get_gid() != tx.get_gid()) || (rx.get_oid()) != tx.get_oid()) { - ESP_LOGE(TAG, "Incorrect response to command: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGE(TAG, "Incorrect response to command: %s", nfc::format_bytes_to(buf, rx.get_message())); return nfc::STATUS_FAILED; } if (!rx.simple_status_response_is(nfc::STATUS_OK)) { - ESP_LOGE(TAG, "Error in response to command: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGE(TAG, "Error in response to command: %s", nfc::format_bytes_to(buf, rx.get_message())); } return rx.get_simple_status_response(); } else { // when requesting data from the endpoint, the first response is from the NFCC; we must validate this, first if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.gid_is(nfc::NCI_CORE_GID)) || (!rx.oid_is(nfc::NCI_CORE_CONN_CREDITS_OID)) || (!rx.message_length_is(3))) { - ESP_LOGE(TAG, "Incorrect response to data message: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGE(TAG, "Incorrect response to data message: %s", nfc::format_bytes_to(buf, rx.get_message())); return nfc::STATUS_FAILED; } @@ -1149,7 +1169,7 @@ uint8_t PN7160::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint ESP_LOGE(TAG, "Error receiving data from endpoint"); return nfc::STATUS_FAILED; } - ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes_to(buf, rx.get_message())); } return nfc::STATUS_OK; diff --git a/esphome/components/pn7160/pn7160_mifare_classic.cpp b/esphome/components/pn7160/pn7160_mifare_classic.cpp index fa63cc00d5..57d2042eaa 100644 --- a/esphome/components/pn7160/pn7160_mifare_classic.cpp +++ b/esphome/components/pn7160/pn7160_mifare_classic.cpp @@ -69,8 +69,9 @@ uint8_t PN7160::read_mifare_classic_tag_(nfc::NfcTag &tag) { uint8_t PN7160::read_mifare_classic_block_(uint8_t block_num, std::vector &data) { nfc::NciMessage rx; nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_READ, block_num}); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; - ESP_LOGVV(TAG, "Read XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str()); + ESP_LOGVV(TAG, "Read XCHG_DATA_REQ: %s", nfc::format_bytes_to(buf, tx.get_message())); if (this->transceive_(tx, rx) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Timeout reading tag data"); return nfc::STATUS_FAILED; @@ -79,13 +80,13 @@ uint8_t PN7160::read_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending MFC_AUTHENTICATE_REQ failed"); return nfc::STATUS_FAILED; @@ -119,7 +121,7 @@ uint8_t PN7160::auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, c if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(MFC_AUTHENTICATE_OID)) || (rx.get_message()[4] != nfc::STATUS_OK)) { ESP_LOGE(TAG, "MFC authentication failed - block 0x%02x", block_num); - ESP_LOGVV(TAG, "MFC_AUTHENTICATE_RSP: %s", nfc::format_bytes(rx.get_message()).c_str()); + ESP_LOGVV(TAG, "MFC_AUTHENTICATE_RSP: %s", nfc::format_bytes_to(buf, rx.get_message())); return nfc::STATUS_FAILED; } @@ -237,8 +239,9 @@ uint8_t PN7160::format_mifare_classic_ndef_() { uint8_t PN7160::write_mifare_classic_block_(uint8_t block_num, std::vector &write_data) { nfc::NciMessage rx; nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_WRITE, block_num}); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; - ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 1: %s", nfc::format_bytes(tx.get_message()).c_str()); + ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 1: %s", nfc::format_bytes_to(buf, tx.get_message())); if (this->transceive_(tx, rx) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending XCHG_DATA_REQ failed"); return nfc::STATUS_FAILED; @@ -247,7 +250,7 @@ uint8_t PN7160::write_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) { ESP_LOGE(TAG, "MFC XCHG_DATA timed out waiting for XCHG_DATA_RSP during block write"); return nfc::STATUS_FAILED; @@ -256,7 +259,7 @@ uint8_t PN7160::write_mifare_classic_block_(uint8_t block_num, std::vectortransceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) { ESP_LOGE(TAG, "Sending halt XCHG_DATA_REQ failed"); return nfc::STATUS_FAILED; diff --git a/esphome/components/pn7160/pn7160_mifare_ultralight.cpp b/esphome/components/pn7160/pn7160_mifare_ultralight.cpp index 65daac494f..584385f113 100644 --- a/esphome/components/pn7160/pn7160_mifare_ultralight.cpp +++ b/esphome/components/pn7160/pn7160_mifare_ultralight.cpp @@ -72,7 +72,8 @@ uint8_t PN7160::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_b } } - ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes(data).c_str()); + char buf[nfc::FORMAT_BYTES_BUFFER_SIZE]; + ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes_to(buf, data)); return nfc::STATUS_OK; }