nfc-tools / libnfc

Platform independent Near Field Communication (NFC) library
http://nfc-tools.org
GNU Lesser General Public License v3.0
1.64k stars 436 forks source link

EIO when send HLTA command #707

Open tinytaro opened 6 months ago

tinytaro commented 6 months ago

Using the nfc_initiator_transceive_bytes function to send HLTA command to the mifare classic tag will fail and report an 'Input / Output Error'.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <nfc/nfc.h>
#include <mifare.h>

int main()
{
    nfc_context* context;
    nfc_device* reader;
    nfc_target card;

    nfc_init(&context);
    reader = nfc_open(context, "pn532_uart:com5");
    nfc_initiator_init(reader);

    nfc_modulation nm = {
        .nmt = NMT_ISO14443A,
        .nbr = NBR_106
    };

    int selected = nfc_initiator_select_passive_target(reader, nm, NULL, 0, &card);
    if (selected > 0)
    {
        mifare_param mp;
        uint8_t cmd[] = {0x50, 0x00};

        memcpy(mp.mpa.abtAuthUid, card.nti.nai.abtUid, 4);
        memset(mp.mpa.abtKey, 0xff, sizeof(mp.mpa.abtKey));

        if (nfc_initiator_mifare_cmd(reader, MC_AUTH_A, 0, &mp) == false)
        {
            printf("auth: %s\n", nfc_strerror(reader));
            return EXIT_FAILURE;
        }

        if (nfc_initiator_transceive_bytes(reader, cmd, sizeof(cmd), NULL, 0, -1) < 0)
        {
            printf("halt: %s\n", nfc_strerror(reader));
            return EXIT_FAILURE;
        }
    }

    nfc_close(reader);
    nfc_exit(context);
    return EXIT_SUCCESS;
}

output:

error   libnfc.driver.pn532_uart    Application level error detected
halt: Input / Output Error