Search in sources :

Example 11 with CardTerminal

use of javax.smartcardio.CardTerminal in project jdk8u_jdk by JetBrains.

the class TestControl method main.

public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    // establish a connection with the card
    Card card = terminal.connect("T=0");
    System.out.println("card: " + card);
    byte[] data = new byte[] { 2 };
    try {
        byte[] resp = card.transmitControlCommand(IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE, data);
        System.out.println("Firmware: " + toString(resp));
        throw new Exception();
    } catch (CardException e) {
        // we currently don't know of any control commands that work with
        // our readers. call the function just to make sure we don't crash
        // or throw the wrong exception
        System.out.println("OK: " + e);
        e.printStackTrace(System.out);
    }
    try {
        card.transmitControlCommand(IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE, null);
    } catch (NullPointerException e) {
        System.out.println("OK: " + e);
    }
    // disconnect
    card.disconnect(true);
    System.out.println("OK.");
}
Also used : CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) CardException(javax.smartcardio.CardException) Card(javax.smartcardio.Card)

Example 12 with CardTerminal

use of javax.smartcardio.CardTerminal in project jdk8u_jdk by JetBrains.

the class TestDefault method main.

public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    if (terminals.isEmpty()) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + terminals);
    System.out.println("OK.");
}
Also used : TerminalFactory(javax.smartcardio.TerminalFactory) CardTerminal(javax.smartcardio.CardTerminal)

Example 13 with CardTerminal

use of javax.smartcardio.CardTerminal in project jdk8u_jdk by JetBrains.

the class TestMultiplePresent method main.

