use of iso.std.iso_iec._24727.tech.schema.ActionType in project open-ecard by ecsec.
the class IFD method disconnect.
@Override
public synchronized DisconnectResponse disconnect(Disconnect parameters) {
try {
DisconnectResponse response;
if (!hasContext()) {
String msg = "Context not initialized.";
Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
response = WSHelper.makeResponse(DisconnectResponse.class, r);
return response;
}
try {
byte[] handle = parameters.getSlotHandle();
SingleThreadChannel ch = cm.getSlaveChannel(handle);
cm.closeSlaveChannel(handle);
// process actions
SCIOCard card = ch.getChannel().getCard();
ActionType action = parameters.getAction();
if (ActionType.RESET == action) {
String ifdName = card.getTerminal().getName();
SingleThreadChannel master = cm.getMasterChannel(ifdName);
HandlerBuilder builder = HandlerBuilder.create();
ConnectionHandleType cHandleIn = builder.setCardType(ECardConstants.UNKNOWN_CARD).setCardIdentifier(card.getATR().getBytes()).setContextHandle(ctxHandle).setIfdName(ifdName).setSlotIdx(BigInteger.ZERO).buildConnectionHandle();
builder = HandlerBuilder.create();
ConnectionHandleType cHandleRm = builder.setContextHandle(ctxHandle).setIfdName(ifdName).setSlotIdx(BigInteger.ZERO).buildConnectionHandle();
try {
master.reconnect();
evManager.resetCard(cHandleRm, cHandleIn, card.getProtocol().toUri());
} catch (IllegalStateException ex) {
LOG.warn("Card reconnect failed, trying to establish new card connection.", ex);
cm.closeMasterChannel(ifdName);
LOG.debug("Master channel closed successfully.");
try {
cm.getMasterChannel(ifdName);
LOG.debug("New card connection established successfully.");
evManager.resetCard(cHandleRm, cHandleIn, card.getProtocol().toUri());
} catch (NoSuchTerminal ex2) {
LOG.error("No terminal present anymore.", ex);
}
}
}
// TODO: take care of other actions (probably over ControlIFD)
// the default is to not disconnect the card, because all existing connections would be broken
response = WSHelper.makeResponse(DisconnectResponse.class, WSHelper.makeResultOK());
return response;
} catch (NoSuchChannel ex) {
String msg = "No card available in the requested terminal.";
Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
response = WSHelper.makeResponse(DisconnectResponse.class, r);
LOG.warn(msg, ex);
return response;
} catch (SCIOException ex) {
String msg = "Unknown error in the underlying SCIO implementation.";
Result r = WSHelper.makeResultUnknownError(msg);
response = WSHelper.makeResponse(DisconnectResponse.class, r);
LOG.warn(msg, ex);
return response;
}
} catch (Exception ex) {
LOG.warn(ex.getMessage(), ex);
throwThreadKillException(ex);
return WSHelper.makeResponse(DisconnectResponse.class, WSHelper.makeResult(ex));
}
}
Aggregations