Java Code Tutorial, Intermediate - martinpaljak/JCardEngine GitHub Wiki

Creating PC/SC adapters programmatically

AbstractTCPAdapter implementations exist for vsmartcard and Oracle Java Card SDK simulator.

Once you've created the simulator, pass in the necessary host (which default to 0.0.0.0 when listening and 127.0.0.1 when connecting) and port parameters.

VirtualCardEngine sim = JavaCardEngine.create();

// Set up the virtual card as needed with sim.installApplet();

AbstractTCPAdapter adapter = new VSmartCardClient("192.168.100.1", 26394, sim);

It is intended to run in a thread, and will pass the commands from the remote side to your applet(s).

ExecutorService executor = Executors.newSingleThreadExecutor();

executor.submit(adapter);

The adapter can be tuned to expose a specific protocol via the PC/SC adapter or to have a specific ATR.


adapter = adapter.withProtocol("T=CL"); // Applets will see all APDU-s as from contactless A interface

adapter = adapter.withATR(Hex.decode("XXYY")); // PC/SC driver will report XXYY as ATR to applications

adapter = adapter.withHost("192.168.100.1"); // Override the default host (either 0.0.0.0 or 127.0.0.1)

adapter = adapter.withPort(38712); // Override the default port

Accessing Applet instances

You can fetch the applet instance for debugging purposes with sim.getApplet(AID aid). If the applet was installed with installExposedApplet() public fields can be accessed directly. Otherwise just a proxy of the Applet class is returned.

Threading and thread-safety

  • JavaCardEngine (installing/deleting applets etc) should NOT be used from multiple threads, but only from the thread that called JavaCardEngine.create()
  • Accessing a single JavaCardEngine concurrently from multiple threads via acquired EngineSession objects via .connect() methods is thread-safe
  • Like a real Java Card, JavaCardEngine is in essence a single-threaded resource

Next steps

Continue with Java Code Tutorial, Advanced.