WifiAutoJoinController.java revision cf5b8eb8a08c45bd4a82f1f4bb789c8a1b08744f
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; 20f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 21f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.NetworkKey; 22f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.NetworkScoreManager; 230c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport android.net.WifiKey; 24f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.WifiConfiguration; 25f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.ScanResult; 26f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.WifiManager; 27f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 28f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock; 29f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.util.Log; 30f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 310c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport java.util.ArrayList; 320c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport java.util.Collection; 33f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.Iterator; 34f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.HashMap; 35f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.List; 36f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.Date; 37f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 38f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/** 39f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * AutoJoin controller is responsible for WiFi Connect decision 40f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 41f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * It runs in the thread context of WifiStateMachine 42f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 43f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 44f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepublic class WifiAutoJoinController { 45f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 46f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private Context mContext; 47f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiStateMachine mWifiStateMachine; 48f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiConfigStore mWifiConfigStore; 49f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiTrafficPoller mWifiTrafficPoller; 50f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiNative mWifiNative; 51f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 52f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private NetworkScoreManager scoreManager; 53f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiNetworkScoreCache mNetworkScoreCache; 54f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 55f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final String TAG = "WifiAutoJoinController "; 56ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle private static boolean DBG = false; 57ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle private static boolean VDBG = false; 58f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final boolean mStaStaSupported = false; 59f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final int SCAN_RESULT_CACHE_SIZE = 80; 60f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 61453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle private String mCurrentConfigurationKey = null; //used by autojoin 62f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 63f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private HashMap<String, ScanResult> scanResultCache = 64f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle new HashMap<String, ScanResult>(); 65f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 66f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiAutoJoinController(Context c, WifiStateMachine w, WifiConfigStore s, 67f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiTrafficPoller t, WifiNative n) { 68f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mContext = c; 69f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine = w; 70f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore = s; 71f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiTrafficPoller = t; 72f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiNative = n; 73f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mNetworkScoreCache = null; 7421bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle scoreManager = 7521bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE); 76f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager == null) 77f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("Registered scoreManager NULL " + " service " + Context.NETWORK_SCORE_SERVICE); 78f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 79f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager != null) { 80f13817203179f41620514718c8668ae7e418f8afJeff Davidson mNetworkScoreCache = new WifiNetworkScoreCache(mContext); 81f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache); 82f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 83f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("No network score service: Couldnt register as a WiFi score Manager, type=" 84f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(NetworkKey.TYPE_WIFI) 85f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " service " + Context.NETWORK_SCORE_SERVICE); 86f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mNetworkScoreCache = null; 87f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 88f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 89f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 90ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle void enableVerboseLogging(int verbose) { 91ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (verbose > 0 ) { 92abde872adced15dfb6781fb71959453d963326dbYuhao Zheng DBG = true; 93ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle VDBG = true; 94ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } else { 95abde872adced15dfb6781fb71959453d963326dbYuhao Zheng DBG = false; 96ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle VDBG = false; 97ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 98ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 99ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle 10021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle int mScanResultMaximumAge = 30000; /* milliseconds unit */ 101f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 102ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle /* 103ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * flush out scan results older than mScanResultMaximumAge 104ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * 105ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * */ 106f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private void ageScanResultsOut(int delay) { 107f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (delay <= 0) { 108f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle delay = mScanResultMaximumAge; //something sane 109f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 110f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Date now = new Date(); 111f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long milli = now.getTime(); 112f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 113f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("ageScanResultsOut delay " + Integer.valueOf(delay) + " size " 114f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.valueOf(scanResultCache.size()) + " now " + Long.valueOf(milli)); 115f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 116f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 117f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Iterator<HashMap.Entry<String,ScanResult>> iter = scanResultCache.entrySet().iterator(); 118f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle while (iter.hasNext()) { 119f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle HashMap.Entry<String,ScanResult> entry = iter.next(); 120f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ScanResult result = entry.getValue(); 121f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 122f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.seen + delay) < milli) { 123f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle iter.remove(); 124f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 125f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 126f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 127f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 128f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void addToScanCache(List<ScanResult> scanList) { 129f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration associatedConfig; 130f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 1310c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle ArrayList<NetworkKey> unknownScanResults = new ArrayList<NetworkKey>(); 1320c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle 133f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for(ScanResult result: scanList) { 1341fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng if (result.SSID == null) continue; 135f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.seen = System.currentTimeMillis(); 136f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 137f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ScanResult sr = scanResultCache.get(result.BSSID); 138f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (sr != null) { 139f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // if there was a previous cache result for this BSSID, average the RSSI values 140f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 141f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int previous_rssi = sr.level; 142f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long previously_seen_milli = sr.seen; 143f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 144f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* average RSSI with previously seen instances of this scan result */ 145f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int avg_rssi = result.level; 146f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 147f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((previously_seen_milli > 0) 148f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (previously_seen_milli < mScanResultMaximumAge/2)) { 149f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* 150f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 151f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * previously_seen_milli = 0 => RSSI = 0.5 * previous_seen_rssi + 0.5 * new_rssi 152f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 153f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * If previously_seen_milli is 15+ seconds old: 154f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * previously_seen_milli = 15000 => RSSI = new_rssi 155f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 156f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 157f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 158f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle double alpha = 0.5 - (double)previously_seen_milli 159f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle / (double)mScanResultMaximumAge; 160f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 161f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle avg_rssi = (int)((double)avg_rssi * (1-alpha) + (double)previous_rssi * alpha); 162f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 163f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.level = avg_rssi; 164f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 165f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //remove the previous Scan Result 166f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scanResultCache.remove(result.BSSID); 1670c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } else { 1680c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle if (!mNetworkScoreCache.isScoredNetwork(result)) { 16921bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle WifiKey wkey; 17021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle //TODO : find out how we can get there without a valid UTF-8 encoded SSID 17121bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle //TODO: which will cause WifiKey constructor to fail 17221bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle try { 17321bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle wkey = new WifiKey("\"" + result.SSID + "\"", result.BSSID); 17421bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle } catch (IllegalArgumentException e) { 17521bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle logDbg("AutoJoinController: received badly encoded SSID=[" + result.SSID + 17621bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle "] ->skipping this network"); 17721bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle wkey = null; 17821bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle } 17921bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle if (wkey != null) { 18021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle NetworkKey nkey = new NetworkKey(wkey); 18121bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle //if we don't know this scan result then request a score to Herrevad 18221bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle unknownScanResults.add(nkey); 18321bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle } 1840c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } 185f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 186f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 187f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scanResultCache.put(result.BSSID, new ScanResult(result)); 188f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 189f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ScanResult srn = scanResultCache.get(result.BSSID); 190f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 191f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //add this BSSID to the scanResultCache of the relevant WifiConfiguration 192f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle associatedConfig = mWifiConfigStore.updateSavedNetworkHistory(result); 193f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 1940c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle //try to associate this BSSID to an existing Saved WifiConfiguration 195f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (associatedConfig == null) { 196f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle associatedConfig = mWifiConfigStore.associateWithConfiguration(result); 1971fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng if (associatedConfig != null && associatedConfig.SSID != null) { 198f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 199f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("addToScanCache save associated config " 200f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + associatedConfig.SSID + " with " + associatedConfig.SSID); 201f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 202f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine.sendMessage(WifiManager.SAVE_NETWORK, associatedConfig); 203f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 204f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 205f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 2060c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle 2070c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle if (unknownScanResults.size() != 0) { 2080c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle NetworkKey[] newKeys = 2090c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle unknownScanResults.toArray(new NetworkKey[unknownScanResults.size()]); 2100c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle //kick the score manager, we will get updated scores asynchronously 2110c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle scoreManager.requestScores(newKeys); 2120c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } 213f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 214f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 215f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void logDbg(String message) { 2160888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle logDbg(message, false); 217ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 218ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 219ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle void logDbg(String message, boolean stackTrace) { 220f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long now = SystemClock.elapsedRealtimeNanos(); 2210888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle String ts = String.format("[%,d us] ", now / 1000); 222ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (stackTrace) { 223ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle Log.e(TAG, ts + message + " stack:" 224ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[2].getMethodName() + " - " 225ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[3].getMethodName() + " - " 226ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[4].getMethodName() + " - " 227ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[5].getMethodName()); 228ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } else { 229ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle Log.e(TAG, ts + message); 230ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 231f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 232f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 233f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* called directly from WifiStateMachine */ 234f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void newSupplicantResults() { 235f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<ScanResult> scanList = mWifiStateMachine.syncGetScanResultsList(); 236f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle addToScanCache(scanList); 237f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(mScanResultMaximumAge); 238f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) 239f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size()) ); 240f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 241f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 242f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 243f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 244f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 245f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 246f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 247f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* not used at the moment 248f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * should be a call back from WifiScanner HAL ?? 249f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * this function is not hooked and working yet, it will receive scan results from WifiScanners 250f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan 251f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * results as normal. 252f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 253f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void newHalScanResults() { 254f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<ScanResult> scanList = null;//mWifiScanner.syncGetScanResultsList(); 255f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String akm = WifiParser.parse_akm(null, null); 256f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg(akm); 257f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle addToScanCache(scanList); 258f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(0); 259f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 260f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 261f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 262f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 263f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* network link quality changed, called directly from WifiTrafficPoller, 264f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle or by listening to Link Quality intent */ 265f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void linkQualitySignificantChange() { 266f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 267f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 268f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 269f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* 270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * compare a WifiConfiguration against the current network, return a delta score 271f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * If not associated, and the candidate will always be better 272f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * For instance if the candidate is a home network versus an unknown public wifi, 273f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * the delta will be infinite, else compare Kepler scores etc… 274f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ***/ 275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private int compareNetwork(WifiConfiguration candidate) { 276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration(); 277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (currentNetwork == null) 278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 1000; 279f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 280f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate.configKey(true).equals(currentNetwork.configKey(true))) { 281f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return -1; 282f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 283f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 284f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = compareWifiConfigurations(currentNetwork, candidate); 285f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 286f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) { 287f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //ascending: currentNetwork < candidate 288f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 10; //will try switch over to the candidate 289f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 290f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 291f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 292f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 293f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 294ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle /** 295ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * update the network history fields fo that configuration 296ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * - if userTriggered, we mark the configuration as "non selfAdded" since the user has seen it 297ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * and took over management 298ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * - if it is a "connect", remember which network were there at the point of the connect, so 299ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * as those networks get a relative lower score than the selected configuration 30062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * 301ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param netId 302ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param userTriggered : if the update come from WiFiManager 303ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param connect : if the update includes a connect 304117be607246604e875de62aa8cdd99700b77a2b4vandwalle **/ 30562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) { 306f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId); 307f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected == null) { 308f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return; 309f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 310f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 311e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle if (selected.SSID == null) { 312e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle return; 313e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle } 314e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle 31562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (userTriggered) { 316ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // reenable autojoin for this network, 31762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle // since the user want to connect to this configuration 31862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.autoJoinStatus = WifiConfiguration.AUTO_JOIN_ENABLED; 31962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.selfAdded = false; 32062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 321f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 322992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle if (DBG && userTriggered) { 323f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected.connectChoices != null) { 324ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will update " 325f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(netId) + " now: " 326992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + Integer.toString(selected.connectChoices.size()) 327992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + " uid=" + Integer.toString(selected.creatorUid), true); 328f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 329ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will update " 330992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + Integer.toString(netId) 331992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + " uid=" + Integer.toString(selected.creatorUid), true); 332f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 333f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 334f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 335ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (connect && userTriggered) { 336ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle boolean found = false; 33762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle List<WifiConfiguration> networks = 33862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle mWifiConfigStore.getRecentConfiguredNetworks(12000, false); 33962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (networks != null) { 34062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle for (WifiConfiguration config : networks) { 341992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle if (DBG) { 342992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle logDbg("updateConfigurationHistory got " + config.SSID + " nid=" 343992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + Integer.toString(config.networkId)); 344992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle } 345f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 34662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.configKey(true).equals(config.configKey(true))) { 347ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle found = true; 34862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle continue; 34962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 350f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 35162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle int rssi = WifiConfiguration.INVALID_RSSI; 35262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.visibility != null) { 35362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle rssi = config.visibility.rssi5; 35462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.visibility.rssi24 > rssi) 35562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle rssi = config.visibility.rssi24; 35662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 35762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (rssi < -80) { 35862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle continue; 35962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 360f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 361ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //the selected configuration was preferred over a recently seen config 362ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //hence remember the user's choice: 363ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //add the recently seen config to the selected's connectChoices array 364ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 365ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (selected.connectChoices == null) { 366ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle selected.connectChoices = new HashMap<String, Integer>(); 367ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 368ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 369ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory add a choice " + selected.configKey(true) 3700888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle + " over " + config.configKey(true) 371992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle + " RSSI " + Integer.toString(rssi)); 372cf5b8eb8a08c45bd4a82f1f4bb789c8a1b08744fvandwalle 3730888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //add the visible config to the selected's connect choice list 37462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.connectChoices.put(config.configKey(true), rssi); 375f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 37662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.connectChoices != null) { 377ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (VDBG) { 378ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will remove " 37962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + selected.configKey(true) + " from " + config.configKey(true)); 380ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 3810888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //remove the selected from the recently seen config's connectChoice list 38262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle config.connectChoices.remove(selected.configKey(true)); 3830888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle 3840888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle if (selected.linkedConfigurations != null) { 3850888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //remove the selected's linked configuration from the 3860888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //recently seen config's connectChoice list 3870888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle for (String key : selected.linkedConfigurations.keySet()) { 3880888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle config.connectChoices.remove(key); 3890888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle } 3900888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle } 39162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 392ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 393ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (found == false) { 394ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // log an error for now but do something stringer later 395ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // we will need a new scan before attempting to connect to this 396ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // configuration anyhow and thus we can process the scan results then 397ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory try to connect to an old network!! : " 398ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + selected.configKey()); 39962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 400f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 40162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.connectChoices != null) { 40262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (VDBG) 403ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory " + Integer.toString(netId) 40462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + " now: " + Integer.toString(selected.connectChoices.size())); 40562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 406ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 407f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 408f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 409992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle 410992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle //TODO: write only if something changed 411992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle if (userTriggered || connect) { 412992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 413992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle } 414f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 415f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 416f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void printChoices(WifiConfiguration config) { 417f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int num = 0; 418f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices!= null) { 419f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle num = config.connectChoices.size(); 420f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 421f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 422f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("printChoices " + config.SSID + " num choices: " + Integer.toString(num)); 423f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices!= null) { 424f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : config.connectChoices.keySet()) { 425f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg(" " + key); 426f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 427f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 428f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 429f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 430f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean hasConnectChoice(WifiConfiguration source, WifiConfiguration target) { 431f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean found = false; 432f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source == null) 433f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return false; 434f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (target == null) 435f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return false; 436f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 437f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source.connectChoices != null) { 438f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ( source.connectChoices.get(target.configKey(true)) != null) { 439f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle found = true; 440f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 441f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 442f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 443f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source.linkedConfigurations != null) { 444f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : source.linkedConfigurations.keySet()) { 445f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key); 446f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config != null) { 447f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices != null) { 448f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices.get(target.configKey(true)) != null) { 449f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle found = true; 450f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 451f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 452f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 453f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 454f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 455f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return found; 456f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 457f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 458f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b) { 459f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 460f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int boost5 = 25; 461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility astatus = a.visibility; 463f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility bstatus = b.visibility; 464f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (astatus == null || bstatus == null) { 465453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //error -> cant happen, need to throw en exception 466f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations NULL band status!"); 467f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 468f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 469453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if ((astatus.rssi5 > -70) && (bstatus.rssi5 == WifiConfiguration.INVALID_RSSI) 470f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((astatus.rssi5+boost5) > (bstatus.rssi24))) { 471f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is seen on 5GHz with good RSSI, greater rssi than b 472f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 473f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -1; 474453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 == WifiConfiguration.INVALID_RSSI) 475f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((bstatus.rssi5+boost5) > (bstatus.rssi24))) { 476f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //b is seen on 5GHz with good RSSI, greater rssi than a 477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 1; 479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 480f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 481f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 482f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 483f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) { 484f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 485117be607246604e875de62aa8cdd99700b77a2b4vandwalle String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration(); 486f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean linked = false; 487f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 488453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null) 489453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (a.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED) 490453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (b.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)) { 491f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((a.linkedConfigurations.get(b.configKey(true))!= null) 492f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (b.linkedConfigurations.get(a.configKey(true))!= null)) { 493f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle linked = true; 494f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 496f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 497f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.ephemeral && b.ephemeral == false) { 498f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 499453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations ephemeral and prefers " + b.configKey() 500453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + a.configKey()); 501f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 502f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 1; //b is of higher priority - ascending 503f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 504f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (b.ephemeral && a.ephemeral == false) { 505f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 506453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations ephemeral and prefers " +a.configKey() 507453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey()); 508f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 509f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return -1; //a is of higher priority - descending 510f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 511f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 512f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int boost5 = 25; 513453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //apply Hysteresis: boost the RSSI value of the currently connected configuration 514453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle int aRssiBoost = 0; 515453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle int bRssiBoost = 0; 516453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (null != mCurrentConfigurationKey) { 517453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (a.configKey().equals(mCurrentConfigurationKey)) { 518453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle aRssiBoost += 10; 519453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else if (b.configKey().equals(mCurrentConfigurationKey)) { 520453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle bRssiBoost += 10; 521453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 522453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 523f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (linked) { 524f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // then we try prefer 5GHz, and try to ignore user's choice 525f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility astatus = a.visibility; 526f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility bstatus = b.visibility; 527f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (astatus == null || bstatus == null) { 528f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //error 529f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations NULL band status!"); 530f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 531f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 532f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 533f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 534f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations linked: " + Integer.toString(astatus.rssi5) 535f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + "," + Integer.toString(astatus.rssi24) + " " 536f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5) + "," 537f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi24)); 538f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 539f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 540453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if ((astatus.rssi5 > -70) && (bstatus.rssi5 <= WifiConfiguration.INVALID_RSSI) 541453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (astatus.rssi5+boost5+aRssiBoost) > (bstatus.rssi24+bRssiBoost)) { 542453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //in this case: a has 5GHz and b doesn't have 5GHz 543453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //compare a's 5GHz RSSI to b's 5GHz RSSI 544453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 545f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is seen on 5GHz with good RSSI, greater rssi than b 546f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 547f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -10; 548f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 549f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 550453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations linked and prefers " + a.configKey() 551453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() 552f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " due to 5GHz RSSI " + Integer.toString(astatus.rssi5) 553f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over: 5=" + Integer.toString(bstatus.rssi5) 554f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + ", 2.4=" + Integer.toString(bstatus.rssi5)); 555f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 556453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 <= WifiConfiguration.INVALID_RSSI) 557453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && ((bstatus.rssi5+boost5+bRssiBoost) > (astatus.rssi24+aRssiBoost))) { 558453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //in this case: b has 5GHz and a doesn't have 5GHz 559453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //b is seen on 5GHz with good RSSI, greater rssi than a 561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 562f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 563453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations linked and prefers " + b.configKey() 564453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + a.configKey() + " due to 5GHz RSSI " 565f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(astatus.rssi5) + " over: 5=" 566f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5) + ", 2.4=" 567f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5)); 568f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 569f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 10; 570453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else { 571453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //TODO: handle cases where configurations are dual band 572f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 573f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 574ede1310be531a84faa08f02c3fd243448dd936ddvandwalle 575ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //compare by user's choice. 576f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (hasConnectChoice(a, b)) { 577f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 578f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = order -2; 579f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 580453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers -2 " + a.configKey() 581453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() 582ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + " due to user choice order -> " + Integer.toString(order)); 583f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 584f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 585f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 586f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (hasConnectChoice(b, a)) { 587f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 588f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = order + 2; 589f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 590453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +2 " + b.configKey() + " over " 591453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + a.configKey() + " due to user choice order ->" + Integer.toString(order)); 592f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 593f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 594f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 595ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //TODO count the number of association rejection 596ede1310be531a84faa08f02c3fd243448dd936ddvandwalle // and use this to adjust the order by more than +/- 3 597ede1310be531a84faa08f02c3fd243448dd936ddvandwalle if ((a.status == WifiConfiguration.Status.DISABLED) 598ede1310be531a84faa08f02c3fd243448dd936ddvandwalle && (a.disableReason == WifiConfiguration.DISABLED_ASSOCIATION_REJECT)) { 599ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //a is of lower priority - ascending 600ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //lower the comparison score a bit 601ede1310be531a84faa08f02c3fd243448dd936ddvandwalle order = order +3; 602ede1310be531a84faa08f02c3fd243448dd936ddvandwalle } 603ede1310be531a84faa08f02c3fd243448dd936ddvandwalle if ((b.status == WifiConfiguration.Status.DISABLED) 604ede1310be531a84faa08f02c3fd243448dd936ddvandwalle && (b.disableReason == WifiConfiguration.DISABLED_ASSOCIATION_REJECT)) { 605ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //a is of higher priority - descending 606ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //lower the comparison score a bit 607ede1310be531a84faa08f02c3fd243448dd936ddvandwalle order = order -3; 608ede1310be531a84faa08f02c3fd243448dd936ddvandwalle } 609ede1310be531a84faa08f02c3fd243448dd936ddvandwalle 610ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if ((lastSelectedConfiguration != null) 611ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle && a.configKey().equals(lastSelectedConfiguration)) { 612ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // a is the last selected configuration, so keep it above connect choices 613ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //by giving a -4 (whereas connect choice preference gives +2) 614ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle order = order - 4; 615ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (VDBG) { 616453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers -4 " + a.configKey() 617453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() + " because a is the last selected -> " 618ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Integer.toString(order)); 619ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 620ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } else if ((lastSelectedConfiguration != null) 621ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle && b.configKey().equals(lastSelectedConfiguration)) { 622ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // b is the last selected configuration, so keep it above connect choices 623ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //by giving a +4 (whereas connect choice preference gives -2) 624ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle order = order + 4; 625ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (VDBG) { 626453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +4 " + a.configKey() 627453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() + " because b is the last selected -> " 628ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Integer.toString(order)); 629ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 630ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 631ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 632f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order == 0) { 633f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we don't know anything - pick the last seen i.e. K behavior 634f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we should do this only for recently picked configurations 635f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.priority > b.priority) { 636f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 637f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 638453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers -1 " + a.configKey() + " over " 639453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + b.configKey() + " due to priority"); 640f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 641f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 642f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -1; 643f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else if (a.priority < b.priority) { 644f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 645f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 646453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +1 " + b.configKey() + " over " 647453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + a.configKey() + " due to priority"); 648f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 649f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 650f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 1; 651f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 652f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //maybe just look at RSSI or band 653f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 654453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +1 " + b.configKey() + " over " 655453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + a.configKey() + " due to nothing"); 656f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 657f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 658f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = compareWifiConfigurationsRSSI(a, b); //compare RSSI 659f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 660f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 661f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 662f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String sorder = " == "; 663f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) 664f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " < "; 665f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order < 0) 666f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " > "; 667f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 668f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 669453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations Done: " + a.configKey() + sorder 670453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + b.configKey() + " order " + Integer.toString(order)); 671f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 672f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 673f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 674f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 675f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 676f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* attemptAutoJoin function implement the core of the a network switching algorithm */ 677f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void attemptAutoJoin() { 6788c9088d11880553458f09377cc60d6eb7e66747bvandwalle String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration(); 6798c9088d11880553458f09377cc60d6eb7e66747bvandwalle 680453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //reset the currentConfiguration Key, and set it only if WifiStateMachine and 681453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle // supplicant agree 682453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle mCurrentConfigurationKey = null; 683453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle WifiConfiguration currentConfiguration = mWifiStateMachine.getCurrentWifiConfiguration(); 684453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 685f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration candidate = null; 686f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 687f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* obtain the subset of recently seen networks */ 688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<WifiConfiguration> list = mWifiConfigStore.getRecentConfiguredNetworks(3000, true); 689f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (list == null) { 690f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) logDbg("attemptAutoJoin nothing"); 691f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return; 692f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 693f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 694f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* find the currently connected network: ask the supplicant directly */ 695f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String val = mWifiNative.status(); 696f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String status[] = val.split("\\r?\\n"); 697f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 698f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin() status=" + val + " split=" 699f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(status.length)); 700f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 701f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 702f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int currentNetId = -1; 703f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : status) { 704f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (key.regionMatches(0, "id=", 0, 3)) { 705f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int idx = 3; 706f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId = 0; 707f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle while (idx < key.length()) { 708f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle char c = key.charAt(idx); 709f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 710f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((c >= 0x30) && (c <= 0x39)) { 711f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId *= 10; 712f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId += c - 0x30; 713f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle idx++; 714f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 715f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle break; 716f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 717f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 718f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 719f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 720ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 721ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size()) 722ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + " ---> currentId=" + Integer.toString(currentNetId)); 723ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 724f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 725453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (currentConfiguration != null) { 726453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (currentNetId != currentConfiguration.networkId) { 727453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("attemptAutoJoin() ERROR wpa_supplicant out of sync nid=" 728453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + Integer.toString(currentNetId) + " WifiStateMachine=" 729453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + Integer.toString(currentConfiguration.networkId)); 730453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //I think this can happen due do race conditions, now what to do?? 731453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle // -> throw an exception, or, 732453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle // -> dont use the current configuration at all for autojoin 733453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //and hope that autojoining will kick us out of this state. 734453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle currentConfiguration = null; 735453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else { 736453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle mCurrentConfigurationKey = currentConfiguration.configKey(); 737453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 738453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 739453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 740f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* select Best Network candidate from known WifiConfigurations */ 741f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (WifiConfiguration config : list) { 742f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((config.status == WifiConfiguration.Status.DISABLED) 743f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (config.disableReason == WifiConfiguration.DISABLED_AUTH_FAILURE)) { 744ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 745ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin skip candidate due to auth failure key " 746ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + config.configKey(true)); 747ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 748f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 749f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 750453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 751e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle if (config.SSID == null) { 752e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle return; 753e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle } 754e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle 755453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (config.autoJoinStatus >= WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED) { 756453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //avoid temporarily disabled networks altogether 757453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //TODO: implement a better logic which will reenable the network after some time 758ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 759ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin skip candidate due to auto join status " 760ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + Integer.toString(config.autoJoinStatus) + " key " 761ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + config.configKey(true)); 762ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 763f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 764f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 765f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 766f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.networkId == currentNetId) { 767ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 76821bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle logDbg("attemptAutoJoin skip current candidate " 76921bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle + Integer.toString(currentNetId) 770ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + " key " + config.configKey(true)); 771ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 772f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 773f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 774f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 77521bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle if (lastSelectedConfiguration == null || 77621bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle !config.configKey().equals(lastSelectedConfiguration)) { 7778c9088d11880553458f09377cc60d6eb7e66747bvandwalle //don't try to autojoin a network that is too far 7788c9088d11880553458f09377cc60d6eb7e66747bvandwalle if (config.visibility == null) { 7798c9088d11880553458f09377cc60d6eb7e66747bvandwalle continue; 7808c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 7818c9088d11880553458f09377cc60d6eb7e66747bvandwalle if (config.visibility.rssi5 < -70 && config.visibility.rssi24 < -80) { 7828c9088d11880553458f09377cc60d6eb7e66747bvandwalle continue; 7838c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 7848c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 7858c9088d11880553458f09377cc60d6eb7e66747bvandwalle 786ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 787ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin trying candidate id=" + config.networkId + " " 788ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + config.SSID + " key " + config.configKey(true)); 789ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 790f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 791f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate == null) { 792f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 793f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 794f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 795453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("attemptAutoJoin will compare candidate " + candidate.configKey() 796453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " with " + config.configKey() + " key " + config.configKey(true)); 797f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 798f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 799f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = compareWifiConfigurations(candidate, config); 800f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) { 801f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //ascending : candidate < config 802f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 803f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 804f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 805f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 806f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 807f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* now, go thru scan result to try finding a better Herrevad network */ 808f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mNetworkScoreCache != null) { 809f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi5 = WifiConfiguration.INVALID_RSSI; 810f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi24 = WifiConfiguration.INVALID_RSSI; 811f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility visibility; 812f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate != null) { 813f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi5 = candidate.visibility.rssi5; 814f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi24 = candidate.visibility.rssi24; 815f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 816f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 817f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //get current date 818f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Date now = new Date(); 819f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long now_ms = now.getTime(); 820f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 821f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (rssi5 < -60 && rssi24 < -70) { 822f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (ScanResult result : scanResultCache.values()) { 823f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((now_ms - result.seen) < 3000) { 824f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int score = mNetworkScoreCache.getNetworkScore(result); 825f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (score > 0) { 826f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // try any arbitrary formula for now, adding apple and oranges, 827f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // i.e. adding network score and "dBm over noise" 828f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (result.frequency < 4000) { 829f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.level + score) > (rssi24 -40)) { 830f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // force it as open, TBD should we otherwise verify that this 831f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // BSSID only supports open?? 832f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.capabilities = ""; 833f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 834f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //switch to this scan result 835f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = 83621bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle mWifiConfigStore.wifiConfigurationFromScanResult(result); 837f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.ephemeral = true; 838f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 839f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 840f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.level + score) > (rssi5 -30)) { 841f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // force it as open, TBD should we otherwise verify that this 842f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // BSSID only supports open?? 843f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.capabilities = ""; 844f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 845f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //switch to this scan result 846f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = 84721bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle mWifiConfigStore.wifiConfigurationFromScanResult(result); 848f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.ephemeral = true; 849f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 850f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 851f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 852f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 853f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 854f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 855f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 856f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate != null) { 857453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle /* if candidate is found, check the state of the connection so as 858453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle to decide if we should be acting on this candidate and switching over */ 859f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int networkDelta = compareNetwork(candidate); 860ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG && (networkDelta > 0)) { 861453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("attemptAutoJoin did find candidate " + candidate.configKey() 862f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " for delta " + Integer.toString(networkDelta)); 863ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 864f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* ASK traffic poller permission to switch: 865f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for instance, 866f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if user is currently streaming voice traffic, 867f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle then don’t switch regardless of the delta */ 868f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 869f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mWifiTrafficPoller.shouldSwitchNetwork(networkDelta)) { 870f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mStaStaSupported) { 871453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("mStaStaSupported --> error do nothing now "); 872f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 873f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) { 874453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("AutoJoin auto connect with netId " 875f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(candidate.networkId) 876453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " to " + candidate.configKey()); 877f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 878f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 879f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT, 880f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.networkId); 881f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //mWifiConfigStore.enableNetworkWithoutBroadcast(candidate.networkId, true); 882f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 883f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we would do the below only if we want to persist the new choice 884f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //mWifiConfigStore.selectNetwork(candidate.networkId); 885f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 886f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 887f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 888f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 889f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) logDbg("Done attemptAutoJoin"); 890f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 891f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle} 892f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 893