Java Card 3 Platform Development Kit User Guide, Classic Edition
APDU I/O API Examples
The following are examples of how to use the APDU I/O API:
To Connect To a Simulator
To establish a connection to a simulator such as cref
:
- Use the following code snippet:
CadClientInterface cad;
Socket sock;
sock = new Socket("localhost", 9025);
InputStream is = sock.getInputStream();
OutputStream os = sock.getOutputStream();
cad=CadDevice.getCadClientInstance(CadDevice.PROTOCOL_T0, is, os);
- The code establishes a T=0 connection to a simulator listening to port
9025
on localhost
.To open a T=1 connection instead, replace PROTOCOL_T0
in the last line with PROTOCOL_T1
.
For dual-interface simulation, open two T=1 connections on ports (n) and (n+1), as described in Two-interface Card Simulation.
To Power Up And Power Down the Card
The dual-interface RI is implemented in such a way that once the client establishes connection to a port, the next command must be powerUp
on that port. For example, the following sequence is valid:
Connect on "contacted" port.
Send powerUp
to it.
Exchange some APDUs.
Connect on "contactless" port.
Send powerUp
to it.
Exchange more APDUs
However, the following sequence is not valid:
Connect on "contacted" port.
Connect on "contactless" port.
Send powerUp
to any port.
To power up and power down the card:
- Use the following code:
- To power down the card and close the socket connection (for simulators only), use either of the following code lines:
cad.powerDown(true);
or
cad.powerDown();
- To power down, but leave the socket open, use the following code.
cad.powerDown(false);
If the simulator continues to run (which is true if this is contactless interface of the RI) you can issue powerUp()
on this card again and continue exchanging APDUs.
To Exchange APDUs
- Create a new APDU object using the following code:
- Copy the header
(CLA, INS, P1, P2)
of the APDU to be sent into the apdu.command
field.
- Set the data to be sent and the
Lc
using the following code:apdu.setDataIn(dataIn, Lc);
where the array dataIn
contains the C-APDU data, and the Lc
contains the data length.
- Set the number of bytes expected into the
apdu.Le
field.
- Exchange the APDU with a card or simulator using the following code:
cad.exchangeApdu(apdu);
After the exchange, apdu.Le
contains the number of bytes received from the card or simulator, apdu.dataOut
contains the data received, and apdu.sw1sw2
contains the SW1 and SW2 status bytes.
These fields can be accessed through the corresponding get
methods.
To Print the APDU
- The following code prints both C-APDU and R-APDU in the
apdutool
output format.
Table of Contents
Java Card: 3 Platform Development Kit User Guide, Classic Edition
Expand |
Collapse