mirror of
https://github.com/esphome/esphome.git
synced 2026-10-01 00:40:21 +00:00
[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:
co-authored by
Claude Fable 5.1
parent
7187acec04
commit
ef0819f983
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user