Java Code Examples for javax.smartcardio.CardTerminal#isCardPresent()

The following examples show how to use javax.smartcardio.CardTerminal#isCardPresent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static CardTerminal getTerminal(String[] args) throws Exception {
    setLibrary(args);

    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal terminal = terminals.get(0);

    if (terminal.isCardPresent() == false) {
        System.out.println("*** Insert card");
        if (terminal.waitForCardPresent(20 * 1000) == false) {
            throw new Exception("no card available");
        }
    }
    return terminal;
}
 
Example 2
Source File: GenericContext.java    From openjavacard-tools with GNU Lesser General Public License v3.0 6 votes vote down vote up
public GenericCard findSingleCard(String prefix, AID application) {
    LOG.debug("findSingleCard()");
    GenericCard card;
    CardTerminal terminal = findSingleTerminal(prefix);
    try {
        // check card presence
        if (!terminal.isCardPresent()) {
            throw new Error("No card present in terminal");
        }
        // create the client
        card = new GenericCard(this, terminal);
    } catch (CardException e) {
        throw new Error("Error detecting card", e);
    }
    return card;
}
 
Example 3
Source File: Utils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
static CardTerminal getTerminal(String[] args) throws Exception {
    setLibrary(args);

    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal terminal = terminals.get(0);

    if (terminal.isCardPresent() == false) {
        System.out.println("*** Insert card");
        if (terminal.waitForCardPresent(20 * 1000) == false) {
            throw new Exception("no card available");
        }
    }
    return terminal;
}
 
Example 4
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static CardTerminal getTerminal(String[] args, String provider) throws Exception {
    setLibrary(args);

    try {
        TerminalFactory factory = (provider == null)
                ? TerminalFactory.getInstance("PC/SC", null)
                : TerminalFactory.getInstance("PC/SC", null, provider);
        System.out.println(factory);

        List<CardTerminal> terminals = factory.terminals().list();
        System.out.println("Terminals: " + terminals);
        if (terminals.isEmpty()) {
            return null;
        }
        CardTerminal terminal = terminals.get(0);

        if (terminal.isCardPresent() == false) {
            System.out.println("*** Insert card");
            if (terminal.waitForCardPresent(20 * 1000) == false) {
                throw new Exception("no card available");
            }
        }
        System.out.println("card present: " + terminal.isCardPresent());

        return terminal;

    } catch (NoSuchAlgorithmException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {
            return null;
        }
        throw e;
    }
}
 
Example 5
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static CardTerminal getTerminal(String[] args, String provider) throws Exception {
    setLibrary(args);

    try {
        TerminalFactory factory = (provider == null)
                ? TerminalFactory.getInstance("PC/SC", null)
                : TerminalFactory.getInstance("PC/SC", null, provider);
        System.out.println(factory);

        List<CardTerminal> terminals = factory.terminals().list();
        System.out.println("Terminals: " + terminals);
        if (terminals.isEmpty()) {
            return null;
        }
        CardTerminal terminal = terminals.get(0);

        if (terminal.isCardPresent() == false) {
            System.out.println("*** Insert card");
            if (terminal.waitForCardPresent(20 * 1000) == false) {
                throw new Exception("no card available");
            }
        }
        System.out.println("card present: " + terminal.isCardPresent());

        return terminal;

    } catch (NoSuchAlgorithmException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {
            return null;
        }
        throw e;
    }
}
 
Example 6
Source File: SignUtils.java    From Websocket-Smart-Card-Signer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static ArrayList<String> getConnectedCardATR(){
    ArrayList<String> ret = new ArrayList<String>();
    try{
        List<CardTerminal> terminalList = TerminalFactory.getDefault().terminals().list();
        
        for(CardTerminal terminal:terminalList)
            if(terminal.isCardPresent()){
                javax.smartcardio.Card card = terminal.connect("*");
                ret.add(StringUtils.toHexString(card.getATR().getBytes()));
                card.disconnect(false);
            }
    }catch(Exception ex){}
    return ret;
}
 
