diff --git a/components/pn532/__init__.py b/components/pn532/__init__.py index cdcaf42..6f679ed 100644 --- a/components/pn532/__init__.py +++ b/components/pn532/__init__.py @@ -1,12 +1,12 @@ -import esphome.codegen as cg -import esphome.config_validation as cv from esphome import automation +import esphome.codegen as cg from esphome.components import nfc +import esphome.config_validation as cv from esphome.const import ( CONF_ID, CONF_ON_FINISHED_WRITE, - CONF_ON_TAG_REMOVED, CONF_ON_TAG, + CONF_ON_TAG_REMOVED, CONF_TRIGGER_ID, ) @@ -55,7 +55,7 @@ def CONFIG_SCHEMA(conf): if conf: raise cv.Invalid( "This component has been moved in 1.16, please see the docs for updated " - "instructions. https://esphome.io/components/binary_sensor/pn532.html" + "instructions. https://esphome.io/components/binary_sensor/pn532/" ) diff --git a/components/pn532/pn532.cpp b/components/pn532/pn532.cpp index 8088e6c..1ab0da3 100644 --- a/components/pn532/pn532.cpp +++ b/components/pn532/pn532.cpp @@ -15,11 +15,9 @@ namespace pn532 { static const char *const TAG = "pn532"; void PN532::setup() { - ESP_LOGCONFIG(TAG, "Setting up PN532..."); - // Get version data if (!this->write_command_({PN532_COMMAND_VERSION_DATA})) { - ESP_LOGW(TAG, "Error sending version command, trying again..."); + ESP_LOGW(TAG, "Error sending version command, trying again"); if (!this->write_command_({PN532_COMMAND_VERSION_DATA})) { ESP_LOGE(TAG, "Error sending version command"); this->mark_failed(); @@ -33,8 +31,7 @@ void PN532::setup() { this->mark_failed(); return; } - ESP_LOGD(TAG, "Found chip PN5%02X", version_data[0]); - ESP_LOGD(TAG, "Firmware ver. %d.%d", version_data[1], version_data[2]); + ESP_LOGD(TAG, "Found chip PN5%02X, Firmware v%d.%d", version_data[0], version_data[1], version_data[2]); if (!this->write_command_({ PN532_COMMAND_SAMCONFIGURATION, @@ -168,11 +165,11 @@ void PN532::loop() { } uint8_t nfcid_length = read[5]; - std::vector nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length); - if (read.size() < 6U + nfcid_length) { + if (nfcid_length > nfc::NFC_UID_MAX_LENGTH || read.size() < 6U + nfcid_length) { // oops, pn532 returned invalid data return; } + nfc::NfcTagUid nfcid(read.begin() + 6, read.begin() + 6 + nfcid_length); bool report = true; for (auto *bin_sens : this->binary_sensors_) { @@ -197,7 +194,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(); @@ -208,21 +206,21 @@ void PN532::loop() { } } } else if (next_task_ == CLEAN) { - ESP_LOGD(TAG, " Tag cleaning..."); + ESP_LOGD(TAG, " Tag cleaning"); if (!this->clean_tag_(nfcid)) { ESP_LOGE(TAG, " Tag was not fully cleaned successfully"); } ESP_LOGD(TAG, " Tag cleaned!"); } else if (next_task_ == FORMAT) { - ESP_LOGD(TAG, " Tag formatting..."); + ESP_LOGD(TAG, " Tag formatting"); if (!this->format_tag_(nfcid)) { ESP_LOGE(TAG, "Error formatting tag as NDEF"); } ESP_LOGD(TAG, " Tag formatted!"); } else if (next_task_ == WRITE) { if (this->next_task_message_to_write_ != nullptr) { - ESP_LOGD(TAG, " Tag writing..."); - ESP_LOGD(TAG, " Tag formatting..."); + ESP_LOGD(TAG, " Tag writing"); + ESP_LOGD(TAG, " Tag formatting"); if (!this->format_tag_(nfcid)) { ESP_LOGE(TAG, " Tag could not be formatted for writing"); } else { @@ -281,7 +279,7 @@ bool PN532::write_command_(const std::vector &data) { } bool PN532::read_ack_() { - ESP_LOGV(TAG, "Reading ACK..."); + ESP_LOGV(TAG, "Reading ACK"); std::vector data; if (!this->read_data(data, 6)) { @@ -357,7 +355,7 @@ void PN532::turn_off_rf_() { }); } -std::unique_ptr PN532::read_tag_(std::vector &uid) { +std::unique_ptr PN532::read_tag_(nfc::NfcTagUid &uid) { uint8_t type = nfc::guess_tag_type(uid.size()); if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) { @@ -392,7 +390,7 @@ void PN532::write_mode(nfc::NdefMessage *message) { ESP_LOGD(TAG, "Waiting to write next tag"); } -bool PN532::clean_tag_(std::vector &uid) { +bool PN532::clean_tag_(nfc::NfcTagUid &uid) { uint8_t type = nfc::guess_tag_type(uid.size()); if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) { return this->format_mifare_classic_mifare_(uid); @@ -403,7 +401,7 @@ bool PN532::clean_tag_(std::vector &uid) { return false; } -bool PN532::format_tag_(std::vector &uid) { +bool PN532::format_tag_(nfc::NfcTagUid &uid) { uint8_t type = nfc::guess_tag_type(uid.size()); if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) { return this->format_mifare_classic_ndef_(uid); @@ -414,7 +412,7 @@ bool PN532::format_tag_(std::vector &uid) { return false; } -bool PN532::write_tag_(std::vector &uid, nfc::NdefMessage *message) { +bool PN532::write_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message) { uint8_t type = nfc::guess_tag_type(uid.size()); if (type == nfc::TAG_TYPE_MIFARE_CLASSIC) { return this->write_mifare_classic_tag_(uid, message); @@ -425,8 +423,6 @@ bool PN532::write_tag_(std::vector &uid, nfc::NdefMessage *message) { return false; } -float PN532::get_setup_priority() const { return setup_priority::DATA; } - void PN532::dump_config() { ESP_LOGCONFIG(TAG, "PN532:"); switch (this->error_code_) { @@ -447,7 +443,7 @@ void PN532::dump_config() { } } -bool PN532BinarySensor::process(std::vector &data) { +bool PN532BinarySensor::process(const nfc::NfcTagUid &data) { if (data.size() != this->uid_.size()) return false; diff --git a/components/pn532/pn532.h b/components/pn532/pn532.h index 8194d86..f98c0f9 100644 --- a/components/pn532/pn532.h +++ b/components/pn532/pn532.h @@ -35,10 +35,9 @@ class PN532 : public PollingComponent { void dump_config() override; void update() override; - float get_setup_priority() const override; void loop() override; - void on_shutdown() override { powerdown(); } + void on_powerdown() override { powerdown(); } void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); } void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); } @@ -69,28 +68,28 @@ class PN532 : public PollingComponent { virtual bool read_data(std::vector &data, uint8_t len) = 0; virtual bool read_response(uint8_t command, std::vector &data) = 0; - std::unique_ptr read_tag_(std::vector &uid); + std::unique_ptr read_tag_(nfc::NfcTagUid &uid); - bool format_tag_(std::vector &uid); - bool clean_tag_(std::vector &uid); - bool write_tag_(std::vector &uid, nfc::NdefMessage *message); + bool format_tag_(nfc::NfcTagUid &uid); + bool clean_tag_(nfc::NfcTagUid &uid); + bool write_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message); - std::unique_ptr read_mifare_classic_tag_(std::vector &uid); + std::unique_ptr read_mifare_classic_tag_(nfc::NfcTagUid &uid); bool read_mifare_classic_block_(uint8_t block_num, std::vector &data); - bool write_mifare_classic_block_(uint8_t block_num, std::vector &data); - bool auth_mifare_classic_block_(std::vector &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key); - bool format_mifare_classic_mifare_(std::vector &uid); - bool format_mifare_classic_ndef_(std::vector &uid); - bool write_mifare_classic_tag_(std::vector &uid, nfc::NdefMessage *message); + bool write_mifare_classic_block_(uint8_t block_num, const uint8_t *data, size_t len); + bool auth_mifare_classic_block_(nfc::NfcTagUid &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key); + bool format_mifare_classic_mifare_(nfc::NfcTagUid &uid); + bool format_mifare_classic_ndef_(nfc::NfcTagUid &uid); + bool write_mifare_classic_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message); - std::unique_ptr read_mifare_ultralight_tag_(std::vector &uid); + std::unique_ptr read_mifare_ultralight_tag_(nfc::NfcTagUid &uid); bool read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector &data); bool is_mifare_ultralight_formatted_(const std::vector &page_3_to_6); uint16_t read_mifare_ultralight_capacity_(); bool find_mifare_ultralight_ndef_(const std::vector &page_3_to_6, uint8_t &message_length, uint8_t &message_start_index); - bool write_mifare_ultralight_page_(uint8_t page_num, std::vector &write_data); - bool write_mifare_ultralight_tag_(std::vector &uid, nfc::NdefMessage *message); + bool write_mifare_ultralight_page_(uint8_t page_num, const uint8_t *write_data, size_t len); + bool write_mifare_ultralight_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message); bool clean_mifare_ultralight_(); bool updates_enabled_{true}; @@ -98,7 +97,7 @@ class PN532 : public PollingComponent { std::vector binary_sensors_; std::vector triggers_ontag_; std::vector triggers_ontagremoved_; - std::vector current_uid_; + nfc::NfcTagUid current_uid_; nfc::NdefMessage *next_task_message_to_write_; uint32_t rd_start_time_{0}; enum PN532ReadReady rd_ready_ { WOULDBLOCK }; @@ -118,9 +117,9 @@ class PN532 : public PollingComponent { class PN532BinarySensor : public binary_sensor::BinarySensor { public: - void set_uid(const std::vector &uid) { uid_ = uid; } + void set_uid(const nfc::NfcTagUid &uid) { uid_ = uid; } - bool process(std::vector &data); + bool process(const nfc::NfcTagUid &data); void on_scan_end() { if (!this->found_) { @@ -130,7 +129,7 @@ class PN532BinarySensor : public binary_sensor::BinarySensor { } protected: - std::vector uid_; + nfc::NfcTagUid uid_; bool found_{false}; }; @@ -143,7 +142,7 @@ class PN532OnFinishedWriteTrigger : public Trigger<> { template class PN532IsWritingCondition : public Condition, public Parented { public: - bool check(Ts... x) override { return this->parent_->is_writing(); } + bool check(const Ts &...x) override { return this->parent_->is_writing(); } }; } // namespace pn532 diff --git a/components/pn532/pn532_mifare_classic.cpp b/components/pn532/pn532_mifare_classic.cpp index 943f8c5..cca6acd 100644 --- a/components/pn532/pn532_mifare_classic.cpp +++ b/components/pn532/pn532_mifare_classic.cpp @@ -1,3 +1,4 @@ +#include #include #include "pn532.h" @@ -8,7 +9,7 @@ namespace pn532 { static const char *const TAG = "pn532.mifare_classic"; -std::unique_ptr PN532::read_mifare_classic_tag_(std::vector &uid) { +std::unique_ptr PN532::read_mifare_classic_tag_(nfc::NfcTagUid &uid) { uint8_t current_block = 4; uint8_t message_start_index = 0; uint32_t message_length = 0; @@ -77,12 +78,12 @@ 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; } -bool PN532::auth_mifare_classic_block_(std::vector &uid, uint8_t block_num, uint8_t key_num, - const uint8_t *key) { +bool PN532::auth_mifare_classic_block_(nfc::NfcTagUid &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key) { std::vector data({ PN532_COMMAND_INDATAEXCHANGE, 0x01, // One card @@ -105,11 +106,11 @@ bool PN532::auth_mifare_classic_block_(std::vector &uid, uint8_t block_ return true; } -bool PN532::format_mifare_classic_mifare_(std::vector &uid) { - std::vector blank_buffer( - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - std::vector trailer_buffer( - {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}); +bool PN532::format_mifare_classic_mifare_(nfc::NfcTagUid &uid) { + static constexpr std::array BLANK_BUFFER = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + static constexpr std::array TRAILER_BUFFER = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; bool error = false; @@ -118,20 +119,20 @@ bool PN532::format_mifare_classic_mifare_(std::vector &uid) { continue; } if (block != 0) { - if (!this->write_mifare_classic_block_(block, blank_buffer)) { + if (!this->write_mifare_classic_block_(block, BLANK_BUFFER.data(), BLANK_BUFFER.size())) { ESP_LOGE(TAG, "Unable to write block %d", block); error = true; } } - if (!this->write_mifare_classic_block_(block + 1, blank_buffer)) { + if (!this->write_mifare_classic_block_(block + 1, BLANK_BUFFER.data(), BLANK_BUFFER.size())) { ESP_LOGE(TAG, "Unable to write block %d", block + 1); error = true; } - if (!this->write_mifare_classic_block_(block + 2, blank_buffer)) { + if (!this->write_mifare_classic_block_(block + 2, BLANK_BUFFER.data(), BLANK_BUFFER.size())) { ESP_LOGE(TAG, "Unable to write block %d", block + 2); error = true; } - if (!this->write_mifare_classic_block_(block + 3, trailer_buffer)) { + if (!this->write_mifare_classic_block_(block + 3, TRAILER_BUFFER.data(), TRAILER_BUFFER.size())) { ESP_LOGE(TAG, "Unable to write block %d", block + 3); error = true; } @@ -140,29 +141,29 @@ bool PN532::format_mifare_classic_mifare_(std::vector &uid) { return !error; } -bool PN532::format_mifare_classic_ndef_(std::vector &uid) { - std::vector empty_ndef_message( - {0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - std::vector blank_block( - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - std::vector block_1_data( - {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}); - std::vector block_2_data( - {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}); - std::vector block_3_trailer( - {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}); - std::vector ndef_trailer( - {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}); +bool PN532::format_mifare_classic_ndef_(nfc::NfcTagUid &uid) { + static constexpr std::array EMPTY_NDEF_MESSAGE = { + 0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + static constexpr std::array BLANK_BLOCK = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + static constexpr std::array BLOCK_1_DATA = { + 0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; + static constexpr std::array BLOCK_2_DATA = { + 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; + static constexpr std::array BLOCK_3_TRAILER = { + 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + static constexpr std::array NDEF_TRAILER = { + 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; if (!this->auth_mifare_classic_block_(uid, 0, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY)) { ESP_LOGE(TAG, "Unable to authenticate block 0 for formatting!"); return false; } - if (!this->write_mifare_classic_block_(1, block_1_data)) + if (!this->write_mifare_classic_block_(1, BLOCK_1_DATA.data(), BLOCK_1_DATA.size())) return false; - if (!this->write_mifare_classic_block_(2, block_2_data)) + if (!this->write_mifare_classic_block_(2, BLOCK_2_DATA.data(), BLOCK_2_DATA.size())) return false; - if (!this->write_mifare_classic_block_(3, block_3_trailer)) + if (!this->write_mifare_classic_block_(3, BLOCK_3_TRAILER.data(), BLOCK_3_TRAILER.size())) return false; ESP_LOGD(TAG, "Sector 0 formatted to NDEF"); @@ -172,36 +173,36 @@ bool PN532::format_mifare_classic_ndef_(std::vector &uid) { return false; } if (block == 4) { - if (!this->write_mifare_classic_block_(block, empty_ndef_message)) { + if (!this->write_mifare_classic_block_(block, EMPTY_NDEF_MESSAGE.data(), EMPTY_NDEF_MESSAGE.size())) { ESP_LOGE(TAG, "Unable to write block %d", block); } } else { - if (!this->write_mifare_classic_block_(block, blank_block)) { + if (!this->write_mifare_classic_block_(block, BLANK_BLOCK.data(), BLANK_BLOCK.size())) { ESP_LOGE(TAG, "Unable to write block %d", block); } } - if (!this->write_mifare_classic_block_(block + 1, blank_block)) { + if (!this->write_mifare_classic_block_(block + 1, BLANK_BLOCK.data(), BLANK_BLOCK.size())) { ESP_LOGE(TAG, "Unable to write block %d", block + 1); } - if (!this->write_mifare_classic_block_(block + 2, blank_block)) { + if (!this->write_mifare_classic_block_(block + 2, BLANK_BLOCK.data(), BLANK_BLOCK.size())) { ESP_LOGE(TAG, "Unable to write block %d", block + 2); } - if (!this->write_mifare_classic_block_(block + 3, ndef_trailer)) { + if (!this->write_mifare_classic_block_(block + 3, NDEF_TRAILER.data(), NDEF_TRAILER.size())) { ESP_LOGE(TAG, "Unable to write trailer block %d", block + 3); } } return true; } -bool PN532::write_mifare_classic_block_(uint8_t block_num, std::vector &write_data) { - std::vector data({ +bool PN532::write_mifare_classic_block_(uint8_t block_num, const uint8_t *data, size_t len) { + std::vector cmd({ PN532_COMMAND_INDATAEXCHANGE, 0x01, // One card nfc::MIFARE_CMD_WRITE, block_num, }); - data.insert(data.end(), write_data.begin(), write_data.end()); - if (!this->write_command_(data)) { + cmd.insert(cmd.end(), data, data + len); + if (!this->write_command_(cmd)) { ESP_LOGE(TAG, "Error writing block %d", block_num); return false; } @@ -215,7 +216,7 @@ bool PN532::write_mifare_classic_block_(uint8_t block_num, std::vector return true; } -bool PN532::write_mifare_classic_tag_(std::vector &uid, nfc::NdefMessage *message) { +bool PN532::write_mifare_classic_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message) { auto encoded = message->encode(); uint32_t message_length = encoded.size(); @@ -243,8 +244,7 @@ bool PN532::write_mifare_classic_tag_(std::vector &uid, nfc::NdefMessag } } - std::vector data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_CLASSIC_BLOCK_SIZE); - if (!this->write_mifare_classic_block_(current_block, data)) { + if (!this->write_mifare_classic_block_(current_block, encoded.data() + index, nfc::MIFARE_CLASSIC_BLOCK_SIZE)) { return false; } index += nfc::MIFARE_CLASSIC_BLOCK_SIZE; diff --git a/components/pn532/pn532_mifare_ultralight.cpp b/components/pn532/pn532_mifare_ultralight.cpp index 6328be3..149dde7 100644 --- a/components/pn532/pn532_mifare_ultralight.cpp +++ b/components/pn532/pn532_mifare_ultralight.cpp @@ -1,3 +1,4 @@ +#include #include #include "pn532.h" @@ -9,7 +10,7 @@ namespace pn532 { static const char *const TAG = "pn532.mifare_ultralight"; -std::unique_ptr PN532::read_mifare_ultralight_tag_(std::vector &uid) { +std::unique_ptr PN532::read_mifare_ultralight_tag_(nfc::NfcTagUid &uid) { std::vector data; // pages 3 to 6 contain various info we are interested in -- do one read to grab it all if (!this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE * nfc::MIFARE_ULTRALIGHT_READ_SIZE, @@ -72,7 +73,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; } @@ -114,7 +116,7 @@ bool PN532::find_mifare_ultralight_ndef_(const std::vector &page_3_to_6 return false; } -bool PN532::write_mifare_ultralight_tag_(std::vector &uid, nfc::NdefMessage *message) { +bool PN532::write_mifare_ultralight_tag_(nfc::NfcTagUid &uid, nfc::NdefMessage *message) { uint32_t capacity = this->read_mifare_ultralight_capacity_(); auto encoded = message->encode(); @@ -143,8 +145,7 @@ bool PN532::write_mifare_ultralight_tag_(std::vector &uid, nfc::NdefMes uint8_t current_page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; while (index < buffer_length) { - std::vector data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE); - if (!this->write_mifare_ultralight_page_(current_page, data)) { + if (!this->write_mifare_ultralight_page_(current_page, encoded.data() + index, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE)) { return false; } index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; @@ -157,25 +158,25 @@ bool PN532::clean_mifare_ultralight_() { uint32_t capacity = this->read_mifare_ultralight_capacity_(); uint8_t pages = (capacity / nfc::MIFARE_ULTRALIGHT_PAGE_SIZE) + nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; - std::vector blank_data = {0x00, 0x00, 0x00, 0x00}; + static constexpr std::array BLANK_DATA = {0x00, 0x00, 0x00, 0x00}; for (int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) { - if (!this->write_mifare_ultralight_page_(i, blank_data)) { + if (!this->write_mifare_ultralight_page_(i, BLANK_DATA.data(), BLANK_DATA.size())) { return false; } } return true; } -bool PN532::write_mifare_ultralight_page_(uint8_t page_num, std::vector &write_data) { - std::vector data({ +bool PN532::write_mifare_ultralight_page_(uint8_t page_num, const uint8_t *write_data, size_t len) { + std::vector cmd({ PN532_COMMAND_INDATAEXCHANGE, 0x01, // One card nfc::MIFARE_CMD_WRITE_ULTRALIGHT, page_num, }); - data.insert(data.end(), write_data.begin(), write_data.end()); - if (!this->write_command_(data)) { + cmd.insert(cmd.end(), write_data, write_data + len); + if (!this->write_command_(cmd)) { ESP_LOGE(TAG, "Error writing page %u", page_num); return false; } diff --git a/tagtuner-D1-custom1.yaml b/tagtuner-D1-custom1.yaml index 062033e..bb67a2b 100644 --- a/tagtuner-D1-custom1.yaml +++ b/tagtuner-D1-custom1.yaml @@ -7,7 +7,6 @@ packages: core-comms: !include tagtuner-core-comms.yaml esphome: - min_version: 2026.1.0 name: "${name}" friendly_name: ${friendly_name} # Automatically add the mac address to the name diff --git a/tagtuner-XIAO-custom.yaml b/tagtuner-XIAO-custom.yaml index 352dab1..0c1179b 100644 --- a/tagtuner-XIAO-custom.yaml +++ b/tagtuner-XIAO-custom.yaml @@ -7,7 +7,6 @@ packages: core-comms: !include tagtuner-core-comms.yaml esphome: - min_version: 2026.1.0 name: "${name}" friendly_name: ${friendly_name} # Automatically add the mac address to the name diff --git a/tagtuner-atom-grove-ble.yaml b/tagtuner-atom-grove-ble.yaml index a569b0e..450184c 100644 --- a/tagtuner-atom-grove-ble.yaml +++ b/tagtuner-atom-grove-ble.yaml @@ -12,7 +12,6 @@ substitutions: friendly_name: "TagTuner" esphome: - min_version: 2026.1.0 name: "${name}" friendly_name: ${friendly_name} # Automatically add the mac address to the name diff --git a/tagtuner-on-ha-voice-pe.yaml b/tagtuner-on-ha-voice-pe.yaml index c4d9496..55db4ec 100644 --- a/tagtuner-on-ha-voice-pe.yaml +++ b/tagtuner-on-ha-voice-pe.yaml @@ -31,7 +31,6 @@ esphome: name: "${name}" friendly_name: ${friendly_name} name_add_mac_suffix: true - min_version: 2026.1.0 on_boot: priority: 375 then: diff --git a/tagtuner-project.yaml b/tagtuner-project.yaml index ce54e0f..3ff315e 100644 --- a/tagtuner-project.yaml +++ b/tagtuner-project.yaml @@ -4,6 +4,7 @@ substitutions: magic: "260103" esphome: + min_version: 2026.2.0 name: "${name}" friendly_name: ${friendly_name} # Automatically add the mac address to the name