Bluetooth: FTP: Acquire wakelocks only when required
This patch updates the wakelock acquiring logic in FTP Server only when
OBEX transaction is being done by FTP Client. Without this patch it was
observed that wakelock was acquired during obex server connection
establishment and was only released when connection was disconnected
which was resulting in extra power consumption when FTP connection was
idle
Change-Id: Iedc72cfdb54bb7a84914c0187063ffccfba25bca
CRs-Fixed: 581718
diff --git a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java
index 923c531..6a6f531 100644
--- a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java
+++ b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java
@@ -32,6 +32,7 @@
import android.content.Context;
import android.os.Message;
import android.os.Handler;
+import android.os.PowerManager;
import android.os.StatFs;
import android.text.TextUtils;
import android.util.Log;
@@ -98,6 +99,8 @@
private Context mContext;
+ private PowerManager.WakeLock mWakeLock = null;
+
public static boolean sIsAborted = false;
public static final String ROOT_FOLDER_PATH = "/sdcard";
@@ -153,13 +156,22 @@
@Override
public int onConnect(final HeaderSet request, HeaderSet reply) {
if (D) Log.d(TAG, "onConnect()+");
+ acquireFtpWakeLock();
+ int returnVal = onConnectInternal(request, reply);
+ releaseFtpWakeLock();
+ if (D) Log.d(TAG, "onConnect()- returning " + returnVal);
+ return returnVal;
+ }
+
+ private int onConnectInternal(final HeaderSet request, HeaderSet reply) {
+ if (D) Log.d(TAG, "onConnectInternal()+");
/* Extract the Target header */
try {
byte[] uuid = (byte[])request.getHeader(HeaderSet.TARGET);
if (uuid == null) {
return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
}
- if (D) Log.d(TAG, "onConnect(): uuid=" + Arrays.toString(uuid));
+ if (D) Log.d(TAG, "onConnectInternal(): uuid=" + Arrays.toString(uuid));
if (uuid.length != UUID_LENGTH) {
Log.w(TAG, "Wrong UUID length");
@@ -175,23 +187,23 @@
/* Add the uuid into the WHO header part of connect reply */
reply.setHeader(HeaderSet.WHO, uuid);
} catch (IOException e) {
- Log.e(TAG,"onConnect "+ e.toString());
+ Log.e(TAG,"onConnectInternal "+ e.toString());
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
/* Extract the remote WHO header and fill it in the Target header*/
try {
byte[] remote = (byte[])request.getHeader(HeaderSet.WHO);
if (remote != null) {
- if (D) Log.d(TAG, "onConnect(): remote=" +
+ if (D) Log.d(TAG, "onConnectInternal(): remote=" +
Arrays.toString(remote));
reply.setHeader(HeaderSet.TARGET, remote);
}
} catch (IOException e) {
- Log.e(TAG,"onConnect "+ e.toString());
+ Log.e(TAG,"onConnectInternal "+ e.toString());
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
- if (V) Log.v(TAG, "onConnect(): uuid is ok, will send out " +
+ if (V) Log.v(TAG, "onConnectInternal(): uuid is ok, will send out " +
"MSG_SESSION_ESTABLISHED msg.");
/* Notify the FTP service about the session establishment */
Message msg = Message.obtain(mCallback);
@@ -206,7 +218,7 @@
"\n SEC: "+ rootSecondaryStoragePath +"\n");
- if (D) Log.d(TAG, "onConnect() -");
+ if (D) Log.d(TAG, "onConnectInternal() -");
return ResponseCodes.OBEX_HTTP_OK;
}
/**
@@ -223,6 +235,7 @@
public void onDisconnect(final HeaderSet req, final HeaderSet resp) {
if (D) Log.d(TAG, "onDisconnect() +");
+ acquireFtpWakeLock();
resp.responseCode = ResponseCodes.OBEX_HTTP_OK;
/* Send a message to the FTP service to close the Server session */
if (mCallback != null) {
@@ -232,6 +245,7 @@
if (V) Log.v(TAG,
"onDisconnect(): msg MSG_SESSION_DISCONNECTED sent out.");
}
+ releaseFtpWakeLock();
if (D) Log.d(TAG, "onDisconnect() -");
}
/**
@@ -240,8 +254,10 @@
@Override
public int onAbort(HeaderSet request, HeaderSet reply) {
if (D) Log.d(TAG, "onAbort() +");
+ acquireFtpWakeLock();
sIsAborted = true;
if (D) Log.d(TAG, "onAbort() -");
+ releaseFtpWakeLock();
return ResponseCodes.OBEX_HTTP_OK;
}
@@ -261,7 +277,16 @@
*/
@Override
public int onDelete(HeaderSet request, HeaderSet reply) {
- if (D) Log.d(TAG, "onDelete() +");
+ if (D) Log.d(TAG, "onDelete()+");
+ acquireFtpWakeLock();
+ int returnVal = onDeleteInternal(request, reply);
+ releaseFtpWakeLock();
+ if (D) Log.d(TAG, "onDelete()- returning " + returnVal);
+ return returnVal;
+ }
+
+ private int onDeleteInternal(HeaderSet request, HeaderSet reply) {
+ if (D) Log.d(TAG, "onDeleteInternal() +");
String name = "";
/* Check if Card is mounted */
if(FileUtils.checkMountedState() == false) {
@@ -282,11 +307,11 @@
if(D) Log.d(TAG, "cannot delete the directory " + name);
return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
}
- if (D) Log.d(TAG,"OnDelete File = " + name +
+ if (D) Log.d(TAG,"onDeleteInternal File = " + name +
"mCurrentPath = " + mCurrentPath );
File deleteFile = new File(mCurrentPath + "/" + name);
if(deleteFile.exists() == true){
- if (D) Log.d(TAG, "onDelete(): Found File" + name + "in folder "
+ if (D) Log.d(TAG, "onDeleteInternal(): Found File" + name + "in folder "
+ mCurrentPath);
if(!deleteFile.canWrite()) {
return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
@@ -311,11 +336,11 @@
return ResponseCodes.OBEX_HTTP_NOT_FOUND;
}
}catch (IOException e) {
- Log.e(TAG,"onDelete "+ e.toString());
+ Log.e(TAG,"onDeleteInternal "+ e.toString());
if (D) Log.d(TAG, "Delete operation failed");
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
- if (D) Log.d(TAG, "onDelete() -");
+ if (D) Log.d(TAG, "onDeleteInternal() -");
return ResponseCodes.OBEX_HTTP_OK;
}
/**
@@ -335,7 +360,16 @@
*/
@Override
public int onPut(final Operation op) {
- if (D) Log.d(TAG, "onPut() +");
+ if (D) Log.d(TAG, "onPut()+");
+ acquireFtpWakeLock();
+ int returnVal = onPutInternal(op);
+ releaseFtpWakeLock();
+ if (D) Log.d(TAG, "onPut()- returning " + returnVal);
+ return returnVal;
+ }
+
+ private int onPutInternal(final Operation op) {
+ if (D) Log.d(TAG, "onPutInternal() +");
HeaderSet request = null;
long length;
String name = "";
@@ -386,7 +420,7 @@
try {
in_stream = op.openInputStream();
} catch (IOException e1) {
- Log.e(TAG,"onPut open input stream "+ e1.toString());
+ Log.e(TAG,"onPutInternal open input stream "+ e1.toString());
if (D) Log.d(TAG, "Error while openInputStream");
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
@@ -452,7 +486,7 @@
}
}
}catch (IOException e1) {
- Log.e(TAG, "onPut File receive"+ e1.toString());
+ Log.e(TAG, "onPutInternal File receive"+ e1.toString());
if (D) Log.d(TAG, "Error when receiving file");
((ServerOperation)op).isAborted = true;
sIsAborted = true;
@@ -465,7 +499,7 @@
try {
buff_op_stream.close();
} catch (IOException e) {
- Log.e(TAG,"onPut close stream "+ e.toString());
+ Log.e(TAG,"onPutInternal close stream "+ e.toString());
if (D) Log.d(TAG, "Error when closing stream after send");
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
@@ -491,11 +525,11 @@
fileinfo.getAbsolutePath());
}catch (IOException e) {
- Log.e(TAG, "onPut headers error "+ e.toString());
+ Log.e(TAG, "onPutInternal headers error "+ e.toString());
if (D) Log.d(TAG, "request headers error");
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
- if (D) Log.d(TAG, "onPut() -");
+ if (D) Log.d(TAG, "onPutInternal() -");
return ResponseCodes.OBEX_HTTP_OK;
}
/**
@@ -518,10 +552,20 @@
* will be used
*/
@Override
- public int onSetPath(final HeaderSet request, final HeaderSet reply, final boolean backup,
- final boolean create) {
+ public int onSetPath(final HeaderSet request, final HeaderSet reply,
+ final boolean backup, final boolean create) {
+ if (D) Log.d(TAG, "onSetPath()+");
+ acquireFtpWakeLock();
+ int returnVal = onSetPathInternal(request, reply, backup, create);
+ releaseFtpWakeLock();
+ if (D) Log.d(TAG, "onSetPath()- returning " + returnVal);
+ return returnVal;
+ }
- if (D) Log.d(TAG, "onSetPath() +");
+ private int onSetPathInternal(final HeaderSet request, final HeaderSet reply,
+ final boolean backup, final boolean create) {
+
+ if (D) Log.d(TAG, "onSetPathInternal() +");
String current_path_tmp = mCurrentPath;
String tmp_path = null;
@@ -534,7 +578,7 @@
try {
tmp_path = (String)request.getHeader(HeaderSet.NAME);
} catch (IOException e) {
- Log.e(TAG,"onSetPath get header"+ e.toString());
+ Log.e(TAG,"onSetPathInternal get header"+ e.toString());
if (D) Log.d(TAG, "Get name header fail");
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
@@ -616,7 +660,7 @@
mCurrentPath = current_path_tmp;
if (V) Log.v(TAG, "after setPath, mCurrentPath == " + mCurrentPath);
- if (D) Log.d(TAG, "onSetPath() -");
+ if (D) Log.d(TAG, "onSetPathInternal() -");
return ResponseCodes.OBEX_HTTP_OK;
}
@@ -626,6 +670,7 @@
@Override
public void onClose() {
if (D) Log.d(TAG, "onClose() +");
+ acquireFtpWakeLock();
/* Send a message to the FTP service to close the Server session */
if (mCallback != null) {
Message msg = Message.obtain(mCallback);
@@ -634,12 +679,22 @@
if (D) Log.d(TAG,
"onClose(): msg MSG_SERVERSESSION_CLOSE sent out.");
}
+ releaseFtpWakeLock();
if (D) Log.d(TAG, "onClose() -");
}
@Override
public int onGet(Operation op) {
- if (D) Log.d(TAG, "onGet() +");
+ if (D) Log.d(TAG, "onGet()+");
+ acquireFtpWakeLock();
+ int returnVal = onGetInternal(op);
+ releaseFtpWakeLock();
+ if (D) Log.d(TAG, "onGet()- returning " + returnVal);
+ return returnVal;
+ }
+
+ private int onGetInternal(Operation op) {
+ if (D) Log.d(TAG, "onGetInternal() +");
sIsAborted = false;
HeaderSet request = null;
@@ -662,7 +717,7 @@
return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
}
} catch (IOException e) {
- Log.e(TAG,"onGet request headers "+ e.toString());
+ Log.e(TAG,"onGetInternal request headers "+ e.toString());
if (D) Log.d(TAG, "request headers error");
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
@@ -749,7 +804,7 @@
File fileinfo = new File (mCurrentPath + "/" + name);
return sendFileContents(op,fileinfo);
}
- if (D) Log.d(TAG, "onGet() -");
+ if (D) Log.d(TAG, "onGetInternal() -");
return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
}
@@ -1154,4 +1209,31 @@
return returnvalue;
}
+
+ private void acquireFtpWakeLock() {
+ if (mWakeLock == null) {
+ PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
+ mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+ "StartingObexFtpTransaction");
+ mWakeLock.setReferenceCounted(false);
+ mWakeLock.acquire();
+ if (V) Log.v(TAG, "mWakeLock acquired");
+ }
+ else
+ {
+ Log.e(TAG, "mWakeLock already acquired");
+ }
+ }
+
+ private void releaseFtpWakeLock() {
+ if (mWakeLock != null) {
+ if (mWakeLock.isHeld()) {
+ mWakeLock.release();
+ if (V) Log.v(TAG, "mWakeLock released");
+ } else {
+ if (V) Log.v(TAG, "mWakeLock already released");
+ }
+ mWakeLock = null;
+ }
+ }
};
diff --git a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
index f17f1f7..45c5440 100644
--- a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
+++ b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
@@ -37,8 +37,6 @@
import android.content.ContentProviderClient;
import android.content.Intent;
import android.os.Message;
-import android.os.PowerManager;
-import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.os.Handler;
import android.text.TextUtils;
@@ -206,8 +204,6 @@
public static boolean isL2capSocket = false;
- private WakeLock mWakeLock;
-
private Notification mConnectedNotification = null;
private BluetoothAdapter mAdapter;
@@ -369,10 +365,6 @@
if (VERBOSE) Log.v(TAG, "Ftp Service onDestroy");
super.onDestroy();
- if (mWakeLock != null) {
- mWakeLock.release();
- mWakeLock = null;
- }
closeService();
}
@@ -489,19 +481,6 @@
private final void startObexServerSession() throws IOException {
if (VERBOSE) Log.v(TAG, "Ftp Service startObexServerSession");
- // acquire the wakeLock before start Obex transaction thread
- if (mWakeLock == null) {
- PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
- mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
- "StartingObexFtpTransaction");
- mWakeLock.setReferenceCounted(false);
- }
-
- if(!mWakeLock.isHeld()) {
- Log.e(TAG,"Acquire partial wake lock");
- mWakeLock.acquire();
- }
-
mFtpServer = new BluetoothFtpObexServer(mSessionStatusHandler, this);
synchronized (this) {
mAuth = new BluetoothFtpAuthenticator(mSessionStatusHandler);
@@ -525,16 +504,6 @@
private void stopObexServerSession() {
if (VERBOSE) Log.v(TAG, "Ftp Service stopObexServerSession");
- // Release the wake lock if obex transaction is over
- if(mWakeLock != null) {
- if (mWakeLock.isHeld()) {
- Log.e(TAG,"Release full wake lock");
- mWakeLock.release();
- mWakeLock = null;
- } else {
- mWakeLock = null;
- }
- }
if (mServerSession != null) {
mServerSession.close();
mServerSession = null;