Skip to content

Commit

Permalink
Ultralight NDEF fixes (dotnet#2060)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbruner authored Mar 30, 2023
1 parent ec9df2a commit 91aa85a
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/devices/Card/Ultralight/UltralightCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,12 @@ public bool WriteNdefMessage(NdefMessage message)

// We need to add 0x03 then the length on 1 or 2 bytes then the trailer 0xFE
int messageLengthBytes = message.Length > 254 ? 3 : 1;
if (messageLengthBytes > NdefCapacity)
Span<byte> serializedMessage = stackalloc byte[message.Length + 2 + messageLengthBytes];
if (serializedMessage.Length > NdefCapacity)
{
return false;
throw new ArgumentOutOfRangeException(nameof(message), $"NDEF message too large, maximum {NdefCapacity} bytes, current size is {serializedMessage.Length} bytes");
}

Span<byte> serializedMessage = stackalloc byte[message.Length + 2 + messageLengthBytes];
message.Serialize(serializedMessage.Slice(1 + messageLengthBytes));
serializedMessage[0] = 0x03;
if (messageLengthBytes == 1)
Expand Down Expand Up @@ -601,7 +601,7 @@ public bool FormatNdef(ReadOnlySpan<byte> authenticationKey = default)
Command = UltralightCommand.Write4Bytes;
Data = new byte[4] { 0xE1, 0x10, (byte)(NdefCapacity / 8), 0x00 };
res = RunUltralightCommand();
if (res <= 0)
if (res < 0)
{
return false;
}
Expand All @@ -613,12 +613,10 @@ public bool FormatNdef(ReadOnlySpan<byte> authenticationKey = default)
}

BlockNumber++;
Data![0] = 0x03; // NDEF start marker
Data[1] = 0x00; // Length of message
Data[2] = 0xFE; // NDEF end marker
Data[3] = 0x00; // Empty
Data = new byte[4] { 0x03, 0x00, 0xFE, 0x00 }; // NDEF start marker, length 0, NDEF end marker, empty
Command = UltralightCommand.Write4Bytes;
res = RunUltralightCommand();
if (res <= 0)
if (res < 0)
{
return false;
}
Expand Down

0 comments on commit 91aa85a

Please sign in to comment.