nfc-tools / libfreefare

A convenience API for NFC cards manipulations on top of libnfc.
Other
402 stars 105 forks source link

libnfc.driver.acr122_usb: Command Code verification failed when calling mifare_classic_write() #127

Closed KyryloAntoshyn closed 4 years ago

KyryloAntoshyn commented 4 years ago

Hi, I am working with Mifare Classic NFC card using your library. My application polls for cards (I use the same logic you implemented in nfc-poll utility) and when the card is polled I wait until its presence on the reader using nfc_initiator_target_is_present() function in another thread. During this process user can read and write to card using mifare_classic_read() mifare_classic_write() functions.

However when I try to do this I am getting the error below. image

I tried to comment the code which always checks nfc_initiator_target_is_present() and it works fine now, but I need to know whether the card is inserted and ejected. I can infer that the problem is connected with infinite checking with nfc_initiator_target_is_present(). Is it possible to implement such logic: when we poll and check for card in another thread and read/write to card from the main thread? How to fix my error?

Here I wait for the card presence:

_isTagPresent = true;
while (SharpNFC.PInvoke.Functions.nfc_initiator_target_is_present(pnd, IntPtr.Zero) == 0) { }
_isTagPresent = false;

Here I try to write to card and the error is raised (before writing I connected to card and authenticated to needed blocks successfully):

if (SharpFreeFare.Functions.mifare_classic_write(freefareTag, blockNumber, data) == 0)
{
    Console.WriteLine($"Successfully wrote to {blockNumber} block");
    return true;
}
else
{
    Console.WriteLine($"Couldn't wrote to {blockNumber} block");
    return false;
}

Thanks in advance!

KyryloAntoshyn commented 4 years ago

Fixed this by implementing thread-safe queue that invokes actions.