1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 00:31:58 +00:00

[nfc] Use stack-based hex formatting in pn7150/pn7160 components (#13163)

This commit is contained in:
J. Nick Koston
2026-01-12 07:38:39 -10:00
committed by GitHub
parent 8cccfa5369
commit 7f0e4eaa84
13 changed files with 154 additions and 73 deletions

View File

@@ -1,9 +1,13 @@
#include "automation.h"
#include "nfc.h"
namespace esphome {
namespace nfc {
void NfcOnTagTrigger::process(const std::unique_ptr<NfcTag> &tag) { this->trigger(format_uid(tag->get_uid()), *tag); }
void NfcOnTagTrigger::process(const std::unique_ptr<NfcTag> &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

View File

@@ -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_));
}
}

View File

@@ -8,9 +8,20 @@ namespace nfc {
static const char *const TAG = "nfc";
char *format_uid_to(char *buffer, const std::vector<uint8_t> &uid) {
return format_hex_pretty_to(buffer, FORMAT_UID_BUFFER_SIZE, uid.data(), uid.size(), '-');
}
char *format_bytes_to(char *buffer, const std::vector<uint8_t> &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<uint8_t> &uid) { return format_hex_pretty(uid, '-', false); }
std::string format_bytes(const std::vector<uint8_t> &bytes) { return format_hex_pretty(bytes, ' ', false); }
#pragma GCC diagnostic pop
uint8_t guess_tag_type(uint8_t uid_length) {
if (uid_length == 4) {

View File

@@ -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<uint8_t> &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<uint8_t> &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<uint8_t> &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<uint8_t> &bytes);
uint8_t guess_tag_type(uint8_t uid_length);

View File

@@ -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();

View File

@@ -77,7 +77,8 @@ bool PN532::read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &
}
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;
}

View File

@@ -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;
}

View File

