Android Open Source - smartcard-reader Reader Xcvr






From Project

Back to project page smartcard-reader.

License

The source code is released under:

GNU General Public License

If you think the Android project smartcard-reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Ryan Jones/*  ww w  . j  a  v  a 2s .c o m*/
 *
 * This file is part of smartcard-reader, package org.docrj.smartcard.reader.
 *
 * smartcard-reader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * smartcard-reader is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with smartcard-reader. If not, see <http://www.gnu.org/licenses/>.
 */

package org.docrj.smartcard.reader;

import java.io.IOException;

import org.docrj.smartcard.emv.GpoApdu;
import org.docrj.smartcard.iso7816.CommandApdu;
import org.docrj.smartcard.iso7816.ResponseApdu;
import org.docrj.smartcard.iso7816.SelectApdu;
import org.docrj.smartcard.iso7816.TLVUtil;
import org.docrj.smartcard.iso7816.TLVException;
import org.docrj.smartcard.util.Util;

import android.content.Context;
import android.nfc.TagLostException;
import android.nfc.tech.IsoDep;
//import android.util.Log;

public class ReaderXcvr implements Runnable {
    protected static final String TAG = "smartcard-reader";

    public interface UiCallbacks {
        // print console messages
        void onMessageSend(String raw, String name);
        void onMessageRcv(String raw, String name, String parsed);
        void onOkay(String message);
        void onError(String message, boolean clearOnNext);

        // clear console messages
        void clearMessages();

        // ui listeners
        void setUserSelectListener(UiListener callback);
    }

    public interface UiListener {
        void onUserSelect(String aid);
    }

    public static final int SW_NO_ERROR = 0x9000;    

    protected IsoDep mIsoDep;
    protected UiCallbacks mUiCallbacks;
    protected Context mContext;

    protected String mAid;
    protected byte[] mAidBytes;

    public ReaderXcvr(IsoDep isoDep, String aid, UiCallbacks uiCallbacks) {
        this.mIsoDep = isoDep;
        this.mAid = aid;
        this.mAidBytes = Util.hexToBytes(aid);
        this.mUiCallbacks = uiCallbacks;
        this.mContext = (Context) uiCallbacks;
    }

    protected static String bytesToHexAndAscii(byte[] data, boolean ascii) {
        int i;
        if (data.length == 0) {
            return "";
        }
        StringBuilder _sbbuffer = new StringBuilder();
        String _hexline = "";
        String _asciiline = "";
        for (i = 0; i < data.length; i++) {
            _hexline = _hexline.concat(String.format("%02x ", data[i]));
            if (ascii) {
            if (data[i] > 31 && data[i] < 127) {
                _asciiline = _asciiline.concat(String.valueOf((char) data[i]));
            } else {
                _asciiline = _asciiline.concat(".");
            }
            }
        }
        _sbbuffer.append(_hexline);
        // don't waste space w/ ascii if we just have the two status bytes
        if (ascii && data.length > 2) {
            _sbbuffer.append("\n...\t");
            _sbbuffer.append(_asciiline);
        }
        return _sbbuffer.toString();
    }

    // build boilerplate command APDU
    protected byte[] buildCmdApdu(CommandApdu cmdApdu) {
        return cmdApdu.toBytes();
    }

    // build select command APDU
    protected byte[] buildSelectApdu(byte[] aidBytes) {
        SelectApdu cmdApdu = new SelectApdu(aidBytes);
        return cmdApdu.toBytes();
    }

