001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.cardreader;
028:
029: import java.io.IOException;
030: import com.sun.midp.security.*;
031:
032: /**
033: * This class represents card device abstraction.
034: */
035:
036: public abstract class CardDevice {
037: /**
038: * Number of start slot of the device. The slot factory holds
039: * a table with global slot numbers. Every device operates with
040: * local ones (numbers inside the device).
041: * <code>startSlotNumber</code> is global number of first
042: * local slot.
043: */
044: private int startSlotNumber;
045:
046: /** Default SAT slot number. */
047: private final static int DEFAULT_SAT_SLOT = 0;
048:
049: /**
050: * Answer-To-Reset bytes receiving during last reset.
051: */
052: private byte[] ATR;
053:
054: /**
055: * Initializes the device.
056: *
057: * @throws IOException If a device initialization failed.
058: * @throws CardDeviceException If a device configuration is bad.
059: */
060: public abstract void init() throws IOException, CardDeviceException;
061:
062: /**
063: * Closes the device.
064: * No exceptions thrown to avoid mess in exception handlers trying
065: * to clean up and close the device.
066: */
067: public abstract void close();
068:
069: /**
070: * Performs platform lock of the device. This is intended to make
071: * sure that no other native application
072: * uses the same device during a transaction.
073: *
074: * @throws IOException If a device locking failed.
075: */
076: public abstract void lock() throws IOException;
077:
078: /**
079: * Unlocks the device.
080: *
081: * @throws IOException If a device unlocking failed.
082: */
083: public abstract void unlock() throws IOException;
084:
085: /**
086: * Open the specified slot. This is for situations when slot creation
087: * requires additional actions. By default doing nothing but this is
088: * not an abstract method because most devices don't need it.
089: *
090: * @param slot Slot number
091: * @param token Security token for this slot
092: * @throws IOException If slot opening failed.
093: */
094: public void openSlot(int slot, SecurityToken token)
095: throws IOException {
096: }
097:
098: /**
099: * Close the specified slot. This is for situations when slot closure
100: * requires additional actions. By default doing nothing but this is
101: * not an abstract method becuase most devices don't need it.
102: *
103: * @param slot Slot number
104: * @throws IOException If slot close failed.
105: */
106: public void closeSlot(int slot) throws IOException {
107: }
108:
109: /**
110: * Selects the current slot for the subsequent transfer operations.
111: * For the one-slot devices the default slot number is 0.
112: *
113: * @param slot Slot number
114: * @throws IOException If a slot selection failed.
115: */
116: public abstract void selectSlot(int slot) throws IOException;
117:
118: /**
119: * Gets number of slots on a device. Default implementation returns 1 which
120: * is ok for most devices.
121: *
122: * @return Number of slots on a device
123: */
124: public int getSlotCount() {
125: return 1;
126: }
127:
128: /**
129: * Checks if this slot is SAT slot. Default implementation returns true
130: * if <code>slotNumber</code> is 0.
131: *
132: * @param slotNumber Slot number
133: * @return <code>true</code> if the slot is dedicated for SAT,
134: * <code>false</code> otherwise
135: * @throws IOException If an error occured.
136: */
137: public boolean isSatSlot(int slotNumber) throws IOException {
138: return slotNumber == DEFAULT_SAT_SLOT;
139: }
140:
141: /**
142: * Resets the device. Wrapper method for
143: * <code>cmdReset</code>.
144: *
145: * @throws IOException If a reset failed.
146: */
147: public void reset() throws IOException {
148: int bytes_read;
149: byte[] r = new byte[255];
150:
151: bytes_read = cmdReset(r);
152: if (bytes_read > 0) {
153: ATR = new byte[bytes_read];
154: System.arraycopy(r, 0, ATR, 0, bytes_read);
155: } else
156: throw new IOException("Empty ATR");
157: }
158:
159: /**
160: * Performs reset of device.
161: * Returns ATR into provided buffer
162: *
163: * @param atr Buffer for ATR bytes
164: * @return Length of ATR
165: * @throws IOException If a reset failed.
166: */
167: protected abstract int cmdReset(byte[] atr) throws IOException;
168:
169: /**
170: * Performs 'POWER DOWN' command. By default does nothing.
171: *
172: * @throws IOException If command failed.
173: */
174: public void cmdPowerDown() throws IOException {
175: }
176:
177: /**
178: * Gets Answer-To-Reset bytes from the device.
179: *
180: * @return ATR bytes
181: * @throws IOException If data transfer failed.
182: */
183: public byte[] getATR() throws IOException {
184: return ATR;
185: }
186:
187: /**
188: * Performs data transfer to the device. Wrapper
189: * method for <code>cmdXfer</code>.
190: *
191: * @param request Request bytes
192: * @param response Response bytes
193: * @return Length of response
194: * @throws IOException If a data transfer failed.
195: */
196: public int xfer(byte[] request, byte[] response) throws IOException {
197: return cmdXfer(request, response);
198: }
199:
200: /**
201: * Performs data transfer to the device.
202: *
203: * @param request Request bytes
204: * @param response Response bytes
205: * @return Length of response
206: * @throws IOException If a data transfer failed.
207: */
208: protected abstract int cmdXfer(byte[] request, byte[] response)
209: throws IOException;
210:
211: /**
212: * Checks if the card in the selected slot was changed
213: * since last call or since last reset.
214: * Called after any transfer operation, so
215: * implementation can check and store that status during
216: * this operation.
217: *
218: * @return true if was changed, false otherwise.
219: * @throws IOException If something fails.
220: */
221: public abstract boolean isCardChanged() throws IOException;
222:
223: /**
224: * Gets start slot number in the global slot numbering scheme.
225: *
226: * @return Number of the first device slot in the global numbering scheme
227: */
228: public int getStartSlotNumber() {
229: return this .startSlotNumber;
230: }
231:
232: /**
233: * Stores start slot number.
234: *
235: * @param slot Number of the first device slot in the global
236: * numbering scheme
237: */
238: public void setStartSlotNumber(int slot) {
239: this .startSlotNumber = slot;
240: }
241:
242: /**
243: * Checks if the global slot number belongs to the device.
244: *
245: * @param slot Global slot number
246: * @return True if slot belongs to this device, otherwise false
247: */
248: public boolean checkSlotNumber(int slot) {
249: return ((slot >= this.startSlotNumber) && (slot < this.startSlotNumber
250: + getSlotCount()));
251: }
252: }
|