├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── android-demo
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── org
│ │ └── openjavacard
│ │ └── android
│ │ └── demo
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── org
│ │ │ └── openjavacard
│ │ │ └── smartcardio
│ │ │ └── android
│ │ │ └── demo
│ │ │ ├── activity
│ │ │ ├── AboutActivity.java
│ │ │ ├── MainActivity.java
│ │ │ └── SettingsActivity.java
│ │ │ ├── adapter
│ │ │ └── TerminalsAdapter.java
│ │ │ ├── application
│ │ │ └── DemoPreferences.java
│ │ │ └── fragment
│ │ │ ├── AboutFragment.java
│ │ │ ├── SettingsFragment.java
│ │ │ ├── TerminalInfoFragment.java
│ │ │ └── TerminalListFragment.java
│ └── res
│ │ ├── layout
│ │ ├── activity_about.xml
│ │ ├── activity_main.xml
│ │ ├── activity_settings.xml
│ │ ├── fragment_about.xml
│ │ ├── fragment_terminal.xml
│ │ └── item_terminal.xml
│ │ ├── menu
│ │ └── menu_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ │ └── xml
│ │ └── settings.xml
│ └── test
│ └── java
│ └── org
│ └── openjavacard
│ └── android
│ └── demo
│ └── ExampleUnitTest.java
├── android-nfc
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── org
│ │ └── openjavacard
│ │ └── smartcardio
│ │ └── android
│ │ └── nfc
│ │ ├── AndroidNfcSCIO.java
│ │ ├── NfcCard.java
│ │ ├── NfcCardChannel.java
│ │ ├── NfcCardTerminal.java
│ │ └── NfcCardTerminals.java
│ └── res
│ └── values
│ └── strings.xml
├── android-omapi
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── org
│ │ └── openjavacard
│ │ └── smartcardio
│ │ └── android
│ │ └── omapi
│ │ ├── AndroidOmapiSCIO.java
│ │ ├── OmapiCard.java
│ │ ├── OmapiCardChannel.java
│ │ ├── OmapiCardTerminal.java
│ │ └── OmapiCardTerminals.java
│ └── res
│ └── values
│ └── strings.xml
├── build.gradle
├── framework
├── build.gradle
└── src
│ └── main
│ └── java
│ └── javax
│ └── smartcardio
│ ├── ATR.java
│ ├── Card.java
│ ├── CardChannel.java
│ ├── CardException.java
│ ├── CardNotPresentException.java
│ ├── CardTerminal.java
│ ├── CardTerminals.java
│ ├── CommandAPDU.java
│ ├── ResponseAPDU.java
│ ├── TerminalFactory.java
│ └── package-info.java
├── generic
├── build.gradle
└── src
│ └── main
│ └── java
│ └── org
│ └── openjavacard
│ └── smartcardio
│ └── generic
│ ├── GenericCard.java
│ ├── GenericCardChannel.java
│ ├── GenericCardTerminal.java
│ ├── GenericCardTerminals.java
│ └── package-info.java
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | out/**
2 | build/**
3 | .gradle/**
4 | .idea/**
5 | **/*.iml
6 | **/*.ipr
7 | **/build
8 | local.properties
9 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | jdk: openjdk8
3 | android:
4 | components:
5 | - android-28
6 | - build-tools-28.0.3
7 | - platform-tools
8 | - tools
9 | before_cache:
10 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
11 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
12 | cache:
13 | directories:
14 | - $HOME/.m2
15 | - $HOME/.gradle/caches/
16 | - $HOME/.gradle/wrapper/
17 | - $HOME/.android/build-cache
18 | #before_script:
19 | # - echo no | android create avd --force -n test -t android-28 --abi armeabi-v7a
20 | # - emulator -avd test -no-skin -no-audio -no-window &
21 | # - android-wait-for-emulator
22 | # - adb shell input keyevent 82 &
23 | script: ./gradlew build
24 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
166 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## OpenJavaCard SmartcardIO
2 |
3 | This is an implementation of the javax.smartcardio API. It can be used on platforms such as Android that do not provide this framework.
4 |
5 | The project contains both an independent framework implementation and interfaces for Android smartcard access methods.
6 |
7 | We implemented this because we need it for our Android applications.
8 |
9 | [](https://travis-ci.org/OpenJavaCard/openjavacard-smartcardio)
10 |
11 | ### Features
12 |
13 | * Android support
14 | * Access NFC cards and tokens
15 | * Access SIM cards and other SE via OMAPI
16 | * Demo app
17 | * Framework
18 | * Not fully completed
19 |
20 | ### Status
21 |
22 | * Not completed
23 | * Goals
24 | * Support NFC first and well
25 | * Support OMAPI well
26 | * Want to support USB
27 | * Want to support Bluetooth/BLE
28 |
29 | ### Acknowledgements
30 |
31 | We are not the first ones to think of doing something like this.
32 |
33 | ### Legal
34 |
35 | Vast majority of the code has been developed for this project:
36 |
37 | ```
38 | openjavacard-smartcardio: OpenJavaCard SmartcardIO
39 | Copyright (C) 2018-2019 Ingo Albrecht, prom@berlin.ccc.de
40 |
41 | This library is free software; you can redistribute it and/or
42 | modify it under the terms of the GNU Lesser General Public
43 | License as published by the Free Software Foundation; either
44 | version 3.0 of the License, or (at your option) any later version.
45 |
46 | This library is distributed in the hope that it will be useful,
47 | but WITHOUT ANY WARRANTY; without even the implied warranty of
48 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49 | Lesser General Public License for more details.
50 |
51 | You should have received a copy of the GNU Lesser General Public
52 | License along with this library; if not, write to the Free Software
53 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
54 |
--------------------------------------------------------------------------------
/android-demo/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion "28.0.3"
6 | defaultConfig {
7 | applicationId "org.openjavacard.smartcardio.android.demo"
8 | minSdkVersion 20
9 | targetSdkVersion 28
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | vectorDrawables.useSupportLibrary = true
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | sourceSets {
22 | main {
23 | // disable NDK build
24 | jni.srcDirs = []
25 | }
26 | }
27 | }
28 |
29 | dependencies {
30 | implementation project(':android-nfc')
31 | implementation project(':android-omapi')
32 | // https://mvnrepository.com/artifact/com.android.support/appcompat-v7
33 | implementation 'com.android.support:appcompat-v7:28.0.0'
34 | // https://mvnrepository.com/artifact/com.android.support/cardview-v7
35 | implementation 'com.android.support:cardview-v7:28.0.0'
36 | // https://mvnrepository.com/artifact/com.android.support/design
37 | implementation 'com.android.support:design:28.0.0'
38 | // https://mvnrepository.com/artifact/com.android.support/preference-v7
39 | implementation 'com.android.support:preference-v7:28.0.0'
40 | // https://mvnrepository.com/artifact/com.android.support.constraint/constraint-layout
41 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
42 | // https://mvnrepository.com/artifact/junit/junit
43 | testImplementation 'junit:junit:4.12'
44 | // https://mvnrepository.com/artifact/com.android.support.test.espresso/espresso-core
45 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
46 | exclude group: 'com.android.support', module: 'support-annotations'
47 | })
48 | }
49 |
--------------------------------------------------------------------------------
/android-demo/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /opt/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/android-demo/src/androidTest/java/org/openjavacard/android/demo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.android.demo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 | import org.junit.Test;
7 | import org.junit.runner.RunWith;
8 |
9 | import static org.junit.Assert.assertEquals;
10 |
11 | /**
12 | * Instrumentation test, which will execute on an Android device.
13 | *
14 | * @see Testing documentation
15 | */
16 | @RunWith(AndroidJUnit4.class)
17 | public class ExampleInstrumentedTest {
18 | @Test
19 | public void useAppContext() throws Exception {
20 | // Context of the app under test.
21 | Context appContext = InstrumentationRegistry.getTargetContext();
22 |
23 | assertEquals("org.openjavacard.smartcardio.android.smartcardio.demo", appContext.getPackageName());
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/android-demo/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
18 |
19 |
20 |
21 |
22 |
23 |
27 |
30 |
31 |
35 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/activity/AboutActivity.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.activity;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 | import org.openjavacard.smartcardio.android.demo.R;
7 |
8 | public class AboutActivity extends AppCompatActivity {
9 |
10 | private static final String TAG = AboutActivity.class.getName();
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | Log.d(TAG, "onCreate()");
15 | super.onCreate(savedInstanceState);
16 | // set the layout
17 | setContentView(R.layout.activity_about);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.activity;
2 |
3 | import android.content.Intent;
4 | import android.content.SharedPreferences;
5 | import android.os.Bundle;
6 | import android.os.Handler;
7 | import android.os.PowerManager;
8 | import android.support.v4.app.FragmentManager;
9 | import android.support.v7.app.ActionBar;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.support.v7.preference.PreferenceManager;
12 | import android.util.Log;
13 | import android.view.Menu;
14 | import android.view.MenuInflater;
15 | import android.view.MenuItem;
16 | import org.openjavacard.smartcardio.android.demo.R;
17 | import org.openjavacard.smartcardio.android.demo.application.DemoPreferences;
18 | import org.openjavacard.smartcardio.android.demo.fragment.TerminalInfoFragment;
19 | import org.openjavacard.smartcardio.android.demo.fragment.TerminalListFragment;
20 | import org.openjavacard.smartcardio.android.nfc.AndroidNfcSCIO;
21 | import org.openjavacard.smartcardio.android.omapi.AndroidOmapiSCIO;
22 | import org.openjavacard.smartcardio.generic.GenericCardTerminal;
23 | import org.openjavacard.smartcardio.generic.GenericCardTerminals;
24 |
25 | import java.util.ArrayList;
26 | import java.util.List;
27 |
28 | public class MainActivity extends AppCompatActivity
29 | implements GenericCardTerminals.Listener,
30 | SharedPreferences.OnSharedPreferenceChangeListener, FragmentManager.OnBackStackChangedListener{
31 |
32 | private static final String TAG = MainActivity.class.getName();
33 |
34 | private Handler mHandler;
35 |
36 | private SharedPreferences mPreferences;
37 |
38 | private FragmentManager mFragmentManager;
39 | private TerminalListFragment mTerminalList;
40 | private TerminalInfoFragment mTerminalInfo;
41 |
42 | private PowerManager mPowerManager;
43 | private PowerManager.WakeLock mWakeLock;
44 | private Runnable mWakeTimer;
45 |
46 | private AndroidNfcSCIO mNfcSC;
47 | private AndroidOmapiSCIO mOmapiSC;
48 |
49 | @Override
50 | protected void onCreate(Bundle savedInstanceState) {
51 | Log.d(TAG, "onCreate()");
52 | super.onCreate(savedInstanceState);
53 | // set the layout
54 | setContentView(R.layout.activity_main);
55 | // set up action bar
56 | ActionBar actionBar = getSupportActionBar();
57 | actionBar.setTitle(R.string.activity_main_title);
58 | // create handler
59 | mHandler = new Handler(getMainLooper());
60 | // get prefs
61 | mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
62 | mPreferences.registerOnSharedPreferenceChangeListener(this);
63 | // get fragment manager and set up up navigation
64 | mFragmentManager = getSupportFragmentManager();
65 | mFragmentManager.addOnBackStackChangedListener(this);
66 | // create fragments
67 | mTerminalList = new TerminalListFragment();
68 | mTerminalInfo = new TerminalInfoFragment();
69 | // create SCIO interfaces
70 | mNfcSC = new AndroidNfcSCIO(this);
71 | mNfcSC.getTerminals().setListener(this);
72 | mOmapiSC = new AndroidOmapiSCIO(this);
73 | mOmapiSC.getTerminals().setListener(this);
74 | // set up wake lock
75 | mPowerManager = (PowerManager)getSystemService(POWER_SERVICE);
76 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
77 | mWakeTimer = new Runnable() {
78 | @Override
79 | public void run() {
80 | releaseWakeLock();
81 | }
82 | };
83 | // start with the terminal list
84 | switchToTerminalList();
85 | }
86 |
87 | @Override
88 | public boolean onCreateOptionsMenu(Menu menu) {
89 | Log.d(TAG, "onCreateOptionsMenu()");
90 | super.onCreateOptionsMenu(menu);
91 | MenuInflater inflater = getMenuInflater();
92 | inflater.inflate(R.menu.menu_main, menu);
93 | return true;
94 | }
95 |
96 | @Override
97 | public boolean onOptionsItemSelected(MenuItem item) {
98 | Log.d(TAG, "onOptionsItemSelected(" + item.getTitle() + ")");
99 | switch (item.getItemId()) {
100 | case R.id.action_settings:
101 | startSettings();
102 | return true;
103 | case R.id.action_about:
104 | startAbout();
105 | return true;
106 | }
107 | return super.onOptionsItemSelected(item);
108 | }
109 |
110 | @Override
111 | protected void onResume() {
112 | Log.d(TAG, "onResume()");
113 | super.onResume();
114 | mNfcSC.enable();
115 | mOmapiSC.enable();
116 | acquireWakeLock();
117 | updateUpEnabled();
118 | updateTerminals();
119 | updateWakeTimer();
120 | }
121 |
122 | @Override
123 | protected void onPause() {
124 | Log.d(TAG, "onPause()");
125 | releaseWakeLock();
126 | mOmapiSC.disable();
127 | mNfcSC.disable();
128 | super.onPause();
129 | }
130 |
131 | @Override
132 | protected void onDestroy() {
133 | Log.d(TAG, "onDestroy()");
134 | mNfcSC = null;
135 | super.onDestroy();
136 | }
137 |
138 | @Override
139 | public void onTerminalAdded(GenericCardTerminals terminals, GenericCardTerminal terminal) {
140 | Log.d(TAG, "onTerminalAdded(" + terminal.getName() + ")");
141 | updateTerminals();
142 | }
143 |
144 | @Override
145 | public void onTerminalRemoved(GenericCardTerminals terminals, GenericCardTerminal terminal) {
146 | Log.d(TAG, "onTerminalRemoved(" + terminal.getName() + ")");
147 | updateTerminals();
148 | }
149 |
150 | @Override
151 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
152 | Log.d(TAG, "onPreferenceChange(" + key + ")");
153 | switch(key) {
154 | case DemoPreferences.STAY_AWAKE:
155 | updateWakeTimer();
156 | break;
157 | case DemoPreferences.USE_NFC:
158 | case DemoPreferences.USE_OMAPI:
159 | updateTerminals();
160 | break;
161 | }
162 | }
163 |
164 | @Override
165 | public void onBackStackChanged() {
166 | Log.d(TAG, "onBackStackChanged()");
167 | updateUpEnabled();
168 | }
169 |
170 | @Override
171 | public boolean onSupportNavigateUp() {
172 | Log.d(TAG, "onSupportNavigateUp()");
173 | mFragmentManager.popBackStack();
174 | return true;
175 | }
176 |
177 | private void acquireWakeLock() {
178 | Log.d(TAG, "acquireWakeLock()");
179 | if(!mWakeLock.isHeld()) {
180 | mWakeLock.acquire();
181 | updateWakeTimer();
182 | }
183 | }
184 |
185 | private void releaseWakeLock() {
186 | Log.d(TAG, "releaseWakeLock()");
187 | if(mWakeLock.isHeld()) {
188 | mWakeLock.release();
189 | }
190 | }
191 |
192 | private void updateUpEnabled() {
193 | boolean haveBackStack = mFragmentManager.getBackStackEntryCount() > 0;
194 | getSupportActionBar().setDisplayHomeAsUpEnabled(haveBackStack);
195 | }
196 |
197 | private void updateWakeTimer() {
198 | Log.d(TAG, "updateWakeTimer()");
199 | String stayAwakeString = mPreferences.getString(DemoPreferences.STAY_AWAKE, "60");
200 | int stayAwake = Integer.parseInt(stayAwakeString);
201 | mHandler.removeCallbacks(mWakeTimer);
202 | if(stayAwake > 0) {
203 | mHandler.postDelayed(mWakeTimer, stayAwake * 1000);
204 | }
205 | }
206 |
207 | private void startSettings() {
208 | Log.d(TAG, "startSettings()");
209 | startActivity(new Intent(this, SettingsActivity.class));
210 | }
211 |
212 | private void startAbout() {
213 | Log.d(TAG, "startAbout()");
214 | startActivity(new Intent(this, AboutActivity.class));
215 | }
216 |
217 | public void switchToTerminalList() {
218 | Log.d(TAG, "switchToTerminalList()");
219 | updateWakeTimer();
220 | updateTerminals();
221 | mFragmentManager.beginTransaction()
222 | .replace(R.id.main_layout, mTerminalList)
223 | .commit();
224 | }
225 |
226 | public void switchToTerminalInfo(GenericCardTerminal terminal) {
227 | Log.d(TAG, "switchToTerminalInfo(" + terminal + ")");
228 | updateWakeTimer();
229 | mTerminalInfo.setTerminal(terminal);
230 | mFragmentManager.beginTransaction()
231 | .replace(R.id.main_layout, mTerminalInfo)
232 | .addToBackStack(null)
233 | .commit();
234 | }
235 |
236 | private void updateTerminals() {
237 | Log.d(TAG, "updateTerminals()");
238 | // check preferences
239 | boolean useOMAPI = mPreferences.getBoolean(DemoPreferences.USE_OMAPI, false);
240 | boolean useNFC = mPreferences.getBoolean(DemoPreferences.USE_NFC, false);
241 | // collect all terminals
242 | List terminals = new ArrayList<>();
243 | if(useOMAPI) {
244 | terminals.addAll(mOmapiSC.getTerminals().getTerminals());
245 | }
246 | if(useNFC) {
247 | terminals.addAll(mNfcSC.getTerminals().getTerminals());
248 | }
249 | // update the list
250 | mTerminalList.setTerminals(terminals);
251 | }
252 |
253 | }
254 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/activity/SettingsActivity.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.activity;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.ActionBar;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.Log;
8 | import org.openjavacard.smartcardio.android.demo.R;
9 |
10 | public class SettingsActivity extends AppCompatActivity {
11 |
12 | private static final String TAG = SettingsActivity.class.getName();
13 |
14 | @Override
15 | protected void onCreate(@Nullable Bundle savedInstanceState) {
16 | Log.d(TAG, "onCreate()");
17 | super.onCreate(savedInstanceState);
18 | // set the layout
19 | setContentView(R.layout.activity_settings);
20 | // configure the action bar
21 | ActionBar actionbar = getSupportActionBar();
22 | if(actionbar != null) {
23 | // enable up-navigation
24 | actionbar.setDisplayHomeAsUpEnabled(true);
25 | // set up action bar
26 | actionbar.setTitle(R.string.activity_settings_title);
27 | }
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/adapter/TerminalsAdapter.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.adapter;
2 |
3 | import android.view.View;
4 | import android.view.ViewGroup;
5 | import android.widget.BaseAdapter;
6 |
7 | public class TerminalsAdapter extends BaseAdapter {
8 |
9 | @Override
10 | public int getCount() {
11 | return 0;
12 | }
13 |
14 | @Override
15 | public int getViewTypeCount() {
16 | return 2;
17 | }
18 |
19 | @Override
20 | public Object getItem(int position) {
21 | return null;
22 | }
23 |
24 | @Override
25 | public long getItemId(int position) {
26 | return 0;
27 | }
28 |
29 | @Override
30 | public int getItemViewType(int position) {
31 | return super.getItemViewType(position);
32 | }
33 |
34 | @Override
35 | public View getView(int position, View convertView, ViewGroup parent) {
36 | return null;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/application/DemoPreferences.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.application;
2 |
3 | public class DemoPreferences {
4 |
5 | public static final String STAY_AWAKE = "pref_stay_awake";
6 | public static final String USE_NFC = "pref_use_nfc";
7 | public static final String USE_OMAPI = "pref_use_omapi";
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/fragment/AboutFragment.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.Fragment;
7 | import android.util.Log;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import org.openjavacard.smartcardio.android.demo.R;
12 |
13 | public class AboutFragment extends Fragment {
14 |
15 | private static final String TAG = AboutFragment.class.getName();
16 |
17 | @Nullable
18 | @Override
19 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
20 | Log.d(TAG, "onCreateView()");
21 | View view = inflater.inflate(R.layout.fragment_about, container, false);
22 | return view;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/fragment/SettingsFragment.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.fragment;
2 |
3 | import android.content.SharedPreferences;
4 | import android.os.Bundle;
5 | import android.support.v14.preference.PreferenceFragment;
6 | import android.support.v7.preference.ListPreference;
7 | import android.support.v7.preference.Preference;
8 | import android.support.v7.preference.PreferenceManager;
9 | import android.util.Log;
10 | import org.openjavacard.smartcardio.android.demo.R;
11 |
12 | import java.util.Map;
13 |
14 | public class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {
15 |
16 | private static final String TAG = SettingsFragment.class.getName();
17 |
18 | private SharedPreferences mPreferences;
19 |
20 | @Override
21 | public void onCreate(Bundle savedInstanceState) {
22 | Log.d(TAG, "onCreate()");
23 | super.onCreate(savedInstanceState);
24 | }
25 |
26 | @Override
27 | public void onCreatePreferences(Bundle savedState, String rootKey) {
28 | Log.d(TAG, "onCreatePreferences()");
29 | // create preferences from XML
30 | addPreferencesFromResource(R.xml.settings);
31 | // bind summaries of list preferences
32 | PreferenceManager pm = getPreferenceManager();
33 | mPreferences = pm.getSharedPreferences();
34 | Map map = mPreferences.getAll();
35 | if(map != null) {
36 | for (String key : map.keySet()) {
37 | Preference pref = pm.findPreference(key);
38 | bindPreferenceSummaryToValue(pref);
39 | }
40 | }
41 | }
42 |
43 | @Override
44 | public boolean onPreferenceChange(Preference preference, Object newValue) {
45 | Log.d(TAG, "onPreferenceChange(" + preference.getKey() + ")");
46 | String stringValue = newValue.toString();
47 | if (preference instanceof ListPreference) {
48 | ListPreference listPreference = (ListPreference) preference;
49 | int index = listPreference.findIndexOfValue(stringValue);
50 | preference.setSummary(
51 | index >= 0 ? listPreference.getEntries()[index] : "");
52 | } else {
53 | preference.setSummary(stringValue);
54 | }
55 | return true;
56 | }
57 |
58 | private void bindPreferenceSummaryToValue(Preference preference) {
59 | Log.v(TAG, "bindPreferenceSummaryToValue(" + preference.getKey() + ")");
60 | if(preference instanceof ListPreference) {
61 | preference.setOnPreferenceChangeListener(this);
62 | onPreferenceChange(preference, ((ListPreference) preference).getValue());
63 | }
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/fragment/TerminalInfoFragment.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.Fragment;
7 | import android.support.v4.app.FragmentManager;
8 | import android.support.v7.widget.CardView;
9 | import android.util.Log;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.Button;
14 | import android.widget.TextView;
15 | import org.openjavacard.smartcardio.android.demo.R;
16 | import org.openjavacard.smartcardio.android.demo.activity.MainActivity;
17 | import org.openjavacard.smartcardio.generic.GenericCardTerminal;
18 |
19 | import javax.smartcardio.Card;
20 | import javax.smartcardio.CardException;
21 |
22 | public class TerminalInfoFragment extends Fragment implements View.OnClickListener {
23 |
24 | private static final String TAG = TerminalInfoFragment.class.getName();
25 |
26 | /** Our parent activity */
27 | private MainActivity mActivity;
28 | /** Fragment manager (for stack interactions) */
29 | private FragmentManager mFragmentManager;
30 |
31 | /** True once views have been instantiated */
32 | private boolean mInitialized;
33 |
34 | private GenericCardTerminal mTerminal;
35 | private Card mCard;
36 |
37 | private CardView mTerminalLayout;
38 | private CardView mCardLayout;
39 |
40 | private TextView mTerminalClass;
41 | private TextView mTerminalName;
42 | private TextView mCardClass;
43 | private TextView mCardProtocol;
44 | private TextView mCardATR;
45 |
46 | private Button mDismissButton;
47 |
48 | @Override
49 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
50 | Log.d(TAG, "onActivityCreated()");
51 | super.onActivityCreated(savedInstanceState);
52 | mActivity = (MainActivity)getActivity();
53 | mFragmentManager = getFragmentManager();
54 | }
55 |
56 | @Nullable
57 | @Override
58 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
59 | Log.d(TAG, "onCreateView()");
60 | // instantiate things
61 | View view = inflater.inflate(R.layout.fragment_terminal, container, false);
62 | // get layout containers
63 | mTerminalLayout = view.findViewById(R.id.terminal_layout);
64 | mCardLayout = view.findViewById(R.id.card_layout);
65 | // get info fields
66 | mTerminalClass = view.findViewById(R.id.terminal_info_class);
67 | mTerminalName = view.findViewById(R.id.terminal_info_name);
68 | mCardClass = view.findViewById(R.id.card_info_class);
69 | mCardProtocol = view.findViewById(R.id.card_info_protocol);
70 | mCardATR = view.findViewById(R.id.card_info_atr);
71 | // get dismiss button
72 | mDismissButton = view.findViewById(R.id.dismiss_button);
73 | mDismissButton.setOnClickListener(this);
74 | // we are now initialized
75 | mInitialized = true;
76 | // update views
77 | updateViews();
78 | // return root
79 | return view;
80 | }
81 |
82 | public void setTerminal(GenericCardTerminal terminal) {
83 | Card card = null;
84 | try {
85 | if(terminal != null) {
86 | if(terminal.isCardPresent()) {
87 | card = terminal.connect("*");
88 | }
89 | }
90 | } catch (CardException e) {
91 | e.printStackTrace();
92 | }
93 | // remember things
94 | mTerminal = terminal;
95 | mCard = card;
96 | // update views
97 | updateViews();
98 | }
99 |
100 | private void updateViews() {
101 | if(!mInitialized) {
102 | return;
103 | }
104 |
105 | mTerminalLayout.setVisibility(mTerminal == null ? View.GONE : View.VISIBLE);
106 | if(mTerminal != null) {
107 | mTerminalClass.setText(mTerminal.getClass().getSimpleName());
108 | mTerminalName.setText(mTerminal.getName());
109 | }
110 |
111 | mCardLayout.setVisibility(mCard == null ? View.GONE : View.VISIBLE);
112 | if(mCard != null) {
113 | mCardClass.setText(mCard.getClass().getSimpleName());
114 | mCardProtocol.setText(mCard.getProtocol());
115 | }
116 | }
117 |
118 | @Override
119 | public void onClick(View v) {
120 | if(v == mDismissButton) {
121 | mFragmentManager.popBackStack();
122 | }
123 | }
124 |
125 | }
126 |
--------------------------------------------------------------------------------
/android-demo/src/main/java/org/openjavacard/smartcardio/android/demo/fragment/TerminalListFragment.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.demo.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.ListFragment;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.widget.ArrayAdapter;
10 | import android.widget.ListView;
11 | import org.openjavacard.smartcardio.android.demo.R;
12 | import org.openjavacard.smartcardio.android.demo.activity.MainActivity;
13 | import org.openjavacard.smartcardio.generic.GenericCardTerminal;
14 | import org.openjavacard.smartcardio.generic.GenericCardTerminals;
15 |
16 | import java.util.List;
17 |
18 | public class TerminalListFragment extends ListFragment {
19 |
20 | private static final String TAG = TerminalListFragment.class.getName();
21 |
22 | private MainActivity mActivity;
23 |
24 | private List mTerminals;
25 | private ArrayAdapter mAdapter;
26 |
27 | @Override
28 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
29 | Log.d(TAG, "onActivityCreated()");
30 | super.onActivityCreated(savedInstanceState);
31 | mActivity = (MainActivity)getActivity();
32 | update();
33 | }
34 |
35 | @Override
36 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
37 | Log.d(TAG, "onViewCreated()");
38 | super.onViewCreated(view, savedInstanceState);
39 | mAdapter = new ArrayAdapter<>(getContext(), R.layout.item_terminal);
40 | setListAdapter(mAdapter);
41 | update();
42 | }
43 |
44 | @Override
45 | public void onResume() {
46 | Log.d(TAG, "onResume()");
47 | super.onResume();
48 | update();
49 | }
50 |
51 | public void setTerminals(List terminals) {
52 | mTerminals = terminals;
53 | update();
54 | }
55 |
56 | @Override
57 | public void onListItemClick(ListView l, View v, int position, long id) {
58 | GenericCardTerminal terminal = mAdapter.getItem(position);
59 | mActivity.switchToTerminalInfo(terminal);
60 | }
61 |
62 | private void update() {
63 | if(mActivity != null) {
64 | mActivity.runOnUiThread(new Runnable() {
65 | @Override
66 | public void run() {
67 | mAdapter.clear();
68 | if(mTerminals != null) {
69 | mAdapter.addAll(mTerminals);
70 | }
71 | }
72 | });
73 | }
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/layout/activity_about.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/layout/activity_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/layout/fragment_about.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
15 |
16 |
19 |
20 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/layout/fragment_terminal.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
20 |
21 |
24 |
30 |
33 |
35 |
41 |
47 |
48 |
50 |
56 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
78 |
79 |
82 |
88 |
91 |
93 |
99 |
105 |
106 |
108 |
114 |
120 |
121 |
123 |
129 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
149 |
150 |
151 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/layout/item_terminal.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/android-demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/android-demo/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SmartcardIO Demo
3 |
4 | SCIO Demo
5 | SmartcardIO Demo
6 | SCIO Settings
7 | SmartcardIO Settings
8 | About SmartcardIO
9 |
10 | Settings
11 | About
12 |
13 | General
14 | Stay awake
15 | NFC
16 | Use NFC
17 | OMAPI
18 | Use OMAPI
19 |
20 | Card
21 | Terminal
22 | Dismiss
23 |
24 |
25 | - System default
26 | - 1 minute
27 | - 2 minutes
28 | - 5 minutes
29 | - Indefinitely
30 |
31 |
32 | - -1
33 | - 60
34 | - 120
35 | - 300
36 | - 0
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/android-demo/src/main/res/xml/settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
15 |
16 |
17 |
18 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/android-demo/src/test/java/org/openjavacard/android/demo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.android.demo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.assertEquals;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/android-nfc/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion "28.0.3"
6 | defaultConfig {
7 | minSdkVersion 20
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | sourceSets {
20 | main {
21 | // disable NDK build
22 | jni.srcDirs = []
23 | }
24 | }
25 | }
26 |
27 | dependencies {
28 | api project(':framework')
29 | api project(':generic')
30 | // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
31 | implementation 'org.slf4j:slf4j-api:1.7.25'
32 | // https://mvnrepository.com/artifact/junit/junit
33 | testImplementation 'junit:junit:4.12'
34 | // https://mvnrepository.com/artifact/com.android.support.test.espresso/espresso-core
35 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
36 | exclude group: 'com.android.support', module: 'support-annotations'
37 | })
38 | }
39 |
--------------------------------------------------------------------------------
/android-nfc/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /opt/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/android-nfc/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/android-nfc/src/main/java/org/openjavacard/smartcardio/android/nfc/AndroidNfcSCIO.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.nfc;
2 |
3 | import android.app.Activity;
4 | import android.nfc.NfcAdapter;
5 | import android.nfc.Tag;
6 | import android.nfc.tech.IsoDep;
7 | import android.os.Handler;
8 | import android.util.Log;
9 | import org.openjavacard.smartcardio.generic.GenericCardTerminal;
10 |
11 | import javax.smartcardio.CardException;
12 |
13 | public class AndroidNfcSCIO {
14 |
15 | private static final String TAG = AndroidNfcSCIO.class.getName();
16 |
17 | /** Polling interval for card removal detection */
18 | private static final long POLL_INTERVAL = 1000;
19 |
20 | /** Activity that we are using for NFC */
21 | private final Activity mActivity;
22 | /** Handler for polling */
23 | private final Handler mHandler;
24 | /** Runnable doing the polling */
25 | private final NfcPollerRunnable mPoller;
26 | /** Adapter in use */
27 | private NfcAdapter mAdapter;
28 | /** SmartcardIO terminals object */
29 | private NfcCardTerminals mTerminals;
30 |
31 | public AndroidNfcSCIO(Activity activity) {
32 | mActivity = activity;
33 | mHandler = new Handler(activity.getMainLooper());
34 | mPoller = new NfcPollerRunnable();
35 | mAdapter = null;
36 | mTerminals = new NfcCardTerminals();
37 | }
38 |
39 | public NfcCardTerminals getTerminals() {
40 | return mTerminals;
41 | }
42 |
43 | public boolean isNfcSupported() {
44 | NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mActivity);
45 | return adapter != null;
46 | }
47 |
48 | public boolean isNfcEnabled() {
49 | NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mActivity);
50 | return adapter != null && adapter.isEnabled();
51 | }
52 |
53 | public boolean isEnabled() {
54 | return mAdapter != null;
55 | }
56 |
57 | public void enable() {
58 | Log.d(TAG, "enable()");
59 | NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mActivity);
60 | int flags =
61 | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
62 | | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
63 | | NfcAdapter.FLAG_READER_NFC_A;
64 | adapter.enableReaderMode(mActivity, new NfcReaderCallback(), flags, null);
65 | mAdapter = adapter;
66 | mHandler.postDelayed(mPoller, POLL_INTERVAL);
67 | }
68 |
69 | public void disable() {
70 | Log.d(TAG, "disable()");
71 | if(mAdapter != null) {
72 | mAdapter.disableReaderMode(mActivity);
73 | mAdapter = null;
74 | }
75 | mHandler.removeCallbacks(mPoller);
76 | }
77 |
78 | private class NfcPollerRunnable implements Runnable {
79 | @Override
80 | public void run() {
81 | Log.v(TAG, "poll()");
82 | for(GenericCardTerminal terminal: mTerminals.getTerminals()) {
83 | try {
84 | if(!terminal.isCardPresent()) {
85 | mTerminals.removeTerminal(terminal);
86 | }
87 | } catch (CardException e) {
88 | Log.e(TAG, "Error polling for card", e);
89 | }
90 | }
91 | mHandler.postDelayed(this, POLL_INTERVAL);
92 | }
93 | }
94 |
95 | private class NfcReaderCallback implements NfcAdapter.ReaderCallback {
96 | @Override
97 | public void onTagDiscovered(Tag tag) {
98 | Log.d(TAG, "onTagDiscovered()");
99 | IsoDep tagIso = IsoDep.get(tag);
100 | if(tagIso != null) {
101 | mTerminals.newTag(tag, tagIso);
102 | }
103 | }
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/android-nfc/src/main/java/org/openjavacard/smartcardio/android/nfc/NfcCard.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.nfc;
2 |
3 | import android.nfc.Tag;
4 | import android.nfc.tech.IsoDep;
5 | import org.openjavacard.smartcardio.generic.GenericCard;
6 | import org.openjavacard.smartcardio.generic.GenericCardChannel;
7 |
8 | import javax.smartcardio.CardChannel;
9 | import javax.smartcardio.CardException;
10 |
11 | public class NfcCard extends GenericCard {
12 |
13 | private NfcCardTerminal mTerminal;
14 | private Tag mTag;
15 | private IsoDep mIsoTag;
16 | private GenericCardChannel mBasicChannel;
17 |
18 | NfcCard(NfcCardTerminal terminal, Tag tag, IsoDep isoTag) {
19 | super(terminal);
20 | mTerminal = terminal;
21 | mTag = tag;
22 | mIsoTag = isoTag;
23 | mBasicChannel = new NfcCardChannel(this, 0);
24 | }
25 |
26 | @Override
27 | public CardChannel getBasicChannel() {
28 | return mBasicChannel;
29 | }
30 |
31 | @Override
32 | public void beginExclusive() throws CardException {
33 | mTerminal.checkConnected();
34 | super.beginExclusive();
35 | }
36 |
37 | @Override
38 | public CardChannel openLogicalChannel() throws CardException {
39 | mTerminal.checkConnected();
40 | return null;
41 | }
42 |
43 | @Override
44 | public void disconnect(boolean reset) throws CardException {
45 | if(reset) {
46 | throw new UnsupportedOperationException("Explicit card reset is not supported on NFC");
47 | }
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/android-nfc/src/main/java/org/openjavacard/smartcardio/android/nfc/NfcCardChannel.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.nfc;
2 |
3 | import org.openjavacard.smartcardio.generic.GenericCardChannel;
4 |
5 | import javax.smartcardio.CardException;
6 | import java.nio.ByteBuffer;
7 |
8 | public class NfcCardChannel extends GenericCardChannel {
9 |
10 | public NfcCardChannel(NfcCard card, int channel) {
11 | super(card, channel);
12 | }
13 |
14 | @Override
15 | public int transmit(ByteBuffer command, ByteBuffer response) throws CardException {
16 | return 0;
17 | }
18 |
19 | @Override
20 | public void close() throws CardException {
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/android-nfc/src/main/java/org/openjavacard/smartcardio/android/nfc/NfcCardTerminal.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.nfc;
2 |
3 | import android.nfc.Tag;
4 | import android.nfc.tech.IsoDep;
5 | import android.util.Log;
6 | import org.openjavacard.smartcardio.generic.GenericCardTerminal;
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 |
10 | import javax.smartcardio.Card;
11 | import javax.smartcardio.CardException;
12 | import javax.smartcardio.CardNotPresentException;
13 | import java.io.IOException;
14 | import java.util.concurrent.atomic.AtomicInteger;
15 |
16 | public class NfcCardTerminal extends GenericCardTerminal {
17 |
18 | private static final String TAG = NfcCardTerminal.class.getName();
19 |
20 | private static final AtomicInteger ID_COUNTER = new AtomicInteger();
21 |
22 | private NfcCardTerminals mTerminals;
23 | private Tag mTag;
24 | private IsoDep mIsoTag;
25 | private NfcCard mCard;
26 |
27 | NfcCardTerminal(NfcCardTerminals terminals, Tag tag, IsoDep isoTag) {
28 | super(terminals, "NFC tag #" + ID_COUNTER.getAndIncrement());
29 | mTerminals = terminals;
30 | mTag = tag;
31 | mIsoTag = isoTag;
32 | mCard = new NfcCard(this, tag, isoTag);
33 | }
34 |
35 | void checkConnected() throws CardException {
36 | if(!isCardPresent()) {
37 | throw new CardNotPresentException("NFC tag disconnected");
38 | }
39 | }
40 |
41 | @Override
42 | public boolean isCardPresent() {
43 | return mIsoTag.isConnected();
44 | }
45 |
46 | @Override
47 | public Card connect(String protocol) throws CardException {
48 | if(protocol == null) {
49 | throw new NullPointerException();
50 | }
51 | Log.d(TAG, "connect(" + protocol + ")");
52 | if(protocol.equals("*") || protocol.equals("T=CL")) {
53 | try {
54 | mIsoTag.connect();
55 | } catch (IOException e) {
56 | throw new CardException("Error connecting to tag", e);
57 | }
58 | if(!mIsoTag.isConnected()) {
59 | throw new CardNotPresentException("NFC tag no longer present");
60 | }
61 | mCard.connected("T=CL", null);
62 | return mCard;
63 | } else {
64 | throw new IllegalArgumentException("Protocol " + protocol + " not supported");
65 | }
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/android-nfc/src/main/java/org/openjavacard/smartcardio/android/nfc/NfcCardTerminals.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.nfc;
2 |
3 | import android.nfc.Tag;
4 | import android.nfc.tech.IsoDep;
5 | import android.util.Log;
6 | import org.openjavacard.smartcardio.generic.GenericCardTerminals;
7 |
8 | import java.io.IOException;
9 |
10 | public class NfcCardTerminals extends GenericCardTerminals {
11 |
12 | private static final String TAG = NfcCardTerminals.class.getName();
13 |
14 | void newTag(Tag tag, IsoDep isoTag) {
15 | Log.d(TAG, "newTag()");
16 | try {
17 | isoTag.connect();
18 | addTerminal(new NfcCardTerminal(this, tag, isoTag));
19 | } catch (IOException e) {
20 | Log.e(TAG, "Could not connect to tag", e);
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/android-nfc/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/android-omapi/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion "28.0.3"
6 | defaultConfig {
7 | minSdkVersion 20
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | sourceSets {
20 | main {
21 | // disable NDK build
22 | jni.srcDirs = []
23 | }
24 | }
25 | }
26 |
27 | dependencies {
28 | api project(':framework')
29 | api project(':generic')
30 | // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
31 | implementation 'org.slf4j:slf4j-api:1.7.25'
32 | // https://mvnrepository.com/artifact/junit/junit
33 | testImplementation 'junit:junit:4.12'
34 | // https://mvnrepository.com/artifact/com.android.support.test.espresso/espresso-core
35 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
36 | exclude group: 'com.android.support', module: 'support-annotations'
37 | })
38 | }
39 |
--------------------------------------------------------------------------------
/android-omapi/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /opt/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/android-omapi/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/android-omapi/src/main/java/org/openjavacard/smartcardio/android/omapi/AndroidOmapiSCIO.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.omapi;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.se.omapi.Reader;
6 | import android.se.omapi.SEService;
7 | import android.util.Log;
8 |
9 | import java.util.concurrent.Executor;
10 |
11 | @TargetApi(28)
12 | public class AndroidOmapiSCIO {
13 |
14 | private static final String TAG = AndroidOmapiSCIO.class.getName();
15 |
16 | /** Context we are running in */
17 | private Context mContext;
18 | /** Reference to SE service */
19 | private SEService mService;
20 | /** True if connected to SE service */
21 | private boolean mConnected;
22 | /** Terminals object */
23 | private OmapiCardTerminals mTerminals;
24 | /** Executor for SE service callbacks */
25 | private Executor mConnectExecutor;
26 | /** Listener for connect events */
27 | private ConnectListener mConnectListener;
28 |
29 | /**
30 | * Main constructor
31 | * @param context to use
32 | */
33 | public AndroidOmapiSCIO(Context context) {
34 | mContext = context;
35 | mTerminals = new OmapiCardTerminals();
36 | mConnectExecutor = new SynchronousExecutor();
37 | mConnectListener = new ConnectListener();
38 | }
39 |
40 | /**
41 | * Get the Terminals object
42 | * @return the Terminals object
43 | */
44 | public OmapiCardTerminals getTerminals() {
45 | return mTerminals;
46 | }
47 |
48 | /**
49 | * @return
50 | */
51 | public boolean isEnabled() {
52 | return mService != null;
53 | }
54 |
55 | /**
56 | * Enable the interface
57 | */
58 | public void enable() {
59 | Log.d(TAG, "enable()");
60 | // connect to service
61 | mService = new SEService(mContext, mConnectExecutor, mConnectListener);
62 | }
63 |
64 | /**
65 | * Disable the interface
66 | */
67 | public void disable() {
68 | Log.d(TAG, "disable()");
69 | // disconnect from service
70 | if(mService != null) {
71 | if(mService.isConnected()) {
72 | mService.shutdown();
73 | }
74 | mService = null;
75 | }
76 | // reset connect state
77 | mConnected = false;
78 | }
79 |
80 | /**
81 | * Service connected callback
82 | */
83 | private void onServiceConnected() {
84 | Log.d(TAG, "onServiceConnected()");
85 | // we are now connected
86 | mConnected = true;
87 | // add available readers
88 | Reader[] readers = mService.getReaders();
89 | for (Reader reader : readers) {
90 | mTerminals.addTerminal(new OmapiCardTerminal(mTerminals, reader));
91 | }
92 | }
93 |
94 | /**
95 | * Dummy synchronous executor
96 | */
97 | private class SynchronousExecutor implements Executor {
98 | @Override
99 | public void execute(Runnable command) {
100 | command.run();
101 | }
102 | }
103 |
104 | /**
105 | * Connect event listener
106 | */
107 | private class ConnectListener implements SEService.OnConnectedListener {
108 | @Override
109 | public void onConnected() {
110 | onServiceConnected();
111 | }
112 | }
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/android-omapi/src/main/java/org/openjavacard/smartcardio/android/omapi/OmapiCard.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.omapi;
2 |
3 | import android.annotation.TargetApi;
4 | import android.se.omapi.Channel;
5 | import android.se.omapi.Session;
6 | import org.openjavacard.smartcardio.generic.GenericCard;
7 |
8 | import javax.smartcardio.CardException;
9 | import java.io.IOException;
10 |
11 | public class OmapiCard extends GenericCard {
12 |
13 | private Session mSession;
14 |
15 | OmapiCard(OmapiCardTerminal terminal, Session session) {
16 | super(terminal);
17 | mSession = session;
18 | }
19 |
20 | @Override
21 | @TargetApi(28)
22 | public OmapiCardChannel getBasicChannel() {
23 | try {
24 | Channel channel = mSession.openBasicChannel(null);
25 | return new OmapiCardChannel(this, true, channel);
26 | } catch (IOException e) {
27 | throw new IllegalStateException("Could not get basic channel", e);
28 | }
29 | }
30 |
31 | @Override
32 | @TargetApi(28)
33 | public OmapiCardChannel openLogicalChannel() throws CardException {
34 | return openLogicalChannel(null);
35 | }
36 |
37 | @TargetApi(28)
38 | public OmapiCardChannel openBasicChannel(byte[] aid) throws CardException {
39 | try {
40 | Channel channel = mSession.openBasicChannel(aid);
41 | return new OmapiCardChannel(this, true, channel);
42 | } catch (IOException e) {
43 | throw new CardException("Could not open basic channel", e);
44 | }
45 | }
46 |
47 | @TargetApi(28)
48 | public OmapiCardChannel openLogicalChannel(byte[] aid) throws CardException {
49 | try {
50 | Channel channel = mSession.openLogicalChannel(aid);
51 | return new OmapiCardChannel(this, false, channel);
52 | } catch (IOException e) {
53 | throw new CardException("Could not open logical channel", e);
54 | }
55 | }
56 |
57 | @Override
58 | @TargetApi(28)
59 | public void disconnect(boolean reset) {
60 | if(reset) {
61 | throw new UnsupportedOperationException("Card reset not supported by OMAPI");
62 | }
63 | if(!mSession.isClosed()) {
64 | mSession.closeChannels();
65 | mSession.close();
66 | }
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/android-omapi/src/main/java/org/openjavacard/smartcardio/android/omapi/OmapiCardChannel.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.omapi;
2 |
3 | import android.annotation.TargetApi;
4 | import android.se.omapi.Channel;
5 | import org.openjavacard.smartcardio.generic.GenericCardChannel;
6 |
7 | import javax.smartcardio.CardException;
8 | import java.io.IOException;
9 | import java.nio.ByteBuffer;
10 |
11 | public class OmapiCardChannel extends GenericCardChannel {
12 |
13 | /** True if this is the basic channel */
14 | private boolean mIsBasic;
15 | /** OMAPI channel object */
16 | private Channel mChannel;
17 |
18 | OmapiCardChannel(OmapiCard card, boolean isBasic, Channel channel) {
19 | // always pass 0 for the channel number
20 | // so the generic code does not change CLA
21 | super(card, 0);
22 | mIsBasic = isBasic;
23 | mChannel = channel;
24 | }
25 |
26 | /**
27 | * Return the channel number
28 | *
29 | * On OMAPI we always return 0 or 1 because
30 | * we do not know the actual channel number.
31 | *
32 | * @return fake channel number
33 | */
34 | @Override
35 | public int getChannelNumber() {
36 | if(mIsBasic) {
37 | return 0;
38 | } else {
39 | return 1;
40 | }
41 | }
42 |
43 | @Override
44 | @TargetApi(28)
45 | public int transmit(ByteBuffer command, ByteBuffer response) throws CardException {
46 | byte[] commandBytes = new byte[command.position()];
47 | command.get(commandBytes);
48 | byte[] responseBytes;
49 | try {
50 | responseBytes = mChannel.transmit(commandBytes);
51 | } catch (IOException e) {
52 | throw new CardException("Could not transmit command", e);
53 | }
54 | response.put(responseBytes);
55 | return responseBytes.length;
56 | }
57 |
58 | @Override
59 | @TargetApi(28)
60 | public void close() throws CardException {
61 | if(mChannel.isOpen()) {
62 | mChannel.close();
63 | }
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/android-omapi/src/main/java/org/openjavacard/smartcardio/android/omapi/OmapiCardTerminal.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.omapi;
2 |
3 | import android.annotation.TargetApi;
4 | import android.se.omapi.Reader;
5 | import android.se.omapi.Session;
6 | import android.util.Log;
7 | import org.openjavacard.smartcardio.generic.GenericCardTerminal;
8 | import org.openjavacard.smartcardio.generic.GenericCardTerminals;
9 |
10 | import javax.smartcardio.CardException;
11 | import java.io.IOException;
12 |
13 | public class OmapiCardTerminal extends GenericCardTerminal {
14 |
15 | private static final String TAG = OmapiCardTerminal.class.getName();
16 |
17 | /** OMAPI reader object */
18 | private Reader mReader;
19 |
20 | @TargetApi(28)
21 | OmapiCardTerminal(GenericCardTerminals terminals, Reader reader) {
22 | super(terminals, reader.getName());
23 | mReader = reader;
24 | }
25 |
26 | @Override
27 | @TargetApi(28)
28 | public boolean isCardPresent() {
29 | return mReader.isSecureElementPresent();
30 | }
31 |
32 | @Override
33 | @TargetApi(28)
34 | public OmapiCard connect(String protocol) throws CardException {
35 | OmapiCard card;
36 | if(protocol == null) {
37 | throw new NullPointerException();
38 | }
39 | Log.d(TAG, "connect(" + protocol + ")");
40 | if(protocol.equals("*")) {
41 | try {
42 | // open session with reader
43 | Session session = mReader.openSession();
44 | // create and return card object
45 | card = new OmapiCard(this, session);
46 | // prepare the object
47 | card.connected("T?", null);
48 | } catch (IOException e) {
49 | throw new CardException("Could not connect to SE " + mReader.getName(), e);
50 | }
51 | } else {
52 | throw new IllegalArgumentException("Protocol " + protocol + " not supported");
53 | }
54 | return card;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/android-omapi/src/main/java/org/openjavacard/smartcardio/android/omapi/OmapiCardTerminals.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.android.omapi;
2 |
3 | import org.openjavacard.smartcardio.generic.GenericCardTerminals;
4 |
5 | public class OmapiCardTerminals extends GenericCardTerminals {
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/android-omapi/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | jcenter()
4 | google()
5 | }
6 | dependencies {
7 | classpath 'com.android.tools.build:gradle:3.5.0'
8 | }
9 | }
10 |
11 | allprojects {
12 | repositories {
13 | jcenter()
14 | maven {
15 | url "https://maven.google.com/"
16 | }
17 | }
18 | }
19 |
20 | task clean(type: Delete) {
21 | delete rootProject.buildDir
22 | }
23 |
--------------------------------------------------------------------------------
/framework/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | sourceCompatibility = "1.7"
4 | targetCompatibility = "1.7"
5 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/ATR.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | import java.util.Arrays;
4 |
5 | /**
6 | * Representation of a smart card answer-to-reset
7 | */
8 | public class ATR {
9 |
10 | private final byte[] mBytes;
11 |
12 | private int mHistOffset;
13 | private int mHistLength;
14 |
15 | /**
16 | * Main constructor
17 | * @param bytes with raw ATR
18 | */
19 | public ATR(byte[] bytes) {
20 | mBytes = bytes.clone();
21 | }
22 |
23 | /** @return a copy of the raw ATR */
24 | public byte[] getBytes() {
25 | return mBytes.clone();
26 | }
27 |
28 | /** @return the historical bytes of the ATR */
29 | public byte[] getHistoricalBytes() {
30 | byte[] b = new byte[mHistLength];
31 | System.arraycopy(mBytes, mHistOffset, b, 0, mHistLength);
32 | return b;
33 | }
34 |
35 | public String toString() {
36 | return "ATR: " + mBytes.length + " bytes";
37 | }
38 |
39 | public int hashCode() {
40 | return Arrays.hashCode(mBytes);
41 | }
42 |
43 | public boolean equals(Object obj) {
44 | if (this == obj) {
45 | return true;
46 | }
47 | if (obj instanceof ATR == false) {
48 | return false;
49 | }
50 | ATR other = (ATR)obj;
51 | return Arrays.equals(this.mBytes, other.getBytes());
52 | }
53 |
54 | private void parse() {
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/Card.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | /**
4 | * Base class for interfacing to smart cards
5 | */
6 | public abstract class Card {
7 |
8 | /**
9 | * Main constructor
10 | */
11 | protected Card() {
12 | }
13 |
14 | /** @return the ATR sent by the card */
15 | public abstract ATR getATR();
16 |
17 | /** @return the protocol used to communicate with the card */
18 | public abstract String getProtocol();
19 |
20 | /** @return the basic channel for the card */
21 | public abstract CardChannel getBasicChannel();
22 |
23 | /**
24 | * Open a logical channel
25 | * @return the channel
26 | * @throws CardException on error
27 | */
28 | public abstract CardChannel openLogicalChannel() throws CardException;
29 |
30 | /**
31 | * Lock the card for thread-exclusive access
32 | * @throws CardException on error
33 | */
34 | public abstract void beginExclusive() throws CardException;
35 |
36 | /**
37 | * End thread-exclusive access
38 | * @throws CardException on error
39 | */
40 | public abstract void endExclusive() throws CardException;
41 |
42 | /**
43 | * Transmit a control command
44 | * @param controlCode
45 | * @param command
46 | * @return
47 | * @throws CardException
48 | */
49 | public abstract byte[] transmitControlCommand(int controlCode,
50 | byte[] command) throws CardException;
51 |
52 | /**
53 | * Disconnect from the card
54 | * @param reset true to reset the card
55 | * @throws CardException on error
56 | */
57 | public abstract void disconnect(boolean reset) throws CardException;
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/CardChannel.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | import java.nio.ByteBuffer;
4 |
5 | /**
6 | * Base class for card channels
7 | */
8 | public abstract class CardChannel {
9 |
10 | /**
11 | * Main constructor
12 | */
13 | protected CardChannel() {
14 | }
15 |
16 | /** @return the card this channel is for */
17 | public abstract Card getCard();
18 |
19 | /** @return the channel number of this channel */
20 | public abstract int getChannelNumber();
21 |
22 | /**
23 | * Transmit an APDU through the channel
24 | * @param command to transmit
25 | * @return response to command
26 | * @throws CardException on error
27 | */
28 | public abstract ResponseAPDU transmit(CommandAPDU command) throws CardException;
29 |
30 | /**
31 | * Transmit a raw APDU through the channel
32 | * @param command buffer with command
33 | * @param response buffer for response
34 | * @return length of response
35 | * @throws CardException on error
36 | */
37 | public abstract int transmit(ByteBuffer command, ByteBuffer response)
38 | throws CardException;
39 |
40 | /**
41 | * Close the channel
42 | * @throws CardException on error
43 | */
44 | public abstract void close() throws CardException;
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/CardException.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | /**
4 | * Base class for smart card exceptions
5 | */
6 | public class CardException extends Exception {
7 |
8 | /**
9 | * Exception constructor
10 | * @param message for exception
11 | */
12 | public CardException(String message) {
13 | super(message);
14 | }
15 |
16 | /**
17 | * Exception constructor
18 | * @param message for exception
19 | * @param cause for exception
20 | */
21 | public CardException(String message, Throwable cause) {
22 | super(message, cause);
23 | }
24 |
25 | /**
26 | * Exception constructor
27 | * @param cause for exception
28 | */
29 | public CardException(Throwable cause) {
30 | super(cause);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/CardNotPresentException.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | /**
4 | * Exception for when the card is not present
5 | */
6 | public class CardNotPresentException extends CardException {
7 |
8 | /**
9 | * Exception constructor
10 | * @param message for exception
11 | */
12 | public CardNotPresentException(String message) {
13 | super(message);
14 | }
15 |
16 | /**
17 | * Exception constructor
18 | * @param message for exception
19 | * @param cause for exception
20 | */
21 | public CardNotPresentException(String message, Throwable cause) {
22 | super(message, cause);
23 | }
24 |
25 | /**
26 | * Exception constructor
27 | * @param cause for exception
28 | */
29 | public CardNotPresentException(Throwable cause) {
30 | super(cause);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/CardTerminal.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | /**
4 | * Base class for interfacing to card terminals
5 | */
6 | public abstract class CardTerminal {
7 |
8 | /**
9 | * Main constructor
10 | */
11 | protected CardTerminal() {
12 | }
13 |
14 | /** @return the name of this terminal */
15 | public abstract String getName();
16 |
17 | /** @return true of the terminal has a card present */
18 | public abstract boolean isCardPresent() throws CardException;
19 |
20 | /**
21 | * Connect to the card in this terminal
22 | * @param protocol to use or "*"
23 | * @return an interface to the card
24 | * @throws CardException on error
25 | */
26 | public abstract Card connect(String protocol) throws CardException;
27 |
28 | /**
29 | * Wait for a card to be present
30 | * @param timeout after which to return
31 | * @return true if there is a card now
32 | * @throws CardException
33 | */
34 | public abstract boolean waitForCardPresent(long timeout) throws CardException;
35 |
36 | /**
37 | * Wait for a card to be absent
38 | * @param timeout after which to return
39 | * @return true if the card is gone now
40 | * @throws CardException
41 | */
42 | public abstract boolean waitForCardAbsent(long timeout) throws CardException;
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/CardTerminals.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Base class for interfacing to a set of terminals
7 | */
8 | public abstract class CardTerminals {
9 |
10 | /** Reader states */
11 | public enum State {
12 | ALL,
13 | CARD_PRESENT,
14 | CARD_ABSENT,
15 | CARD_INSERTION,
16 | CARD_REMOVAL,
17 | }
18 |
19 | /**
20 | * Main constructor
21 | */
22 | protected CardTerminals() {
23 | }
24 |
25 | /**
26 | * Get a specific terminal by name
27 | * @param name of the terminal
28 | * @return the terminal or null
29 | */
30 | public CardTerminal getTerminal(String name) {
31 | try {
32 | for (CardTerminal terminal : list()) {
33 | if (terminal.getName().equals(name)) {
34 | return terminal;
35 | }
36 | }
37 | return null;
38 | } catch (CardException e) {
39 | return null;
40 | }
41 | }
42 |
43 | /**
44 | * List all terminals in this set
45 | * @return list of all terminals
46 | * @throws CardException on error
47 | */
48 | public List list() throws CardException {
49 | return list(State.ALL);
50 | }
51 |
52 | /**
53 | * List all terminals in the given state
54 | * @param state to query
55 | * @return list of terminals in given state
56 | * @throws CardException on error
57 | */
58 | public abstract List list(State state) throws CardException;
59 |
60 | /**
61 | * Wait for any addition, removal or state change
62 | *
63 | * Convenience variant with infinite timeout.
64 | *
65 | * @throws CardException on error
66 | */
67 | public void waitForChange() throws CardException {
68 | waitForChange(0);
69 | }
70 |
71 | /**
72 | * Wait for any addition, removal or state change
73 | * @throws CardException on error
74 | */
75 | public abstract boolean waitForChange(long timeout) throws CardException;
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/CommandAPDU.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | import java.nio.ByteBuffer;
4 | import java.util.Arrays;
5 |
6 | /**
7 | * Representation of a smart card command APDU
8 | */
9 | public class CommandAPDU {
10 |
11 | private byte[] mBytes;
12 | private int mNc;
13 | private int mNe;
14 |
15 | /**
16 | * Parsing constructor
17 | * @param apdu byte array containing APDU
18 | * @param apduOffset
19 | * @param apduLength
20 | */
21 | public CommandAPDU(byte[] apdu, int apduOffset, int apduLength) {
22 | }
23 |
24 | /**
25 | * Convenience parsing constructor
26 | * @param apdu byte array containing APDU
27 | */
28 | public CommandAPDU(byte[] apdu) {
29 | this(apdu, 0, apdu.length);
30 | }
31 |
32 | /**
33 | * Convenience parsing constructor
34 | * @param apdu buffer containing APDU
35 | */
36 | public CommandAPDU(ByteBuffer apdu) {
37 | this(apdu.array());
38 | }
39 |
40 | /**
41 | * Raw constructor
42 | * @param cla class
43 | * @param ins instruction
44 | * @param p1 parameter byte 1
45 | * @param p2 parameter byte 2
46 | * @param data byte array containing command data
47 | * @param dataOffset offset of command data in array
48 | * @param dataLength length of command data
49 | * @param ne expected response length
50 | */
51 | public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data,
52 | int dataOffset, int dataLength, int ne) {
53 | }
54 |
55 | /**
56 | * Convenience constructor
57 | * @param cla class
58 | * @param ins instruction
59 | * @param p1 parameter byte 1
60 | * @param p2 parameter byte 2
61 | */
62 | public CommandAPDU(int cla, int ins, int p1, int p2) {
63 | this(cla, ins, p1, p2, null, 0, 0, 0);
64 | }
65 |
66 | /**
67 | * Convenience constructor
68 | * @param cla class
69 | * @param ins instruction
70 | * @param p1 parameter byte 1
71 | * @param p2 parameter byte 2
72 | * @param ne expected response length
73 | */
74 | public CommandAPDU(int cla, int ins, int p1, int p2, int ne) {
75 | this(cla, ins, p1, p2, null, 0, 0, ne);
76 | }
77 |
78 | /**
79 | * Convenience constructor
80 | * @param cla class
81 | * @param ins instruction
82 | * @param p1 parameter byte 1
83 | * @param p2 parameter byte 2
84 | * @param data byte array containing command data
85 | */
86 | public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data) {
87 | this(cla, ins, p1, p2, data, 0, arrayLength(data), 0);
88 | }
89 |
90 | /**
91 | * Convenience constructor
92 | * @param cla class
93 | * @param ins instruction
94 | * @param p1 parameter byte 1
95 | * @param p2 parameter byte 2
96 | * @param data byte array containing command data
97 | * @param dataOffset offset of command data in array
98 | * @param dataLength length of command data
99 | */
100 | public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data,
101 | int dataOffset, int dataLength) {
102 | this(cla, ins, p1, p2, data, dataOffset, dataLength, 0);
103 | }
104 |
105 | /**
106 | * Convenience constructor
107 | * @param cla class
108 | * @param ins instruction
109 | * @param p1 parameter byte 1
110 | * @param p2 parameter byte 2
111 | * @param data byte array containing command data
112 | * @param ne expected response length
113 | */
114 | public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne) {
115 | this(cla, ins, p1, p2, data, 0, arrayLength(data), ne);
116 | }
117 |
118 | private static int arrayLength(byte[] b) {
119 | return (b != null) ? b.length : 0;
120 | }
121 |
122 | /** @return APDU as raw bytes */
123 | byte[] getBytes() {
124 | return mBytes;
125 | }
126 |
127 | /** @return class of the APDU */
128 | public int getCLA() {
129 | return mBytes[0] & 0xff;
130 | }
131 |
132 | /** @return instruction of the APDU */
133 | public int getINS() {
134 | return mBytes[0] & 0xff;
135 | }
136 |
137 | /** @return parameter byte 1 of the APDU */
138 | public int getP1() {
139 | return mBytes[0] & 0xff;
140 | }
141 |
142 | /** @return parameter byte 2 of the APDU */
143 | public int getP2() {
144 | return mBytes[0] & 0xff;
145 | }
146 |
147 | /** @return command data length of the APDU */
148 | public int getNc() {
149 | return mNc;
150 | }
151 |
152 | /** @return expected response length of the APDU */
153 | public int getNe() {
154 | return mNe;
155 | }
156 |
157 | /** @return command data of the APDU */
158 | public byte[] getData() {
159 | return null;
160 | }
161 |
162 | public String toString() {
163 | return "CommmandAPDU: " + mBytes.length + " bytes, nc=" + mNc + ", ne=" + mNe;
164 | }
165 |
166 | public int hashCode() {
167 | return Arrays.hashCode(mBytes);
168 | }
169 |
170 | public boolean equals(Object obj) {
171 | if (this == obj) {
172 | return true;
173 | }
174 | if (!(obj instanceof CommandAPDU)) {
175 | return false;
176 | }
177 | CommandAPDU other = (CommandAPDU)obj;
178 | return Arrays.equals(mBytes, other.getBytes());
179 | }
180 |
181 | }
182 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/ResponseAPDU.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | public class ResponseAPDU {
4 | }
5 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/TerminalFactory.java:
--------------------------------------------------------------------------------
1 | package javax.smartcardio;
2 |
3 | public class TerminalFactory {
4 | }
5 |
--------------------------------------------------------------------------------
/framework/src/main/java/javax/smartcardio/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Implementation of javax.smartcardio
3 | */
4 | package javax.smartcardio;
5 |
--------------------------------------------------------------------------------
/generic/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | sourceCompatibility = "1.7"
4 | targetCompatibility = "1.7"
5 |
--------------------------------------------------------------------------------
/generic/src/main/java/org/openjavacard/smartcardio/generic/GenericCard.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.generic;
2 |
3 | import javax.smartcardio.ATR;
4 | import javax.smartcardio.Card;
5 | import javax.smartcardio.CardException;
6 | import java.util.concurrent.locks.Condition;
7 | import java.util.concurrent.locks.Lock;
8 | import java.util.concurrent.locks.ReentrantLock;
9 |
10 | public abstract class GenericCard extends Card {
11 |
12 | private final Lock mLock;
13 | private final Condition mCondition;
14 |
15 | private final GenericCardTerminal mTerminal;
16 |
17 | private Thread mOwner;
18 |
19 | private String mProtocol;
20 | private ATR mATR;
21 |
22 | protected GenericCard(GenericCardTerminal terminal) {
23 | mLock = new ReentrantLock();
24 | mCondition = mLock.newCondition();
25 | mTerminal = terminal;
26 | }
27 |
28 | public void connected(String protocol, ATR atr) {
29 | mProtocol = protocol;
30 | mATR = atr;
31 | }
32 |
33 | @Override
34 | public String getProtocol() {
35 | return mProtocol;
36 | }
37 |
38 | @Override
39 | public ATR getATR() {
40 | return mATR;
41 | }
42 |
43 | @Override
44 | public void beginExclusive() throws CardException {
45 | mLock.lock();
46 | try {
47 | if(mOwner != null) {
48 | throw new CardException("Card already owned by thread " + mOwner);
49 | }
50 | mOwner = Thread.currentThread();
51 | } finally {
52 | mLock.unlock();
53 | }
54 | }
55 |
56 | @Override
57 | public void endExclusive() throws CardException {
58 | mLock.lock();
59 | try {
60 | if(mOwner == null) {
61 | throw new CardException("Card not owned");
62 | }
63 | if(mOwner != Thread.currentThread()) {
64 | throw new CardException("Card is owned by other thread " + mOwner);
65 | }
66 | mOwner = null;
67 | } finally {
68 | mLock.unlock();
69 | }
70 | }
71 |
72 | @Override
73 | public byte[] transmitControlCommand(int controlCode, byte[] command) throws CardException {
74 | throw new UnsupportedOperationException("Control commands not supported");
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/generic/src/main/java/org/openjavacard/smartcardio/generic/GenericCardChannel.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.generic;
2 |
3 | import javax.smartcardio.*;
4 | import java.nio.ByteBuffer;
5 |
6 | public abstract class GenericCardChannel extends CardChannel {
7 |
8 | private final Card mCard;
9 | private final int mChannelNumber;
10 |
11 | public GenericCardChannel(Card card, int channel) {
12 | mCard = card;
13 | mChannelNumber = channel;
14 | }
15 |
16 | @Override
17 | public Card getCard() {
18 | return mCard;
19 | }
20 |
21 | @Override
22 | public int getChannelNumber() {
23 | return mChannelNumber;
24 | }
25 |
26 | @Override
27 | public ResponseAPDU transmit(CommandAPDU command) throws CardException {
28 | // serialize the command
29 | byte[] commandBytes = command.getBytes();
30 | ByteBuffer commandBuffer = ByteBuffer.wrap(commandBytes);
31 | ByteBuffer responseBuffer = ByteBuffer.allocate(command.getNe());
32 | commandBuffer.put(commandBytes);
33 | // perform the operation
34 | int responseLength = transmit(commandBuffer, responseBuffer);
35 | // unpack response
36 | byte[] responseBytes = new byte[responseLength];
37 | responseBuffer.get(responseBytes, 0, responseLength);
38 | return new ResponseAPDU(responseBytes);
39 | }
40 |
41 | @Override
42 | public abstract int transmit(ByteBuffer command, ByteBuffer response) throws CardException;
43 |
44 | @Override
45 | public abstract void close() throws CardException;
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/generic/src/main/java/org/openjavacard/smartcardio/generic/GenericCardTerminal.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.generic;
2 |
3 | import javax.smartcardio.CardException;
4 | import javax.smartcardio.CardTerminal;
5 | import java.util.concurrent.TimeUnit;
6 | import java.util.concurrent.locks.Condition;
7 | import java.util.concurrent.locks.Lock;
8 | import java.util.concurrent.locks.ReentrantLock;
9 |
10 | public abstract class GenericCardTerminal extends CardTerminal {
11 |
12 | private final Lock mLock;
13 | private final Condition mCondition;
14 |
15 | private final GenericCardTerminals mTerminals;
16 |
17 | private final String mName;
18 |
19 | protected GenericCardTerminal(GenericCardTerminals terminals, String name) {
20 | mLock = new ReentrantLock();
21 | mCondition = mLock.newCondition();
22 | mTerminals = terminals;
23 | mName = name;
24 | }
25 |
26 | @Override
27 | public String getName() {
28 | return mName;
29 | }
30 |
31 | @Override
32 | public boolean waitForCardPresent(long timeout) throws CardException {
33 | if(timeout < 0) {
34 | throw new IllegalArgumentException();
35 | }
36 | boolean event, present;
37 | mLock.lock();
38 | try {
39 | try {
40 | event = mCondition.await(timeout, TimeUnit.MILLISECONDS);
41 | } catch (InterruptedException e) {
42 | throw new CardException("Wait interrupted", e);
43 | }
44 | present = isCardPresent();
45 | } finally {
46 | mLock.unlock();
47 | }
48 | return event && present;
49 | }
50 |
51 | @Override
52 | public boolean waitForCardAbsent(long timeout) throws CardException {
53 | if(timeout < 0) {
54 | throw new IllegalArgumentException();
55 | }
56 | boolean event, present;
57 | mLock.lock();
58 | try {
59 | try {
60 | event = mCondition.await(timeout, TimeUnit.MILLISECONDS);
61 | } catch (InterruptedException e) {
62 | throw new CardException("Wait interrupted", e);
63 | }
64 | present = isCardPresent();
65 | } finally {
66 | mLock.unlock();
67 | }
68 | return event && !present;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/generic/src/main/java/org/openjavacard/smartcardio/generic/GenericCardTerminals.java:
--------------------------------------------------------------------------------
1 | package org.openjavacard.smartcardio.generic;
2 |
3 | import javax.smartcardio.CardException;
4 | import javax.smartcardio.CardTerminal;
5 | import javax.smartcardio.CardTerminals;
6 | import java.util.ArrayList;
7 | import java.util.HashMap;
8 | import java.util.List;
9 | import java.util.concurrent.TimeUnit;
10 | import java.util.concurrent.locks.Condition;
11 | import java.util.concurrent.locks.Lock;
12 | import java.util.concurrent.locks.ReentrantLock;
13 |
14 | public abstract class GenericCardTerminals extends CardTerminals {
15 |
16 | private final Lock mLock;
17 | private final Condition mCondition;
18 |
19 | private final HashMap mTerminals;
20 |
21 | private Listener mListener;
22 |
23 | public interface Listener {
24 | void onTerminalAdded(GenericCardTerminals terminals, GenericCardTerminal terminal);
25 | void onTerminalRemoved(GenericCardTerminals terminals, GenericCardTerminal terminal);
26 | }
27 |
28 | protected GenericCardTerminals() {
29 | super();
30 | mLock = new ReentrantLock();
31 | mCondition = mLock.newCondition();
32 | mTerminals = new HashMap<>();
33 | }
34 |
35 | public List getTerminals() {
36 | return new ArrayList<>(mTerminals.values());
37 | }
38 |
39 | public Listener getListener() {
40 | return mListener;
41 | }
42 |
43 | public void setListener(Listener listener) {
44 | mListener = listener;
45 | }
46 |
47 | public void addTerminal(GenericCardTerminal terminal) {
48 | mLock.lock();
49 | try {
50 | // add the terminal
51 | String name = terminal.getName();
52 | mTerminals.put(name, terminal);
53 | // signal waiters
54 | mCondition.signalAll();
55 | } finally {
56 | mLock.unlock();
57 | }
58 | if(mListener != null) {
59 | mListener.onTerminalAdded(this, terminal);
60 | }
61 | }
62 |
63 | public void removeTerminal(GenericCardTerminal terminal) {
64 | mLock.lock();
65 | try {
66 | // remove the terminal
67 | String name = terminal.getName();
68 | mTerminals.remove(name);
69 | // signal waiters
70 | mCondition.signalAll();
71 | } finally {
72 | mLock.unlock();
73 | }
74 | if(mListener != null) {
75 | mListener.onTerminalRemoved(this, terminal);
76 | }
77 | }
78 |
79 | @Override
80 | public CardTerminal getTerminal(String name) {
81 | return mTerminals.get(name);
82 | }
83 |
84 | @Override
85 | public List list() throws CardException {
86 | ArrayList result;
87 | mLock.lock();
88 | try {
89 | result = new ArrayList(mTerminals.values());
90 | } finally {
91 | mLock.unlock();
92 | }
93 | return result;
94 | }
95 |
96 | @Override
97 | public List list(State state) throws CardException {
98 | ArrayList result = new ArrayList<>();
99 | mLock.lock();
100 | try {
101 | for(GenericCardTerminal terminal: mTerminals.values()) {
102 | switch(state) {
103 | case CARD_PRESENT:
104 | if(terminal.isCardPresent()) {
105 | result.add(terminal);
106 | }
107 | break;
108 | case CARD_ABSENT:
109 | if(!terminal.isCardPresent()) {
110 | result.add(terminal);
111 | }
112 | break;
113 | }
114 | }
115 | } finally {
116 | mLock.unlock();
117 | }
118 | return result;
119 | }
120 |
121 | @Override
122 | public boolean waitForChange(long timeout) throws CardException {
123 | if(timeout < 0) {
124 | throw new IllegalArgumentException();
125 | }
126 | mLock.lock();
127 | try {
128 | return mCondition.await(timeout, TimeUnit.MILLISECONDS);
129 | } catch (InterruptedException e) {
130 | throw new CardException("Wait interrupted", e);
131 | } finally {
132 | mLock.unlock();
133 | }
134 | }
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/generic/src/main/java/org/openjavacard/smartcardio/generic/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Generic base classes for implementing javax.smartcardio
3 | */
4 | package org.openjavacard.smartcardio.generic;
5 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenJavaCard/openjavacard-smartcardio/195950ea27435971728a3f104316ab6b18754ec4/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Feb 12 17:38:50 CET 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':framework'
2 | include ':generic'
3 | include ':android-nfc'
4 | include ':android-omapi'
5 | include ':android-demo'
6 |
--------------------------------------------------------------------------------