Skip to content

SEService.shutdown() shall release SE resources for the SEService #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: android-5.0.0_r3-scapi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/org/simalliance/openmobileapi/service/Session.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ public class Session {
private final Terminal mReader;
private Context mContext;
private boolean mIsClosed;
private int mClientId;

/**
* List of open channels in use by this client.
*/
private final List<Channel> mChannels = new ArrayList<>();

private final Object mLock = new Object();

public Session(Terminal reader, Context context) {
public Session(Terminal reader, int clientId, Context context) {
mReader = reader;
mClientId = clientId;
mIsClosed = false;
mContext = context;
}
Expand All @@ -62,6 +65,10 @@ public Terminal getReader() {
return mReader;
}

public int getClientId() {
return mClientId;
}

public byte[] getAtr() throws RemoteException {
return mReader.getAtr();
}
Expand Down
31 changes: 26 additions & 5 deletions src/org/simalliance/openmobileapi/service/Terminal.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.NoSuchElementException;

import java.util.concurrent.atomic.AtomicInteger;

import android.content.pm.PackageManager;
import android.util.Log;
Expand All @@ -62,6 +62,8 @@ public class Terminal {

private final Object mLock = new Object();

private AtomicInteger mIdGenerator = new AtomicInteger(0);

/* Async task */
InitialiseTask mInitialiseTask;

Expand Down Expand Up @@ -223,7 +225,7 @@ public void onSmartcardServiceShutdown() {
mContext.unbindService(mTerminalConnection);
}

public ISmartcardServiceSession openSession() throws Exception {
public ISmartcardServiceSession openSession(int clientId) throws Exception {
if (!isCardPresent()) {
throw new IOException("Secure Element is not presented.");
}
Expand All @@ -232,7 +234,7 @@ public ISmartcardServiceSession openSession() throws Exception {
if (mAccessControlEnforcer == null || !mAccessControlEnforcer.isInitialized()) {
initializeAccessControl(false);
}
Session session = new Session(this, mContext);
Session session = new Session(this, clientId, mContext);
mSessions.add(session);
return session.getBinder();
}
Expand All @@ -253,6 +255,19 @@ private synchronized void closeSessions() throws Exception {
}
}

private synchronized void closeSessions(int clientId) throws Exception {
int count = mSessions.size();
int index = 0;

for (int i = 0; i < count; i++) {
if (clientId == mSessions.get(index).getClientId()) {
mSessions.get(index).close();
} else {
index++;
}
}
}

public Channel getBasicChannel() {
for (Session session : mSessions) {
Channel basicChannel = session.getBasicChannel();
Expand Down Expand Up @@ -558,6 +573,12 @@ private synchronized void resetAccessControl() {
*/
private class SmartcardServiceReader extends ISmartcardServiceReader.Stub {

private int mId;

public SmartcardServiceReader() {
mId = mIdGenerator.getAndIncrement();
}

@Override
public boolean isSecureElementPresent() throws RemoteException {
return Terminal.this.isCardPresent();
Expand All @@ -566,7 +587,7 @@ public boolean isSecureElementPresent() throws RemoteException {
@Override
public ISmartcardServiceSession openSession(SmartcardError error) throws RemoteException {
try {
return Terminal.this.openSession();
return Terminal.this.openSession(mId);
} catch (Exception e) {
Log.e(SmartcardService.LOG_TAG, "Error during openSession()", e);
error.set(e);
Expand All @@ -577,7 +598,7 @@ public ISmartcardServiceSession openSession(SmartcardError error) throws RemoteE
@Override
public void closeSessions(SmartcardError error) throws RemoteException {
try {
Terminal.this.closeSessions();
Terminal.this.closeSessions(mId);
} catch (Exception e) {
Log.e(SmartcardService.LOG_TAG, "Error during closeSessions()", e);
error.set(e);
Expand Down