diff options
Diffstat (limited to 'wifi/java/android/net')
-rw-r--r-- | wifi/java/android/net/wifi/WifiConfigStore.java | 61 | ||||
-rw-r--r-- | wifi/java/android/net/wifi/WifiStateMachine.java | 389 |
2 files changed, 251 insertions, 199 deletions
diff --git a/wifi/java/android/net/wifi/WifiConfigStore.java b/wifi/java/android/net/wifi/WifiConfigStore.java index 6455d84..7f9fc31 100644 --- a/wifi/java/android/net/wifi/WifiConfigStore.java +++ b/wifi/java/android/net/wifi/WifiConfigStore.java @@ -23,6 +23,7 @@ import android.net.LinkAddress; import android.net.LinkProperties; import android.net.NetworkUtils; import android.net.ProxyProperties; +import android.net.RouteInfo; import android.net.wifi.WifiConfiguration.IpAssignment; import android.net.wifi.WifiConfiguration.KeyMgmt; import android.net.wifi.WifiConfiguration.ProxySettings; @@ -120,7 +121,7 @@ class WifiConfigStore { private static final String ipConfigFile = Environment.getDataDirectory() + "/misc/wifi/ipconfig.txt"; - private static final int IPCONFIG_FILE_VERSION = 1; + private static final int IPCONFIG_FILE_VERSION = 2; /* IP and proxy configuration keys */ private static final String ID_KEY = "id"; @@ -445,9 +446,8 @@ class WifiConfigStore { if (iter.hasNext()) { LinkAddress linkAddress = iter.next(); dhcpInfoInternal.ipAddress = linkAddress.getAddress().getHostAddress(); - Iterator<InetAddress>gateways = linkProperties.getGateways().iterator(); - if (gateways.hasNext()) { - dhcpInfoInternal.gateway = gateways.next().getHostAddress(); + for (RouteInfo route : linkProperties.getRoutes()) { + dhcpInfoInternal.addRoute(route); } dhcpInfoInternal.prefixLength = linkAddress.getNetworkPrefixLength(); Iterator<InetAddress> dnsIterator = linkProperties.getDnses().iterator(); @@ -604,9 +604,22 @@ class WifiConfigStore { out.writeUTF(linkAddr.getAddress().getHostAddress()); out.writeInt(linkAddr.getNetworkPrefixLength()); } - for (InetAddress gateway : linkProperties.getGateways()) { + for (RouteInfo route : linkProperties.getRoutes()) { out.writeUTF(GATEWAY_KEY); - out.writeUTF(gateway.getHostAddress()); + LinkAddress dest = route.getDestination(); + if (dest != null) { + out.writeInt(1); + out.writeUTF(dest.getAddress().getHostAddress()); + out.writeInt(dest.getNetworkPrefixLength()); + } else { + out.writeInt(0); + } + if (route.getGateway() != null) { + out.writeInt(1); + out.writeUTF(route.getGateway().getHostAddress()); + } else { + out.writeInt(0); + } } for (InetAddress inetAddr : linkProperties.getDnses()) { out.writeUTF(DNS_KEY); @@ -682,7 +695,8 @@ class WifiConfigStore { in = new DataInputStream(new BufferedInputStream(new FileInputStream( ipConfigFile))); - if (in.readInt() != IPCONFIG_FILE_VERSION) { + int version = in.readInt(); + if (version != 2 && version != 1) { Log.e(TAG, "Bad version on IP configuration file, ignore read"); return; } @@ -709,8 +723,22 @@ class WifiConfigStore { NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt()); linkProperties.addLinkAddress(linkAddr); } else if (key.equals(GATEWAY_KEY)) { - linkProperties.addGateway( - NetworkUtils.numericToInetAddress(in.readUTF())); + LinkAddress dest = null; + InetAddress gateway = null; + if (version == 1) { + // only supported default gateways - leave the dest/prefix empty + gateway = NetworkUtils.numericToInetAddress(in.readUTF()); + } else { + if (in.readInt() == 1) { + dest = new LinkAddress( + NetworkUtils.numericToInetAddress(in.readUTF()), + in.readInt()); + } + if (in.readInt() == 1) { + gateway = NetworkUtils.numericToInetAddress(in.readUTF()); + } + } + linkProperties.addRoute(new RouteInfo(dest, gateway)); } else if (key.equals(DNS_KEY)) { linkProperties.addDns( NetworkUtils.numericToInetAddress(in.readUTF())); @@ -1022,22 +1050,21 @@ class WifiConfigStore { .getLinkAddresses(); Collection<InetAddress> currentDnses = currentConfig.linkProperties.getDnses(); Collection<InetAddress> newDnses = newConfig.linkProperties.getDnses(); - Collection<InetAddress> currentGateways = - currentConfig.linkProperties.getGateways(); - Collection<InetAddress> newGateways = newConfig.linkProperties.getGateways(); + Collection<RouteInfo> currentRoutes = currentConfig.linkProperties.getRoutes(); + Collection<RouteInfo> newRoutes = newConfig.linkProperties.getRoutes(); boolean linkAddressesDiffer = (currentLinkAddresses.size() != newLinkAddresses.size()) || !currentLinkAddresses.containsAll(newLinkAddresses); boolean dnsesDiffer = (currentDnses.size() != newDnses.size()) || !currentDnses.containsAll(newDnses); - boolean gatewaysDiffer = (currentGateways.size() != newGateways.size()) || - !currentGateways.containsAll(newGateways); + boolean routesDiffer = (currentRoutes.size() != newRoutes.size()) || + !currentRoutes.containsAll(newRoutes); if ((currentConfig.ipAssignment != newConfig.ipAssignment) || linkAddressesDiffer || dnsesDiffer || - gatewaysDiffer) { + routesDiffer) { ipChanged = true; } break; @@ -1112,8 +1139,8 @@ class WifiConfigStore { for (LinkAddress linkAddr : config.linkProperties.getLinkAddresses()) { linkProperties.addLinkAddress(linkAddr); } - for (InetAddress gateway : config.linkProperties.getGateways()) { - linkProperties.addGateway(gateway); + for (RouteInfo route : config.linkProperties.getRoutes()) { + linkProperties.addRoute(route); } for (InetAddress dns : config.linkProperties.getDnses()) { linkProperties.addDns(dns); diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java index 16611d8..9125037 100644 --- a/wifi/java/android/net/wifi/WifiStateMachine.java +++ b/wifi/java/android/net/wifi/WifiStateMachine.java @@ -48,6 +48,7 @@ import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.DhcpInfo; import android.net.DhcpInfoInternal; +import android.net.DhcpStateMachine; import android.net.InterfaceConfiguration; import android.net.LinkAddress; import android.net.LinkProperties; @@ -73,6 +74,7 @@ import android.util.LruCache; import com.android.internal.app.IBatteryStats; import com.android.internal.util.AsyncChannel; +import com.android.internal.util.Protocol; import com.android.internal.util.State; import com.android.internal.util.StateMachine; @@ -151,6 +153,7 @@ public class WifiStateMachine extends StateMachine { private NetworkInfo mNetworkInfo; private SupplicantStateTracker mSupplicantStateTracker; private WpsStateMachine mWpsStateMachine; + private DhcpStateMachine mDhcpStateMachine; private AlarmManager mAlarmManager; private PendingIntent mScanIntent; @@ -165,95 +168,97 @@ public class WifiStateMachine extends StateMachine { private static final int EVENTLOG_WIFI_EVENT_HANDLED = 50022; private static final int EVENTLOG_SUPPLICANT_STATE_CHANGED = 50023; + /* The base for wifi message types */ + static final int BASE = Protocol.BASE_WIFI; /* Load the driver */ - static final int CMD_LOAD_DRIVER = 1; + static final int CMD_LOAD_DRIVER = BASE + 1; /* Unload the driver */ - static final int CMD_UNLOAD_DRIVER = 2; + static final int CMD_UNLOAD_DRIVER = BASE + 2; /* Indicates driver load succeeded */ - static final int CMD_LOAD_DRIVER_SUCCESS = 3; + static final int CMD_LOAD_DRIVER_SUCCESS = BASE + 3; /* Indicates driver load failed */ - static final int CMD_LOAD_DRIVER_FAILURE = 4; + static final int CMD_LOAD_DRIVER_FAILURE = BASE + 4; /* Indicates driver unload succeeded */ - static final int CMD_UNLOAD_DRIVER_SUCCESS = 5; + static final int CMD_UNLOAD_DRIVER_SUCCESS = BASE + 5; /* Indicates driver unload failed */ - static final int CMD_UNLOAD_DRIVER_FAILURE = 6; + static final int CMD_UNLOAD_DRIVER_FAILURE = BASE + 6; /* Start the supplicant */ - static final int CMD_START_SUPPLICANT = 11; + static final int CMD_START_SUPPLICANT = BASE + 11; /* Stop the supplicant */ - static final int CMD_STOP_SUPPLICANT = 12; + static final int CMD_STOP_SUPPLICANT = BASE + 12; /* Start the driver */ - static final int CMD_START_DRIVER = 13; + static final int CMD_START_DRIVER = BASE + 13; /* Start the driver */ - static final int CMD_STOP_DRIVER = 14; - /* Indicates DHCP succeded */ - static final int CMD_IP_CONFIG_SUCCESS = 15; - /* Indicates DHCP failed */ - static final int CMD_IP_CONFIG_FAILURE = 16; + static final int CMD_STOP_DRIVER = BASE + 14; + /* Indicates Static IP succeded */ + static final int CMD_STATIC_IP_SUCCESS = BASE + 15; + /* Indicates Static IP failed */ + static final int CMD_STATIC_IP_FAILURE = BASE + 16; /* Start the soft access point */ - static final int CMD_START_AP = 21; + static final int CMD_START_AP = BASE + 21; /* Stop the soft access point */ - static final int CMD_STOP_AP = 22; + static final int CMD_STOP_AP = BASE + 22; - static final int CMD_BLUETOOTH_ADAPTER_STATE_CHANGE = 23; + static final int CMD_BLUETOOTH_ADAPTER_STATE_CHANGE = BASE + 23; /* Supplicant events */ /* Connection to supplicant established */ - static final int SUP_CONNECTION_EVENT = 31; + static final int SUP_CONNECTION_EVENT = BASE + 31; /* Connection to supplicant lost */ - static final int SUP_DISCONNECTION_EVENT = 32; + static final int SUP_DISCONNECTION_EVENT = BASE + 32; /* Driver start completed */ - static final int DRIVER_START_EVENT = 33; + static final int DRIVER_START_EVENT = BASE + 33; /* Driver stop completed */ - static final int DRIVER_STOP_EVENT = 34; + static final int DRIVER_STOP_EVENT = BASE + 34; /* Network connection completed */ - static final int NETWORK_CONNECTION_EVENT = 36; + static final int NETWORK_CONNECTION_EVENT = BASE + 36; /* Network disconnection completed */ - static final int NETWORK_DISCONNECTION_EVENT = 37; + static final int NETWORK_DISCONNECTION_EVENT = BASE + 37; /* Scan results are available */ - static final int SCAN_RESULTS_EVENT = 38; + static final int SCAN_RESULTS_EVENT = BASE + 38; /* Supplicate state changed */ - static final int SUPPLICANT_STATE_CHANGE_EVENT = 39; + static final int SUPPLICANT_STATE_CHANGE_EVENT = BASE + 39; /* Password failure and EAP authentication failure */ - static final int AUTHENTICATION_FAILURE_EVENT = 40; + static final int AUTHENTICATION_FAILURE_EVENT = BASE + 40; /* WPS overlap detected */ - static final int WPS_OVERLAP_EVENT = 41; + static final int WPS_OVERLAP_EVENT = BASE + 41; /* Supplicant commands */ /* Is supplicant alive ? */ - static final int CMD_PING_SUPPLICANT = 51; + static final int CMD_PING_SUPPLICANT = BASE + 51; /* Add/update a network configuration */ - static final int CMD_ADD_OR_UPDATE_NETWORK = 52; + static final int CMD_ADD_OR_UPDATE_NETWORK = BASE + 52; /* Delete a network */ - static final int CMD_REMOVE_NETWORK = 53; + static final int CMD_REMOVE_NETWORK = BASE + 53; /* Enable a network. The device will attempt a connection to the given network. */ - static final int CMD_ENABLE_NETWORK = 54; + static final int CMD_ENABLE_NETWORK = BASE + 54; /* Enable all networks */ - static final int CMD_ENABLE_ALL_NETWORKS = 55; + static final int CMD_ENABLE_ALL_NETWORKS = BASE + 55; /* Disable a network. The device does not attempt a connection to the given network. */ - static final int CMD_DISABLE_NETWORK = 56; + static final int CMD_DISABLE_NETWORK = BASE + 56; /* Blacklist network. De-prioritizes the given BSSID for connection. */ - static final int CMD_BLACKLIST_NETWORK = 57; + static final int CMD_BLACKLIST_NETWORK = BASE + 57; /* Clear the blacklist network list */ - static final int CMD_CLEAR_BLACKLIST = 58; + static final int CMD_CLEAR_BLACKLIST = BASE + 58; /* Save configuration */ - static final int CMD_SAVE_CONFIG = 59; + static final int CMD_SAVE_CONFIG = BASE + 59; /* Supplicant commands after driver start*/ /* Initiate a scan */ - static final int CMD_START_SCAN = 71; + static final int CMD_START_SCAN = BASE + 71; /* Set scan mode. CONNECT_MODE or SCAN_ONLY_MODE */ - static final int CMD_SET_SCAN_MODE = 72; + static final int CMD_SET_SCAN_MODE = BASE + 72; /* Set scan type. SCAN_ACTIVE or SCAN_PASSIVE */ - static final int CMD_SET_SCAN_TYPE = 73; + static final int CMD_SET_SCAN_TYPE = BASE + 73; /* Disconnect from a network */ - static final int CMD_DISCONNECT = 74; + static final int CMD_DISCONNECT = BASE + 74; /* Reconnect to a network */ - static final int CMD_RECONNECT = 75; + static final int CMD_RECONNECT = BASE + 75; /* Reassociate to a network */ - static final int CMD_REASSOCIATE = 76; + static final int CMD_REASSOCIATE = BASE + 76; /* Controls power mode and suspend mode optimizations * * When high perf mode is enabled, power mode is set to @@ -267,19 +272,19 @@ public class WifiStateMachine extends StateMachine { * - turn off roaming * - DTIM wake up settings */ - static final int CMD_SET_HIGH_PERF_MODE = 77; + static final int CMD_SET_HIGH_PERF_MODE = BASE + 77; /* Set the country code */ - static final int CMD_SET_COUNTRY_CODE = 80; + static final int CMD_SET_COUNTRY_CODE = BASE + 80; /* Request connectivity manager wake lock before driver stop */ - static final int CMD_REQUEST_CM_WAKELOCK = 81; + static final int CMD_REQUEST_CM_WAKELOCK = BASE + 81; /* Enables RSSI poll */ - static final int CMD_ENABLE_RSSI_POLL = 82; + static final int CMD_ENABLE_RSSI_POLL = BASE + 82; /* RSSI poll */ - static final int CMD_RSSI_POLL = 83; + static final int CMD_RSSI_POLL = BASE + 83; /* Set up packet filtering */ - static final int CMD_START_PACKET_FILTERING = 84; + static final int CMD_START_PACKET_FILTERING = BASE + 84; /* Clear packet filter */ - static final int CMD_STOP_PACKET_FILTERING = 85; + static final int CMD_STOP_PACKET_FILTERING = BASE + 85; /* Connect to a specified network (network id * or WifiConfiguration) This involves increasing * the priority of the network, enabling the network @@ -288,33 +293,33 @@ public class WifiStateMachine extends StateMachine { * an existing network. All the networks get enabled * upon a successful connection or a failure. */ - static final int CMD_CONNECT_NETWORK = 86; + static final int CMD_CONNECT_NETWORK = BASE + 86; /* Save the specified network. This involves adding * an enabled network (if new) and updating the * config and issuing a save on supplicant config. */ - static final int CMD_SAVE_NETWORK = 87; + static final int CMD_SAVE_NETWORK = BASE + 87; /* Delete the specified network. This involves * removing the network and issuing a save on * supplicant config. */ - static final int CMD_FORGET_NETWORK = 88; + static final int CMD_FORGET_NETWORK = BASE + 88; /* Start Wi-Fi protected setup */ - static final int CMD_START_WPS = 89; + static final int CMD_START_WPS = BASE + 89; /* Set the frequency band */ - static final int CMD_SET_FREQUENCY_BAND = 90; + static final int CMD_SET_FREQUENCY_BAND = BASE + 90; /* Enable background scan for configured networks */ - static final int CMD_ENABLE_BACKGROUND_SCAN = 91; + static final int CMD_ENABLE_BACKGROUND_SCAN = BASE + 91; /* Commands from/to the SupplicantStateTracker */ /* Reset the supplicant state tracker */ - static final int CMD_RESET_SUPPLICANT_STATE = 111; + static final int CMD_RESET_SUPPLICANT_STATE = BASE + 111; /* Commands/events reported by WpsStateMachine */ /* Indicates the completion of WPS activity */ - static final int WPS_COMPLETED_EVENT = 121; + static final int WPS_COMPLETED_EVENT = BASE + 121; /* Reset the WPS state machine */ - static final int CMD_RESET_WPS_STATE = 122; + static final int CMD_RESET_WPS_STATE = BASE + 122; private static final int CONNECT_MODE = 1; private static final int SCAN_ONLY_MODE = 2; @@ -335,8 +340,11 @@ public class WifiStateMachine extends StateMachine { */ private static final int DEFAULT_MAX_DHCP_RETRIES = 9; - private static final int POWER_MODE_ACTIVE = 1; - private static final int POWER_MODE_AUTO = 0; + static final int POWER_MODE_ACTIVE = 1; + static final int POWER_MODE_AUTO = 0; + + /* Tracks the power mode for restoration after a DHCP request/renewal goes through */ + private int mPowerMode = POWER_MODE_AUTO; /** * Default framework scan interval in milliseconds. This is used in the scenario in which @@ -1405,8 +1413,10 @@ public class WifiStateMachine extends StateMachine { */ NetworkUtils.resetConnections(mInterfaceName); - if (!NetworkUtils.stopDhcp(mInterfaceName)) { - Log.e(TAG, "Could not stop DHCP"); + if (mDhcpStateMachine != null) { + mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_STOP_DHCP); + mDhcpStateMachine.quit(); + mDhcpStateMachine = null; } /* Disable interface */ @@ -1432,6 +1442,100 @@ public class WifiStateMachine extends StateMachine { } + void handlePreDhcpSetup() { + if (!mBluetoothConnectionActive) { + /* + * There are problems setting the Wi-Fi driver's power + * mode to active when bluetooth coexistence mode is + * enabled or sense. + * <p> + * We set Wi-Fi to active mode when + * obtaining an IP address because we've found + * compatibility issues with some routers with low power + * mode. + * <p> + * In order for this active power mode to properly be set, + * we disable coexistence mode until we're done with + * obtaining an IP address. One exception is if we + * are currently connected to a headset, since disabling + * coexistence would interrupt that connection. + */ + // Disable the coexistence mode + WifiNative.setBluetoothCoexistenceModeCommand( + WifiNative.BLUETOOTH_COEXISTENCE_MODE_DISABLED); + } + + mPowerMode = WifiNative.getPowerModeCommand(); + if (mPowerMode < 0) { + // Handle the case where supplicant driver does not support + // getPowerModeCommand. + mPowerMode = WifiStateMachine.POWER_MODE_AUTO; + } + if (mPowerMode != WifiStateMachine.POWER_MODE_ACTIVE) { + WifiNative.setPowerModeCommand(WifiStateMachine.POWER_MODE_ACTIVE); + } + } + + + void handlePostDhcpSetup() { + /* restore power mode */ + WifiNative.setPowerModeCommand(mPowerMode); + + // Set the coexistence mode back to its default value + WifiNative.setBluetoothCoexistenceModeCommand( + WifiNative.BLUETOOTH_COEXISTENCE_MODE_SENSE); + } + + private void handleSuccessfulIpConfiguration(DhcpInfoInternal dhcpInfoInternal) { + synchronized (mDhcpInfoInternal) { + mDhcpInfoInternal = dhcpInfoInternal; + } + mLastSignalLevel = -1; // force update of signal strength + WifiConfigStore.setIpConfiguration(mLastNetworkId, dhcpInfoInternal); + InetAddress addr = NetworkUtils.numericToInetAddress(dhcpInfoInternal.ipAddress); + mWifiInfo.setInetAddress(addr); + if (getNetworkDetailedState() == DetailedState.CONNECTED) { + //DHCP renewal in connected state + LinkProperties linkProperties = dhcpInfoInternal.makeLinkProperties(); + linkProperties.setHttpProxy(WifiConfigStore.getProxyProperties(mLastNetworkId)); + linkProperties.setInterfaceName(mInterfaceName); + if (!linkProperties.equals(mLinkProperties)) { + Log.d(TAG, "Link configuration changed for netId: " + mLastNetworkId + + " old: " + mLinkProperties + "new: " + linkProperties); + NetworkUtils.resetConnections(mInterfaceName); + mLinkProperties = linkProperties; + sendLinkConfigurationChangedBroadcast(); + } + } else { + configureLinkProperties(); + setNetworkDetailedState(DetailedState.CONNECTED); + sendNetworkStateChangeBroadcast(mLastBssid); + } + } + + private void handleFailedIpConfiguration() { + Log.e(TAG, "IP configuration failed"); + + mWifiInfo.setInetAddress(null); + /** + * If we've exceeded the maximum number of retries for DHCP + * to a given network, disable the network + */ + if (++mReconnectCount > getMaxDhcpRetries()) { + Log.e(TAG, "Failed " + + mReconnectCount + " times, Disabling " + mLastNetworkId); + WifiConfigStore.disableNetwork(mLastNetworkId); + mReconnectCount = 0; + } + + /* DHCP times out after about 30 seconds, we do a + * disconnect and an immediate reconnect to try again + */ + WifiNative.disconnectCommand(); + WifiNative.reconnectCommand(); + + } + /********************************************************* * Notifications from WifiMonitor @@ -1603,6 +1707,8 @@ public class WifiStateMachine extends StateMachine { case CMD_FORGET_NETWORK: case CMD_RSSI_POLL: case CMD_ENABLE_ALL_NETWORKS: + case DhcpStateMachine.CMD_PRE_DHCP_ACTION: + case DhcpStateMachine.CMD_POST_DHCP_ACTION: break; case CMD_START_WPS: /* Return failure when the state machine cannot handle WPS initiation*/ @@ -2480,74 +2586,18 @@ public class WifiStateMachine extends StateMachine { } class ConnectingState extends State { - boolean mModifiedBluetoothCoexistenceMode; - int mPowerMode; - boolean mUseStaticIp; - Thread mDhcpThread; @Override public void enter() { if (DBG) Log.d(TAG, getName() + "\n"); EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName()); - mUseStaticIp = WifiConfigStore.isUsingStaticIp(mLastNetworkId); - if (!mUseStaticIp) { - mDhcpThread = null; - mModifiedBluetoothCoexistenceMode = false; - mPowerMode = POWER_MODE_AUTO; - - if (!mBluetoothConnectionActive) { - /* - * There are problems setting the Wi-Fi driver's power - * mode to active when bluetooth coexistence mode is - * enabled or sense. - * <p> - * We set Wi-Fi to active mode when - * obtaining an IP address because we've found - * compatibility issues with some routers with low power - * mode. - * <p> - * In order for this active power mode to properly be set, - * we disable coexistence mode until we're done with - * obtaining an IP address. One exception is if we - * are currently connected to a headset, since disabling - * coexistence would interrupt that connection. - */ - mModifiedBluetoothCoexistenceMode = true; - - // Disable the coexistence mode - WifiNative.setBluetoothCoexistenceModeCommand( - WifiNative.BLUETOOTH_COEXISTENCE_MODE_DISABLED); - } - - mPowerMode = WifiNative.getPowerModeCommand(); - if (mPowerMode < 0) { - // Handle the case where supplicant driver does not support - // getPowerModeCommand. - mPowerMode = POWER_MODE_AUTO; - } - if (mPowerMode != POWER_MODE_ACTIVE) { - WifiNative.setPowerModeCommand(POWER_MODE_ACTIVE); - } - Log.d(TAG, "DHCP request started"); - mDhcpThread = new Thread(new Runnable() { - public void run() { - DhcpInfoInternal dhcpInfoInternal = new DhcpInfoInternal(); - if (NetworkUtils.runDhcp(mInterfaceName, dhcpInfoInternal)) { - Log.d(TAG, "DHCP request succeeded"); - synchronized (mDhcpInfoInternal) { - mDhcpInfoInternal = dhcpInfoInternal; - } - WifiConfigStore.setIpConfiguration(mLastNetworkId, dhcpInfoInternal); - sendMessage(CMD_IP_CONFIG_SUCCESS); - } else { - Log.d(TAG, "DHCP request failed: " + - NetworkUtils.getDhcpError()); - sendMessage(CMD_IP_CONFIG_FAILURE); - } - } - }); - mDhcpThread.start(); + if (!WifiConfigStore.isUsingStaticIp(mLastNetworkId)) { + //start DHCP + mDhcpStateMachine = DhcpStateMachine.makeDhcpStateMachine( + mContext, WifiStateMachine.this, mInterfaceName); + mDhcpStateMachine.registerForPreDhcpNotification(); + mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_START_DHCP); } else { DhcpInfoInternal dhcpInfoInternal = WifiConfigStore.getIpConfiguration( mLastNetworkId); @@ -2559,16 +2609,13 @@ public class WifiStateMachine extends StateMachine { try { netd.setInterfaceConfig(mInterfaceName, ifcg); Log.v(TAG, "Static IP configuration succeeded"); - synchronized (mDhcpInfoInternal) { - mDhcpInfoInternal = dhcpInfoInternal; - } - sendMessage(CMD_IP_CONFIG_SUCCESS); + sendMessage(CMD_STATIC_IP_SUCCESS, dhcpInfoInternal); } catch (RemoteException re) { Log.v(TAG, "Static IP configuration failed: " + re); - sendMessage(CMD_IP_CONFIG_FAILURE); + sendMessage(CMD_STATIC_IP_FAILURE); } catch (IllegalStateException e) { Log.v(TAG, "Static IP configuration failed: " + e); - sendMessage(CMD_IP_CONFIG_FAILURE); + sendMessage(CMD_STATIC_IP_FAILURE); } } } @@ -2577,44 +2624,26 @@ public class WifiStateMachine extends StateMachine { if (DBG) Log.d(TAG, getName() + message.toString() + "\n"); switch(message.what) { - case CMD_IP_CONFIG_SUCCESS: - mLastSignalLevel = -1; // force update of signal strength - InetAddress addr; - synchronized (mDhcpInfoInternal) { - addr = NetworkUtils.numericToInetAddress(mDhcpInfoInternal.ipAddress); - } - mWifiInfo.setInetAddress(addr); - configureLinkProperties(); - if (getNetworkDetailedState() == DetailedState.CONNECTED) { - sendLinkConfigurationChangedBroadcast(); - } else { - setNetworkDetailedState(DetailedState.CONNECTED); - sendNetworkStateChangeBroadcast(mLastBssid); + case DhcpStateMachine.CMD_PRE_DHCP_ACTION: + handlePreDhcpSetup(); + mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_PRE_DHCP_ACTION_COMPLETE); + break; + case DhcpStateMachine.CMD_POST_DHCP_ACTION: + handlePostDhcpSetup(); + if (message.arg1 == DhcpStateMachine.DHCP_SUCCESS) { + handleSuccessfulIpConfiguration((DhcpInfoInternal) message.obj); + transitionTo(mConnectedState); + } else if (message.arg1 == DhcpStateMachine.DHCP_FAILURE) { + handleFailedIpConfiguration(); + transitionTo(mDisconnectingState); } - //TODO: The framework is not detecting a DHCP renewal and a possible - //IP change. we should detect this and send out a config change broadcast + break; + case CMD_STATIC_IP_SUCCESS: + handleSuccessfulIpConfiguration((DhcpInfoInternal) message.obj); transitionTo(mConnectedState); break; - case CMD_IP_CONFIG_FAILURE: - mWifiInfo.setInetAddress(null); - - Log.e(TAG, "IP configuration failed"); - /** - * If we've exceeded the maximum number of retries for DHCP - * to a given network, disable the network - */ - if (++mReconnectCount > getMaxDhcpRetries()) { - Log.e(TAG, "Failed " + - mReconnectCount + " times, Disabling " + mLastNetworkId); - WifiConfigStore.disableNetwork(mLastNetworkId); - mReconnectCount = 0; - } - - /* DHCP times out after about 30 seconds, we do a - * disconnect and an immediate reconnect to try again - */ - WifiNative.disconnectCommand(); - WifiNative.reconnectCommand(); + case CMD_STATIC_IP_FAILURE: + handleFailedIpConfiguration(); transitionTo(mDisconnectingState); break; case CMD_DISCONNECT: @@ -2658,23 +2687,6 @@ public class WifiStateMachine extends StateMachine { EventLog.writeEvent(EVENTLOG_WIFI_EVENT_HANDLED, message.what); return HANDLED; } - - @Override - public void exit() { - /* reset power state & bluetooth coexistence if on DHCP */ - if (!mUseStaticIp) { - if (mPowerMode != POWER_MODE_ACTIVE) { - WifiNative.setPowerModeCommand(mPowerMode); - } - - if (mModifiedBluetoothCoexistenceMode) { - // Set the coexistence mode back to its default value - WifiNative.setBluetoothCoexistenceModeCommand( - WifiNative.BLUETOOTH_COEXISTENCE_MODE_SENSE); - } - } - - } } class ConnectedState extends State { @@ -2692,6 +2704,19 @@ public class WifiStateMachine extends StateMachine { if (DBG) Log.d(TAG, getName() + message.toString() + "\n"); boolean eventLoggingEnabled = true; switch (message.what) { + case DhcpStateMachine.CMD_PRE_DHCP_ACTION: + handlePreDhcpSetup(); + mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_PRE_DHCP_ACTION_COMPLETE); + break; + case DhcpStateMachine.CMD_POST_DHCP_ACTION: + handlePostDhcpSetup(); + if (message.arg1 == DhcpStateMachine.DHCP_SUCCESS) { + handleSuccessfulIpConfiguration((DhcpInfoInternal) message.obj); + } else if (message.arg1 == DhcpStateMachine.DHCP_FAILURE) { + handleFailedIpConfiguration(); + transitionTo(mDisconnectingState); + } + break; case CMD_DISCONNECT: WifiNative.disconnectCommand(); transitionTo(mDisconnectingState); |