Package uk.co.nimp.scard

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

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

import java.util.logging.Logger;
import java.util.logging.Level;
import uk.co.nimp.scard.log.ScardPrintStreamLogHandler;
import java.io.IOException;
import java.util.List;
import com.atolsystems.atolutilities.IoServer;
import com.atolsystems.atolutilities.MutableInteger;
import javax.smartcardio.CardNotPresentException;
import uk.co.nimp.smartcard.Apdu;
import uk.co.nimp.smartcard.UnexpectedCardResponseException;
import static uk.co.nimp.scard.RemoteTerminalManager.*;



class RemoteTerminal extends GenericTerminal {
    IoServer context;
    RemoteTerminal(String name, IoServer context) {
        super(name);
        this.context =context;
    }

    @Override
    public boolean isCardPresent() throws ScardException {
        return SCardIsPresent(context) == 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 (SCardIsPresent(context) == GenericTerminal.State.CARD_PRESENT) {
                    exit = true;
                    out = true;
                }
            } else {
                if (SCardIsPresent(context) == 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);
    }

    @Override
    public void connectImpl(int protocol, int activation) throws ScardException, CardNotPresentException {
        try {
            MutableInteger i=new MutableInteger();
            atr= SCardConnect(context);
            logAtr(atr);
            this.protocol = PROTOCOL_T_0;
            state = State.CARD_PRESENT;
        } catch (Throwable e) {
            throw new ScardException("connect() failed", e);
        }
    }

    @Override
    protected void disconnectImpl() throws ScardException {
        if (GenericTerminal.State.CARD_PRESENT != state) {
            return;
        }
        SCardDisconnect(context, PcScTerminalManager.SCARD_LEAVE_CARD);
    }
    @Override
    protected void forceDisconnectImpl() throws ScardException {
        disconnectImpl();
    }
   
    @Override
    public void sendApduImpl(Apdu apdu) throws ScardException, UnexpectedCardResponseException {
        SCardTransmit(context, apdu);
    }

    public static void main(String[] args) throws ScardException, IOException, CardNotPresentException {
        try {
            RemoteTerminalManager manager = new RemoteTerminalManager();
            List<GenericTerminal> terminals = manager.list();
            terminals = manager.list();
            terminals = manager.list();
            if (0 == terminals.size()) {
                System.out.println("Remote 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(RemoteTerminal.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);
        }
    }
}
TOP

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

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.