9 static const char *
const TAG =
"pn532.mifare_classic";
12 uint8_t current_block = 4;
13 uint8_t message_start_index = 0;
14 uint32_t message_length = 0;
17 std::vector<uint8_t> data;
20 return make_unique<nfc::NfcTag>(uid, nfc::ERROR);
23 ESP_LOGE(TAG,
"Failed to read block %d", current_block);
24 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
27 ESP_LOGV(TAG,
"Tag is not NDEF formatted");
28 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
33 std::vector<uint8_t> buffer;
35 while (index < buffer_size) {
38 ESP_LOGE(TAG,
"Error, Block authentication failed for %d", current_block);
41 std::vector<uint8_t> block_data;
43 buffer.insert(buffer.end(), block_data.begin(), block_data.end());
45 ESP_LOGE(TAG,
"Error reading block %d", current_block);
48 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
55 buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
56 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
61 PN532_COMMAND_INDATAEXCHANGE,
69 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, data) || data[0] != 0x00) {
72 data.erase(data.begin());
80 std::vector<uint8_t> data({
81 PN532_COMMAND_INDATAEXCHANGE,
86 data.insert(data.end(), key, key + 6);
87 data.insert(data.end(), uid.begin(), uid.end());
89 ESP_LOGE(TAG,
"Authentication failed - Block %d", block_num);
93 std::vector<uint8_t> response;
94 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) {
95 ESP_LOGE(TAG,
"Authentication failed - Block 0x%02x", block_num);
103 std::vector<uint8_t> blank_buffer(
104 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
105 std::vector<uint8_t> trailer_buffer(
106 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
110 for (
int block = 0; block < 64; block += 4) {
116 ESP_LOGE(TAG,
"Unable to write block %d", block);
121 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
125 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
129 ESP_LOGE(TAG,
"Unable to write block %d", block + 3);
138 std::vector<uint8_t> empty_ndef_message(
139 {0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
140 std::vector<uint8_t> blank_block(
141 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
142 std::vector<uint8_t> block_1_data(
143 {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
144 std::vector<uint8_t> block_2_data(
145 {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
146 std::vector<uint8_t> block_3_trailer(
147 {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
148 std::vector<uint8_t> ndef_trailer(
149 {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
152 ESP_LOGE(TAG,
"Unable to authenticate block 0 for formatting!");
162 ESP_LOGD(TAG,
"Sector 0 formatted to NDEF");
164 for (
int block = 4; block < 64; block += 4) {
170 ESP_LOGE(TAG,
"Unable to write block %d", block);
174 ESP_LOGE(TAG,
"Unable to write block %d", block);
178 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
181 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
184 ESP_LOGE(TAG,
"Unable to write trailer block %d", block + 3);
191 std::vector<uint8_t> data({
192 PN532_COMMAND_INDATAEXCHANGE,
194 nfc::MIFARE_CMD_WRITE,
197 data.insert(data.end(), write_data.begin(), write_data.end());
199 ESP_LOGE(TAG,
"Error writing block %d", block_num);
203 std::vector<uint8_t> response;
204 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
205 ESP_LOGE(TAG,
"Error writing block %d", block_num);
213 auto encoded = message->
encode();
215 uint32_t message_length = encoded.size();
218 encoded.insert(encoded.begin(), 0x03);
219 if (message_length < 255) {
220 encoded.insert(encoded.begin() + 1, message_length);
222 encoded.insert(encoded.begin() + 1, 0xFF);
223 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
224 encoded.insert(encoded.begin() + 3, message_length & 0xFF);
226 encoded.push_back(0xFE);
228 encoded.resize(buffer_length, 0);
231 uint8_t current_block = 4;
233 while (index < buffer_length) {
240 std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_CLASSIC_BLOCK_SIZE);
244 index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
bool format_mifare_classic_ndef_(std::vector< uint8_t > &uid)
virtual bool write_data(const std::vector< uint8_t > &data)=0
virtual bool read_response(uint8_t command, std::vector< uint8_t > &data)=0
uint32_t get_mifare_classic_buffer_size(uint32_t message_length)
bool mifare_classic_is_trailer_block(uint8_t block_num)
bool decode_mifare_classic_tlv(std::vector< uint8_t > &data, uint32_t &message_length, uint8_t &message_start_index)
bool write_mifare_classic_block_(uint8_t block_num, std::vector< uint8_t > &data)
bool format_mifare_classic_mifare_(std::vector< uint8_t > &uid)
bool auth_mifare_classic_block_(std::vector< uint8_t > &uid, uint8_t block_num, uint8_t key_num, const uint8_t *key)
std::vector< uint8_t > encode()
bool write_mifare_classic_tag_(std::vector< uint8_t > &uid, nfc::NdefMessage *message)
bool mifare_classic_is_first_block(uint8_t block_num)
std::unique_ptr< nfc::NfcTag > read_mifare_classic_tag_(std::vector< uint8_t > &uid)
bool write_command_(const std::vector< uint8_t > &data)
std::string format_bytes(std::vector< uint8_t > &bytes)
bool read_mifare_classic_block_(uint8_t block_num, std::vector< uint8_t > &data)