Example 7
Source File: TestPresent.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal terminal = terminals.get(0);

    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }

    Timer timer = new Timer();

    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();

    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());

    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();

    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();

    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("OK.");
}
 
Example 8
Source File: TestPresent.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal terminal = terminals.get(0);

    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }

    Timer timer = new Timer();

    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();

    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());

    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();

    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();

    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("OK.");
}
 
Example 9
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static CardTerminal getTerminal(String[] args, String provider) throws Exception {
    setLibrary(args);

    try {
        TerminalFactory factory = (provider == null)
                ? TerminalFactory.getInstance("PC/SC", null)
                : TerminalFactory.getInstance("PC/SC", null, provider);
        System.out.println(factory);

        List<CardTerminal> terminals = factory.terminals().list();
        System.out.println("Terminals: " + terminals);
        if (terminals.isEmpty()) {
            return null;
        }
        CardTerminal terminal = terminals.get(0);

        if (terminal.isCardPresent() == false) {
            System.out.println("*** Insert card");
            if (terminal.waitForCardPresent(20 * 1000) == false) {
                throw new Exception("no card available");
            }
        }
        System.out.println("card present: " + terminal.isCardPresent());

        return terminal;

    } catch (NoSuchAlgorithmException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {
            return null;
        }
        throw e;
    }
}
 
Example 10
Source File: Utils.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static CardTerminal getTerminal(String[] args, String provider) throws Exception {
    setLibrary(args);

    try {
        TerminalFactory factory = (provider == null)
                ? TerminalFactory.getInstance("PC/SC", null)
                : TerminalFactory.getInstance("PC/SC", null, provider);
        System.out.println(factory);

        List<CardTerminal> terminals = factory.terminals().list();
        System.out.println("Terminals: " + terminals);
        if (terminals.isEmpty()) {
            return null;
        }
        CardTerminal terminal = terminals.get(0);

        if (terminal.isCardPresent() == false) {
            System.out.println("*** Insert card");
            if (terminal.waitForCardPresent(20 * 1000) == false) {
                throw new Exception("no card available");
            }
        }
        System.out.println("card present: " + terminal.isCardPresent());

        return terminal;

    } catch (NoSuchAlgorithmException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {
            return null;
        }
        throw e;
    }
}
 
Example 11
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new org.taktik.connector.technical.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 12
Source File: FancyChooser.java    From apdu4j with MIT License 4 votes vote down vote up
synchronized void setSelection(List<CardTerminal> terminals) {
    try {
        // -1 == Selection not made; -2 == Selection is quit
        int previouslySelected = quitButton.isFocused() ? -2 : mainActions.getSelectedIndex();
        //setStatus("set " + previouslySelected);
        int selectedIndex = previouslySelected;
        mainActions.clearItems();
        HashMap<String, String> statuses = new HashMap<>();
        int i = 0;
        for (CardTerminal t : terminals) {
            final String name = t.getName();
            final boolean present = t.isCardPresent();
            final boolean exclusive = isExlusive(t);
            String status = EMPTY;
            if (present)
                status = PRESENT;
            if (exclusive)
                status = EXCLUSIVE;
            statuses.put(name, status);
            mainActions.addItem(String.format("[%s] %s", status, aliases.translate(name)), () -> {
                if (exclusive) {
                    MessageDialog warn = new MessageDialogBuilder()
                            .setTitle(" Warning! ")
                            .setText("Reader is in exclusive use by some other application")
                            .addButton(MessageDialogButton.Cancel)
                            .addButton(MessageDialogButton.Continue)
                            .build();
                    warn.setCloseWindowWithEscape(true);
                    MessageDialogButton r = warn.showDialog(gui);
                    if (r == null || r == MessageDialogButton.Cancel) {
                        return;
                    }
                    // Continue below
                }
                chosenOne = t;
                mainWindow.close();
            });


            // Reset if reader becomes exclusive (unless it is the only reader)
            if (i == selectedIndex && exclusive && !previousStates.get(name).equals(EXCLUSIVE) && terminals.size() > 1)
                selectedIndex = -1;
            // Select if only reader becomes non-exclusive
            if (!exclusive && previousStates.getOrDefault(name, "").equals(EXCLUSIVE) && terminals.size() == 1)
                selectedIndex = i;
            // New reader connected
            if (!previousStates.containsKey(name))
                selectedIndex = i;
            // Select first non-exclusive reader
            if (selectedIndex == -1 && !exclusive)
                selectedIndex = i;
            // Existing reader got a card
            if (previousStates.getOrDefault(name, "").equals(EMPTY) && present && !exclusive)
                selectedIndex = i;
            i++;
        }

        // Set title
        if (terminals.size() == 0) {
            mainWindow.setTitle(" Connect a reader ");
        } else {
            mainWindow.setTitle(" Choose a reader ");
        }

        // Update selected index and related focus
        if (selectedIndex >= 0) {
            mainActions.setSelectedIndex(selectedIndex);
            mainActions.takeFocus();
        } else {
            quitButton.takeFocus();
        }
        previousStates = statuses;
        // Refresh screen
        mainPanel.invalidate();
        //screen.refresh(Screen.RefreshType.COMPLETE);
        gui.updateScreen();
    } catch (CardException | IOException e) {
        System.err.println(e.getMessage());
        throw new RuntimeException(e);
    }
}
 
Example 13
Source File: TestPresent.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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;
    }

    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }

    Timer timer = new Timer();

    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();

    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());

    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();

    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();

    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("OK.");
}
 
Example 14
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new be.ehealth.technicalconnector.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 15
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new be.ehealth.technicalconnector.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 16
Source File: TestPresent.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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;
    }

    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }

    Timer timer = new Timer();

    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();

    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());

    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();

    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();

    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("OK.");
}
 
Example 17
Source File: TestPresent.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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;
    }

    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }

    Timer timer = new Timer();

    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();

    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());

    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();

    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();

    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("OK.");
}
 
Example 18
Source File: CardManager.java    From JCMathLib with MIT License 4 votes vote down vote up
private CardChannel connectToCardByTerminalFactory(TerminalFactory factory, int targetReaderIndex) throws CardException {
    List<CardTerminal> terminals = new ArrayList<>();

    boolean card_found = false;
    CardTerminal terminal = null;
    Card card = null;
    try {
        for (CardTerminal t : factory.terminals().list()) {
            terminals.add(t);
            if (t.isCardPresent()) {
                card_found = true;
            }
        }
        System.out.println("Success.");
    } catch (Exception e) {
        System.out.println("Failed.");
    }

    if (card_found) {
        System.out.println("Cards found: " + terminals);

        terminal = terminals.get(targetReaderIndex); // Prioritize physical card over simulations

        System.out.print("Connecting...");
        card = terminal.connect("*"); // Connect with the card

        System.out.println(" Done.");

        System.out.print("Establishing channel...");
        m_channel = card.getBasicChannel();

        System.out.println(" Done.");

        // Select applet (mpcapplet)
        System.out.println("Smartcard: Selecting applet...");

        CommandAPDU cmd = new CommandAPDU(0x00, 0xa4, 0x04, 0x00, m_APPLET_AID);
        ResponseAPDU response = transmit(cmd);
    } else {
        System.out.print("Failed to find physical card.");
    }

    if (card != null) {
        return card.getBasicChannel();
    } else {
        return null;
    }
}
 
Example 19
Source File: TestPresent.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal terminal = terminals.get(0);

    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }

    Timer timer = new Timer();

    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();

    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());

    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();

    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();

    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("OK.");
}
 
Example 20
Source File: TestPresent.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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;
    }

    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }

    Timer timer = new Timer();

    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();

    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());

    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();

    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();

    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();

    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());

    System.out.println("OK.");
}