style improved
This commit is contained in:
60
MFRC522.cpp
60
MFRC522.cpp
@@ -585,7 +585,7 @@ byte MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally outp
|
|||||||
|
|
||||||
// Repeat Cascade Level loop until we have a complete UID.
|
// Repeat Cascade Level loop until we have a complete UID.
|
||||||
uidComplete = false;
|
uidComplete = false;
|
||||||
while ( ! uidComplete) {
|
while (!uidComplete) {
|
||||||
// Set the Cascade Level in the SEL byte, find out if we need to use the Cascade Tag in byte 2.
|
// Set the Cascade Level in the SEL byte, find out if we need to use the Cascade Tag in byte 2.
|
||||||
switch (cascadeLevel) {
|
switch (cascadeLevel) {
|
||||||
case 1:
|
case 1:
|
||||||
@@ -638,7 +638,7 @@ byte MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally outp
|
|||||||
|
|
||||||
// Repeat anti collision loop until we can transmit all UID bits + BCC and receive a SAK - max 32 iterations.
|
// Repeat anti collision loop until we can transmit all UID bits + BCC and receive a SAK - max 32 iterations.
|
||||||
selectDone = false;
|
selectDone = false;
|
||||||
while ( ! selectDone) {
|
while (!selectDone) {
|
||||||
// Find out how many bits and bytes to send and receive.
|
// Find out how many bits and bytes to send and receive.
|
||||||
if (currentLevelKnownBits >= 32) { // All UID bits in this Cascade Level are known. This is a SELECT.
|
if (currentLevelKnownBits >= 32) { // All UID bits in this Cascade Level are known. This is a SELECT.
|
||||||
//Serial.print("SELECT: currentLevelKnownBits="); Serial.println(currentLevelKnownBits, DEC);
|
//Serial.print("SELECT: currentLevelKnownBits="); Serial.println(currentLevelKnownBits, DEC);
|
||||||
@@ -706,7 +706,7 @@ byte MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally outp
|
|||||||
// Run loop again to do the SELECT.
|
// Run loop again to do the SELECT.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // End of while ( ! selectDone)
|
} // End of while (!selectDone)
|
||||||
|
|
||||||
// We do not check the CBB - it was constructed by us above.
|
// We do not check the CBB - it was constructed by us above.
|
||||||
|
|
||||||
@@ -736,7 +736,7 @@ byte MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally outp
|
|||||||
uidComplete = true;
|
uidComplete = true;
|
||||||
uid->sak = responseBuffer[0];
|
uid->sak = responseBuffer[0];
|
||||||
}
|
}
|
||||||
} // End of while ( ! uidComplete)
|
} // End of while (!uidComplete)
|
||||||
|
|
||||||
// Set correct uid->size
|
// Set correct uid->size
|
||||||
uid->size = 3 * cascadeLevel + 1;
|
uid->size = 3 * cascadeLevel + 1;
|
||||||
@@ -898,7 +898,7 @@ byte MFRC522::MIFARE_Write( byte blockAddr, ///< MIFARE Classic: The block (0-0x
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Transfer the data
|
// Step 2: Transfer the data
|
||||||
result = PCD_MIFARE_Transceive( buffer, bufferSize); // Adds CRC_A and checks that the response is MF_ACK.
|
result = PCD_MIFARE_Transceive(buffer, bufferSize); // Adds CRC_A and checks that the response is MF_ACK.
|
||||||
if (result != STATUS_OK) {
|
if (result != STATUS_OK) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1526,16 +1526,16 @@ bool MFRC522::MIFARE_OpenUidBackdoor(bool logErrors) {
|
|||||||
byte response[32]; // Card's response is written here
|
byte response[32]; // Card's response is written here
|
||||||
byte received;
|
byte received;
|
||||||
byte status = PCD_TransceiveData(&cmd, (byte)1, response, &received, &validBits, (byte)0, false); // 40
|
byte status = PCD_TransceiveData(&cmd, (byte)1, response, &received, &validBits, (byte)0, false); // 40
|
||||||
if( status != STATUS_OK ) {
|
if(status != STATUS_OK) {
|
||||||
if( logErrors ) {
|
if(logErrors) {
|
||||||
Serial.println(F("Card did not respond to 0x40 after HALT command. Are you sure it is a UID changeable one?"));
|
Serial.println(F("Card did not respond to 0x40 after HALT command. Are you sure it is a UID changeable one?"));
|
||||||
Serial.print(F("Error name: "));
|
Serial.print(F("Error name: "));
|
||||||
Serial.println(GetStatusCodeName(status));
|
Serial.println(GetStatusCodeName(status));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ( received != 1 || response[0] != 0x0A ) {
|
if (received != 1 || response[0] != 0x0A) {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.print(F("Got bad response on backdoor 0x40 command: "));
|
Serial.print(F("Got bad response on backdoor 0x40 command: "));
|
||||||
Serial.print(response[0], HEX);
|
Serial.print(response[0], HEX);
|
||||||
Serial.print(F(" ("));
|
Serial.print(F(" ("));
|
||||||
@@ -1548,16 +1548,16 @@ bool MFRC522::MIFARE_OpenUidBackdoor(bool logErrors) {
|
|||||||
cmd = 0x43;
|
cmd = 0x43;
|
||||||
validBits = 8;
|
validBits = 8;
|
||||||
status = PCD_TransceiveData(&cmd, (byte)1, response, &received, &validBits, (byte)0, false); // 43
|
status = PCD_TransceiveData(&cmd, (byte)1, response, &received, &validBits, (byte)0, false); // 43
|
||||||
if( status != STATUS_OK ) {
|
if(status != STATUS_OK) {
|
||||||
if( logErrors ) {
|
if(logErrors) {
|
||||||
Serial.println(F("Error in communication at command 0x43, after successfully executing 0x40"));
|
Serial.println(F("Error in communication at command 0x43, after successfully executing 0x40"));
|
||||||
Serial.print(F("Error name: "));
|
Serial.print(F("Error name: "));
|
||||||
Serial.println(GetStatusCodeName(status));
|
Serial.println(GetStatusCodeName(status));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ( received != 1 || response[0] != 0x0A ) {
|
if (received != 1 || response[0] != 0x0A) {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.print(F("Got bad response on backdoor 0x43 command: "));
|
Serial.print(F("Got bad response on backdoor 0x43 command: "));
|
||||||
Serial.print(response[0], HEX);
|
Serial.print(response[0], HEX);
|
||||||
Serial.print(F(" ("));
|
Serial.print(F(" ("));
|
||||||
@@ -1579,11 +1579,11 @@ bool MFRC522::MIFARE_OpenUidBackdoor(bool logErrors) {
|
|||||||
* It assumes a default KEY A of 0xFFFFFFFFFFFF.
|
* It assumes a default KEY A of 0xFFFFFFFFFFFF.
|
||||||
* Make sure to have selected the card before this function is called.
|
* Make sure to have selected the card before this function is called.
|
||||||
*/
|
*/
|
||||||
bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
bool MFRC522::MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors) {
|
||||||
|
|
||||||
// UID + BCC byte can not be larger than 16 together
|
// UID + BCC byte can not be larger than 16 together
|
||||||
if ( !newUid || !uidSize || uidSize > 15) {
|
if (!newUid || !uidSize || uidSize > 15) {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.println(F("New UID buffer empty, size 0, or size > 15 given"));
|
Serial.println(F("New UID buffer empty, size 0, or size > 15 given"));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -1592,9 +1592,9 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
// Authenticate for reading
|
// Authenticate for reading
|
||||||
MIFARE_Key key = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
MIFARE_Key key = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||||
byte status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid);
|
byte status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid);
|
||||||
if ( status != STATUS_OK ) {
|
if (status != STATUS_OK) {
|
||||||
|
|
||||||
if ( status == STATUS_TIMEOUT ) {
|
if (status == STATUS_TIMEOUT) {
|
||||||
// We get a read timeout if no card is selected yet, so let's select one
|
// We get a read timeout if no card is selected yet, so let's select one
|
||||||
|
|
||||||
// Wake the card up again if sleeping
|
// Wake the card up again if sleeping
|
||||||
@@ -1602,15 +1602,15 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
// byte atqa_size = 2;
|
// byte atqa_size = 2;
|
||||||
// PICC_WakeupA(atqa_answer, &atqa_size);
|
// PICC_WakeupA(atqa_answer, &atqa_size);
|
||||||
|
|
||||||
if ( !PICC_IsNewCardPresent() || !PICC_ReadCardSerial() ) {
|
if (!PICC_IsNewCardPresent() || !PICC_ReadCardSerial()) {
|
||||||
Serial.println(F("No card was previously selected, and none are available. Failed to set UID."));
|
Serial.println(F("No card was previously selected, and none are available. Failed to set UID."));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid);
|
status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid);
|
||||||
if ( status != STATUS_OK ) {
|
if (status != STATUS_OK) {
|
||||||
// We tried, time to give up
|
// We tried, time to give up
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.println(F("Failed to authenticate to card for reading, could not set UID: "));
|
Serial.println(F("Failed to authenticate to card for reading, could not set UID: "));
|
||||||
Serial.println(GetStatusCodeName(status));
|
Serial.println(GetStatusCodeName(status));
|
||||||
}
|
}
|
||||||
@@ -1618,7 +1618,7 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.print(F("PCD_Authenticate() failed: "));
|
Serial.print(F("PCD_Authenticate() failed: "));
|
||||||
Serial.println(GetStatusCodeName(status));
|
Serial.println(GetStatusCodeName(status));
|
||||||
}
|
}
|
||||||
@@ -1630,8 +1630,8 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
byte block0_buffer[18];
|
byte block0_buffer[18];
|
||||||
byte byteCount = sizeof(block0_buffer);
|
byte byteCount = sizeof(block0_buffer);
|
||||||
status = MIFARE_Read((byte)0, block0_buffer, &byteCount);
|
status = MIFARE_Read((byte)0, block0_buffer, &byteCount);
|
||||||
if ( status != STATUS_OK ) {
|
if (status != STATUS_OK) {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.print(F("MIFARE_Read() failed: "));
|
Serial.print(F("MIFARE_Read() failed: "));
|
||||||
Serial.println(GetStatusCodeName(status));
|
Serial.println(GetStatusCodeName(status));
|
||||||
Serial.println(F("Are you sure your KEY A for sector 0 is 0xFFFFFFFFFFFF?"));
|
Serial.println(F("Are you sure your KEY A for sector 0 is 0xFFFFFFFFFFFF?"));
|
||||||
@@ -1641,7 +1641,7 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
|
|
||||||
// Write new UID to the data we just read, and calculate BCC byte
|
// Write new UID to the data we just read, and calculate BCC byte
|
||||||
byte bcc = 0;
|
byte bcc = 0;
|
||||||
for ( int i = 0; i < uidSize; i++ ) {
|
for (int i = 0; i < uidSize; i++) {
|
||||||
block0_buffer[i] = newUid[i];
|
block0_buffer[i] = newUid[i];
|
||||||
bcc ^= newUid[i];
|
bcc ^= newUid[i];
|
||||||
}
|
}
|
||||||
@@ -1653,8 +1653,8 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
PCD_StopCrypto1();
|
PCD_StopCrypto1();
|
||||||
|
|
||||||
// Activate UID backdoor
|
// Activate UID backdoor
|
||||||
if ( !MIFARE_OpenUidBackdoor(logErrors) ) {
|
if (!MIFARE_OpenUidBackdoor(logErrors)) {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.println(F("Activating the UID backdoor failed."));
|
Serial.println(F("Activating the UID backdoor failed."));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -1663,7 +1663,7 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
// Write modified block 0 back to card
|
// Write modified block 0 back to card
|
||||||
status = MIFARE_Write((byte)0, block0_buffer, (byte)16);
|
status = MIFARE_Write((byte)0, block0_buffer, (byte)16);
|
||||||
if (status != STATUS_OK) {
|
if (status != STATUS_OK) {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.print(F("MIFARE_Write() failed: "));
|
Serial.print(F("MIFARE_Write() failed: "));
|
||||||
Serial.println(GetStatusCodeName(status));
|
Serial.println(GetStatusCodeName(status));
|
||||||
}
|
}
|
||||||
@@ -1682,14 +1682,14 @@ bool MFRC522::MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors) {
|
|||||||
* Resets entire sector 0 to zeroes, so the card can be read again by readers.
|
* Resets entire sector 0 to zeroes, so the card can be read again by readers.
|
||||||
*/
|
*/
|
||||||
bool MFRC522::MIFARE_UnbrickUidSector(bool logErrors) {
|
bool MFRC522::MIFARE_UnbrickUidSector(bool logErrors) {
|
||||||
MIFARE_OpenUidBackdoor( logErrors );
|
MIFARE_OpenUidBackdoor(logErrors);
|
||||||
|
|
||||||
byte block0_buffer[] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
byte block0_buffer[] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||||
|
|
||||||
// Write modified block 0 back to card
|
// Write modified block 0 back to card
|
||||||
byte status = MIFARE_Write((byte)0, block0_buffer, (byte)16);
|
byte status = MIFARE_Write((byte)0, block0_buffer, (byte)16);
|
||||||
if (status != STATUS_OK) {
|
if (status != STATUS_OK) {
|
||||||
if ( logErrors ) {
|
if (logErrors) {
|
||||||
Serial.print(F("MIFARE_Write() failed: "));
|
Serial.print(F("MIFARE_Write() failed: "));
|
||||||
Serial.println(GetStatusCodeName(status));
|
Serial.println(GetStatusCodeName(status));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,7 +360,7 @@ public:
|
|||||||
void PICC_DumpMifareUltralightToSerial();
|
void PICC_DumpMifareUltralightToSerial();
|
||||||
void MIFARE_SetAccessBits(byte *accessBitBuffer, byte g0, byte g1, byte g2, byte g3);
|
void MIFARE_SetAccessBits(byte *accessBitBuffer, byte g0, byte g1, byte g2, byte g3);
|
||||||
bool MIFARE_OpenUidBackdoor(bool logErrors);
|
bool MIFARE_OpenUidBackdoor(bool logErrors);
|
||||||
bool MIFARE_SetUid(byte* newUid, byte uidSize, bool logErrors);
|
bool MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors);
|
||||||
bool MIFARE_UnbrickUidSector(bool logErrors);
|
bool MIFARE_UnbrickUidSector(bool logErrors);
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user