mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 08:55:22 +01:00
Fix some NFC/PN532 crashes (#4678)
* Add + use some constants, fix some crashes * Fix PN532 crashes
This commit is contained in:
parent
3d7d689040
commit
5a4840f641
5 changed files with 31 additions and 9 deletions
|
@ -44,6 +44,11 @@ NdefMessage::NdefMessage(std::vector<uint8_t> &data) {
|
||||||
index += id_length;
|
index += id_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((data.begin() + index > data.end()) || (data.begin() + index + payload_length > data.end())) {
|
||||||
|
ESP_LOGE(TAG, "Corrupt record encountered; NdefMessage constructor aborting");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> payload_data(data.begin() + index, data.begin() + index + payload_length);
|
std::vector<uint8_t> payload_data(data.begin() + index, data.begin() + index + payload_length);
|
||||||
|
|
||||||
std::unique_ptr<NdefRecord> record;
|
std::unique_ptr<NdefRecord> record;
|
||||||
|
|
|
@ -42,8 +42,8 @@ class NdefRecord {
|
||||||
virtual const std::string &get_payload() const { return this->payload_; };
|
virtual const std::string &get_payload() const { return this->payload_; };
|
||||||
|
|
||||||
virtual std::vector<uint8_t> get_encoded_payload() {
|
virtual std::vector<uint8_t> get_encoded_payload() {
|
||||||
std::vector<uint8_t> empty_payload;
|
std::vector<uint8_t> payload(this->payload_.begin(), this->payload_.end());
|
||||||
return empty_payload;
|
return payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -89,18 +89,18 @@ uint32_t get_mifare_classic_buffer_size(uint32_t message_length) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mifare_classic_is_first_block(uint8_t block_num) {
|
bool mifare_classic_is_first_block(uint8_t block_num) {
|
||||||
if (block_num < 128) {
|
if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
|
||||||
return (block_num % 4 == 0);
|
return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
|
||||||
} else {
|
} else {
|
||||||
return (block_num % 16 == 0);
|
return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mifare_classic_is_trailer_block(uint8_t block_num) {
|
bool mifare_classic_is_trailer_block(uint8_t block_num) {
|
||||||
if (block_num < 128) {
|
if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
|
||||||
return ((block_num + 1) % 4 == 0);
|
return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
|
||||||
} else {
|
} else {
|
||||||
return ((block_num + 1) % 16 == 0);
|
return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,9 @@ namespace nfc {
|
||||||
static const uint8_t MIFARE_CLASSIC_BLOCK_SIZE = 16;
|
static const uint8_t MIFARE_CLASSIC_BLOCK_SIZE = 16;
|
||||||
static const uint8_t MIFARE_CLASSIC_LONG_TLV_SIZE = 4;
|
static const uint8_t MIFARE_CLASSIC_LONG_TLV_SIZE = 4;
|
||||||
static const uint8_t MIFARE_CLASSIC_SHORT_TLV_SIZE = 2;
|
static const uint8_t MIFARE_CLASSIC_SHORT_TLV_SIZE = 2;
|
||||||
|
static const uint8_t MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW = 4;
|
||||||
|
static const uint8_t MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH = 16;
|
||||||
|
static const uint8_t MIFARE_CLASSIC_16BLOCK_SECT_START = 32;
|
||||||
|
|
||||||
static const uint8_t MIFARE_ULTRALIGHT_PAGE_SIZE = 4;
|
static const uint8_t MIFARE_ULTRALIGHT_PAGE_SIZE = 4;
|
||||||
static const uint8_t MIFARE_ULTRALIGHT_READ_SIZE = 4;
|
static const uint8_t MIFARE_ULTRALIGHT_READ_SIZE = 4;
|
||||||
|
@ -30,10 +33,18 @@ static const uint8_t TAG_TYPE_UNKNOWN = 99;
|
||||||
// Mifare Commands
|
// Mifare Commands
|
||||||
static const uint8_t MIFARE_CMD_AUTH_A = 0x60;
|
static const uint8_t MIFARE_CMD_AUTH_A = 0x60;
|
||||||
static const uint8_t MIFARE_CMD_AUTH_B = 0x61;
|
static const uint8_t MIFARE_CMD_AUTH_B = 0x61;
|
||||||
|
static const uint8_t MIFARE_CMD_HALT = 0x50;
|
||||||
static const uint8_t MIFARE_CMD_READ = 0x30;
|
static const uint8_t MIFARE_CMD_READ = 0x30;
|
||||||
static const uint8_t MIFARE_CMD_WRITE = 0xA0;
|
static const uint8_t MIFARE_CMD_WRITE = 0xA0;
|
||||||
static const uint8_t MIFARE_CMD_WRITE_ULTRALIGHT = 0xA2;
|
static const uint8_t MIFARE_CMD_WRITE_ULTRALIGHT = 0xA2;
|
||||||
|
|
||||||
|
// Mifare Ack/Nak
|
||||||
|
static const uint8_t MIFARE_CMD_ACK = 0x0A;
|
||||||
|
static const uint8_t MIFARE_CMD_NAK_INVALID_XFER_BUFF_VALID = 0x00;
|
||||||
|
static const uint8_t MIFARE_CMD_NAK_CRC_ERROR_XFER_BUFF_VALID = 0x01;
|
||||||
|
static const uint8_t MIFARE_CMD_NAK_INVALID_XFER_BUFF_INVALID = 0x04;
|
||||||
|
static const uint8_t MIFARE_CMD_NAK_CRC_ERROR_XFER_BUFF_INVALID = 0x05;
|
||||||
|
|
||||||
static const char *const MIFARE_CLASSIC = "Mifare Classic";
|
static const char *const MIFARE_CLASSIC = "Mifare Classic";
|
||||||
static const char *const NFC_FORUM_TYPE_2 = "NFC Forum Type 2";
|
static const char *const NFC_FORUM_TYPE_2 = "NFC Forum Type 2";
|
||||||
static const char *const ERROR = "Error";
|
static const char *const ERROR = "Error";
|
||||||
|
|
|
@ -52,7 +52,13 @@ std::unique_ptr<nfc::NfcTag> PN532::read_mifare_classic_tag_(std::vector<uint8_t
|
||||||
current_block++;
|
current_block++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
|
|
||||||
|
if (buffer.begin() + message_start_index < buffer.end()) {
|
||||||
|
buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
|
||||||
|
} else {
|
||||||
|
return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
|
||||||
|
}
|
||||||
|
|
||||||
return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
|
return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue