Search in sources :

Example 1 with ATR

use of es.inteco.labs.android.usb.device.data.ATR in project jmulticard by ctt-gob-es.

the class SmartCardUsbDevice method resetCCID.

/**
 * Manda un reset al dispositivo CCID.
 * @return ATR devuelto por el dispositivo
 * @throws UsbDeviceException
 * @throws NotAvailableUSBDeviceException
 */
public ATR resetCCID() throws UsbDeviceException, NotAvailableUSBDeviceException {
    if (!isCardPresent()) {
        // $NON-NLS-1$
        throw new UsbDeviceException("No se ha detectado una tarjeta en el lector");
    }
    try {
        // PowerOff
        final UsbCommand powerOffCommand = UsbInstructionFactory.getInstance().getIccPowerOffCommand();
        final UsbResponse powerOffResponse = getChannel().transmit(powerOffCommand);
        if (!powerOffResponse.isOk()) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            throw new UsbDeviceException("Imposible enviar PowerOff al terminal [" + HexUtils.hexify(new byte[] { powerOffResponse.getStatus() }, false) + "] - (" + HexUtils.hexify(new byte[] { powerOffResponse.getError() }, false) + ")");
        }
        // PowerOn
        final UsbCommand powerOnCommand = UsbInstructionFactory.getInstance().getIccPowerOnCommand();
        final UsbResponse powerOnResponse = getChannel().transmit(powerOnCommand);
        if (powerOnResponse.isOk()) {
            return new ATR(powerOnResponse.getDataBytes());
        }
        // $NON-NLS-1$
        throw new UsbDeviceException("Imposible enviar PowerOn al terminal");
    } catch (final UsbCommandTransmissionException e) {
        throw new UsbDeviceException(e);
    } catch (final UsbResponseException e) {
        throw new UsbDeviceException(e);
    } catch (final UsbSmartCardChannelException e) {
        // Es necesario reconectar
        if (this.flag_reconnect_channel++ < MAX_RECONNECT_CHANNEL_RETRIES) {
            releaseChannel();
            return resetCCID();
        }
        throw new UsbDeviceException(e);
    }
}
Also used : UsbCommandTransmissionException(es.inteco.labs.android.usb.device.exception.UsbCommandTransmissionException) UsbResponseException(es.inteco.labs.android.usb.device.exception.UsbResponseException) UsbResponse(es.inteco.labs.android.usb.device.ccid.response.UsbResponse) UsbCommand(es.inteco.labs.android.usb.device.ccid.instruction.UsbCommand) UsbDeviceException(es.inteco.labs.android.usb.device.exception.UsbDeviceException) UsbSmartCardChannelException(es.inteco.labs.android.usb.device.exception.UsbSmartCardChannelException) ATR(es.inteco.labs.android.usb.device.data.ATR)

Aggregations

UsbCommand (es.inteco.labs.android.usb.device.ccid.instruction.UsbCommand)1 UsbResponse (es.inteco.labs.android.usb.device.ccid.response.UsbResponse)1 ATR (es.inteco.labs.android.usb.device.data.ATR)1 UsbCommandTransmissionException (es.inteco.labs.android.usb.device.exception.UsbCommandTransmissionException)1 UsbDeviceException (es.inteco.labs.android.usb.device.exception.UsbDeviceException)1 UsbResponseException (es.inteco.labs.android.usb.device.exception.UsbResponseException)1 UsbSmartCardChannelException (es.inteco.labs.android.usb.device.exception.UsbSmartCardChannelException)1