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;
56 if (buffer.begin() + message_start_index < buffer.end()) {
57 buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
59 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
62 return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
67 PN532_COMMAND_INDATAEXCHANGE,
75 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, data) || data[0] != 0x00) {
78 data.erase(data.begin());
86 std::vector<uint8_t> data({
87 PN532_COMMAND_INDATAEXCHANGE,
92 data.insert(data.end(), key, key + 6);
93 data.insert(data.end(), uid.begin(), uid.end());
95 ESP_LOGE(TAG,
"Authentication failed - Block %d", block_num);
99 std::vector<uint8_t> response;
100 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) {
101 ESP_LOGE(TAG,
"Authentication failed - Block 0x%02x", block_num);
109 std::vector<uint8_t> blank_buffer(
110 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
111 std::vector<uint8_t> trailer_buffer(
112 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
116 for (
int block = 0; block < 64; block += 4) {
122 ESP_LOGE(TAG,
"Unable to write block %d", block);
127 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
131 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
135 ESP_LOGE(TAG,
"Unable to write block %d", block + 3);
144 std::vector<uint8_t> empty_ndef_message(
145 {0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
146 std::vector<uint8_t> blank_block(
147 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
148 std::vector<uint8_t> block_1_data(
149 {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
150 std::vector<uint8_t> block_2_data(
151 {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
152 std::vector<uint8_t> block_3_trailer(
153 {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
154 std::vector<uint8_t> ndef_trailer(
155 {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
158 ESP_LOGE(TAG,
"Unable to authenticate block 0 for formatting!");
168 ESP_LOGD(TAG,
"Sector 0 formatted to NDEF");
170 for (
int block = 4; block < 64; block += 4) {
176 ESP_LOGE(TAG,
"Unable to write block %d", block);
180 ESP_LOGE(TAG,
"Unable to write block %d", block);
184 ESP_LOGE(TAG,
"Unable to write block %d", block + 1);
187 ESP_LOGE(TAG,
"Unable to write block %d", block + 2);
190 ESP_LOGE(TAG,
"Unable to write trailer block %d", block + 3);
197 std::vector<uint8_t> data({
198 PN532_COMMAND_INDATAEXCHANGE,
200 nfc::MIFARE_CMD_WRITE,
203 data.insert(data.end(), write_data.begin(), write_data.end());
205 ESP_LOGE(TAG,
"Error writing block %d", block_num);
209 std::vector<uint8_t> response;
210 if (!this->
read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
211 ESP_LOGE(TAG,
"Error writing block %d", block_num);
219 auto encoded = message->
encode();
221 uint32_t message_length = encoded.size();
224 encoded.insert(encoded.begin(), 0x03);
225 if (message_length < 255) {
226 encoded.insert(encoded.begin() + 1, message_length);
228 encoded.insert(encoded.begin() + 1, 0xFF);
229 encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
230 encoded.insert(encoded.begin() + 3, message_length & 0xFF);
232 encoded.push_back(0xFE);
234 encoded.resize(buffer_length, 0);
237 uint8_t current_block = 4;
239 while (index < buffer_length) {
246 std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_CLASSIC_BLOCK_SIZE);
250 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)
Implementation of SPI Controller mode.
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)