0

I will explain my project :

I have my RC522 and a door connected on my Arduino UNO.

I can currently open the door with a MIFARE classic.

But now I want to open it with my Android smartphone, this is why I develop a HCE applet to accept the good APDU with the selected AID, then my phone will transfer the data in order to open the door.

But the problem is :

I don't know how to send an APDU command with my Arduino using the RC522.

Currently, for my MIFARE Cards, I use the https://github.com/miguelbalboa/rfid library.

My test code :

byte selectApdu[] = { 
  0x00, /* CLA */
  0xA4, /* INS */
  0x04, /* P1  */
  0x00, /* P2  */
  0x05, /* Length of AID  */
  0xF2, 0x22, 0x22, 0x22, 0x22,
};
byte * backData = (byte *)malloc(16*sizeof(byte));
byte * dataLen = (byte *)16;

status = mfrc522.PCD_TransceiveData(selectApdu,10,backData,dataLen,NULL,0,false);
if ( status != MFRC522::STATUS_OK) {
    Serial.print(F("PCD_TransceiveData() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
}
else
{
  Serial.println(F("PICC_TransceiveData() success "));
}

Neverless, it doesn't work ( "Timeout in communication"), and I slowly need to think that the RC522 is not compatible...

Romain Henry
  • 1
  • 1
  • 2
  • Please try to improve this question by showing us your best good faith attempt and by asking a much more specific question over just what confuses you. – abarisone Jun 20 '16 at 13:08
  • My question can not be more specific : How to send an APDU Command on Arduino with RC522 ? Thx for help ! :)) – Romain Henry Jun 20 '16 at 15:31
  • I don't know C++ language, is it the same to pass 0 or null to validBits parameter ? It's used as this: byte txLastBits = validBits ? *validBits : 0; – LaurentY Jun 21 '16 at 12:17
  • @LaurentY I tried the both, and nothing works... Are you sure it's possible/compatible to send APDU with RC522 ? – Romain Henry Jun 21 '16 at 12:36

1 Answers1

0

It's an (well commented) open-source project. Just have a look to source code, for instance If you use MIFARE_Read function of MFRC522.cpp

MFRC522::StatusCode MFRC522::MIFARE_Read(   byte blockAddr,     ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from.
                                            byte *buffer,       ///< The buffer to store the data in
                                            byte *bufferSize    ///< Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK.
                                        ) {
    MFRC522::StatusCode result;

    // Sanity check
    if (buffer == NULL || *bufferSize < 18) {
        return STATUS_NO_ROOM;
    }

    // Build command buffer
    buffer[0] = PICC_CMD_MF_READ;
    buffer[1] = blockAddr;
    // Calculate CRC_A
    result = PCD_CalculateCRC(buffer, 2, &buffer[2]);
    if (result != STATUS_OK) {
        return result;
    }

    // Transmit the buffer and receive the response, validate CRC_A.
    return PCD_TransceiveData(buffer, 4, buffer, bufferSize, NULL, 0, true);
} // End MIFARE_Read()

You could see function PCD_TransceiveData is called and check source of this function:

/**
 * Executes the Transceive command.
 * CRC validation can only be done if backData and backLen are specified.
 * 
 * @return STATUS_OK on success, STATUS_??? otherwise.
 */
MFRC522::StatusCode MFRC522::PCD_TransceiveData(    byte *sendData,     ///< Pointer to the data to transfer to the FIFO.
                                                    byte sendLen,       ///< Number of bytes to transfer to the FIFO.
                                                    byte *backData,     ///< NULL or pointer to buffer if data should be read back after executing the command.
                                                    byte *backLen,      ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned.
                                                    byte *validBits,    ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. Default NULL.
                                                    byte rxAlign,       ///< In: Defines the bit position in backData[0] for the first bit received. Default 0.
                                                    bool checkCRC       ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
                                 ) {
    byte waitIRq = 0x30;        // RxIRq and IdleIRq
    return PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, sendData, sendLen, backData, backLen, validBits, rxAlign, checkCRC);
} // End PCD_TransceiveData()

You could called PCD_TransceiveData or PCD_CommunicateWithPICC functions.

UPDATE

You put 0 values for parameters: "backData", "backLen", and "validBits". validBits could be null. backData and backLen must be defined as byte.

UPDATE2

If you have a look at library support protocols It supports ISO/IEC 14443-3 (type A) and not supports ISO/IEC 14443-4 (type B).

If you have a look at Android HCE documentation :

Specifically, Android 4.4 supports emulating cards that are based on the NFC-Forum ISO-DEP specification (based on ISO/IEC 14443-4) and process Application Protocol Data Units (APDUs) as defined in the ISO/IEC 7816-4 specification. Android mandates emulating ISO-DEP only on top of the Nfc-A (ISO/IEC 14443-3 Type A) technology. Support for Nfc-B (ISO/IEC 14443-4 Type B) technology is optional. The layering of all these specifications is shown in the figure 3

In this post: HCE support for ISO/IEC 14443-3 Type B?

Looking at devices in the field, some devices use Type A for HCE and some seem to use Type B. So it's basically the device manufacturer who decides if Type A or Type B is used. The Android API provides no means for the app developer to influence this.

So you have to check with another Android NFC device if your device emulate ISO/IEC 14443-3 (Type A) or ISO/IEC 14443-4 (Type B). You could use NfcTagInfo application on other android device to check this.

Community
  • 1
  • 1
LaurentY
  • 7,495
  • 3
  • 37
  • 55