WifiAutoJoinController.java revision be4d4bfef88bb0870e1e8bb464c302a2e54bbed4
1f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/* 262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * Copyright (C) 2014 The Android Open Source Project 3f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 4f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * Licensed under the Apache License, Version 2.0 (the "License"); 5f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * you may not use this file except in compliance with the License. 6f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * You may obtain a copy of the License at 7f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 8f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * http://www.apache.org/licenses/LICENSE-2.0 9f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 10f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * Unless required by applicable law or agreed to in writing, software 11f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * distributed under the License is distributed on an "AS IS" BASIS, 12f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * See the License for the specific language governing permissions and 14f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * limitations under the License. 15f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 16f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 17f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepackage com.android.server.wifi; 18f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 19f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.content.Context; 20f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.NetworkKey; 21f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.NetworkScoreManager; 220c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport android.net.WifiKey; 2377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.net.wifi.ScanResult; 2477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.net.wifi.WifiConfiguration; 258639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidsonimport android.net.wifi.WifiConfiguration.KeyMgmt; 2677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.net.wifi.WifiConnectionStatistics; 27f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalleimport android.os.Process; 2877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.provider.Settings; 29c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.text.TextUtils; 30f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.util.Log; 31e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalleimport android.os.SystemClock; 32f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 33ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.anqp.ANQPElement; 34ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.anqp.Constants; 35ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.ANQPData; 36ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.AnqpCache; 37ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.Chronograph; 38ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.NetworkDetail; 39ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.PasspointMatch; 40ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.PasspointMatchInfo; 41ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.SupplicantBridge; 42ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.pps.HomeSP; 43ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist 4477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.io.BufferedReader; 4577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.io.IOException; 4677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.io.StringReader; 470c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport java.util.ArrayList; 4877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.util.Arrays; 49d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpandeimport java.util.BitSet; 50ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport java.util.Collection; 51dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport java.util.Collections; 52dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport java.util.Comparator; 53f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.HashMap; 5477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.util.Iterator; 55f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.List; 56ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport java.util.Map; 57f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 58f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/** 59f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * AutoJoin controller is responsible for WiFi Connect decision 60f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 61f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * It runs in the thread context of WifiStateMachine 62f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 63f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 64f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepublic class WifiAutoJoinController { 65f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 66f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private Context mContext; 67f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiStateMachine mWifiStateMachine; 68f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiConfigStore mWifiConfigStore; 69f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiNative mWifiNative; 70f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 71f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private NetworkScoreManager scoreManager; 72f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiNetworkScoreCache mNetworkScoreCache; 73f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 74f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final String TAG = "WifiAutoJoinController "; 7531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist private static boolean DBG = false; 76ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle private static boolean VDBG = false; 77f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final boolean mStaStaSupported = false; 78f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 79c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle public static int mScanResultMaximumAge = 40000; /* milliseconds unit */ 80833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle public static int mScanResultAutoJoinAge = 5000; /* milliseconds unit */ 81c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle 82453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle private String mCurrentConfigurationKey = null; //used by autojoin 83f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 8425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande private final HashMap<String, ScanDetail> scanResultCache = new HashMap<>(); 85f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 86c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle private WifiConnectionStatistics mWifiConnectionStatistics; 87c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle 88dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle /** 89dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * Whether to allow connections to untrusted networks. 90dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle */ 918639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson private boolean mAllowUntrustedConnections = false; 928639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson 93c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle /* For debug purpose only: if the scored override a score */ 94c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle boolean didOverride = false; 95c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle 96931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Lose the non-auth failure blacklisting after 8 hours 974dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle private final static long loseBlackListHardMilli = 1000 * 60 * 60 * 8; 98931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Lose some temporary blacklisting after 30 minutes 994dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle private final static long loseBlackListSoftMilli = 1000 * 60 * 30; 10027355a942653264388e909a4276196ee63e57811vandwalle 101dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle /** 102dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * @see android.provider.Settings.Global#WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS 103dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle */ 10416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson private static final long DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS = 1000 * 60; // 1 minute 10516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson 106b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle public static final int AUTO_JOIN_IDLE = 0; 107b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle public static final int AUTO_JOIN_ROAMING = 1; 108b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle public static final int AUTO_JOIN_EXTENDED_ROAMING = 2; 109b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle public static final int AUTO_JOIN_OUT_OF_NETWORK_ROAMING = 3; 110b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle 11197b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle public static final int HIGH_THRESHOLD_MODIFIER = 5; 11297b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle 11387df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle public static final int MAX_RSSI_DELTA = 50; 11487df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle 1151ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // Below are AutoJoin wide parameters indicating if we should be aggressive before joining 1161ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // weak network. Note that we cannot join weak network that are going to be marked as unanted by 1171ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // ConnectivityService because this will trigger link flapping. 1181ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle /** 1191ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle * There was a non-blacklisted configuration that we bailed from because of a weak signal 1201ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle */ 1211ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle boolean didBailDueToWeakRssi = false; 1221ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle /** 1231ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle * number of time we consecutively bailed out of an eligible network because its signal 1241ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle * was too weak 1251ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle */ 1261ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle int weakRssiBailCount = 0; 1271ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle 128f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiAutoJoinController(Context c, WifiStateMachine w, WifiConfigStore s, 129c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle WifiConnectionStatistics st, WifiNative n) { 130f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mContext = c; 131f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine = w; 132f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore = s; 133f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiNative = n; 134f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mNetworkScoreCache = null; 135c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle mWifiConnectionStatistics = st; 13621bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle scoreManager = 13721bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE); 138f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager == null) 139f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("Registered scoreManager NULL " + " service " + Context.NETWORK_SCORE_SERVICE); 140f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 141f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager != null) { 142f13817203179f41620514718c8668ae7e418f8afJeff Davidson mNetworkScoreCache = new WifiNetworkScoreCache(mContext); 143f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache); 144f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 145f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("No network score service: Couldnt register as a WiFi score Manager, type=" 146f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(NetworkKey.TYPE_WIFI) 147f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " service " + Context.NETWORK_SCORE_SERVICE); 148f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mNetworkScoreCache = null; 149f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 150f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 151f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 152ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle void enableVerboseLogging(int verbose) { 153dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (verbose > 0) { 154abde872adced15dfb6781fb71959453d963326dbYuhao Zheng DBG = true; 155ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle VDBG = true; 156ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } else { 157abde872adced15dfb6781fb71959453d963326dbYuhao Zheng DBG = false; 158ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle VDBG = false; 159ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 160ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 161ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle 162931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 163931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * Flush out scan results older than mScanResultMaximumAge 164931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 165f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private void ageScanResultsOut(int delay) { 166f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (delay <= 0) { 167931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle delay = mScanResultMaximumAge; // Something sane 168f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 169b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle long milli = System.currentTimeMillis(); 170f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 171f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("ageScanResultsOut delay " + Integer.valueOf(delay) + " size " 172f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.valueOf(scanResultCache.size()) + " now " + Long.valueOf(milli)); 173f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 174f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 175dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle Iterator<HashMap.Entry<String, ScanDetail>> iter = scanResultCache.entrySet().iterator(); 176f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle while (iter.hasNext()) { 177dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle HashMap.Entry<String, ScanDetail> entry = iter.next(); 17825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanDetail scanDetail = entry.getValue(); 17925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if ((scanDetail.getSeen() + delay) < milli) { 180f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle iter.remove(); 181f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 182f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 183f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 184f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 185ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist 186d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande void averageRssiAndRemoveFromCache(ScanResult result) { 187d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // Fetch the previous instance for this result 18825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanDetail sd = scanResultCache.get(result.BSSID); 18925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if (sd != null) { 19025ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanResult sr = sd.getScanResult(); 191d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0 192d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande && result.level == 0 193d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande && sr.level < -20) { 194d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // A 'zero' RSSI reading is most likely a chip problem which returns 195d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // an unknown RSSI, hence ignore it 196d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande result.level = sr.level; 197ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist } 198ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist 199d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // If there was a previous cache result for this BSSID, average the RSSI values 200d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande result.averageRssi(sr.level, sr.seen, mScanResultMaximumAge); 201ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist 202d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // Remove the previous Scan Result - this is not necessary 203d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande scanResultCache.remove(result.BSSID); 204d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } else if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0 && result.level == 0) { 205d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // A 'zero' RSSI reading is most likely a chip problem which returns 206d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // an unknown RSSI, hence initialize it to a sane value 207d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande result.level = mWifiConfigStore.scanResultRssiLevelPatchUp; 208ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist } 209ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist } 210ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist 211d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande void addToUnscoredNetworks(ScanResult result, List<NetworkKey> unknownScanResults) { 212d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande WifiKey wkey; 213d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande // Quoted SSIDs are the only one valid at this stage 214d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande try { 215d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande wkey = new WifiKey("\"" + result.SSID + "\"", result.BSSID); 216d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } catch (IllegalArgumentException e) { 217d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande logDbg("AutoJoinController: received badly encoded SSID=[" + result.SSID + 218d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande "] ->skipping this network"); 219d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande wkey = null; 220d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } 221d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande if (wkey != null) { 222d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande NetworkKey nkey = new NetworkKey(wkey); 223d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande //if we don't know this scan result then request a score from the scorer 224d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande unknownScanResults.add(nkey); 225ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist } 226d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande if (VDBG) { 227d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande String cap = ""; 228d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande if (result.capabilities != null) 229d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande cap = result.capabilities; 230d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande logDbg(result.SSID + " " + result.BSSID + " rssi=" 2310510c350db16f6bd2930f0a5ace483efbfa681d5Pierre Vandwalle + result.level + " cap " + cap + " tsf " + result.timestamp + " is not scored"); 232ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist } 233ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist } 234ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist 23577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist int addToScanCache(List<ScanDetail> scanList) { 236be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle int numScanResultsKnown = 0; // Record number of scan results we knew about 237be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle WifiConfiguration associatedConfig = null; 2387b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle boolean didAssociate = false; 2398242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle long now = System.currentTimeMillis(); 240f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 2410c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle ArrayList<NetworkKey> unknownScanResults = new ArrayList<NetworkKey>(); 2420c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle 243dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle for (ScanDetail scanDetail : scanList) { 24477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist ScanResult result = scanDetail.getScanResult(); 2451fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng if (result.SSID == null) continue; 246c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle 247e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle if (VDBG) { 248e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle logDbg(" addToScanCache " + result.SSID + " " + result.BSSID 249e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle + " tsf=" + result.timestamp 250e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle + " now= " + now + " uptime=" + SystemClock.uptimeMillis()); 2510510c350db16f6bd2930f0a5ace483efbfa681d5Pierre Vandwalle } 2520510c350db16f6bd2930f0a5ace483efbfa681d5Pierre Vandwalle 253c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle // Make sure we record the last time we saw this result 25425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande scanDetail.setSeen(); 255f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 256d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande averageRssiAndRemoveFromCache(result); 257e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle 258e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle if (!mNetworkScoreCache.isScoredNetwork(result)) { 259d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande addToUnscoredNetworks(result, unknownScanResults); 2600c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } else { 261e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle if (VDBG) { 2627b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle String cap = ""; 2637b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle if (result.capabilities != null) 2647b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle cap = result.capabilities; 265e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle int score = mNetworkScoreCache.getNetworkScore(result); 266e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle logDbg(result.SSID + " " + result.BSSID + " rssi=" 2677b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle + result.level + " cap " + cap + " is scored : " + score); 2680c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } 269f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 271e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // scanResultCache.put(result.BSSID, new ScanResult(result)); 27225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande scanResultCache.put(result.BSSID, scanDetail); 273be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle // Add this BSSID to the scanResultCache of a Saved WifiConfiguration 274d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande didAssociate = mWifiConfigStore.updateSavedNetworkHistory(scanDetail); 275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 276be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle // If not successful, try to associate this BSSID to an existing Saved WifiConfiguration 2777b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle if (!didAssociate) { 278be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle // We couldn't associate the scan result to a Saved WifiConfiguration 279c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle // Hence it is untrusted 280c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle result.untrusted = true; 2810d616ef3bf635dff8722e064c0be842676390ed8vandwalle } else { 2820d616ef3bf635dff8722e064c0be842676390ed8vandwalle // If the scan result has been blacklisted fir 18 hours -> unblacklist 2830d616ef3bf635dff8722e064c0be842676390ed8vandwalle if ((now - result.blackListTimestamp) > loseBlackListHardMilli) { 2840d616ef3bf635dff8722e064c0be842676390ed8vandwalle result.setAutoJoinStatus(ScanResult.ENABLED); 2850d616ef3bf635dff8722e064c0be842676390ed8vandwalle } 286f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 2877b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle if (didAssociate) { 288be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle numScanResultsKnown++; 289dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle result.isAutoJoinCandidate++; 290a0708b09ad17b086c008ab100aec7143d7613c80vandwalle } else { 291a0708b09ad17b086c008ab100aec7143d7613c80vandwalle result.isAutoJoinCandidate = 0; 292be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle } 293f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 2940c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle 2950c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle if (unknownScanResults.size() != 0) { 2960c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle NetworkKey[] newKeys = 2970c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle unknownScanResults.toArray(new NetworkKey[unknownScanResults.size()]); 298931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Kick the score manager, we will get updated scores asynchronously 2990c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle scoreManager.requestScores(newKeys); 3000c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } 301be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle return numScanResultsKnown; 302f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 303f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 304f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void logDbg(String message) { 3050888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle logDbg(message, false); 306ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 307ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 308ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle void logDbg(String message, boolean stackTrace) { 309ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (stackTrace) { 31033f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande Log.d(TAG, message + " stack:" 311ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[2].getMethodName() + " - " 312ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[3].getMethodName() + " - " 313ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[4].getMethodName() + " - " 314ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[5].getMethodName()); 315ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } else { 31633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande Log.d(TAG, message); 317ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 318f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 319f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 320931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Called directly from WifiStateMachine 321be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle int newSupplicantResults(boolean doAutoJoin) { 322be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle int numScanResultsKnown; 32377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist List<ScanDetail> scanList = mWifiStateMachine.getScanResultsListNoCopyUnsync(); 324be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle numScanResultsKnown = addToScanCache(scanList); 325f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(mScanResultMaximumAge); 326be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle if (DBG) { 327be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size()) 328dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " known=" + numScanResultsKnown + " " 329dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + doAutoJoin); 330be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle } 3317806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle if (doAutoJoin) { 3327806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle attemptAutoJoin(); 3337806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle } 334005c1ef113192f898499a407dd266393a8d6b076vandwalle mWifiConfigStore.writeKnownNetworkHistory(false); 335be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle return numScanResultsKnown; 336f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 337f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 338f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 339931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 340931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * Not used at the moment 341f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * should be a call back from WifiScanner HAL ?? 342f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * this function is not hooked and working yet, it will receive scan results from WifiScanners 343f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan 344f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * results as normal. 345f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 346f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void newHalScanResults() { 34777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist List<ScanDetail> scanList = null;//mWifiScanner.syncGetScanResultsList(); 348f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String akm = WifiParser.parse_akm(null, null); 349f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg(akm); 350f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle addToScanCache(scanList); 351f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(0); 352f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 353005c1ef113192f898499a407dd266393a8d6b076vandwalle mWifiConfigStore.writeKnownNetworkHistory(false); 354f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 355f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 356931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 357dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * network link quality changed, called directly from WifiTrafficPoller, 358931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * or by listening to Link Quality intent 359931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 360f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void linkQualitySignificantChange() { 361f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 362f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 363f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 364931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 365f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * compare a WifiConfiguration against the current network, return a delta score 366f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * If not associated, and the candidate will always be better 367f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * For instance if the candidate is a home network versus an unknown public wifi, 368f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * the delta will be infinite, else compare Kepler scores etc… 369b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle * Negatve return values from this functions are meaningless per se, just trying to 370e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle * keep them distinct for debug purpose (i.e. -1, -2 etc...) 371931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 372e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle private int compareNetwork(WifiConfiguration candidate, 373e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle String lastSelectedConfiguration) { 374b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle if (candidate == null) 375b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle return -3; 376b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle 377f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration(); 378b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle if (currentNetwork == null) { 379c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // Return any absurdly high score, if we are not connected there is no current 380c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // network to... 381dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle return 1000; 382b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle } 383f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 384f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate.configKey(true).equals(currentNetwork.configKey(true))) { 385b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle return -2; 386f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 387f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 388b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle if (DBG) { 389e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle logDbg("compareNetwork will compare " + candidate.configKey() 390e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " with current " + currentNetwork.configKey()); 391b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle } 392833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle int order = compareWifiConfigurations(currentNetwork, candidate); 393e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle 394e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // The lastSelectedConfiguration is the configuration the user has manually selected 395e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // thru WifiPicker, or that a 3rd party app asked us to connect to via the 396e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // enableNetwork with disableOthers=true WifiManager API 397e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // As this is a direct user choice, we strongly prefer this configuration, 398e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // hence give +/-100 399e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle if ((lastSelectedConfiguration != null) 400e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle && currentNetwork.configKey().equals(lastSelectedConfiguration)) { 401e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // currentNetwork is the last selected configuration, 402e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // so keep it above connect choices (+/-60) and 403e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // above RSSI/scorer based selection of linked configuration (+/- 50) 404e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // by reducing order by -100 405e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle order = order - 100; 406dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 407e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle logDbg(" ...and prefers -100 " + currentNetwork.configKey() 408e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " over " + candidate.configKey() 409e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " because it is the last selected -> " 410e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + Integer.toString(order)); 411e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle } 412e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle } else if ((lastSelectedConfiguration != null) 413e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle && candidate.configKey().equals(lastSelectedConfiguration)) { 414e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // candidate is the last selected configuration, 415e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // so keep it above connect choices (+/-60) and 416e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // above RSSI/scorer based selection of linked configuration (+/- 50) 417e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // by increasing order by +100 418e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle order = order + 100; 419dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 420e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle logDbg(" ...and prefers +100 " + candidate.configKey() 421e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " over " + currentNetwork.configKey() 422e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " because it is the last selected -> " 423e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + Integer.toString(order)); 424e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle } 425e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle } 426e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle 427ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle return order; 428f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 429f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 430ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle /** 431ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * update the network history fields fo that configuration 432ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * - if userTriggered, we mark the configuration as "non selfAdded" since the user has seen it 433ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * and took over management 434ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * - if it is a "connect", remember which network were there at the point of the connect, so 435ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * as those networks get a relative lower score than the selected configuration 43662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * 437ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param netId 438ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param userTriggered : if the update come from WiFiManager 439dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * @param connect : if the update includes a connect 440931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 44162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) { 442f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId); 443f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected == null) { 444c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle logDbg("updateConfigurationHistory nid=" + netId + " no selected configuration!"); 445f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return; 446f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 447f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 448e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle if (selected.SSID == null) { 449c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle logDbg("updateConfigurationHistory nid=" + netId + 450c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle " no SSID in selected configuration!"); 451e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle return; 452e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle } 453e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle 45462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (userTriggered) { 455931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Reenable autojoin for this network, 45662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle // since the user want to connect to this configuration 45727355a942653264388e909a4276196ee63e57811vandwalle selected.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED); 45862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.selfAdded = false; 459e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle selected.dirty = true; 46062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 462992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle if (DBG && userTriggered) { 463f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected.connectChoices != null) { 464ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will update " 465f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(netId) + " now: " 466992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + Integer.toString(selected.connectChoices.size()) 467992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + " uid=" + Integer.toString(selected.creatorUid), true); 468f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 469ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will update " 470992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + Integer.toString(netId) 471992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + " uid=" + Integer.toString(selected.creatorUid), true); 472f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 473f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 474f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 475ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (connect && userTriggered) { 476ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle boolean found = false; 4772451dbcc4f9641df188326215b204b798eb70c46vandwalle int choice = 0; 478c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle int size = 0; 4799f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle 4809f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle // Reset the triggered disabled count, because user wanted to connect to this 4819f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle // configuration, and we were not. 4829f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle selected.numUserTriggeredWifiDisableLowRSSI = 0; 4839f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle selected.numUserTriggeredWifiDisableBadRSSI = 0; 4849f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle selected.numUserTriggeredWifiDisableNotHighRSSI = 0; 4859f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle selected.numUserTriggeredJoinAttempts++; 4869f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle 48762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle List<WifiConfiguration> networks = 48862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle mWifiConfigStore.getRecentConfiguredNetworks(12000, false); 489c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle if (networks != null) size = networks.size(); 490c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle logDbg("updateConfigurationHistory found " + size + " networks"); 49162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (networks != null) { 49262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle for (WifiConfiguration config : networks) { 493992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle if (DBG) { 494992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle logDbg("updateConfigurationHistory got " + config.SSID + " nid=" 495992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + Integer.toString(config.networkId)); 496992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle } 497f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 49862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.configKey(true).equals(config.configKey(true))) { 499ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle found = true; 50062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle continue; 50162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 502f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 50387df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle // If the selection was made while config was visible with reasonably good RSSI 50487df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle // then register the user preference, else ignore 50587df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle if (config.visibility == null || 50687df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle (config.visibility.rssi24 < mWifiConfigStore.thresholdBadRssi24.get() 50787df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle && config.visibility.rssi5 < mWifiConfigStore.thresholdBadRssi5.get()) 50887df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle ) { 50987df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle continue; 51062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 511f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 51287df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle choice = MAX_RSSI_DELTA + 10; // Make sure the choice overrides the RSSI diff 51387df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle 514931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // The selected configuration was preferred over a recently seen config 515931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // hence remember the user's choice: 516931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // add the recently seen config to the selected's connectChoices array 517ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 518ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (selected.connectChoices == null) { 519ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle selected.connectChoices = new HashMap<String, Integer>(); 520ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 521ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 522ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory add a choice " + selected.configKey(true) 5230888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle + " over " + config.configKey(true) 5242451dbcc4f9641df188326215b204b798eb70c46vandwalle + " choice " + Integer.toString(choice)); 525cf5b8eb8a08c45bd4a82f1f4bb789c8a1b08744fvandwalle 5262f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // Add the visible config to the selected's connect choice list 5272f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle selected.connectChoices.put(config.configKey(true), choice); 528f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 52962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.connectChoices != null) { 530ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (VDBG) { 531ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will remove " 53262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + selected.configKey(true) + " from " + config.configKey(true)); 533ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 534931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Remove the selected from the recently seen config's connectChoice list 53562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle config.connectChoices.remove(selected.configKey(true)); 5360888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle 5370888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle if (selected.linkedConfigurations != null) { 538dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle // Remove the selected's linked configuration from the 539dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle // recently seen config's connectChoice list 540dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle for (String key : selected.linkedConfigurations.keySet()) { 541dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle config.connectChoices.remove(key); 542dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 5430888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle } 54462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 545ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 546ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (found == false) { 547dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle // We haven't found the configuration that the user just selected in our 548dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle // scan cache. 549dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle // In that case we will need a new scan before attempting to connect to this 550dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle // configuration anyhow and thus we can process the scan results then. 551dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle logDbg("updateConfigurationHistory try to connect to an old network!! : " 552dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + selected.configKey()); 55362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 554f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 55562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.connectChoices != null) { 55662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (VDBG) 557ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory " + Integer.toString(netId) 55862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + " now: " + Integer.toString(selected.connectChoices.size())); 55962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 562992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle 563931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // TODO: write only if something changed 564992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle if (userTriggered || connect) { 565005c1ef113192f898499a407dd266393a8d6b076vandwalle mWifiConfigStore.writeKnownNetworkHistory(false); 566992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle } 567f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 568f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 56921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle int getConnectChoice(WifiConfiguration source, WifiConfiguration target, boolean strict) { 57021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle int choice = 0; 5712451dbcc4f9641df188326215b204b798eb70c46vandwalle if (source == null || target == null) { 5722451dbcc4f9641df188326215b204b798eb70c46vandwalle return 0; 573f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 574f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 5752451dbcc4f9641df188326215b204b798eb70c46vandwalle if (source.connectChoices != null 5762451dbcc4f9641df188326215b204b798eb70c46vandwalle && source.connectChoices.containsKey(target.configKey(true))) { 577a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle Integer val = source.connectChoices.get(target.configKey(true)); 578a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle if (val != null) { 579a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle choice = val; 580a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle } 5812451dbcc4f9641df188326215b204b798eb70c46vandwalle } else if (source.linkedConfigurations != null) { 582f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : source.linkedConfigurations.keySet()) { 583f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key); 584f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config != null) { 585f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices != null) { 586a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle Integer val = config.connectChoices.get(target.configKey(true)); 587a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle if (val != null) { 588a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle choice = val; 589a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle } 590f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 591f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 592f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 5932451dbcc4f9641df188326215b204b798eb70c46vandwalle } 5942451dbcc4f9641df188326215b204b798eb70c46vandwalle 59521d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (!strict && choice == 0) { 59633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande // We didn't find the connect choice; fallback to some default choices 59733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande int sourceScore = getSecurityScore(source); 59833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande int targetScore = getSecurityScore(target); 5998eefaf2c281b44fb7a4d951ba587f447800c4b4fVinit Deshpande choice = sourceScore - targetScore; 60033f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande } 60133f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande 60233f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande return choice; 6032451dbcc4f9641df188326215b204b798eb70c46vandwalle } 6042451dbcc4f9641df188326215b204b798eb70c46vandwalle 60533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande int compareWifiConfigurationsFromVisibility(WifiConfiguration.Visibility a, int aRssiBoost, 606dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle String dbgA, WifiConfiguration.Visibility b, int bRssiBoost, String dbgB) { 6072451dbcc4f9641df188326215b204b798eb70c46vandwalle 608815788ba7838fc54310baed3deb9b95548e0ce69vandwalle int aRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref) 609815788ba7838fc54310baed3deb9b95548e0ce69vandwalle int bRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref) 610815788ba7838fc54310baed3deb9b95548e0ce69vandwalle 611815788ba7838fc54310baed3deb9b95548e0ce69vandwalle int aScore = 0; 612815788ba7838fc54310baed3deb9b95548e0ce69vandwalle int bScore = 0; 613815788ba7838fc54310baed3deb9b95548e0ce69vandwalle 614815788ba7838fc54310baed3deb9b95548e0ce69vandwalle boolean aPrefers5GHz = false; 615815788ba7838fc54310baed3deb9b95548e0ce69vandwalle boolean bPrefers5GHz = false; 6164dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 6172451dbcc4f9641df188326215b204b798eb70c46vandwalle /** 618815788ba7838fc54310baed3deb9b95548e0ce69vandwalle * Calculate a boost to apply to RSSI value of configuration we want to join on 5GHz: 619815788ba7838fc54310baed3deb9b95548e0ce69vandwalle * Boost RSSI value of 5GHz bands iff the base value is better than threshold, 620815788ba7838fc54310baed3deb9b95548e0ce69vandwalle * penalize the RSSI value of 5GHz band iff the base value is lower than threshold 6212451dbcc4f9641df188326215b204b798eb70c46vandwalle * This implements band preference where we prefer 5GHz if RSSI5 is good enough, whereas 6222451dbcc4f9641df188326215b204b798eb70c46vandwalle * we prefer 2.4GHz otherwise. 6232451dbcc4f9641df188326215b204b798eb70c46vandwalle */ 62433f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande aRssiBoost5 = rssiBoostFrom5GHzRssi(a.rssi5, dbgA + "->"); 62533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande bRssiBoost5 = rssiBoostFrom5GHzRssi(b.rssi5, dbgB + "->"); 6262451dbcc4f9641df188326215b204b798eb70c46vandwalle 627815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // Select which band to use for a 62833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande if (a.rssi5 + aRssiBoost5 > a.rssi24) { 6292451dbcc4f9641df188326215b204b798eb70c46vandwalle // Prefer a's 5GHz 630815788ba7838fc54310baed3deb9b95548e0ce69vandwalle aPrefers5GHz = true; 631815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } 632815788ba7838fc54310baed3deb9b95548e0ce69vandwalle 633815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // Select which band to use for b 63433f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande if (b.rssi5 + bRssiBoost5 > b.rssi24) { 635815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // Prefer b's 5GHz 636815788ba7838fc54310baed3deb9b95548e0ce69vandwalle bPrefers5GHz = true; 637815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } 638815788ba7838fc54310baed3deb9b95548e0ce69vandwalle 639815788ba7838fc54310baed3deb9b95548e0ce69vandwalle if (aPrefers5GHz) { 640815788ba7838fc54310baed3deb9b95548e0ce69vandwalle if (bPrefers5GHz) { 641815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either 642815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // one, but directly compare the RSSI values, this improves stability, 643815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // since the 5GHz RSSI boost can introduce large fluctuations 64433f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande aScore = a.rssi5 + aRssiBoost; 645815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } else { 646815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // If only a is on 5GHz, then apply the 5GHz preference boost to a 64733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande aScore = a.rssi5 + aRssiBoost + aRssiBoost5; 648815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } 6492451dbcc4f9641df188326215b204b798eb70c46vandwalle } else { 65033f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande aScore = a.rssi24 + aRssiBoost; 651f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 6522451dbcc4f9641df188326215b204b798eb70c46vandwalle 653815788ba7838fc54310baed3deb9b95548e0ce69vandwalle if (bPrefers5GHz) { 654815788ba7838fc54310baed3deb9b95548e0ce69vandwalle if (aPrefers5GHz) { 655815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either 656815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // one, but directly compare the RSSI values, this improves stability, 657815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // since the 5GHz RSSI boost can introduce large fluctuations 65833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande bScore = b.rssi5 + bRssiBoost; 659815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } else { 660815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // If only b is on 5GHz, then apply the 5GHz preference boost to b 66133f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande bScore = b.rssi5 + bRssiBoost + bRssiBoost5; 662815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } 663815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } else { 66433f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande bScore = b.rssi24 + bRssiBoost; 665815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } 66633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande 667815788ba7838fc54310baed3deb9b95548e0ce69vandwalle if (VDBG) { 66833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande logDbg(" " + dbgA + " is5=" + aPrefers5GHz + " score=" + aScore 66933f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande + " " + dbgB + " is5=" + bPrefers5GHz + " score=" + bScore); 670815788ba7838fc54310baed3deb9b95548e0ce69vandwalle } 671f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle 672f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle // Debug only, record RSSI comparison parameters 67333f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande if (a != null) { 67433f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande a.score = aScore; 67533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande a.currentNetworkBoost = aRssiBoost; 67633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande a.bandPreferenceBoost = aRssiBoost5; 677f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle } 67833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande if (b != null) { 67933f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande b.score = bScore; 68033f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande b.currentNetworkBoost = bRssiBoost; 68133f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande b.bandPreferenceBoost = bRssiBoost5; 682f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle } 683f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle 684815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // Compare a and b 685815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // If a score is higher then a > b and the order is descending (negative) 686815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // If b score is higher then a < b and the order is ascending (positive) 687815788ba7838fc54310baed3deb9b95548e0ce69vandwalle return bScore - aScore; 688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 689f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 6902451dbcc4f9641df188326215b204b798eb70c46vandwalle // Compare WifiConfiguration by RSSI, and return a comparison value in the range [-50, +50] 6912451dbcc4f9641df188326215b204b798eb70c46vandwalle // The result represents "approximately" an RSSI difference measured in dBM 6922451dbcc4f9641df188326215b204b798eb70c46vandwalle // Adjusted with various parameters: 6932451dbcc4f9641df188326215b204b798eb70c46vandwalle // +) current network gets a +15 boost 6942451dbcc4f9641df188326215b204b798eb70c46vandwalle // +) 5GHz signal, if they are strong enough, get a +15 or +25 boost, representing the 6952451dbcc4f9641df188326215b204b798eb70c46vandwalle // fact that at short range we prefer 5GHz band as it is cleaner of interference and 6962451dbcc4f9641df188326215b204b798eb70c46vandwalle // provides for wider channels 6972451dbcc4f9641df188326215b204b798eb70c46vandwalle int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b, 6982451dbcc4f9641df188326215b204b798eb70c46vandwalle String currentConfiguration) { 699f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 7002451dbcc4f9641df188326215b204b798eb70c46vandwalle 7012451dbcc4f9641df188326215b204b798eb70c46vandwalle // Boost used so as to favor current config 7022451dbcc4f9641df188326215b204b798eb70c46vandwalle int aRssiBoost = 0; 7032451dbcc4f9641df188326215b204b798eb70c46vandwalle int bRssiBoost = 0; 7042451dbcc4f9641df188326215b204b798eb70c46vandwalle 7052451dbcc4f9641df188326215b204b798eb70c46vandwalle // Retrieve the visibility 706f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility astatus = a.visibility; 707f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility bstatus = b.visibility; 708f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (astatus == null || bstatus == null) { 7092451dbcc4f9641df188326215b204b798eb70c46vandwalle // Error visibility wasn't set 710b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurations NULL band status!"); 711f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 712f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 7132451dbcc4f9641df188326215b204b798eb70c46vandwalle 7142451dbcc4f9641df188326215b204b798eb70c46vandwalle // Apply Hysteresis, boost RSSI of current configuration 7152451dbcc4f9641df188326215b204b798eb70c46vandwalle if (null != currentConfiguration) { 7162451dbcc4f9641df188326215b204b798eb70c46vandwalle if (a.configKey().equals(currentConfiguration)) { 71793a1fddee50a244d31036cddae6b7db6630fd93dvandwalle aRssiBoost = mWifiConfigStore.currentNetworkBoost; 7182451dbcc4f9641df188326215b204b798eb70c46vandwalle } else if (b.configKey().equals(currentConfiguration)) { 71993a1fddee50a244d31036cddae6b7db6630fd93dvandwalle bRssiBoost = mWifiConfigStore.currentNetworkBoost; 7202451dbcc4f9641df188326215b204b798eb70c46vandwalle } 7212451dbcc4f9641df188326215b204b798eb70c46vandwalle } 7222451dbcc4f9641df188326215b204b798eb70c46vandwalle 723dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 724b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurationsRSSI: " + a.configKey() 725dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " rssi=" + Integer.toString(astatus.rssi24) 726dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + "," + Integer.toString(astatus.rssi5) 727dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " boost=" + Integer.toString(aRssiBoost) 728dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " " + b.configKey() + " rssi=" 729dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + Integer.toString(bstatus.rssi24) + "," 730dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + Integer.toString(bstatus.rssi5) 731dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " boost=" + Integer.toString(bRssiBoost) 7322451dbcc4f9641df188326215b204b798eb70c46vandwalle ); 733f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 7342451dbcc4f9641df188326215b204b798eb70c46vandwalle 73533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande order = compareWifiConfigurationsFromVisibility( 73633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande a.visibility, aRssiBoost, a.configKey(), 73733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande b.visibility, bRssiBoost, b.configKey()); 7382451dbcc4f9641df188326215b204b798eb70c46vandwalle 73987df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle // Normalize the order to [-50, +50] = [ -MAX_RSSI_DELTA, MAX_RSSI_DELTA] 74087df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle if (order > MAX_RSSI_DELTA) order = MAX_RSSI_DELTA; 74187df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle else if (order < -MAX_RSSI_DELTA) order = -MAX_RSSI_DELTA; 7422451dbcc4f9641df188326215b204b798eb70c46vandwalle 7432451dbcc4f9641df188326215b204b798eb70c46vandwalle if (VDBG) { 7442451dbcc4f9641df188326215b204b798eb70c46vandwalle String prefer = " = "; 7452451dbcc4f9641df188326215b204b798eb70c46vandwalle if (order > 0) { 7462451dbcc4f9641df188326215b204b798eb70c46vandwalle prefer = " < "; // Ascending 7472451dbcc4f9641df188326215b204b798eb70c46vandwalle } else if (order < 0) { 7482451dbcc4f9641df188326215b204b798eb70c46vandwalle prefer = " > "; // Descending 7492451dbcc4f9641df188326215b204b798eb70c46vandwalle } 750b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurationsRSSI " + a.configKey() 7512451dbcc4f9641df188326215b204b798eb70c46vandwalle + " rssi=(" + a.visibility.rssi24 7522451dbcc4f9641df188326215b204b798eb70c46vandwalle + "," + a.visibility.rssi5 7532451dbcc4f9641df188326215b204b798eb70c46vandwalle + ") num=(" + a.visibility.num24 7542451dbcc4f9641df188326215b204b798eb70c46vandwalle + "," + a.visibility.num5 + ")" 7552451dbcc4f9641df188326215b204b798eb70c46vandwalle + prefer + b.configKey() 7562451dbcc4f9641df188326215b204b798eb70c46vandwalle + " rssi=(" + b.visibility.rssi24 7572451dbcc4f9641df188326215b204b798eb70c46vandwalle + "," + b.visibility.rssi5 7582451dbcc4f9641df188326215b204b798eb70c46vandwalle + ") num=(" + b.visibility.num24 7592451dbcc4f9641df188326215b204b798eb70c46vandwalle + "," + b.visibility.num5 + ")" 7602451dbcc4f9641df188326215b204b798eb70c46vandwalle + " -> " + order); 7612451dbcc4f9641df188326215b204b798eb70c46vandwalle } 7622451dbcc4f9641df188326215b204b798eb70c46vandwalle 763f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 764f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 765f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 766833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle /** 767833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle * b/18490330 only use scorer for untrusted networks 768dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 769dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * int compareWifiConfigurationsWithScorer(WifiConfiguration a, WifiConfiguration b) { 770dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 771dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * boolean aIsActive = false; 772dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * boolean bIsActive = false; 773dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 774dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * // Apply Hysteresis : boost RSSI of current configuration before 775dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * // looking up the score 776dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * if (null != mCurrentConfigurationKey) { 777dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * if (a.configKey().equals(mCurrentConfigurationKey)) { 778dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * aIsActive = true; 779dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } else if (b.configKey().equals(mCurrentConfigurationKey)) { 780dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * bIsActive = true; 781dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } 782dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } 783dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * int scoreA = getConfigNetworkScore(a, mScanResultAutoJoinAge, aIsActive); 784dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * int scoreB = getConfigNetworkScore(b, mScanResultAutoJoinAge, bIsActive); 785dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 786dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * // Both configurations need to have a score for the scorer to be used 787dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * // ...and the scores need to be different:-) 788dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * if (scoreA == WifiNetworkScoreCache.INVALID_NETWORK_SCORE 789dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * || scoreB == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) { 790dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * if (VDBG) { 791dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * logDbg(" compareWifiConfigurationsWithScorer no-scores: " 792dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + a.configKey() 793dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + " " 794dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + b.configKey()); 795dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } 796dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * return 0; 797dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } 798dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 799dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * if (VDBG) { 800dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * String prefer = " = "; 801dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * if (scoreA < scoreB) { 802dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * prefer = " < "; 803dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } if (scoreA > scoreB) { 804dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * prefer = " > "; 805dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } 806dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * logDbg(" compareWifiConfigurationsWithScorer " + a.configKey() 807dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + " rssi=(" + a.visibility.rssi24 808dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + "," + a.visibility.rssi5 809dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + ") num=(" + a.visibility.num24 810dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + "," + a.visibility.num5 + ")" 811dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + " sc=" + scoreA 812dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + prefer + b.configKey() 813dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + " rssi=(" + b.visibility.rssi24 814dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + "," + b.visibility.rssi5 815dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + ") num=(" + b.visibility.num24 816dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + "," + b.visibility.num5 + ")" 817dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + " sc=" + scoreB 818dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * + " -> " + Integer.toString(scoreB - scoreA)); 819dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } 820dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 821dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * // If scoreA > scoreB, the comparison is descending hence the return value is negative 822dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * return scoreB - scoreA; 823dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * } 824833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle */ 8252451dbcc4f9641df188326215b204b798eb70c46vandwalle 826d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande int getSecurityScore(WifiConfiguration config) { 827d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande 828d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande if (TextUtils.isEmpty(config.SSID) == false) { 829d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) 830d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande || config.allowedKeyManagement.get(KeyMgmt.WPA_PSK) 831d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande || config.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)) { 832d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande /* enterprise or PSK networks get highest score */ 833d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande return 100; 834d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } else if (config.allowedKeyManagement.get(KeyMgmt.NONE)) { 835d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande /* open networks have lowest score */ 836d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande return 33; 837d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } 838d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } else if (TextUtils.isEmpty(config.FQDN) == false) { 839d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande /* passpoint networks have medium preference */ 840d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande return 66; 841d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } 842d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande 843d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande /* bad network */ 844d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande return 0; 845d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande } 846d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande 847f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) { 848f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 849f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean linked = false; 850f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 851453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null) 852453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (a.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED) 853453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (b.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)) { 8542451dbcc4f9641df188326215b204b798eb70c46vandwalle if ((a.linkedConfigurations.get(b.configKey(true)) != null) 8552451dbcc4f9641df188326215b204b798eb70c46vandwalle && (b.linkedConfigurations.get(a.configKey(true)) != null)) { 856f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle linked = true; 857f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 858f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 859f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 860f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.ephemeral && b.ephemeral == false) { 861f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 862b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurations ephemeral and prefers " + b.configKey() 863453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + a.configKey()); 864f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 865931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle return 1; // b is of higher priority - ascending 866f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 867f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (b.ephemeral && a.ephemeral == false) { 868f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 869b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurations ephemeral and prefers " + a.configKey() 870453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey()); 871f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 872931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle return -1; // a is of higher priority - descending 873f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 874f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 8752451dbcc4f9641df188326215b204b798eb70c46vandwalle // Apply RSSI, in the range [-5, +5] 8762451dbcc4f9641df188326215b204b798eb70c46vandwalle // after band adjustment, +n difference roughly corresponds to +10xn dBm 8772451dbcc4f9641df188326215b204b798eb70c46vandwalle order = order + compareWifiConfigurationsRSSI(a, b, mCurrentConfigurationKey); 878f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 8792451dbcc4f9641df188326215b204b798eb70c46vandwalle // If the configurations are not linked, compare by user's choice, only a 8802451dbcc4f9641df188326215b204b798eb70c46vandwalle // very high RSSI difference can then override the choice 8812451dbcc4f9641df188326215b204b798eb70c46vandwalle if (!linked) { 8822451dbcc4f9641df188326215b204b798eb70c46vandwalle int choice; 883f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 88421d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle choice = getConnectChoice(a, b, false); 8852451dbcc4f9641df188326215b204b798eb70c46vandwalle if (choice > 0) { 886931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // a is of higher priority - descending 8872451dbcc4f9641df188326215b204b798eb70c46vandwalle order = order - choice; 888f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 889b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurations prefers " + a.configKey() 890453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() 891b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + " due to user choice of " + choice 892b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + " order -> " + Integer.toString(order)); 893f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 894f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle if (a.visibility != null) { 895f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle a.visibility.lastChoiceBoost = choice; 896f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle a.visibility.lastChoiceConfig = b.configKey(); 897f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle } 8982451dbcc4f9641df188326215b204b798eb70c46vandwalle } 8992451dbcc4f9641df188326215b204b798eb70c46vandwalle 90021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle choice = getConnectChoice(b, a, false); 9012451dbcc4f9641df188326215b204b798eb70c46vandwalle if (choice > 0) { 902931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // a is of lower priority - ascending 9032451dbcc4f9641df188326215b204b798eb70c46vandwalle order = order + choice; 9044dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle if (VDBG) { 905b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurations prefers " + b.configKey() + " over " 906b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + a.configKey() + " due to user choice of " + choice 907b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + " order ->" + Integer.toString(order)); 908f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 909f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle if (b.visibility != null) { 910f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle b.visibility.lastChoiceBoost = choice; 911f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle b.visibility.lastChoiceConfig = a.configKey(); 912f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle } 913f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 914f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 915ede1310be531a84faa08f02c3fd243448dd936ddvandwalle 916f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order == 0) { 917931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // We don't know anything - pick the last seen i.e. K behavior 918931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // we should do this only for recently picked configurations 919f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.priority > b.priority) { 920931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // a is of higher priority - descending 9212451dbcc4f9641df188326215b204b798eb70c46vandwalle if (VDBG) { 922b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurations prefers -1 " + a.configKey() + " over " 923453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + b.configKey() + " due to priority"); 924f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 925f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 926f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -1; 927f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else if (a.priority < b.priority) { 928931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // a is of lower priority - ascending 9292451dbcc4f9641df188326215b204b798eb70c46vandwalle if (VDBG) { 930b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" compareWifiConfigurations prefers +1 " + b.configKey() + " over " 931453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + a.configKey() + " due to priority"); 932f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 9332451dbcc4f9641df188326215b204b798eb70c46vandwalle order = 1; 934f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 935f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 936f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 937f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String sorder = " == "; 938931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle if (order > 0) { 939f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " < "; 940931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle } else if (order < 0) { 941f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " > "; 942931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle } 943f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 9442451dbcc4f9641df188326215b204b798eb70c46vandwalle if (VDBG) { 945b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg("compareWifiConfigurations: " + a.configKey() + sorder 946453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + b.configKey() + " order " + Integer.toString(order)); 947f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 948f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 949f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 950f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 951f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 952c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle boolean isBadCandidate(int rssi5, int rssi24) { 953c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle return (rssi5 < -80 && rssi24 < -90); 954c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle } 955c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle 956833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle /* 957c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle int compareWifiConfigurationsTop(WifiConfiguration a, WifiConfiguration b) { 958c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle int scorerOrder = compareWifiConfigurationsWithScorer(a, b); 959c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle int order = compareWifiConfigurations(a, b); 960c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle 961c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle if (scorerOrder * order < 0) { 962b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle if (VDBG) { 963b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" -> compareWifiConfigurationsTop: " + 964b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle "scorer override " + scorerOrder + " " + order); 965b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle } 966c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // For debugging purpose, remember that an override happened 967c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // during that autojoin Attempt 968c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle didOverride = true; 969c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle a.numScorerOverride++; 970c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle b.numScorerOverride++; 971c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle } 972c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle 973c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle if (scorerOrder != 0) { 974c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // If the scorer came up with a result then use the scorer's result, else use 975c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // the order provided by the base comparison function 976c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle order = scorerOrder; 977c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle } 978c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle return order; 979c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle } 980833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle */ 981c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle 9829f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle public int rssiBoostFrom5GHzRssi(int rssi, String dbg) { 9830eebae7334d6129f7ca1344e4b20199794994358vandwalle if (!mWifiConfigStore.enable5GHzPreference) { 9840eebae7334d6129f7ca1344e4b20199794994358vandwalle return 0; 9850eebae7334d6129f7ca1344e4b20199794994358vandwalle } 986e67ec726c07410073575473c0f50dc737629f5davandwalle if (rssi 98777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist > mWifiConfigStore.bandPreferenceBoostThreshold5.get()) { 988e67ec726c07410073575473c0f50dc737629f5davandwalle // Boost by 2 dB for each point 989e67ec726c07410073575473c0f50dc737629f5davandwalle // Start boosting at -65 990e67ec726c07410073575473c0f50dc737629f5davandwalle // Boost by 20 if above -55 991e67ec726c07410073575473c0f50dc737629f5davandwalle // Boost by 40 if abore -45 9920eebae7334d6129f7ca1344e4b20199794994358vandwalle int boost = mWifiConfigStore.bandPreferenceBoostFactor5 993dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * (rssi - mWifiConfigStore.bandPreferenceBoostThreshold5.get()); 994e67ec726c07410073575473c0f50dc737629f5davandwalle if (boost > 50) { 995815788ba7838fc54310baed3deb9b95548e0ce69vandwalle // 50 dB boost allows jumping from 2.4 to 5GHz 996e67ec726c07410073575473c0f50dc737629f5davandwalle // consistently 997e67ec726c07410073575473c0f50dc737629f5davandwalle boost = 50; 998e67ec726c07410073575473c0f50dc737629f5davandwalle } 9999f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle if (VDBG && dbg != null) { 1000f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle logDbg(" " + dbg + ": rssi5 " + rssi + " 5GHz-boost " + boost); 10019f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle } 1002e67ec726c07410073575473c0f50dc737629f5davandwalle return boost; 1003e67ec726c07410073575473c0f50dc737629f5davandwalle } 1004e67ec726c07410073575473c0f50dc737629f5davandwalle 1005e67ec726c07410073575473c0f50dc737629f5davandwalle if (rssi 100677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist < mWifiConfigStore.bandPreferencePenaltyThreshold5.get()) { 10070eebae7334d6129f7ca1344e4b20199794994358vandwalle // penalize if < -75 10080eebae7334d6129f7ca1344e4b20199794994358vandwalle int boost = mWifiConfigStore.bandPreferencePenaltyFactor5 1009dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * (rssi - mWifiConfigStore.bandPreferencePenaltyThreshold5.get()); 10100eebae7334d6129f7ca1344e4b20199794994358vandwalle return boost; 1011e67ec726c07410073575473c0f50dc737629f5davandwalle } 1012e67ec726c07410073575473c0f50dc737629f5davandwalle return 0; 1013e67ec726c07410073575473c0f50dc737629f5davandwalle } 1014dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle 1015dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle /** 1016dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * attemptRoam() function implements the core of the same SSID switching algorithm 1017dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 1018dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * Run thru all recent scan result of a WifiConfiguration and select the 1019dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * best one. 1020dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle */ 1021e67ec726c07410073575473c0f50dc737629f5davandwalle public ScanResult attemptRoam(ScanResult a, 1022e67ec726c07410073575473c0f50dc737629f5davandwalle WifiConfiguration current, int age, String currentBSSID) { 1023b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle if (current == null) { 1024dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 1025b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle logDbg("attemptRoam not associated"); 1026b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 1027e67ec726c07410073575473c0f50dc737629f5davandwalle return a; 10284dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 102925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande 10304d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande ScanDetailCache scanDetailCache = 103125ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande mWifiConfigStore.getScanDetailCache(current); 103225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande 103325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if (scanDetailCache == null) { 1034dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 1035b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle logDbg("attemptRoam no scan cache"); 1036b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 1037e67ec726c07410073575473c0f50dc737629f5davandwalle return a; 10384dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 103925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if (scanDetailCache.size() > 6) { 1040dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 1041c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle logDbg("attemptRoam scan cache size " 104225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande + scanDetailCache.size() + " --> bail"); 1043b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 1044931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Implement same SSID roaming only for configurations 1045c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle // that have less than 4 BSSIDs 1046e67ec726c07410073575473c0f50dc737629f5davandwalle return a; 10474dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 1048b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle 10492f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle if (current.BSSID != null && !current.BSSID.equals("any")) { 1050dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (DBG) { 10512f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle logDbg("attemptRoam() BSSID is set " 10522f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle + current.BSSID + " -> bail"); 1053b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 1054e67ec726c07410073575473c0f50dc737629f5davandwalle return a; 10554dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 10564dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 1057931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Determine which BSSID we want to associate to, taking account 1058c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle // relative strength of 5 and 2.4 GHz BSSIDs 10592451dbcc4f9641df188326215b204b798eb70c46vandwalle long nowMs = System.currentTimeMillis(); 10604dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 106125ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande for (ScanDetail sd : scanDetailCache.values()) { 106225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanResult b = sd.getScanResult(); 106397b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle int bRssiBoost5 = 0; 106497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle int aRssiBoost5 = 0; 106597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle int bRssiBoost = 0; 106697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle int aRssiBoost = 0; 106725ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if ((sd.getSeen() == 0) || (b.BSSID == null) 106825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande || ((nowMs - sd.getSeen()) > age) 1069e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle || b.autoJoinStatus != ScanResult.ENABLED 1070e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle || b.numIpConfigFailures > 8) { 10714dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle continue; 10723a2a3d226881cce8a4e511302231d843b0def303vandwalle } 10734dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 1074931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Pick first one 10754dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle if (a == null) { 10764dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle a = b; 10774dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle continue; 10784dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 10794dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 1080e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle if (b.numIpConfigFailures < (a.numIpConfigFailures - 1)) { 1081e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle // Prefer a BSSID that doesn't have less number of Ip config failures 1082e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle logDbg("attemptRoam: " 1083dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + b.BSSID + " rssi=" + b.level + " ipfail=" + b.numIpConfigFailures 1084e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " freq=" + b.frequency 1085e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " > " 1086dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + a.BSSID + " rssi=" + a.level + " ipfail=" + a.numIpConfigFailures 1087e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " freq=" + a.frequency); 1088e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle a = b; 1089e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle continue; 1090e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle } 1091e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle 10922451dbcc4f9641df188326215b204b798eb70c46vandwalle // Apply hysteresis: we favor the currentBSSID by giving it a boost 10937806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle if (currentBSSID != null && currentBSSID.equals(b.BSSID)) { 1094931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Reduce the benefit of hysteresis if RSSI <= -75 109577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (b.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5.get()) { 10960eebae7334d6129f7ca1344e4b20199794994358vandwalle bRssiBoost = mWifiConfigStore.associatedHysteresisLow; 1097c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle } else { 10980eebae7334d6129f7ca1344e4b20199794994358vandwalle bRssiBoost = mWifiConfigStore.associatedHysteresisHigh; 1099c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle } 11004dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 11017806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle if (currentBSSID != null && currentBSSID.equals(a.BSSID)) { 110277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (a.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5.get()) { 1103931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Reduce the benefit of hysteresis if RSSI <= -75 11040eebae7334d6129f7ca1344e4b20199794994358vandwalle aRssiBoost = mWifiConfigStore.associatedHysteresisLow; 1105c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle } else { 11060eebae7334d6129f7ca1344e4b20199794994358vandwalle aRssiBoost = mWifiConfigStore.associatedHysteresisHigh; 1107c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle } 11084dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 11092451dbcc4f9641df188326215b204b798eb70c46vandwalle 111097b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle // Favor 5GHz: give a boost to 5GHz BSSIDs, with a slightly progressive curve 11112451dbcc4f9641df188326215b204b798eb70c46vandwalle // Boost the BSSID if it is on 5GHz, above a threshold 11122451dbcc4f9641df188326215b204b798eb70c46vandwalle // But penalize it if it is on 5GHz and below threshold 111397b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle // 111497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle // With he current threshold values, 5GHz network with RSSI above -55 111597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle // Are given a boost of 30DB which is enough to overcome the current BSSID 111697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle // hysteresis (+14) plus 2.4/5 GHz signal strength difference on most cases 11179f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle // 11189f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle // The "current BSSID" Boost must be added to the BSSID's level so as to introduce\ 11199f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle // soem amount of hysteresis 1120e67ec726c07410073575473c0f50dc737629f5davandwalle if (b.is5GHz()) { 11219f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle bRssiBoost5 = rssiBoostFrom5GHzRssi(b.level + bRssiBoost, b.BSSID); 11224dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 1123e67ec726c07410073575473c0f50dc737629f5davandwalle if (a.is5GHz()) { 11249f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle aRssiBoost5 = rssiBoostFrom5GHzRssi(a.level + aRssiBoost, a.BSSID); 11254dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 11264dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 1127dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 1128c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle String comp = " < "; 1129dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (b.level + bRssiBoost + bRssiBoost5 > a.level + aRssiBoost + aRssiBoost5) { 1130c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle comp = " > "; 1131c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle } 11324dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle logDbg("attemptRoam: " 1133c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle + b.BSSID + " rssi=" + b.level + " boost=" + Integer.toString(bRssiBoost) 113497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle + "/" + Integer.toString(bRssiBoost5) + " freq=" + b.frequency 113597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle + comp 1136c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle + a.BSSID + " rssi=" + a.level + " boost=" + Integer.toString(aRssiBoost) 1137c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle + "/" + Integer.toString(aRssiBoost5) + " freq=" + a.frequency); 1138c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle } 1139c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle 11402451dbcc4f9641df188326215b204b798eb70c46vandwalle // Compare the RSSIs after applying the hysteresis boost and the 5GHz 11412451dbcc4f9641df188326215b204b798eb70c46vandwalle // boost if applicable 1142dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (b.level + bRssiBoost + bRssiBoost5 > a.level + aRssiBoost + aRssiBoost5) { 1143931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // b is the better BSSID 1144c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle a = b; 11454dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 11464dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 11473a2a3d226881cce8a4e511302231d843b0def303vandwalle if (a != null) { 1148dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 11497806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle StringBuilder sb = new StringBuilder(); 1150e67ec726c07410073575473c0f50dc737629f5davandwalle sb.append("attemptRoam: " + current.configKey() + 1151e67ec726c07410073575473c0f50dc737629f5davandwalle " Found " + a.BSSID + " rssi=" + a.level + " freq=" + a.frequency); 11527806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle if (currentBSSID != null) { 11537806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle sb.append(" Current: " + currentBSSID); 11547806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle } 11557806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle sb.append("\n"); 11567806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle logDbg(sb.toString()); 11573a2a3d226881cce8a4e511302231d843b0def303vandwalle } 11584dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 11593a2a3d226881cce8a4e511302231d843b0def303vandwalle return a; 11604dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 11614dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 1162931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 11632451dbcc4f9641df188326215b204b798eb70c46vandwalle * getNetworkScore() 1164dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 11652451dbcc4f9641df188326215b204b798eb70c46vandwalle * if scorer is present, get the network score of a WifiConfiguration 1166dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * <p/> 11672451dbcc4f9641df188326215b204b798eb70c46vandwalle * Note: this should be merge with setVisibility 11682451dbcc4f9641df188326215b204b798eb70c46vandwalle * 11692451dbcc4f9641df188326215b204b798eb70c46vandwalle * @param config 11702451dbcc4f9641df188326215b204b798eb70c46vandwalle * @return score 11712451dbcc4f9641df188326215b204b798eb70c46vandwalle */ 117281c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson int getConfigNetworkScore(WifiConfiguration config, int age, boolean isActive) { 11732451dbcc4f9641df188326215b204b798eb70c46vandwalle 11742451dbcc4f9641df188326215b204b798eb70c46vandwalle if (mNetworkScoreCache == null) { 1175e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle if (VDBG) { 1176b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" getConfigNetworkScore for " + config.configKey() 1177e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle + " -> no scorer, hence no scores"); 1178e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle } 11791db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle return WifiNetworkScoreCache.INVALID_NETWORK_SCORE; 11801db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle } 118125ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande 118225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if (mWifiConfigStore.getScanDetailCache(config) == null) { 1183e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle if (VDBG) { 1184b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" getConfigNetworkScore for " + config.configKey() 1185e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle + " -> no scan cache"); 1186e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle } 11871db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle return WifiNetworkScoreCache.INVALID_NETWORK_SCORE; 11882451dbcc4f9641df188326215b204b798eb70c46vandwalle } 11892451dbcc4f9641df188326215b204b798eb70c46vandwalle 11902451dbcc4f9641df188326215b204b798eb70c46vandwalle // Get current date 11912451dbcc4f9641df188326215b204b798eb70c46vandwalle long nowMs = System.currentTimeMillis(); 11922451dbcc4f9641df188326215b204b798eb70c46vandwalle 11931db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle int startScore = -10000; 11941db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle 11952451dbcc4f9641df188326215b204b798eb70c46vandwalle // Run thru all cached scan results 119625ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande for (ScanDetail sd : mWifiConfigStore.getScanDetailCache(config).values()) { 119725ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanResult result = sd.getScanResult(); 119825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if ((nowMs - sd.getSeen()) < age) { 119981c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson int sc = mNetworkScoreCache.getNetworkScore(result, isActive); 12001db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle if (sc > startScore) { 12011db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle startScore = sc; 12022451dbcc4f9641df188326215b204b798eb70c46vandwalle } 12032451dbcc4f9641df188326215b204b798eb70c46vandwalle } 12042451dbcc4f9641df188326215b204b798eb70c46vandwalle } 12051db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle if (startScore == -10000) { 12061db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle startScore = WifiNetworkScoreCache.INVALID_NETWORK_SCORE; 12071db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle } 1208e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle if (VDBG) { 1209e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle if (startScore == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) { 1210b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" getConfigNetworkScore for " + config.configKey() 1211e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle + " -> no available score"); 1212e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle } else { 1213b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg(" getConfigNetworkScore for " + config.configKey() 121481c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson + " isActive=" + isActive 1215e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle + " score = " + Integer.toString(startScore)); 1216e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle } 1217e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle } 1218e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle 12191db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle return startScore; 12202451dbcc4f9641df188326215b204b798eb70c46vandwalle } 12212451dbcc4f9641df188326215b204b798eb70c46vandwalle 12222451dbcc4f9641df188326215b204b798eb70c46vandwalle /** 12238639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson * Set whether connections to untrusted connections are allowed. 12248639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson */ 12258639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson void setAllowUntrustedConnections(boolean allow) { 12268639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson boolean changed = mAllowUntrustedConnections != allow; 12278639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson mAllowUntrustedConnections = allow; 12288639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson if (changed) { 1229005c1ef113192f898499a407dd266393a8d6b076vandwalle // Trigger a scan so as to reattempt autojoin 1230005c1ef113192f898499a407dd266393a8d6b076vandwalle mWifiStateMachine.startScanForUntrustedSettingChange(); 12318639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson } 12328639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson } 12338639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson 12348639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson private boolean isOpenNetwork(ScanResult result) { 12358639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson return !result.capabilities.contains("WEP") && 12368639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson !result.capabilities.contains("PSK") && 12378639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson !result.capabilities.contains("EAP"); 12388639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson } 12398639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson 124016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson private boolean haveRecentlySeenScoredBssid(WifiConfiguration config) { 124116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson long ephemeralOutOfRangeTimeoutMs = Settings.Global.getLong( 124216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson mContext.getContentResolver(), 124316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson Settings.Global.WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS, 124416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS); 124516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson 124616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson // Check whether the currently selected network has a score curve. If 124716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson // ephemeralOutOfRangeTimeoutMs is <= 0, then this is all we check, and we stop here. 124816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson // Otherwise, we stop here if the currently selected network has a score. If it doesn't, we 124916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson // keep going - it could be that another BSSID is in range (has been seen recently) which 125016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson // has a score, even if the one we're immediately connected to doesn't. 1251dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle ScanResult currentScanResult = mWifiStateMachine.getCurrentScanResult(); 1252be4d4bfef88bb0870e1e8bb464c302a2e54bbed4Pierre Vandwalle boolean currentNetworkHasScoreCurve = currentScanResult != null 1253be4d4bfef88bb0870e1e8bb464c302a2e54bbed4Pierre Vandwalle && mNetworkScoreCache.hasScoreCurve(currentScanResult); 125416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson if (ephemeralOutOfRangeTimeoutMs <= 0 || currentNetworkHasScoreCurve) { 125516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson if (DBG) { 125616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson if (currentNetworkHasScoreCurve) { 125716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson logDbg("Current network has a score curve, keeping network: " 125816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson + currentScanResult); 125916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } else { 126016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson logDbg("Current network has no score curve, giving up: " + config.SSID); 126116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 126216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 126316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson return currentNetworkHasScoreCurve; 126416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 126516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson 126625ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if (mWifiConfigStore.getScanDetailCache(config) == null 126725ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande || mWifiConfigStore.getScanDetailCache(config).isEmpty()) { 126816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson return false; 126916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 127016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson 127116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson long currentTimeMs = System.currentTimeMillis(); 127225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande for (ScanDetail sd : mWifiConfigStore.getScanDetailCache(config).values()) { 127325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanResult result = sd.getScanResult(); 127425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande if (currentTimeMs > sd.getSeen() 127525ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande && currentTimeMs - sd.getSeen() < ephemeralOutOfRangeTimeoutMs 127616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson && mNetworkScoreCache.hasScoreCurve(result)) { 127716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson if (DBG) { 127816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson logDbg("Found scored BSSID, keeping network: " + result.BSSID); 127916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 128016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson return true; 128116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 128216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 128316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson 128416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson if (DBG) { 128516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson logDbg("No recently scored BSSID found, giving up connection: " + config.SSID); 128616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 128716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson return false; 128816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson } 128916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson 129077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // After WifiStateMachine ask the supplicant to associate or reconnect 129177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // we might still obtain scan results from supplicant 129277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // however the supplicant state in the mWifiInfo and supplicant state tracker 129377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // are updated when we get the supplicant state change message which can be 129477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // processed after the SCAN_RESULT message, so at this point the framework doesn't 129577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // know that supplicant is ASSOCIATING. 129677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // A good fix for this race condition would be for the WifiStateMachine to add 129777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // a new transient state where it expects to get the supplicant message indicating 129877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // that it started the association process and within which critical operations 129977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // like autojoin should be deleted. 130077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 130177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // This transient state would remove the need for the roam Wathchdog which 130277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // basically does that. 130377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 130477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // At the moment, we just query the supplicant state synchronously with the 130577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // mWifiNative.status() command, which allow us to know that 130677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // supplicant has started association process, even though we didnt yet get the 130777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // SUPPLICANT_STATE_CHANGE message. 130877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 130977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist private static final List<String> ASSOC_STATES = Arrays.asList( 131077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist "ASSOCIATING", 131177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist "ASSOCIATED", 131277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist "FOUR_WAY_HANDSHAKE", 131377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist "GROUP_KEY_HANDSHAKE"); 131477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 131577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist private int getNetID(String wpaStatus) { 131677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (VDBG) { 131777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist logDbg("attemptAutoJoin() status=" + wpaStatus); 131877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 131977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 132077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist try { 132177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist int id = WifiConfiguration.INVALID_NETWORK_ID; 132277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist String state = null; 132377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist BufferedReader br = new BufferedReader(new StringReader(wpaStatus)); 132477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist String line; 1325dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle while ((line = br.readLine()) != null) { 132677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist int split = line.indexOf('='); 132777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (split < 0) { 132877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist continue; 132977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 133077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 133177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist String name = line.substring(0, split); 133277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (name.equals("id")) { 133377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist try { 1334dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle id = Integer.parseInt(line.substring(split + 1)); 133577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (state != null) { 133677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist break; 133777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 1338dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } catch (NumberFormatException nfe) { 133977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return WifiConfiguration.INVALID_NETWORK_ID; 134077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 1341dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } else if (name.equals("wpa_state")) { 1342dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle state = line.substring(split + 1); 134377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (ASSOC_STATES.contains(state)) { 134477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return WifiConfiguration.INVALID_NETWORK_ID; 1345dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } else if (id >= 0) { 134677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist break; 134777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 134877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 134977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 135077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return id; 1351dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } catch (IOException ioe) { 135277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return WifiConfiguration.INVALID_NETWORK_ID; // Won't happen 135377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 135477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 135577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 135677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist private boolean setCurrentConfigurationKey(WifiConfiguration currentConfig, 135777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist int supplicantNetId) { 135877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (currentConfig != null) { 135977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (supplicantNetId != currentConfig.networkId 136077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // https://b.corp.google.com/issue?id=16484607 136177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // mark this condition as an error only if the mismatched networkId are valid 136277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist && supplicantNetId != WifiConfiguration.INVALID_NETWORK_ID 136377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist && currentConfig.networkId != WifiConfiguration.INVALID_NETWORK_ID) { 136477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist logDbg("attemptAutoJoin() ERROR wpa_supplicant out of sync nid=" 136577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist + Integer.toString(supplicantNetId) + " WifiStateMachine=" 136677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist + Integer.toString(currentConfig.networkId)); 136777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist mWifiStateMachine.disconnectCommand(); 136877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return false; 136977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } else if (currentConfig.ephemeral && (!mAllowUntrustedConnections || 137077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist !haveRecentlySeenScoredBssid(currentConfig))) { 137177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // The current connection is untrusted (the framework added it), but we're either 137277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // no longer allowed to connect to such networks, the score has been nullified 137377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // since we connected, or the scored BSSID has gone out of range. 137477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // Drop the current connection and perform the rest of autojoin. 137577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist logDbg("attemptAutoJoin() disconnecting from unwanted ephemeral network"); 137677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist mWifiStateMachine.disconnectCommand(Process.WIFI_UID, 137777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist mAllowUntrustedConnections ? 1 : 0); 137877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return false; 137977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } else { 138077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist mCurrentConfigurationKey = currentConfig.configKey(); 138177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return true; 138277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 138377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } else { 138477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // If not invalid, then maybe in the process of associating, skip this attempt 138577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return supplicantNetId == WifiConfiguration.INVALID_NETWORK_ID; 138677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 138777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 138877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 138977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist private void updateBlackListStatus(WifiConfiguration config, long now) { 139077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // Wait for 5 minutes before reenabling config that have known, 139177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // repeated connection or DHCP failures 139277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (config.disableReason == WifiConfiguration.DISABLED_DHCP_FAILURE 139377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist || config.disableReason 139477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist == WifiConfiguration.DISABLED_ASSOCIATION_REJECT 139577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist || config.disableReason 139677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist == WifiConfiguration.DISABLED_AUTH_FAILURE) { 139777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (config.blackListTimestamp == 0 139877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist || (config.blackListTimestamp > now)) { 139977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // Sanitize the timestamp 140077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist config.blackListTimestamp = now; 140177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 140277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if ((now - config.blackListTimestamp) > 140377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist mWifiConfigStore.wifiConfigBlacklistMinTimeMilli) { 140477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // Re-enable the WifiConfiguration 140577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist config.status = WifiConfiguration.Status.ENABLED; 140677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 140777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // Reset the blacklist condition 140877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist config.numConnectionFailures = 0; 140977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist config.numIpConfigFailures = 0; 141077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist config.numAuthFailures = 0; 141177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED); 141277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 141377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist config.dirty = true; 141477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } else { 141577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (VDBG) { 141677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist long delay = mWifiConfigStore.wifiConfigBlacklistMinTimeMilli 141777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist - (now - config.blackListTimestamp); 141877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist logDbg("attemptautoJoin " + config.configKey() 141977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist + " dont unblacklist yet, waiting for " 142077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist + delay + " ms"); 142177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 142277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 142377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 142477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist // Avoid networks disabled because of AUTH failure altogether 142577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (DBG) { 142677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist logDbg("attemptAutoJoin skip candidate due to auto join status " 142777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist + Integer.toString(config.autoJoinStatus) + " key " 142877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist + config.configKey(true) 142977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist + " reason " + config.disableReason); 143077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 143177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 1432dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle 143377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist boolean underSoftThreshold(WifiConfiguration config) { 143477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return config.visibility.rssi24 < mWifiConfigStore.thresholdUnblacklistThreshold24Soft.get() 1435dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle && config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Soft.get(); 143677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 143777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 143877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist boolean underHardThreshold(WifiConfiguration config) { 143977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return config.visibility.rssi24 < mWifiConfigStore.thresholdUnblacklistThreshold24Hard.get() 1440dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle && config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Hard.get(); 144177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 144277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 144377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist boolean underThreshold(WifiConfiguration config, int rssi24, int rssi5) { 144477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return config.visibility.rssi24 < rssi24 && config.visibility.rssi5 < rssi5; 144577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 144677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 14478639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson /** 1448931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * attemptAutoJoin() function implements the core of the a network switching algorithm 144968fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle * Return false if no acceptable networks were found. 1450931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 145168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle boolean attemptAutoJoin() { 145268fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle boolean found = false; 1453c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle didOverride = false; 14541ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle didBailDueToWeakRssi = false; 1455b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle int networkSwitchType = AUTO_JOIN_IDLE; 1456e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle int age = mScanResultAutoJoinAge; 14574dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 14588242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle long now = System.currentTimeMillis(); 14598242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle 14608c9088d11880553458f09377cc60d6eb7e66747bvandwalle String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration(); 1461e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle if (lastSelectedConfiguration != null) { 1462e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle age = 14000; 1463e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle } 1464931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Reset the currentConfiguration Key, and set it only if WifiStateMachine and 1465453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle // supplicant agree 1466453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle mCurrentConfigurationKey = null; 1467453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle WifiConfiguration currentConfiguration = mWifiStateMachine.getCurrentWifiConfiguration(); 1468453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 1469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration candidate = null; 1470931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Obtain the subset of recently seen networks 1471833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle List<WifiConfiguration> list = 1472e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle mWifiConfigStore.getRecentConfiguredNetworks(age, false); 1473f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (list == null) { 1474dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) logDbg("attemptAutoJoin nothing known=" + 147531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist mWifiConfigStore.getConfiguredNetworksSize()); 147668fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle return false; 1477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 1479931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Find the currently connected network: ask the supplicant directly 148077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist int supplicantNetId = getNetID(mWifiNative.status(true)); 148149bc8dd35845318cd9bc5ce9064fc1ad33bbece8xinhe 1482ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 14837806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle String conf = ""; 1484b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle String last = ""; 14857806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle if (currentConfiguration != null) { 1486e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle conf = " current=" + currentConfiguration.configKey(); 1487b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle } 1488b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle if (lastSelectedConfiguration != null) { 14892f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle last = " last=" + lastSelectedConfiguration; 14907806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle } 1491ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size()) 1492b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + conf + last 1493b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + " ---> suppNetId=" + Integer.toString(supplicantNetId)); 1494ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 1495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 149677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (!setCurrentConfigurationKey(currentConfiguration, supplicantNetId)) { 149777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return false; 1498453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 1499453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 1500b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle int currentNetId = -1; 1501b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle if (currentConfiguration != null) { 1502931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // If we are associated to a configuration, it will 1503b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle // be compared thru the compareNetwork function 1504b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle currentNetId = currentConfiguration.networkId; 1505b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle } 1506b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle 1507931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 1508931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * Run thru all visible configurations without looking at the one we 1509c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle * are currently associated to 15104dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle * select Best Network candidate from known WifiConfigurations 1511931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 1512f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (WifiConfiguration config : list) { 1513e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle if (config.SSID == null) { 1514b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle continue; 1515e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle } 1516e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle 151777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (config.autoJoinStatus >= WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) { 151877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist updateBlackListStatus(config, now); 1519f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 1520f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1521f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 1522e487a4648dd41881e754f1224aaedead78a0777dSky Faber if (config.userApproved == WifiConfiguration.USER_PENDING || 1523e487a4648dd41881e754f1224aaedead78a0777dSky Faber config.userApproved == WifiConfiguration.USER_BANNED) { 1524e487a4648dd41881e754f1224aaedead78a0777dSky Faber if (DBG) { 1525e487a4648dd41881e754f1224aaedead78a0777dSky Faber logDbg("attemptAutoJoin skip candidate due to user approval status " 1526e487a4648dd41881e754f1224aaedead78a0777dSky Faber + WifiConfiguration.userApprovedAsString(config.userApproved) + " key " 1527e487a4648dd41881e754f1224aaedead78a0777dSky Faber + config.configKey(true)); 1528e487a4648dd41881e754f1224aaedead78a0777dSky Faber } 1529e487a4648dd41881e754f1224aaedead78a0777dSky Faber continue; 1530e487a4648dd41881e754f1224aaedead78a0777dSky Faber } 1531e487a4648dd41881e754f1224aaedead78a0777dSky Faber 1532931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Try to un-blacklist based on elapsed time 153327355a942653264388e909a4276196ee63e57811vandwalle if (config.blackListTimestamp > 0) { 153427355a942653264388e909a4276196ee63e57811vandwalle if (now < config.blackListTimestamp) { 1535931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 1536931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * looks like there was a change in the system clock since we black listed, and 1537931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * timestamp is not meaningful anymore, hence lose it. 1538931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * this event should be rare enough so that we still want to lose the black list 15392451dbcc4f9641df188326215b204b798eb70c46vandwalle */ 154027355a942653264388e909a4276196ee63e57811vandwalle config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED); 154127355a942653264388e909a4276196ee63e57811vandwalle } else { 15424dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle if ((now - config.blackListTimestamp) > loseBlackListHardMilli) { 1543931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Reenable it after 18 hours, i.e. next day 154427355a942653264388e909a4276196ee63e57811vandwalle config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED); 15454dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } else if ((now - config.blackListTimestamp) > loseBlackListSoftMilli) { 1546931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Lose blacklisting due to bad link 154727355a942653264388e909a4276196ee63e57811vandwalle config.setAutoJoinStatus(config.autoJoinStatus - 8); 154827355a942653264388e909a4276196ee63e57811vandwalle } 154927355a942653264388e909a4276196ee63e57811vandwalle } 155027355a942653264388e909a4276196ee63e57811vandwalle } 155127355a942653264388e909a4276196ee63e57811vandwalle 155277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (config.visibility == null) { 155377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist continue; 155477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 155577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 1556931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Try to unblacklist based on good visibility 155777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (underSoftThreshold(config)) { 155821d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (DBG) { 155921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle logDbg("attemptAutoJoin do not unblacklist due to low visibility " + 156021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle config.configKey() + " status=" + config.autoJoinStatus); 156121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 156277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } else if (underHardThreshold(config)) { 1563931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // If the network is simply temporary disabled, don't allow reconnect until 1564931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // RSSI becomes good enough 156527355a942653264388e909a4276196ee63e57811vandwalle config.setAutoJoinStatus(config.autoJoinStatus - 1); 156621d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (DBG) { 156721d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle logDbg("attemptAutoJoin good candidate seen, bumped soft -> status=" + 156821d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle config.configKey() + " status=" + config.autoJoinStatus); 156921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 157027355a942653264388e909a4276196ee63e57811vandwalle } else { 1571c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle config.setAutoJoinStatus(config.autoJoinStatus - 3); 157221d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (DBG) { 157321d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle logDbg("attemptAutoJoin good candidate seen, bumped hard -> status=" + 157421d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle config.configKey() + " status=" + config.autoJoinStatus); 157521d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 157627355a942653264388e909a4276196ee63e57811vandwalle } 157727355a942653264388e909a4276196ee63e57811vandwalle 157827355a942653264388e909a4276196ee63e57811vandwalle if (config.autoJoinStatus >= 157927355a942653264388e909a4276196ee63e57811vandwalle WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED) { 1580931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Network is blacklisted, skip 158121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (DBG) { 158221d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle logDbg("attemptAutoJoin skip blacklisted -> status=" + 158321d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle config.configKey() + " status=" + config.autoJoinStatus); 158421d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 158527355a942653264388e909a4276196ee63e57811vandwalle continue; 158627355a942653264388e909a4276196ee63e57811vandwalle } 1587f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.networkId == currentNetId) { 1588ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 158921bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle logDbg("attemptAutoJoin skip current candidate " 159021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle + Integer.toString(currentNetId) 1591ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + " key " + config.configKey(true)); 1592ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 1593f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 1594f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1595f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 15960eebae7334d6129f7ca1344e4b20199794994358vandwalle boolean isLastSelected = false; 15970eebae7334d6129f7ca1344e4b20199794994358vandwalle if (lastSelectedConfiguration != null && 15980eebae7334d6129f7ca1344e4b20199794994358vandwalle config.configKey().equals(lastSelectedConfiguration)) { 15990eebae7334d6129f7ca1344e4b20199794994358vandwalle isLastSelected = true; 16000eebae7334d6129f7ca1344e4b20199794994358vandwalle } 16010eebae7334d6129f7ca1344e4b20199794994358vandwalle 16024fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle if (config.lastRoamingFailure != 0 16034fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle && currentConfiguration != null 16044fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle && (lastSelectedConfiguration == null 16054fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle || !config.configKey().equals(lastSelectedConfiguration))) { 16064fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle // Apply blacklisting for roaming to this config if: 16074fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle // - the target config had a recent roaming failure 16084fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle // - we are currently associated 16094fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle // - the target config is not the last selected 16104fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle if (now > config.lastRoamingFailure 16114fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle && (now - config.lastRoamingFailure) 16124fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle < config.roamingFailureBlackListTimeMilli) { 16134fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle if (DBG) { 16144fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle logDbg("compareNetwork not switching to " + config.configKey() 16154fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle + " from current " + currentConfiguration.configKey() 16164fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle + " because it is blacklisted due to roam failure, " 16174fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle + " blacklist remain time = " 16184fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle + (now - config.lastRoamingFailure) + " ms"); 16194fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle } 16204fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle continue; 16214fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle } 16224fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle } 16234fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle 16241ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle int boost = config.autoJoinUseAggressiveJoinAttemptThreshold + weakRssiBailCount; 162577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (underThreshold(config, 162677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist mWifiConfigStore.thresholdInitialAutoJoinAttemptMin24RSSI.get() - boost, 162777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist mWifiConfigStore.thresholdInitialAutoJoinAttemptMin5RSSI.get() - boost)) { 162877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 162921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (DBG) { 163021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle logDbg("attemptAutoJoin skip due to low visibility " + config.configKey()); 163121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 16320eebae7334d6129f7ca1344e4b20199794994358vandwalle 1633c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // Don't try to autojoin a network that is too far but 1634c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // If that configuration is a user's choice however, try anyway 16350eebae7334d6129f7ca1344e4b20199794994358vandwalle if (!isLastSelected) { 16360eebae7334d6129f7ca1344e4b20199794994358vandwalle config.autoJoinBailedDueToLowRssi = true; 16371ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle didBailDueToWeakRssi = true; 16388c9088d11880553458f09377cc60d6eb7e66747bvandwalle continue; 16390eebae7334d6129f7ca1344e4b20199794994358vandwalle } else { 16400eebae7334d6129f7ca1344e4b20199794994358vandwalle // Next time, try to be a bit more aggressive in auto-joining 16410eebae7334d6129f7ca1344e4b20199794994358vandwalle if (config.autoJoinUseAggressiveJoinAttemptThreshold 1642dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle < WifiConfiguration.MAX_INITIAL_AUTO_JOIN_RSSI_BOOST 1643dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle && config.autoJoinBailedDueToLowRssi) { 1644dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle config.autoJoinUseAggressiveJoinAttemptThreshold += 4; 16454dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 16468c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 16470eebae7334d6129f7ca1344e4b20199794994358vandwalle } 1648d30127db46224e45554f8964209221bba8ad41d9vandwalle if (config.numNoInternetAccessReports > 0 1649d30127db46224e45554f8964209221bba8ad41d9vandwalle && !isLastSelected 1650d30127db46224e45554f8964209221bba8ad41d9vandwalle && !config.validatedInternetAccess) { 1651d30127db46224e45554f8964209221bba8ad41d9vandwalle // Avoid autoJoining this network because last time we used it, it didn't 1652d30127db46224e45554f8964209221bba8ad41d9vandwalle // have internet access, and we never manage to validate internet access on this 1653d30127db46224e45554f8964209221bba8ad41d9vandwalle // network configuration 16540eebae7334d6129f7ca1344e4b20199794994358vandwalle if (DBG) { 1655d30127db46224e45554f8964209221bba8ad41d9vandwalle logDbg("attemptAutoJoin skip candidate due to no InternetAccess " 1656d30127db46224e45554f8964209221bba8ad41d9vandwalle + config.configKey(true) 1657d30127db46224e45554f8964209221bba8ad41d9vandwalle + " num reports " + config.numNoInternetAccessReports); 16589f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle } 16590eebae7334d6129f7ca1344e4b20199794994358vandwalle continue; 16608c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 16618c9088d11880553458f09377cc60d6eb7e66747bvandwalle 1662ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 1663e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle String cur = ""; 1664e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle if (candidate != null) { 1665e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle cur = " current candidate " + candidate.configKey(); 1666e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle } 1667e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle logDbg("attemptAutoJoin trying id=" 1668c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle + Integer.toString(config.networkId) + " " 1669b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + config.configKey(true) 1670e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + " status=" + config.autoJoinStatus 1671e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle + cur); 1672ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 1673f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 1674f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate == null) { 1675f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 1676f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 1677dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 1678453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("attemptAutoJoin will compare candidate " + candidate.configKey() 16794dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle + " with " + config.configKey()); 1680f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 16814d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande 1682833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle int order = compareWifiConfigurations(candidate, config); 16834d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande if (VDBG) { 16844d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande logDbg("attemptAutoJoin compareWifiConfigurations returned " + order); 16854d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande } 16862451dbcc4f9641df188326215b204b798eb70c46vandwalle 16872451dbcc4f9641df188326215b204b798eb70c46vandwalle // The lastSelectedConfiguration is the configuration the user has manually selected 1688c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle // thru WifiPicker, or that a 3rd party app asked us to connect to via the 16892451dbcc4f9641df188326215b204b798eb70c46vandwalle // enableNetwork with disableOthers=true WifiManager API 16902451dbcc4f9641df188326215b204b798eb70c46vandwalle // As this is a direct user choice, we strongly prefer this configuration, 16912451dbcc4f9641df188326215b204b798eb70c46vandwalle // hence give +/-100 16922451dbcc4f9641df188326215b204b798eb70c46vandwalle if ((lastSelectedConfiguration != null) 16932451dbcc4f9641df188326215b204b798eb70c46vandwalle && candidate.configKey().equals(lastSelectedConfiguration)) { 16942451dbcc4f9641df188326215b204b798eb70c46vandwalle // candidate is the last selected configuration, 16952451dbcc4f9641df188326215b204b798eb70c46vandwalle // so keep it above connect choices (+/-60) and 16962451dbcc4f9641df188326215b204b798eb70c46vandwalle // above RSSI/scorer based selection of linked configuration (+/- 50) 16972451dbcc4f9641df188326215b204b798eb70c46vandwalle // by reducing order by -100 16982451dbcc4f9641df188326215b204b798eb70c46vandwalle order = order - 100; 1699dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 17002f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle logDbg(" ...and prefers -100 " + candidate.configKey() 17012451dbcc4f9641df188326215b204b798eb70c46vandwalle + " over " + config.configKey() 17022451dbcc4f9641df188326215b204b798eb70c46vandwalle + " because it is the last selected -> " 17032451dbcc4f9641df188326215b204b798eb70c46vandwalle + Integer.toString(order)); 17042451dbcc4f9641df188326215b204b798eb70c46vandwalle } 17052451dbcc4f9641df188326215b204b798eb70c46vandwalle } else if ((lastSelectedConfiguration != null) 17062451dbcc4f9641df188326215b204b798eb70c46vandwalle && config.configKey().equals(lastSelectedConfiguration)) { 17072451dbcc4f9641df188326215b204b798eb70c46vandwalle // config is the last selected configuration, 17082451dbcc4f9641df188326215b204b798eb70c46vandwalle // so keep it above connect choices (+/-60) and 17092451dbcc4f9641df188326215b204b798eb70c46vandwalle // above RSSI/scorer based selection of linked configuration (+/- 50) 17102451dbcc4f9641df188326215b204b798eb70c46vandwalle // by increasing order by +100 17112451dbcc4f9641df188326215b204b798eb70c46vandwalle order = order + 100; 1712dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (VDBG) { 17132f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle logDbg(" ...and prefers +100 " + config.configKey() 17142451dbcc4f9641df188326215b204b798eb70c46vandwalle + " over " + candidate.configKey() 17152451dbcc4f9641df188326215b204b798eb70c46vandwalle + " because it is the last selected -> " 17162451dbcc4f9641df188326215b204b798eb70c46vandwalle + Integer.toString(order)); 17172451dbcc4f9641df188326215b204b798eb70c46vandwalle } 17182451dbcc4f9641df188326215b204b798eb70c46vandwalle } 17192451dbcc4f9641df188326215b204b798eb70c46vandwalle 1720f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) { 1721931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Ascending : candidate < config 1722f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 1723f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1724f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1725f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1726f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 17272451dbcc4f9641df188326215b204b798eb70c46vandwalle // Now, go thru scan result to try finding a better untrusted network 1728833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle if (mNetworkScoreCache != null && mAllowUntrustedConnections) { 1729f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi5 = WifiConfiguration.INVALID_RSSI; 1730f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi24 = WifiConfiguration.INVALID_RSSI; 1731f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate != null) { 1732f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi5 = candidate.visibility.rssi5; 1733f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi24 = candidate.visibility.rssi24; 1734f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1735f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 1736931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Get current date 17372451dbcc4f9641df188326215b204b798eb70c46vandwalle long nowMs = System.currentTimeMillis(); 1738c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle int currentScore = -10000; 1739c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle // The untrusted network with highest score 174025ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanDetail untrustedCandidate = null; 17412451dbcc4f9641df188326215b204b798eb70c46vandwalle // Look for untrusted scored network only if the current candidate is bad 1742c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle if (isBadCandidate(rssi24, rssi5)) { 174325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande for (ScanDetail scanDetail : scanResultCache.values()) { 174425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande ScanResult result = scanDetail.getScanResult(); 1745c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle // We look only at untrusted networks with a valid SSID 1746c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle // A trusted result would have been looked at thru it's Wificonfiguration 17478639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson if (TextUtils.isEmpty(result.SSID) || !result.untrusted || 17488639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson !isOpenNetwork(result)) { 1749c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle continue; 1750c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle } 17512c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson String quotedSSID = "\"" + result.SSID + "\""; 17522c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson if (mWifiConfigStore.mDeletedEphemeralSSIDs.contains(quotedSSID)) { 17532ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle // SSID had been Forgotten by user, then don't score it 17542ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle continue; 17552ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle } 1756833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle if ((nowMs - result.seen) < mScanResultAutoJoinAge) { 1757c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle // Increment usage count for the network 17582c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson mWifiConnectionStatistics.incrementOrAddUntrusted(quotedSSID, 0, 1); 1759c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle 17602c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson boolean isActiveNetwork = currentConfiguration != null 17612c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson && currentConfiguration.SSID.equals(quotedSSID); 176281c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson int score = mNetworkScoreCache.getNetworkScore(result, isActiveNetwork); 1763c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle if (score != WifiNetworkScoreCache.INVALID_NETWORK_SCORE 1764c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle && score > currentScore) { 1765c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle // Highest score: Select this candidate 1766c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle currentScore = score; 176725ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande untrustedCandidate = scanDetail; 1768c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle if (VDBG) { 1769c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle logDbg("AutoJoinController: found untrusted candidate " 1770c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle + result.SSID 1771dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " RSSI=" + result.level 1772dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " freq=" + result.frequency 1773dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle + " score=" + score); 1774c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle } 1775f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1776f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1777f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1778f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1779c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle if (untrustedCandidate != null) { 17808639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson // At this point, we have an untrusted network candidate. 17818639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson // Create the new ephemeral configuration and see if we should switch over 1782833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle candidate = 1783833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle mWifiConfigStore.wifiConfigurationFromScanResult(untrustedCandidate); 1784833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle candidate.allowedKeyManagement.set(KeyMgmt.NONE); 1785833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle candidate.ephemeral = true; 1786fb650bfb264b7ff31c927498faf8343b5895c6abPierre Vandwalle candidate.dirty = true; 1787c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle } 1788c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle } 1789b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle 17901ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle long lastUnwanted = 17911ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle System.currentTimeMillis() 17921ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle - mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp; 17931ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle if (candidate == null 17941ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle && lastSelectedConfiguration == null 17951ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle && currentConfiguration == null 17961ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle && didBailDueToWeakRssi 17971ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle && (mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp == 0 1798dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle || lastUnwanted > (1000 * 60 * 60 * 24 * 7)) 17991ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle ) { 18001ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // We are bailing out of autojoin although we are seeing a weak configuration, and 18011ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // - we didn't find another valid candidate 18021ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // - we are not connected 18031ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // - without a user network selection choice 18041ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // - ConnectivityService has not triggered an unwanted network disconnect 18051ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // on this device for a week (hence most likely there is no SIM card or cellular) 18061ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // If all those conditions are met, then boost the RSSI of the weak networks 18071ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle // that we are seeing so as we will eventually pick one 18081ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle if (weakRssiBailCount < 10) 18091ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle weakRssiBailCount += 1; 18101ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle } else { 18111ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle if (weakRssiBailCount > 0) 18121ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle weakRssiBailCount -= 1; 18131ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle } 18141ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle 1815931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 1816931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * If candidate is found, check the state of the connection so as 1817931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * to decide if we should be acting on this candidate and switching over 1818931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 1819e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle int networkDelta = compareNetwork(candidate, lastSelectedConfiguration); 1820b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle if (DBG && candidate != null) { 1821b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle String doSwitch = ""; 1822b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle String current = ""; 1823b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle if (networkDelta < 0) { 1824b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle doSwitch = " -> not switching"; 1825b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle } 1826b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle if (currentConfiguration != null) { 18272f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle current = " with current " + currentConfiguration.configKey(); 1828b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle } 1829b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle logDbg("attemptAutoJoin networkSwitching candidate " 1830b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle + candidate.configKey() 1831b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + current 1832c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle + " linked=" + (currentConfiguration != null 1833dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle && currentConfiguration.isLinked(candidate)) 1834b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + " : delta=" 1835b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + Integer.toString(networkDelta) + " " 1836b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle + doSwitch); 1837b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 18384dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 1839931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle /** 1840931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * Ask WifiStateMachine permission to switch : 1841931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * if user is currently streaming voice traffic, 1842931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle * then we should not be allowed to switch regardless of the delta 1843931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle */ 184477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (mWifiStateMachine.shouldSwitchNetwork(networkDelta)) { // !!! JNo: Here! 1845b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle if (mStaStaSupported) { 1846b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle logDbg("mStaStaSupported --> error do nothing now "); 1847b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } else { 1848b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle if (currentConfiguration != null && currentConfiguration.isLinked(candidate)) { 1849b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle networkSwitchType = AUTO_JOIN_EXTENDED_ROAMING; 1850b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } else { 1851b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle networkSwitchType = AUTO_JOIN_OUT_OF_NETWORK_ROAMING; 1852b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 1853b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle if (DBG) { 1854b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle logDbg("AutoJoin auto connect with netId " 1855b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle + Integer.toString(candidate.networkId) 1856b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle + " to " + candidate.configKey()); 1857b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 18582451dbcc4f9641df188326215b204b798eb70c46vandwalle if (didOverride) { 18592451dbcc4f9641df188326215b204b798eb70c46vandwalle candidate.numScorerOverrideAndSwitchedNetwork++; 18602451dbcc4f9641df188326215b204b798eb70c46vandwalle } 1861c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle candidate.numAssociation++; 1862e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle mWifiConnectionStatistics.numAutoJoinAttempt++; 18639f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle 18642c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson if (candidate.ephemeral) { 18652c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson // We found a new candidate that we are going to connect to, then 18662c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson // increase its connection count 18672c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson mWifiConnectionStatistics. 18682c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson incrementOrAddUntrusted(candidate.SSID, 1, 0); 18692c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson } 18702c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson 18712f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle if (candidate.BSSID == null || candidate.BSSID.equals("any")) { 18722f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // First step we selected the configuration we want to connect to 18732f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // Second step: Look for the best Scan result for this configuration 18742f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // TODO this algorithm should really be done in one step 18752f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle String currentBSSID = mWifiStateMachine.getCurrentBSSID(); 1876833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle ScanResult roamCandidate = 1877833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle attemptRoam(null, candidate, mScanResultAutoJoinAge, null); 18782f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle if (roamCandidate != null && currentBSSID != null 18792f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle && currentBSSID.equals(roamCandidate.BSSID)) { 18802f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // Sanity, we were already asociated to that candidate 18812f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle roamCandidate = null; 18822f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle } 18832f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle if (roamCandidate != null && roamCandidate.is5GHz()) { 18842f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // If the configuration hasn't a default BSSID selected, and the best 18852f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // candidate is 5GHZ, then select this candidate so as WifiStateMachine and 18862f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle // supplicant will pick it first 18872f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle candidate.autoJoinBSSID = roamCandidate.BSSID; 18882f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle if (VDBG) { 18892f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle logDbg("AutoJoinController: lock to 5GHz " 18902f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle + candidate.autoJoinBSSID 18912f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle + " RSSI=" + roamCandidate.level 18922f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle + " freq=" + roamCandidate.frequency); 18932f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle } 1894448c9536a302c58a79e271b1721c08b8882f800evandwalle } else { 1895448c9536a302c58a79e271b1721c08b8882f800evandwalle // We couldnt find a roam candidate 1896448c9536a302c58a79e271b1721c08b8882f800evandwalle candidate.autoJoinBSSID = "any"; 18979f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle } 18989f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle } 1899448c9536a302c58a79e271b1721c08b8882f800evandwalle mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT, 1900dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle candidate.networkId, networkSwitchType, candidate); 190168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle found = true; 19024dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle } 1903b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 19044dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle 1905d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle if (networkSwitchType == AUTO_JOIN_IDLE && !mWifiConfigStore.enableHalBasedPno.get()) { 1906e67ec726c07410073575473c0f50dc737629f5davandwalle String currentBSSID = mWifiStateMachine.getCurrentBSSID(); 1907931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle // Attempt same WifiConfiguration roaming 1908833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle ScanResult roamCandidate = 1909833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle attemptRoam(null, currentConfiguration, mScanResultAutoJoinAge, currentBSSID); 1910e67ec726c07410073575473c0f50dc737629f5davandwalle if (roamCandidate != null && currentBSSID != null 1911e67ec726c07410073575473c0f50dc737629f5davandwalle && currentBSSID.equals(roamCandidate.BSSID)) { 1912e67ec726c07410073575473c0f50dc737629f5davandwalle roamCandidate = null; 1913e67ec726c07410073575473c0f50dc737629f5davandwalle } 19142f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle if (roamCandidate != null && mWifiStateMachine.shouldSwitchNetwork(999)) { 1915b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle if (DBG) { 1916b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle logDbg("AutoJoin auto roam with netId " 1917b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle + Integer.toString(currentConfiguration.networkId) 1918b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle + " " + currentConfiguration.configKey() + " to BSSID=" 1919b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle + roamCandidate.BSSID + " freq=" + roamCandidate.frequency 19201ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle + " RSSI=" + roamCandidate.level); 1921b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle } 1922b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle networkSwitchType = AUTO_JOIN_ROAMING; 1923e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle mWifiConnectionStatistics.numAutoRoamAttempt++; 1924e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle 1925b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_ROAM, 1926dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle currentConfiguration.networkId, 1, roamCandidate); 192768fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle found = true; 1928f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1929f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1930b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle if (VDBG) logDbg("Done attemptAutoJoin status=" + Integer.toString(networkSwitchType)); 193168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle return found; 1932f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 193377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist 193477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist private void logDenial(String reason, WifiConfiguration config) { 193577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist if (!DBG) { 193677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist return; 193777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 193877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist logDbg(reason + config.toString()); 193977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist } 1940dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle 1941dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle WifiConfiguration getWifiConfiguration(WifiNative.WifiPnoNetwork network) { 1942dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (network.configKey != null) { 1943dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle return mWifiConfigStore.getWifiConfiguration(network.configKey); 1944dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 1945dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle return null; 1946dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 1947dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle 1948dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle ArrayList<WifiNative.WifiPnoNetwork> getPnoList(WifiConfiguration current) { 1949dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle int size = -1; 19509ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle ArrayList<WifiNative.WifiPnoNetwork> list = new ArrayList<WifiNative.WifiPnoNetwork>(); 1951dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle 1952b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle if (mWifiConfigStore.mCachedPnoList != null) { 1953b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle size = mWifiConfigStore.mCachedPnoList.size(); 1954dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 1955dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle 195621d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (DBG) { 195721d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle String s = ""; 195821d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (current != null) { 195921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle s = " for: " + current.configKey(); 196021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 196121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle Log.e(TAG, " get Pno List total size:" + size + s); 196221d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 1963dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle if (current != null) { 1964dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle String configKey = current.configKey(); 1965dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle /** 1966dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * If we are currently associated to a WifiConfiguration then include 1967dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle * only those networks that have a higher priority 1968dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle */ 1969b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle for (WifiNative.WifiPnoNetwork network : mWifiConfigStore.mCachedPnoList) { 1970dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle WifiConfiguration config = getWifiConfiguration(network); 19719ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle if (config == null) { 19729ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle continue; 19739ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle } 19749ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle if (config.autoJoinStatus 19759ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle >= WifiConfiguration.AUTO_JOIN_DISABLED_NO_CREDENTIALS) { 19769ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle continue; 19779ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle } 19789ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle 19799ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle if (!configKey.equals(network.configKey)) { 198021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle int choice = getConnectChoice(config, current, true); 1981b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle if (choice > 0) { 1982b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle // config is of higher priority 198321d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle if (DBG) { 198421d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle Log.e(TAG, " Pno List adding:" + network.configKey 198521d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle + " choice " + choice); 198621d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle } 1987dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle list.add(network); 1988b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle network.rssi_threshold = mWifiConfigStore.thresholdGoodRssi24.get(); 1989dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 1990dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 1991dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 1992dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } else { 19939ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle for (WifiNative.WifiPnoNetwork network : mWifiConfigStore.mCachedPnoList) { 19949ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle WifiConfiguration config = getWifiConfiguration(network); 19959ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle if (config == null) { 19969ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle continue; 19979ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle } 19989ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle if (config.autoJoinStatus 19999ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle >= WifiConfiguration.AUTO_JOIN_DISABLED_NO_CREDENTIALS) { 20009ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle continue; 20019ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle } 20029ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle list.add(network); 20039ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle network.rssi_threshold = mWifiConfigStore.thresholdGoodRssi24.get(); 20049ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle } 2005dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 2006dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle return list; 2007dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle } 2008f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle} 2009f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 2010