#include #include "pn532.h" #include "esphome/core/log.h" namespace esphome { namespace pn532 { static const char *const TAG = "pn532.mifare_ultralight"; std::unique_ptr PN532::read_mifare_ultralight_tag_(std::vector &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, data)) { return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } if (!this->is_mifare_ultralight_formatted_(data)) { ESP_LOGW(TAG, "Not NDEF formatted"); return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } uint8_t message_length; uint8_t message_start_index; if (!this->find_mifare_ultralight_ndef_(data, message_length, message_start_index)) { ESP_LOGW(TAG, "Couldn't find NDEF message"); return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } ESP_LOGVV(TAG, "NDEF message length: %u, start: %u", message_length, message_start_index); if (message_length == 0) { return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } // we already read pages 3-6 earlier -- pick up where we left off so we're not re-reading pages const uint8_t read_length = message_length + message_start_index > 12 ? message_length + message_start_index - 12 : 0; if (read_length) { if (!read_mifare_ultralight_bytes_(nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE + 3, read_length, data)) { ESP_LOGE(TAG, "Error reading tag data"); return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } } // we need to trim off page 3 as well as any bytes ahead of message_start_index data.erase(data.begin(), data.begin() + message_start_index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE); return make_unique(uid, nfc::NFC_FORUM_TYPE_2, data); } bool PN532::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector &data) { const uint8_t read_increment = nfc::MIFARE_ULTRALIGHT_READ_SIZE * nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; std::vector response; for (uint8_t i = 0; i * read_increment < num_bytes; i++) { if (!this->write_command_({ PN532_COMMAND_INDATAEXCHANGE, 0x01, // One card nfc::MIFARE_CMD_READ, uint8_t(i * nfc::MIFARE_ULTRALIGHT_READ_SIZE + start_page), })) { return false; } if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) { return false; } uint16_t bytes_offset = (i + 1) * read_increment; auto pages_in_end_itr = bytes_offset <= num_bytes ? response.end() : response.end() - (bytes_offset - num_bytes); if ((pages_in_end_itr > response.begin()) && (pages_in_end_itr <= response.end())) { data.insert(data.end(), response.begin() + 1, pages_in_end_itr); } } ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes(data).c_str()); return true; } bool PN532::is_mifare_ultralight_formatted_(const std::vector &page_3_to_6) { const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector return (page_3_to_6.size() > p4_offset + 3) && ((page_3_to_6[p4_offset + 0] != 0xFF) || (page_3_to_6[p4_offset + 1] != 0xFF) || (page_3_to_6[p4_offset + 2] != 0xFF) || (page_3_to_6[p4_offset + 3] != 0xFF)); } uint16_t PN532::read_mifare_ultralight_capacity_() { std::vector data; if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE, data)) { ESP_LOGV(TAG, "Tag capacity is %u bytes", data[2] * 8U); return data[2] * 8U; } return 0; } bool PN532::find_mifare_ultralight_ndef_(const std::vector &page_3_to_6, uint8_t &message_length, uint8_t &message_start_index) { const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector if (!(page_3_to_6.size() > p4_offset + 5)) { return false; } if (page_3_to_6[p4_offset + 0] == 0x03) { message_length = page_3_to_6[p4_offset + 1]; message_start_index = 2; return true; } else if (page_3_to_6[p4_offset + 5] == 0x03) { message_length = page_3_to_6[p4_offset + 6]; message_start_index = 7; return true; } return false; } bool PN532::write_mifare_ultralight_tag_(std::vector &uid, nfc::NdefMessage *message) { uint32_t capacity = this->read_mifare_ultralight_capacity_(); auto encoded = message->encode(); uint32_t message_length = encoded.size(); uint32_t buffer_length = nfc::get_mifare_ultralight_buffer_size(message_length); if (buffer_length > capacity) { ESP_LOGE(TAG, "Message length exceeds tag capacity %" PRIu32 " > %" PRIu32, buffer_length, capacity); return false; } encoded.insert(encoded.begin(), 0x03); if (message_length < 255) { encoded.insert(encoded.begin() + 1, message_length); } else { encoded.insert(encoded.begin() + 1, 0xFF); encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF); encoded.insert(encoded.begin() + 2, message_length & 0xFF); } encoded.push_back(0xFE); encoded.resize(buffer_length, 0); uint32_t index = 0; 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)) { return false; } index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; current_page++; } return true; } 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}; for (int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) { if (!this->write_mifare_ultralight_page_(i, blank_data)) { return false; } } return true; } bool PN532::write_mifare_ultralight_page_(uint8_t page_num, std::vector &write_data) { std::vector data({ 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)) { ESP_LOGE(TAG, "Error writing page %u", page_num); return false; } std::vector response; if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response)) { ESP_LOGE(TAG, "Error writing page %u", page_num); return false; } return true; } } // namespace pn532 } // namespace esphome