Grabba Driver for Android
Unified driver for Grabba devices on the Android operating system
ResponseAPDU Class Reference

Description

Encapsulation of a smartcard response Application Protocol Data Unit (APDU)

The response APDU is the communication message sent from a smartcard to a card reader. This class encapsulates a response APDU, ensuring its validity at construction-time, and guaranteeing it remains valid for the lifetime of the object. The get() method provides read-only access to the raw APDU data.

Additional information may be found at: https://en.wikipedia.org/wiki/Smart_card_application_protocol_data_unit

The full specification of the format is defined in ISO/IEC 7816-3 and ISO/IEC 7816-4.

Invariant
Stored fields are all within range for the APDU format.

Thread safety: This class is immutable, and thus fully thread-safe post-construction.

See also
CommandAPDU for the command equivalent, and SmartcardAPI for related functionality

Public Member Functions

 ResponseAPDU ()
 Construct an empty APDU - no response data, and status set to 0x6F00. More...
 
 ResponseAPDU (@NonNull byte[] data, int SW1, int SW2)
 Create an APDU containing given data and status values. More...
 
 ResponseAPDU (@NonNull byte[] stream)
 Extract APDU data from a byte stream. More...
 
byte [] get ()
 Copy the entire APDU in byte-array format. More...
 
String hexDump ()
 Obtain a hex dump of the entire APDU. More...
 
byte [] responseData ()
 Copy the response data field, if present. More...
 
int responseDataSize ()
 Query the size in bytes of the response (excluding SW1 and SW2)
 
String statusString ()
 Description of the status value stored in fields SW1 and SW2. More...
 
boolean successComplete ()
 Return whether the response indicates successful processing of the command with no further data remaining. More...
 
boolean successIncomplete ()
 Return whether the response indicates successful processing of the command, but there is more data remaining. More...
 
short SW1 ()
 Query the first command processing status byte (SW1) More...
 
short SW2 ()
 Query the second command processing status byte (SW2) More...
 

Static Public Member Functions

static String statusString (int SW1, int SW2)
 String description of a given response status. More...
 

Static Public Attributes

static final int MaxResponseLength
 Maximum permitted response length (excluding status bytes)
 

Constructor & Destructor Documentation

◆ ResponseAPDU() [1/3]

Construct an empty APDU - no response data, and status set to 0x6F00.

This constructor should only be used when it is necessary to return a sentinel value, and that some other indication (e.g. an error code parameter) is supplied to a user to notify them that the value is invalid.

◆ ResponseAPDU() [2/3]

ResponseAPDU ( @NonNull byte []  data,
int  SW1,
int  SW2 
)

Create an APDU containing given data and status values.

Parameters
dataResponse data; length must not exceed MaxResponseLength
SW1First status byte - e.g. 0x90 for success. Valid ranges are 0x61-0x6F and 0x90-0x9F.
SW2Second status byte - e.g. 0x00 for success. Valid range is 0-255.
Exceptions
IllegalArgumentExceptionif SW1 or SW2 are out of range, or if data is too long

◆ ResponseAPDU() [3/3]

ResponseAPDU ( @NonNull byte []  stream)

Extract APDU data from a byte stream.

The last two bytes in the array are used to construct the SW1 and SW2 fields; the remainder is treated as response data.

Parameters
streamAPDU data stream; length must be at least 2 bytes, and at most MaxResponseLength + 2
Exceptions
IllegalArgumentExceptionif data length is illegal, or if the second-last byte holds an invalid value for SW1

Member Function Documentation

◆ get()

byte [] get ( )

Copy the entire APDU in byte-array format.

Returns
Entire APDU, including data, SW1 and SW2 fields

◆ hexDump()

String hexDump ( )

Obtain a hex dump of the entire APDU.

Returns
Hex dump with spaces separating the bytes

◆ responseData()

byte [] responseData ( )

Copy the response data field, if present.

Returns
Response data (length 1-65535 bytes) if present, otherwise a zero-length array

◆ statusString() [1/2]

String statusString ( )

Description of the status value stored in fields SW1 and SW2.

Returns
String containing a textual description of the response status

◆ statusString() [2/2]

static String statusString ( int  SW1,
int  SW2 
)
static

String description of a given response status.

Parameters
SW1First status byte, valid ranges 0x61-0x6F and 0x90-0x9F
SW2Second status byte, valid range 0-255
Returns
String containing a textual description of the status value stored in the supplied parameters
Exceptions
IllegalArgumentExceptionif either of the parameters are out of range

◆ successComplete()

boolean successComplete ( )

Return whether the response indicates successful processing of the command with no further data remaining.

Returns
True for status 0x9000, false otherwise

◆ successIncomplete()

boolean successIncomplete ( )

Return whether the response indicates successful processing of the command, but there is more data remaining.

Returns
True for status 0x6100-0x61FF (inclusive), false otherwise

◆ SW1()

short SW1 ( )

Query the first command processing status byte (SW1)

Returns
SW1 byte - e.g. 0x90 for success with no further data remaining; valid ranges are 0x61-0x6F and 0x90-0x9F inclusive

◆ SW2()

short SW2 ( )

Query the second command processing status byte (SW2)

Returns
SW2 byte - e.g. 0x90 for success with no further data remaining; valid ranges are 0x61-0x6F and 0x90-0x9F inclusive