nfc: Implement UID filter

This commit is contained in:
GaryOderNichts 2024-05-17 14:19:51 +02:00
parent 8e8431113a
commit 41fe598e33

View File

@ -41,6 +41,10 @@ namespace nfc
uint32 nfcStatus; uint32 nfcStatus;
std::chrono::time_point<std::chrono::system_clock> touchTime; std::chrono::time_point<std::chrono::system_clock> touchTime;
std::chrono::time_point<std::chrono::system_clock> discoveryTimeout; std::chrono::time_point<std::chrono::system_clock> discoveryTimeout;
struct {
NFCUid uid;
NFCUid mask;
} filter;
MPTR tagDetectCallback; MPTR tagDetectCallback;
void* tagDetectContext; void* tagDetectContext;
@ -124,6 +128,19 @@ namespace nfc
return gNFCContexts[chan].isInitialized; return gNFCContexts[chan].isInitialized;
} }
bool __NFCCompareUid(NFCUid* uid, NFCUid* filterUid, NFCUid* filterMask)
{
for (int i = 0; i < sizeof(uid->uid); i++)
{
if ((uid->uid[i] & filterMask->uid[i]) != filterUid->uid[i])
{
return false;
}
}
return true;
}
void __NFCHandleRead(uint32 chan) void __NFCHandleRead(uint32 chan)
{ {
NFCContext* ctx = &gNFCContexts[chan]; NFCContext* ctx = &gNFCContexts[chan];
@ -139,6 +156,10 @@ namespace nfc
StackAllocator<uint8_t, 0x200> lockedData; StackAllocator<uint8_t, 0x200> lockedData;
if (ctx->tag) if (ctx->tag)
{
// Compare UID
memcpy(uid.GetPointer(), ctx->tag->GetUIDBlock().data(), sizeof(NFCUid));
if (__NFCCompareUid(uid.GetPointer(), &ctx->filter.uid, &ctx->filter.mask))
{ {
// Try to parse ndef message // Try to parse ndef message
auto ndefMsg = ndef::Message::FromBytes(ctx->tag->GetNDEFData()); auto ndefMsg = ndef::Message::FromBytes(ctx->tag->GetNDEFData());
@ -162,9 +183,6 @@ namespace nfc
lockedDataSize = ctx->tag->GetLockedArea().size(); lockedDataSize = ctx->tag->GetLockedArea().size();
memcpy(lockedData.GetPointer(), ctx->tag->GetLockedArea().data(), lockedDataSize); memcpy(lockedData.GetPointer(), ctx->tag->GetLockedArea().data(), lockedDataSize);
// Fill in uid
memcpy(uid.GetPointer(), ctx->tag->GetUIDBlock().data(), sizeof(NFCUid));
result = 0; result = 0;
} }
else else
@ -178,6 +196,11 @@ namespace nfc
} }
} }
else else
{
result = -0x1F6;
}
}
else
{ {
result = -0x1DD; result = -0x1DD;
} }
@ -194,6 +217,10 @@ namespace nfc
sint32 result; sint32 result;
if (ctx->tag) if (ctx->tag)
{
NFCUid uid;
memcpy(&uid, ctx->tag->GetUIDBlock().data(), sizeof(NFCUid));
if (__NFCCompareUid(&uid, &ctx->filter.uid, &ctx->filter.mask))
{ {
// Update tag NDEF data // Update tag NDEF data
ctx->tag->SetNDEFData(ctx->writeMessage.ToBytes()); ctx->tag->SetNDEFData(ctx->writeMessage.ToBytes());
@ -222,6 +249,11 @@ namespace nfc
} }
} }
else else
{
result = -0x2F6;
}
}
else
{ {
result = -0x2DD; result = -0x2DD;
} }
@ -548,7 +580,8 @@ namespace nfc
ctx->discoveryTimeout = std::chrono::system_clock::now() + std::chrono::milliseconds(discoveryTimeout); ctx->discoveryTimeout = std::chrono::system_clock::now() + std::chrono::milliseconds(discoveryTimeout);
} }
// TODO uid filter? memcpy(&ctx->filter.uid, uid, sizeof(*uid));
memcpy(&ctx->filter.mask, uidMask, sizeof(*uidMask));
return 0; return 0;
} }
@ -598,7 +631,8 @@ namespace nfc
ctx->discoveryTimeout = std::chrono::system_clock::now() + std::chrono::milliseconds(discoveryTimeout); ctx->discoveryTimeout = std::chrono::system_clock::now() + std::chrono::milliseconds(discoveryTimeout);
} }
// TODO uid filter? memcpy(&ctx->filter.uid, uid, sizeof(*uid));
memcpy(&ctx->filter.mask, uidMask, sizeof(*uidMask));
return 0; return 0;
} }