Search in sources :

Example 1 with UsbDeviceException

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

the class AndroidCCIDConnection method transmit.

/**
 * {@inheritDoc}
 */
@Override
public ResponseApdu transmit(final CommandApdu command) throws ApduConnectionException {
    if (!isOpen()) {
        // $NON-NLS-1$
        throw new UnavailableReaderException("No existe dispositivo USB asignado a la conexion");
    }
    try {
        if (!this.ccidReader.isCardActive()) {
            if (!this.ccidReader.isCardPresent()) {
                // No hay tarjeta en el lector
                throw new CardNotPresentException();
            }
            // Hay tarjeta, pero no esta activa
            // $NON-NLS-1$ //$NON-NLS-2$
            Log.i("es.gob.jmulticard", "La tarjeta del lector no esta activa, se reiniciara");
            this.reset();
        }
        if (DEBUG) {
            // $NON-NLS-1$ //$NON-NLS-2$
            Log.d("es.gob.jmulticard", "APDU Enviada:\n" + HexUtils.hexify(command.getBytes(), true));
        }
        final ResponseApdu response;
        try {
            response = new ResponseApdu(this.ccidReader.transmit(command.getBytes()));
            if (DEBUG) {
                // $NON-NLS-1$ //$NON-NLS-2$
                Log.d("es.gob.jmulticard", "APDU Recibida:\n" + HexUtils.hexify(response.getBytes(), true));
            }
            // Solicitamos el resultado de la operacion si es necesario
            if (response.getStatusWord().getMsb() == TAG_RESPONSE_PENDING) {
                // Si ya se ha devuelto parte de los datos, los concatenamos al resultado
                if (response.getData().length > 0) {
                    final byte[] data = response.getData();
                    final byte[] additionalData = transmit(new GetResponseApduCommand((byte) 0x00, response.getStatusWord().getLsb())).getBytes();
                    final byte[] fullResponse = new byte[data.length + additionalData.length];
                    System.arraycopy(data, 0, fullResponse, 0, data.length);
                    System.arraycopy(additionalData, 0, fullResponse, data.length, additionalData.length);
                    return new ResponseApdu(fullResponse);
                }
                return transmit(new GetResponseApduCommand((byte) 0x00, response.getStatusWord().getLsb()));
            } else // (de eso se encargara la clase de conexion con canal seguro)
            if (response.getStatusWord().getMsb() == TAG_RESPONSE_INVALID_LENGTH && command.getCla() == (byte) 0x00) {
                command.setLe(response.getStatusWord().getLsb());
                return transmit(command);
            }
            return response;
        } catch (final UsbDeviceException e) {
            // $NON-NLS-1$
            throw new ApduConnectionException("Error enviando APDU: " + e, e);
        }
    } catch (final NotAvailableUSBDeviceException e) {
        // $NON-NLS-1$
        throw new UnavailableReaderException("No se puede acceder al dispositivo USB: " + e, e);
    }
}
Also used : UnavailableReaderException(es.gob.jmulticard.apdu.connection.UnavailableReaderException) UsbDeviceException(es.inteco.labs.android.usb.device.exception.UsbDeviceException) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) GetResponseApduCommand(es.gob.jmulticard.apdu.iso7816four.GetResponseApduCommand) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) NotAvailableUSBDeviceException(es.inteco.labs.android.usb.device.exception.NotAvailableUSBDeviceException)

Example 2 with UsbDeviceException

use of es.inteco.labs.android.usb.device.exception.UsbDeviceException 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)

Example 3 with UsbDeviceException

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

the class SmartCardUsbDevice method transmit.

/**
 * Env&iacute;a una APDU al dispositivo.
 * @param apdu Comando APDU
 * @return Respuesta al env&iacute;o (APDU respuesta)
 * @throws UsbDeviceException
 * @throws NotAvailableUSBDeviceException
 */
public byte[] transmit(final byte[] apdu) throws UsbDeviceException, NotAvailableUSBDeviceException {
    try {
        final UsbCommand xfrBlock = UsbInstructionFactory.getInstance().getXfrBlockCommand(apdu);
        final UsbResponse response = getChannel().transmit(xfrBlock);
        if (response.isOk()) {
            this.flag_transmit_retries = 0;
            this.flag_reconnect_channel = 0;
            return response.getDataBytes();
        }
        // $NON-NLS-1$
        throw new UsbDeviceException("Error en la transmision de la APDU");
    } catch (final UsbCommandTransmissionException e) {
        // Es necesario reconectar
        if (this.flag_transmit_retries++ < MAX_TRANSMIT_RETRIES) {
            SystemClock.sleep(RETRY_TIMEOUT);
            return transmit(apdu);
        }
        throw new UsbDeviceException(e);
    } catch (final UsbResponseException e) {
        throw new UsbDeviceException(e);
    } catch (final UsbSmartCardChannelException e) {
        if (this.flag_reconnect_channel++ < MAX_RECONNECT_CHANNEL_RETRIES) {
            releaseChannel();
            return transmit(apdu);
        }
        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)

Aggregations

UsbDeviceException (es.inteco.labs.android.usb.device.exception.UsbDeviceException)3 UsbCommand (es.inteco.labs.android.usb.device.ccid.instruction.UsbCommand)2 UsbResponse (es.inteco.labs.android.usb.device.ccid.response.UsbResponse)2 UsbCommandTransmissionException (es.inteco.labs.android.usb.device.exception.UsbCommandTransmissionException)2 UsbResponseException (es.inteco.labs.android.usb.device.exception.UsbResponseException)2 UsbSmartCardChannelException (es.inteco.labs.android.usb.device.exception.UsbSmartCardChannelException)2 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)1 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)1 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)1 UnavailableReaderException (es.gob.jmulticard.apdu.connection.UnavailableReaderException)1 GetResponseApduCommand (es.gob.jmulticard.apdu.iso7816four.GetResponseApduCommand)1 ATR (es.inteco.labs.android.usb.device.data.ATR)1 NotAvailableUSBDeviceException (es.inteco.labs.android.usb.device.exception.NotAvailableUSBDeviceException)1