Search in sources :

Example 11 with IFDStatusType

use of iso.std.iso_iec._24727.tech.schema.IFDStatusType in project open-ecard by ecsec.

the class ExecuteRecognition method testExecute.

@Test(enabled = false)
public void testExecute() throws Exception {
    Environment env = new ClientEnv();
    IFD ifd = new org.openecard.ifd.scio.IFD();
    env.setIFD(ifd);
    byte[] ctx;
    // establish context
    EstablishContext eCtx = new EstablishContext();
    EstablishContextResponse eCtxR = ifd.establishContext(eCtx);
    ctx = eCtxR.getContextHandle();
    // get status to see if we can execute the recognition
    GetStatus status = new GetStatus();
    status.setContextHandle(ctx);
    GetStatusResponse statusR = ifd.getStatus(status);
    if (statusR.getIFDStatus().size() > 0 && statusR.getIFDStatus().get(0).getSlotStatus().get(0).isCardAvailable()) {
        CardRecognitionImpl recog = new CardRecognitionImpl(env);
        IFDStatusType stat = statusR.getIFDStatus().get(0);
        RecognitionInfo info = recog.recognizeCard(ctx, stat.getIFDName(), BigInteger.ZERO);
        if (info == null) {
            System.out.println("Card not recognized.");
        } else {
            System.out.println(info.getCardType());
        }
    }
}
Also used : GetStatusResponse(iso.std.iso_iec._24727.tech.schema.GetStatusResponse) IFD(org.openecard.ws.IFD) EstablishContextResponse(iso.std.iso_iec._24727.tech.schema.EstablishContextResponse) ClientEnv(org.openecard.common.ClientEnv) RecognitionInfo(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType.RecognitionInfo) Environment(org.openecard.common.interfaces.Environment) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) EstablishContext(iso.std.iso_iec._24727.tech.schema.EstablishContext) GetStatus(iso.std.iso_iec._24727.tech.schema.GetStatus) Test(org.testng.annotations.Test)

Example 12 with IFDStatusType

use of iso.std.iso_iec._24727.tech.schema.IFDStatusType in project open-ecard by ecsec.

the class EventWatcher method compare.

/**
 * Compares the current status against the expected status and returns the difference if any.
 * This function does not request new information from the hardware, but only uses the last state retrieved. The
 * result of this function can be used as events in
 * {@link org.openecard.ws.IFD#wait(iso.std.iso_iec._24727.tech.schema.Wait)}.
 *
 * @param expectedStatus Status known to the caller of the function.
 * @return The difference between the internal state of this object and the given reference status.
 */
@Nonnull
public List<IFDStatusType> compare(@Nonnull List<IFDStatusType> expectedStatus) {
    ArrayList<IFDStatusType> remaining = new ArrayList<>(currentState);
    for (IFDStatusType nextExpect : expectedStatus) {
        Iterator<IFDStatusType> it = remaining.iterator();
        boolean matchFound = false;
        // see if the current state contains the terminal that is expected to be present
        while (it.hasNext()) {
            IFDStatusType nextRemain = it.next();
            // found matching terminal
            if (nextRemain.getIFDName().equals(nextExpect.getIFDName())) {
                matchFound = true;
                // see if there is any difference between the two
                if (isStateEqual(nextRemain, nextExpect)) {
                    // no difference, so delete this entry
                    it.remove();
                }
                break;
            }
        }
        // if remaining does not contain the expected status, the terminal was removed
        if (!matchFound) {
            IFDStatusType removed = clone(nextExpect);
            removed.setIFDName(nextExpect.getIFDName());
            removed.setConnected(false);
            remaining.add(removed);
        }
    }
    // clone entries, to prevent altering the state of this object from the outside
    return clone(remaining);
}
Also used : ArrayList(java.util.ArrayList) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) Nonnull(javax.annotation.Nonnull)

Example 13 with IFDStatusType

use of iso.std.iso_iec._24727.tech.schema.IFDStatusType in project open-ecard by ecsec.

the class EventWatcher method updateState.

