Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool MFRC522::MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors) {
  2.    
  3.     // UID + BCC byte can not be larger than 16 together
  4.     if (!newUid || !uidSize || uidSize > 15) {
  5.         if (logErrors) {
  6.             Serial.println(F("New UID buffer empty, size 0, or size > 15 given"));
  7.         }
  8.         return false;
  9.     }
  10.    
  11.     // Authenticate for reading
  12.     MIFARE_Key key = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  13.     MFRC522::StatusCode status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid);
  14.     if (status != STATUS_OK) {
  15.        
  16.         if (status == STATUS_TIMEOUT) {
  17.             // We get a read timeout if no card is selected yet, so let's select one
  18.            
  19.             // Wake the card up again if sleeping
  20. //            byte atqa_answer[2];
  21. //            byte atqa_size = 2;
  22. //            PICC_WakeupA(atqa_answer, &atqa_size);
  23.            
  24.             if (!PICC_IsNewCardPresent() || !PICC_ReadCardSerial()) {
  25.                 Serial.println(F("No card was previously selected, and none are available. Failed to set UID."));
  26.                 return false;
  27.             }
  28.            
  29.             status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid);
  30.             if (status != STATUS_OK) {
  31.                 // We tried, time to give up
  32.                 if (logErrors) {
  33.                     Serial.println(F("Failed to authenticate to card for reading, could not set UID: "));
  34.                     Serial.println(GetStatusCodeName(status));
  35.                 }
  36.                 return false;
  37.             }
  38.         }
  39.         else {
  40.             if (logErrors) {
  41.                 Serial.print(F("PCD_Authenticate() failed: "));
  42.                 Serial.println(GetStatusCodeName(status));
  43.             }
  44.             return false;
  45.         }
  46.     }
  47.    
  48.     // Read block 0
  49.     byte block0_buffer[18];
  50.     byte byteCount = sizeof(block0_buffer);
  51.     status = MIFARE_Read((byte)0, block0_buffer, &byteCount);
  52.     if (status != STATUS_OK) {
  53.         if (logErrors) {
  54.             Serial.print(F("MIFARE_Read() failed: "));
  55.             Serial.println(GetStatusCodeName(status));
  56.             Serial.println(F("Are you sure your KEY A for sector 0 is 0xFFFFFFFFFFFF?"));
  57.         }
  58.         return false;
  59.     }
  60.    
  61.     // Write new UID to the data we just read, and calculate BCC byte
  62.     byte bcc = 0;
  63.     for (int i = 0; i < uidSize; i++) {
  64.         block0_buffer[i] = newUid[i];
  65.         bcc ^= newUid[i];
  66.     }
  67.    
  68.     // Write BCC byte to buffer
  69.     block0_buffer[uidSize] = bcc;
  70.    
  71.     // Stop encrypted traffic so we can send raw bytes
  72.     PCD_StopCrypto1();
  73.    
  74.     // Activate UID backdoor
  75.     if (!MIFARE_OpenUidBackdoor(logErrors)) {
  76.         if (logErrors) {
  77.             Serial.println(F("Activating the UID backdoor failed."));
  78.         }
  79.         return false;
  80.     }
  81.    
  82.     // Write modified block 0 back to card
  83.     status = MIFARE_Write((byte)0, block0_buffer, (byte)16);
  84.     if (status != STATUS_OK) {
  85.         if (logErrors) {
  86.             Serial.print(F("MIFARE_Write() failed: "));
  87.             Serial.println(GetStatusCodeName(status));
  88.         }
  89.         return false;
  90.     }
  91.    
  92.     // Wake the card up again
  93.     byte atqa_answer[2];
  94.     byte atqa_size = 2;
  95.     PICC_WakeupA(atqa_answer, &atqa_size);
  96.    
  97.     return true;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement