1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.smartcardio;
27
28import java.util.*;
29
30/**
31 * A Smart Card terminal, sometimes referred to as a Smart Card Reader.
32 * A CardTerminal object can be obtained by calling
33 * {@linkplain CardTerminals#list}
34 * or {@linkplain CardTerminals#getTerminal CardTerminals.getTerminal()}.
35 *
36 * <p>Note that physical card readers with slots for multiple cards are
37 * represented by one <code>CardTerminal</code> object per such slot.
38 *
39 * @see CardTerminals
40 * @see TerminalFactory
41 *
42 * @since   1.6
43 * @author  Andreas Sterbenz
44 * @author  JSR 268 Expert Group
45 */
46public abstract class CardTerminal {
47
48    /**
49     * Constructs a new CardTerminal object.
50     *
51     * <p>This constructor is called by subclasses only. Application should
52     * call {@linkplain CardTerminals#list list()}
53     * or {@linkplain CardTerminals#getTerminal getTerminal()}
54     * to obtain a CardTerminal object.
55     */
56    protected CardTerminal() {
57        // empty
58    }
59
60    /**
61     * Returns the unique name of this terminal.
62     *
63     * @return the unique name of this terminal.
64     */
65    public abstract String getName();
66
67    /**
68     * Establishes a connection to the card.
69     * If a connection has previously established using
70     * the specified protocol, this method returns the same Card object as
71     * the previous call.
72     *
73     * @param protocol the protocol to use ("T=0", "T=1", or "T=CL"), or "*" to
74     *   connect using any available protocol.
75     *
76     * @throws NullPointerException if protocol is null
77     * @throws IllegalArgumentException if protocol is an invalid protocol
78     *   specification
79     * @throws CardNotPresentException if no card is present in this terminal
80     * @throws CardException if a connection could not be established
81     *   using the specified protocol or if a connection has previously been
82     *   established using a different protocol
83     * @throws SecurityException if a SecurityManager exists and the
84     *   caller does not have the required
85     *   {@linkplain CardPermission permission}
86     * @return the card the connection has been established with
87     */
88    public abstract Card connect(String protocol) throws CardException;
89
90    /**
91     * Returns whether a card is present in this terminal.
92     *
93     * @return whether a card is present in this terminal.
94     *
95     * @throws CardException if the status could not be determined
96     */
97    public abstract boolean isCardPresent() throws CardException;
98
99    /**
100     * Waits until a card is present in this terminal or the timeout
101     * expires. If the method returns due to an expired timeout, it returns
102     * false. Otherwise it return true.
103     *
104     * <P>If a card is present in this terminal when this
105     * method is called, it returns immediately.
106     *
107     * @param timeout if positive, block for up to <code>timeout</code>
108     *   milliseconds; if zero, block indefinitely; must not be negative
109     * @return false if the method returns due to an expired timeout,
110     *   true otherwise.
111     *
112     * @throws IllegalArgumentException if timeout is negative
113     * @throws CardException if the operation failed
114     */
115    public abstract boolean waitForCardPresent(long timeout) throws CardException;
116
117    /**
118     * Waits until a card is absent in this terminal or the timeout
119     * expires. If the method returns due to an expired timeout, it returns
120     * false. Otherwise it return true.
121     *
122     * <P>If no card is present in this terminal when this
123     * method is called, it returns immediately.
124     *
125     * @param timeout if positive, block for up to <code>timeout</code>
126     *   milliseconds; if zero, block indefinitely; must not be negative
127     * @return false if the method returns due to an expired timeout,
128     *   true otherwise.
129     *
130     * @throws IllegalArgumentException if timeout is negative
131     * @throws CardException if the operation failed
132     */
133    public abstract boolean waitForCardAbsent(long timeout) throws CardException;
134
135}
136