private void updateState(TerminalWatcher.StateChangeEvent event) {
    String name = event.getTerminal();
    if (event.getState() == TerminalWatcher.EventType.TERMINAL_ADDED) {
        currentState.add(createEmptyState(name));
    } else {
        Iterator<IFDStatusType> it = currentState.iterator();
        while (it.hasNext()) {
            IFDStatusType next = it.next();
            SlotStatusType slot = next.getSlotStatus().get(0);
            if (next.getIFDName().equals(name)) {
                switch(event.getState()) {
                    case CARD_INSERTED:
                        try {
                            SingleThreadChannel ch = cm.openMasterChannel(name);
                            slot.setCardAvailable(true);
                            slot.setATRorATS(ch.getChannel().getCard().getATR().getBytes());
                        } catch (NoSuchTerminal | SCIOException ex) {
                            LOG.error("Failed to open master channel for terminal '" + name + "'.", ex);
                            slot.setCardAvailable(false);
                            cm.closeMasterChannel(name);
                        }
                        break;
                    case CARD_REMOVED:
                        cm.closeMasterChannel(name);
                        slot.setCardAvailable(false);
                        break;
                    case TERMINAL_REMOVED:
                        // just in case
                        slot.setCardAvailable(false);
                        next.setConnected(false);
                        break;
                }
                // no need to look any further
                break;
            }
        }
    }
}
Also used : NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) SlotStatusType(iso.std.iso_iec._24727.tech.schema.SlotStatusType)

Example 14 with IFDStatusType

use of iso.std.iso_iec._24727.tech.schema.IFDStatusType in project open-ecard by ecsec.

the class IFD method getStatus.

@Override
public GetStatusResponse getStatus(GetStatus parameters) {
    GetStatusResponse response;
    // you thought of a different IFD obviously
    if (!ByteUtils.compare(ctxHandle, parameters.getContextHandle())) {
        String msg = "Invalid context handle specified.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_CONTEXT_HANDLE, msg);
        response = WSHelper.makeResponse(GetStatusResponse.class, r);
        return response;
    }
    // get specific ifd or all if no specific one is requested
    List<SCIOTerminal> ifds = new LinkedList<>();
    try {
        String requestedIfd = parameters.getIFDName();
        if (requestedIfd != null) {
            try {
                SCIOTerminal t = cm.getTerminals().getTerminal(requestedIfd);
                ifds.add(t);
            } catch (NoSuchTerminal ex) {
                String msg = "The requested IFD name does not exist.";
                LOG.warn(msg, ex);
                String minor = ECardConstants.Minor.IFD.Terminal.UNKNOWN_IFD;
                Result r = WSHelper.makeResult(ECardConstants.Major.ERROR, minor, msg);
                response = WSHelper.makeResponse(GetStatusResponse.class, r);
                return response;
            }
        } else {
            ifds.addAll(cm.getTerminals().list());
        }
    } catch (SCIOException ex) {
        String msg = "Failed to get list with the terminals.";
        LOG.warn(msg, ex);
        response = WSHelper.makeResponse(GetStatusResponse.class, WSHelper.makeResultUnknownError(msg));
        return response;
    }
    // request status for each ifd
    ArrayList<IFDStatusType> status = new ArrayList<>(ifds.size());
    for (SCIOTerminal ifd : ifds) {
        TerminalInfo info;
        try {
            SingleThreadChannel channel = cm.openMasterChannel(ifd.getName());
            info = new TerminalInfo(cm, channel);
        } catch (NoSuchTerminal | SCIOException ex) {
            // continue without a channel
            info = new TerminalInfo(cm, ifd);
        }
        try {
            IFDStatusType s = info.getStatus();
            status.add(s);
        } catch (SCIOException ex) {
            if (ex.getCode() != SCIOErrorCode.SCARD_W_UNPOWERED_CARD && ex.getCode() != SCIOErrorCode.SCARD_W_UNRESPONSIVE_CARD && ex.getCode() != SCIOErrorCode.SCARD_W_UNSUPPORTED_CARD && ex.getCode() != SCIOErrorCode.SCARD_E_PROTO_MISMATCH) {
                String msg = String.format("Failed to determine status of terminal '%s'.", ifd.getName());
                LOG.warn(msg, ex);
                Result r = WSHelper.makeResultUnknownError(msg);
                response = WSHelper.makeResponse(GetStatusResponse.class, r);
                return response;
            } else {
                // fall througth if there is a card which can not be connected
                LOG.info("Ignoring failed status request from terminal.", ex);
            }
        }
    }
    // everything worked out well
    response = WSHelper.makeResponse(GetStatusResponse.class, WSHelper.makeResultOK());
    response.getIFDStatus().addAll(status);
    return response;
}
Also used : NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) GetStatusResponse(iso.std.iso_iec._24727.tech.schema.GetStatusResponse) SCIOException(org.openecard.common.ifd.scio.SCIOException) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOTerminal(org.openecard.common.ifd.scio.SCIOTerminal) ArrayList(java.util.ArrayList) TerminalInfo(org.openecard.ifd.scio.wrapper.TerminalInfo) LinkedList(java.util.LinkedList) Result(oasis.names.tc.dss._1_0.core.schema.Result) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType)

