add support for Mifare application directory (resolves #2030) #2067
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add support for the Mifare Application Directory, as described in AN10787.
The application directory occupies sector 0 (blocks 1 and 2) and - for 2K and 4K cards - sector 16 (blocks 64, 65, and 66). The directory is actually a sector map that defines the assignment of sectors to applications. Applications are identified by a 16-bit value (encoded in little-endian order on two bytes).
Previously,
MifareCard.FormatNdef
created the application directory, assigning all sectors to NDEF content. The NDEF content could be read and written withTryReadNdefMessage
andWriteNdefMessage
. However, as noted in #2030, these methods assume that the entire card has been formatted for NDEF, so if the directory was formatted elsewhere and specifies something else, they do not work correctly. In addition, they do not handle the last eight sectors of a 4K card correctly (because those sectors are a different size than the first 32 sectors).This contribution adds a new class
MifareDirectory
to manage the Mifare application directory for aMifareCard
. The new classMifareDirectoryEntry
describes a directory entry (i.e., the assignment of sectors to an application). The new classMifareApplicationIdentifier
represents an application identifier.MifareDirectory.CreateEmpty
creates an emptyMifareDirectory
object for a specifiedMifareCard
.MifareDirectory.LoadFromCard
loads the directory from a specifiedMifareCard
.GetApplications
returns an enumeration ofMifareDirectoryEntry
objects representing the directory contents.TryGetApplication
returns theMifareDirectoryEntry
of a specified application, if it exists. Sectors are allocated to applications withAllocate
and freed withFree
. After the directory has been modified, it must be written back to the card withStoreToCard
.FormatNdef
,IsFormattedNdef
,WriteNdefMessage
, andTryReadNdefMessage
have been reimplemented to use the directory. An overload ofFormatNdef
can specify that only a portion of the card is to be allocated to NDEF content. The remainder is free and can be assigned to other applications. For example, to format 14 sectors for NDEF and assign one sector to an issuer-specific application ID, the following could be used:This was also added to the Pn532 sample code. In addition, some miscellaneous changes were made to both the Pn532 and Mfrc522 sample code (including a workaround for #1869).
Microsoft Reviewers: Open in CodeFlow