summaryrefslogtreecommitdiff
path: root/mobicore/rootpa/Code/Android/app/src/com/gd/mobicore/pa/service/BaseService.java
diff options
context:
space:
mode:
Diffstat (limited to 'mobicore/rootpa/Code/Android/app/src/com/gd/mobicore/pa/service/BaseService.java')
-rw-r--r--mobicore/rootpa/Code/Android/app/src/com/gd/mobicore/pa/service/BaseService.java167
1 files changed, 86 insertions, 81 deletions
diff --git a/mobicore/rootpa/Code/Android/app/src/com/gd/mobicore/pa/service/BaseService.java b/mobicore/rootpa/Code/Android/app/src/com/gd/mobicore/pa/service/BaseService.java
index 536a6b8..8c8bb2a 100644
--- a/mobicore/rootpa/Code/Android/app/src/com/gd/mobicore/pa/service/BaseService.java
+++ b/mobicore/rootpa/Code/Android/app/src/com/gd/mobicore/pa/service/BaseService.java
@@ -1,33 +1,33 @@
/*
-Copyright © Trustonic Limited 2013
-
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without modification,
-are permitted provided that the following conditions are met:
-
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
-
- 3. Neither the name of the Trustonic Limited nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
-OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
-OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * Copyright (c) 2013 TRUSTONIC LIMITED
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
package com.gd.mobicore.pa.service;
@@ -39,11 +39,11 @@ import android.content.Context;
import android.net.NetworkInfo;
import android.net.ConnectivityManager;
+import java.io.File;
import java.net.URI;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.ProxySelector;
-
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@@ -54,21 +54,21 @@ import com.gd.mobicore.pa.ifc.RootPAProvisioningIntents;
import com.gd.mobicore.pa.ifc.CommandResult;
public abstract class BaseService extends Service {
- protected static final String TAG = "RootPA-J";
+ protected static final String TAG = "RootPA-J";
/*
- being statically linked library, the Common C implementation does not handle locks,
+ being statically linked library, the Common C implementation does not handle locks,
they must be handled in the using implementation, in this case here.
*/
private static final int LOCK_FREE=0;
private static final AtomicInteger lock_= new AtomicInteger(LOCK_FREE);
- private static final int LOCK_TIMEOUT_MS=60000;
+ private static final int LOCK_TIMEOUT_MS=300000; //5 mins in ms
private TimerTask timerTask_=null;
private Timer timer_=null;
protected int doProvisioningLockSuid_=0;
- protected byte[] se_ = null;
-
+ protected byte[] se_ = null;
+
private static final int C_CONNECTING_SERVICE_ENABLER=1;
private static final int C_AUTHENTICATING_SOC=2;
private static final int C_CREATING_ROOT_CONTAINER=3;
@@ -81,15 +81,15 @@ public abstract class BaseService extends Service {
protected final CommonPAWrapper commonPaWrapper_=new CommonPAWrapper(this);
private boolean sessionOpened_=false;
-
+
protected CommonPAWrapper commonPAWrapper(){
return commonPaWrapper_;
}
-
+
protected synchronized CommandResult acquireLock(int uid, boolean openSession){
Log.d(TAG,">>BaseService.acquireLock "+uid+" "+lock_.get()+" "+timer_);
if(uid==LOCK_FREE){
- return new CommandResult(CommandResult.ROOTPA_ERROR_ILLEGAL_ARGUMENT);
+ return new CommandResult(CommandResult.ROOTPA_ERROR_ILLEGAL_ARGUMENT);
}
boolean result=lock_.compareAndSet(LOCK_FREE, uid);
if(result==true || lock_.get() == uid){
@@ -111,7 +111,7 @@ public abstract class BaseService extends Service {
Log.i(TAG,"Timer expired, releasing lock");
lock_.set(LOCK_FREE);
if(sessionOpened_==true){
- Log.d(TAG,"BaseService.Timer.run, closingSession");
+ Log.d(TAG,"BaseService.Timer.run, closingSession");
commonPAWrapper().closeSession();
sessionOpened_=false;
}
@@ -123,13 +123,13 @@ public abstract class BaseService extends Service {
}
return new CommandResult(CommandResult.ROOTPA_ERROR_LOCK);
}
-
+
// this is public for the ProvisioningService to call it
protected synchronized CommandResult releaseLock(int uid, boolean closeSession){
Log.d(TAG,"BaseService.releaseLock "+uid+" "+lock_.get()+" "+timer_);
-
+
if(uid==LOCK_FREE){
- return new CommandResult(CommandResult.ROOTPA_ERROR_ILLEGAL_ARGUMENT);
+ return new CommandResult(CommandResult.ROOTPA_ERROR_ILLEGAL_ARGUMENT);
}
if((lock_.get()==LOCK_FREE) || (lock_.compareAndSet(uid, LOCK_FREE)==true)){
@@ -158,7 +158,7 @@ public abstract class BaseService extends Service {
BroadcastReceiver networkChangeReceiver_=null;
protected void setupProxy()
- {
+ {
byte[] proxyAddress=null;
ProxySelector defaultProxySelector = ProxySelector.getDefault();
@@ -169,15 +169,15 @@ public abstract class BaseService extends Service {
if(se_==null){
uri=new URI("https://se.cgbe.trustonic.com"); // the URI here does not matter a lot, as long as one exists. We try to use as real one as is easily possible
}else{
- uri=new URI(new String(se_));
+ uri=new URI(new String(se_));
}
proxyList = defaultProxySelector.select(uri);
if (proxyList.size() > 0)
{
Proxy proxy = proxyList.get(0);
- Log.d(TAG,"BaseService.setupProxy proxy "+proxy); // there should be only one element in the list in the current Android versions, it is for the current connection
+ Log.d(TAG,"BaseService.setupProxy proxy "+proxy); // there should be only one element in the list in the current Android versions, it is for the current connection
if(proxy != Proxy.NO_PROXY){
- Log.d(TAG,"BaseService.setupProxy proxy.type "+proxy.type());
+ Log.d(TAG,"BaseService.setupProxy proxy.type "+proxy.type());
if(proxy.type()==Proxy.Type.HTTP){
// TODO-future there is currently no way for the user to store proxy user name and password in Android,
// so they need to be asked at connection time. There is not any kind of user/password support for proxies in RootPA.
@@ -187,14 +187,14 @@ public abstract class BaseService extends Service {
}
}
}
-
+
}catch(Exception e){
Log.e(TAG,"BaseService.setupProxy FAILURE in getting the proxy: "+e.toString());
}
}
commonPAWrapper().setEnvironmentVariable("http_proxy".getBytes(), proxyAddress);
- commonPAWrapper().setEnvironmentVariable("https_proxy".getBytes(), proxyAddress);
+ commonPAWrapper().setEnvironmentVariable("https_proxy".getBytes(), proxyAddress);
Log.d(TAG,"BaseService.setupProxy just set the proxy to: "+(proxyAddress==null?proxyAddress:new String(proxyAddress)));
// start listening to intents on network changes if not doing it already
@@ -209,7 +209,7 @@ public abstract class BaseService extends Service {
Log.d(TAG,"BaseService: Network "+ni.getTypeName()+" connected");
setupProxy();
}else{
- if(ni!=null){
+ if(ni!=null){
Log.d(TAG, "BaseService: network state "+ni.getState());
}else{
Log.d(TAG, "BaseService: no network info");
@@ -223,14 +223,14 @@ public abstract class BaseService extends Service {
IntentFilter filter=new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(networkChangeReceiver_, filter);
}
- }
-
+ }
+
protected synchronized boolean locked(int uid){
return(lock_.get() != uid && uid != LOCK_FREE);
}
-
+
/**
- This method is called from the C code to send the trustlet binary to the client
+ This method is called from the C code to send the trustlet binary to the client
(trustlet connector/"sp.pa" for develope trustlet) that then can store it where desired.
*/
public void trustletInstallCallback(byte[] trustlet){
@@ -238,29 +238,34 @@ public abstract class BaseService extends Service {
Intent intent=new Intent(RootPAProvisioningIntents.INSTALL_TRUSTLET);
intent.putExtra(RootPAProvisioningIntents.TRUSTLET, trustlet);
sendBroadcast(intent);
- Log.d(TAG,"<<BaseService.trustletInstallCallback");
+ Log.d(TAG,"<<BaseService.trustletInstallCallback");
}
-
+
/**
This method is called from the C code to get the path for files directory
*/
public String getFilesDirPath(){
- return this.getFilesDir().getAbsolutePath();
+ File file = this.getFilesDir();
+ if (null != file){
+ return file.getAbsolutePath();
+ } else {
+ return null;
+ }
}
/**
This method is called from the C code to send the intents while executing doProvisioning
*/
public void provisioningStateCallback(int state, int ret){
- Log.d(TAG,">>provisioningStateCallback "+state+" "+ret);
+ Log.d(TAG,">>provisioningStateCallback "+state+" "+ret);
// since sommunication with SE may take consirderable amount of time, we refresh the Lock timer
- // by calling acquireLock every time a state notification callback is called. This way the lock
+ // by calling acquireLock every time a state notification callback is called. This way the lock
// will not timeout before the communication with SE is complete.
try{
CommandResult res=acquireLock(doProvisioningLockSuid_, false);
if(!res.isOk()){
- Log.e(TAG,"provisioningStateCallback re-acquiring lock failed, res: "+res.result());
+ Log.e(TAG,"provisioningStateCallback re-acquiring lock failed, res: "+res.result());
}
}catch(Exception e){
Log.e(TAG,"provisioningStateCallback re-acquiring lock failed: "+e);
@@ -291,7 +296,7 @@ public abstract class BaseService extends Service {
break;
case C_ERROR:
intent=new Intent(RootPAProvisioningIntents.PROVISIONING_ERROR);
-
+
intent.putExtra(RootPAProvisioningIntents.ERROR, ret);
break;
@@ -299,9 +304,9 @@ public abstract class BaseService extends Service {
try{
CommandResult res=releaseLock(doProvisioningLockSuid_, false);
if(!res.isOk()){
- Log.e(TAG,"provisioningStateCallback releasing lock failed, res: "+res.result());
+ Log.e(TAG,"provisioningStateCallback releasing lock failed, res: "+res.result());
}
- doProvisioningLockSuid_=0;
+ doProvisioningLockSuid_=0;
intent=null; // no intent sent in this case
}catch(Exception e){
Log.e(TAG,"provisioningStateCallback releasing lock failed: "+e);
@@ -310,7 +315,7 @@ public abstract class BaseService extends Service {
unregisterReceiver(networkChangeReceiver_);
networkChangeReceiver_=null;
}
- sendBroadcast(new Intent(RootPAProvisioningIntents.FINISHED_ROOT_PROVISIONING));
+ sendBroadcast(new Intent(RootPAProvisioningIntents.FINISHED_ROOT_PROVISIONING));
break;
default:
Log.e(TAG,"unknown state: "+state);
@@ -327,8 +332,8 @@ public abstract class BaseService extends Service {
public void onConfigurationChanged(android.content.res.Configuration newConfig){
super.onConfigurationChanged(newConfig);
Log.d(TAG,"BaseService.onConfigurationChanged");
- }
-
+ }
+
public void onCreate(){
super.onCreate();
Log.d(TAG,"BaseService.onCreate");
@@ -339,45 +344,45 @@ public abstract class BaseService extends Service {
unregisterReceiver(networkChangeReceiver_);
networkChangeReceiver_=null;
}
- Log.d(TAG,"BaseService.onDestroy");
+ Log.d(TAG,"BaseService.onDestroy");
}
-
+
public void onLowMemory(){
super.onLowMemory();
Log.d(TAG,"BaseService.onLowMemory");
}
-
+
public void onRebind(Intent intent){
super.onRebind(intent);
Log.d(TAG,"BaseService.onRebind");
- }
-
+ }
+
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Log.d(TAG,"BaseService.onStart");
- }
+ }
public int onStartCommand(Intent intent, int flags, int startId){
int res=super.onStartCommand(intent, flags, startId);
Log.d(TAG,"BaseService.onStartCommand");
- return res;
- }
-
+ return res;
+ }
+
public void onTaskRemoved(Intent intent){
super.onTaskRemoved(intent);
Log.d(TAG,"BaseService.onTaskRemoved");
- }
-
-
+ }
+
+
public void onTrimMemory(int level){
super.onTrimMemory(level);
Log.d(TAG,"BaseService.onTrimMemory");
}
-
+
public boolean onUnbind(Intent intent){
boolean res=super.onUnbind(intent);
Log.d(TAG,"BaseService.onUnbind");
return res;
- }
-
-} \ No newline at end of file
+ }
+
+}