    // send command APDU, get response APDU, and display to user   
    protected ResponseApdu sendAndRcv(CommandApdu cmdApdu, boolean ascii)
            throws TagLostException, IOException {
        byte[] cmdBytes = cmdApdu.toBytes();
        String cmdStr = GpoApdu.toString(cmdBytes, cmdApdu.getLc());
        mUiCallbacks.onMessageSend(cmdStr, cmdApdu.getCommandName());
        byte[] rsp = mIsoDep.transceive(cmdBytes);
        ResponseApdu rspApdu = new ResponseApdu(rsp);
        byte[] data = rspApdu.getData();

        String parsed = null;
        String errMsg = "no error";
        try {
            if (data.length > 0) {
                parsed = TLVUtil.prettyPrintAPDUResponse(data);
            }
        } catch (TLVException e) {
            parsed = null;
            errMsg = e.getMessage();
        }

        mUiCallbacks.onMessageRcv(bytesToHexAndAscii(rsp, ascii), cmdApdu.getCommandName(), parsed);

        if (data.length > 0 && parsed == null) {
            mUiCallbacks.onError(errMsg, false);
        }

        /*
        Log.d(TAG, "response APDU: " + Util.bytesToHex(rsp));
        if (data.length > 0) {
            Log.d(TAG, TLVUtil.prettyPrintAPDUResponse(data));
        }
        */
        return rspApdu;
    }

    @Override
    public void run() {
    }
}




Java Source Code List

org.docrj.smartcard.emv.AppElementaryFile.java
org.docrj.smartcard.emv.AppFileLocator.java
org.docrj.smartcard.emv.AppInterchangeProfile.java
org.docrj.smartcard.emv.AppPriorityIndicator.java
org.docrj.smartcard.emv.DDF.java
org.docrj.smartcard.emv.DOL.java
org.docrj.smartcard.emv.EMVApp.java
org.docrj.smartcard.emv.EMVTags.java
org.docrj.smartcard.emv.EMVTerminal.java
org.docrj.smartcard.emv.GpoApdu.java
org.docrj.smartcard.emv.IssuerIdNumber.java
org.docrj.smartcard.emv.LanguagePref.java
org.docrj.smartcard.emv.LogEntry.java
org.docrj.smartcard.emv.MCTags.java
org.docrj.smartcard.emv.Record.java
org.docrj.smartcard.emv.TagProvider.java
org.docrj.smartcard.emv.TerminalTranQualifiers.java
org.docrj.smartcard.emv.TerminalVerifResults.java
org.docrj.smartcard.emv.VISATags.java
org.docrj.smartcard.iso7816.BERTLV.java
org.docrj.smartcard.iso7816.CommandApdu.java
org.docrj.smartcard.iso7816.ReadRecordApdu.java
org.docrj.smartcard.iso7816.ResponseApdu.java
org.docrj.smartcard.iso7816.SelectApdu.java
org.docrj.smartcard.iso7816.SmartcardException.java
org.docrj.smartcard.iso7816.TLVException.java
org.docrj.smartcard.iso7816.TLVUtil.java
org.docrj.smartcard.iso7816.TagAndLength.java
org.docrj.smartcard.iso7816.TagImpl.java
org.docrj.smartcard.iso7816.TagType.java
org.docrj.smartcard.iso7816.TagValueType.java
org.docrj.smartcard.iso7816.Tag.java
org.docrj.smartcard.reader.ApduParser.java
org.docrj.smartcard.reader.AppAdapter.java
org.docrj.smartcard.reader.DemoReaderXcvr.java
org.docrj.smartcard.reader.FileShareActivity.java
org.docrj.smartcard.reader.ManualReaderXcvr.java
org.docrj.smartcard.reader.MessageAdapter.java
org.docrj.smartcard.reader.OtherReaderXcvr.java
org.docrj.smartcard.reader.PaymentReaderXcvr.java
org.docrj.smartcard.reader.ReaderActivity.java
org.docrj.smartcard.reader.ReaderXcvr.java
org.docrj.smartcard.reader.SmartcardApp.java
org.docrj.smartcard.util.ByteArrayWrapper.java
org.docrj.smartcard.util.ISO3166_1.java
org.docrj.smartcard.util.ISO4217_Numeric.java
org.docrj.smartcard.util.Util.java