[nfc] Hold NDEF records in a fixed-size list (#19912)

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Keith Burzinski
2026-09-30 01:12:55 +02:00
committed by GitHub
co-authored by Claude Fable 5.1
parent 7187acec04
commit ef0819f983
3 changed files with 10 additions and 7 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ bool NdefMessage::add_record(std::unique_ptr<NdefRecord> record) {
ESP_LOGE(TAG, "Too many records. Max: %d", MAX_NDEF_RECORDS);
return false;
}
this->records_.emplace_back(std::move(record));
this->records_.emplace_next() = std::move(record);
return true;
}
+5 -4
View File
@@ -13,6 +13,8 @@
namespace esphome::nfc {
static constexpr uint8_t MAX_NDEF_RECORDS = 4;
/// The records of one message, owned in place so parsing a message allocates only the records themselves
using NdefRecords = StaticVector<std::unique_ptr<NdefRecord>, MAX_NDEF_RECORDS>;
class NdefMessage {
public:
@@ -20,13 +22,12 @@ class NdefMessage {
NdefMessage(std::span<const uint8_t> data);
NdefMessage(std::vector<uint8_t> &data) : NdefMessage(std::span<const uint8_t>(data)) {}
NdefMessage(const NdefMessage &msg) {
records_.reserve(msg.records_.size());
for (const auto &r : msg.records_) {
records_.emplace_back(r->clone());
records_.emplace_next() = r->clone();
}
}
const std::vector<std::shared_ptr<NdefRecord>> &get_records() { return this->records_; };
const NdefRecords &get_records() const { return this->records_; };
bool add_record(std::unique_ptr<NdefRecord> record);
bool add_text_record(const std::string &text);
@@ -36,7 +37,7 @@ class NdefMessage {
std::vector<uint8_t> encode();
protected:
std::vector<std::shared_ptr<NdefRecord>> records_;
NdefRecords records_;
};
} // namespace esphome::nfc
+4 -2
View File
@@ -339,8 +339,9 @@ enum PN532ReadReady PN532::read_ready_(bool block) {
return READY;
}
// the non-blocking call comes from loop(), where the tick's timestamp is already cached
if (!this->rd_started_) {
this->rd_start_time_ = millis();
this->rd_start_time_ = block ? millis() : App.get_loop_component_start_time();
this->rd_started_ = true;
}
const uint32_t rd_start_time = this->rd_start_time_;
@@ -351,7 +352,8 @@ enum PN532ReadReady PN532::read_ready_(bool block) {
break;
}
if (millis() - rd_start_time > 100) {
const uint32_t now = block ? millis() : App.get_loop_component_start_time();
if (now - rd_start_time > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
this->rd_ready_ = TIMEOUT;
break;