Example 15 with IFDStatusType

use of iso.std.iso_iec._24727.tech.schema.IFDStatusType in project open-ecard by ecsec.

the class IFD method cancel.

@Override
public CancelResponse cancel(Cancel parameters) {
    CancelResponse response;
    // you thought of a different IFD obviously
    if (!ByteUtils.compare(ctxHandle, parameters.getContextHandle())) {
        String msg = "Invalid context handle specified.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_CONTEXT_HANDLE, msg);
        response = WSHelper.makeResponse(CancelResponse.class, r);
        return response;
    }
    String ifdName = parameters.getIFDName();
    String session = parameters.getSessionIdentifier();
    if (session != null) {
        // async wait
        Future<List<IFDStatusType>> f = this.asyncWaitThreads.get(session);
        if (f != null) {
            f.cancel(true);
            response = WSHelper.makeResponse(CancelResponse.class, WSHelper.makeResultOK());
        } else {
            String msg = "No matching Wait call exists for the given session.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.IO.CANCEL_NOT_POSSIBLE, msg);
            response = WSHelper.makeResponse(CancelResponse.class, r);
        }
    } else if (ifdName != null) {
        // sync wait
        synchronized (this) {
            if (syncWaitThread != null) {
                syncWaitThread.cancel(true);
                // not really needed but seems cleaner
                syncWaitThread = null;
                response = WSHelper.makeResponse(CancelResponse.class, WSHelper.makeResultOK());
            } else {
                String msg = "No synchronous Wait to cancel.";
                Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.IO.CANCEL_NOT_POSSIBLE, msg);
                response = WSHelper.makeResponse(CancelResponse.class, r);
            }
        }
    } else {
        // nothing to cancel
        String msg = "Invalid parameters given.";
        response = WSHelper.makeResponse(CancelResponse.class, WSHelper.makeResultUnknownError(msg));
    }
    return response;
}
Also used : CancelResponse(iso.std.iso_iec._24727.tech.schema.CancelResponse) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Aggregations

IFDStatusType (iso.std.iso_iec._24727.tech.schema.IFDStatusType)16 SlotStatusType (iso.std.iso_iec._24727.tech.schema.SlotStatusType)9 ArrayList (java.util.ArrayList)7 Nonnull (javax.annotation.Nonnull)6 SCIOException (org.openecard.common.ifd.scio.SCIOException)5 GetStatusResponse (iso.std.iso_iec._24727.tech.schema.GetStatusResponse)4 NoSuchTerminal (org.openecard.common.ifd.scio.NoSuchTerminal)4 GetStatus (iso.std.iso_iec._24727.tech.schema.GetStatus)3 BigInteger (java.math.BigInteger)3 LinkedList (java.util.LinkedList)3 Result (oasis.names.tc.dss._1_0.core.schema.Result)3 SingleThreadChannel (org.openecard.ifd.scio.wrapper.SingleThreadChannel)3 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)2 WaitResponse (iso.std.iso_iec._24727.tech.schema.WaitResponse)2 List (java.util.List)2 CancelResponse (iso.std.iso_iec._24727.tech.schema.CancelResponse)1 ChannelHandleType (iso.std.iso_iec._24727.tech.schema.ChannelHandleType)1 RecognitionInfo (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType.RecognitionInfo)1 EstablishContext (iso.std.iso_iec._24727.tech.schema.EstablishContext)1 EstablishContextResponse (iso.std.iso_iec._24727.tech.schema.EstablishContextResponse)1