public static void main(String[] args) throws Exception {
    Utils.setLibrary(args);
    TerminalFactory factory = Utils.getTerminalFactory(null);
    if (factory == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    System.out.println(factory);
    CardTerminals terminals = factory.terminals();
    List<CardTerminal> list = terminals.list();
    System.out.println("Terminals: " + list);
    boolean multipleReaders = true;
    if (list.size() < 2) {
        if (list.isEmpty()) {
            System.out.println("Skipping the test: " + "no card terminals available");
            return;
        }
        System.out.println("Only one reader present, using simplified test");
        multipleReaders = false;
    }
    while (true) {
        boolean present = false;
        for (CardTerminal terminal : list) {
            present |= terminal.isCardPresent();
        }
        if (present == false) {
            break;
        }
        System.out.println("*** Remove all cards!");
        Thread.sleep(1000);
    }
    System.out.println("OK");
    List<CardTerminal> result;
    result = terminals.list(CARD_PRESENT);
    if (result.size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (terminals.waitForChange(1000)) {
        throw new Exception("no timeout");
    }
    if (terminals.waitForChange(1000)) {
        throw new Exception("no timeout");
    }
    result = terminals.list(CARD_PRESENT);
    if (result.size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    System.out.println("*** Insert card!");
    terminals.waitForChange();
    result = terminals.list(CARD_INSERTION);
    System.out.println(result);
    if (result.size() != 1) {
        throw new Exception("no card present");
    }
    CardTerminal t1 = result.get(0);
    result = terminals.list(CARD_INSERTION);
    System.out.println(result);
    if ((result.size() != 1) || (result.get(0) != t1)) {
        throw new Exception("no card present");
    }
    if (terminals.list(CARD_REMOVAL).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (terminals.list(CARD_REMOVAL).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (terminals.waitForChange(1000)) {
        throw new Exception("no timeout");
    }
    if (terminals.list(CARD_REMOVAL).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (multipleReaders) {
        System.out.println("*** Insert card into other reader!");
        terminals.waitForChange();
        result = terminals.list(CARD_INSERTION);
        System.out.println(result);
        if (result.size() != 1) {
            throw new Exception("no card present");
        }
        CardTerminal t2 = result.get(0);
        if (t1.getName().equals(t2.getName())) {
            throw new Exception("same terminal");
        }
        System.out.println("*** Remove card from 2nd reader!");
        terminals.waitForChange();
        if (terminals.list(CARD_INSERTION).size() != 0) {
            throw new Exception("List not empty: " + result);
        }
        result = terminals.list(CARD_REMOVAL);
        if ((result.size() != 1) || (result.get(0) != t2)) {
            throw new Exception("no or wrong terminal");
        }
    }
    System.out.println("*** Remove card from 1st reader!");
    terminals.waitForChange();
    if (terminals.list(CARD_INSERTION).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    result = terminals.list(CARD_REMOVAL);
    if ((result.size() != 1) || (result.get(0) != t1)) {
        throw new Exception("no or wrong terminal");
    }
    System.out.println("OK.");
}
Also used : TerminalFactory(javax.smartcardio.TerminalFactory) CardTerminals(javax.smartcardio.CardTerminals) CardTerminal(javax.smartcardio.CardTerminal)

Example 14 with CardTerminal

use of javax.smartcardio.CardTerminal in project jmulticard by ctt-gob-es.

the class SmartcardIoConnection method getTerminals.

/**
 * {@inheritDoc}
 */
@Override
public long[] getTerminals(final boolean onlyWithCardPresent) throws ApduConnectionException {
    final List<CardTerminal> terminales;
    try {
        terminales = TerminalFactory.getDefault().terminals().list();
    } catch (final CardException e) {
        // $NON-NLS-1$
        LOGGER.warning("No se ha podido recuperar la lista de lectores del sistema: " + e);
        return new long[0];
    }
    try {
        // Listamos los indices de los lectores que correspondan segun si tienen o no tarjeta insertada
        final ArrayList<Long> idsTerminales = new ArrayList<>(terminales.size());
        for (int idx = 0; idx < terminales.size(); idx++) {
            if (onlyWithCardPresent) {
                if (terminales.get(idx).isCardPresent()) {
                    idsTerminales.add(Long.valueOf(idx));
                }
            } else {
                idsTerminales.add(Long.valueOf(idx));
            }
        }
        final long[] ids = new long[idsTerminales.size()];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = idsTerminales.get(i).longValue();
        }
        return ids;
    } catch (final Exception ex) {
        throw new ApduConnectionException(// $NON-NLS-1$
        "Error recuperando la lista de lectores de tarjetas del sistema: " + ex, // $NON-NLS-1$
        ex);
    }
}
Also used : ArrayList(java.util.ArrayList) CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) CardException(javax.smartcardio.CardException) ApduConnectionOpenedInExclusiveModeException(es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 15 with CardTerminal

use of javax.smartcardio.CardTerminal in project open-ecard by ecsec.

the class PCSCTest method connect.

private void connect() {
    try {
        // File libPcscLite = new File("/usr/lib/libpcsclite.so.1");
        // if (libPcscLite.exists()) {
        // System.setProperty("sun.security.smartcardio.library", libPcscLite.getAbsolutePath());
        // }
        TerminalFactory t = TerminalFactory.getInstance("PC/SCs", null);
        // TerminalFactory t = TerminalFactory.getDefault();
        CardTerminals c = t.terminals();
        logger.info("Card terminals: {}", c.list().size());
        List terminals = c.list();
        if (terminals.isEmpty()) {
            logger.info("No presend cards!");
        } else {
            for (int i = 0; i < terminals.size(); i++) {
                CardTerminal ct = (CardTerminal) terminals.get(i);
                if (ct.isCardPresent()) {
                    Card card = ct.connect("*");
                    connection = card.getBasicChannel();
                    logger.info("Card found at card terminal " + i + ": ", card.toString());
                }
            }
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}
Also used : TerminalFactory(javax.smartcardio.TerminalFactory) CardTerminals(javax.smartcardio.CardTerminals) List(java.util.List) CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) Card(javax.smartcardio.Card)

Aggregations

CardTerminal (javax.smartcardio.CardTerminal)15 Card (javax.smartcardio.Card)8 CardException (javax.smartcardio.CardException)7 TerminalFactory (javax.smartcardio.TerminalFactory)5 CardChannel (javax.smartcardio.CardChannel)4 CardTerminals (javax.smartcardio.CardTerminals)3 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)2 ApduConnectionOpenedInExclusiveModeException (es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException)2 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)2 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)2 NoReadersFoundException (es.gob.jmulticard.apdu.connection.NoReadersFoundException)2 IOException (java.io.IOException)2 CommandAPDU (javax.smartcardio.CommandAPDU)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResponseAPDU (javax.smartcardio.ResponseAPDU)1 SCIOErrorCode (org.openecard.common.ifd.scio.SCIOErrorCode)1