Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vinit Deshapnde | ffadfb9 | 2013-12-06 15:12:41 -0800 | [diff] [blame] | 17 | package com.android.server.wifi; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 18 | |
| 19 | import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED; |
| 20 | import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING; |
| 21 | import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED; |
| 22 | import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING; |
| 23 | import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN; |
| 24 | |
| 25 | /** |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 26 | * TODO: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 27 | * Deprecate WIFI_STATE_UNKNOWN |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 28 | */ |
| 29 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; |
| 30 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING; |
| 31 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED; |
| 32 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING; |
| 33 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED; |
| 34 | |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 35 | import android.app.AlarmManager; |
| 36 | import android.app.PendingIntent; |
Jesse Wilson | 34ee52e | 2011-02-10 11:52:17 -0800 | [diff] [blame] | 37 | import android.app.backup.IBackupManager; |
| 38 | import android.bluetooth.BluetoothAdapter; |
| 39 | import android.content.BroadcastReceiver; |
| 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
| 42 | import android.content.IntentFilter; |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Irfan Sheriff | 3fb4ba6 | 2012-08-09 15:07:45 -0700 | [diff] [blame] | 44 | import android.database.ContentObserver; |
Jesse Wilson | 34ee52e | 2011-02-10 11:52:17 -0800 | [diff] [blame] | 45 | import android.net.ConnectivityManager; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 46 | import android.net.DhcpResults; |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 47 | import android.net.DhcpStateMachine; |
Robert Greenwalt | 27d3c59 | 2011-01-19 18:57:22 -0800 | [diff] [blame] | 48 | import android.net.InterfaceConfiguration; |
Jesse Wilson | 34ee52e | 2011-02-10 11:52:17 -0800 | [diff] [blame] | 49 | import android.net.LinkAddress; |
| 50 | import android.net.LinkProperties; |
| 51 | import android.net.NetworkInfo; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 52 | import android.net.NetworkInfo.DetailedState; |
Robert Greenwalt | 27d3c59 | 2011-01-19 18:57:22 -0800 | [diff] [blame] | 53 | import android.net.NetworkUtils; |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 54 | import android.net.RouteInfo; |
Vinit Deshapnde | ffadfb9 | 2013-12-06 15:12:41 -0800 | [diff] [blame] | 55 | import android.net.wifi.BatchedScanResult; |
| 56 | import android.net.wifi.BatchedScanSettings; |
| 57 | import android.net.wifi.RssiPacketCountInfo; |
| 58 | import android.net.wifi.ScanResult; |
| 59 | import android.net.wifi.SupplicantState; |
| 60 | import android.net.wifi.WifiConfiguration; |
| 61 | import android.net.wifi.WifiInfo; |
| 62 | import android.net.wifi.WifiManager; |
| 63 | import android.net.wifi.WifiSsid; |
| 64 | import android.net.wifi.WpsInfo; |
| 65 | import android.net.wifi.WpsResult; |
Irfan Sheriff | e4c56c9 | 2011-01-12 16:33:58 -0800 | [diff] [blame] | 66 | import android.net.wifi.WpsResult.Status; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 67 | import android.net.wifi.p2p.WifiP2pManager; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 68 | import android.os.BatteryStats; |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 69 | import android.os.Bundle; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 70 | import android.os.IBinder; |
| 71 | import android.os.INetworkManagementService; |
Jesse Wilson | 34ee52e | 2011-02-10 11:52:17 -0800 | [diff] [blame] | 72 | import android.os.Message; |
Irfan Sheriff | ebe606f | 2011-02-24 11:39:15 -0800 | [diff] [blame] | 73 | import android.os.Messenger; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 74 | import android.os.PowerManager; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 75 | import android.os.RemoteException; |
| 76 | import android.os.ServiceManager; |
Robert Greenwalt | dee971b | 2013-09-11 15:21:10 -0700 | [diff] [blame] | 77 | import android.os.SystemClock; |
Jesse Wilson | 34ee52e | 2011-02-10 11:52:17 -0800 | [diff] [blame] | 78 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 79 | import android.os.UserHandle; |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 80 | import android.os.WorkSource; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 81 | import android.provider.Settings; |
Vineeta Srivastava | c2cbd4c | 2013-06-17 20:24:47 +0000 | [diff] [blame] | 82 | import android.util.LruCache; |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 83 | import android.text.TextUtils; |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 84 | |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 85 | import com.android.internal.R; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 86 | import com.android.internal.app.IBatteryStats; |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 87 | import com.android.internal.util.AsyncChannel; |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 88 | import com.android.internal.util.Protocol; |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 89 | import com.android.internal.util.State; |
| 90 | import com.android.internal.util.StateMachine; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 91 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 92 | import com.android.server.net.BaseNetworkObserver; |
Vinit Deshapnde | ffadfb9 | 2013-12-06 15:12:41 -0800 | [diff] [blame] | 93 | import com.android.server.wifi.p2p.WifiP2pService; |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 94 | |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 95 | import java.io.FileDescriptor; |
| 96 | import java.io.PrintWriter; |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 97 | import java.net.InetAddress; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 98 | import java.util.ArrayList; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 99 | import java.util.List; |
Vinit Deshapnde | da40d92 | 2013-10-21 11:58:40 -0700 | [diff] [blame] | 100 | import java.util.Locale; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 101 | import java.util.concurrent.atomic.AtomicInteger; |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 102 | import java.util.concurrent.atomic.AtomicBoolean; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 103 | import java.util.Iterator; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 104 | import java.util.regex.Pattern; |
| 105 | |
| 106 | /** |
| 107 | * Track the state of Wifi connectivity. All event handling is done here, |
| 108 | * and all changes in connectivity state are initiated here. |
| 109 | * |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 110 | * Wi-Fi now supports three modes of operation: Client, SoftAp and p2p |
| 111 | * In the current implementation, we support concurrent wifi p2p and wifi operation. |
| 112 | * The WifiStateMachine handles SoftAp and Client operations while WifiP2pService |
| 113 | * handles p2p operation. |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 114 | * |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 115 | * @hide |
| 116 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 117 | public class WifiStateMachine extends StateMachine { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 118 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 119 | private static final String NETWORKTYPE = "WIFI"; |
| 120 | private static final boolean DBG = false; |
| 121 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 122 | private WifiMonitor mWifiMonitor; |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 123 | private WifiNative mWifiNative; |
| 124 | private WifiConfigStore mWifiConfigStore; |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 125 | private INetworkManagementService mNwService; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 126 | private ConnectivityManager mCm; |
| 127 | |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 128 | private final boolean mP2pSupported; |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 129 | private final AtomicBoolean mP2pConnected = new AtomicBoolean(false); |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 130 | private boolean mTemporarilyDisconnectWifi = false; |
Irfan Sheriff | 4dd5a25 | 2012-05-22 12:53:47 -0700 | [diff] [blame] | 131 | private final String mPrimaryDeviceType; |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 132 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 133 | /* Scan results handling */ |
Irfan Sheriff | 06d371f | 2012-08-21 11:44:01 -0700 | [diff] [blame] | 134 | private List<ScanResult> mScanResults = new ArrayList<ScanResult>(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 135 | private static final Pattern scanResultPattern = Pattern.compile("\t+"); |
| 136 | private static final int SCAN_RESULT_CACHE_SIZE = 80; |
Jesse Wilson | 34ee52e | 2011-02-10 11:52:17 -0800 | [diff] [blame] | 137 | private final LruCache<String, ScanResult> mScanResultCache; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 138 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 139 | /* Batch scan results */ |
| 140 | private final List<BatchedScanResult> mBatchedScanResults = |
| 141 | new ArrayList<BatchedScanResult>(); |
| 142 | private int mBatchedScanOwnerUid = UNKNOWN_SCAN_SOURCE; |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 143 | private int mExpectedBatchedScans = 0; |
| 144 | private long mBatchedScanMinPollTime = 0; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 145 | |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 146 | /* Chipset supports background scan */ |
| 147 | private final boolean mBackgroundScanSupported; |
| 148 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 149 | private String mInterfaceName; |
Yoshihiko Ikenaga | 7d9cf11 | 2012-11-16 16:58:10 +0900 | [diff] [blame] | 150 | /* Tethering interface could be separate from wlan interface */ |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 151 | private String mTetherInterfaceName; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 152 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 153 | private int mLastSignalLevel = -1; |
| 154 | private String mLastBssid; |
| 155 | private int mLastNetworkId; |
| 156 | private boolean mEnableRssiPolling = false; |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 157 | private boolean mEnableBackgroundScan = false; |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 158 | private int mRssiPollToken = 0; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 159 | private int mReconnectCount = 0; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 160 | /* 3 operational states for STA operation: CONNECT_MODE, SCAN_ONLY_MODE, SCAN_ONLY_WIFI_OFF_MODE |
| 161 | * In CONNECT_MODE, the STA can scan and connect to an access point |
| 162 | * In SCAN_ONLY_MODE, the STA can only scan for access points |
| 163 | * In SCAN_ONLY_WIFI_OFF_MODE, the STA can only scan for access points with wifi toggle being off |
| 164 | */ |
| 165 | private int mOperationalMode = CONNECT_MODE; |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 166 | private boolean mScanResultIsPending = false; |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 167 | private WorkSource mScanWorkSource = null; |
| 168 | private static final int UNKNOWN_SCAN_SOURCE = -1; |
Irfan Sheriff | 1c045ec | 2012-09-16 14:55:26 -0700 | [diff] [blame] | 169 | /* Tracks if state machine has received any screen state change broadcast yet. |
| 170 | * We can miss one of these at boot. |
| 171 | */ |
| 172 | private AtomicBoolean mScreenBroadcastReceived = new AtomicBoolean(false); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 173 | |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 174 | private boolean mBluetoothConnectionActive = false; |
| 175 | |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 176 | private PowerManager.WakeLock mSuspendWakeLock; |
| 177 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 178 | /** |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 179 | * Interval in milliseconds between polling for RSSI |
| 180 | * and linkspeed information |
| 181 | */ |
| 182 | private static final int POLL_RSSI_INTERVAL_MSECS = 3000; |
| 183 | |
| 184 | /** |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 185 | * Delay between supplicant restarts upon failure to establish connection |
| 186 | */ |
| 187 | private static final int SUPPLICANT_RESTART_INTERVAL_MSECS = 5000; |
| 188 | |
| 189 | /** |
| 190 | * Number of times we attempt to restart supplicant |
| 191 | */ |
| 192 | private static final int SUPPLICANT_RESTART_TRIES = 5; |
| 193 | |
| 194 | private int mSupplicantRestartCount = 0; |
Irfan Sheriff | cf54e4a | 2011-09-19 16:27:16 -0700 | [diff] [blame] | 195 | /* Tracks sequence number on stop failure message */ |
| 196 | private int mSupplicantStopFailureToken = 0; |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 197 | |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 198 | /** |
| 199 | * Tether state change notification time out |
| 200 | */ |
| 201 | private static final int TETHER_NOTIFICATION_TIME_OUT_MSECS = 5000; |
| 202 | |
| 203 | /* Tracks sequence number on a tether notification time out */ |
| 204 | private int mTetherToken = 0; |
| 205 | |
Irfan Sheriff | f91d4e7 | 2012-05-14 14:26:07 -0700 | [diff] [blame] | 206 | /** |
| 207 | * Driver start time out. |
| 208 | */ |
| 209 | private static final int DRIVER_START_TIME_OUT_MSECS = 10000; |
| 210 | |
| 211 | /* Tracks sequence number on a driver time out */ |
| 212 | private int mDriverStartToken = 0; |
| 213 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 214 | /** |
| 215 | * The link properties of the wifi interface. |
| 216 | * Do not modify this directly; use updateLinkProperties instead. |
| 217 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 218 | private LinkProperties mLinkProperties; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 219 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 220 | /** |
| 221 | * Subset of link properties coming from netlink. |
| 222 | * Currently includes IPv4 and IPv6 addresses. In the future will also include IPv6 DNS servers |
| 223 | * and domains obtained from router advertisements (RFC 6106). |
| 224 | */ |
| 225 | private final LinkProperties mNetlinkLinkProperties; |
| 226 | |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 227 | /* Tracks sequence number on a periodic scan message */ |
| 228 | private int mPeriodicScanToken = 0; |
| 229 | |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 230 | // Wakelock held during wifi start/stop and driver load/unload |
| 231 | private PowerManager.WakeLock mWakeLock; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 232 | |
| 233 | private Context mContext; |
| 234 | |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 235 | private final Object mDhcpResultsLock = new Object(); |
| 236 | private DhcpResults mDhcpResults; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 237 | private WifiInfo mWifiInfo; |
| 238 | private NetworkInfo mNetworkInfo; |
| 239 | private SupplicantStateTracker mSupplicantStateTracker; |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 240 | private DhcpStateMachine mDhcpStateMachine; |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 241 | private boolean mDhcpActive = false; |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 242 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 243 | private class InterfaceObserver extends BaseNetworkObserver { |
| 244 | private WifiStateMachine mWifiStateMachine; |
| 245 | |
| 246 | InterfaceObserver(WifiStateMachine wifiStateMachine) { |
| 247 | super(); |
| 248 | mWifiStateMachine = wifiStateMachine; |
| 249 | } |
| 250 | |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 251 | private void maybeLog(String operation, String iface, LinkAddress address) { |
| 252 | if (DBG) { |
| 253 | log(operation + ": " + address + " on " + iface + |
| 254 | " flags " + address.getFlags() + " scope " + address.getScope()); |
| 255 | } |
| 256 | } |
| 257 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 258 | @Override |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 259 | public void addressUpdated(String iface, LinkAddress address) { |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 260 | if (mWifiStateMachine.mInterfaceName.equals(iface)) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 261 | maybeLog("addressUpdated", iface, address); |
Lorenzo Colitti | 5ad421a | 2013-11-17 15:05:02 +0900 | [diff] [blame] | 262 | mWifiStateMachine.sendMessage(CMD_IP_ADDRESS_UPDATED, address); |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | @Override |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 267 | public void addressRemoved(String iface, LinkAddress address) { |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 268 | if (mWifiStateMachine.mInterfaceName.equals(iface)) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 269 | maybeLog("addressRemoved", iface, address); |
Lorenzo Colitti | 5ad421a | 2013-11-17 15:05:02 +0900 | [diff] [blame] | 270 | mWifiStateMachine.sendMessage(CMD_IP_ADDRESS_REMOVED, address); |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private InterfaceObserver mInterfaceObserver; |
| 276 | |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 277 | private AlarmManager mAlarmManager; |
| 278 | private PendingIntent mScanIntent; |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 279 | private PendingIntent mDriverStopIntent; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 280 | private PendingIntent mBatchedScanIntervalIntent; |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 281 | |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 282 | /* Tracks current frequency mode */ |
| 283 | private AtomicInteger mFrequencyBand = new AtomicInteger(WifiManager.WIFI_FREQUENCY_BAND_AUTO); |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 284 | |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 285 | /* Tracks if we are filtering Multicast v4 packets. Default is to filter. */ |
| 286 | private AtomicBoolean mFilteringMulticastV4Packets = new AtomicBoolean(true); |
| 287 | |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 288 | // Channel for sending replies. |
| 289 | private AsyncChannel mReplyChannel = new AsyncChannel(); |
| 290 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 291 | private WifiP2pManager mWifiP2pManager; |
| 292 | //Used to initiate a connection with WifiP2pService |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 293 | private AsyncChannel mWifiP2pChannel; |
| 294 | private AsyncChannel mWifiApConfigChannel; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 295 | |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 296 | /* The base for wifi message types */ |
| 297 | static final int BASE = Protocol.BASE_WIFI; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 298 | /* Start the supplicant */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 299 | static final int CMD_START_SUPPLICANT = BASE + 11; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 300 | /* Stop the supplicant */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 301 | static final int CMD_STOP_SUPPLICANT = BASE + 12; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 302 | /* Start the driver */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 303 | static final int CMD_START_DRIVER = BASE + 13; |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 304 | /* Stop the driver */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 305 | static final int CMD_STOP_DRIVER = BASE + 14; |
Yoshihiko Ikenaga | 7d9cf11 | 2012-11-16 16:58:10 +0900 | [diff] [blame] | 306 | /* Indicates Static IP succeeded */ |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 307 | static final int CMD_STATIC_IP_SUCCESS = BASE + 15; |
| 308 | /* Indicates Static IP failed */ |
| 309 | static final int CMD_STATIC_IP_FAILURE = BASE + 16; |
Irfan Sheriff | cf54e4a | 2011-09-19 16:27:16 -0700 | [diff] [blame] | 310 | /* Indicates supplicant stop failed */ |
| 311 | static final int CMD_STOP_SUPPLICANT_FAILED = BASE + 17; |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 312 | /* Delayed stop to avoid shutting down driver too quick*/ |
| 313 | static final int CMD_DELAYED_STOP_DRIVER = BASE + 18; |
Irfan Sheriff | f91d4e7 | 2012-05-14 14:26:07 -0700 | [diff] [blame] | 314 | /* A delayed message sent to start driver when it fail to come up */ |
| 315 | static final int CMD_DRIVER_START_TIMED_OUT = BASE + 19; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 316 | |
| 317 | /* Start the soft access point */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 318 | static final int CMD_START_AP = BASE + 21; |
Yoshihiko Ikenaga | 7d9cf11 | 2012-11-16 16:58:10 +0900 | [diff] [blame] | 319 | /* Indicates soft ap start succeeded */ |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 320 | static final int CMD_START_AP_SUCCESS = BASE + 22; |
| 321 | /* Indicates soft ap start failed */ |
| 322 | static final int CMD_START_AP_FAILURE = BASE + 23; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 323 | /* Stop the soft access point */ |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 324 | static final int CMD_STOP_AP = BASE + 24; |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 325 | /* Set the soft access point configuration */ |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 326 | static final int CMD_SET_AP_CONFIG = BASE + 25; |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 327 | /* Soft access point configuration set completed */ |
| 328 | static final int CMD_SET_AP_CONFIG_COMPLETED = BASE + 26; |
| 329 | /* Request the soft access point configuration */ |
| 330 | static final int CMD_REQUEST_AP_CONFIG = BASE + 27; |
| 331 | /* Response to access point configuration request */ |
| 332 | static final int CMD_RESPONSE_AP_CONFIG = BASE + 28; |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 333 | /* Invoked when getting a tether state change notification */ |
| 334 | static final int CMD_TETHER_STATE_CHANGE = BASE + 29; |
| 335 | /* A delayed message sent to indicate tether state change failed to arrive */ |
| 336 | static final int CMD_TETHER_NOTIFICATION_TIMED_OUT = BASE + 30; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 337 | |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 338 | static final int CMD_BLUETOOTH_ADAPTER_STATE_CHANGE = BASE + 31; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 339 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 340 | /* Supplicant commands */ |
| 341 | /* Is supplicant alive ? */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 342 | static final int CMD_PING_SUPPLICANT = BASE + 51; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 343 | /* Add/update a network configuration */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 344 | static final int CMD_ADD_OR_UPDATE_NETWORK = BASE + 52; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 345 | /* Delete a network */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 346 | static final int CMD_REMOVE_NETWORK = BASE + 53; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 347 | /* Enable a network. The device will attempt a connection to the given network. */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 348 | static final int CMD_ENABLE_NETWORK = BASE + 54; |
Irfan Sheriff | 8e86b89 | 2010-12-22 11:02:20 -0800 | [diff] [blame] | 349 | /* Enable all networks */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 350 | static final int CMD_ENABLE_ALL_NETWORKS = BASE + 55; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 351 | /* Blacklist network. De-prioritizes the given BSSID for connection. */ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 352 | static final int CMD_BLACKLIST_NETWORK = BASE + 56; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 353 | /* Clear the blacklist network list */ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 354 | static final int CMD_CLEAR_BLACKLIST = BASE + 57; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 355 | /* Save configuration */ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 356 | static final int CMD_SAVE_CONFIG = BASE + 58; |
Irfan Sheriff | e744cff | 2011-12-11 09:17:50 -0800 | [diff] [blame] | 357 | /* Get configured networks*/ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 358 | static final int CMD_GET_CONFIGURED_NETWORKS = BASE + 59; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 359 | |
| 360 | /* Supplicant commands after driver start*/ |
| 361 | /* Initiate a scan */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 362 | static final int CMD_START_SCAN = BASE + 71; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 363 | /* Set operational mode. CONNECT, SCAN ONLY, SCAN_ONLY with Wi-Fi off mode */ |
| 364 | static final int CMD_SET_OPERATIONAL_MODE = BASE + 72; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 365 | /* Disconnect from a network */ |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 366 | static final int CMD_DISCONNECT = BASE + 73; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 367 | /* Reconnect to a network */ |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 368 | static final int CMD_RECONNECT = BASE + 74; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 369 | /* Reassociate to a network */ |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 370 | static final int CMD_REASSOCIATE = BASE + 75; |
Irfan Sheriff | e2639d7 | 2012-04-27 12:20:00 -0700 | [diff] [blame] | 371 | /* Controls suspend mode optimizations |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 372 | * |
Irfan Sheriff | e2639d7 | 2012-04-27 12:20:00 -0700 | [diff] [blame] | 373 | * When high perf mode is enabled, suspend mode optimizations are disabled |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 374 | * |
Irfan Sheriff | e2639d7 | 2012-04-27 12:20:00 -0700 | [diff] [blame] | 375 | * When high perf mode is disabled, suspend mode optimizations are enabled |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 376 | * |
| 377 | * Suspend mode optimizations include: |
| 378 | * - packet filtering |
| 379 | * - turn off roaming |
| 380 | * - DTIM wake up settings |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 381 | */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 382 | static final int CMD_SET_HIGH_PERF_MODE = BASE + 77; |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 383 | /* Set the country code */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 384 | static final int CMD_SET_COUNTRY_CODE = BASE + 80; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 385 | /* Enables RSSI poll */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 386 | static final int CMD_ENABLE_RSSI_POLL = BASE + 82; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 387 | /* RSSI poll */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 388 | static final int CMD_RSSI_POLL = BASE + 83; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 389 | /* Set up packet filtering */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 390 | static final int CMD_START_PACKET_FILTERING = BASE + 84; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 391 | /* Clear packet filter */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 392 | static final int CMD_STOP_PACKET_FILTERING = BASE + 85; |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 393 | /* Enable suspend mode optimizations in the driver */ |
| 394 | static final int CMD_SET_SUSPEND_OPT_ENABLED = BASE + 86; |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 395 | /* When there are no saved networks, we do a periodic scan to notify user of |
| 396 | * an open network */ |
| 397 | static final int CMD_NO_NETWORKS_PERIODIC_SCAN = BASE + 88; |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 398 | |
| 399 | /* arg1 values to CMD_STOP_PACKET_FILTERING and CMD_START_PACKET_FILTERING */ |
| 400 | static final int MULTICAST_V6 = 1; |
| 401 | static final int MULTICAST_V4 = 0; |
| 402 | |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 403 | /* Set the frequency band */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 404 | static final int CMD_SET_FREQUENCY_BAND = BASE + 90; |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 405 | /* Enable background scan for configured networks */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 406 | static final int CMD_ENABLE_BACKGROUND_SCAN = BASE + 91; |
Vinit Deshapnde | 64f0d5b | 2013-07-11 15:12:44 -0700 | [diff] [blame] | 407 | /* Enable TDLS on a specific MAC address */ |
| 408 | static final int CMD_ENABLE_TDLS = BASE + 92; |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 409 | |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 410 | /* Commands from/to the SupplicantStateTracker */ |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 411 | /* Reset the supplicant state tracker */ |
Irfan Sheriff | 3fc75e9 | 2011-04-28 10:47:12 -0700 | [diff] [blame] | 412 | static final int CMD_RESET_SUPPLICANT_STATE = BASE + 111; |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 413 | |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 414 | /* P2p commands */ |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 415 | /* We are ok with no response here since we wont do much with it anyway */ |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 416 | public static final int CMD_ENABLE_P2P = BASE + 131; |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 417 | /* In order to shut down supplicant cleanly, we wait till p2p has |
| 418 | * been disabled */ |
| 419 | public static final int CMD_DISABLE_P2P_REQ = BASE + 132; |
| 420 | public static final int CMD_DISABLE_P2P_RSP = BASE + 133; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 421 | |
Robert Greenwalt | 304aad4 | 2013-05-31 17:57:10 -0700 | [diff] [blame] | 422 | public static final int CMD_BOOT_COMPLETED = BASE + 134; |
| 423 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 424 | /* change the batch scan settings. |
| 425 | * arg1 = responsible UID |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 426 | * arg2 = csph (channel scans per hour) |
| 427 | * obj = bundle with the new settings and the optional worksource |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 428 | */ |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 429 | public static final int CMD_SET_BATCHED_SCAN = BASE + 135; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 430 | public static final int CMD_START_NEXT_BATCHED_SCAN = BASE + 136; |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 431 | public static final int CMD_POLL_BATCHED_SCAN = BASE + 137; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 432 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 433 | /* Link configuration (IP address, DNS, ...) changes */ |
| 434 | /* An new IP address was added to our interface, or an existing IP address was updated */ |
| 435 | static final int CMD_IP_ADDRESS_UPDATED = BASE + 140; |
| 436 | /* An IP address was removed from our interface */ |
| 437 | static final int CMD_IP_ADDRESS_REMOVED = BASE + 141; |
Vinit Deshapnde | a3038b2 | 2013-10-02 17:26:05 -0700 | [diff] [blame] | 438 | /* Reload all networks and reconnect */ |
| 439 | static final int CMD_RELOAD_TLS_AND_RECONNECT = BASE + 142; |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 440 | |
Vinit Deshapnde | 6029611 | 2013-09-24 17:38:09 -0700 | [diff] [blame] | 441 | /* Wifi state machine modes of operation */ |
| 442 | /* CONNECT_MODE - connect to any 'known' AP when it becomes available */ |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 443 | public static final int CONNECT_MODE = 1; |
Vinit Deshapnde | 6029611 | 2013-09-24 17:38:09 -0700 | [diff] [blame] | 444 | /* SCAN_ONLY_MODE - don't connect to any APs; scan, but only while apps hold lock */ |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 445 | public static final int SCAN_ONLY_MODE = 2; |
Vinit Deshapnde | 6029611 | 2013-09-24 17:38:09 -0700 | [diff] [blame] | 446 | /* SCAN_ONLY_WITH_WIFI_OFF - scan, but don't connect to any APs */ |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 447 | public static final int SCAN_ONLY_WITH_WIFI_OFF_MODE = 3; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 448 | |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 449 | private static final int SUCCESS = 1; |
| 450 | private static final int FAILURE = -1; |
| 451 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 452 | /** |
| 453 | * The maximum number of times we will retry a connection to an access point |
| 454 | * for which we have failed in acquiring an IP address from DHCP. A value of |
| 455 | * N means that we will make N+1 connection attempts in all. |
| 456 | * <p> |
| 457 | * See {@link Settings.Secure#WIFI_MAX_DHCP_RETRY_COUNT}. This is the default |
| 458 | * value if a Settings value is not present. |
| 459 | */ |
| 460 | private static final int DEFAULT_MAX_DHCP_RETRIES = 9; |
| 461 | |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 462 | /* Tracks if suspend optimizations need to be disabled by DHCP, |
| 463 | * screen or due to high perf mode. |
| 464 | * When any of them needs to disable it, we keep the suspend optimizations |
| 465 | * disabled |
| 466 | */ |
| 467 | private int mSuspendOptNeedsDisabled = 0; |
| 468 | |
| 469 | private static final int SUSPEND_DUE_TO_DHCP = 1; |
| 470 | private static final int SUSPEND_DUE_TO_HIGH_PERF = 1<<1; |
| 471 | private static final int SUSPEND_DUE_TO_SCREEN = 1<<2; |
| 472 | |
| 473 | /* Tracks if user has enabled suspend optimizations through settings */ |
| 474 | private AtomicBoolean mUserWantsSuspendOpt = new AtomicBoolean(true); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 475 | |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 476 | /** |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 477 | * Default framework scan interval in milliseconds. This is used in the scenario in which |
| 478 | * wifi chipset does not support background scanning to set up a |
| 479 | * periodic wake up scan so that the device can connect to a new access |
Christopher Tate | 6f5a9a9 | 2012-09-14 17:24:28 -0700 | [diff] [blame] | 480 | * point on the move. {@link Settings.Global#WIFI_FRAMEWORK_SCAN_INTERVAL_MS} can |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 481 | * override this. |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 482 | */ |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 483 | private final int mDefaultFrameworkScanIntervalMs; |
| 484 | |
| 485 | /** |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 486 | * Supplicant scan interval in milliseconds. |
Christopher Tate | 6f5a9a9 | 2012-09-14 17:24:28 -0700 | [diff] [blame] | 487 | * Comes from {@link Settings.Global#WIFI_SUPPLICANT_SCAN_INTERVAL_MS} or |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 488 | * from the default config if the setting is not set |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 489 | */ |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 490 | private long mSupplicantScanIntervalMs; |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 491 | |
Irfan Sheriff | 0f43161 | 2011-10-05 12:40:00 -0700 | [diff] [blame] | 492 | /** |
| 493 | * Minimum time interval between enabling all networks. |
| 494 | * A device can end up repeatedly connecting to a bad network on screen on/off toggle |
| 495 | * due to enabling every time. We add a threshold to avoid this. |
| 496 | */ |
| 497 | private static final int MIN_INTERVAL_ENABLE_ALL_NETWORKS_MS = 10 * 60 * 1000; /* 10 minutes */ |
| 498 | private long mLastEnableAllNetworksTime; |
| 499 | |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 500 | /** |
| 501 | * Starting and shutting down driver too quick causes problems leading to driver |
| 502 | * being in a bad state. Delay driver stop. |
| 503 | */ |
Mike Lockwood | 09a12bc | 2012-02-04 12:05:31 -0500 | [diff] [blame] | 504 | private final int mDriverStopDelayMs; |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 505 | private int mDelayedStopCounter; |
| 506 | private boolean mInDelayedStop = false; |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 507 | |
Robert Greenwalt | 304aad4 | 2013-05-31 17:57:10 -0700 | [diff] [blame] | 508 | // sometimes telephony gives us this data before boot is complete and we can't store it |
| 509 | // until after, so the write is deferred |
| 510 | private volatile String mPersistedCountryCode; |
| 511 | |
Vinit Deshapnde | da40d92 | 2013-10-21 11:58:40 -0700 | [diff] [blame] | 512 | // Supplicant doesn't like setting the same country code multiple times (it may drop |
| 513 | // currently connected network), so we save the country code here to avoid redundency |
| 514 | private String mLastSetCountryCode; |
| 515 | |
Irfan Sheriff | 9b3710b | 2011-03-01 12:38:20 -0800 | [diff] [blame] | 516 | private static final int MIN_RSSI = -200; |
| 517 | private static final int MAX_RSSI = 256; |
| 518 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 519 | /* Default parent state */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 520 | private State mDefaultState = new DefaultState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 521 | /* Temporary initial state */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 522 | private State mInitialState = new InitialState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 523 | /* Driver loaded, waiting for supplicant to start */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 524 | private State mSupplicantStartingState = new SupplicantStartingState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 525 | /* Driver loaded and supplicant ready */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 526 | private State mSupplicantStartedState = new SupplicantStartedState(); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 527 | /* Waiting for supplicant to stop and monitor to exit */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 528 | private State mSupplicantStoppingState = new SupplicantStoppingState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 529 | /* Driver start issued, waiting for completed event */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 530 | private State mDriverStartingState = new DriverStartingState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 531 | /* Driver started */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 532 | private State mDriverStartedState = new DriverStartedState(); |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 533 | /* Wait until p2p is disabled |
| 534 | * This is a special state which is entered right after we exit out of DriverStartedState |
| 535 | * before transitioning to another state. |
| 536 | */ |
| 537 | private State mWaitForP2pDisableState = new WaitForP2pDisableState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 538 | /* Driver stopping */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 539 | private State mDriverStoppingState = new DriverStoppingState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 540 | /* Driver stopped */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 541 | private State mDriverStoppedState = new DriverStoppedState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 542 | /* Scan for networks, no connection will be established */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 543 | private State mScanModeState = new ScanModeState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 544 | /* Connecting to an access point */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 545 | private State mConnectModeState = new ConnectModeState(); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 546 | /* Connected at 802.11 (L2) level */ |
| 547 | private State mL2ConnectedState = new L2ConnectedState(); |
| 548 | /* fetching IP after connection to access point (assoc+auth complete) */ |
| 549 | private State mObtainingIpState = new ObtainingIpState(); |
| 550 | /* Waiting for link quality verification to be complete */ |
| 551 | private State mVerifyingLinkState = new VerifyingLinkState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 552 | /* Connected with IP addr */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 553 | private State mConnectedState = new ConnectedState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 554 | /* disconnect issued, waiting for network disconnect confirmation */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 555 | private State mDisconnectingState = new DisconnectingState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 556 | /* Network is not connected, supplicant assoc+auth is not complete */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 557 | private State mDisconnectedState = new DisconnectedState(); |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 558 | /* Waiting for WPS to be completed*/ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 559 | private State mWpsRunningState = new WpsRunningState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 560 | |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 561 | /* Soft ap is starting up */ |
| 562 | private State mSoftApStartingState = new SoftApStartingState(); |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 563 | /* Soft ap is running */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 564 | private State mSoftApStartedState = new SoftApStartedState(); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 565 | /* Soft ap is running and we are waiting for tether notification */ |
| 566 | private State mTetheringState = new TetheringState(); |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 567 | /* Soft ap is running and we are tethered through connectivity service */ |
| 568 | private State mTetheredState = new TetheredState(); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 569 | /* Waiting for untether confirmation before stopping soft Ap */ |
| 570 | private State mUntetheringState = new UntetheringState(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 571 | |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 572 | private class TetherStateChange { |
| 573 | ArrayList<String> available; |
| 574 | ArrayList<String> active; |
| 575 | TetherStateChange(ArrayList<String> av, ArrayList<String> ac) { |
| 576 | available = av; |
| 577 | active = ac; |
| 578 | } |
| 579 | } |
| 580 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 581 | |
| 582 | /** |
| 583 | * One of {@link WifiManager#WIFI_STATE_DISABLED}, |
| 584 | * {@link WifiManager#WIFI_STATE_DISABLING}, |
| 585 | * {@link WifiManager#WIFI_STATE_ENABLED}, |
| 586 | * {@link WifiManager#WIFI_STATE_ENABLING}, |
| 587 | * {@link WifiManager#WIFI_STATE_UNKNOWN} |
| 588 | * |
| 589 | */ |
| 590 | private final AtomicInteger mWifiState = new AtomicInteger(WIFI_STATE_DISABLED); |
| 591 | |
| 592 | /** |
| 593 | * One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, |
| 594 | * {@link WifiManager#WIFI_AP_STATE_DISABLING}, |
| 595 | * {@link WifiManager#WIFI_AP_STATE_ENABLED}, |
| 596 | * {@link WifiManager#WIFI_AP_STATE_ENABLING}, |
| 597 | * {@link WifiManager#WIFI_AP_STATE_FAILED} |
| 598 | * |
| 599 | */ |
| 600 | private final AtomicInteger mWifiApState = new AtomicInteger(WIFI_AP_STATE_DISABLED); |
| 601 | |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 602 | private static final int SCAN_REQUEST = 0; |
| 603 | private static final String ACTION_START_SCAN = |
| 604 | "com.android.server.WifiManager.action.START_SCAN"; |
| 605 | |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 606 | private static final String DELAYED_STOP_COUNTER = "DelayedStopCounter"; |
| 607 | private static final int DRIVER_STOP_REQUEST = 0; |
| 608 | private static final String ACTION_DELAYED_DRIVER_STOP = |
| 609 | "com.android.server.WifiManager.action.DELAYED_DRIVER_STOP"; |
| 610 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 611 | private static final String ACTION_REFRESH_BATCHED_SCAN = |
| 612 | "com.android.server.WifiManager.action.REFRESH_BATCHED_SCAN"; |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 613 | /** |
| 614 | * Keep track of whether WIFI is running. |
| 615 | */ |
| 616 | private boolean mIsRunning = false; |
Irfan Sheriff | 5ee8980 | 2010-09-16 17:53:34 -0700 | [diff] [blame] | 617 | |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 618 | /** |
| 619 | * Keep track of whether we last told the battery stats we had started. |
| 620 | */ |
| 621 | private boolean mReportedRunning = false; |
| 622 | |
| 623 | /** |
| 624 | * Most recently set source of starting WIFI. |
| 625 | */ |
| 626 | private final WorkSource mRunningWifiUids = new WorkSource(); |
| 627 | |
| 628 | /** |
| 629 | * The last reported UIDs that were responsible for starting WIFI. |
| 630 | */ |
| 631 | private final WorkSource mLastRunningWifiUids = new WorkSource(); |
| 632 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 633 | private final IBatteryStats mBatteryStats; |
| 634 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 635 | private BatchedScanSettings mBatchedScanSettings = null; |
| 636 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 637 | /** |
| 638 | * Track the worksource/cost of the current settings and track what's been noted |
| 639 | * to the battery stats, so we can mark the end of the previous when changing. |
| 640 | */ |
| 641 | private WorkSource mBatchedScanWorkSource = null; |
| 642 | private int mBatchedScanCsph = 0; |
| 643 | private WorkSource mNotedBatchedScanWorkSource = null; |
| 644 | private int mNotedBatchedScanCsph = 0; |
| 645 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 646 | |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 647 | public WifiStateMachine(Context context, String wlanInterface) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 648 | super("WifiStateMachine"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 649 | mContext = context; |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 650 | mInterfaceName = wlanInterface; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 651 | |
| 652 | mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, NETWORKTYPE, ""); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 653 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 654 | BatteryStats.SERVICE_NAME)); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 655 | |
| 656 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 657 | mNwService = INetworkManagementService.Stub.asInterface(b); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 658 | |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 659 | mP2pSupported = mContext.getPackageManager().hasSystemFeature( |
| 660 | PackageManager.FEATURE_WIFI_DIRECT); |
| 661 | |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 662 | mWifiNative = new WifiNative(mInterfaceName); |
| 663 | mWifiConfigStore = new WifiConfigStore(context, mWifiNative); |
| 664 | mWifiMonitor = new WifiMonitor(this, mWifiNative); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 665 | mWifiInfo = new WifiInfo(); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 666 | mSupplicantStateTracker = new SupplicantStateTracker(context, this, mWifiConfigStore, |
| 667 | getHandler()); |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 668 | mLinkProperties = new LinkProperties(); |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 669 | mNetlinkLinkProperties = new LinkProperties(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 670 | |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 671 | mWifiP2pManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE); |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 672 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 673 | mNetworkInfo.setIsAvailable(false); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 674 | mLastBssid = null; |
Irfan Sheriff | 0a8bd60 | 2011-05-20 16:06:19 -0700 | [diff] [blame] | 675 | mLastNetworkId = WifiConfiguration.INVALID_NETWORK_ID; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 676 | mLastSignalLevel = -1; |
| 677 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 678 | mInterfaceObserver = new InterfaceObserver(this); |
| 679 | try { |
| 680 | mNwService.registerObserver(mInterfaceObserver); |
| 681 | } catch (RemoteException e) { |
| 682 | loge("Couldn't register interface observer: " + e.toString()); |
| 683 | } |
| 684 | |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 685 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 686 | Intent scanIntent = new Intent(ACTION_START_SCAN, null); |
| 687 | mScanIntent = PendingIntent.getBroadcast(mContext, SCAN_REQUEST, scanIntent, 0); |
| 688 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 689 | Intent batchedIntent = new Intent(ACTION_REFRESH_BATCHED_SCAN, null); |
| 690 | mBatchedScanIntervalIntent = PendingIntent.getBroadcast(mContext, 0, batchedIntent, 0); |
| 691 | |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 692 | mDefaultFrameworkScanIntervalMs = mContext.getResources().getInteger( |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 693 | R.integer.config_wifi_framework_scan_interval); |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 694 | |
Mike Lockwood | 09a12bc | 2012-02-04 12:05:31 -0500 | [diff] [blame] | 695 | mDriverStopDelayMs = mContext.getResources().getInteger( |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 696 | R.integer.config_wifi_driver_stop_delay); |
Mike Lockwood | 09a12bc | 2012-02-04 12:05:31 -0500 | [diff] [blame] | 697 | |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 698 | mBackgroundScanSupported = mContext.getResources().getBoolean( |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 699 | R.bool.config_wifi_background_scan_support); |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 700 | |
Irfan Sheriff | 4dd5a25 | 2012-05-22 12:53:47 -0700 | [diff] [blame] | 701 | mPrimaryDeviceType = mContext.getResources().getString( |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 702 | R.string.config_wifi_p2p_device_type); |
Irfan Sheriff | 4dd5a25 | 2012-05-22 12:53:47 -0700 | [diff] [blame] | 703 | |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 704 | mUserWantsSuspendOpt.set(Settings.Global.getInt(mContext.getContentResolver(), |
| 705 | Settings.Global.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED, 1) == 1); |
Irfan Sheriff | 3fb4ba6 | 2012-08-09 15:07:45 -0700 | [diff] [blame] | 706 | |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 707 | mContext.registerReceiver( |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 708 | new BroadcastReceiver() { |
| 709 | @Override |
| 710 | public void onReceive(Context context, Intent intent) { |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 711 | ArrayList<String> available = intent.getStringArrayListExtra( |
| 712 | ConnectivityManager.EXTRA_AVAILABLE_TETHER); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 713 | ArrayList<String> active = intent.getStringArrayListExtra( |
| 714 | ConnectivityManager.EXTRA_ACTIVE_TETHER); |
| 715 | sendMessage(CMD_TETHER_STATE_CHANGE, new TetherStateChange(available, active)); |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 716 | } |
| 717 | },new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)); |
| 718 | |
| 719 | mContext.registerReceiver( |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 720 | new BroadcastReceiver() { |
| 721 | @Override |
| 722 | public void onReceive(Context context, Intent intent) { |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 723 | final WorkSource workSource = null; |
| 724 | startScan(UNKNOWN_SCAN_SOURCE, workSource); |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 725 | } |
| 726 | }, |
| 727 | new IntentFilter(ACTION_START_SCAN)); |
| 728 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 729 | IntentFilter filter = new IntentFilter(); |
| 730 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 731 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 732 | filter.addAction(ACTION_REFRESH_BATCHED_SCAN); |
| 733 | mContext.registerReceiver( |
| 734 | new BroadcastReceiver() { |
| 735 | @Override |
| 736 | public void onReceive(Context context, Intent intent) { |
| 737 | String action = intent.getAction(); |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 738 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 739 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 740 | handleScreenStateChanged(true); |
| 741 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 742 | handleScreenStateChanged(false); |
| 743 | } else if (action.equals(ACTION_REFRESH_BATCHED_SCAN)) { |
| 744 | startNextBatchedScanAsync(); |
| 745 | } |
| 746 | } |
| 747 | }, filter); |
Irfan Sheriff | 3fb4ba6 | 2012-08-09 15:07:45 -0700 | [diff] [blame] | 748 | |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 749 | mContext.registerReceiver( |
| 750 | new BroadcastReceiver() { |
| 751 | @Override |
| 752 | public void onReceive(Context context, Intent intent) { |
| 753 | int counter = intent.getIntExtra(DELAYED_STOP_COUNTER, 0); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 754 | sendMessage(CMD_DELAYED_STOP_DRIVER, counter, 0); |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 755 | } |
| 756 | }, |
| 757 | new IntentFilter(ACTION_DELAYED_DRIVER_STOP)); |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 758 | |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 759 | mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor( |
| 760 | Settings.Global.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED), false, |
Irfan Sheriff | 3fb4ba6 | 2012-08-09 15:07:45 -0700 | [diff] [blame] | 761 | new ContentObserver(getHandler()) { |
| 762 | @Override |
| 763 | public void onChange(boolean selfChange) { |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 764 | mUserWantsSuspendOpt.set(Settings.Global.getInt(mContext.getContentResolver(), |
| 765 | Settings.Global.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED, 1) == 1); |
Irfan Sheriff | 3fb4ba6 | 2012-08-09 15:07:45 -0700 | [diff] [blame] | 766 | } |
| 767 | }); |
| 768 | |
Robert Greenwalt | 304aad4 | 2013-05-31 17:57:10 -0700 | [diff] [blame] | 769 | mContext.registerReceiver( |
| 770 | new BroadcastReceiver() { |
| 771 | @Override |
| 772 | public void onReceive(Context context, Intent intent) { |
| 773 | sendMessage(CMD_BOOT_COMPLETED); |
| 774 | } |
| 775 | }, |
| 776 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED)); |
| 777 | |
Jesse Wilson | 34ee52e | 2011-02-10 11:52:17 -0800 | [diff] [blame] | 778 | mScanResultCache = new LruCache<String, ScanResult>(SCAN_RESULT_CACHE_SIZE); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 779 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 780 | PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 781 | mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getName()); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 782 | |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 783 | mSuspendWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WifiSuspend"); |
| 784 | mSuspendWakeLock.setReferenceCounted(false); |
| 785 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 786 | addState(mDefaultState); |
| 787 | addState(mInitialState, mDefaultState); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 788 | addState(mSupplicantStartingState, mDefaultState); |
| 789 | addState(mSupplicantStartedState, mDefaultState); |
| 790 | addState(mDriverStartingState, mSupplicantStartedState); |
| 791 | addState(mDriverStartedState, mSupplicantStartedState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 792 | addState(mScanModeState, mDriverStartedState); |
| 793 | addState(mConnectModeState, mDriverStartedState); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 794 | addState(mL2ConnectedState, mConnectModeState); |
| 795 | addState(mObtainingIpState, mL2ConnectedState); |
| 796 | addState(mVerifyingLinkState, mL2ConnectedState); |
| 797 | addState(mConnectedState, mL2ConnectedState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 798 | addState(mDisconnectingState, mConnectModeState); |
| 799 | addState(mDisconnectedState, mConnectModeState); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 800 | addState(mWpsRunningState, mConnectModeState); |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 801 | addState(mWaitForP2pDisableState, mSupplicantStartedState); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 802 | addState(mDriverStoppingState, mSupplicantStartedState); |
| 803 | addState(mDriverStoppedState, mSupplicantStartedState); |
| 804 | addState(mSupplicantStoppingState, mDefaultState); |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 805 | addState(mSoftApStartingState, mDefaultState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 806 | addState(mSoftApStartedState, mDefaultState); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 807 | addState(mTetheringState, mSoftApStartedState); |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 808 | addState(mTetheredState, mSoftApStartedState); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 809 | addState(mUntetheringState, mSoftApStartedState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 810 | |
| 811 | setInitialState(mInitialState); |
| 812 | |
Vinit Deshapnde | 48c4747 | 2013-08-28 13:13:40 -0700 | [diff] [blame] | 813 | setLogRecSize(2000); |
Robert Greenwalt | 02ba86f | 2013-05-18 11:24:22 -0700 | [diff] [blame] | 814 | setLogOnlyTransitions(false); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 815 | if (DBG) setDbg(true); |
| 816 | |
| 817 | //start the state machine |
| 818 | start(); |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 819 | |
| 820 | final Intent intent = new Intent(WifiManager.WIFI_SCAN_AVAILABLE); |
| 821 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 822 | intent.putExtra(WifiManager.EXTRA_SCAN_AVAILABLE, WIFI_STATE_DISABLED); |
| 823 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /********************************************************* |
| 827 | * Methods exposed for public use |
| 828 | ********************************************************/ |
| 829 | |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 830 | public Messenger getMessenger() { |
| 831 | return new Messenger(getHandler()); |
| 832 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 833 | /** |
| 834 | * TODO: doc |
| 835 | */ |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 836 | public boolean syncPingSupplicant(AsyncChannel channel) { |
| 837 | Message resultMsg = channel.sendMessageSynchronously(CMD_PING_SUPPLICANT); |
| 838 | boolean result = (resultMsg.arg1 != FAILURE); |
| 839 | resultMsg.recycle(); |
| 840 | return result; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /** |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 844 | * Initiate a wifi scan. If workSource is not null, blame is given to it, |
| 845 | * otherwise blame is given to callingUid. |
| 846 | * |
| 847 | * @param callingUid The uid initiating the wifi scan. Blame will be given |
| 848 | * here unless workSource is specified. |
| 849 | * @param workSource If not null, blame is given to workSource. |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 850 | */ |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 851 | public void startScan(int callingUid, WorkSource workSource) { |
| 852 | sendMessage(CMD_START_SCAN, callingUid, 0, workSource); |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 855 | /** |
| 856 | * start or stop batched scanning using the given settings |
| 857 | */ |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 858 | private static final String BATCHED_SETTING = "batched_settings"; |
| 859 | private static final String BATCHED_WORKSOURCE = "batched_worksource"; |
| 860 | public void setBatchedScanSettings(BatchedScanSettings settings, int callingUid, int csph, |
| 861 | WorkSource workSource) { |
| 862 | Bundle bundle = new Bundle(); |
| 863 | bundle.putParcelable(BATCHED_SETTING, settings); |
| 864 | bundle.putParcelable(BATCHED_WORKSOURCE, workSource); |
| 865 | sendMessage(CMD_SET_BATCHED_SCAN, callingUid, csph, bundle); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | public List<BatchedScanResult> syncGetBatchedScanResultsList() { |
| 869 | synchronized (mBatchedScanResults) { |
| 870 | List<BatchedScanResult> batchedScanList = |
| 871 | new ArrayList<BatchedScanResult>(mBatchedScanResults.size()); |
| 872 | for(BatchedScanResult result: mBatchedScanResults) { |
| 873 | batchedScanList.add(new BatchedScanResult(result)); |
| 874 | } |
| 875 | return batchedScanList; |
| 876 | } |
| 877 | } |
| 878 | |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 879 | public void requestBatchedScanPoll() { |
| 880 | sendMessage(CMD_POLL_BATCHED_SCAN); |
| 881 | } |
| 882 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 883 | private void startBatchedScan() { |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 884 | if (mBatchedScanSettings == null) return; |
| 885 | |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 886 | if (mDhcpActive) { |
| 887 | if (DBG) log("not starting Batched Scans due to DHCP"); |
| 888 | return; |
| 889 | } |
| 890 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 891 | // first grab any existing data |
| 892 | retrieveBatchedScanData(); |
| 893 | |
| 894 | mAlarmManager.cancel(mBatchedScanIntervalIntent); |
| 895 | |
| 896 | String scansExpected = mWifiNative.setBatchedScanSettings(mBatchedScanSettings); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 897 | try { |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 898 | mExpectedBatchedScans = Integer.parseInt(scansExpected); |
| 899 | setNextBatchedAlarm(mExpectedBatchedScans); |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 900 | if (mExpectedBatchedScans > 0) noteBatchedScanStart(); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 901 | } catch (NumberFormatException e) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 902 | stopBatchedScan(); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 903 | loge("Exception parsing WifiNative.setBatchedScanSettings response " + e); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | // called from BroadcastListener |
| 908 | private void startNextBatchedScanAsync() { |
| 909 | sendMessage(CMD_START_NEXT_BATCHED_SCAN); |
| 910 | } |
| 911 | |
| 912 | private void startNextBatchedScan() { |
| 913 | // first grab any existing data |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 914 | retrieveBatchedScanData(); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 915 | |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 916 | setNextBatchedAlarm(mExpectedBatchedScans); |
| 917 | } |
| 918 | |
| 919 | private void handleBatchedScanPollRequest() { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 920 | if (DBG) { |
| 921 | log("handleBatchedScanPoll Request - mBatchedScanMinPollTime=" + |
| 922 | mBatchedScanMinPollTime + " , mBatchedScanSettings=" + |
| 923 | mBatchedScanSettings); |
| 924 | } |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 925 | // if there is no appropriate PollTime that's because we either aren't |
| 926 | // batching or we've already set a time for a poll request |
| 927 | if (mBatchedScanMinPollTime == 0) return; |
| 928 | if (mBatchedScanSettings == null) return; |
| 929 | |
| 930 | long now = System.currentTimeMillis(); |
| 931 | |
| 932 | if (now > mBatchedScanMinPollTime) { |
| 933 | // do the poll and reset our timers |
| 934 | startNextBatchedScan(); |
| 935 | } else { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 936 | mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, mBatchedScanMinPollTime, |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 937 | mBatchedScanIntervalIntent); |
| 938 | mBatchedScanMinPollTime = 0; |
| 939 | } |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | // return true if new/different |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 943 | private boolean recordBatchedScanSettings(int responsibleUid, int csph, Bundle bundle) { |
| 944 | BatchedScanSettings settings = bundle.getParcelable(BATCHED_SETTING); |
| 945 | WorkSource responsibleWorkSource = bundle.getParcelable(BATCHED_WORKSOURCE); |
| 946 | |
| 947 | if (DBG) { |
| 948 | log("set batched scan to " + settings + " for uid=" + responsibleUid + |
| 949 | ", worksource=" + responsibleWorkSource); |
| 950 | } |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 951 | if (settings != null) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 952 | if (settings.equals(mBatchedScanSettings)) return false; |
| 953 | } else { |
| 954 | if (mBatchedScanSettings == null) return false; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 955 | } |
| 956 | mBatchedScanSettings = settings; |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 957 | if (responsibleWorkSource == null) responsibleWorkSource = new WorkSource(responsibleUid); |
| 958 | mBatchedScanWorkSource = responsibleWorkSource; |
| 959 | mBatchedScanCsph = csph; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 960 | return true; |
| 961 | } |
| 962 | |
| 963 | private void stopBatchedScan() { |
| 964 | mAlarmManager.cancel(mBatchedScanIntervalIntent); |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 965 | retrieveBatchedScanData(); |
| 966 | mWifiNative.setBatchedScanSettings(null); |
| 967 | noteBatchedScanStop(); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | private void setNextBatchedAlarm(int scansExpected) { |
| 971 | |
| 972 | if (mBatchedScanSettings == null || scansExpected < 1) return; |
| 973 | |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 974 | mBatchedScanMinPollTime = System.currentTimeMillis() + |
| 975 | mBatchedScanSettings.scanIntervalSec * 1000; |
| 976 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 977 | if (mBatchedScanSettings.maxScansPerBatch < scansExpected) { |
| 978 | scansExpected = mBatchedScanSettings.maxScansPerBatch; |
| 979 | } |
| 980 | |
| 981 | int secToFull = mBatchedScanSettings.scanIntervalSec; |
| 982 | secToFull *= scansExpected; |
| 983 | |
| 984 | int debugPeriod = SystemProperties.getInt("wifi.batchedScan.pollPeriod", 0); |
| 985 | if (debugPeriod > 0) secToFull = debugPeriod; |
| 986 | |
| 987 | // set the alarm to do the next poll. We set it a little short as we'd rather |
| 988 | // wake up wearly than miss a scan due to buffer overflow |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 989 | mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 990 | + ((secToFull - (mBatchedScanSettings.scanIntervalSec / 2)) * 1000), |
| 991 | mBatchedScanIntervalIntent); |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * Start reading new scan data |
| 996 | * Data comes in as: |
| 997 | * "scancount=5\n" |
| 998 | * "nextcount=5\n" |
| 999 | * "apcount=3\n" |
| 1000 | * "trunc\n" (optional) |
| 1001 | * "bssid=...\n" |
| 1002 | * "ssid=...\n" |
| 1003 | * "freq=...\n" (in Mhz) |
| 1004 | * "level=...\n" |
| 1005 | * "dist=...\n" (in cm) |
| 1006 | * "distsd=...\n" (standard deviation, in cm) |
| 1007 | * "====" |
| 1008 | * "bssid=...\n" |
| 1009 | * etc |
| 1010 | * "====" |
| 1011 | * "bssid=...\n" |
| 1012 | * etc |
| 1013 | * "%%%%" |
| 1014 | * "apcount=2\n" |
| 1015 | * "bssid=...\n" |
| 1016 | * etc |
| 1017 | * "%%%% |
| 1018 | * etc |
| 1019 | * "----" |
| 1020 | */ |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1021 | private final static boolean DEBUG_PARSE = false; |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1022 | private void retrieveBatchedScanData() { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1023 | String rawData = mWifiNative.getBatchedScanResults(); |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1024 | if (DEBUG_PARSE) log("rawData = " + rawData); |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1025 | mBatchedScanMinPollTime = 0; |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1026 | if (rawData == null || rawData.equalsIgnoreCase("OK")) { |
| 1027 | loge("Unexpected BatchedScanResults :" + rawData); |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1028 | return; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1031 | int scanCount = 0; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1032 | final String END_OF_BATCHES = "----"; |
| 1033 | final String SCANCOUNT = "scancount="; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1034 | final String TRUNCATED = "trunc"; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1035 | final String AGE = "age="; |
| 1036 | final String DIST = "dist="; |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1037 | final String DISTSD = "distSd="; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1038 | |
| 1039 | String splitData[] = rawData.split("\n"); |
| 1040 | int n = 0; |
| 1041 | if (splitData[n].startsWith(SCANCOUNT)) { |
| 1042 | try { |
| 1043 | scanCount = Integer.parseInt(splitData[n++].substring(SCANCOUNT.length())); |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1044 | } catch (NumberFormatException e) { |
| 1045 | loge("scancount parseInt Exception from " + splitData[n]); |
| 1046 | } |
| 1047 | } else log("scancount not found"); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1048 | if (scanCount == 0) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1049 | loge("scanCount==0 - aborting"); |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1050 | return; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | final Intent intent = new Intent(WifiManager.BATCHED_SCAN_RESULTS_AVAILABLE_ACTION); |
| 1054 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1055 | |
| 1056 | synchronized (mBatchedScanResults) { |
| 1057 | mBatchedScanResults.clear(); |
| 1058 | BatchedScanResult batchedScanResult = new BatchedScanResult(); |
| 1059 | |
| 1060 | String bssid = null; |
| 1061 | WifiSsid wifiSsid = null; |
| 1062 | int level = 0; |
| 1063 | int freq = 0; |
| 1064 | int dist, distSd; |
| 1065 | long tsf = 0; |
| 1066 | dist = distSd = ScanResult.UNSPECIFIED; |
Robert Greenwalt | c95d938 | 2013-10-11 12:38:07 -0700 | [diff] [blame] | 1067 | final long now = SystemClock.elapsedRealtime(); |
| 1068 | final int bssidStrLen = BSSID_STR.length(); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1069 | |
| 1070 | while (true) { |
| 1071 | while (n < splitData.length) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1072 | if (DEBUG_PARSE) logd("parsing " + splitData[n]); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1073 | if (splitData[n].equals(END_OF_BATCHES)) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1074 | if (n+1 != splitData.length) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1075 | loge("didn't consume " + (splitData.length-n)); |
| 1076 | } |
| 1077 | if (mBatchedScanResults.size() > 0) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1078 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1079 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1080 | logd("retrieveBatchedScanResults X"); |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1081 | return; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1082 | } |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1083 | if ((splitData[n].equals(END_STR)) || splitData[n].equals(DELIMITER_STR)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1084 | if (bssid != null) { |
| 1085 | batchedScanResult.scanResults.add(new ScanResult( |
| 1086 | wifiSsid, bssid, "", level, freq, tsf, dist, distSd)); |
| 1087 | wifiSsid = null; |
| 1088 | bssid = null; |
| 1089 | level = 0; |
| 1090 | freq = 0; |
| 1091 | tsf = 0; |
| 1092 | dist = distSd = ScanResult.UNSPECIFIED; |
| 1093 | } |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1094 | if (splitData[n].equals(END_STR)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1095 | if (batchedScanResult.scanResults.size() != 0) { |
| 1096 | mBatchedScanResults.add(batchedScanResult); |
| 1097 | batchedScanResult = new BatchedScanResult(); |
| 1098 | } else { |
| 1099 | logd("Found empty batch"); |
| 1100 | } |
| 1101 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1102 | } else if (splitData[n].equals(TRUNCATED)) { |
| 1103 | batchedScanResult.truncated = true; |
| 1104 | } else if (splitData[n].startsWith(BSSID_STR)) { |
Robert Greenwalt | c95d938 | 2013-10-11 12:38:07 -0700 | [diff] [blame] | 1105 | bssid = new String(splitData[n].getBytes(), bssidStrLen, |
| 1106 | splitData[n].length() - bssidStrLen); |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1107 | } else if (splitData[n].startsWith(FREQ_STR)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1108 | try { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1109 | freq = Integer.parseInt(splitData[n].substring(FREQ_STR.length())); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1110 | } catch (NumberFormatException e) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1111 | loge("Invalid freqency: " + splitData[n]); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1112 | freq = 0; |
| 1113 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1114 | } else if (splitData[n].startsWith(AGE)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1115 | try { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1116 | tsf = now - Long.parseLong(splitData[n].substring(AGE.length())); |
Robert Greenwalt | dee971b | 2013-09-11 15:21:10 -0700 | [diff] [blame] | 1117 | tsf *= 1000; // convert mS -> uS |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1118 | } catch (NumberFormatException e) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1119 | loge("Invalid timestamp: " + splitData[n]); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1120 | tsf = 0; |
| 1121 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1122 | } else if (splitData[n].startsWith(SSID_STR)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1123 | wifiSsid = WifiSsid.createFromAsciiEncoded( |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1124 | splitData[n].substring(SSID_STR.length())); |
| 1125 | } else if (splitData[n].startsWith(LEVEL_STR)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1126 | try { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1127 | level = Integer.parseInt(splitData[n].substring(LEVEL_STR.length())); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1128 | if (level > 0) level -= 256; |
| 1129 | } catch (NumberFormatException e) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1130 | loge("Invalid level: " + splitData[n]); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1131 | level = 0; |
| 1132 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1133 | } else if (splitData[n].startsWith(DIST)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1134 | try { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1135 | dist = Integer.parseInt(splitData[n].substring(DIST.length())); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1136 | } catch (NumberFormatException e) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1137 | loge("Invalid distance: " + splitData[n]); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1138 | dist = ScanResult.UNSPECIFIED; |
| 1139 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1140 | } else if (splitData[n].startsWith(DISTSD)) { |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1141 | try { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1142 | distSd = Integer.parseInt(splitData[n].substring(DISTSD.length())); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1143 | } catch (NumberFormatException e) { |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1144 | loge("Invalid distanceSd: " + splitData[n]); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1145 | distSd = ScanResult.UNSPECIFIED; |
| 1146 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1147 | } else { |
| 1148 | loge("Unable to parse batched scan result line: " + splitData[n]); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1149 | } |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1150 | n++; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1151 | } |
| 1152 | rawData = mWifiNative.getBatchedScanResults(); |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 1153 | if (DEBUG_PARSE) log("reading more data:\n" + rawData); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1154 | if (rawData == null) { |
| 1155 | loge("Unexpected null BatchedScanResults"); |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1156 | return; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1157 | } |
| 1158 | splitData = rawData.split("\n"); |
| 1159 | if (splitData.length == 0 || splitData[0].equals("ok")) { |
| 1160 | loge("batch scan results just ended!"); |
| 1161 | if (mBatchedScanResults.size() > 0) { |
| 1162 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1163 | } |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 1164 | return; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 1165 | } |
| 1166 | n = 0; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 1171 | // If workSource is not null, blame is given to it, otherwise blame is given to callingUid. |
| 1172 | private void noteScanStart(int callingUid, WorkSource workSource) { |
| 1173 | if (mScanWorkSource == null && (callingUid != UNKNOWN_SCAN_SOURCE || workSource != null)) { |
| 1174 | mScanWorkSource = workSource != null ? workSource : new WorkSource(callingUid); |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 1175 | try { |
| 1176 | mBatteryStats.noteWifiScanStartedFromSource(mScanWorkSource); |
| 1177 | } catch (RemoteException e) { |
| 1178 | log(e.toString()); |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | private void noteScanEnd() { |
| 1184 | if (mScanWorkSource != null) { |
| 1185 | try { |
| 1186 | mBatteryStats.noteWifiScanStoppedFromSource(mScanWorkSource); |
| 1187 | } catch (RemoteException e) { |
| 1188 | log(e.toString()); |
| 1189 | } finally { |
| 1190 | mScanWorkSource = null; |
| 1191 | } |
| 1192 | } |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 1195 | private void noteBatchedScanStart() { |
| 1196 | // note the end of a previous scan set |
| 1197 | if (mNotedBatchedScanWorkSource != null && |
| 1198 | (mNotedBatchedScanWorkSource.equals(mBatchedScanWorkSource) == false || |
| 1199 | mNotedBatchedScanCsph != mBatchedScanCsph)) { |
| 1200 | try { |
| 1201 | mBatteryStats.noteWifiBatchedScanStoppedFromSource(mNotedBatchedScanWorkSource); |
| 1202 | } catch (RemoteException e) { |
| 1203 | log(e.toString()); |
| 1204 | } finally { |
| 1205 | mNotedBatchedScanWorkSource = null; |
| 1206 | mNotedBatchedScanCsph = 0; |
| 1207 | } |
| 1208 | } |
| 1209 | // note the start of the new |
| 1210 | try { |
| 1211 | mBatteryStats.noteWifiBatchedScanStartedFromSource(mBatchedScanWorkSource, |
| 1212 | mBatchedScanCsph); |
| 1213 | mNotedBatchedScanWorkSource = mBatchedScanWorkSource; |
| 1214 | mNotedBatchedScanCsph = mBatchedScanCsph; |
| 1215 | } catch (RemoteException e) { |
| 1216 | log(e.toString()); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | private void noteBatchedScanStop() { |
| 1221 | if (mNotedBatchedScanWorkSource != null) { |
| 1222 | try { |
| 1223 | mBatteryStats.noteWifiBatchedScanStoppedFromSource(mNotedBatchedScanWorkSource); |
| 1224 | } catch (RemoteException e) { |
| 1225 | log(e.toString()); |
| 1226 | } finally { |
| 1227 | mNotedBatchedScanWorkSource = null; |
| 1228 | mNotedBatchedScanCsph = 0; |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 1233 | private void startScanNative(int type) { |
| 1234 | mWifiNative.scan(type); |
| 1235 | mScanResultIsPending = true; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /** |
| 1239 | * TODO: doc |
| 1240 | */ |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1241 | public void setSupplicantRunning(boolean enable) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1242 | if (enable) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1243 | sendMessage(CMD_START_SUPPLICANT); |
| 1244 | } else { |
| 1245 | sendMessage(CMD_STOP_SUPPLICANT); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * TODO: doc |
| 1251 | */ |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1252 | public void setHostApRunning(WifiConfiguration wifiConfig, boolean enable) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1253 | if (enable) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1254 | sendMessage(CMD_START_AP, wifiConfig); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1255 | } else { |
| 1256 | sendMessage(CMD_STOP_AP); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 1260 | public void setWifiApConfiguration(WifiConfiguration config) { |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 1261 | mWifiApConfigChannel.sendMessage(CMD_SET_AP_CONFIG, config); |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 1264 | public WifiConfiguration syncGetWifiApConfiguration() { |
| 1265 | Message resultMsg = mWifiApConfigChannel.sendMessageSynchronously(CMD_REQUEST_AP_CONFIG); |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 1266 | WifiConfiguration ret = (WifiConfiguration) resultMsg.obj; |
| 1267 | resultMsg.recycle(); |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1271 | /** |
| 1272 | * TODO: doc |
| 1273 | */ |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1274 | public int syncGetWifiState() { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1275 | return mWifiState.get(); |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * TODO: doc |
| 1280 | */ |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1281 | public String syncGetWifiStateByName() { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1282 | switch (mWifiState.get()) { |
| 1283 | case WIFI_STATE_DISABLING: |
| 1284 | return "disabling"; |
| 1285 | case WIFI_STATE_DISABLED: |
| 1286 | return "disabled"; |
| 1287 | case WIFI_STATE_ENABLING: |
| 1288 | return "enabling"; |
| 1289 | case WIFI_STATE_ENABLED: |
| 1290 | return "enabled"; |
| 1291 | case WIFI_STATE_UNKNOWN: |
| 1292 | return "unknown state"; |
| 1293 | default: |
| 1294 | return "[invalid state]"; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | /** |
| 1299 | * TODO: doc |
| 1300 | */ |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1301 | public int syncGetWifiApState() { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1302 | return mWifiApState.get(); |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * TODO: doc |
| 1307 | */ |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1308 | public String syncGetWifiApStateByName() { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1309 | switch (mWifiApState.get()) { |
| 1310 | case WIFI_AP_STATE_DISABLING: |
| 1311 | return "disabling"; |
| 1312 | case WIFI_AP_STATE_DISABLED: |
| 1313 | return "disabled"; |
| 1314 | case WIFI_AP_STATE_ENABLING: |
| 1315 | return "enabling"; |
| 1316 | case WIFI_AP_STATE_ENABLED: |
| 1317 | return "enabled"; |
| 1318 | case WIFI_AP_STATE_FAILED: |
| 1319 | return "failed"; |
| 1320 | default: |
| 1321 | return "[invalid state]"; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * Get status information for the current connection, if any. |
| 1327 | * @return a {@link WifiInfo} object containing information about the current connection |
| 1328 | * |
| 1329 | */ |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1330 | public WifiInfo syncRequestConnectionInfo() { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1331 | return mWifiInfo; |
| 1332 | } |
| 1333 | |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 1334 | public DhcpResults syncGetDhcpResults() { |
| 1335 | synchronized (mDhcpResultsLock) { |
| 1336 | return new DhcpResults(mDhcpResults); |
Irfan Sheriff | 31b6232 | 2010-08-30 12:26:00 -0700 | [diff] [blame] | 1337 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * TODO: doc |
| 1342 | */ |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1343 | public void setDriverStart(boolean enable) { |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 1344 | if (enable) { |
| 1345 | sendMessage(CMD_START_DRIVER); |
| 1346 | } else { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1347 | sendMessage(CMD_STOP_DRIVER); |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 1348 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * TODO: doc |
| 1353 | */ |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1354 | public void setOperationalMode(int mode) { |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 1355 | if (DBG) log("setting operational mode to " + String.valueOf(mode)); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1356 | sendMessage(CMD_SET_OPERATIONAL_MODE, mode, 0); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * TODO: doc |
| 1361 | */ |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1362 | public List<ScanResult> syncGetScanResultsList() { |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1363 | synchronized (mScanResultCache) { |
| 1364 | List<ScanResult> scanList = new ArrayList<ScanResult>(); |
| 1365 | for(ScanResult result: mScanResults) { |
| 1366 | scanList.add(new ScanResult(result)); |
| 1367 | } |
| 1368 | return scanList; |
| 1369 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * Disconnect from Access Point |
| 1374 | */ |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 1375 | public void disconnectCommand() { |
| 1376 | sendMessage(CMD_DISCONNECT); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Initiate a reconnection to AP |
| 1381 | */ |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 1382 | public void reconnectCommand() { |
| 1383 | sendMessage(CMD_RECONNECT); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /** |
| 1387 | * Initiate a re-association to AP |
| 1388 | */ |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 1389 | public void reassociateCommand() { |
| 1390 | sendMessage(CMD_REASSOCIATE); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /** |
Vinit Deshapnde | a3038b2 | 2013-10-02 17:26:05 -0700 | [diff] [blame] | 1394 | * Reload networks and then reconnect; helps load correct data for TLS networks |
| 1395 | */ |
| 1396 | |
| 1397 | public void reloadTlsNetworksAndReconnect() { |
| 1398 | sendMessage(CMD_RELOAD_TLS_AND_RECONNECT); |
| 1399 | } |
| 1400 | |
| 1401 | /** |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1402 | * Add a network synchronously |
| 1403 | * |
| 1404 | * @return network id of the new network |
| 1405 | */ |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 1406 | public int syncAddOrUpdateNetwork(AsyncChannel channel, WifiConfiguration config) { |
| 1407 | Message resultMsg = channel.sendMessageSynchronously(CMD_ADD_OR_UPDATE_NETWORK, config); |
| 1408 | int result = resultMsg.arg1; |
| 1409 | resultMsg.recycle(); |
| 1410 | return result; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Irfan Sheriff | e744cff | 2011-12-11 09:17:50 -0800 | [diff] [blame] | 1413 | public List<WifiConfiguration> syncGetConfiguredNetworks(AsyncChannel channel) { |
| 1414 | Message resultMsg = channel.sendMessageSynchronously(CMD_GET_CONFIGURED_NETWORKS); |
| 1415 | List<WifiConfiguration> result = (List<WifiConfiguration>) resultMsg.obj; |
| 1416 | resultMsg.recycle(); |
| 1417 | return result; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * Delete a network |
| 1422 | * |
| 1423 | * @param networkId id of the network to be removed |
| 1424 | */ |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 1425 | public boolean syncRemoveNetwork(AsyncChannel channel, int networkId) { |
| 1426 | Message resultMsg = channel.sendMessageSynchronously(CMD_REMOVE_NETWORK, networkId); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 1427 | boolean result = (resultMsg.arg1 != FAILURE); |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 1428 | resultMsg.recycle(); |
| 1429 | return result; |
| 1430 | } |
| 1431 | |
| 1432 | /** |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1433 | * Enable a network |
| 1434 | * |
| 1435 | * @param netId network id of the network |
| 1436 | * @param disableOthers true, if all other networks have to be disabled |
| 1437 | * @return {@code true} if the operation succeeds, {@code false} otherwise |
| 1438 | */ |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 1439 | public boolean syncEnableNetwork(AsyncChannel channel, int netId, boolean disableOthers) { |
| 1440 | Message resultMsg = channel.sendMessageSynchronously(CMD_ENABLE_NETWORK, netId, |
| 1441 | disableOthers ? 1 : 0); |
| 1442 | boolean result = (resultMsg.arg1 != FAILURE); |
| 1443 | resultMsg.recycle(); |
| 1444 | return result; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * Disable a network |
| 1449 | * |
| 1450 | * @param netId network id of the network |
| 1451 | * @return {@code true} if the operation succeeds, {@code false} otherwise |
| 1452 | */ |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 1453 | public boolean syncDisableNetwork(AsyncChannel channel, int netId) { |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 1454 | Message resultMsg = channel.sendMessageSynchronously(WifiManager.DISABLE_NETWORK, netId); |
| 1455 | boolean result = (resultMsg.arg1 != WifiManager.DISABLE_NETWORK_FAILED); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 1456 | resultMsg.recycle(); |
| 1457 | return result; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /** |
| 1461 | * Blacklist a BSSID. This will avoid the AP if there are |
| 1462 | * alternate APs to connect |
| 1463 | * |
| 1464 | * @param bssid BSSID of the network |
| 1465 | */ |
| 1466 | public void addToBlacklist(String bssid) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1467 | sendMessage(CMD_BLACKLIST_NETWORK, bssid); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * Clear the blacklist list |
| 1472 | * |
| 1473 | */ |
| 1474 | public void clearBlacklist() { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1475 | sendMessage(CMD_CLEAR_BLACKLIST); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1478 | public void enableRssiPolling(boolean enabled) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1479 | sendMessage(CMD_ENABLE_RSSI_POLL, enabled ? 1 : 0, 0); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1480 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1481 | |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 1482 | public void enableBackgroundScanCommand(boolean enabled) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1483 | sendMessage(CMD_ENABLE_BACKGROUND_SCAN, enabled ? 1 : 0, 0); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 1484 | } |
| 1485 | |
Irfan Sheriff | 8e86b89 | 2010-12-22 11:02:20 -0800 | [diff] [blame] | 1486 | public void enableAllNetworks() { |
| 1487 | sendMessage(CMD_ENABLE_ALL_NETWORKS); |
| 1488 | } |
| 1489 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1490 | /** |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1491 | * Start filtering Multicast v4 packets |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1492 | */ |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1493 | public void startFilteringMulticastV4Packets() { |
| 1494 | mFilteringMulticastV4Packets.set(true); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1495 | sendMessage(CMD_START_PACKET_FILTERING, MULTICAST_V4, 0); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | /** |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1499 | * Stop filtering Multicast v4 packets |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1500 | */ |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1501 | public void stopFilteringMulticastV4Packets() { |
| 1502 | mFilteringMulticastV4Packets.set(false); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1503 | sendMessage(CMD_STOP_PACKET_FILTERING, MULTICAST_V4, 0); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /** |
| 1507 | * Start filtering Multicast v4 packets |
| 1508 | */ |
| 1509 | public void startFilteringMulticastV6Packets() { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1510 | sendMessage(CMD_START_PACKET_FILTERING, MULTICAST_V6, 0); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * Stop filtering Multicast v4 packets |
| 1515 | */ |
| 1516 | public void stopFilteringMulticastV6Packets() { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1517 | sendMessage(CMD_STOP_PACKET_FILTERING, MULTICAST_V6, 0); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /** |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1521 | * Set high performance mode of operation. |
| 1522 | * Enabling would set active power mode and disable suspend optimizations; |
| 1523 | * disabling would set auto power mode and enable suspend optimizations |
| 1524 | * @param enable true if enable, false otherwise |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1525 | */ |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1526 | public void setHighPerfModeEnabled(boolean enable) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1527 | sendMessage(CMD_SET_HIGH_PERF_MODE, enable ? 1 : 0, 0); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /** |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 1531 | * Set the country code |
| 1532 | * @param countryCode following ISO 3166 format |
| 1533 | * @param persist {@code true} if the setting should be remembered. |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1534 | */ |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 1535 | public void setCountryCode(String countryCode, boolean persist) { |
| 1536 | if (persist) { |
Robert Greenwalt | 304aad4 | 2013-05-31 17:57:10 -0700 | [diff] [blame] | 1537 | mPersistedCountryCode = countryCode; |
Robert Greenwalt | 3ea0c99 | 2013-10-03 21:13:49 +0000 | [diff] [blame] | 1538 | Settings.Global.putString(mContext.getContentResolver(), |
| 1539 | Settings.Global.WIFI_COUNTRY_CODE, |
| 1540 | countryCode); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1541 | } |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1542 | sendMessage(CMD_SET_COUNTRY_CODE, countryCode); |
Vinit Deshapnde | 2c385ec | 2013-09-09 16:28:31 -0700 | [diff] [blame] | 1543 | mWifiP2pChannel.sendMessage(WifiP2pService.SET_COUNTRY_CODE, countryCode); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /** |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 1547 | * Set the operational frequency band |
| 1548 | * @param band |
| 1549 | * @param persist {@code true} if the setting should be remembered. |
| 1550 | */ |
| 1551 | public void setFrequencyBand(int band, boolean persist) { |
| 1552 | if (persist) { |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 1553 | Settings.Global.putInt(mContext.getContentResolver(), |
| 1554 | Settings.Global.WIFI_FREQUENCY_BAND, |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 1555 | band); |
| 1556 | } |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1557 | sendMessage(CMD_SET_FREQUENCY_BAND, band, 0); |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /** |
Vinit Deshapnde | 64f0d5b | 2013-07-11 15:12:44 -0700 | [diff] [blame] | 1561 | * Enable TDLS for a specific MAC address |
| 1562 | */ |
| 1563 | public void enableTdls(String remoteMacAddress, boolean enable) { |
| 1564 | int enabler = enable ? 1 : 0; |
| 1565 | sendMessage(CMD_ENABLE_TDLS, enabler, 0, remoteMacAddress); |
| 1566 | } |
| 1567 | |
| 1568 | /** |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 1569 | * Returns the operational frequency band |
| 1570 | */ |
| 1571 | public int getFrequencyBand() { |
| 1572 | return mFrequencyBand.get(); |
| 1573 | } |
| 1574 | |
| 1575 | /** |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 1576 | * Returns the wifi configuration file |
| 1577 | */ |
| 1578 | public String getConfigFile() { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 1579 | return mWifiConfigStore.getConfigFile(); |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /** |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 1583 | * Send a message indicating bluetooth adapter connection state changed |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1584 | */ |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 1585 | public void sendBluetoothAdapterStateChange(int state) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1586 | sendMessage(CMD_BLUETOOTH_ADAPTER_STATE_CHANGE, state, 0); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /** |
| 1590 | * Save configuration on supplicant |
| 1591 | * |
| 1592 | * @return {@code true} if the operation succeeds, {@code false} otherwise |
| 1593 | * |
| 1594 | * TODO: deprecate this |
| 1595 | */ |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 1596 | public boolean syncSaveConfig(AsyncChannel channel) { |
| 1597 | Message resultMsg = channel.sendMessageSynchronously(CMD_SAVE_CONFIG); |
| 1598 | boolean result = (resultMsg.arg1 != FAILURE); |
| 1599 | resultMsg.recycle(); |
| 1600 | return result; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1603 | public void updateBatteryWorkSource(WorkSource newSource) { |
| 1604 | synchronized (mRunningWifiUids) { |
| 1605 | try { |
| 1606 | if (newSource != null) { |
| 1607 | mRunningWifiUids.set(newSource); |
| 1608 | } |
| 1609 | if (mIsRunning) { |
| 1610 | if (mReportedRunning) { |
| 1611 | // If the work source has changed since last time, need |
| 1612 | // to remove old work from battery stats. |
| 1613 | if (mLastRunningWifiUids.diff(mRunningWifiUids)) { |
| 1614 | mBatteryStats.noteWifiRunningChanged(mLastRunningWifiUids, |
| 1615 | mRunningWifiUids); |
| 1616 | mLastRunningWifiUids.set(mRunningWifiUids); |
| 1617 | } |
| 1618 | } else { |
| 1619 | // Now being started, report it. |
| 1620 | mBatteryStats.noteWifiRunning(mRunningWifiUids); |
| 1621 | mLastRunningWifiUids.set(mRunningWifiUids); |
| 1622 | mReportedRunning = true; |
| 1623 | } |
| 1624 | } else { |
| 1625 | if (mReportedRunning) { |
| 1626 | // Last reported we were running, time to stop. |
| 1627 | mBatteryStats.noteWifiStopped(mLastRunningWifiUids); |
| 1628 | mLastRunningWifiUids.clear(); |
| 1629 | mReportedRunning = false; |
| 1630 | } |
| 1631 | } |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 1632 | mWakeLock.setWorkSource(newSource); |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1633 | } catch (RemoteException ignore) { |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | |
Irfan Sheriff | bbc85ab | 2010-08-26 12:59:58 -0700 | [diff] [blame] | 1638 | @Override |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 1639 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1640 | super.dump(fd, pw, args); |
| 1641 | mSupplicantStateTracker.dump(fd, pw, args); |
| 1642 | pw.println("mLinkProperties " + mLinkProperties); |
| 1643 | pw.println("mWifiInfo " + mWifiInfo); |
| 1644 | pw.println("mDhcpResults " + mDhcpResults); |
| 1645 | pw.println("mNetworkInfo " + mNetworkInfo); |
| 1646 | pw.println("mLastSignalLevel " + mLastSignalLevel); |
| 1647 | pw.println("mLastBssid " + mLastBssid); |
| 1648 | pw.println("mLastNetworkId " + mLastNetworkId); |
| 1649 | pw.println("mReconnectCount " + mReconnectCount); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1650 | pw.println("mOperationalMode " + mOperationalMode); |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 1651 | pw.println("mUserWantsSuspendOpt " + mUserWantsSuspendOpt); |
| 1652 | pw.println("mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled); |
| 1653 | pw.println("Supplicant status " + mWifiNative.status()); |
Irfan Sheriff | cca308e | 2013-04-25 23:22:41 -0700 | [diff] [blame] | 1654 | pw.println("mEnableBackgroundScan " + mEnableBackgroundScan); |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 1655 | pw.println(); |
| 1656 | mWifiConfigStore.dump(fd, pw, args); |
Irfan Sheriff | bbc85ab | 2010-08-26 12:59:58 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1659 | /********************************************************* |
| 1660 | * Internal private functions |
| 1661 | ********************************************************/ |
| 1662 | |
Irfan Sheriff | 1c045ec | 2012-09-16 14:55:26 -0700 | [diff] [blame] | 1663 | private void handleScreenStateChanged(boolean screenOn) { |
| 1664 | if (DBG) log("handleScreenStateChanged: " + screenOn); |
| 1665 | enableRssiPolling(screenOn); |
| 1666 | if (mBackgroundScanSupported) { |
| 1667 | enableBackgroundScanCommand(screenOn == false); |
| 1668 | } |
| 1669 | |
| 1670 | if (screenOn) enableAllNetworks(); |
| 1671 | if (mUserWantsSuspendOpt.get()) { |
| 1672 | if (screenOn) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1673 | sendMessage(CMD_SET_SUSPEND_OPT_ENABLED, 0, 0); |
Irfan Sheriff | 1c045ec | 2012-09-16 14:55:26 -0700 | [diff] [blame] | 1674 | } else { |
| 1675 | //Allow 2s for suspend optimizations to be set |
| 1676 | mSuspendWakeLock.acquire(2000); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 1677 | sendMessage(CMD_SET_SUSPEND_OPT_ENABLED, 1, 0); |
Irfan Sheriff | 1c045ec | 2012-09-16 14:55:26 -0700 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | mScreenBroadcastReceived.set(true); |
| 1681 | } |
| 1682 | |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1683 | private void checkAndSetConnectivityInstance() { |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1684 | if (mCm == null) { |
| 1685 | mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 1686 | } |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Irfan Sheriff | e8daf2a | 2011-07-24 14:28:15 -0700 | [diff] [blame] | 1689 | private boolean startTethering(ArrayList<String> available) { |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1690 | |
| 1691 | boolean wifiAvailable = false; |
| 1692 | |
| 1693 | checkAndSetConnectivityInstance(); |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1694 | |
| 1695 | String[] wifiRegexs = mCm.getTetherableWifiRegexs(); |
| 1696 | |
| 1697 | for (String intf : available) { |
| 1698 | for (String regex : wifiRegexs) { |
| 1699 | if (intf.matches(regex)) { |
| 1700 | |
| 1701 | InterfaceConfiguration ifcg = null; |
| 1702 | try { |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 1703 | ifcg = mNwService.getInterfaceConfig(intf); |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1704 | if (ifcg != null) { |
| 1705 | /* IP/netmask: 192.168.43.1/255.255.255.0 */ |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 1706 | ifcg.setLinkAddress(new LinkAddress( |
| 1707 | NetworkUtils.numericToInetAddress("192.168.43.1"), 24)); |
| 1708 | ifcg.setInterfaceUp(); |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1709 | |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 1710 | mNwService.setInterfaceConfig(intf, ifcg); |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1711 | } |
| 1712 | } catch (Exception e) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 1713 | loge("Error configuring interface " + intf + ", :" + e); |
Irfan Sheriff | e8daf2a | 2011-07-24 14:28:15 -0700 | [diff] [blame] | 1714 | return false; |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 1718 | loge("Error tethering on " + intf); |
Irfan Sheriff | e8daf2a | 2011-07-24 14:28:15 -0700 | [diff] [blame] | 1719 | return false; |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1720 | } |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 1721 | mTetherInterfaceName = intf; |
Irfan Sheriff | e8daf2a | 2011-07-24 14:28:15 -0700 | [diff] [blame] | 1722 | return true; |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | } |
Irfan Sheriff | e8daf2a | 2011-07-24 14:28:15 -0700 | [diff] [blame] | 1726 | // We found no interfaces to tether |
| 1727 | return false; |
Irfan Sheriff | 43e747e | 2011-01-20 07:12:20 -0800 | [diff] [blame] | 1728 | } |
| 1729 | |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1730 | private void stopTethering() { |
| 1731 | |
| 1732 | checkAndSetConnectivityInstance(); |
| 1733 | |
| 1734 | /* Clear the interface config to allow dhcp correctly configure new |
| 1735 | ip settings */ |
| 1736 | InterfaceConfiguration ifcg = null; |
| 1737 | try { |
Vishal Mahaveer | a6b6556 | 2012-03-27 13:06:50 -0500 | [diff] [blame] | 1738 | ifcg = mNwService.getInterfaceConfig(mTetherInterfaceName); |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1739 | if (ifcg != null) { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 1740 | ifcg.setLinkAddress( |
| 1741 | new LinkAddress(NetworkUtils.numericToInetAddress("0.0.0.0"), 0)); |
Vishal Mahaveer | a6b6556 | 2012-03-27 13:06:50 -0500 | [diff] [blame] | 1742 | mNwService.setInterfaceConfig(mTetherInterfaceName, ifcg); |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1743 | } |
| 1744 | } catch (Exception e) { |
Vishal Mahaveer | a6b6556 | 2012-03-27 13:06:50 -0500 | [diff] [blame] | 1745 | loge("Error resetting interface " + mTetherInterfaceName + ", :" + e); |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 1748 | if (mCm.untether(mTetherInterfaceName) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 1749 | loge("Untether initiate failed!"); |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 1753 | private boolean isWifiTethered(ArrayList<String> active) { |
| 1754 | |
| 1755 | checkAndSetConnectivityInstance(); |
| 1756 | |
| 1757 | String[] wifiRegexs = mCm.getTetherableWifiRegexs(); |
| 1758 | for (String intf : active) { |
| 1759 | for (String regex : wifiRegexs) { |
| 1760 | if (intf.matches(regex)) { |
| 1761 | return true; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | // We found no interfaces that are tethered |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 1769 | /** |
| 1770 | * Set the country code from the system setting value, if any. |
| 1771 | */ |
| 1772 | private void setCountryCode() { |
Robert Greenwalt | 3ea0c99 | 2013-10-03 21:13:49 +0000 | [diff] [blame] | 1773 | String countryCode = Settings.Global.getString(mContext.getContentResolver(), |
| 1774 | Settings.Global.WIFI_COUNTRY_CODE); |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 1775 | if (countryCode != null && !countryCode.isEmpty()) { |
| 1776 | setCountryCode(countryCode, false); |
| 1777 | } else { |
| 1778 | //use driver default |
| 1779 | } |
| 1780 | } |
| 1781 | |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 1782 | /** |
| 1783 | * Set the frequency band from the system setting value, if any. |
| 1784 | */ |
| 1785 | private void setFrequencyBand() { |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 1786 | int band = Settings.Global.getInt(mContext.getContentResolver(), |
| 1787 | Settings.Global.WIFI_FREQUENCY_BAND, WifiManager.WIFI_FREQUENCY_BAND_AUTO); |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 1788 | setFrequencyBand(band, false); |
| 1789 | } |
| 1790 | |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 1791 | private void setSuspendOptimizationsNative(int reason, boolean enabled) { |
| 1792 | if (DBG) log("setSuspendOptimizationsNative: " + reason + " " + enabled); |
| 1793 | if (enabled) { |
| 1794 | mSuspendOptNeedsDisabled &= ~reason; |
| 1795 | /* None of dhcp, screen or highperf need it disabled and user wants it enabled */ |
| 1796 | if (mSuspendOptNeedsDisabled == 0 && mUserWantsSuspendOpt.get()) { |
| 1797 | mWifiNative.setSuspendOptimizations(true); |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 1798 | } |
| 1799 | } else { |
| 1800 | mSuspendOptNeedsDisabled |= reason; |
| 1801 | mWifiNative.setSuspendOptimizations(false); |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | private void setSuspendOptimizations(int reason, boolean enabled) { |
| 1806 | if (DBG) log("setSuspendOptimizations: " + reason + " " + enabled); |
| 1807 | if (enabled) { |
| 1808 | mSuspendOptNeedsDisabled &= ~reason; |
| 1809 | } else { |
| 1810 | mSuspendOptNeedsDisabled |= reason; |
| 1811 | } |
| 1812 | if (DBG) log("mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled); |
| 1813 | } |
| 1814 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1815 | private void setWifiState(int wifiState) { |
| 1816 | final int previousWifiState = mWifiState.get(); |
| 1817 | |
| 1818 | try { |
| 1819 | if (wifiState == WIFI_STATE_ENABLED) { |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1820 | mBatteryStats.noteWifiOn(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1821 | } else if (wifiState == WIFI_STATE_DISABLED) { |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1822 | mBatteryStats.noteWifiOff(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } catch (RemoteException e) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 1825 | loge("Failed to note battery stats in wifi"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | mWifiState.set(wifiState); |
| 1829 | |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 1830 | if (DBG) log("setWifiState: " + syncGetWifiStateByName()); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1831 | |
| 1832 | final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 1833 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1834 | intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState); |
| 1835 | intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1836 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | private void setWifiApState(int wifiApState) { |
| 1840 | final int previousWifiApState = mWifiApState.get(); |
| 1841 | |
| 1842 | try { |
| 1843 | if (wifiApState == WIFI_AP_STATE_ENABLED) { |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1844 | mBatteryStats.noteWifiOn(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1845 | } else if (wifiApState == WIFI_AP_STATE_DISABLED) { |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1846 | mBatteryStats.noteWifiOff(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1847 | } |
| 1848 | } catch (RemoteException e) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 1849 | loge("Failed to note battery stats in wifi"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | // Update state |
| 1853 | mWifiApState.set(wifiApState); |
| 1854 | |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 1855 | if (DBG) log("setWifiApState: " + syncGetWifiApStateByName()); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1856 | |
| 1857 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 1858 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1859 | intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, wifiApState); |
| 1860 | intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE, previousWifiApState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1861 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 1864 | private static final String ID_STR = "id="; |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1865 | private static final String BSSID_STR = "bssid="; |
| 1866 | private static final String FREQ_STR = "freq="; |
| 1867 | private static final String LEVEL_STR = "level="; |
| 1868 | private static final String TSF_STR = "tsf="; |
| 1869 | private static final String FLAGS_STR = "flags="; |
| 1870 | private static final String SSID_STR = "ssid="; |
| 1871 | private static final String DELIMITER_STR = "===="; |
Dmitry Shmidt | bca462a | 2013-03-01 09:57:58 -0800 | [diff] [blame] | 1872 | private static final String END_STR = "####"; |
Dmitry Shmidt | 67b3fbf | 2013-02-26 14:47:16 -0800 | [diff] [blame] | 1873 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1874 | /** |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1875 | * Format: |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 1876 | * |
| 1877 | * id=1 |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1878 | * bssid=68:7f:76:d7:1a:6e |
| 1879 | * freq=2412 |
| 1880 | * level=-44 |
| 1881 | * tsf=1344626243700342 |
| 1882 | * flags=[WPA2-PSK-CCMP][WPS][ESS] |
| 1883 | * ssid=zfdy |
| 1884 | * ==== |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 1885 | * id=2 |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1886 | * bssid=68:5f:74:d7:1a:6f |
| 1887 | * freq=5180 |
| 1888 | * level=-73 |
| 1889 | * tsf=1344626243700373 |
| 1890 | * flags=[WPA2-PSK-CCMP][WPS][ESS] |
| 1891 | * ssid=zuby |
| 1892 | * ==== |
Iliyan Malchev | 5785310 | 2012-08-14 11:11:49 -0700 | [diff] [blame] | 1893 | */ |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 1894 | private void setScanResults() { |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1895 | String bssid = ""; |
| 1896 | int level = 0; |
| 1897 | int freq = 0; |
| 1898 | long tsf = 0; |
| 1899 | String flags = ""; |
Irfan Sheriff | b6deeed | 2012-09-05 10:46:24 -0700 | [diff] [blame] | 1900 | WifiSsid wifiSsid = null; |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 1901 | String scanResults; |
| 1902 | String tmpResults; |
| 1903 | StringBuffer scanResultsBuf = new StringBuffer(); |
| 1904 | int sid = 0; |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1905 | |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 1906 | while (true) { |
| 1907 | tmpResults = mWifiNative.scanResults(sid); |
| 1908 | if (TextUtils.isEmpty(tmpResults)) break; |
| 1909 | scanResultsBuf.append(tmpResults); |
| 1910 | scanResultsBuf.append("\n"); |
| 1911 | String[] lines = tmpResults.split("\n"); |
| 1912 | sid = -1; |
| 1913 | for (int i=lines.length - 1; i >= 0; i--) { |
| 1914 | if (lines[i].startsWith(END_STR)) { |
| 1915 | break; |
| 1916 | } else if (lines[i].startsWith(ID_STR)) { |
| 1917 | try { |
| 1918 | sid = Integer.parseInt(lines[i].substring(ID_STR.length())) + 1; |
| 1919 | } catch (NumberFormatException e) { |
| 1920 | // Nothing to do |
| 1921 | } |
| 1922 | break; |
| 1923 | } |
| 1924 | } |
| 1925 | if (sid == -1) break; |
| 1926 | } |
| 1927 | |
| 1928 | scanResults = scanResultsBuf.toString(); |
| 1929 | if (TextUtils.isEmpty(scanResults)) { |
| 1930 | return; |
Iliyan Malchev | 5785310 | 2012-08-14 11:11:49 -0700 | [diff] [blame] | 1931 | } |
| 1932 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 1933 | // note that all these splits and substrings keep references to the original |
| 1934 | // huge string buffer while the amount we really want is generally pretty small |
| 1935 | // so make copies instead (one example b/11087956 wasted 400k of heap here). |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1936 | synchronized(mScanResultCache) { |
| 1937 | mScanResults = new ArrayList<ScanResult>(); |
| 1938 | String[] lines = scanResults.split("\n"); |
Robert Greenwalt | c95d938 | 2013-10-11 12:38:07 -0700 | [diff] [blame] | 1939 | final int bssidStrLen = BSSID_STR.length(); |
| 1940 | final int flagLen = FLAGS_STR.length(); |
Iliyan Malchev | 5785310 | 2012-08-14 11:11:49 -0700 | [diff] [blame] | 1941 | |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1942 | for (String line : lines) { |
| 1943 | if (line.startsWith(BSSID_STR)) { |
Robert Greenwalt | c95d938 | 2013-10-11 12:38:07 -0700 | [diff] [blame] | 1944 | bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen); |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1945 | } else if (line.startsWith(FREQ_STR)) { |
| 1946 | try { |
| 1947 | freq = Integer.parseInt(line.substring(FREQ_STR.length())); |
| 1948 | } catch (NumberFormatException e) { |
| 1949 | freq = 0; |
Iliyan Malchev | 5785310 | 2012-08-14 11:11:49 -0700 | [diff] [blame] | 1950 | } |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1951 | } else if (line.startsWith(LEVEL_STR)) { |
| 1952 | try { |
| 1953 | level = Integer.parseInt(line.substring(LEVEL_STR.length())); |
| 1954 | /* some implementations avoid negative values by adding 256 |
| 1955 | * so we need to adjust for that here. |
| 1956 | */ |
| 1957 | if (level > 0) level -= 256; |
| 1958 | } catch(NumberFormatException e) { |
| 1959 | level = 0; |
| 1960 | } |
| 1961 | } else if (line.startsWith(TSF_STR)) { |
| 1962 | try { |
| 1963 | tsf = Long.parseLong(line.substring(TSF_STR.length())); |
| 1964 | } catch (NumberFormatException e) { |
| 1965 | tsf = 0; |
| 1966 | } |
| 1967 | } else if (line.startsWith(FLAGS_STR)) { |
Robert Greenwalt | c95d938 | 2013-10-11 12:38:07 -0700 | [diff] [blame] | 1968 | flags = new String(line.getBytes(), flagLen, line.length() - flagLen); |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1969 | } else if (line.startsWith(SSID_STR)) { |
Irfan Sheriff | b6deeed | 2012-09-05 10:46:24 -0700 | [diff] [blame] | 1970 | wifiSsid = WifiSsid.createFromAsciiEncoded( |
| 1971 | line.substring(SSID_STR.length())); |
Dmitry Shmidt | bca462a | 2013-03-01 09:57:58 -0800 | [diff] [blame] | 1972 | } else if (line.startsWith(DELIMITER_STR) || line.startsWith(END_STR)) { |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1973 | if (bssid != null) { |
Irfan Sheriff | b6deeed | 2012-09-05 10:46:24 -0700 | [diff] [blame] | 1974 | String ssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE; |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1975 | String key = bssid + ssid; |
| 1976 | ScanResult scanResult = mScanResultCache.get(key); |
| 1977 | if (scanResult != null) { |
| 1978 | scanResult.level = level; |
Irfan Sheriff | b6deeed | 2012-09-05 10:46:24 -0700 | [diff] [blame] | 1979 | scanResult.wifiSsid = wifiSsid; |
| 1980 | // Keep existing API |
| 1981 | scanResult.SSID = (wifiSsid != null) ? wifiSsid.toString() : |
| 1982 | WifiSsid.NONE; |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1983 | scanResult.capabilities = flags; |
| 1984 | scanResult.frequency = freq; |
| 1985 | scanResult.timestamp = tsf; |
| 1986 | } else { |
| 1987 | scanResult = |
| 1988 | new ScanResult( |
Irfan Sheriff | b6deeed | 2012-09-05 10:46:24 -0700 | [diff] [blame] | 1989 | wifiSsid, bssid, flags, level, freq, tsf); |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 1990 | mScanResultCache.put(key, scanResult); |
| 1991 | } |
| 1992 | mScanResults.add(scanResult); |
| 1993 | } |
| 1994 | bssid = null; |
| 1995 | level = 0; |
| 1996 | freq = 0; |
| 1997 | tsf = 0; |
| 1998 | flags = ""; |
Irfan Sheriff | b6deeed | 2012-09-05 10:46:24 -0700 | [diff] [blame] | 1999 | wifiSsid = null; |
Iliyan Malchev | 5785310 | 2012-08-14 11:11:49 -0700 | [diff] [blame] | 2000 | } |
Iliyan Malchev | 5785310 | 2012-08-14 11:11:49 -0700 | [diff] [blame] | 2001 | } |
| 2002 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2005 | /* |
| 2006 | * Fetch RSSI and linkspeed on current connection |
| 2007 | */ |
| 2008 | private void fetchRssiAndLinkSpeedNative() { |
Irfan Sheriff | 921df5c | 2011-09-26 16:30:15 -0700 | [diff] [blame] | 2009 | int newRssi = -1; |
| 2010 | int newLinkSpeed = -1; |
| 2011 | |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2012 | String signalPoll = mWifiNative.signalPoll(); |
Irfan Sheriff | 921df5c | 2011-09-26 16:30:15 -0700 | [diff] [blame] | 2013 | |
| 2014 | if (signalPoll != null) { |
| 2015 | String[] lines = signalPoll.split("\n"); |
| 2016 | for (String line : lines) { |
| 2017 | String[] prop = line.split("="); |
| 2018 | if (prop.length < 2) continue; |
| 2019 | try { |
| 2020 | if (prop[0].equals("RSSI")) { |
| 2021 | newRssi = Integer.parseInt(prop[1]); |
| 2022 | } else if (prop[0].equals("LINKSPEED")) { |
| 2023 | newLinkSpeed = Integer.parseInt(prop[1]); |
| 2024 | } |
| 2025 | } catch (NumberFormatException e) { |
| 2026 | //Ignore, defaults on rssi and linkspeed are assigned |
| 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | |
Irfan Sheriff | 9b3710b | 2011-03-01 12:38:20 -0800 | [diff] [blame] | 2031 | if (newRssi != -1 && MIN_RSSI < newRssi && newRssi < MAX_RSSI) { // screen out invalid values |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2032 | /* some implementations avoid negative values by adding 256 |
| 2033 | * so we need to adjust for that here. |
| 2034 | */ |
| 2035 | if (newRssi > 0) newRssi -= 256; |
| 2036 | mWifiInfo.setRssi(newRssi); |
| 2037 | /* |
| 2038 | * Rather then sending the raw RSSI out every time it |
| 2039 | * changes, we precalculate the signal level that would |
| 2040 | * be displayed in the status bar, and only send the |
| 2041 | * broadcast if that much more coarse-grained number |
| 2042 | * changes. This cuts down greatly on the number of |
Irfan Sheriff | ea933cb | 2011-12-09 10:04:30 -0800 | [diff] [blame] | 2043 | * broadcasts, at the cost of not informing others |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2044 | * interested in RSSI of all the changes in signal |
| 2045 | * level. |
| 2046 | */ |
Irfan Sheriff | ea933cb | 2011-12-09 10:04:30 -0800 | [diff] [blame] | 2047 | int newSignalLevel = WifiManager.calculateSignalLevel(newRssi, WifiManager.RSSI_LEVELS); |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2048 | if (newSignalLevel != mLastSignalLevel) { |
| 2049 | sendRssiChangeBroadcast(newRssi); |
| 2050 | } |
| 2051 | mLastSignalLevel = newSignalLevel; |
| 2052 | } else { |
Irfan Sheriff | 9b3710b | 2011-03-01 12:38:20 -0800 | [diff] [blame] | 2053 | mWifiInfo.setRssi(MIN_RSSI); |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2054 | } |
Irfan Sheriff | 921df5c | 2011-09-26 16:30:15 -0700 | [diff] [blame] | 2055 | |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2056 | if (newLinkSpeed != -1) { |
| 2057 | mWifiInfo.setLinkSpeed(newLinkSpeed); |
| 2058 | } |
| 2059 | } |
| 2060 | |
Yuhao Zheng | b33227d | 2012-07-20 10:55:17 -0700 | [diff] [blame] | 2061 | /* |
| 2062 | * Fetch TX packet counters on current connection |
| 2063 | */ |
Yuhao Zheng | f630782 | 2012-08-14 14:21:25 -0700 | [diff] [blame] | 2064 | private void fetchPktcntNative(RssiPacketCountInfo info) { |
Yuhao Zheng | b33227d | 2012-07-20 10:55:17 -0700 | [diff] [blame] | 2065 | String pktcntPoll = mWifiNative.pktcntPoll(); |
| 2066 | |
| 2067 | if (pktcntPoll != null) { |
| 2068 | String[] lines = pktcntPoll.split("\n"); |
| 2069 | for (String line : lines) { |
| 2070 | String[] prop = line.split("="); |
| 2071 | if (prop.length < 2) continue; |
| 2072 | try { |
| 2073 | if (prop[0].equals("TXGOOD")) { |
Yuhao Zheng | f630782 | 2012-08-14 14:21:25 -0700 | [diff] [blame] | 2074 | info.txgood = Integer.parseInt(prop[1]); |
Yuhao Zheng | b33227d | 2012-07-20 10:55:17 -0700 | [diff] [blame] | 2075 | } else if (prop[0].equals("TXBAD")) { |
Yuhao Zheng | f630782 | 2012-08-14 14:21:25 -0700 | [diff] [blame] | 2076 | info.txbad = Integer.parseInt(prop[1]); |
Yuhao Zheng | b33227d | 2012-07-20 10:55:17 -0700 | [diff] [blame] | 2077 | } |
| 2078 | } catch (NumberFormatException e) { |
| 2079 | //Ignore |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 2085 | /** |
| 2086 | * Updates mLinkProperties by merging information from various sources. |
| 2087 | * |
| 2088 | * This is needed because the information in mLinkProperties comes from multiple sources (DHCP, |
| 2089 | * netlink, static configuration, ...). When one of these sources of information has updated |
| 2090 | * link properties, we can't just assign them to mLinkProperties or we'd lose track of the |
| 2091 | * information that came from other sources. Instead, when one of those sources has new |
| 2092 | * information, we update the object that tracks the information from that source and then |
| 2093 | * call this method to apply the change to mLinkProperties. |
| 2094 | * |
| 2095 | * The information in mLinkProperties is currently obtained as follows: |
| 2096 | * - Interface name: set in the constructor. |
| 2097 | * - IPv4 and IPv6 addresses: netlink, via mInterfaceObserver. |
| 2098 | * - IPv4 routes, DNS servers, and domains: DHCP. |
| 2099 | * - HTTP proxy: the wifi config store. |
| 2100 | */ |
| 2101 | private void updateLinkProperties() { |
| 2102 | LinkProperties newLp = new LinkProperties(); |
| 2103 | |
| 2104 | // Interface name and proxy are locally configured. |
| 2105 | newLp.setInterfaceName(mInterfaceName); |
| 2106 | newLp.setHttpProxy(mWifiConfigStore.getProxyProperties(mLastNetworkId)); |
| 2107 | |
| 2108 | // IPv4 and IPv6 addresses come from netlink. |
| 2109 | newLp.setLinkAddresses(mNetlinkLinkProperties.getLinkAddresses()); |
| 2110 | |
| 2111 | // For now, routing and DNS only come from DHCP or static configuration. In the future, |
| 2112 | // we'll need to merge IPv6 DNS servers and domains coming from netlink. |
| 2113 | synchronized (mDhcpResultsLock) { |
| 2114 | // Even when we're using static configuration, we don't need to look at the config |
| 2115 | // store, because static IP configuration also populates mDhcpResults. |
| 2116 | if ((mDhcpResults != null) && (mDhcpResults.linkProperties != null)) { |
| 2117 | LinkProperties lp = mDhcpResults.linkProperties; |
| 2118 | for (RouteInfo route: lp.getRoutes()) { |
| 2119 | newLp.addRoute(route); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 2120 | } |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 2121 | for (InetAddress dns: lp.getDnses()) { |
| 2122 | newLp.addDns(dns); |
| 2123 | } |
| 2124 | newLp.setDomains(lp.getDomains()); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 2125 | } |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 2126 | } |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 2127 | |
| 2128 | // If anything has changed, and we're already connected, send out a notification. |
| 2129 | // If we're still connecting, apps will be notified when we connect. |
| 2130 | if (!newLp.equals(mLinkProperties)) { |
| 2131 | if (DBG) { |
| 2132 | log("Link configuration changed for netId: " + mLastNetworkId |
| 2133 | + " old: " + mLinkProperties + "new: " + newLp); |
| 2134 | } |
| 2135 | mLinkProperties = newLp; |
| 2136 | if (getNetworkDetailedState() == DetailedState.CONNECTED) { |
| 2137 | sendLinkConfigurationChangedBroadcast(); |
| 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | /** |
| 2143 | * Clears all our link properties. |
| 2144 | */ |
| 2145 | private void clearLinkProperties() { |
| 2146 | // If the network used DHCP, clear the LinkProperties we stored in the config store. |
| 2147 | if (!mWifiConfigStore.isUsingStaticIp(mLastNetworkId)) { |
| 2148 | mWifiConfigStore.clearLinkProperties(mLastNetworkId); |
| 2149 | } |
| 2150 | |
| 2151 | // Clear the link properties obtained from DHCP and netlink. |
| 2152 | synchronized(mDhcpResultsLock) { |
| 2153 | if (mDhcpResults != null && mDhcpResults.linkProperties != null) { |
| 2154 | mDhcpResults.linkProperties.clear(); |
| 2155 | } |
| 2156 | } |
| 2157 | mNetlinkLinkProperties.clear(); |
| 2158 | |
| 2159 | // Now clear the merged link properties. |
| 2160 | mLinkProperties.clear(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2163 | private int getMaxDhcpRetries() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2164 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 2165 | Settings.Global.WIFI_MAX_DHCP_RETRY_COUNT, |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2166 | DEFAULT_MAX_DHCP_RETRIES); |
| 2167 | } |
| 2168 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2169 | private void sendScanResultsAvailableBroadcast() { |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 2170 | noteScanEnd(); |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 2171 | Intent intent = new Intent(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 2172 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2173 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | private void sendRssiChangeBroadcast(final int newRssi) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2177 | Intent intent = new Intent(WifiManager.RSSI_CHANGED_ACTION); |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 2178 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2179 | intent.putExtra(WifiManager.EXTRA_NEW_RSSI, newRssi); |
Irfan Sheriff | 17d9f67 | 2012-09-30 16:25:50 -0700 | [diff] [blame] | 2180 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | private void sendNetworkStateChangeBroadcast(String bssid) { |
| 2184 | Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
Irfan Sheriff | ea22ddd | 2012-04-26 14:13:23 -0700 | [diff] [blame] | 2185 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 2186 | intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, new NetworkInfo(mNetworkInfo)); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2187 | intent.putExtra(WifiManager.EXTRA_LINK_PROPERTIES, new LinkProperties (mLinkProperties)); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2188 | if (bssid != null) |
| 2189 | intent.putExtra(WifiManager.EXTRA_BSSID, bssid); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 2190 | if (mNetworkInfo.getDetailedState() == DetailedState.VERIFYING_POOR_LINK || |
| 2191 | mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) { |
Isaac Levy | 3550ac9 | 2011-07-07 09:17:29 -0700 | [diff] [blame] | 2192 | intent.putExtra(WifiManager.EXTRA_WIFI_INFO, new WifiInfo(mWifiInfo)); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 2193 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2194 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 2197 | private void sendLinkConfigurationChangedBroadcast() { |
| 2198 | Intent intent = new Intent(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION); |
| 2199 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2200 | intent.putExtra(WifiManager.EXTRA_LINK_PROPERTIES, new LinkProperties(mLinkProperties)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2201 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2204 | private void sendSupplicantConnectionChangedBroadcast(boolean connected) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2205 | Intent intent = new Intent(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 2206 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2207 | intent.putExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, connected); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2208 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | /** |
| 2212 | * Record the detailed state of a network. |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 2213 | * @param state the new {@code DetailedState} |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2214 | */ |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 2215 | private void setNetworkDetailedState(NetworkInfo.DetailedState state) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2216 | if (DBG) { |
| 2217 | log("setDetailed state, old =" |
| 2218 | + mNetworkInfo.getDetailedState() + " and new state=" + state); |
| 2219 | } |
| 2220 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2221 | if (state != mNetworkInfo.getDetailedState()) { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2222 | mNetworkInfo.setDetailedState(state, null, mWifiInfo.getSSID()); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 2226 | private DetailedState getNetworkDetailedState() { |
| 2227 | return mNetworkInfo.getDetailedState(); |
| 2228 | } |
| 2229 | |
Irfan Sheriff | 1523da2 | 2011-06-10 14:00:27 -0700 | [diff] [blame] | 2230 | |
| 2231 | private SupplicantState handleSupplicantStateChange(Message message) { |
| 2232 | StateChangeResult stateChangeResult = (StateChangeResult) message.obj; |
| 2233 | SupplicantState state = stateChangeResult.state; |
| 2234 | // Supplicant state change |
| 2235 | // [31-13] Reserved for future use |
| 2236 | // [8 - 0] Supplicant state (as defined in SupplicantState.java) |
| 2237 | // 50023 supplicant_state_changed (custom|1|5) |
Irfan Sheriff | 1523da2 | 2011-06-10 14:00:27 -0700 | [diff] [blame] | 2238 | mWifiInfo.setSupplicantState(state); |
| 2239 | // Network id is only valid when we start connecting |
| 2240 | if (SupplicantState.isConnecting(state)) { |
| 2241 | mWifiInfo.setNetworkId(stateChangeResult.networkId); |
| 2242 | } else { |
| 2243 | mWifiInfo.setNetworkId(WifiConfiguration.INVALID_NETWORK_ID); |
| 2244 | } |
| 2245 | |
Yuhao Zheng | b33227d | 2012-07-20 10:55:17 -0700 | [diff] [blame] | 2246 | mWifiInfo.setBSSID(stateChangeResult.BSSID); |
Irfan Sheriff | b6deeed | 2012-09-05 10:46:24 -0700 | [diff] [blame] | 2247 | mWifiInfo.setSSID(stateChangeResult.wifiSsid); |
Irfan Sheriff | 1523da2 | 2011-06-10 14:00:27 -0700 | [diff] [blame] | 2248 | |
| 2249 | mSupplicantStateTracker.sendMessage(Message.obtain(message)); |
Irfan Sheriff | 1523da2 | 2011-06-10 14:00:27 -0700 | [diff] [blame] | 2250 | |
| 2251 | return state; |
| 2252 | } |
| 2253 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2254 | /** |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2255 | * Resets the Wi-Fi Connections by clearing any state, resetting any sockets |
| 2256 | * using the interface, stopping DHCP & disabling interface |
| 2257 | */ |
| 2258 | private void handleNetworkDisconnect() { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2259 | if (DBG) log("Stopping DHCP and clearing IP"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2260 | |
Irfan Sheriff | 51e4ece | 2013-03-28 08:54:09 -0700 | [diff] [blame] | 2261 | stopDhcp(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2262 | |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 2263 | try { |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 2264 | mNwService.clearInterfaceAddresses(mInterfaceName); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 2265 | mNwService.disableIpv6(mInterfaceName); |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 2266 | } catch (Exception e) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2267 | loge("Failed to clear addresses or disable ipv6" + e); |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2270 | /* Reset data structures */ |
Robert Greenwalt | 124b44d | 2011-02-16 17:31:11 -0800 | [diff] [blame] | 2271 | mWifiInfo.setInetAddress(null); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2272 | mWifiInfo.setBSSID(null); |
| 2273 | mWifiInfo.setSSID(null); |
Irfan Sheriff | 0a8bd60 | 2011-05-20 16:06:19 -0700 | [diff] [blame] | 2274 | mWifiInfo.setNetworkId(WifiConfiguration.INVALID_NETWORK_ID); |
Irfan Sheriff | 9b3710b | 2011-03-01 12:38:20 -0800 | [diff] [blame] | 2275 | mWifiInfo.setRssi(MIN_RSSI); |
| 2276 | mWifiInfo.setLinkSpeed(-1); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2277 | mWifiInfo.setMeteredHint(false); |
Irfan Sheriff | 9b3710b | 2011-03-01 12:38:20 -0800 | [diff] [blame] | 2278 | |
Irfan Sheriff | 9b3710b | 2011-03-01 12:38:20 -0800 | [diff] [blame] | 2279 | setNetworkDetailedState(DetailedState.DISCONNECTED); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2280 | mWifiConfigStore.updateStatus(mLastNetworkId, DetailedState.DISCONNECTED); |
Irfan Sheriff | cdc077c | 2011-12-11 22:33:31 -0800 | [diff] [blame] | 2281 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2282 | /* Clear network properties */ |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 2283 | clearLinkProperties(); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 2284 | |
Irfan Sheriff | 81196b2 | 2013-03-26 13:38:57 -0700 | [diff] [blame] | 2285 | /* send event to CM & network change broadcast */ |
| 2286 | sendNetworkStateChangeBroadcast(mLastBssid); |
| 2287 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2288 | mLastBssid= null; |
Irfan Sheriff | 0a8bd60 | 2011-05-20 16:06:19 -0700 | [diff] [blame] | 2289 | mLastNetworkId = WifiConfiguration.INVALID_NETWORK_ID; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2292 | private void handleSupplicantConnectionLoss() { |
| 2293 | /* Socket connection can be lost when we do a graceful shutdown |
| 2294 | * or when the driver is hung. Ensure supplicant is stopped here. |
| 2295 | */ |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 2296 | mWifiMonitor.killSupplicant(mP2pSupported); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2297 | sendSupplicantConnectionChangedBroadcast(false); |
| 2298 | setWifiState(WIFI_STATE_DISABLED); |
| 2299 | } |
| 2300 | |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2301 | void handlePreDhcpSetup() { |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 2302 | mDhcpActive = true; |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2303 | if (!mBluetoothConnectionActive) { |
| 2304 | /* |
| 2305 | * There are problems setting the Wi-Fi driver's power |
| 2306 | * mode to active when bluetooth coexistence mode is |
| 2307 | * enabled or sense. |
| 2308 | * <p> |
| 2309 | * We set Wi-Fi to active mode when |
| 2310 | * obtaining an IP address because we've found |
| 2311 | * compatibility issues with some routers with low power |
| 2312 | * mode. |
| 2313 | * <p> |
| 2314 | * In order for this active power mode to properly be set, |
| 2315 | * we disable coexistence mode until we're done with |
| 2316 | * obtaining an IP address. One exception is if we |
| 2317 | * are currently connected to a headset, since disabling |
| 2318 | * coexistence would interrupt that connection. |
| 2319 | */ |
| 2320 | // Disable the coexistence mode |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2321 | mWifiNative.setBluetoothCoexistenceMode( |
| 2322 | mWifiNative.BLUETOOTH_COEXISTENCE_MODE_DISABLED); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 2325 | /* Disable power save and suspend optimizations during DHCP */ |
Irfan Sheriff | 9da603c | 2012-09-17 10:58:08 -0700 | [diff] [blame] | 2326 | // Note: The order here is important for now. Brcm driver changes |
| 2327 | // power settings when we control suspend mode optimizations. |
| 2328 | // TODO: Remove this comment when the driver is fixed. |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 2329 | setSuspendOptimizationsNative(SUSPEND_DUE_TO_DHCP, false); |
Irfan Sheriff | 9da603c | 2012-09-17 10:58:08 -0700 | [diff] [blame] | 2330 | mWifiNative.setPowerSave(false); |
Robert Greenwalt | 6433ef2 | 2013-06-28 15:55:28 -0700 | [diff] [blame] | 2331 | |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 2332 | stopBatchedScan(); |
| 2333 | |
Robert Greenwalt | 6433ef2 | 2013-06-28 15:55:28 -0700 | [diff] [blame] | 2334 | /* P2p discovery breaks dhcp, shut it down in order to get through this */ |
| 2335 | Message msg = new Message(); |
| 2336 | msg.what = WifiP2pService.BLOCK_DISCOVERY; |
| 2337 | msg.arg1 = WifiP2pService.ENABLED; |
| 2338 | msg.arg2 = DhcpStateMachine.CMD_PRE_DHCP_ACTION_COMPLETE; |
| 2339 | msg.obj = mDhcpStateMachine; |
| 2340 | mWifiP2pChannel.sendMessage(msg); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | |
Irfan Sheriff | 51e4ece | 2013-03-28 08:54:09 -0700 | [diff] [blame] | 2344 | void startDhcp() { |
| 2345 | if (mDhcpStateMachine == null) { |
| 2346 | mDhcpStateMachine = DhcpStateMachine.makeDhcpStateMachine( |
| 2347 | mContext, WifiStateMachine.this, mInterfaceName); |
| 2348 | |
| 2349 | } |
| 2350 | mDhcpStateMachine.registerForPreDhcpNotification(); |
| 2351 | mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_START_DHCP); |
| 2352 | } |
| 2353 | |
| 2354 | void stopDhcp() { |
| 2355 | if (mDhcpStateMachine != null) { |
| 2356 | /* In case we were in middle of DHCP operation restore back powermode */ |
| 2357 | handlePostDhcpSetup(); |
| 2358 | mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_STOP_DHCP); |
| 2359 | } |
| 2360 | } |
| 2361 | |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2362 | void handlePostDhcpSetup() { |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 2363 | /* Restore power save and suspend optimizations */ |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 2364 | setSuspendOptimizationsNative(SUSPEND_DUE_TO_DHCP, true); |
Irfan Sheriff | 9da603c | 2012-09-17 10:58:08 -0700 | [diff] [blame] | 2365 | mWifiNative.setPowerSave(true); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2366 | |
Robert Greenwalt | 6433ef2 | 2013-06-28 15:55:28 -0700 | [diff] [blame] | 2367 | mWifiP2pChannel.sendMessage(WifiP2pService.BLOCK_DISCOVERY, WifiP2pService.DISABLED); |
| 2368 | |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2369 | // Set the coexistence mode back to its default value |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2370 | mWifiNative.setBluetoothCoexistenceMode( |
| 2371 | mWifiNative.BLUETOOTH_COEXISTENCE_MODE_SENSE); |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 2372 | |
| 2373 | mDhcpActive = false; |
| 2374 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 2375 | startBatchedScan(); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2376 | } |
| 2377 | |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 2378 | private void handleSuccessfulIpConfiguration(DhcpResults dhcpResults) { |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2379 | mLastSignalLevel = -1; // force update of signal strength |
Irfan Sheriff | 1667a48 | 2011-11-01 16:28:17 -0700 | [diff] [blame] | 2380 | mReconnectCount = 0; //Reset IP failure tracking |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 2381 | synchronized (mDhcpResultsLock) { |
| 2382 | mDhcpResults = dhcpResults; |
| 2383 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 2384 | LinkProperties linkProperties = dhcpResults.linkProperties; |
Irfan Sheriff | 51e4ece | 2013-03-28 08:54:09 -0700 | [diff] [blame] | 2385 | mWifiConfigStore.setLinkProperties(mLastNetworkId, new LinkProperties(linkProperties)); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 2386 | InetAddress addr = null; |
| 2387 | Iterator<InetAddress> addrs = linkProperties.getAddresses().iterator(); |
| 2388 | if (addrs.hasNext()) { |
| 2389 | addr = addrs.next(); |
| 2390 | } |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2391 | mWifiInfo.setInetAddress(addr); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 2392 | mWifiInfo.setMeteredHint(dhcpResults.hasMeteredHint()); |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 2393 | updateLinkProperties(); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | private void handleFailedIpConfiguration() { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2397 | loge("IP configuration failed"); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2398 | |
| 2399 | mWifiInfo.setInetAddress(null); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2400 | mWifiInfo.setMeteredHint(false); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2401 | /** |
| 2402 | * If we've exceeded the maximum number of retries for DHCP |
| 2403 | * to a given network, disable the network |
| 2404 | */ |
Mike Lockwood | 79f5a04 | 2012-02-13 13:37:23 -0800 | [diff] [blame] | 2405 | int maxRetries = getMaxDhcpRetries(); |
| 2406 | // maxRetries == 0 means keep trying forever |
| 2407 | if (maxRetries > 0 && ++mReconnectCount > maxRetries) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2408 | loge("Failed " + |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2409 | mReconnectCount + " times, Disabling " + mLastNetworkId); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2410 | mWifiConfigStore.disableNetwork(mLastNetworkId, |
Isaac Levy | 8dc6a1b | 2011-07-27 08:00:03 -0700 | [diff] [blame] | 2411 | WifiConfiguration.DISABLED_DHCP_FAILURE); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2412 | mReconnectCount = 0; |
| 2413 | } |
| 2414 | |
| 2415 | /* DHCP times out after about 30 seconds, we do a |
| 2416 | * disconnect and an immediate reconnect to try again |
| 2417 | */ |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2418 | mWifiNative.disconnect(); |
| 2419 | mWifiNative.reconnect(); |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 2420 | } |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2421 | |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 2422 | /* Current design is to not set the config on a running hostapd but instead |
| 2423 | * stop and start tethering when user changes config on a running access point |
| 2424 | * |
| 2425 | * TODO: Add control channel setup through hostapd that allows changing config |
| 2426 | * on a running daemon |
| 2427 | */ |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 2428 | private void startSoftApWithConfig(final WifiConfiguration config) { |
| 2429 | // start hostapd on a seperate thread |
| 2430 | new Thread(new Runnable() { |
| 2431 | public void run() { |
| 2432 | try { |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 2433 | mNwService.startAccessPoint(config, mInterfaceName); |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 2434 | } catch (Exception e) { |
| 2435 | loge("Exception in softap start " + e); |
| 2436 | try { |
| 2437 | mNwService.stopAccessPoint(mInterfaceName); |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 2438 | mNwService.startAccessPoint(config, mInterfaceName); |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 2439 | } catch (Exception e1) { |
| 2440 | loge("Exception in softap re-start " + e1); |
| 2441 | sendMessage(CMD_START_AP_FAILURE); |
| 2442 | return; |
| 2443 | } |
| 2444 | } |
| 2445 | if (DBG) log("Soft AP start successful"); |
| 2446 | sendMessage(CMD_START_AP_SUCCESS); |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 2447 | } |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 2448 | }).start(); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2451 | /******************************************************** |
| 2452 | * HSM states |
| 2453 | *******************************************************/ |
| 2454 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 2455 | class DefaultState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2456 | @Override |
| 2457 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2458 | switch (message.what) { |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2459 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: |
| 2460 | if (message.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2461 | mWifiP2pChannel.sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2462 | } else { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2463 | loge("WifiP2pService connection failure, error=" + message.arg1); |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2464 | } |
| 2465 | break; |
| 2466 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2467 | loge("WifiP2pService channel lost, message.arg1 =" + message.arg1); |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2468 | //TODO: Re-establish connection to state machine after a delay |
| 2469 | //mWifiP2pChannel.connect(mContext, getHandler(), mWifiP2pManager.getMessenger()); |
| 2470 | break; |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 2471 | case CMD_BLUETOOTH_ADAPTER_STATE_CHANGE: |
| 2472 | mBluetoothConnectionActive = (message.arg1 != |
| 2473 | BluetoothAdapter.STATE_DISCONNECTED); |
Irfan Sheriff | cdf8ede | 2010-10-11 12:57:14 -0700 | [diff] [blame] | 2474 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2475 | /* Synchronous call returns */ |
| 2476 | case CMD_PING_SUPPLICANT: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2477 | case CMD_ENABLE_NETWORK: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2478 | case CMD_ADD_OR_UPDATE_NETWORK: |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 2479 | case CMD_REMOVE_NETWORK: |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 2480 | case CMD_SAVE_CONFIG: |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 2481 | replyToMessage(message, message.what, FAILURE); |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 2482 | break; |
Irfan Sheriff | e744cff | 2011-12-11 09:17:50 -0800 | [diff] [blame] | 2483 | case CMD_GET_CONFIGURED_NETWORKS: |
Irfan Sheriff | a3cbe75 | 2012-05-02 10:24:40 -0700 | [diff] [blame] | 2484 | replyToMessage(message, message.what, (List<WifiConfiguration>) null); |
Irfan Sheriff | e744cff | 2011-12-11 09:17:50 -0800 | [diff] [blame] | 2485 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2486 | case CMD_ENABLE_RSSI_POLL: |
| 2487 | mEnableRssiPolling = (message.arg1 == 1); |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2488 | break; |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 2489 | case CMD_ENABLE_BACKGROUND_SCAN: |
| 2490 | mEnableBackgroundScan = (message.arg1 == 1); |
| 2491 | break; |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 2492 | case CMD_SET_HIGH_PERF_MODE: |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 2493 | if (message.arg1 == 1) { |
| 2494 | setSuspendOptimizations(SUSPEND_DUE_TO_HIGH_PERF, false); |
| 2495 | } else { |
| 2496 | setSuspendOptimizations(SUSPEND_DUE_TO_HIGH_PERF, true); |
| 2497 | } |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 2498 | break; |
Robert Greenwalt | 304aad4 | 2013-05-31 17:57:10 -0700 | [diff] [blame] | 2499 | case CMD_BOOT_COMPLETED: |
| 2500 | String countryCode = mPersistedCountryCode; |
| 2501 | if (TextUtils.isEmpty(countryCode) == false) { |
Robert Greenwalt | 3ea0c99 | 2013-10-03 21:13:49 +0000 | [diff] [blame] | 2502 | Settings.Global.putString(mContext.getContentResolver(), |
| 2503 | Settings.Global.WIFI_COUNTRY_CODE, |
| 2504 | countryCode); |
| 2505 | // it may be that the state transition that should send this info |
| 2506 | // to the driver happened between mPersistedCountryCode getting set |
| 2507 | // and now, so simply persisting it here would mean we have sent |
| 2508 | // nothing to the driver. Send the cmd so it might be set now. |
Robert Greenwalt | 304aad4 | 2013-05-31 17:57:10 -0700 | [diff] [blame] | 2509 | sendMessageAtFrontOfQueue(CMD_SET_COUNTRY_CODE, countryCode); |
| 2510 | } |
| 2511 | break; |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 2512 | case CMD_SET_BATCHED_SCAN: |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 2513 | recordBatchedScanSettings(message.arg1, message.arg2, (Bundle)message.obj); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 2514 | break; |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 2515 | case CMD_POLL_BATCHED_SCAN: |
| 2516 | handleBatchedScanPollRequest(); |
Robert Greenwalt | 126755c | 2013-09-05 18:32:12 -0700 | [diff] [blame] | 2517 | break; |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 2518 | case CMD_START_NEXT_BATCHED_SCAN: |
| 2519 | startNextBatchedScan(); |
| 2520 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2521 | /* Discard */ |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 2522 | case CMD_START_SCAN: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2523 | case CMD_START_SUPPLICANT: |
| 2524 | case CMD_STOP_SUPPLICANT: |
Irfan Sheriff | cf54e4a | 2011-09-19 16:27:16 -0700 | [diff] [blame] | 2525 | case CMD_STOP_SUPPLICANT_FAILED: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2526 | case CMD_START_DRIVER: |
| 2527 | case CMD_STOP_DRIVER: |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 2528 | case CMD_DELAYED_STOP_DRIVER: |
Irfan Sheriff | f91d4e7 | 2012-05-14 14:26:07 -0700 | [diff] [blame] | 2529 | case CMD_DRIVER_START_TIMED_OUT: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2530 | case CMD_START_AP: |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 2531 | case CMD_START_AP_SUCCESS: |
| 2532 | case CMD_START_AP_FAILURE: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2533 | case CMD_STOP_AP: |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 2534 | case CMD_TETHER_STATE_CHANGE: |
| 2535 | case CMD_TETHER_NOTIFICATION_TIMED_OUT: |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 2536 | case CMD_DISCONNECT: |
| 2537 | case CMD_RECONNECT: |
| 2538 | case CMD_REASSOCIATE: |
Vinit Deshapnde | a3038b2 | 2013-10-02 17:26:05 -0700 | [diff] [blame] | 2539 | case CMD_RELOAD_TLS_AND_RECONNECT: |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2540 | case WifiMonitor.SUP_CONNECTION_EVENT: |
| 2541 | case WifiMonitor.SUP_DISCONNECTION_EVENT: |
| 2542 | case WifiMonitor.NETWORK_CONNECTION_EVENT: |
| 2543 | case WifiMonitor.NETWORK_DISCONNECTION_EVENT: |
| 2544 | case WifiMonitor.SCAN_RESULTS_EVENT: |
| 2545 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
| 2546 | case WifiMonitor.AUTHENTICATION_FAILURE_EVENT: |
Deepthi Gowri | c1b631e | 2013-04-30 18:23:57 +0530 | [diff] [blame] | 2547 | case WifiMonitor.ASSOCIATION_REJECTION_EVENT: |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2548 | case WifiMonitor.WPS_OVERLAP_EVENT: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2549 | case CMD_BLACKLIST_NETWORK: |
| 2550 | case CMD_CLEAR_BLACKLIST: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 2551 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 2552 | case CMD_SET_COUNTRY_CODE: |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 2553 | case CMD_SET_FREQUENCY_BAND: |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 2554 | case CMD_RSSI_POLL: |
Irfan Sheriff | 8e86b89 | 2010-12-22 11:02:20 -0800 | [diff] [blame] | 2555 | case CMD_ENABLE_ALL_NETWORKS: |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 2556 | case DhcpStateMachine.CMD_PRE_DHCP_ACTION: |
| 2557 | case DhcpStateMachine.CMD_POST_DHCP_ACTION: |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 2558 | /* Handled by WifiApConfigStore */ |
| 2559 | case CMD_SET_AP_CONFIG: |
| 2560 | case CMD_SET_AP_CONFIG_COMPLETED: |
| 2561 | case CMD_REQUEST_AP_CONFIG: |
| 2562 | case CMD_RESPONSE_AP_CONFIG: |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 2563 | case WifiWatchdogStateMachine.POOR_LINK_DETECTED: |
| 2564 | case WifiWatchdogStateMachine.GOOD_LINK_DETECTED: |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 2565 | case CMD_NO_NETWORKS_PERIODIC_SCAN: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 2566 | case CMD_DISABLE_P2P_RSP: |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 2567 | break; |
Irfan Sheriff | 6bfc888 | 2012-08-29 15:35:57 -0700 | [diff] [blame] | 2568 | case DhcpStateMachine.CMD_ON_QUIT: |
| 2569 | mDhcpStateMachine = null; |
| 2570 | break; |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 2571 | case CMD_SET_SUSPEND_OPT_ENABLED: |
| 2572 | if (message.arg1 == 1) { |
| 2573 | mSuspendWakeLock.release(); |
| 2574 | setSuspendOptimizations(SUSPEND_DUE_TO_SCREEN, true); |
| 2575 | } else { |
| 2576 | setSuspendOptimizations(SUSPEND_DUE_TO_SCREEN, false); |
| 2577 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2578 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2579 | case WifiMonitor.DRIVER_HUNG_EVENT: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 2580 | setSupplicantRunning(false); |
| 2581 | setSupplicantRunning(true); |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2582 | break; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 2583 | case WifiManager.CONNECT_NETWORK: |
| 2584 | replyToMessage(message, WifiManager.CONNECT_NETWORK_FAILED, |
| 2585 | WifiManager.BUSY); |
| 2586 | break; |
| 2587 | case WifiManager.FORGET_NETWORK: |
| 2588 | replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED, |
| 2589 | WifiManager.BUSY); |
| 2590 | break; |
| 2591 | case WifiManager.SAVE_NETWORK: |
| 2592 | replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, |
| 2593 | WifiManager.BUSY); |
| 2594 | break; |
| 2595 | case WifiManager.START_WPS: |
| 2596 | replyToMessage(message, WifiManager.WPS_FAILED, |
| 2597 | WifiManager.BUSY); |
| 2598 | break; |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 2599 | case WifiManager.CANCEL_WPS: |
| 2600 | replyToMessage(message, WifiManager.CANCEL_WPS_FAILED, |
| 2601 | WifiManager.BUSY); |
| 2602 | break; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 2603 | case WifiManager.DISABLE_NETWORK: |
| 2604 | replyToMessage(message, WifiManager.DISABLE_NETWORK_FAILED, |
| 2605 | WifiManager.BUSY); |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 2606 | break; |
Yuhao Zheng | f630782 | 2012-08-14 14:21:25 -0700 | [diff] [blame] | 2607 | case WifiManager.RSSI_PKTCNT_FETCH: |
| 2608 | replyToMessage(message, WifiManager.RSSI_PKTCNT_FETCH_FAILED, |
| 2609 | WifiManager.BUSY); |
Irfan Sheriff | 4c8982a | 2012-03-08 16:11:02 -0800 | [diff] [blame] | 2610 | break; |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 2611 | case WifiP2pService.P2P_CONNECTION_CHANGED: |
| 2612 | NetworkInfo info = (NetworkInfo) message.obj; |
| 2613 | mP2pConnected.set(info.isConnected()); |
| 2614 | break; |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 2615 | case WifiP2pService.DISCONNECT_WIFI_REQUEST: |
| 2616 | mTemporarilyDisconnectWifi = (message.arg1 == 1); |
| 2617 | replyToMessage(message, WifiP2pService.DISCONNECT_WIFI_RESPONSE); |
| 2618 | break; |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 2619 | case CMD_IP_ADDRESS_UPDATED: |
| 2620 | // addLinkAddress is a no-op if called more than once with the same address. |
| 2621 | if (mNetlinkLinkProperties.addLinkAddress((LinkAddress) message.obj)) { |
| 2622 | updateLinkProperties(); |
| 2623 | } |
| 2624 | break; |
| 2625 | case CMD_IP_ADDRESS_REMOVED: |
| 2626 | if (mNetlinkLinkProperties.removeLinkAddress((LinkAddress) message.obj)) { |
| 2627 | updateLinkProperties(); |
| 2628 | } |
| 2629 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2630 | default: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2631 | loge("Error! unhandled message" + message); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2632 | break; |
| 2633 | } |
| 2634 | return HANDLED; |
| 2635 | } |
| 2636 | } |
| 2637 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 2638 | class InitialState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2639 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2640 | public void enter() { |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2641 | mWifiNative.unloadDriver(); |
| 2642 | |
| 2643 | if (mWifiP2pChannel == null) { |
| 2644 | mWifiP2pChannel = new AsyncChannel(); |
| 2645 | mWifiP2pChannel.connect(mContext, getHandler(), mWifiP2pManager.getMessenger()); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2646 | } |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2647 | |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2648 | if (mWifiApConfigChannel == null) { |
| 2649 | mWifiApConfigChannel = new AsyncChannel(); |
| 2650 | WifiApConfigStore wifiApConfigStore = WifiApConfigStore.makeWifiApConfigStore( |
| 2651 | mContext, getHandler()); |
| 2652 | wifiApConfigStore.loadApConfiguration(); |
| 2653 | mWifiApConfigChannel.connectSync(mContext, getHandler(), |
| 2654 | wifiApConfigStore.getMessenger()); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 2655 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2656 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2657 | @Override |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2658 | public boolean processMessage(Message message) { |
| 2659 | switch (message.what) { |
| 2660 | case CMD_START_SUPPLICANT: |
| 2661 | if (mWifiNative.loadDriver()) { |
| 2662 | try { |
| 2663 | mNwService.wifiFirmwareReload(mInterfaceName, "STA"); |
| 2664 | } catch (Exception e) { |
| 2665 | loge("Failed to reload STA firmware " + e); |
| 2666 | // continue |
| 2667 | } |
| 2668 | |
| 2669 | try { |
| 2670 | // A runtime crash can leave the interface up and |
| 2671 | // this affects connectivity when supplicant starts up. |
| 2672 | // Ensure interface is down before a supplicant start. |
| 2673 | mNwService.setInterfaceDown(mInterfaceName); |
| 2674 | // Set privacy extensions |
| 2675 | mNwService.setInterfaceIpv6PrivacyExtensions(mInterfaceName, true); |
| 2676 | |
| 2677 | // IPv6 is enabled only as long as access point is connected since: |
| 2678 | // - IPv6 addresses and routes stick around after disconnection |
| 2679 | // - kernel is unaware when connected and fails to start IPv6 negotiation |
| 2680 | // - kernel can start autoconfiguration when 802.1x is not complete |
| 2681 | mNwService.disableIpv6(mInterfaceName); |
| 2682 | } catch (RemoteException re) { |
| 2683 | loge("Unable to change interface settings: " + re); |
| 2684 | } catch (IllegalStateException ie) { |
| 2685 | loge("Unable to change interface settings: " + ie); |
| 2686 | } |
| 2687 | |
| 2688 | /* Stop a running supplicant after a runtime restart |
| 2689 | * Avoids issues with drivers that do not handle interface down |
| 2690 | * on a running supplicant properly. |
| 2691 | */ |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 2692 | mWifiMonitor.killSupplicant(mP2pSupported); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2693 | if(mWifiNative.startSupplicant(mP2pSupported)) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2694 | setWifiState(WIFI_STATE_ENABLING); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2695 | if (DBG) log("Supplicant start successful"); |
| 2696 | mWifiMonitor.startMonitoring(); |
| 2697 | transitionTo(mSupplicantStartingState); |
| 2698 | } else { |
| 2699 | loge("Failed to start supplicant!"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2700 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2701 | } else { |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2702 | loge("Failed to load driver"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2703 | } |
| 2704 | break; |
| 2705 | case CMD_START_AP: |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2706 | if (mWifiNative.loadDriver()) { |
| 2707 | setWifiApState(WIFI_AP_STATE_ENABLING); |
| 2708 | transitionTo(mSoftApStartingState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2709 | } else { |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2710 | loge("Failed to load driver for softap"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2711 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2712 | default: |
| 2713 | return NOT_HANDLED; |
| 2714 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2715 | return HANDLED; |
| 2716 | } |
| 2717 | } |
| 2718 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 2719 | class SupplicantStartingState extends State { |
Irfan Sheriff | 4dd5a25 | 2012-05-22 12:53:47 -0700 | [diff] [blame] | 2720 | private void initializeWpsDetails() { |
| 2721 | String detail; |
| 2722 | detail = SystemProperties.get("ro.product.name", ""); |
| 2723 | if (!mWifiNative.setDeviceName(detail)) { |
| 2724 | loge("Failed to set device name " + detail); |
| 2725 | } |
| 2726 | detail = SystemProperties.get("ro.product.manufacturer", ""); |
| 2727 | if (!mWifiNative.setManufacturer(detail)) { |
| 2728 | loge("Failed to set manufacturer " + detail); |
| 2729 | } |
| 2730 | detail = SystemProperties.get("ro.product.model", ""); |
| 2731 | if (!mWifiNative.setModelName(detail)) { |
| 2732 | loge("Failed to set model name " + detail); |
| 2733 | } |
| 2734 | detail = SystemProperties.get("ro.product.model", ""); |
| 2735 | if (!mWifiNative.setModelNumber(detail)) { |
| 2736 | loge("Failed to set model number " + detail); |
| 2737 | } |
| 2738 | detail = SystemProperties.get("ro.serialno", ""); |
| 2739 | if (!mWifiNative.setSerialNumber(detail)) { |
| 2740 | loge("Failed to set serial number " + detail); |
| 2741 | } |
Yoshihiko Ikenaga | 1201dd9 | 2013-01-24 13:48:58 +0900 | [diff] [blame] | 2742 | if (!mWifiNative.setConfigMethods("physical_display virtual_push_button")) { |
Irfan Sheriff | 4dd5a25 | 2012-05-22 12:53:47 -0700 | [diff] [blame] | 2743 | loge("Failed to set WPS config methods"); |
| 2744 | } |
| 2745 | if (!mWifiNative.setDeviceType(mPrimaryDeviceType)) { |
| 2746 | loge("Failed to set primary device type " + mPrimaryDeviceType); |
| 2747 | } |
| 2748 | } |
| 2749 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2750 | @Override |
| 2751 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2752 | switch(message.what) { |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2753 | case WifiMonitor.SUP_CONNECTION_EVENT: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2754 | if (DBG) log("Supplicant connection established"); |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 2755 | setWifiState(WIFI_STATE_ENABLED); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2756 | mSupplicantRestartCount = 0; |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 2757 | /* Reset the supplicant state to indicate the supplicant |
| 2758 | * state is not known at this time */ |
| 2759 | mSupplicantStateTracker.sendMessage(CMD_RESET_SUPPLICANT_STATE); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2760 | /* Initialize data structures */ |
| 2761 | mLastBssid = null; |
Irfan Sheriff | 0a8bd60 | 2011-05-20 16:06:19 -0700 | [diff] [blame] | 2762 | mLastNetworkId = WifiConfiguration.INVALID_NETWORK_ID; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2763 | mLastSignalLevel = -1; |
| 2764 | |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2765 | mWifiInfo.setMacAddress(mWifiNative.getMacAddress()); |
Irfan Sheriff | cca308e | 2013-04-25 23:22:41 -0700 | [diff] [blame] | 2766 | mWifiConfigStore.loadAndEnableAllNetworks(); |
Irfan Sheriff | 4dd5a25 | 2012-05-22 12:53:47 -0700 | [diff] [blame] | 2767 | initializeWpsDetails(); |
Irfan Sheriff | 9e6222f | 2010-08-23 10:41:11 -0700 | [diff] [blame] | 2768 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2769 | sendSupplicantConnectionChangedBroadcast(true); |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 2770 | transitionTo(mDriverStartedState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2771 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2772 | case WifiMonitor.SUP_DISCONNECTION_EVENT: |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2773 | if (++mSupplicantRestartCount <= SUPPLICANT_RESTART_TRIES) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2774 | loge("Failed to setup control channel, restart supplicant"); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 2775 | mWifiMonitor.killSupplicant(mP2pSupported); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2776 | transitionTo(mInitialState); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2777 | sendMessageDelayed(CMD_START_SUPPLICANT, SUPPLICANT_RESTART_INTERVAL_MSECS); |
| 2778 | } else { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2779 | loge("Failed " + mSupplicantRestartCount + |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2780 | " times to start supplicant, unload driver"); |
Ji-Hwan Lee | c4501f7 | 2011-07-04 14:27:26 +0900 | [diff] [blame] | 2781 | mSupplicantRestartCount = 0; |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2782 | setWifiState(WIFI_STATE_UNKNOWN); |
| 2783 | transitionTo(mInitialState); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2784 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2785 | break; |
Irfan Sheriff | 68825ac | 2010-10-12 16:34:07 -0700 | [diff] [blame] | 2786 | case CMD_START_SUPPLICANT: |
| 2787 | case CMD_STOP_SUPPLICANT: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2788 | case CMD_START_AP: |
Irfan Sheriff | 68825ac | 2010-10-12 16:34:07 -0700 | [diff] [blame] | 2789 | case CMD_STOP_AP: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2790 | case CMD_START_DRIVER: |
| 2791 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 2792 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 2793 | case CMD_SET_COUNTRY_CODE: |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 2794 | case CMD_SET_FREQUENCY_BAND: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2795 | case CMD_START_PACKET_FILTERING: |
| 2796 | case CMD_STOP_PACKET_FILTERING: |
| 2797 | deferMessage(message); |
| 2798 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2799 | default: |
| 2800 | return NOT_HANDLED; |
| 2801 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2802 | return HANDLED; |
| 2803 | } |
| 2804 | } |
| 2805 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 2806 | class SupplicantStartedState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2807 | @Override |
| 2808 | public void enter() { |
Irfan Sheriff | 6bb7652 | 2011-01-11 11:17:14 -0800 | [diff] [blame] | 2809 | /* Wifi is available as long as we have a connection to supplicant */ |
| 2810 | mNetworkInfo.setIsAvailable(true); |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 2811 | |
| 2812 | int defaultInterval = mContext.getResources().getInteger( |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 2813 | R.integer.config_wifi_supplicant_scan_interval); |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 2814 | |
Christopher Tate | 6f5a9a9 | 2012-09-14 17:24:28 -0700 | [diff] [blame] | 2815 | mSupplicantScanIntervalMs = Settings.Global.getLong(mContext.getContentResolver(), |
| 2816 | Settings.Global.WIFI_SUPPLICANT_SCAN_INTERVAL_MS, |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 2817 | defaultInterval); |
| 2818 | |
| 2819 | mWifiNative.setScanInterval((int)mSupplicantScanIntervalMs / 1000); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2820 | } |
| 2821 | @Override |
| 2822 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2823 | switch(message.what) { |
| 2824 | case CMD_STOP_SUPPLICANT: /* Supplicant stopped by user */ |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 2825 | if (mP2pSupported) { |
| 2826 | transitionTo(mWaitForP2pDisableState); |
| 2827 | } else { |
| 2828 | transitionTo(mSupplicantStoppingState); |
| 2829 | } |
Irfan Sheriff | 68825ac | 2010-10-12 16:34:07 -0700 | [diff] [blame] | 2830 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2831 | case WifiMonitor.SUP_DISCONNECTION_EVENT: /* Supplicant connection lost */ |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2832 | loge("Connection lost, restart supplicant"); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2833 | handleSupplicantConnectionLoss(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2834 | handleNetworkDisconnect(); |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 2835 | mSupplicantStateTracker.sendMessage(CMD_RESET_SUPPLICANT_STATE); |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 2836 | if (mP2pSupported) { |
| 2837 | transitionTo(mWaitForP2pDisableState); |
| 2838 | } else { |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2839 | transitionTo(mInitialState); |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 2840 | } |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2841 | sendMessageDelayed(CMD_START_SUPPLICANT, SUPPLICANT_RESTART_INTERVAL_MSECS); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2842 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2843 | case WifiMonitor.SCAN_RESULTS_EVENT: |
Iliyan Malchev | 9233fc0 | 2013-06-21 03:09:44 +0000 | [diff] [blame] | 2844 | setScanResults(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2845 | sendScanResultsAvailableBroadcast(); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 2846 | mScanResultIsPending = false; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2847 | break; |
| 2848 | case CMD_PING_SUPPLICANT: |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 2849 | boolean ok = mWifiNative.ping(); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 2850 | replyToMessage(message, message.what, ok ? SUCCESS : FAILURE); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2851 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2852 | /* Cannot start soft AP while in client mode */ |
| 2853 | case CMD_START_AP: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2854 | loge("Failed to start soft AP with a running supplicant"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2855 | setWifiApState(WIFI_AP_STATE_FAILED); |
| 2856 | break; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 2857 | case CMD_SET_OPERATIONAL_MODE: |
| 2858 | mOperationalMode = message.arg1; |
Irfan Sheriff | e04653c | 2010-08-09 09:09:59 -0700 | [diff] [blame] | 2859 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2860 | default: |
| 2861 | return NOT_HANDLED; |
| 2862 | } |
| 2863 | return HANDLED; |
| 2864 | } |
Irfan Sheriff | 6bb7652 | 2011-01-11 11:17:14 -0800 | [diff] [blame] | 2865 | |
| 2866 | @Override |
| 2867 | public void exit() { |
| 2868 | mNetworkInfo.setIsAvailable(false); |
| 2869 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2870 | } |
| 2871 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 2872 | class SupplicantStoppingState extends State { |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2873 | @Override |
| 2874 | public void enter() { |
Irfan Sheriff | 4aa9bcd | 2012-02-13 16:12:48 -0800 | [diff] [blame] | 2875 | /* Send any reset commands to supplicant before shutting it down */ |
| 2876 | handleNetworkDisconnect(); |
Irfan Sheriff | 6bfc888 | 2012-08-29 15:35:57 -0700 | [diff] [blame] | 2877 | if (mDhcpStateMachine != null) { |
| 2878 | mDhcpStateMachine.doQuit(); |
| 2879 | } |
Irfan Sheriff | 4aa9bcd | 2012-02-13 16:12:48 -0800 | [diff] [blame] | 2880 | |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2881 | if (DBG) log("stopping supplicant"); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 2882 | mWifiMonitor.stopSupplicant(); |
Irfan Sheriff | cf54e4a | 2011-09-19 16:27:16 -0700 | [diff] [blame] | 2883 | |
| 2884 | /* Send ourselves a delayed message to indicate failure after a wait time */ |
| 2885 | sendMessageDelayed(obtainMessage(CMD_STOP_SUPPLICANT_FAILED, |
| 2886 | ++mSupplicantStopFailureToken, 0), SUPPLICANT_RESTART_INTERVAL_MSECS); |
Irfan Sheriff | 5cd8d4d | 2011-04-07 15:56:22 -0700 | [diff] [blame] | 2887 | setWifiState(WIFI_STATE_DISABLING); |
Irfan Sheriff | 5cd8d4d | 2011-04-07 15:56:22 -0700 | [diff] [blame] | 2888 | mSupplicantStateTracker.sendMessage(CMD_RESET_SUPPLICANT_STATE); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2889 | } |
| 2890 | @Override |
| 2891 | public boolean processMessage(Message message) { |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2892 | switch(message.what) { |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2893 | case WifiMonitor.SUP_CONNECTION_EVENT: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2894 | loge("Supplicant connection received while stopping"); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2895 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2896 | case WifiMonitor.SUP_DISCONNECTION_EVENT: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2897 | if (DBG) log("Supplicant connection lost"); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2898 | handleSupplicantConnectionLoss(); |
| 2899 | transitionTo(mInitialState); |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2900 | break; |
Irfan Sheriff | cf54e4a | 2011-09-19 16:27:16 -0700 | [diff] [blame] | 2901 | case CMD_STOP_SUPPLICANT_FAILED: |
| 2902 | if (message.arg1 == mSupplicantStopFailureToken) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 2903 | loge("Timed out on a supplicant stop, kill and proceed"); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 2904 | handleSupplicantConnectionLoss(); |
| 2905 | transitionTo(mInitialState); |
Irfan Sheriff | cf54e4a | 2011-09-19 16:27:16 -0700 | [diff] [blame] | 2906 | } |
| 2907 | break; |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2908 | case CMD_START_SUPPLICANT: |
| 2909 | case CMD_STOP_SUPPLICANT: |
| 2910 | case CMD_START_AP: |
| 2911 | case CMD_STOP_AP: |
| 2912 | case CMD_START_DRIVER: |
| 2913 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 2914 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2915 | case CMD_SET_COUNTRY_CODE: |
| 2916 | case CMD_SET_FREQUENCY_BAND: |
| 2917 | case CMD_START_PACKET_FILTERING: |
| 2918 | case CMD_STOP_PACKET_FILTERING: |
| 2919 | deferMessage(message); |
| 2920 | break; |
| 2921 | default: |
| 2922 | return NOT_HANDLED; |
| 2923 | } |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 2924 | return HANDLED; |
| 2925 | } |
| 2926 | } |
| 2927 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 2928 | class DriverStartingState extends State { |
Irfan Sheriff | f91d4e7 | 2012-05-14 14:26:07 -0700 | [diff] [blame] | 2929 | private int mTries; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2930 | @Override |
| 2931 | public void enter() { |
Irfan Sheriff | f91d4e7 | 2012-05-14 14:26:07 -0700 | [diff] [blame] | 2932 | mTries = 1; |
| 2933 | /* Send ourselves a delayed message to start driver a second time */ |
| 2934 | sendMessageDelayed(obtainMessage(CMD_DRIVER_START_TIMED_OUT, |
| 2935 | ++mDriverStartToken, 0), DRIVER_START_TIME_OUT_MSECS); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2936 | } |
| 2937 | @Override |
| 2938 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2939 | switch(message.what) { |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2940 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
Irfan Sheriff | 1523da2 | 2011-06-10 14:00:27 -0700 | [diff] [blame] | 2941 | SupplicantState state = handleSupplicantStateChange(message); |
| 2942 | /* If suplicant is exiting out of INTERFACE_DISABLED state into |
| 2943 | * a state that indicates driver has started, it is ready to |
| 2944 | * receive driver commands |
| 2945 | */ |
| 2946 | if (SupplicantState.isDriverActive(state)) { |
| 2947 | transitionTo(mDriverStartedState); |
| 2948 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2949 | break; |
Irfan Sheriff | f91d4e7 | 2012-05-14 14:26:07 -0700 | [diff] [blame] | 2950 | case CMD_DRIVER_START_TIMED_OUT: |
| 2951 | if (message.arg1 == mDriverStartToken) { |
| 2952 | if (mTries >= 2) { |
| 2953 | loge("Failed to start driver after " + mTries); |
| 2954 | transitionTo(mDriverStoppedState); |
| 2955 | } else { |
| 2956 | loge("Driver start failed, retrying"); |
| 2957 | mWakeLock.acquire(); |
| 2958 | mWifiNative.startDriver(); |
| 2959 | mWakeLock.release(); |
| 2960 | |
| 2961 | ++mTries; |
| 2962 | /* Send ourselves a delayed message to start driver again */ |
| 2963 | sendMessageDelayed(obtainMessage(CMD_DRIVER_START_TIMED_OUT, |
| 2964 | ++mDriverStartToken, 0), DRIVER_START_TIME_OUT_MSECS); |
| 2965 | } |
| 2966 | } |
| 2967 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2968 | /* Queue driver commands & connection events */ |
| 2969 | case CMD_START_DRIVER: |
| 2970 | case CMD_STOP_DRIVER: |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2971 | case WifiMonitor.NETWORK_CONNECTION_EVENT: |
| 2972 | case WifiMonitor.NETWORK_DISCONNECTION_EVENT: |
| 2973 | case WifiMonitor.AUTHENTICATION_FAILURE_EVENT: |
Deepthi Gowri | c1b631e | 2013-04-30 18:23:57 +0530 | [diff] [blame] | 2974 | case WifiMonitor.ASSOCIATION_REJECTION_EVENT: |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 2975 | case WifiMonitor.WPS_OVERLAP_EVENT: |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 2976 | case CMD_SET_COUNTRY_CODE: |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 2977 | case CMD_SET_FREQUENCY_BAND: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2978 | case CMD_START_PACKET_FILTERING: |
| 2979 | case CMD_STOP_PACKET_FILTERING: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2980 | case CMD_START_SCAN: |
| 2981 | case CMD_DISCONNECT: |
| 2982 | case CMD_REASSOCIATE: |
| 2983 | case CMD_RECONNECT: |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 2984 | deferMessage(message); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2985 | break; |
| 2986 | default: |
| 2987 | return NOT_HANDLED; |
| 2988 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2989 | return HANDLED; |
| 2990 | } |
| 2991 | } |
| 2992 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 2993 | class DriverStartedState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 2994 | @Override |
| 2995 | public void enter() { |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 2996 | mIsRunning = true; |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 2997 | mInDelayedStop = false; |
Robert Greenwalt | e9868c1 | 2013-05-28 19:33:48 -0700 | [diff] [blame] | 2998 | mDelayedStopCounter++; |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 2999 | updateBatteryWorkSource(null); |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 3000 | /** |
| 3001 | * Enable bluetooth coexistence scan mode when bluetooth connection is active. |
| 3002 | * When this mode is on, some of the low-level scan parameters used by the |
| 3003 | * driver are changed to reduce interference with bluetooth |
| 3004 | */ |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3005 | mWifiNative.setBluetoothCoexistenceScanMode(mBluetoothConnectionActive); |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 3006 | /* set country code */ |
| 3007 | setCountryCode(); |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 3008 | /* set frequency band of operation */ |
| 3009 | setFrequencyBand(); |
Irfan Sheriff | 3a65de7 | 2010-12-14 17:45:26 -0800 | [diff] [blame] | 3010 | /* initialize network state */ |
| 3011 | setNetworkDetailedState(DetailedState.DISCONNECTED); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3012 | |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3013 | /* Remove any filtering on Multicast v6 at start */ |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3014 | mWifiNative.stopFilteringMulticastV6Packets(); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3015 | |
| 3016 | /* Reset Multicast v4 filtering state */ |
| 3017 | if (mFilteringMulticastV4Packets.get()) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3018 | mWifiNative.startFilteringMulticastV4Packets(); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3019 | } else { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3020 | mWifiNative.stopFilteringMulticastV4Packets(); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 3023 | mDhcpActive = false; |
| 3024 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 3025 | startBatchedScan(); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 3026 | |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3027 | if (mOperationalMode != CONNECT_MODE) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3028 | mWifiNative.disconnect(); |
Robert Greenwalt | 8d9167d | 2013-10-09 16:31:35 -0700 | [diff] [blame] | 3029 | mWifiConfigStore.disableAllNetworks(); |
Robert Greenwalt | 15af212 | 2013-10-08 17:43:33 -0700 | [diff] [blame] | 3030 | if (mOperationalMode == SCAN_ONLY_WITH_WIFI_OFF_MODE) { |
Robert Greenwalt | 15af212 | 2013-10-08 17:43:33 -0700 | [diff] [blame] | 3031 | setWifiState(WIFI_STATE_DISABLED); |
| 3032 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3033 | transitionTo(mScanModeState); |
| 3034 | } else { |
Irfan Sheriff | b8df545 | 2013-04-16 13:52:32 -0700 | [diff] [blame] | 3035 | /* Driver stop may have disabled networks, enable right after start */ |
| 3036 | mWifiConfigStore.enableAllNetworks(); |
Vinit Deshapnde | e8fabf9 | 2013-08-23 11:53:40 -0700 | [diff] [blame] | 3037 | |
| 3038 | if (DBG) log("Attempting to reconnect to wifi network .."); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3039 | mWifiNative.reconnect(); |
Vinit Deshapnde | e8fabf9 | 2013-08-23 11:53:40 -0700 | [diff] [blame] | 3040 | |
Irfan Sheriff | 576db76 | 2012-01-11 16:54:09 -0800 | [diff] [blame] | 3041 | // Status pulls in the current supplicant state and network connection state |
| 3042 | // events over the monitor connection. This helps framework sync up with |
| 3043 | // current supplicant state |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3044 | mWifiNative.status(); |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 3045 | transitionTo(mDisconnectedState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3046 | } |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 3047 | |
Irfan Sheriff | 1c045ec | 2012-09-16 14:55:26 -0700 | [diff] [blame] | 3048 | // We may have missed screen update at boot |
| 3049 | if (mScreenBroadcastReceived.get() == false) { |
| 3050 | PowerManager powerManager = (PowerManager)mContext.getSystemService( |
| 3051 | Context.POWER_SERVICE); |
| 3052 | handleScreenStateChanged(powerManager.isScreenOn()); |
| 3053 | } else { |
| 3054 | // Set the right suspend mode settings |
| 3055 | mWifiNative.setSuspendOptimizations(mSuspendOptNeedsDisabled == 0 |
| 3056 | && mUserWantsSuspendOpt.get()); |
| 3057 | } |
Irfan Sheriff | 9da603c | 2012-09-17 10:58:08 -0700 | [diff] [blame] | 3058 | mWifiNative.setPowerSave(true); |
Irfan Sheriff | 1c045ec | 2012-09-16 14:55:26 -0700 | [diff] [blame] | 3059 | |
Vinit Deshapnde | 6029611 | 2013-09-24 17:38:09 -0700 | [diff] [blame] | 3060 | if (mP2pSupported) { |
| 3061 | if (mOperationalMode == CONNECT_MODE) { |
| 3062 | mWifiP2pChannel.sendMessage(WifiStateMachine.CMD_ENABLE_P2P); |
| 3063 | } else { |
| 3064 | // P2P statemachine starts in disabled state, and is not enabled until |
| 3065 | // CMD_ENABLE_P2P is sent from here; so, nothing needs to be done to |
| 3066 | // keep it disabled. |
| 3067 | } |
| 3068 | } |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 3069 | |
| 3070 | final Intent intent = new Intent(WifiManager.WIFI_SCAN_AVAILABLE); |
| 3071 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 3072 | intent.putExtra(WifiManager.EXTRA_SCAN_AVAILABLE, WIFI_STATE_ENABLED); |
| 3073 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3074 | } |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 3075 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3076 | @Override |
| 3077 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3078 | switch(message.what) { |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3079 | case CMD_START_SCAN: |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 3080 | noteScanStart(message.arg1, (WorkSource) message.obj); |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 3081 | startScanNative(WifiNative.SCAN_WITH_CONNECTION_SETUP); |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3082 | break; |
Robert Greenwalt | 8e628da | 2013-08-14 16:07:44 -0700 | [diff] [blame] | 3083 | case CMD_SET_BATCHED_SCAN: |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 3084 | if (recordBatchedScanSettings(message.arg1, message.arg2, |
| 3085 | (Bundle)message.obj)) { |
| 3086 | startBatchedScan(); |
| 3087 | } |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 3088 | break; |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 3089 | case CMD_SET_COUNTRY_CODE: |
| 3090 | String country = (String) message.obj; |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3091 | if (DBG) log("set country code " + country); |
Vinit Deshapnde | da40d92 | 2013-10-21 11:58:40 -0700 | [diff] [blame] | 3092 | if (country != null) { |
| 3093 | country = country.toUpperCase(Locale.ROOT); |
| 3094 | if (mLastSetCountryCode == null |
| 3095 | || country.equals(mLastSetCountryCode) == false) { |
| 3096 | if (mWifiNative.setCountryCode(country)) { |
| 3097 | mLastSetCountryCode = country; |
| 3098 | } else { |
| 3099 | loge("Failed to set country code " + country); |
| 3100 | } |
| 3101 | } |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 3102 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3103 | break; |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 3104 | case CMD_SET_FREQUENCY_BAND: |
| 3105 | int band = message.arg1; |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3106 | if (DBG) log("set frequency band " + band); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3107 | if (mWifiNative.setBand(band)) { |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 3108 | mFrequencyBand.set(band); |
Vinit Deshapnde | b8d116a | 2013-07-09 10:42:38 -0700 | [diff] [blame] | 3109 | // flush old data - like scan results |
| 3110 | mWifiNative.bssFlush(); |
Irfan Sheriff | cf34f47 | 2010-12-06 10:07:10 -0800 | [diff] [blame] | 3111 | //Fetch the latest scan results when frequency band is set |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 3112 | startScanNative(WifiNative.SCAN_WITH_CONNECTION_SETUP); |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 3113 | } else { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3114 | loge("Failed to set frequency band " + band); |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 3115 | } |
| 3116 | break; |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 3117 | case CMD_BLUETOOTH_ADAPTER_STATE_CHANGE: |
| 3118 | mBluetoothConnectionActive = (message.arg1 != |
| 3119 | BluetoothAdapter.STATE_DISCONNECTED); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3120 | mWifiNative.setBluetoothCoexistenceScanMode(mBluetoothConnectionActive); |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 3121 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3122 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 4494c90 | 2011-12-08 10:47:54 -0800 | [diff] [blame] | 3123 | int mode = message.arg1; |
| 3124 | |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3125 | /* Already doing a delayed stop */ |
| 3126 | if (mInDelayedStop) { |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 3127 | if (DBG) log("Already in delayed stop"); |
| 3128 | break; |
| 3129 | } |
Robert Greenwalt | a05e458 | 2013-04-04 15:51:08 -0700 | [diff] [blame] | 3130 | /* disconnect right now, but leave the driver running for a bit */ |
| 3131 | mWifiConfigStore.disableAllNetworks(); |
| 3132 | |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 3133 | mInDelayedStop = true; |
| 3134 | mDelayedStopCounter++; |
| 3135 | if (DBG) log("Delayed stop message " + mDelayedStopCounter); |
Irfan Sheriff | 4494c90 | 2011-12-08 10:47:54 -0800 | [diff] [blame] | 3136 | |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3137 | /* send regular delayed shut down */ |
| 3138 | Intent driverStopIntent = new Intent(ACTION_DELAYED_DRIVER_STOP, null); |
| 3139 | driverStopIntent.putExtra(DELAYED_STOP_COUNTER, mDelayedStopCounter); |
| 3140 | mDriverStopIntent = PendingIntent.getBroadcast(mContext, |
| 3141 | DRIVER_STOP_REQUEST, driverStopIntent, |
| 3142 | PendingIntent.FLAG_UPDATE_CURRENT); |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 3143 | |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3144 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() |
| 3145 | + mDriverStopDelayMs, mDriverStopIntent); |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 3146 | break; |
| 3147 | case CMD_START_DRIVER: |
| 3148 | if (mInDelayedStop) { |
| 3149 | mInDelayedStop = false; |
| 3150 | mDelayedStopCounter++; |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 3151 | mAlarmManager.cancel(mDriverStopIntent); |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 3152 | if (DBG) log("Delayed stop ignored due to start"); |
Robert Greenwalt | a05e458 | 2013-04-04 15:51:08 -0700 | [diff] [blame] | 3153 | if (mOperationalMode == CONNECT_MODE) { |
| 3154 | mWifiConfigStore.enableAllNetworks(); |
| 3155 | } |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 3156 | } |
| 3157 | break; |
| 3158 | case CMD_DELAYED_STOP_DRIVER: |
Vishal Mahaveer | f48fb85 | 2012-05-24 14:03:36 -0500 | [diff] [blame] | 3159 | if (DBG) log("delayed stop " + message.arg1 + " " + mDelayedStopCounter); |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 3160 | if (message.arg1 != mDelayedStopCounter) break; |
| 3161 | if (getCurrentState() != mDisconnectedState) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3162 | mWifiNative.disconnect(); |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 3163 | handleNetworkDisconnect(); |
| 3164 | } |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 3165 | mWakeLock.acquire(); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3166 | mWifiNative.stopDriver(); |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 3167 | mWakeLock.release(); |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3168 | if (mP2pSupported) { |
| 3169 | transitionTo(mWaitForP2pDisableState); |
| 3170 | } else { |
| 3171 | transitionTo(mDriverStoppingState); |
| 3172 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3173 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3174 | case CMD_START_PACKET_FILTERING: |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3175 | if (message.arg1 == MULTICAST_V6) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3176 | mWifiNative.startFilteringMulticastV6Packets(); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3177 | } else if (message.arg1 == MULTICAST_V4) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3178 | mWifiNative.startFilteringMulticastV4Packets(); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3179 | } else { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3180 | loge("Illegal arugments to CMD_START_PACKET_FILTERING"); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3181 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3182 | break; |
| 3183 | case CMD_STOP_PACKET_FILTERING: |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3184 | if (message.arg1 == MULTICAST_V6) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3185 | mWifiNative.stopFilteringMulticastV6Packets(); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3186 | } else if (message.arg1 == MULTICAST_V4) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3187 | mWifiNative.stopFilteringMulticastV4Packets(); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3188 | } else { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3189 | loge("Illegal arugments to CMD_STOP_PACKET_FILTERING"); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 3190 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3191 | break; |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 3192 | case CMD_SET_SUSPEND_OPT_ENABLED: |
| 3193 | if (message.arg1 == 1) { |
| 3194 | setSuspendOptimizationsNative(SUSPEND_DUE_TO_SCREEN, true); |
| 3195 | mSuspendWakeLock.release(); |
| 3196 | } else { |
| 3197 | setSuspendOptimizationsNative(SUSPEND_DUE_TO_SCREEN, false); |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 3198 | } |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 3199 | break; |
| 3200 | case CMD_SET_HIGH_PERF_MODE: |
Irfan Sheriff | ec7d138 | 2012-09-11 14:26:33 -0700 | [diff] [blame] | 3201 | if (message.arg1 == 1) { |
| 3202 | setSuspendOptimizationsNative(SUSPEND_DUE_TO_HIGH_PERF, false); |
| 3203 | } else { |
| 3204 | setSuspendOptimizationsNative(SUSPEND_DUE_TO_HIGH_PERF, true); |
Irfan Sheriff | 262f766 | 2012-03-26 23:48:53 -0700 | [diff] [blame] | 3205 | } |
| 3206 | break; |
Vinit Deshapnde | 64f0d5b | 2013-07-11 15:12:44 -0700 | [diff] [blame] | 3207 | case CMD_ENABLE_TDLS: |
| 3208 | if (message.obj != null) { |
| 3209 | String remoteAddress = (String) message.obj; |
| 3210 | boolean enable = (message.arg1 == 1); |
| 3211 | mWifiNative.startTdls(remoteAddress, enable); |
| 3212 | } |
| 3213 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3214 | default: |
| 3215 | return NOT_HANDLED; |
| 3216 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3217 | return HANDLED; |
| 3218 | } |
| 3219 | @Override |
| 3220 | public void exit() { |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 3221 | mIsRunning = false; |
| 3222 | updateBatteryWorkSource(null); |
Iliyan Malchev | 06c43d5 | 2012-08-20 12:52:35 -0700 | [diff] [blame] | 3223 | mScanResults = new ArrayList<ScanResult>(); |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 3224 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 3225 | stopBatchedScan(); |
Robert Greenwalt | 0451d59 | 2013-08-01 18:24:13 -0700 | [diff] [blame] | 3226 | |
Robert Greenwalt | 8971084 | 2013-06-25 11:41:53 -0700 | [diff] [blame] | 3227 | final Intent intent = new Intent(WifiManager.WIFI_SCAN_AVAILABLE); |
| 3228 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 3229 | intent.putExtra(WifiManager.EXTRA_SCAN_AVAILABLE, WIFI_STATE_DISABLED); |
| 3230 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 3231 | noteScanEnd(); // wrap up any pending request. |
Vinit Deshapnde | da40d92 | 2013-10-21 11:58:40 -0700 | [diff] [blame] | 3232 | |
| 3233 | mLastSetCountryCode = null; |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3234 | } |
| 3235 | } |
Irfan Sheriff | 7d6d9c0 | 2012-01-10 15:50:45 -0800 | [diff] [blame] | 3236 | |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3237 | class WaitForP2pDisableState extends State { |
| 3238 | private State mTransitionToState; |
| 3239 | @Override |
| 3240 | public void enter() { |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3241 | switch (getCurrentMessage().what) { |
| 3242 | case WifiMonitor.SUP_DISCONNECTION_EVENT: |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 3243 | mTransitionToState = mInitialState; |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3244 | break; |
| 3245 | case CMD_DELAYED_STOP_DRIVER: |
| 3246 | mTransitionToState = mDriverStoppingState; |
| 3247 | break; |
| 3248 | case CMD_STOP_SUPPLICANT: |
| 3249 | mTransitionToState = mSupplicantStoppingState; |
| 3250 | break; |
| 3251 | default: |
| 3252 | mTransitionToState = mDriverStoppingState; |
| 3253 | break; |
| 3254 | } |
| 3255 | mWifiP2pChannel.sendMessage(WifiStateMachine.CMD_DISABLE_P2P_REQ); |
| 3256 | } |
| 3257 | @Override |
| 3258 | public boolean processMessage(Message message) { |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3259 | switch(message.what) { |
| 3260 | case WifiStateMachine.CMD_DISABLE_P2P_RSP: |
| 3261 | transitionTo(mTransitionToState); |
| 3262 | break; |
| 3263 | /* Defer wifi start/shut and driver commands */ |
Irfan Sheriff | e6464af | 2012-10-23 10:22:13 -0700 | [diff] [blame] | 3264 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3265 | case CMD_START_SUPPLICANT: |
| 3266 | case CMD_STOP_SUPPLICANT: |
| 3267 | case CMD_START_AP: |
| 3268 | case CMD_STOP_AP: |
| 3269 | case CMD_START_DRIVER: |
| 3270 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3271 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | 62fa6de | 2012-10-18 12:39:22 -0700 | [diff] [blame] | 3272 | case CMD_SET_COUNTRY_CODE: |
| 3273 | case CMD_SET_FREQUENCY_BAND: |
| 3274 | case CMD_START_PACKET_FILTERING: |
| 3275 | case CMD_STOP_PACKET_FILTERING: |
| 3276 | case CMD_START_SCAN: |
| 3277 | case CMD_DISCONNECT: |
| 3278 | case CMD_REASSOCIATE: |
| 3279 | case CMD_RECONNECT: |
| 3280 | deferMessage(message); |
| 3281 | break; |
| 3282 | default: |
| 3283 | return NOT_HANDLED; |
| 3284 | } |
| 3285 | return HANDLED; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 3289 | class DriverStoppingState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3290 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3291 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3292 | switch(message.what) { |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 3293 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
Irfan Sheriff | 1523da2 | 2011-06-10 14:00:27 -0700 | [diff] [blame] | 3294 | SupplicantState state = handleSupplicantStateChange(message); |
| 3295 | if (state == SupplicantState.INTERFACE_DISABLED) { |
| 3296 | transitionTo(mDriverStoppedState); |
| 3297 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3298 | break; |
| 3299 | /* Queue driver commands */ |
| 3300 | case CMD_START_DRIVER: |
| 3301 | case CMD_STOP_DRIVER: |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 3302 | case CMD_SET_COUNTRY_CODE: |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 3303 | case CMD_SET_FREQUENCY_BAND: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3304 | case CMD_START_PACKET_FILTERING: |
| 3305 | case CMD_STOP_PACKET_FILTERING: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3306 | case CMD_START_SCAN: |
| 3307 | case CMD_DISCONNECT: |
| 3308 | case CMD_REASSOCIATE: |
| 3309 | case CMD_RECONNECT: |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 3310 | deferMessage(message); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3311 | break; |
| 3312 | default: |
| 3313 | return NOT_HANDLED; |
| 3314 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3315 | return HANDLED; |
| 3316 | } |
| 3317 | } |
| 3318 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 3319 | class DriverStoppedState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3320 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3321 | public boolean processMessage(Message message) { |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 3322 | switch (message.what) { |
Irfan Sheriff | 56379d2 | 2011-10-04 16:45:22 -0700 | [diff] [blame] | 3323 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
| 3324 | StateChangeResult stateChangeResult = (StateChangeResult) message.obj; |
| 3325 | SupplicantState state = stateChangeResult.state; |
| 3326 | // A WEXT bug means that we can be back to driver started state |
| 3327 | // unexpectedly |
| 3328 | if (SupplicantState.isDriverActive(state)) { |
| 3329 | transitionTo(mDriverStartedState); |
| 3330 | } |
| 3331 | break; |
Irfan Sheriff | 3ac127f | 2011-08-29 14:05:14 -0700 | [diff] [blame] | 3332 | case CMD_START_DRIVER: |
| 3333 | mWakeLock.acquire(); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3334 | mWifiNative.startDriver(); |
Irfan Sheriff | 3ac127f | 2011-08-29 14:05:14 -0700 | [diff] [blame] | 3335 | mWakeLock.release(); |
| 3336 | transitionTo(mDriverStartingState); |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 3337 | break; |
| 3338 | default: |
| 3339 | return NOT_HANDLED; |
| 3340 | } |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 3341 | return HANDLED; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 3345 | class ScanModeState extends State { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3346 | private int mLastOperationMode; |
| 3347 | @Override |
| 3348 | public void enter() { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3349 | mLastOperationMode = mOperationalMode; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3350 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3351 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3352 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3353 | switch(message.what) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3354 | case CMD_SET_OPERATIONAL_MODE: |
| 3355 | if (message.arg1 == CONNECT_MODE) { |
Vinit Deshapnde | 6029611 | 2013-09-24 17:38:09 -0700 | [diff] [blame] | 3356 | |
| 3357 | if (mLastOperationMode == SCAN_ONLY_WITH_WIFI_OFF_MODE) { |
| 3358 | setWifiState(WIFI_STATE_ENABLED); |
| 3359 | // Load and re-enable networks when going back to enabled state |
| 3360 | // This is essential for networks to show up after restore |
| 3361 | mWifiConfigStore.loadAndEnableAllNetworks(); |
| 3362 | mWifiP2pChannel.sendMessage(CMD_ENABLE_P2P); |
| 3363 | } else { |
| 3364 | mWifiConfigStore.enableAllNetworks(); |
| 3365 | } |
| 3366 | |
| 3367 | mWifiNative.reconnect(); |
| 3368 | |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3369 | mOperationalMode = CONNECT_MODE; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3370 | transitionTo(mDisconnectedState); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3371 | } else { |
| 3372 | // Nothing to do |
| 3373 | return HANDLED; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3374 | } |
| 3375 | break; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3376 | // Handle scan. All the connection related commands are |
| 3377 | // handled only in ConnectModeState |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 3378 | case CMD_START_SCAN: |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 3379 | noteScanStart(message.arg1, (WorkSource) message.obj); |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 3380 | startScanNative(WifiNative.SCAN_WITHOUT_CONNECTION_SETUP); |
| 3381 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3382 | default: |
| 3383 | return NOT_HANDLED; |
| 3384 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3385 | return HANDLED; |
| 3386 | } |
| 3387 | } |
| 3388 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 3389 | class ConnectModeState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3390 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3391 | public boolean processMessage(Message message) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3392 | WifiConfiguration config; |
| 3393 | boolean ok; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3394 | switch(message.what) { |
Deepthi Gowri | c1b631e | 2013-04-30 18:23:57 +0530 | [diff] [blame] | 3395 | case WifiMonitor.ASSOCIATION_REJECTION_EVENT: |
| 3396 | mSupplicantStateTracker.sendMessage(WifiMonitor.ASSOCIATION_REJECTION_EVENT); |
| 3397 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 3398 | case WifiMonitor.AUTHENTICATION_FAILURE_EVENT: |
| 3399 | mSupplicantStateTracker.sendMessage(WifiMonitor.AUTHENTICATION_FAILURE_EVENT); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3400 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 3401 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
Irfan Sheriff | 56379d2 | 2011-10-04 16:45:22 -0700 | [diff] [blame] | 3402 | SupplicantState state = handleSupplicantStateChange(message); |
Irfan Sheriff | b678320 | 2012-04-27 13:34:07 -0700 | [diff] [blame] | 3403 | // A driver/firmware hang can now put the interface in a down state. |
| 3404 | // We detect the interface going down and recover from it |
Irfan Sheriff | 56379d2 | 2011-10-04 16:45:22 -0700 | [diff] [blame] | 3405 | if (!SupplicantState.isDriverActive(state)) { |
| 3406 | if (mNetworkInfo.getState() != NetworkInfo.State.DISCONNECTED) { |
| 3407 | handleNetworkDisconnect(); |
| 3408 | } |
Irfan Sheriff | b678320 | 2012-04-27 13:34:07 -0700 | [diff] [blame] | 3409 | log("Detected an interface down, restart driver"); |
Irfan Sheriff | 56379d2 | 2011-10-04 16:45:22 -0700 | [diff] [blame] | 3410 | transitionTo(mDriverStoppedState); |
Irfan Sheriff | b678320 | 2012-04-27 13:34:07 -0700 | [diff] [blame] | 3411 | sendMessage(CMD_START_DRIVER); |
Irfan Sheriff | a2a1078 | 2011-10-17 13:29:52 -0700 | [diff] [blame] | 3412 | break; |
| 3413 | } |
| 3414 | |
| 3415 | // Supplicant can fail to report a NETWORK_DISCONNECTION_EVENT |
| 3416 | // when authentication times out after a successful connection, |
| 3417 | // we can figure this from the supplicant state. If supplicant |
| 3418 | // state is DISCONNECTED, but the mNetworkInfo says we are not |
| 3419 | // disconnected, we need to handle a disconnection |
| 3420 | if (state == SupplicantState.DISCONNECTED && |
| 3421 | mNetworkInfo.getState() != NetworkInfo.State.DISCONNECTED) { |
| 3422 | if (DBG) log("Missed CTRL-EVENT-DISCONNECTED, disconnect"); |
| 3423 | handleNetworkDisconnect(); |
| 3424 | transitionTo(mDisconnectedState); |
Irfan Sheriff | 56379d2 | 2011-10-04 16:45:22 -0700 | [diff] [blame] | 3425 | } |
| 3426 | break; |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 3427 | case WifiP2pService.DISCONNECT_WIFI_REQUEST: |
| 3428 | if (message.arg1 == 1) { |
| 3429 | mWifiNative.disconnect(); |
| 3430 | mTemporarilyDisconnectWifi = true; |
| 3431 | } else { |
| 3432 | mWifiNative.reconnect(); |
| 3433 | mTemporarilyDisconnectWifi = false; |
| 3434 | } |
| 3435 | break; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3436 | case CMD_ADD_OR_UPDATE_NETWORK: |
| 3437 | config = (WifiConfiguration) message.obj; |
| 3438 | replyToMessage(message, CMD_ADD_OR_UPDATE_NETWORK, |
| 3439 | mWifiConfigStore.addOrUpdateNetwork(config)); |
| 3440 | break; |
| 3441 | case CMD_REMOVE_NETWORK: |
| 3442 | ok = mWifiConfigStore.removeNetwork(message.arg1); |
| 3443 | replyToMessage(message, message.what, ok ? SUCCESS : FAILURE); |
| 3444 | break; |
| 3445 | case CMD_ENABLE_NETWORK: |
| 3446 | ok = mWifiConfigStore.enableNetwork(message.arg1, message.arg2 == 1); |
| 3447 | replyToMessage(message, message.what, ok ? SUCCESS : FAILURE); |
| 3448 | break; |
| 3449 | case CMD_ENABLE_ALL_NETWORKS: |
| 3450 | long time = android.os.SystemClock.elapsedRealtime(); |
| 3451 | if (time - mLastEnableAllNetworksTime > MIN_INTERVAL_ENABLE_ALL_NETWORKS_MS) { |
| 3452 | mWifiConfigStore.enableAllNetworks(); |
| 3453 | mLastEnableAllNetworksTime = time; |
| 3454 | } |
| 3455 | break; |
| 3456 | case WifiManager.DISABLE_NETWORK: |
| 3457 | if (mWifiConfigStore.disableNetwork(message.arg1, |
| 3458 | WifiConfiguration.DISABLED_UNKNOWN_REASON) == true) { |
| 3459 | replyToMessage(message, WifiManager.DISABLE_NETWORK_SUCCEEDED); |
| 3460 | } else { |
| 3461 | replyToMessage(message, WifiManager.DISABLE_NETWORK_FAILED, |
| 3462 | WifiManager.ERROR); |
| 3463 | } |
| 3464 | break; |
| 3465 | case CMD_BLACKLIST_NETWORK: |
| 3466 | mWifiNative.addToBlacklist((String)message.obj); |
| 3467 | break; |
| 3468 | case CMD_CLEAR_BLACKLIST: |
| 3469 | mWifiNative.clearBlacklist(); |
| 3470 | break; |
| 3471 | case CMD_SAVE_CONFIG: |
| 3472 | ok = mWifiConfigStore.saveConfig(); |
| 3473 | replyToMessage(message, CMD_SAVE_CONFIG, ok ? SUCCESS : FAILURE); |
| 3474 | |
| 3475 | // Inform the backup manager about a data change |
| 3476 | IBackupManager ibm = IBackupManager.Stub.asInterface( |
| 3477 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3478 | if (ibm != null) { |
| 3479 | try { |
| 3480 | ibm.dataChanged("com.android.providers.settings"); |
| 3481 | } catch (Exception e) { |
| 3482 | // Try again later |
| 3483 | } |
| 3484 | } |
| 3485 | break; |
| 3486 | case CMD_GET_CONFIGURED_NETWORKS: |
| 3487 | replyToMessage(message, message.what, |
| 3488 | mWifiConfigStore.getConfiguredNetworks()); |
| 3489 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3490 | /* Do a redundant disconnect without transition */ |
| 3491 | case CMD_DISCONNECT: |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3492 | mWifiNative.disconnect(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3493 | break; |
| 3494 | case CMD_RECONNECT: |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3495 | mWifiNative.reconnect(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3496 | break; |
| 3497 | case CMD_REASSOCIATE: |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3498 | mWifiNative.reassociate(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3499 | break; |
Vinit Deshapnde | a3038b2 | 2013-10-02 17:26:05 -0700 | [diff] [blame] | 3500 | case CMD_RELOAD_TLS_AND_RECONNECT: |
| 3501 | if (mWifiConfigStore.needsUnlockedKeyStore()) { |
| 3502 | logd("Reconnecting to give a chance to un-connected TLS networks"); |
| 3503 | mWifiNative.disconnect(); |
| 3504 | mWifiNative.reconnect(); |
| 3505 | } |
| 3506 | break; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3507 | case WifiManager.CONNECT_NETWORK: |
Irfan Sheriff | b3e96c5 | 2012-04-27 20:37:36 -0700 | [diff] [blame] | 3508 | /* The connect message can contain a network id passed as arg1 on message or |
| 3509 | * or a config passed as obj on message. |
| 3510 | * For a new network, a config is passed to create and connect. |
| 3511 | * For an existing network, a network id is passed |
| 3512 | */ |
Irfan Sheriff | e04653c | 2010-08-09 09:09:59 -0700 | [diff] [blame] | 3513 | int netId = message.arg1; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3514 | config = (WifiConfiguration) message.obj; |
Irfan Sheriff | e04653c | 2010-08-09 09:09:59 -0700 | [diff] [blame] | 3515 | |
Irfan Sheriff | b3e96c5 | 2012-04-27 20:37:36 -0700 | [diff] [blame] | 3516 | /* Save the network config */ |
Irfan Sheriff | 04db1d5 | 2010-08-30 20:29:10 -0700 | [diff] [blame] | 3517 | if (config != null) { |
Irfan Sheriff | b3e96c5 | 2012-04-27 20:37:36 -0700 | [diff] [blame] | 3518 | NetworkUpdateResult result = mWifiConfigStore.saveNetwork(config); |
| 3519 | netId = result.getNetworkId(); |
Irfan Sheriff | 04db1d5 | 2010-08-30 20:29:10 -0700 | [diff] [blame] | 3520 | } |
| 3521 | |
Irfan Sheriff | b3e96c5 | 2012-04-27 20:37:36 -0700 | [diff] [blame] | 3522 | if (mWifiConfigStore.selectNetwork(netId) && |
| 3523 | mWifiNative.reconnect()) { |
| 3524 | /* The state tracker handles enabling networks upon completion/failure */ |
| 3525 | mSupplicantStateTracker.sendMessage(WifiManager.CONNECT_NETWORK); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3526 | replyToMessage(message, WifiManager.CONNECT_NETWORK_SUCCEEDED); |
Irfan Sheriff | b3e96c5 | 2012-04-27 20:37:36 -0700 | [diff] [blame] | 3527 | /* Expect a disconnection from the old connection */ |
| 3528 | transitionTo(mDisconnectingState); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3529 | } else { |
Irfan Sheriff | b3e96c5 | 2012-04-27 20:37:36 -0700 | [diff] [blame] | 3530 | loge("Failed to connect config: " + config + " netId: " + netId); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3531 | replyToMessage(message, WifiManager.CONNECT_NETWORK_FAILED, |
| 3532 | WifiManager.ERROR); |
Irfan Sheriff | b3e96c5 | 2012-04-27 20:37:36 -0700 | [diff] [blame] | 3533 | break; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3534 | } |
Irfan Sheriff | e04653c | 2010-08-09 09:09:59 -0700 | [diff] [blame] | 3535 | break; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3536 | case WifiManager.SAVE_NETWORK: |
| 3537 | config = (WifiConfiguration) message.obj; |
| 3538 | NetworkUpdateResult result = mWifiConfigStore.saveNetwork(config); |
| 3539 | if (result.getNetworkId() != WifiConfiguration.INVALID_NETWORK_ID) { |
| 3540 | replyToMessage(message, WifiManager.SAVE_NETWORK_SUCCEEDED); |
| 3541 | } else { |
| 3542 | loge("Failed to save network"); |
| 3543 | replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, |
| 3544 | WifiManager.ERROR); |
| 3545 | } |
| 3546 | break; |
| 3547 | case WifiManager.FORGET_NETWORK: |
| 3548 | if (mWifiConfigStore.forgetNetwork(message.arg1)) { |
| 3549 | replyToMessage(message, WifiManager.FORGET_NETWORK_SUCCEEDED); |
| 3550 | } else { |
| 3551 | loge("Failed to forget network"); |
| 3552 | replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED, |
| 3553 | WifiManager.ERROR); |
| 3554 | } |
| 3555 | break; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3556 | case WifiManager.START_WPS: |
| 3557 | WpsInfo wpsInfo = (WpsInfo) message.obj; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3558 | WpsResult wpsResult; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3559 | switch (wpsInfo.setup) { |
| 3560 | case WpsInfo.PBC: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3561 | wpsResult = mWifiConfigStore.startWpsPbc(wpsInfo); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3562 | break; |
| 3563 | case WpsInfo.KEYPAD: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3564 | wpsResult = mWifiConfigStore.startWpsWithPinFromAccessPoint(wpsInfo); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3565 | break; |
| 3566 | case WpsInfo.DISPLAY: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3567 | wpsResult = mWifiConfigStore.startWpsWithPinFromDevice(wpsInfo); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3568 | break; |
| 3569 | default: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3570 | wpsResult = new WpsResult(Status.FAILURE); |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 3571 | loge("Invalid setup for WPS"); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3572 | break; |
| 3573 | } |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3574 | if (wpsResult.status == Status.SUCCESS) { |
| 3575 | replyToMessage(message, WifiManager.START_WPS_SUCCEEDED, wpsResult); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3576 | transitionTo(mWpsRunningState); |
| 3577 | } else { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 3578 | loge("Failed to start WPS with config " + wpsInfo.toString()); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3579 | replyToMessage(message, WifiManager.WPS_FAILED, WifiManager.ERROR); |
| 3580 | } |
Irfan Sheriff | f235c5a | 2010-10-21 16:44:48 -0700 | [diff] [blame] | 3581 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 3582 | case WifiMonitor.NETWORK_CONNECTION_EVENT: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3583 | if (DBG) log("Network connection established"); |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 3584 | mLastNetworkId = message.arg1; |
| 3585 | mLastBssid = (String) message.obj; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3586 | |
Irfan Sheriff | b45e726 | 2010-12-21 09:44:15 -0800 | [diff] [blame] | 3587 | mWifiInfo.setBSSID(mLastBssid); |
| 3588 | mWifiInfo.setNetworkId(mLastNetworkId); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3589 | /* send event to CM & network change broadcast */ |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 3590 | setNetworkDetailedState(DetailedState.OBTAINING_IPADDR); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3591 | sendNetworkStateChangeBroadcast(mLastBssid); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3592 | transitionTo(mObtainingIpState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3593 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 3594 | case WifiMonitor.NETWORK_DISCONNECTION_EVENT: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3595 | if (DBG) log("Network connection lost"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3596 | handleNetworkDisconnect(); |
| 3597 | transitionTo(mDisconnectedState); |
| 3598 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3599 | default: |
| 3600 | return NOT_HANDLED; |
| 3601 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3602 | return HANDLED; |
| 3603 | } |
| 3604 | } |
| 3605 | |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3606 | class L2ConnectedState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3607 | @Override |
| 3608 | public void enter() { |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3609 | mRssiPollToken++; |
| 3610 | if (mEnableRssiPolling) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3611 | sendMessage(CMD_RSSI_POLL, mRssiPollToken, 0); |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3612 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3613 | } |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3614 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3615 | @Override |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 3616 | public void exit() { |
Yuhao Zheng | a9e6be8 | 2013-10-08 17:44:05 -0700 | [diff] [blame] | 3617 | handleNetworkDisconnect(); |
Robert Greenwalt | 81fec6b | 2013-09-19 17:30:19 -0700 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3621 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3622 | switch (message.what) { |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 3623 | case DhcpStateMachine.CMD_PRE_DHCP_ACTION: |
| 3624 | handlePreDhcpSetup(); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 3625 | break; |
| 3626 | case DhcpStateMachine.CMD_POST_DHCP_ACTION: |
| 3627 | handlePostDhcpSetup(); |
| 3628 | if (message.arg1 == DhcpStateMachine.DHCP_SUCCESS) { |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3629 | if (DBG) log("DHCP successful"); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 3630 | handleSuccessfulIpConfiguration((DhcpResults) message.obj); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3631 | transitionTo(mVerifyingLinkState); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 3632 | } else if (message.arg1 == DhcpStateMachine.DHCP_FAILURE) { |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3633 | if (DBG) log("DHCP failed"); |
Irfan Sheriff | 31be7cf | 2011-04-28 10:25:31 -0700 | [diff] [blame] | 3634 | handleFailedIpConfiguration(); |
| 3635 | transitionTo(mDisconnectingState); |
| 3636 | } |
| 3637 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3638 | case CMD_DISCONNECT: |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3639 | mWifiNative.disconnect(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3640 | transitionTo(mDisconnectingState); |
| 3641 | break; |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 3642 | case WifiP2pService.DISCONNECT_WIFI_REQUEST: |
| 3643 | if (message.arg1 == 1) { |
| 3644 | mWifiNative.disconnect(); |
| 3645 | mTemporarilyDisconnectWifi = true; |
| 3646 | transitionTo(mDisconnectingState); |
| 3647 | } |
| 3648 | break; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3649 | case CMD_SET_OPERATIONAL_MODE: |
| 3650 | if (message.arg1 != CONNECT_MODE) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3651 | sendMessage(CMD_DISCONNECT); |
| 3652 | deferMessage(message); |
| 3653 | } |
| 3654 | break; |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3655 | case CMD_START_SCAN: |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 3656 | /* Do not attempt to connect when we are already connected */ |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 3657 | noteScanStart(message.arg1, (WorkSource) message.obj); |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 3658 | startScanNative(WifiNative.SCAN_WITHOUT_CONNECTION_SETUP); |
| 3659 | break; |
Irfan Sheriff | a2d5fbf | 2010-08-24 14:26:54 -0700 | [diff] [blame] | 3660 | /* Ignore connection to same network */ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3661 | case WifiManager.CONNECT_NETWORK: |
Irfan Sheriff | a2d5fbf | 2010-08-24 14:26:54 -0700 | [diff] [blame] | 3662 | int netId = message.arg1; |
| 3663 | if (mWifiInfo.getNetworkId() == netId) { |
| 3664 | break; |
| 3665 | } |
| 3666 | return NOT_HANDLED; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3667 | case WifiManager.SAVE_NETWORK: |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 3668 | WifiConfiguration config = (WifiConfiguration) message.obj; |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3669 | NetworkUpdateResult result = mWifiConfigStore.saveNetwork(config); |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 3670 | if (mWifiInfo.getNetworkId() == result.getNetworkId()) { |
| 3671 | if (result.hasIpChanged()) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3672 | log("Reconfiguring IP on connection"); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3673 | transitionTo(mObtainingIpState); |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 3674 | } |
| 3675 | if (result.hasProxyChanged()) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 3676 | log("Reconfiguring proxy on connection"); |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 3677 | updateLinkProperties(); |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 3678 | } |
| 3679 | } |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3680 | |
| 3681 | if (result.getNetworkId() != WifiConfiguration.INVALID_NETWORK_ID) { |
| 3682 | replyToMessage(message, WifiManager.SAVE_NETWORK_SUCCEEDED); |
| 3683 | } else { |
| 3684 | loge("Failed to save network"); |
| 3685 | replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, |
| 3686 | WifiManager.ERROR); |
| 3687 | } |
Irfan Sheriff | be9ee6a | 2010-12-01 16:13:16 -0800 | [diff] [blame] | 3688 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3689 | /* Ignore */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 3690 | case WifiMonitor.NETWORK_CONNECTION_EVENT: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3691 | break; |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3692 | case CMD_RSSI_POLL: |
| 3693 | if (message.arg1 == mRssiPollToken) { |
| 3694 | // Get Info and continue polling |
| 3695 | fetchRssiAndLinkSpeedNative(); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3696 | sendMessageDelayed(obtainMessage(CMD_RSSI_POLL, |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3697 | mRssiPollToken, 0), POLL_RSSI_INTERVAL_MSECS); |
| 3698 | } else { |
| 3699 | // Polling has completed |
| 3700 | } |
| 3701 | break; |
| 3702 | case CMD_ENABLE_RSSI_POLL: |
| 3703 | mEnableRssiPolling = (message.arg1 == 1); |
| 3704 | mRssiPollToken++; |
| 3705 | if (mEnableRssiPolling) { |
| 3706 | // first poll |
| 3707 | fetchRssiAndLinkSpeedNative(); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3708 | sendMessageDelayed(obtainMessage(CMD_RSSI_POLL, |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3709 | mRssiPollToken, 0), POLL_RSSI_INTERVAL_MSECS); |
| 3710 | } |
| 3711 | break; |
Yuhao Zheng | f630782 | 2012-08-14 14:21:25 -0700 | [diff] [blame] | 3712 | case WifiManager.RSSI_PKTCNT_FETCH: |
| 3713 | RssiPacketCountInfo info = new RssiPacketCountInfo(); |
Irfan Sheriff | 4c8982a | 2012-03-08 16:11:02 -0800 | [diff] [blame] | 3714 | fetchRssiAndLinkSpeedNative(); |
Yuhao Zheng | f630782 | 2012-08-14 14:21:25 -0700 | [diff] [blame] | 3715 | info.rssi = mWifiInfo.getRssi(); |
| 3716 | fetchPktcntNative(info); |
| 3717 | replyToMessage(message, WifiManager.RSSI_PKTCNT_FETCH_SUCCEEDED, info); |
Irfan Sheriff | 4c8982a | 2012-03-08 16:11:02 -0800 | [diff] [blame] | 3718 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3719 | default: |
| 3720 | return NOT_HANDLED; |
| 3721 | } |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3722 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3723 | return HANDLED; |
| 3724 | } |
| 3725 | } |
| 3726 | |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3727 | class ObtainingIpState extends State { |
| 3728 | @Override |
| 3729 | public void enter() { |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3730 | if (!mWifiConfigStore.isUsingStaticIp(mLastNetworkId)) { |
Lorenzo Colitti | 34b2b9b | 2013-08-08 19:38:37 +0900 | [diff] [blame] | 3731 | // TODO: If we're switching between static IP configuration and DHCP, remove the |
| 3732 | // static configuration first. |
Irfan Sheriff | 51e4ece | 2013-03-28 08:54:09 -0700 | [diff] [blame] | 3733 | startDhcp(); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3734 | } else { |
Irfan Sheriff | 51e4ece | 2013-03-28 08:54:09 -0700 | [diff] [blame] | 3735 | // stop any running dhcp before assigning static IP |
| 3736 | stopDhcp(); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 3737 | DhcpResults dhcpResults = new DhcpResults( |
| 3738 | mWifiConfigStore.getLinkProperties(mLastNetworkId)); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3739 | InterfaceConfiguration ifcg = new InterfaceConfiguration(); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 3740 | Iterator<LinkAddress> addrs = |
| 3741 | dhcpResults.linkProperties.getLinkAddresses().iterator(); |
| 3742 | if (!addrs.hasNext()) { |
| 3743 | loge("Static IP lacks address"); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3744 | sendMessage(CMD_STATIC_IP_FAILURE); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 3745 | } else { |
| 3746 | ifcg.setLinkAddress(addrs.next()); |
| 3747 | ifcg.setInterfaceUp(); |
| 3748 | try { |
| 3749 | mNwService.setInterfaceConfig(mInterfaceName, ifcg); |
| 3750 | if (DBG) log("Static IP configuration succeeded"); |
| 3751 | sendMessage(CMD_STATIC_IP_SUCCESS, dhcpResults); |
| 3752 | } catch (RemoteException re) { |
| 3753 | loge("Static IP configuration failed: " + re); |
| 3754 | sendMessage(CMD_STATIC_IP_FAILURE); |
| 3755 | } catch (IllegalStateException e) { |
| 3756 | loge("Static IP configuration failed: " + e); |
| 3757 | sendMessage(CMD_STATIC_IP_FAILURE); |
| 3758 | } |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3759 | } |
| 3760 | } |
| 3761 | } |
| 3762 | @Override |
| 3763 | public boolean processMessage(Message message) { |
| 3764 | if (DBG) log(getName() + message.toString() + "\n"); |
| 3765 | switch(message.what) { |
| 3766 | case CMD_STATIC_IP_SUCCESS: |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 3767 | handleSuccessfulIpConfiguration((DhcpResults) message.obj); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3768 | transitionTo(mVerifyingLinkState); |
| 3769 | break; |
| 3770 | case CMD_STATIC_IP_FAILURE: |
| 3771 | handleFailedIpConfiguration(); |
| 3772 | transitionTo(mDisconnectingState); |
| 3773 | break; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 3774 | case WifiManager.SAVE_NETWORK: |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3775 | deferMessage(message); |
| 3776 | break; |
| 3777 | /* Defer any power mode changes since we must keep active power mode at DHCP */ |
| 3778 | case CMD_SET_HIGH_PERF_MODE: |
| 3779 | deferMessage(message); |
| 3780 | break; |
Vitaly Wool | d7fc38c | 2012-09-04 15:03:13 +0200 | [diff] [blame] | 3781 | /* Defer scan request since we should not switch to other channels at DHCP */ |
| 3782 | case CMD_START_SCAN: |
| 3783 | deferMessage(message); |
| 3784 | break; |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3785 | default: |
| 3786 | return NOT_HANDLED; |
| 3787 | } |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3788 | return HANDLED; |
| 3789 | } |
| 3790 | } |
| 3791 | |
| 3792 | class VerifyingLinkState extends State { |
| 3793 | @Override |
| 3794 | public void enter() { |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 3795 | log(getName() + " enter"); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3796 | setNetworkDetailedState(DetailedState.VERIFYING_POOR_LINK); |
| 3797 | mWifiConfigStore.updateStatus(mLastNetworkId, DetailedState.VERIFYING_POOR_LINK); |
| 3798 | sendNetworkStateChangeBroadcast(mLastBssid); |
| 3799 | } |
| 3800 | @Override |
| 3801 | public boolean processMessage(Message message) { |
| 3802 | switch (message.what) { |
| 3803 | case WifiWatchdogStateMachine.POOR_LINK_DETECTED: |
| 3804 | //stay here |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 3805 | log(getName() + " POOR_LINK_DETECTED: no transition"); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3806 | break; |
| 3807 | case WifiWatchdogStateMachine.GOOD_LINK_DETECTED: |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 3808 | log(getName() + " GOOD_LINK_DETECTED: transition to captive portal check"); |
Narayan Kamath | 98e1797 | 2013-08-29 13:40:43 +0100 | [diff] [blame] | 3809 | // Send out a broadcast with the CAPTIVE_PORTAL_CHECK to preserve |
| 3810 | // existing behaviour. The captive portal check really happens after we |
| 3811 | // transition into DetailedState.CONNECTED. |
| 3812 | setNetworkDetailedState(DetailedState.CAPTIVE_PORTAL_CHECK); |
| 3813 | mWifiConfigStore.updateStatus(mLastNetworkId, |
| 3814 | DetailedState.CAPTIVE_PORTAL_CHECK); |
| 3815 | sendNetworkStateChangeBroadcast(mLastBssid); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 3816 | |
Narayan Kamath | 98e1797 | 2013-08-29 13:40:43 +0100 | [diff] [blame] | 3817 | // NOTE: This might look like an odd place to enable IPV6 but this is in |
| 3818 | // response to transitioning into GOOD_LINK_DETECTED. Similarly, we disable |
| 3819 | // ipv6 when we transition into POOR_LINK_DETECTED in mConnectedState. |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3820 | try { |
| 3821 | mNwService.enableIpv6(mInterfaceName); |
| 3822 | } catch (RemoteException re) { |
| 3823 | loge("Failed to enable IPv6: " + re); |
| 3824 | } catch (IllegalStateException e) { |
| 3825 | loge("Failed to enable IPv6: " + e); |
| 3826 | } |
Narayan Kamath | 98e1797 | 2013-08-29 13:40:43 +0100 | [diff] [blame] | 3827 | |
| 3828 | log(getName() + " GOOD_LINK_DETECTED: transition to CONNECTED"); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3829 | setNetworkDetailedState(DetailedState.CONNECTED); |
| 3830 | mWifiConfigStore.updateStatus(mLastNetworkId, DetailedState.CONNECTED); |
| 3831 | sendNetworkStateChangeBroadcast(mLastBssid); |
| 3832 | transitionTo(mConnectedState); |
Narayan Kamath | 98e1797 | 2013-08-29 13:40:43 +0100 | [diff] [blame] | 3833 | |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3834 | break; |
| 3835 | default: |
Narayan Kamath | 98e1797 | 2013-08-29 13:40:43 +0100 | [diff] [blame] | 3836 | if (DBG) log(getName() + " what=" + message.what + " NOT_HANDLED"); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3837 | return NOT_HANDLED; |
| 3838 | } |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3839 | return HANDLED; |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | class ConnectedState extends State { |
| 3844 | @Override |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3845 | public boolean processMessage(Message message) { |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3846 | switch (message.what) { |
| 3847 | case WifiWatchdogStateMachine.POOR_LINK_DETECTED: |
| 3848 | if (DBG) log("Watchdog reports poor link"); |
| 3849 | try { |
| 3850 | mNwService.disableIpv6(mInterfaceName); |
| 3851 | } catch (RemoteException re) { |
| 3852 | loge("Failed to disable IPv6: " + re); |
| 3853 | } catch (IllegalStateException e) { |
| 3854 | loge("Failed to disable IPv6: " + e); |
| 3855 | } |
| 3856 | /* Report a disconnect */ |
| 3857 | setNetworkDetailedState(DetailedState.DISCONNECTED); |
| 3858 | mWifiConfigStore.updateStatus(mLastNetworkId, DetailedState.DISCONNECTED); |
| 3859 | sendNetworkStateChangeBroadcast(mLastBssid); |
| 3860 | |
| 3861 | transitionTo(mVerifyingLinkState); |
| 3862 | break; |
| 3863 | default: |
| 3864 | return NOT_HANDLED; |
| 3865 | } |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3866 | return HANDLED; |
| 3867 | } |
Narayan Kamath | 98e1797 | 2013-08-29 13:40:43 +0100 | [diff] [blame] | 3868 | |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3869 | @Override |
| 3870 | public void exit() { |
| 3871 | /* Request a CS wakelock during transition to mobile */ |
| 3872 | checkAndSetConnectivityInstance(); |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 3873 | mCm.requestNetworkTransitionWakelock(getName()); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 3874 | } |
| 3875 | } |
| 3876 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 3877 | class DisconnectingState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3878 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3879 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3880 | switch (message.what) { |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3881 | case CMD_SET_OPERATIONAL_MODE: |
| 3882 | if (message.arg1 != CONNECT_MODE) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3883 | deferMessage(message); |
| 3884 | } |
| 3885 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 3886 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
Irfan Sheriff | 2d57d86 | 2011-10-20 14:38:54 -0700 | [diff] [blame] | 3887 | /* If we get a SUPPLICANT_STATE_CHANGE_EVENT before NETWORK_DISCONNECTION_EVENT |
| 3888 | * we have missed the network disconnection, transition to mDisconnectedState |
| 3889 | * and handle the rest of the events there |
| 3890 | */ |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3891 | deferMessage(message); |
Irfan Sheriff | 2d57d86 | 2011-10-20 14:38:54 -0700 | [diff] [blame] | 3892 | handleNetworkDisconnect(); |
| 3893 | transitionTo(mDisconnectedState); |
Irfan Sheriff | 19d245b79 | 2010-11-11 16:40:06 -0800 | [diff] [blame] | 3894 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3895 | default: |
| 3896 | return NOT_HANDLED; |
| 3897 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3898 | return HANDLED; |
| 3899 | } |
| 3900 | } |
| 3901 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 3902 | class DisconnectedState extends State { |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 3903 | private boolean mAlarmEnabled = false; |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 3904 | /* This is set from the overlay config file or from a secure setting. |
| 3905 | * A value of 0 disables scanning in the framework. |
| 3906 | */ |
| 3907 | private long mFrameworkScanIntervalMs; |
Irfan Sheriff | f9e2a49 | 2011-02-28 12:08:11 -0800 | [diff] [blame] | 3908 | |
| 3909 | private void setScanAlarm(boolean enabled) { |
| 3910 | if (enabled == mAlarmEnabled) return; |
| 3911 | if (enabled) { |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 3912 | if (mFrameworkScanIntervalMs > 0) { |
| 3913 | mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, |
| 3914 | System.currentTimeMillis() + mFrameworkScanIntervalMs, |
| 3915 | mFrameworkScanIntervalMs, |
| 3916 | mScanIntent); |
| 3917 | mAlarmEnabled = true; |
| 3918 | } |
Irfan Sheriff | f9e2a49 | 2011-02-28 12:08:11 -0800 | [diff] [blame] | 3919 | } else { |
| 3920 | mAlarmManager.cancel(mScanIntent); |
| 3921 | mAlarmEnabled = false; |
| 3922 | } |
| 3923 | } |
| 3924 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3925 | @Override |
| 3926 | public void enter() { |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 3927 | // We dont scan frequently if this is a temporary disconnect |
| 3928 | // due to p2p |
| 3929 | if (mTemporarilyDisconnectWifi) { |
| 3930 | mWifiP2pChannel.sendMessage(WifiP2pService.DISCONNECT_WIFI_RESPONSE); |
| 3931 | return; |
| 3932 | } |
| 3933 | |
Christopher Tate | 6f5a9a9 | 2012-09-14 17:24:28 -0700 | [diff] [blame] | 3934 | mFrameworkScanIntervalMs = Settings.Global.getLong(mContext.getContentResolver(), |
| 3935 | Settings.Global.WIFI_FRAMEWORK_SCAN_INTERVAL_MS, |
Irfan Sheriff | 2b7f638 | 2011-03-25 14:29:19 -0700 | [diff] [blame] | 3936 | mDefaultFrameworkScanIntervalMs); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 3937 | /* |
| 3938 | * We initiate background scanning if it is enabled, otherwise we |
| 3939 | * initiate an infrequent scan that wakes up the device to ensure |
| 3940 | * a user connects to an access point on the move |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 3941 | */ |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 3942 | if (mEnableBackgroundScan) { |
| 3943 | /* If a regular scan result is pending, do not initiate background |
| 3944 | * scan until the scan results are returned. This is needed because |
| 3945 | * initiating a background scan will cancel the regular scan and |
| 3946 | * scan results will not be returned until background scanning is |
| 3947 | * cleared |
| 3948 | */ |
| 3949 | if (!mScanResultIsPending) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 3950 | mWifiNative.enableBackgroundScan(true); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 3951 | } |
| 3952 | } else { |
Irfan Sheriff | f9e2a49 | 2011-02-28 12:08:11 -0800 | [diff] [blame] | 3953 | setScanAlarm(true); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 3954 | } |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 3955 | |
| 3956 | /** |
| 3957 | * If we have no networks saved, the supplicant stops doing the periodic scan. |
| 3958 | * The scans are useful to notify the user of the presence of an open network. |
| 3959 | * Note that these are not wake up scans. |
| 3960 | */ |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 3961 | if (!mP2pConnected.get() && mWifiConfigStore.getConfiguredNetworks().size() == 0) { |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 3962 | sendMessageDelayed(obtainMessage(CMD_NO_NETWORKS_PERIODIC_SCAN, |
| 3963 | ++mPeriodicScanToken, 0), mSupplicantScanIntervalMs); |
| 3964 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3965 | } |
| 3966 | @Override |
| 3967 | public boolean processMessage(Message message) { |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 3968 | boolean ret = HANDLED; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3969 | switch (message.what) { |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 3970 | case CMD_NO_NETWORKS_PERIODIC_SCAN: |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 3971 | if (mP2pConnected.get()) break; |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 3972 | if (message.arg1 == mPeriodicScanToken && |
| 3973 | mWifiConfigStore.getConfiguredNetworks().size() == 0) { |
David Christie | 6942a12 | 2013-07-25 15:25:07 -0700 | [diff] [blame] | 3974 | sendMessage(CMD_START_SCAN, UNKNOWN_SCAN_SOURCE, 0, (WorkSource) null); |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 3975 | sendMessageDelayed(obtainMessage(CMD_NO_NETWORKS_PERIODIC_SCAN, |
| 3976 | ++mPeriodicScanToken, 0), mSupplicantScanIntervalMs); |
| 3977 | } |
| 3978 | break; |
| 3979 | case WifiManager.FORGET_NETWORK: |
| 3980 | case CMD_REMOVE_NETWORK: |
| 3981 | // Set up a delayed message here. After the forget/remove is handled |
| 3982 | // the handled delayed message will determine if there is a need to |
| 3983 | // scan and continue |
| 3984 | sendMessageDelayed(obtainMessage(CMD_NO_NETWORKS_PERIODIC_SCAN, |
| 3985 | ++mPeriodicScanToken, 0), mSupplicantScanIntervalMs); |
| 3986 | ret = NOT_HANDLED; |
| 3987 | break; |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 3988 | case CMD_SET_OPERATIONAL_MODE: |
| 3989 | if (message.arg1 != CONNECT_MODE) { |
| 3990 | mOperationalMode = message.arg1; |
Vinit Deshapnde | 6029611 | 2013-09-24 17:38:09 -0700 | [diff] [blame] | 3991 | |
| 3992 | mWifiConfigStore.disableAllNetworks(); |
| 3993 | if (mOperationalMode == SCAN_ONLY_WITH_WIFI_OFF_MODE) { |
| 3994 | mWifiP2pChannel.sendMessage(CMD_DISABLE_P2P_REQ); |
| 3995 | setWifiState(WIFI_STATE_DISABLED); |
| 3996 | } |
| 3997 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 3998 | transitionTo(mScanModeState); |
| 3999 | } |
| 4000 | break; |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4001 | case CMD_ENABLE_BACKGROUND_SCAN: |
| 4002 | mEnableBackgroundScan = (message.arg1 == 1); |
Irfan Sheriff | f9e2a49 | 2011-02-28 12:08:11 -0800 | [diff] [blame] | 4003 | if (mEnableBackgroundScan) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 4004 | mWifiNative.enableBackgroundScan(true); |
Irfan Sheriff | f9e2a49 | 2011-02-28 12:08:11 -0800 | [diff] [blame] | 4005 | setScanAlarm(false); |
| 4006 | } else { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 4007 | mWifiNative.enableBackgroundScan(false); |
Irfan Sheriff | f9e2a49 | 2011-02-28 12:08:11 -0800 | [diff] [blame] | 4008 | setScanAlarm(true); |
| 4009 | } |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4010 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4011 | /* Ignore network disconnect */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 4012 | case WifiMonitor.NETWORK_DISCONNECTION_EVENT: |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4013 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 4014 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
Irfan Sheriff | e1f9064 | 2011-06-16 14:40:48 -0700 | [diff] [blame] | 4015 | StateChangeResult stateChangeResult = (StateChangeResult) message.obj; |
| 4016 | setNetworkDetailedState(WifiInfo.getDetailedStateOf(stateChangeResult.state)); |
| 4017 | /* ConnectModeState does the rest of the handling */ |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 4018 | ret = NOT_HANDLED; |
| 4019 | break; |
Irfan Sheriff | e1f9064 | 2011-06-16 14:40:48 -0700 | [diff] [blame] | 4020 | case CMD_START_SCAN: |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4021 | /* Disable background scan temporarily during a regular scan */ |
| 4022 | if (mEnableBackgroundScan) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 4023 | mWifiNative.enableBackgroundScan(false); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4024 | } |
| 4025 | /* Handled in parent state */ |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 4026 | ret = NOT_HANDLED; |
| 4027 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 4028 | case WifiMonitor.SCAN_RESULTS_EVENT: |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4029 | /* Re-enable background scan when a pending scan result is received */ |
| 4030 | if (mEnableBackgroundScan && mScanResultIsPending) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 4031 | mWifiNative.enableBackgroundScan(true); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4032 | } |
| 4033 | /* Handled in parent state */ |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 4034 | ret = NOT_HANDLED; |
| 4035 | break; |
Irfan Sheriff | 3809f50 | 2012-09-17 16:04:57 -0700 | [diff] [blame] | 4036 | case WifiP2pService.P2P_CONNECTION_CHANGED: |
| 4037 | NetworkInfo info = (NetworkInfo) message.obj; |
| 4038 | mP2pConnected.set(info.isConnected()); |
| 4039 | if (mP2pConnected.get()) { |
| 4040 | int defaultInterval = mContext.getResources().getInteger( |
| 4041 | R.integer.config_wifi_scan_interval_p2p_connected); |
| 4042 | long scanIntervalMs = Settings.Global.getLong(mContext.getContentResolver(), |
| 4043 | Settings.Global.WIFI_SCAN_INTERVAL_WHEN_P2P_CONNECTED_MS, |
| 4044 | defaultInterval); |
| 4045 | mWifiNative.setScanInterval((int) scanIntervalMs/1000); |
| 4046 | } else if (mWifiConfigStore.getConfiguredNetworks().size() == 0) { |
| 4047 | if (DBG) log("Turn on scanning after p2p disconnected"); |
| 4048 | sendMessageDelayed(obtainMessage(CMD_NO_NETWORKS_PERIODIC_SCAN, |
| 4049 | ++mPeriodicScanToken, 0), mSupplicantScanIntervalMs); |
| 4050 | } |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 4051 | case CMD_RECONNECT: |
| 4052 | case CMD_REASSOCIATE: |
Irfan Sheriff | accec86 | 2013-03-11 08:46:06 -0700 | [diff] [blame] | 4053 | if (mTemporarilyDisconnectWifi) { |
| 4054 | // Drop a third party reconnect/reassociate if STA is |
| 4055 | // temporarily disconnected for p2p |
| 4056 | break; |
| 4057 | } else { |
| 4058 | // ConnectModeState handles it |
| 4059 | ret = NOT_HANDLED; |
| 4060 | } |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 4061 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4062 | default: |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 4063 | ret = NOT_HANDLED; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4064 | } |
Irfan Sheriff | 027828bf | 2012-05-02 12:43:05 -0700 | [diff] [blame] | 4065 | return ret; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4066 | } |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 4067 | |
| 4068 | @Override |
| 4069 | public void exit() { |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4070 | /* No need for a background scan upon exit from a disconnected state */ |
| 4071 | if (mEnableBackgroundScan) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 4072 | mWifiNative.enableBackgroundScan(false); |
Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame] | 4073 | } |
Irfan Sheriff | f9e2a49 | 2011-02-28 12:08:11 -0800 | [diff] [blame] | 4074 | setScanAlarm(false); |
Irfan Sheriff | 090813a | 2010-11-03 16:13:32 -0700 | [diff] [blame] | 4075 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4076 | } |
| 4077 | |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4078 | class WpsRunningState extends State { |
| 4079 | //Tracks the source to provide a reply |
| 4080 | private Message mSourceMessage; |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4081 | @Override |
| 4082 | public void enter() { |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4083 | mSourceMessage = Message.obtain(getCurrentMessage()); |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4084 | } |
| 4085 | @Override |
| 4086 | public boolean processMessage(Message message) { |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4087 | switch (message.what) { |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4088 | case WifiMonitor.WPS_SUCCESS_EVENT: |
Irfan Sheriff | 08b1ad4 | 2013-04-11 15:55:31 -0700 | [diff] [blame] | 4089 | // Ignore intermediate success, wait for full connection |
| 4090 | break; |
| 4091 | case WifiMonitor.NETWORK_CONNECTION_EVENT: |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4092 | replyToMessage(mSourceMessage, WifiManager.WPS_COMPLETED); |
| 4093 | mSourceMessage.recycle(); |
| 4094 | mSourceMessage = null; |
Irfan Sheriff | 08b1ad4 | 2013-04-11 15:55:31 -0700 | [diff] [blame] | 4095 | deferMessage(message); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4096 | transitionTo(mDisconnectedState); |
| 4097 | break; |
| 4098 | case WifiMonitor.WPS_OVERLAP_EVENT: |
| 4099 | replyToMessage(mSourceMessage, WifiManager.WPS_FAILED, |
| 4100 | WifiManager.WPS_OVERLAP_ERROR); |
| 4101 | mSourceMessage.recycle(); |
| 4102 | mSourceMessage = null; |
| 4103 | transitionTo(mDisconnectedState); |
| 4104 | break; |
| 4105 | case WifiMonitor.WPS_FAIL_EVENT: |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 4106 | //arg1 has the reason for the failure |
| 4107 | replyToMessage(mSourceMessage, WifiManager.WPS_FAILED, message.arg1); |
| 4108 | mSourceMessage.recycle(); |
| 4109 | mSourceMessage = null; |
| 4110 | transitionTo(mDisconnectedState); |
| 4111 | break; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4112 | case WifiMonitor.WPS_TIMEOUT_EVENT: |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 4113 | replyToMessage(mSourceMessage, WifiManager.WPS_FAILED, |
| 4114 | WifiManager.WPS_TIMED_OUT); |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4115 | mSourceMessage.recycle(); |
| 4116 | mSourceMessage = null; |
| 4117 | transitionTo(mDisconnectedState); |
| 4118 | break; |
| 4119 | case WifiManager.START_WPS: |
| 4120 | replyToMessage(message, WifiManager.WPS_FAILED, WifiManager.IN_PROGRESS); |
| 4121 | break; |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 4122 | case WifiManager.CANCEL_WPS: |
| 4123 | if (mWifiNative.cancelWps()) { |
| 4124 | replyToMessage(message, WifiManager.CANCEL_WPS_SUCCEDED); |
| 4125 | } else { |
| 4126 | replyToMessage(message, WifiManager.CANCEL_WPS_FAILED, WifiManager.ERROR); |
| 4127 | } |
| 4128 | transitionTo(mDisconnectedState); |
| 4129 | break; |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4130 | /* Defer all commands that can cause connections to a different network |
| 4131 | * or put the state machine out of connect mode |
| 4132 | */ |
| 4133 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 4134 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4135 | case WifiManager.CONNECT_NETWORK: |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4136 | case CMD_ENABLE_NETWORK: |
| 4137 | case CMD_RECONNECT: |
| 4138 | case CMD_REASSOCIATE: |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4139 | deferMessage(message); |
| 4140 | break; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 4141 | case WifiMonitor.NETWORK_DISCONNECTION_EVENT: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 4142 | if (DBG) log("Network connection lost"); |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4143 | handleNetworkDisconnect(); |
| 4144 | break; |
Deepthi Gowri | c1b631e | 2013-04-30 18:23:57 +0530 | [diff] [blame] | 4145 | case WifiMonitor.ASSOCIATION_REJECTION_EVENT: |
| 4146 | if (DBG) log("Ignore Assoc reject event during WPS Connection"); |
| 4147 | break; |
Irfan Sheriff | dfd42a9 | 2012-07-02 15:26:47 -0700 | [diff] [blame] | 4148 | case WifiMonitor.AUTHENTICATION_FAILURE_EVENT: |
Arik Nemtsov | 5770b2d | 2012-08-22 16:27:15 -0500 | [diff] [blame] | 4149 | // Disregard auth failure events during WPS connection. The |
| 4150 | // EAP sequence is retried several times, and there might be |
| 4151 | // failures (especially for wps pin). We will get a WPS_XXX |
| 4152 | // event at the end of the sequence anyway. |
| 4153 | if (DBG) log("Ignore auth failure during WPS connection"); |
| 4154 | break; |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 4155 | case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT: |
| 4156 | //Throw away supplicant state changes when WPS is running. |
| 4157 | //We will start getting supplicant state changes once we get |
| 4158 | //a WPS success or failure |
| 4159 | break; |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4160 | default: |
| 4161 | return NOT_HANDLED; |
| 4162 | } |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4163 | return HANDLED; |
| 4164 | } |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4165 | |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 4166 | @Override |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4167 | public void exit() { |
| 4168 | mWifiConfigStore.enableAllNetworks(); |
| 4169 | mWifiConfigStore.loadConfiguredNetworks(); |
| 4170 | } |
Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 4171 | } |
| 4172 | |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4173 | class SoftApStartingState extends State { |
| 4174 | @Override |
| 4175 | public void enter() { |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 4176 | final Message message = getCurrentMessage(); |
| 4177 | if (message.what == CMD_START_AP) { |
| 4178 | final WifiConfiguration config = (WifiConfiguration) message.obj; |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4179 | |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 4180 | if (config == null) { |
| 4181 | mWifiApConfigChannel.sendMessage(CMD_REQUEST_AP_CONFIG); |
| 4182 | } else { |
| 4183 | mWifiApConfigChannel.sendMessage(CMD_SET_AP_CONFIG, config); |
| 4184 | startSoftApWithConfig(config); |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4185 | } |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 4186 | } else { |
| 4187 | throw new RuntimeException("Illegal transition to SoftApStartingState: " + message); |
| 4188 | } |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4189 | } |
| 4190 | @Override |
| 4191 | public boolean processMessage(Message message) { |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4192 | switch(message.what) { |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4193 | case CMD_START_SUPPLICANT: |
| 4194 | case CMD_STOP_SUPPLICANT: |
| 4195 | case CMD_START_AP: |
| 4196 | case CMD_STOP_AP: |
| 4197 | case CMD_START_DRIVER: |
| 4198 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 4199 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4200 | case CMD_SET_COUNTRY_CODE: |
| 4201 | case CMD_SET_FREQUENCY_BAND: |
| 4202 | case CMD_START_PACKET_FILTERING: |
| 4203 | case CMD_STOP_PACKET_FILTERING: |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4204 | case CMD_TETHER_STATE_CHANGE: |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4205 | deferMessage(message); |
| 4206 | break; |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 4207 | case WifiStateMachine.CMD_RESPONSE_AP_CONFIG: |
| 4208 | WifiConfiguration config = (WifiConfiguration) message.obj; |
| 4209 | if (config != null) { |
| 4210 | startSoftApWithConfig(config); |
| 4211 | } else { |
| 4212 | loge("Softap config is null!"); |
| 4213 | sendMessage(CMD_START_AP_FAILURE); |
| 4214 | } |
| 4215 | break; |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4216 | case CMD_START_AP_SUCCESS: |
| 4217 | setWifiApState(WIFI_AP_STATE_ENABLED); |
| 4218 | transitionTo(mSoftApStartedState); |
| 4219 | break; |
| 4220 | case CMD_START_AP_FAILURE: |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4221 | setWifiApState(WIFI_AP_STATE_FAILED); |
| 4222 | transitionTo(mInitialState); |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4223 | break; |
| 4224 | default: |
| 4225 | return NOT_HANDLED; |
| 4226 | } |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4227 | return HANDLED; |
| 4228 | } |
| 4229 | } |
| 4230 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 4231 | class SoftApStartedState extends State { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4232 | @Override |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4233 | public boolean processMessage(Message message) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4234 | switch(message.what) { |
| 4235 | case CMD_STOP_AP: |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 4236 | if (DBG) log("Stopping Soft AP"); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4237 | /* We have not tethered at this point, so we just shutdown soft Ap */ |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4238 | try { |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 4239 | mNwService.stopAccessPoint(mInterfaceName); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4240 | } catch(Exception e) { |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 4241 | loge("Exception in stopAccessPoint()"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4242 | } |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4243 | setWifiApState(WIFI_AP_STATE_DISABLED); |
| 4244 | transitionTo(mInitialState); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4245 | break; |
| 4246 | case CMD_START_AP: |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4247 | // Ignore a start on a running access point |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4248 | break; |
Irfan Sheriff | 7dd0413 | 2011-07-26 13:29:28 -0700 | [diff] [blame] | 4249 | /* Fail client mode operation when soft AP is enabled */ |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4250 | case CMD_START_SUPPLICANT: |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4251 | loge("Cannot start supplicant with a running soft AP"); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4252 | setWifiState(WIFI_STATE_UNKNOWN); |
| 4253 | break; |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4254 | case CMD_TETHER_STATE_CHANGE: |
| 4255 | TetherStateChange stateChange = (TetherStateChange) message.obj; |
| 4256 | if (startTethering(stateChange.available)) { |
| 4257 | transitionTo(mTetheringState); |
Irfan Sheriff | e8daf2a | 2011-07-24 14:28:15 -0700 | [diff] [blame] | 4258 | } |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4259 | break; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4260 | default: |
| 4261 | return NOT_HANDLED; |
| 4262 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4263 | return HANDLED; |
| 4264 | } |
| 4265 | } |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4266 | |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4267 | class TetheringState extends State { |
| 4268 | @Override |
| 4269 | public void enter() { |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4270 | /* Send ourselves a delayed message to shut down if tethering fails to notify */ |
| 4271 | sendMessageDelayed(obtainMessage(CMD_TETHER_NOTIFICATION_TIMED_OUT, |
| 4272 | ++mTetherToken, 0), TETHER_NOTIFICATION_TIME_OUT_MSECS); |
| 4273 | } |
| 4274 | @Override |
| 4275 | public boolean processMessage(Message message) { |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4276 | switch(message.what) { |
| 4277 | case CMD_TETHER_STATE_CHANGE: |
| 4278 | TetherStateChange stateChange = (TetherStateChange) message.obj; |
| 4279 | if (isWifiTethered(stateChange.active)) { |
| 4280 | transitionTo(mTetheredState); |
| 4281 | } |
| 4282 | return HANDLED; |
| 4283 | case CMD_TETHER_NOTIFICATION_TIMED_OUT: |
| 4284 | if (message.arg1 == mTetherToken) { |
| 4285 | loge("Failed to get tether update, shutdown soft access point"); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4286 | transitionTo(mSoftApStartedState); |
Irfan Sheriff | b630b7e | 2013-03-14 13:26:51 -0700 | [diff] [blame] | 4287 | // Needs to be first thing handled |
| 4288 | sendMessageAtFrontOfQueue(CMD_STOP_AP); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4289 | } |
| 4290 | break; |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4291 | case CMD_START_SUPPLICANT: |
| 4292 | case CMD_STOP_SUPPLICANT: |
| 4293 | case CMD_START_AP: |
| 4294 | case CMD_STOP_AP: |
| 4295 | case CMD_START_DRIVER: |
| 4296 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 4297 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4298 | case CMD_SET_COUNTRY_CODE: |
| 4299 | case CMD_SET_FREQUENCY_BAND: |
| 4300 | case CMD_START_PACKET_FILTERING: |
| 4301 | case CMD_STOP_PACKET_FILTERING: |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4302 | deferMessage(message); |
| 4303 | break; |
| 4304 | default: |
| 4305 | return NOT_HANDLED; |
| 4306 | } |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4307 | return HANDLED; |
| 4308 | } |
| 4309 | } |
| 4310 | |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4311 | class TetheredState extends State { |
| 4312 | @Override |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4313 | public boolean processMessage(Message message) { |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4314 | switch(message.what) { |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4315 | case CMD_TETHER_STATE_CHANGE: |
| 4316 | TetherStateChange stateChange = (TetherStateChange) message.obj; |
| 4317 | if (!isWifiTethered(stateChange.active)) { |
| 4318 | loge("Tethering reports wifi as untethered!, shut down soft Ap"); |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 4319 | setHostApRunning(null, false); |
sunil duttu | e325e41 | 2013-04-30 17:23:25 -0700 | [diff] [blame] | 4320 | setHostApRunning(null, true); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4321 | } |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4322 | return HANDLED; |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4323 | case CMD_STOP_AP: |
| 4324 | if (DBG) log("Untethering before stopping AP"); |
| 4325 | setWifiApState(WIFI_AP_STATE_DISABLING); |
| 4326 | stopTethering(); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4327 | transitionTo(mUntetheringState); |
Irfan Sheriff | 367a96b | 2013-03-18 13:52:22 -0700 | [diff] [blame] | 4328 | // More work to do after untethering |
| 4329 | deferMessage(message); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4330 | break; |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4331 | default: |
| 4332 | return NOT_HANDLED; |
| 4333 | } |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4334 | return HANDLED; |
| 4335 | } |
| 4336 | } |
| 4337 | |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4338 | class UntetheringState extends State { |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4339 | @Override |
| 4340 | public void enter() { |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4341 | /* Send ourselves a delayed message to shut down if tethering fails to notify */ |
| 4342 | sendMessageDelayed(obtainMessage(CMD_TETHER_NOTIFICATION_TIMED_OUT, |
| 4343 | ++mTetherToken, 0), TETHER_NOTIFICATION_TIME_OUT_MSECS); |
| 4344 | |
| 4345 | } |
| 4346 | @Override |
| 4347 | public boolean processMessage(Message message) { |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4348 | switch(message.what) { |
| 4349 | case CMD_TETHER_STATE_CHANGE: |
| 4350 | TetherStateChange stateChange = (TetherStateChange) message.obj; |
| 4351 | |
| 4352 | /* Wait till wifi is untethered */ |
| 4353 | if (isWifiTethered(stateChange.active)) break; |
| 4354 | |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4355 | transitionTo(mSoftApStartedState); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4356 | break; |
| 4357 | case CMD_TETHER_NOTIFICATION_TIMED_OUT: |
| 4358 | if (message.arg1 == mTetherToken) { |
| 4359 | loge("Failed to get tether update, force stop access point"); |
Irfan Sheriff | fd7adc5 | 2013-03-04 10:00:37 -0800 | [diff] [blame] | 4360 | transitionTo(mSoftApStartedState); |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4361 | } |
| 4362 | break; |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4363 | case CMD_START_SUPPLICANT: |
| 4364 | case CMD_STOP_SUPPLICANT: |
| 4365 | case CMD_START_AP: |
| 4366 | case CMD_STOP_AP: |
| 4367 | case CMD_START_DRIVER: |
| 4368 | case CMD_STOP_DRIVER: |
Irfan Sheriff | 11aefad | 2013-03-06 07:57:41 -0800 | [diff] [blame] | 4369 | case CMD_SET_OPERATIONAL_MODE: |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4370 | case CMD_SET_COUNTRY_CODE: |
| 4371 | case CMD_SET_FREQUENCY_BAND: |
| 4372 | case CMD_START_PACKET_FILTERING: |
| 4373 | case CMD_STOP_PACKET_FILTERING: |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4374 | deferMessage(message); |
| 4375 | break; |
| 4376 | default: |
| 4377 | return NOT_HANDLED; |
| 4378 | } |
Irfan Sheriff | c74694d | 2011-11-22 12:57:17 -0800 | [diff] [blame] | 4379 | return HANDLED; |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 4380 | } |
| 4381 | } |
Irfan Sheriff | 7f8a12c | 2011-10-04 11:01:50 -0700 | [diff] [blame] | 4382 | |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4383 | //State machine initiated requests can have replyTo set to null indicating |
| 4384 | //there are no recepients, we ignore those reply actions |
| 4385 | private void replyToMessage(Message msg, int what) { |
| 4386 | if (msg.replyTo == null) return; |
| 4387 | Message dstMsg = obtainMessageWithArg2(msg); |
| 4388 | dstMsg.what = what; |
| 4389 | mReplyChannel.replyToMessage(msg, dstMsg); |
| 4390 | } |
| 4391 | |
| 4392 | private void replyToMessage(Message msg, int what, int arg1) { |
| 4393 | if (msg.replyTo == null) return; |
| 4394 | Message dstMsg = obtainMessageWithArg2(msg); |
| 4395 | dstMsg.what = what; |
| 4396 | dstMsg.arg1 = arg1; |
| 4397 | mReplyChannel.replyToMessage(msg, dstMsg); |
| 4398 | } |
| 4399 | |
| 4400 | private void replyToMessage(Message msg, int what, Object obj) { |
| 4401 | if (msg.replyTo == null) return; |
| 4402 | Message dstMsg = obtainMessageWithArg2(msg); |
| 4403 | dstMsg.what = what; |
| 4404 | dstMsg.obj = obj; |
| 4405 | mReplyChannel.replyToMessage(msg, dstMsg); |
| 4406 | } |
| 4407 | |
| 4408 | /** |
| 4409 | * arg2 on the source message has a unique id that needs to be retained in replies |
| 4410 | * to match the request |
Vinit Deshapnde | 2c385ec | 2013-09-09 16:28:31 -0700 | [diff] [blame] | 4411 | |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 4412 | * see WifiManager for details |
| 4413 | */ |
| 4414 | private Message obtainMessageWithArg2(Message srcMsg) { |
| 4415 | Message msg = Message.obtain(); |
| 4416 | msg.arg2 = srcMsg.arg2; |
| 4417 | return msg; |
| 4418 | } |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 4419 | } |