Added reaing of RFID contents.
This commit is contained in:
parent
b9df55012f
commit
15a65f7391
@ -77,61 +77,49 @@ void Controller::handle_rfid() {
|
|||||||
|
|
||||||
String Controller::read_rfid_data() {
|
String Controller::read_rfid_data() {
|
||||||
log_v("read_rfid_data() running...");
|
log_v("read_rfid_data() running...");
|
||||||
static MFRC522::MIFARE_Key keys[8] = {
|
MFRC522::StatusCode status;
|
||||||
{{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}}, // D3 F7 D3 F7 D3 F7
|
|
||||||
{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, // FF FF FF FF FF FF = factory default
|
|
||||||
{{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}}, // A0 A1 A2 A3 A4 A5
|
|
||||||
{{0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}}, // B0 B1 B2 B3 B4 B5
|
|
||||||
{{0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}}, // 4D 3A 99 C3 51 DD
|
|
||||||
{{0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}}, // 1A 98 2C 7E 45 9A
|
|
||||||
{{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}}, // AA BB CC DD EE FF
|
|
||||||
{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}} // 00 00 00 00 00 00
|
|
||||||
};
|
|
||||||
log_d("Trying to read RFID data...");
|
|
||||||
|
|
||||||
String data = "";
|
|
||||||
MFRC522::PICC_Type type = rfid->PICC_GetType(rfid->uid.sak);
|
MFRC522::PICC_Type type = rfid->PICC_GetType(rfid->uid.sak);
|
||||||
|
uint16_t pageStart = 0;
|
||||||
uint8_t sectors = 0;
|
uint16_t pages = 4;
|
||||||
|
uint16_t pageSize = 1;
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case MFRC522Constants::PICC_TYPE_MIFARE_MINI: sectors = 5; break;
|
case MFRC522Constants::PICC_TYPE_MIFARE_MINI:
|
||||||
case MFRC522Constants::PICC_TYPE_MIFARE_1K: sectors = 16; break;
|
case MFRC522Constants::PICC_TYPE_MIFARE_1K:
|
||||||
case MFRC522Constants::PICC_TYPE_MIFARE_4K: sectors = 40; break;
|
case MFRC522Constants::PICC_TYPE_MIFARE_4K: {
|
||||||
default: log_i("Unknown PICC type %s\n", String(MFRC522Debug::PICC_GetTypeName(type)).c_str());
|
log_v("Trying to authenticate Mifare card.");
|
||||||
}
|
MFRC522::MIFARE_Key key = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7};
|
||||||
sectors = 2; // Pretend we have only two sectors, so we read only sector #1.
|
status = rfid->PCD_Authenticate(MFRC522Constants::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &rfid->uid);
|
||||||
for (uint8_t sector=1; sector<sectors; sector++) {
|
|
||||||
uint8_t blocks = (sector < 32) ? 4 : 16;
|
|
||||||
uint8_t block_offset = (sector < 32) ? sector * 4 : 128 + (sector - 32) * 16;
|
|
||||||
|
|
||||||
MFRC522::StatusCode status;
|
|
||||||
|
|
||||||
for (int i=0; i<8; i++) {
|
|
||||||
MFRC522::MIFARE_Key *k = &keys[i];
|
|
||||||
log_v("Trying MIFARE key %02X %02X %02X %02X %02X %02X...\n", k->keyByte[0], k->keyByte[1], k->keyByte[2], k->keyByte[3], k->keyByte[4], k->keyByte[5]);
|
|
||||||
status = rfid->PCD_Authenticate(MFRC522Constants::PICC_CMD_MF_AUTH_KEY_A, block_offset, k, &rfid->uid);
|
|
||||||
if (status == MFRC522Constants::STATUS_OK) {
|
if (status == MFRC522Constants::STATUS_OK) {
|
||||||
log_v("Authentication succeeded with key #%d\n", i);
|
log_v("Authentication succeeded.");
|
||||||
break;
|
} else {
|
||||||
|
log_v("Authentication failed. Trying to read anyway.");
|
||||||
}
|
}
|
||||||
|
pageStart = 4;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
for (uint8_t block=0; block<blocks-1; block++) {
|
case MFRC522Constants::PICC_TYPE_MIFARE_UL:
|
||||||
byte buffer[18];
|
log_v("PICC type is Mifare Ultralight. No authentication necessary.");
|
||||||
uint8_t byte_count = 18;
|
pages = 16;
|
||||||
status = rfid->MIFARE_Read(block_offset + block, buffer, &byte_count);
|
pageSize = 4;
|
||||||
if (status != MFRC522Constants::STATUS_OK) {
|
break;
|
||||||
log_d("MIFARE_Read() failed: %s\n", String(MFRC522Debug::GetStatusCodeName(status)).c_str());
|
default:
|
||||||
continue;
|
log_v("Unexpected rfid card type %s. Trying to read anyway.", MFRC522Debug::PICC_GetTypeName(type));
|
||||||
}
|
}
|
||||||
for (int i=0; i<16; i++) {
|
String data = "";
|
||||||
if (buffer[i]>=0x20 && buffer[i]<0x7F) data.concat((char)buffer[i]);
|
for (uint8_t block=pageStart; block<pages+pageStart; block+=pageSize) {
|
||||||
}
|
byte buffer[18];
|
||||||
|
uint8_t byte_count = 18;
|
||||||
|
status = rfid->MIFARE_Read(block, buffer, &byte_count);
|
||||||
|
if (status != MFRC522Constants::STATUS_OK) {
|
||||||
|
log_d("MIFARE_Read() failed: %s\n", String(MFRC522Debug::GetStatusCodeName(status)).c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int i=0; i<16; i++) {
|
||||||
|
if (buffer[i]>=0x20 && buffer[i]<0x7F) data.concat((char)buffer[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//_rfid->PICC_HaltA();
|
|
||||||
rfid->PCD_StopCrypto1();
|
rfid->PCD_StopCrypto1();
|
||||||
log_d("Data from RFID: %s\n", data.c_str());
|
log_v("Read rfid data: '%s'", data.c_str());
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user