Package uk.co.nimp.scard

Source Code of uk.co.nimp.scard.VirtualTerminal

/*
*/
/**
*
* @author Sebastien Riou
*/
package uk.co.nimp.scard;

import com.atolsystems.atolutilities.MutableInteger;
import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.smartcardio.CardException;
import javax.smartcardio.CardNotPresentException;
import uk.co.nimp.smartcard.Apdu;
import uk.co.nimp.smartcard.UnexpectedCardResponseException;
import static uk.co.nimp.scard.VirtualTerminalManager.*;



class VirtualTerminal extends GenericTerminal implements FrequencySetter, PowerSupplyVoltageSetter {
    int vddMillivolts = 5000;
    int clkHertz = 5000000;

    VirtualTerminal(String name) {
        super(name);
    }

    @Override
    public boolean isCardPresent() throws ScardException {
        return VirtualTerminalManager.SCardIsPresent(name) == GenericTerminal.State.CARD_PRESENT;
    }

    private boolean waitForCard(boolean wantPresent, long timeout) throws ScardException {
        if (timeout < 0) {
            throw new IllegalArgumentException("timeout must not be negative");
        }
        long start = System.currentTimeMillis();
        boolean exit = false;
        boolean out = false;
        do {
            if (wantPresent) {
                if (VirtualTerminalManager.SCardIsPresent(name) == GenericTerminal.State.CARD_PRESENT) {
                    exit = true;
                    out = true;
                }
            } else {
                if (VirtualTerminalManager.SCardIsPresent(name) == GenericTerminal.State.CARD_ABSENT) {
                    exit = true;
                    out = true;
                }
            }
            if ((0 != timeout) && (System.currentTimeMillis() - start < timeout)) {
                exit = true;
            }
        } while (false == exit);
        return out;
    }

    @Override
    public boolean waitForCardPresent(long timeout) throws ScardException {
        return waitForCard(true, timeout);
    }

    @Override
    public boolean waitForCardAbsent(long timeout) throws ScardException {
        return waitForCard(false, timeout);
    }

    int curCyclePerEtu=372;
    boolean is260=false;
    @Override
    public void connectImpl(int protocol, int activation) throws ScardException, CardNotPresentException {

        try {

            MutableInteger i=new MutableInteger();
            atr= SCardConnect(name, vddMillivolts, clkHertz, protocol, i);
            curCyclePerEtu=i.value;
            logAtr(atr);
           


            //TODO: retrieve the actual protocol
            this.protocol = PROTOCOL_T_0;
            //basicChannel = new ChannelImpl(this, 0);
            state = State.CARD_PRESENT;
        } catch (MP300Exception e) {
            if (e.code == MP300Exception.CRET_ABSENT) {
                throw new CardNotPresentException("No card present", e);
            } else {
                throw new ScardException("connect() failed", e);
            }
        }
    }

    @Override
    protected void disconnectImpl() throws ScardException {
        curCyclePerEtu=372;
        if (GenericTerminal.State.CARD_PRESENT != state) {
            return;
        }
        SCardDisconnect(name, PcScTerminalManager.SCARD_LEAVE_CARD);
    }
    @Override
    protected void forceDisconnectImpl() throws ScardException {
        disconnectImpl();
    }

    private byte[] concat(byte[] b1, byte[] b2, int n2) {
        int n1 = b1.length;
        if ((n1 == 0) && (n2 == b2.length)) {
            return b2;
        }
        byte[] res = new byte[n1 + n2];
        System.arraycopy(b1, 0, res, 0, n1);
        System.arraycopy(b2, 0, res, n1, n2);
        return res;
    }
   
    @Override
    public void sendApduImpl(Apdu apdu) throws ScardException, UnexpectedCardResponseException {
        SCardTransmit(name, apdu);
    }

    public void setFrequency(int hertz) throws ScardException {
        //To avoid overrun
        this.logLine(ScardLogHandler.LOG_INFO, "INFO: Setting frequency to "+hertz+"Hz");
        SCardSetClk(name, hertz);
        clkHertz = hertz;
    }

    public void setVoltage(int millivolts) throws ScardException {
        this.logLine(ScardLogHandler.LOG_INFO, "INFO: Setting Vdd to "+millivolts+"mV");
        SCardSetVdd(name, millivolts);
        vddMillivolts = millivolts;
    }


/*    public static void main(String[] args) throws ScardException, CardException, IOException {
        try {
            VirtualTerminalManager manager = new VirtualTerminalManager(VirtualTerminal.class.getCanonicalName());
            List<GenericTerminal> terminals = manager.list();
            terminals = manager.list();
            terminals = manager.list();
            if (0 == terminals.size()) {
                System.out.println("Virtual terminal not detected.");
                return;
            }
            GenericTerminal terminal = terminals.get(0);
            terminals = manager.list();
            terminal.addLogHandler(new ScardPrintStreamLogHandler(System.out));
            System.out.println("Please insert a card in terminal " + terminal.getName());
            while (false == terminal.isCardPresent()) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Star265Terminal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.out.println("Try to coldConnect to " + terminal.getName());
            terminal.coldConnect();
            Apdu apdu = new Apdu(0x00, 0x8A, 0x00, 0x44, 0x02);
            terminal.sendApdu(apdu);
        } catch (ScardException ex) {
            Logger.getLogger(VirtualTerminal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CardException ex) {
            Logger.getLogger(VirtualTerminal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
*
*/
TOP

Related Classes of uk.co.nimp.scard.VirtualTerminal

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.