PCSC4Java  0.2
Library PCSC for Java language.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
D:/Affaire/Perso/SmartCardToolBox/pcsc4java-framework-0.2/src/fr/redbilled/security/pcscforjava/PCSC.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
00003  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00004  *
00005  * This code is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License version 2 only, as
00007  * published by the Free Software Foundation.  Oracle designates this
00008  * particular file as subject to the "Classpath" exception as provided
00009  * by Oracle in the LICENSE file that accompanied this code.
00010  *
00011  * This code is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00014  * version 2 for more details (a copy is included in the LICENSE file that
00015  * accompanied this code).
00016  *
00017  * You should have received a copy of the GNU General Public License version
00018  * 2 along with this work; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
00020  *
00021  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
00022  * or visit www.oracle.com if you need additional information or have any
00023  * questions.
00024  */
00025 
00026 package fr.redbilled.security.pcscforjava;
00027 
00036 final class PCSC extends PlatformPCSC {
00037 
00038     private PCSC() {
00039         // no instantiation
00040     }
00041 
00042     static void checkAvailable() throws RuntimeException {
00043         if (initException != null) {
00044             throw new UnsupportedOperationException
00045                     ("PC/SC not available on this platform", initException);
00046         }
00047     }
00048 
00049     // returns SCARDCONTEXT (contextId)
00050     static native long SCardEstablishContext
00051             (int scope)
00052             throws PCSCException;
00053 
00054     static native void SCardReleaseContext
00055             (long lContextId)
00056             throws PCSCException;
00057 
00058     static native void SCardCancel
00059             (long lContextId)
00060             throws PCSCException;
00061 
00062     static native void SCardIsValidContext
00063             (long lContextId)
00064             throws PCSCException;
00065 
00066     static native String[] SCardListReaders
00067             (long contextId)
00068             throws PCSCException;
00069 
00070     // returns SCARDHANDLE (cardId)
00071     static native long SCardConnect
00072             (long contextId, String readerName, int shareMode, 
00073             int preferredProtocols) throws PCSCException;
00074 
00075     static native byte[] SCardReconnect
00076             (long lCardHandle, int iShareMode, int iPreferredProtocols, 
00077             int iInitialization)
00078             throws PCSCException;
00079 
00080     static native byte[] SCardTransmit
00081             (long cardId, int protocol, byte[] buf, int ofs, int len)
00082             throws PCSCException;
00083 
00084     static byte[] SCardStatus(long cardId, byte[] status) throws PCSCException
00085     {
00086         String[]  _psReaderName = new String[1];
00087         byte [] _pBResponse = SCardStatus(cardId, status, _psReaderName);
00088         //System.out.println("SCardStatus reader name: " + _psReaderName[0]);
00089         return _pBResponse;
00090     }
00091 
00092     // returns the ATR of the card, updates status[] with reader state and
00093     // protocol
00094     static native byte[] SCardStatus
00095             (long cardId, byte[] status, String[] psReaderName)
00096             throws PCSCException;
00097 
00098     static native void SCardDisconnect
00099             (long cardId, int disposition)
00100             throws PCSCException;
00101 
00102     // returns dwEventState[] of the same size and order as readerNames[]
00103     static native int[] SCardGetStatusChange
00104             (long contextId, long timeout, int[] currentState, 
00105             String[] readerNames) throws PCSCException;
00106 
00107     static native void SCardBeginTransaction
00108             (long cardId)
00109             throws PCSCException;
00110 
00111     static native void SCardEndTransaction
00112             (long cardId, int disposition)
00113             throws PCSCException;
00114 
00115     static native byte[] SCardControl
00116             (long cardId, int controlCode, byte[] sendBuffer)
00117             throws PCSCException;
00118 
00119     static native byte[] SCardGetAttrib
00120             (long lCardId, int iAttribute)
00121             throws PCSCException;
00122 
00123     static native void SCardSetAttrib
00124             (long lCardId, int iAttribute, byte[] pBCommand)
00125             throws PCSCException;
00126 
00127     // Find readers via SCardGetStatusChange
00128     // Rmove readers via ""
00129         
00130     // PCSC success/error/failure/warning codes
00131     final static int SCARD_S_SUCCESS             = 0x00000000;
00132     final static int SCARD_E_CANCELLED           = 0x80100002;
00133     final static int SCARD_E_CANT_DISPOSE        = 0x8010000E;
00134     final static int SCARD_E_INSUFFICIENT_BUFFER = 0x80100008;
00135     final static int SCARD_E_INVALID_ATR         = 0x80100015;
00136     final static int SCARD_E_INVALID_HANDLE      = 0x80100003;
00137     final static int SCARD_E_INVALID_PARAMETER   = 0x80100004;
00138     final static int SCARD_E_INVALID_TARGET      = 0x80100005;
00139     final static int SCARD_E_INVALID_VALUE       = 0x80100011;
00140     final static int SCARD_E_NO_MEMORY           = 0x80100006;
00141     final static int SCARD_F_COMM_ERROR          = 0x80100013;
00142     final static int SCARD_F_INTERNAL_ERROR      = 0x80100001;
00143     final static int SCARD_F_UNKNOWN_ERROR       = 0x80100014;
00144     final static int SCARD_F_WAITED_TOO_LONG     = 0x80100007;
00145     final static int SCARD_E_UNKNOWN_READER      = 0x80100009;
00146     final static int SCARD_E_TIMEOUT             = 0x8010000A;
00147     final static int SCARD_E_SHARING_VIOLATION   = 0x8010000B;
00148     final static int SCARD_E_NO_SMARTCARD        = 0x8010000C;
00149     final static int SCARD_E_UNKNOWN_CARD        = 0x8010000D;
00150     final static int SCARD_E_PROTO_MISMATCH      = 0x8010000F;
00151     final static int SCARD_E_NOT_READY           = 0x80100010;
00152     final static int SCARD_E_SYSTEM_CANCELLED    = 0x80100012;
00153     final static int SCARD_E_NOT_TRANSACTED      = 0x80100016;
00154     final static int SCARD_E_READER_UNAVAILABLE  = 0x80100017;
00155 
00156     final static int SCARD_W_UNSUPPORTED_CARD    = 0x80100065;
00157     final static int SCARD_W_UNRESPONSIVE_CARD   = 0x80100066;
00158     final static int SCARD_W_UNPOWERED_CARD      = 0x80100067;
00159     final static int SCARD_W_RESET_CARD          = 0x80100068;
00160     final static int SCARD_W_REMOVED_CARD        = 0x80100069;
00161     final static int SCARD_W_INSERTED_CARD       = 0x8010006A;
00162 
00163     final static int SCARD_E_UNSUPPORTED_FEATURE = 0x8010001F;
00164     final static int SCARD_E_PCI_TOO_SMALL       = 0x80100019;
00165     final static int SCARD_E_READER_UNSUPPORTED  = 0x8010001A;
00166     final static int SCARD_E_DUPLICATE_READER    = 0x8010001B;
00167     final static int SCARD_E_CARD_UNSUPPORTED    = 0x8010001C;
00168     final static int SCARD_E_NO_SERVICE          = 0x8010001D;
00169     final static int SCARD_E_SERVICE_STOPPED     = 0x8010001E;
00170 
00171     // MS undocumented
00172     final static int SCARD_E_NO_READERS_AVAILABLE = 0x8010002E;
00173     // std. Windows invalid handle return code, used instead of SCARD code
00174     final static int WINDOWS_ERROR_INVALID_HANDLE = 6;
00175     final static int WINDOWS_ERROR_INVALID_PARAMETER = 87;
00176 
00177     final static int TIMEOUT_INFINITE = 0xffffffff;
00178 
00179     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
00180 
00181     public static String toString(byte[] b) {
00182         StringBuilder sb = new StringBuilder(b.length * 3);
00183         for (int i = 0; i < b.length; i++) {
00184             int k = b[i] & 0xff;
00185             if (i != 0) {
00186                 sb.append(':');
00187             }
00188             sb.append(hexDigits[k >>> 4]);
00189             sb.append(hexDigits[k & 0xf]);
00190         }
00191         return sb.toString();
00192     }
00193 
00194 }