@@ -203,7 +203,8 @@ uint8_t PN7150::set_test_mode(const TestMode test_mode, const std::vector<uint8_
result = rx.get_message();
result.erase(result.begin(), result.begin() + 4); // remove NCI header
if (!result.empty()) {
ESP_LOGW(TAG, "Test results: %s", nfc::format_bytes(result).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGW(TAG, "Test results: %s", nfc::format_bytes_to(buf, result));
}
}
return status;
@@ -229,14 +230,16 @@ uint8_t PN7150::reset_core_(const bool reset_config, const bool power) {
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid reset response: %s", nfc::format_bytes(rx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGE(TAG, "Invalid reset response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return rx.get_simple_status_response();
}
// verify reset response
if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_RESPONSE)) || (!rx.message_length_is(3)) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != 0x11) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] != (uint8_t) reset_config)) {
ESP_LOGE(TAG, "Reset response was malformed: %s", nfc::format_bytes(rx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGE(TAG, "Reset response was malformed: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
@@ -259,7 +262,8 @@ uint8_t PN7150::init_core_() {
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid initialise response: %s", nfc::format_bytes(rx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGE(TAG, "Invalid initialise response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
@@ -574,7 +578,8 @@ void PN7150::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);
}
}
@@ -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<uint8_t> 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<uint8_t> &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<uint8_t> &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<uint8_t> &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;

View File

@@ -70,7 +70,8 @@ uint8_t PN7150::read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_READ, block_num});
ESP_LOGVV(TAG, "Read XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
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 PN7150::read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(!rx.message_length_is(18))) {
ESP_LOGE(TAG, "MFC read block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Read response: %s", nfc::format_bytes(rx.get_message()).c_str());
ESP_LOGV(TAG, "Read response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
data.insert(data.begin(), rx.get_message().begin() + 4, rx.get_message().end() - 1);
ESP_LOGVV(TAG, " Block %u: %s", block_num, nfc::format_bytes(data).c_str());
ESP_LOGVV(TAG, " Block %u: %s", block_num, nfc::format_bytes_to(buf, data));
return nfc::STATUS_OK;
}
@@ -111,7 +112,8 @@ uint8_t PN7150::auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, c
tx.get_message().insert(tx.get_message().end(), key, key + 6);
}
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_REQ: %s", nfc::format_bytes_to(buf, tx.get_message()));
if (this->transceive_(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::vector<uint8
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_WRITE, block_num});
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 1: %s", nfc::format_bytes(tx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
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 PN7150::write_mifare_classic_block_(uint8_t block_num, std::vector<uint8
tx.set_payload({XCHG_DATA_OID});
tx.get_message().insert(tx.get_message().end(), write_data.begin(), write_data.end());
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 2: %s", nfc::format_bytes(tx.get_message()).c_str());
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 2: %s", nfc::format_bytes_to(buf, tx.get_message()));
if (this->transceive_(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::vector<uint8
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(rx.get_message()[4] != nfc::MIFARE_CMD_ACK)) {
ESP_LOGE(TAG, "MFC write block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Write response: %s", nfc::format_bytes(rx.get_message()).c_str());
ESP_LOGV(TAG, "Write response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
@@ -310,7 +313,8 @@ uint8_t PN7150::halt_mifare_classic_tag_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_HALT, 0});
ESP_LOGVV(TAG, "Halt XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGVV(TAG, "Halt XCHG_DATA_REQ: %s", nfc::format_bytes_to(buf, tx.get_message()));
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending halt XCHG_DATA_REQ failed");
return nfc::STATUS_FAILED;

View File

@@ -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;
}

View File

@@ -215,7 +215,8 @@ uint8_t PN7160::set_test_mode(const TestMode test_mode, const std::vector<uint8_
result = rx.get_message();
result.erase(result.begin(), result.begin() + 4); // remove NCI header
if (!result.empty()) {
ESP_LOGW(TAG, "Test results: %s", nfc::format_bytes(result).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGW(TAG, "Test results: %s", nfc::format_bytes_to(buf, result));
}
}
return status;
@@ -246,7 +247,8 @@ uint8_t PN7160::reset_core_(const bool reset_config, const bool power) {
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid reset response: %s", nfc::format_bytes(rx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGE(TAG, "Invalid reset response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return rx.get_simple_status_response();
}
// read reset notification
@@ -258,7 +260,8 @@ uint8_t PN7160::reset_core_(const bool reset_config, const bool power) {
if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.message_length_is(9)) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET] != 0x02) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != (uint8_t) reset_config)) {
ESP_LOGE(TAG, "Reset notification was malformed: %s", nfc::format_bytes(rx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGE(TAG, "Reset notification was malformed: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
@@ -269,7 +272,8 @@ uint8_t PN7160::reset_core_(const bool reset_config, const bool power) {
rx.get_message()[4] ? "reset" : "retained", rx.get_message()[5] == 0x20 ? "2.0" : "1.0",
rx.get_message()[6]);
rx.get_message().erase(rx.get_message().begin(), rx.get_message().begin() + 8);
ESP_LOGD(TAG, "Manufacturer info: %s", nfc::format_bytes(rx.get_message()).c_str());
char mfr_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGD(TAG, "Manufacturer info: %s", nfc::format_bytes_to(mfr_buf, rx.get_message()));
return nfc::STATUS_OK;
}
@@ -284,7 +288,8 @@ uint8_t PN7160::init_core_() {
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid initialise response: %s", nfc::format_bytes(rx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGE(TAG, "Invalid initialise response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
@@ -294,13 +299,15 @@ uint8_t PN7160::init_core_() {
uint8_t flash_minor_version = rx.get_message()[20 + rx.get_message()[8]];
std::vector<uint8_t> 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<uint8_t> 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<uint8_t> &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<uint8_t> &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<uint8_t> &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;

View File

@@ -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<uint8_t> &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::vector<uint8_
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(!rx.message_length_is(18))) {
ESP_LOGE(TAG, "MFC read block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Read response: %s", nfc::format_bytes(rx.get_message()).c_str());
ESP_LOGV(TAG, "Read response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
data.insert(data.begin(), rx.get_message().begin() + 4, rx.get_message().end() - 1);
ESP_LOGVV(TAG, " Block %u: %s", block_num, nfc::format_bytes(data).c_str());
ESP_LOGVV(TAG, " Block %u: %s", block_num, nfc::format_bytes_to(buf, data));
return nfc::STATUS_OK;
}
@@ -111,7 +112,8 @@ uint8_t PN7160::auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, c
tx.get_message().insert(tx.get_message().end(), key, key + 6);
}
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_REQ: %s", nfc::format_bytes_to(buf, tx.get_message()));
if (this->transceive_(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<uint8_t> &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::vector<uint8
tx.set_payload({XCHG_DATA_OID});
tx.get_message().insert(tx.get_message().end(), write_data.begin(), write_data.end());
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 2: %s", nfc::format_bytes(tx.get_message()).c_str());
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 2: %s", nfc::format_bytes_to(buf, tx.get_message()));
if (this->transceive_(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::vector<uint8
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(rx.get_message()[4] != nfc::MIFARE_CMD_ACK)) {
ESP_LOGE(TAG, "MFC write block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Write response: %s", nfc::format_bytes(rx.get_message()).c_str());
ESP_LOGV(TAG, "Write response: %s", nfc::format_bytes_to(buf, rx.get_message()));
return nfc::STATUS_FAILED;
}
@@ -310,7 +313,8 @@ uint8_t PN7160::halt_mifare_classic_tag_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_HALT, 0});
ESP_LOGVV(TAG, "Halt XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
ESP_LOGVV(TAG, "Halt XCHG_DATA_REQ: %s", nfc::format_bytes_to(buf, tx.get_message()));
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending halt XCHG_DATA_REQ failed");
return nfc::STATUS_FAILED;

View File

@@ -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;
}