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/pcscforjava/Card.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.pcscforjava;
00027 
00028 import static fr.redbilled.pcscforjava.PCSCDefines.*;
00029 import java.util.HashMap;
00030 import java.util.Map;
00031 
00042 public abstract class Card {
00043 
00052     protected Card() {
00053         // empty
00054     }
00055 
00061     public abstract ATR getATR();
00062 
00068     public abstract String getProtocol();
00069 
00080     public abstract CardChannel getBasicChannel();
00081 
00094     public abstract CardChannel openLogicalChannel() throws CardException;
00095 
00117     public abstract void beginExclusive() throws CardException;
00118 
00132     public abstract void endExclusive() throws CardException;
00133 
00151     public abstract byte[] transmitControlCommand(int controlCode,
00152             byte[] command) throws CardException;
00153 
00177     public abstract void reconnect(int iShareMode, int iInitialization)
00178             throws CardException;
00179 
00199     public abstract void disconnect(int iDisposition) throws CardException;
00200         
00314     public abstract byte[] getAttrib(int iAttribute) throws CardException;
00315 
00333     public abstract void setAttrib(int iAttribute, byte[] pBCommand)
00334             throws CardException;
00335 
00339     protected final int IOCTL_GET_FEATURE_REQUEST = SCARD_CTL_CODE(3400);
00340 
00344     protected Map<Byte, Integer> m_features;
00345 
00353     public byte[] modifyPinDirect(byte[] abyModifyPin) throws CardException {
00354         int _ioctl = 0;
00355 
00356         if (!hasFeature(FEATURE_MODIFY_PIN_DIRECT))
00357             throw new CardException("sun.security.smartcardio.PCSCException: "
00358                     + "FEATURE_MODIFY_PIN_DIRECT not supported");
00359 
00360         _ioctl = this.m_features.get(FEATURE_MODIFY_PIN_DIRECT);
00361         return(transmitControlCommand(_ioctl, abyModifyPin));
00362   }
00363 
00371     public byte[] verifyPinDirect(byte[] abyVerifyPin) throws CardException {
00372         int _ioctl = 0;
00373 
00374         if (!hasFeature(FEATURE_VERIFY_PIN_DIRECT))
00375             throw new CardException("sun.security.smartcardio.PCSCException: "
00376                     + "FEATURE_VERIFY_PIN_DIRECT not supported");
00377 
00378         _ioctl = this.m_features.get(FEATURE_VERIFY_PIN_DIRECT);
00379         return(transmitControlCommand(_ioctl, abyVerifyPin));
00380     }
00381 
00388     protected boolean hasFeature(Byte feature) throws CardException {
00389         if (this.m_features != null)
00390             return this.m_features.containsKey(feature);
00391         else
00392         {
00393             queryFeatures();
00394             return this.m_features.containsKey(feature);
00395         }
00396     }
00397 
00402     protected void queryFeatures() throws CardException {
00403         this.m_features = new HashMap<Byte, Integer>();
00404         byte[] _abyResponse = transmitControlCommand(IOCTL_GET_FEATURE_REQUEST,
00405                 new byte[0]);
00406 
00407         // tag
00408         // length in bytes (always 4)
00409         // control code value for supported feature (in big endian)
00410         for (int i = 0; i < _abyResponse.length; i += 6)
00411         {
00412             Byte feature = new Byte(_abyResponse[i]);
00413             int ioctlBigEndian = ((0xff & _abyResponse[i + 2]) << 24) |
00414                   ((0xff & _abyResponse[i + 3]) << 16) |
00415                   ((0xff & _abyResponse[i + 4]) << 8) |
00416                   (0xff & _abyResponse[i + 5]);
00417             Integer ioctl = new Integer(ioctlBigEndian);
00418             this.m_features.put(feature, ioctl);
00419         }
00420     }
00421 
00422     public abstract int getSharingMode();   
00423 }