public class BasicService extends Object implements Service
Service
interface, and defines a set of helper methods that
manage the APDU buffer to enable co-operation among different Services.
The BasicService
class uses the state of APDU processing to
enforce the validity of the various helper operations. It expects and
maintains the following Common Service Format (CSF) of data in the APDU
Buffer corresponding to the various APDU processing states (See
APDU
):
Init State format of APDU Buffer. This format corresponds to the APDU processing state -STATE_INITIAL
: 0 1 2 3 4 5 <- offset +------------------------------------------------------------+ | CLA | INS | P1 | P2 | P3 | ... Implementation dependent ...| +------------------------------------------------------------+ Input Ready format of APDU Buffer. This format corresponds to the APDU processing state -STATE_FULL_INCOMING
. 0 1 2 3 4 5 <- offset +------------------------------------------------------------+ | CLA | INS | P1 | P2 | Lc | Incoming Data( Lc bytes ) | +------------------------------------------------------------+ Output Ready format of APDU Buffer. This format corresponds to the APDU processing status -STATE_OUTGOING
..STATE_FULL_OUTGOING
0 1 2 3 4 5 <- offset +------------------------------------------------------------+ | CLA | INS | SW1 | SW2 | La | Outgoing Data( La bytes ) | +------------------------------------------------------------+
When the APDU buffer is in the Init and Input Ready formats, the helper methods allow input access methods but flag errors if output access is attempted. Conversely, when the APDU buffer is in the Output format, input access methods result in exceptions.
The Common Service Format (CSF) of the APDU Buffer is only defined for APDUs
using the short length (normal semantics) of the ISO7816 protocol. When an
implementation supports extended length APDU format (see
ExtendedLength
) and an APDU with more
than 255 input or output data bytes is being processed, the behavior of
BasicService
class is undefined.
If the header areas maintained by the BasicService
helper
methods are modified directly in the APDU buffer and the format of the APDU
buffer described above is not maintained, unexpected behavior might result.
In addition, both La=0 and La=256 are represented in the CSF format as La=0.
The distinction is implementation dependent. The getOutputLength
method must be used to avoid ambiguity.
Many of the helper methods also throw exceptions if the APDU object is in an error state ( processing status code < 0 ).
APDU
,
javacardx.apdu.ExtendedLength
Constructor and Description |
---|
BasicService()
Creates new
BasicService . |
Modifier and Type | Method and Description |
---|---|
boolean |
fail(APDU apdu,
short sw)
Sets the processing state for the command in the
APDU
object to processed, and indicates that the processing has
failed. |
byte |
getCLA(APDU apdu)
Returns the class byte for the command in the
APDU object. |
byte |
getINS(APDU apdu)
Returns the instruction byte for the command in the
APDU
object. |
short |
getOutputLength(APDU apdu)
Returns the output length for the command in the
APDU
object. |
byte |
getP1(APDU apdu)
Returns the first parameter byte for the command in the
APDU
object. |
byte |
getP2(APDU apdu)
Returns the second parameter byte for the command in the
APDU object. |
short |
getStatusWord(APDU apdu)
Returns the response status word for the command in the
APDU
object. |
boolean |
isProcessed(APDU apdu)
Checks if the command in the
APDU object has already been
processed. |
boolean |
processCommand(APDU apdu)
This
BasicService method is a default implementation and
simply returns false without performing any processing. |
boolean |
processDataIn(APDU apdu)
This
BasicService method is a default implementation and
simply returns false without performing any processing. |
boolean |
processDataOut(APDU apdu)
This
BasicService method is a default implementation and
simply returns false without performing any processing. |
short |
receiveInData(APDU apdu)
Receives the input data for the command in the
APDU object
if the input has not already been received. |
boolean |
selectingApplet()
This method is used to determine if the command in the
APDU
object is the applet SELECT FILE command which selected the currently
selected applet. |
void |
setOutputLength(APDU apdu,
short length)
Sets the output length of the outgoing response for the command in the
APDU object. |
void |
setProcessed(APDU apdu)
Sets the processing state of the command in the
APDU
object to processed. |
void |
setStatusWord(APDU apdu,
short sw)
Sets the response status word for the command in the
APDU
object. |
boolean |
succeed(APDU apdu)
Sets the processing state for the command in the
APDU
object to processed, and indicates that the processing has
succeeded. |
boolean |
succeedWithStatusWord(APDU apdu,
short sw)
Sets the processing state for the command in the
APDU
object to processed, and indicates that the processing has
partially succeeded. |
public boolean processDataIn(APDU apdu)
BasicService
method is a default implementation and
simply returns false without performing any processing.processDataIn
in interface Service
apdu
- the APDU
object containing the command being
processedfalse
public boolean processCommand(APDU apdu)
BasicService
method is a default implementation and
simply returns false without performing any processing.processCommand
in interface Service
apdu
- the APDU
object containing the command being
processedfalse
public boolean processDataOut(APDU apdu)
BasicService
method is a default implementation and
simply returns false without performing any processing.processDataOut
in interface Service
apdu
- the APDU
object containing the command being
processedfalse
public short receiveInData(APDU apdu) throws ServiceException
APDU
object
if the input has not already been received. The entire input data must
fit in the APDU buffer starting at offset 5. When invoked, the APDU
object must either be in STATE_INITIAL
with the APDU
buffer in the Init format or in STATE_FULL_INCOMING
with
the APDU buffer in the Input Ready formatapdu
- the APDU
object containing the apdu being
processedServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_IN_COMMAND
if the APDU object is not in STATE_INITIAL or in
STATE_FULL_INCOMING or,
ServiceException.COMMAND_DATA_TOO_LONG
if
the input data does not fit in the APDU buffer starting at
offset 5.
public void setProcessed(APDU apdu) throws ServiceException
APDU
object to processed. This is done by setting the
APDU
object in outgoing mode by invoking the
APDU.setOutgoing
method. If the APDU is already in
outgoing mode, this method does nothing (allowing the method to be called
several times).apdu
- the APDU
object containing the command being
processedServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_OUT_COMMAND
if the APDU object is not accessible (APDU object in
STATE_ERROR_.. )
javacard.framework.APDU.getCurrentState()
public boolean isProcessed(APDU apdu)
APDU
object has already been
processed. This is done by checking whether or not the
APDU
object has been set in outgoing mode via a previous
invocation of the APDU.setOutgoing
method.
Note:
apdu
- the APDU
object containing the command being
processedtrue
if the command has been processed,
false
otherwisepublic void setOutputLength(APDU apdu, short length) throws ServiceException
APDU
object. This method can be called regardless of the
current state of the APDU processing.apdu
- the APDU
object containing the command being
processedlength
- the number of bytes in the response to the commandServiceException
- with the following reason code:
ServiceException.ILLEGAL_PARAM
if the
length
parameter is greater than 256 or if the
outgoing response will not fit within the APDU Buffer.
public short getOutputLength(APDU apdu) throws ServiceException
APDU
object. This method can only be called if the APDU processing state
indicates that the command has been processed.apdu
- the APDU
object containing the command being
processedServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_OUT_COMMAND
if the command is not processed or if the APDU
object is not accessible (APDU object in STATE_ERROR_.. )
javacard.framework.APDU.getCurrentState()
public void setStatusWord(APDU apdu, short sw)
APDU
object. This method can be called regardless of the APDU processing state
of the current command.apdu
- the APDU
object containing the command being
processedsw
- the status word response for this commandpublic short getStatusWord(APDU apdu) throws ServiceException
APDU
object. This method can only be called if the APDU processing state
indicates that the command has been processed.apdu
- the APDU
object containing the command being
processedServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_OUT_COMMAND
if the command is not processed or if the APDU
object is not accessible (APDU object in STATE_ERROR_.. )
javacard.framework.APDU.getCurrentState()
public boolean fail(APDU apdu, short sw) throws ServiceException
APDU
object to processed, and indicates that the processing has
failed. Sets the output length to 0
and the status word of
the response to the specified value.apdu
- the APDU
object containing the command being
processedsw
- the status word response for this commandtrue
ServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_OUT_COMMAND
if the APDU object is not accessible (APDU object in
STATE_ERROR_.. )
javacard.framework.APDU.getCurrentState()
public boolean succeed(APDU apdu) throws ServiceException
APDU
object to processed, and indicates that the processing has
succeeded. Sets the status word of the response to 0x9000
.
The output length of the response must be set separately.apdu
- the APDU
object containing the command being
processed.true
ServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_OUT_COMMAND
if the APDU object is not accessible (APDU object in
STATE_ERROR_.. )
javacard.framework.APDU.getCurrentState()
public boolean succeedWithStatusWord(APDU apdu, short sw) throws ServiceException
APDU
object to processed, and indicates that the processing has
partially succeeded. Sets the status word of the response to the
specified value. The output length of the response must be set
separately.apdu
- the APDU
object containing the command being
processedsw
- the status word to be returned for this commandtrue
ServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_OUT_COMMAND
if the APDU object is not accessible (APDU object in
STATE_ERROR_.. )
javacard.framework.APDU.getCurrentState()
public byte getCLA(APDU apdu)
APDU
object.
This method can be called regardless of the APDU processing state of the
current command.apdu
- the APDU
object containing the command being
processedpublic byte getINS(APDU apdu)
APDU
object. This method can be called regardless of the APDU processing state
of the current command.apdu
- the APDU
object containing the command being
processedpublic byte getP1(APDU apdu) throws ServiceException
APDU
object. When invoked, the APDU
object must be in
STATE_INITIAL
or STATE_FULL_INCOMING
.apdu
- the APDU
object containing the command being
processedServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_IN_COMMAND
if the APDU object is not in STATE_INITIAL or in
STATE_FULL_INCOMING. public byte getP2(APDU apdu) throws ServiceException
APDU
object. When invoked, the APDU
object
must be in STATE_INITIAL
or
STATE_FULL_INCOMING
.apdu
- the APDU
object containing the command being
processedServiceException
- with the following reason code:
ServiceException.CANNOT_ACCESS_IN_COMMAND
if the APDU object is not in STATE_INITIAL or in
STATE_FULL_INCOMING. public boolean selectingApplet()
APDU
object is the applet SELECT FILE command which selected the currently
selected applet.true
if applet SELECT FILE command is being
processedCopyright © 1998, 2015, Oracle and/or its affiliates. All rights reserved.