Interface CommandProcessorApi
To be implemented by the plugin.
Exception Handling
All methods may throw exceptions to indicate error conditions. Implementations should distinguish between:
IllegalArgumentException- For invalid parameters that can be validated before hardware accessException- For technical errors during hardware operations (communication failures, authentication failures, etc.)
The exception message should provide clear diagnostic information about the failure cause.
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptionbooleangeneralAuthenticate(int blockAddress, int keyType, int keyNumber) Performs authentication to a contactless card using a previously loaded key.byte[]getUID()Retrieves the Unique Identifier (UID) of the card.voidloadKey(KeyStorageType keyStorageType, int keyNumber, byte[] key) Loads a card-specific authentication key into the reader's memory.byte[]readBlock(int blockAddress, int length) Reads data starting from a specified block address.byte[]transmitIsoApdu(byte[] apdu) Transmits a standard APDU command to the card.voidwriteBlock(int blockAddress, byte[] data) Writes data to a specified memory block on the card.
-
Method Details
-
transmitIsoApdu
Transmits a standard APDU command to the card.- Parameters:
apdu- The APDU ISO 7816-4 command.- Returns:
- The response from the card including both data and status words.
- Throws:
IllegalArgumentException- if apdu is null or invalidException- if transmission fails or communication error occurs- Since:
- 1.0.0
-
getUID
Retrieves the Unique Identifier (UID) of the card.This method obtains the UID which uniquely identifies the card. The UID length varies depending on the card type.
If the UID is not available for the current card type, an empty byte array will be returned.
- Returns:
- A byte array containing the card's UID, or an empty array if the UID is not available.
- Throws:
Exception- if the retrieval operation fails or communication error occurs- Since:
- 1.0.0
-
readBlock
Reads data starting from a specified block address.This method handles reading memory blocks according to the card's memory page structure. The requested length should be a multiple of the card's page size and must not exceed the maximum readable size in a single exchange with the specific card type.
For example, Mifare Ultralight cards have 4-byte pages, but each read command returns 4 pages (16 bytes) at once. ST25/SRT512 cards read one page at a time.
If the requested
lengthis shorter than the actual exchange size with the card, the response will be truncated accordingly.- Parameters:
blockAddress- The address of the first block to be read.length- The address of bytes to read, should be a multiple of the card's page size and must not exceed the maximum readable size for the specific card type.- Returns:
- A byte array containing the block data.
- Throws:
IllegalArgumentException- ifblockAddressis out of range orlengthis negative, not compatible with the card's page structure, or exceeds the maximum readable size.Exception- if the read operation fails or communication error occurs- Since:
- 1.0.0
-
writeBlock
Writes data to a specified memory block on the card.This method handles writing memory blocks according to the card's memory page structure. The data length must exactly match the exchange size for the specific card type.
- Parameters:
blockAddress- The address of the first block to be written.data- The byte array containing the data to write. The length must match exactly the card's page size or exchange size.- Throws:
IllegalArgumentException- ifblockAddressis out of range or ifdatalength does not match the required exchange size for the card type.Exception- if the write operation fails or communication error occurs- Since:
- 1.0.0
-
loadKey
Loads a card-specific authentication key into the reader's memory.This method stores a card key in either volatile (RAM) or non-volatile (EEPROM) memory of the card reader. The key can be subsequently used by the
generalAuthenticate(int, int, int)method to authenticate to the card. This command can be used for all kinds of contactless cards.Volatile memory provides temporary storage that is cleared when the reader loses power, while non-volatile memory persists across power cycles. The availability of non-volatile memory depends on the specific reader hardware.
The key structure and length depend on the card type. For example:
- Mifare cards: 6-byte keys (Type A or Type B)
- Other contactless cards: card-specific key formats
The key number parameter identifies the storage location in the reader's memory. The valid range and meaning of key numbers depend on the reader implementation and whether volatile or non-volatile memory is used. Consult the reader documentation for specific key number assignments.
Note that keys stored in memory cannot be read back for security reasons. Once loaded, they can only be used for authentication operations.
- Parameters:
keyStorageType- The type of memory to store the key in (VOLATILE or NON_VOLATILE).keyNumber- The key index identifying the storage location. Valid ranges depend on the reader implementation and memory type.key- A byte array containing the card-specific key value. Must not be null. The required length depends on the card type and authentication algorithm.- Throws:
IllegalArgumentException- ifkeyStorageTypeorkeyis null, if the key length is not valid for the card type, or ifkeyNumberis not in the valid range for the reader and memory type.Exception- if the load operation fails, if the specified memory type is not available on the reader hardware, or if a communication error occurs.- Since:
- 1.1.0
- See Also:
-
generalAuthenticate
Performs authentication to a contactless card using a previously loaded key.This method authenticates to a specific memory location on the card using a key that was previously loaded into the reader's memory via the
loadKey(KeyStorageType, int, byte[])method. Successful authentication is typically required before performing read or write operations on protected memory areas.The authentication process establishes a secure session between the reader and the card. The block address represents the block number or starting byte number of the card to be authenticated, depending on the card type.
The key type parameter is card-specific and indicates which type of key to use for authentication. Examples:
- Mifare cards: 0x60 (KEY_A) or 0x61 (KEY_B)
- Other contactless cards: card-specific key type values
The key number parameter references a key previously loaded via
loadKey(KeyStorageType, int, byte[])and identifies which stored key to use for this authentication operation.- Parameters:
blockAddress- The block number or starting byte number on the card where authentication is to be performed. Valid range depends on the card type and memory structure.keyType- The type of key to use for authentication. The valid values are card-specific (e.g., 0x60 or 0x61 for Mifare cards).keyNumber- The index of the previously loaded key to use for authentication. Must reference a key that was loaded vialoadKey(KeyStorageType, int, byte[]).- Returns:
trueif authentication succeeded,falseif authentication failed (e.g., incorrect key).- Throws:
IllegalArgumentException- ifblockAddressis out of valid range for the card type, ifkeyTypeis not supported by the card, or ifkeyNumberdoes not reference a valid loaded key.Exception- if the referenced key was not previously loaded, if the card does not support authentication, or if a communication error occurs.- Since:
- 1.1.0
- See Also:
-