OSDN Git Service

wifi: get interface name from wlan.interface
[android-x86/frameworks-base.git] / core / java / android / net / DhcpStateMachine.java
index f5cf14b..2c94650 100644 (file)
@@ -31,6 +31,7 @@ import android.net.NetworkUtils;
 import android.os.Message;
 import android.os.PowerManager;
 import android.os.SystemClock;
+import android.os.SystemProperties;
 import android.util.Log;
 
 /**
@@ -66,6 +67,9 @@ public class DhcpStateMachine extends HierarchicalStateMachine {
     private static final int DHCP_RENEW = 0;
     private static final String ACTION_DHCP_RENEW = "android.net.wifi.DHCP_RENEW";
 
+    //Used for sanity check on setting up renewal
+    private static final int MIN_RENEWAL_TIME_SECS = 5 * 60;  // 5 minutes
+
     private enum DhcpAction {
         START,
         RENEW
@@ -106,7 +110,7 @@ public class DhcpStateMachine extends HierarchicalStateMachine {
 
         mContext = context;
         mController = controller;
-        mInterfaceName = intf;
+        mInterfaceName = intf.isEmpty() ? SystemProperties.get("wlan.interface", "wlan0") : intf;
 
         mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
         Intent dhcpRenewalIntent = new Intent(ACTION_DHCP_RENEW, null);
@@ -114,13 +118,14 @@ public class DhcpStateMachine extends HierarchicalStateMachine {
 
         PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
         mDhcpRenewWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
+        mDhcpRenewWakeLock.setReferenceCounted(false);
 
         mBroadcastReceiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
                 //DHCP renew
                 if (DBG) Log.d(TAG, "Sending a DHCP renewal " + this);
-                //acquire a 40s wakelock to finish DHCP renewal
+                //Lock released after 40s in worst case scenario
                 mDhcpRenewWakeLock.acquire(40000);
                 sendMessage(CMD_RENEW_DHCP);
             }
@@ -163,6 +168,7 @@ public class DhcpStateMachine extends HierarchicalStateMachine {
             switch (message.what) {
                 case CMD_RENEW_DHCP:
                     Log.e(TAG, "Error! Failed to handle a DHCP renewal on " + mInterfaceName);
+                    mDhcpRenewWakeLock.release();
                     break;
                 case HSM_QUIT_CMD:
                     mContext.unregisterReceiver(mBroadcastReceiver);
@@ -265,10 +271,12 @@ public class DhcpStateMachine extends HierarchicalStateMachine {
                         /* Notify controller before starting DHCP */
                         mController.sendMessage(CMD_PRE_DHCP_ACTION);
                         transitionTo(mWaitBeforeRenewalState);
+                        //mDhcpRenewWakeLock is released in WaitBeforeRenewalState
                     } else {
                         if (!runDhcp(DhcpAction.RENEW)) {
                             transitionTo(mStoppedState);
                         }
+                        mDhcpRenewWakeLock.release();
                     }
                     break;
                 case CMD_START_DHCP:
@@ -315,6 +323,10 @@ public class DhcpStateMachine extends HierarchicalStateMachine {
             }
             return retValue;
         }
+        @Override
+        public void exit() {
+            mDhcpRenewWakeLock.release();
+        }
     }
 
     private boolean runDhcp(DhcpAction dhcpAction) {
@@ -331,13 +343,21 @@ public class DhcpStateMachine extends HierarchicalStateMachine {
 
         if (success) {
             Log.d(TAG, "DHCP succeeded on " + mInterfaceName);
-            //Do it a bit earlier than half the lease duration time
-            //to beat the native DHCP client and avoid extra packets
-            //48% for one hour lease time = 29 minutes
-            mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
-                    SystemClock.elapsedRealtime() +
-                    dhcpInfoInternal.leaseDuration * 480, //in milliseconds
-                    mDhcpRenewalIntent);
+           long leaseDuration = dhcpInfoInternal.leaseDuration; //int to long conversion
+
+           //Sanity check for renewal
+           //TODO: would be good to notify the user that his network configuration is
+           //bad and that the device cannot renew below MIN_RENEWAL_TIME_SECS
+           if (leaseDuration < MIN_RENEWAL_TIME_SECS) {
+               leaseDuration = MIN_RENEWAL_TIME_SECS;
+           }
+           //Do it a bit earlier than half the lease duration time
+           //to beat the native DHCP client and avoid extra packets
+           //48% for one hour lease time = 29 minutes
+           mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
+                   SystemClock.elapsedRealtime() +
+                   leaseDuration * 480, //in milliseconds
+                   mDhcpRenewalIntent);
 
             mController.obtainMessage(CMD_POST_DHCP_ACTION, DHCP_SUCCESS, 0, dhcpInfoInternal)
                 .sendToTarget();