Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change Mfrc522.ReselectTarget to use WUPA instead of REQA #2061

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/devices/Mfrc522/Mfrc522.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ public bool ListenToCardIso14443TypeA(out Data106kbpsTypeA card, TimeSpan timeou
/// Check if a new card is present.
/// </summary>
/// <param name="atqa">ATQA buffer must be 2 bytes length and will contain the ATQA answer if there is a card.</param>
/// <param name="reselect">true if this is reselecting an existing card (e.g., after a halt)</param>
/// <returns>true if there is a card, else false.</returns>
public bool IsCardPresent(byte[] atqa)
public bool IsCardPresent(byte[] atqa, bool reselect = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: In order to not be binary breaking, any reason why we shouldn't just add new overloads as opposed to using default parameters?

{
if (atqa is not object or { Length: not 2 })
{
Expand All @@ -306,7 +307,7 @@ public bool IsCardPresent(byte[] atqa)

// Switch off the cryptography for Mifare card in case it's on
ClearRegisterBit(Register.Status2, (byte)Status2.MFCrypto1On);
Status sc = PiccRequestA(atqa);
Status sc = PiccRequestA(atqa, reselect ? CardCommand.WupA : CardCommand.ReqA);
if (sc == Status.Collision || sc == Status.Ok)
{
return true;
Expand Down Expand Up @@ -426,15 +427,15 @@ private Status Select(out byte[]? uid, out byte sak)
return Status.Ok;
}

private Status PiccRequestA(byte[] bufferAtqa)
private Status PiccRequestA(byte[] bufferAtqa, CardCommand cardCommand = CardCommand.ReqA)
{
// all received bits will be cleared after a collision
// only used during bitwise anticollision at 106 kBd,
// otherwise it is set to logic 1
ClearRegisterBit(Register.Coll, 0x80);
// Only 7 bits are valid in th ReqA request
byte validBits = 0x07;
Status sc = SendAndReceiveData(MfrcCommand.Transceive, new[] { (byte)CardCommand.ReqA }, bufferAtqa, validBits);
Status sc = SendAndReceiveData(MfrcCommand.Transceive, new[] { (byte)cardCommand }, bufferAtqa, validBits);
if (sc != Status.Ok)
{
return sc;
Expand Down Expand Up @@ -987,8 +988,7 @@ public override bool ReselectTarget(byte targetNumber)
Halt();
// We reselect the card and ignore the target number as reader supports only 1 card
// And we assume here that the card hasn't been changed in the mean time
IsCardPresent(new byte[2]);
return Select(out byte[]? uuid, out byte sak) == Status.Ok;
return IsCardPresent(new byte[2], true) && (Select(out byte[]? uuid, out byte sak) == Status.Ok);
}
}
}
2 changes: 1 addition & 1 deletion src/devices/Mfrc522/MifareCardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal enum CardCommand
{
ReqA = 0x26,
HaltA = 0x50,
RequestAll = 0x52,
WupA = 0x52,
SelectCascadeLevel1 = 0x93, // Used for anticollision as well
SelectCascadeLevel2 = 0x95,
SelectCascadeLevel3 = 0x97
Expand Down