WifiAutoJoinController.java revision 1fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4f
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; 74f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scoreManager = (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE); 75f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager == null) 76f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("Registered scoreManager NULL " + " service " + Context.NETWORK_SCORE_SERVICE); 77f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle else 78f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("Registered scoreManager NOT NULL" + " service " + Context.NETWORK_SCORE_SERVICE); 79f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 80f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager != null) { 81f13817203179f41620514718c8668ae7e418f8afJeff Davidson mNetworkScoreCache = new WifiNetworkScoreCache(mContext); 82f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache); 83f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 84f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("No network score service: Couldnt register as a WiFi score Manager, type=" 85f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(NetworkKey.TYPE_WIFI) 86f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " service " + Context.NETWORK_SCORE_SERVICE); 87f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mNetworkScoreCache = null; 88f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 89f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 90f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 91ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle void enableVerboseLogging(int verbose) { 92ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (verbose > 0 ) { 93ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle VDBG = true; 94ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } else { 95ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle VDBG = false; 96ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 97ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 98ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle 99ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle 100ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle 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)) { 1690c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle //TODO : properly handle SSID formatting, i.e. among others check for string 1700c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle //TODO : representing hexadecimal SSIDs 1710c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle WifiKey wkey = new WifiKey("\"" + result.SSID + "\"", result.BSSID); 1720c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle NetworkKey nkey = new NetworkKey(wkey); 1730c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle //if we don't know this scan result then request a score to Herrevad 1740c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle unknownScanResults.add(nkey); 1750c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } 176f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 177f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 178f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scanResultCache.put(result.BSSID, new ScanResult(result)); 179f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 180f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ScanResult srn = scanResultCache.get(result.BSSID); 181f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 182f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //add this BSSID to the scanResultCache of the relevant WifiConfiguration 183f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle associatedConfig = mWifiConfigStore.updateSavedNetworkHistory(result); 184f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 1850c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle //try to associate this BSSID to an existing Saved WifiConfiguration 186f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (associatedConfig == null) { 187f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle associatedConfig = mWifiConfigStore.associateWithConfiguration(result); 1881fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng if (associatedConfig != null && associatedConfig.SSID != null) { 189f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 190f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("addToScanCache save associated config " 191f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + associatedConfig.SSID + " with " + associatedConfig.SSID); 192f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 193f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine.sendMessage(WifiManager.SAVE_NETWORK, associatedConfig); 194f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 195f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 196f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 1970c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle 1980c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle if (unknownScanResults.size() != 0) { 1990c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle NetworkKey[] newKeys = 2000c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle unknownScanResults.toArray(new NetworkKey[unknownScanResults.size()]); 2010c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle //kick the score manager, we will get updated scores asynchronously 2020c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle scoreManager.requestScores(newKeys); 2030c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle } 204f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 205f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 206f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void logDbg(String message) { 2070888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle logDbg(message, false); 208ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 209ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 210ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle void logDbg(String message, boolean stackTrace) { 211f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long now = SystemClock.elapsedRealtimeNanos(); 2120888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle String ts = String.format("[%,d us] ", now / 1000); 213ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (stackTrace) { 214ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle Log.e(TAG, ts + message + " stack:" 215ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[2].getMethodName() + " - " 216ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[3].getMethodName() + " - " 217ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[4].getMethodName() + " - " 218ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Thread.currentThread().getStackTrace()[5].getMethodName()); 219ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } else { 220ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle Log.e(TAG, ts + message); 221ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 222f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 223f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 224f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* called directly from WifiStateMachine */ 225f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void newSupplicantResults() { 226f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<ScanResult> scanList = mWifiStateMachine.syncGetScanResultsList(); 227f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle addToScanCache(scanList); 228f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(mScanResultMaximumAge); 229f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) 230f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size()) ); 231f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 232f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 233f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 234f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 235f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 236f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 237f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 238f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* not used at the moment 239f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * should be a call back from WifiScanner HAL ?? 240f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * this function is not hooked and working yet, it will receive scan results from WifiScanners 241f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan 242f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * results as normal. 243f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 244f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void newHalScanResults() { 245f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<ScanResult> scanList = null;//mWifiScanner.syncGetScanResultsList(); 246f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String akm = WifiParser.parse_akm(null, null); 247f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg(akm); 248f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle addToScanCache(scanList); 249f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(0); 250f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 251f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 252f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 253f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 254f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* network link quality changed, called directly from WifiTrafficPoller, 255f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle or by listening to Link Quality intent */ 256f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void linkQualitySignificantChange() { 257f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 258f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 259f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 260f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* 261f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * compare a WifiConfiguration against the current network, return a delta score 262f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * If not associated, and the candidate will always be better 263f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * For instance if the candidate is a home network versus an unknown public wifi, 264f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * the delta will be infinite, else compare Kepler scores etc… 265f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ***/ 266f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private int compareNetwork(WifiConfiguration candidate) { 267f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration(); 268f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (currentNetwork == null) 269f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 1000; 270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 271f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate.configKey(true).equals(currentNetwork.configKey(true))) { 272f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return -1; 273f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 274f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = compareWifiConfigurations(currentNetwork, candidate); 276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) { 278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //ascending: currentNetwork < candidate 279f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 10; //will try switch over to the candidate 280f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 281f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 282f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 283f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 284f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 285ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle /** 286ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * update the network history fields fo that configuration 287ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * - if userTriggered, we mark the configuration as "non selfAdded" since the user has seen it 288ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * and took over management 289ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * - if it is a "connect", remember which network were there at the point of the connect, so 290ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * as those networks get a relative lower score than the selected configuration 29162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * 292ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param netId 293ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param userTriggered : if the update come from WiFiManager 294ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle * @param connect : if the update includes a connect 295117be607246604e875de62aa8cdd99700b77a2b4vandwalle **/ 29662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) { 297f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId); 298f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected == null) { 299f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return; 300f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 301f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 302e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle if (selected.SSID == null) { 303e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle return; 304e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle } 305e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle 30662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (userTriggered) { 307ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // reenable autojoin for this network, 30862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle // since the user want to connect to this configuration 30962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.autoJoinStatus = WifiConfiguration.AUTO_JOIN_ENABLED; 31062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.selfAdded = false; 31162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 312f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 313f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) { 314f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected.connectChoices != null) { 315ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will update " 316f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(netId) + " now: " 3170888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle + Integer.toString(selected.connectChoices.size()), true); 318f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 319ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will update " 3200888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle + Integer.toString(netId), true); 321f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 322f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 323f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 324ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (connect && userTriggered) { 325ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle boolean found = false; 32662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle List<WifiConfiguration> networks = 32762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle mWifiConfigStore.getRecentConfiguredNetworks(12000, false); 32862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (networks != null) { 32962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle for (WifiConfiguration config : networks) { 33062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (DBG) 331ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory got " + config.SSID); 332f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 33362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.configKey(true).equals(config.configKey(true))) { 334ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle found = true; 33562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle continue; 33662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 337f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 33862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle int rssi = WifiConfiguration.INVALID_RSSI; 33962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.visibility != null) { 34062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle rssi = config.visibility.rssi5; 34162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.visibility.rssi24 > rssi) 34262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle rssi = config.visibility.rssi24; 34362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 34462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (rssi < -80) { 34562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle continue; 34662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 347f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 348ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //the selected configuration was preferred over a recently seen config 349ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //hence remember the user's choice: 350ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //add the recently seen config to the selected's connectChoices array 351ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 352ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (selected.connectChoices == null) { 353ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle selected.connectChoices = new HashMap<String, Integer>(); 354ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 355ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 356ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory add a choice " + selected.configKey(true) 3570888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle + " over " + config.configKey(true) 3580888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle + " RSSI " + Integer.toString(rssi), true); 3590888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //add the visible config to the selected's connect choice list 36062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.connectChoices.put(config.configKey(true), rssi); 361f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 36262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.connectChoices != null) { 363ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (VDBG) { 364ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory will remove " 36562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + selected.configKey(true) + " from " + config.configKey(true)); 366ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 3670888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //remove the selected from the recently seen config's connectChoice list 36862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle config.connectChoices.remove(selected.configKey(true)); 3690888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle 3700888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle if (selected.linkedConfigurations != null) { 3710888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //remove the selected's linked configuration from the 3720888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle //recently seen config's connectChoice list 3730888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle for (String key : selected.linkedConfigurations.keySet()) { 3740888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle config.connectChoices.remove(key); 3750888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle } 3760888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle } 37762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 378ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 379ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (found == false) { 380ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // log an error for now but do something stringer later 381ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // we will need a new scan before attempting to connect to this 382ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // configuration anyhow and thus we can process the scan results then 383ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory try to connect to an old network!! : " 384ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + selected.configKey()); 38562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 386f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 38762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.connectChoices != null) { 38862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (VDBG) 389ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle logDbg("updateConfigurationHistory " + Integer.toString(netId) 39062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + " now: " + Integer.toString(selected.connectChoices.size())); 39162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 392ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 393ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle mWifiConfigStore.writeKnownNetworkHistory(); 394f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 395f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 396f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 397f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 398f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void printChoices(WifiConfiguration config) { 399f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int num = 0; 400f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices!= null) { 401f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle num = config.connectChoices.size(); 402f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 403f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 404f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("printChoices " + config.SSID + " num choices: " + Integer.toString(num)); 405f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices!= null) { 406f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : config.connectChoices.keySet()) { 407f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg(" " + key); 408f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 409f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 410f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 411f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 412f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean hasConnectChoice(WifiConfiguration source, WifiConfiguration target) { 413f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean found = false; 414f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source == null) 415f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return false; 416f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (target == null) 417f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return false; 418f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 419f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source.connectChoices != null) { 420f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ( source.connectChoices.get(target.configKey(true)) != null) { 421f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle found = true; 422f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 423f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 424f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 425f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source.linkedConfigurations != null) { 426f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : source.linkedConfigurations.keySet()) { 427f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key); 428f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config != null) { 429f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices != null) { 430f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices.get(target.configKey(true)) != null) { 431f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle found = true; 432f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 433f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 434f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 435f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 436f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 437f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return found; 438f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 439f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 440f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b) { 441f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 442f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int boost5 = 25; 443f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 444f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility astatus = a.visibility; 445f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility bstatus = b.visibility; 446f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (astatus == null || bstatus == null) { 447453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //error -> cant happen, need to throw en exception 448f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations NULL band status!"); 449f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 450f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 451453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if ((astatus.rssi5 > -70) && (bstatus.rssi5 == WifiConfiguration.INVALID_RSSI) 452f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((astatus.rssi5+boost5) > (bstatus.rssi24))) { 453f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is seen on 5GHz with good RSSI, greater rssi than b 454f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 455f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -1; 456453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 == WifiConfiguration.INVALID_RSSI) 457f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((bstatus.rssi5+boost5) > (bstatus.rssi24))) { 458f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //b is seen on 5GHz with good RSSI, greater rssi than a 459f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 460f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 1; 461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 463f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 464f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 465f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) { 466f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 467117be607246604e875de62aa8cdd99700b77a2b4vandwalle String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration(); 468f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean linked = false; 469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 470453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null) 471453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (a.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED) 472453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (b.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)) { 473f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((a.linkedConfigurations.get(b.configKey(true))!= null) 474f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (b.linkedConfigurations.get(a.configKey(true))!= null)) { 475f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle linked = true; 476f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.ephemeral && b.ephemeral == false) { 480f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 481453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations ephemeral and prefers " + b.configKey() 482453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + a.configKey()); 483f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 484f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 1; //b is of higher priority - ascending 485f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 486f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (b.ephemeral && a.ephemeral == false) { 487f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 488453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations ephemeral and prefers " +a.configKey() 489453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey()); 490f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 491f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return -1; //a is of higher priority - descending 492f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 493f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 494f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int boost5 = 25; 495453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //apply Hysteresis: boost the RSSI value of the currently connected configuration 496453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle int aRssiBoost = 0; 497453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle int bRssiBoost = 0; 498453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (null != mCurrentConfigurationKey) { 499453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (a.configKey().equals(mCurrentConfigurationKey)) { 500453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle aRssiBoost += 10; 501453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else if (b.configKey().equals(mCurrentConfigurationKey)) { 502453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle bRssiBoost += 10; 503453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 504453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 505f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (linked) { 506f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // then we try prefer 5GHz, and try to ignore user's choice 507f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility astatus = a.visibility; 508f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility bstatus = b.visibility; 509f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (astatus == null || bstatus == null) { 510f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //error 511f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations NULL band status!"); 512f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 513f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 514f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 515f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 516f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations linked: " + Integer.toString(astatus.rssi5) 517f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + "," + Integer.toString(astatus.rssi24) + " " 518f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5) + "," 519f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi24)); 520f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 521f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 522453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if ((astatus.rssi5 > -70) && (bstatus.rssi5 <= WifiConfiguration.INVALID_RSSI) 523453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && (astatus.rssi5+boost5+aRssiBoost) > (bstatus.rssi24+bRssiBoost)) { 524453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //in this case: a has 5GHz and b doesn't have 5GHz 525453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //compare a's 5GHz RSSI to b's 5GHz RSSI 526453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 527f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is seen on 5GHz with good RSSI, greater rssi than b 528f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 529f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -10; 530f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 531f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 532453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations linked and prefers " + a.configKey() 533453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() 534f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " due to 5GHz RSSI " + Integer.toString(astatus.rssi5) 535f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over: 5=" + Integer.toString(bstatus.rssi5) 536f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + ", 2.4=" + Integer.toString(bstatus.rssi5)); 537f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 538453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 <= WifiConfiguration.INVALID_RSSI) 539453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle && ((bstatus.rssi5+boost5+bRssiBoost) > (astatus.rssi24+aRssiBoost))) { 540453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //in this case: b has 5GHz and a doesn't have 5GHz 541453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 542f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //b is seen on 5GHz with good RSSI, greater rssi than a 543f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 544f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 545453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations linked and prefers " + b.configKey() 546453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + a.configKey() + " due to 5GHz RSSI " 547f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(astatus.rssi5) + " over: 5=" 548f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5) + ", 2.4=" 549f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5)); 550f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 551f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 10; 552453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else { 553453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //TODO: handle cases where configurations are dual band 554f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 555f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 556ede1310be531a84faa08f02c3fd243448dd936ddvandwalle 557ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //compare by user's choice. 558f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (hasConnectChoice(a, b)) { 559f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = order -2; 561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 562453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers -2 " + a.configKey() 563453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() 564ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + " due to user choice order -> " + Integer.toString(order)); 565f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 566f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 567f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 568f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (hasConnectChoice(b, a)) { 569f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 570f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = order + 2; 571f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 572453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +2 " + b.configKey() + " over " 573453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + a.configKey() + " due to user choice order ->" + Integer.toString(order)); 574f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 575f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 576f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 577ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //TODO count the number of association rejection 578ede1310be531a84faa08f02c3fd243448dd936ddvandwalle // and use this to adjust the order by more than +/- 3 579ede1310be531a84faa08f02c3fd243448dd936ddvandwalle if ((a.status == WifiConfiguration.Status.DISABLED) 580ede1310be531a84faa08f02c3fd243448dd936ddvandwalle && (a.disableReason == WifiConfiguration.DISABLED_ASSOCIATION_REJECT)) { 581ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //a is of lower priority - ascending 582ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //lower the comparison score a bit 583ede1310be531a84faa08f02c3fd243448dd936ddvandwalle order = order +3; 584ede1310be531a84faa08f02c3fd243448dd936ddvandwalle } 585ede1310be531a84faa08f02c3fd243448dd936ddvandwalle if ((b.status == WifiConfiguration.Status.DISABLED) 586ede1310be531a84faa08f02c3fd243448dd936ddvandwalle && (b.disableReason == WifiConfiguration.DISABLED_ASSOCIATION_REJECT)) { 587ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //a is of higher priority - descending 588ede1310be531a84faa08f02c3fd243448dd936ddvandwalle //lower the comparison score a bit 589ede1310be531a84faa08f02c3fd243448dd936ddvandwalle order = order -3; 590ede1310be531a84faa08f02c3fd243448dd936ddvandwalle } 591ede1310be531a84faa08f02c3fd243448dd936ddvandwalle 592ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if ((lastSelectedConfiguration != null) 593ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle && a.configKey().equals(lastSelectedConfiguration)) { 594ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // a is the last selected configuration, so keep it above connect choices 595ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //by giving a -4 (whereas connect choice preference gives +2) 596ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle order = order - 4; 597ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (VDBG) { 598453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers -4 " + a.configKey() 599453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() + " because a is the last selected -> " 600ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Integer.toString(order)); 601ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 602ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } else if ((lastSelectedConfiguration != null) 603ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle && b.configKey().equals(lastSelectedConfiguration)) { 604ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle // b is the last selected configuration, so keep it above connect choices 605ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle //by giving a +4 (whereas connect choice preference gives -2) 606ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle order = order + 4; 607ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle if (VDBG) { 608453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +4 " + a.configKey() 609453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " over " + b.configKey() + " because b is the last selected -> " 610ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle + Integer.toString(order)); 611ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 612ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle } 613ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle 614f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order == 0) { 615f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we don't know anything - pick the last seen i.e. K behavior 616f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we should do this only for recently picked configurations 617f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.priority > b.priority) { 618f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 619f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 620453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers -1 " + a.configKey() + " over " 621453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + b.configKey() + " due to priority"); 622f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 623f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 624f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -1; 625f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else if (a.priority < b.priority) { 626f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 627f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 628453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +1 " + b.configKey() + " over " 629453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + a.configKey() + " due to priority"); 630f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 631f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 632f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 1; 633f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 634f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //maybe just look at RSSI or band 635f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 636453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations prefers +1 " + b.configKey() + " over " 637453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + a.configKey() + " due to nothing"); 638f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 639f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 640f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = compareWifiConfigurationsRSSI(a, b); //compare RSSI 641f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 642f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 643f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 644f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String sorder = " == "; 645f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) 646f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " < "; 647f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order < 0) 648f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " > "; 649f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 650f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 651453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("compareWifiConfigurations Done: " + a.configKey() + sorder 652453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + b.configKey() + " order " + Integer.toString(order)); 653f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 654f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 655f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 656f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 657f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 658f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* attemptAutoJoin function implement the core of the a network switching algorithm */ 659f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void attemptAutoJoin() { 6608c9088d11880553458f09377cc60d6eb7e66747bvandwalle String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration(); 6618c9088d11880553458f09377cc60d6eb7e66747bvandwalle 662453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //reset the currentConfiguration Key, and set it only if WifiStateMachine and 663453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle // supplicant agree 664453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle mCurrentConfigurationKey = null; 665453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle WifiConfiguration currentConfiguration = mWifiStateMachine.getCurrentWifiConfiguration(); 666453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 667f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration candidate = null; 668f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 669f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* obtain the subset of recently seen networks */ 670f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<WifiConfiguration> list = mWifiConfigStore.getRecentConfiguredNetworks(3000, true); 671f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (list == null) { 672f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) logDbg("attemptAutoJoin nothing"); 673f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return; 674f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 675f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 676f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* find the currently connected network: ask the supplicant directly */ 677f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String val = mWifiNative.status(); 678f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String status[] = val.split("\\r?\\n"); 679f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 680f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin() status=" + val + " split=" 681f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(status.length)); 682f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 683f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 684f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int currentNetId = -1; 685f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : status) { 686f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (key.regionMatches(0, "id=", 0, 3)) { 687f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int idx = 3; 688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId = 0; 689f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle while (idx < key.length()) { 690f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle char c = key.charAt(idx); 691f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 692f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((c >= 0x30) && (c <= 0x39)) { 693f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId *= 10; 694f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId += c - 0x30; 695f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle idx++; 696f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 697f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle break; 698f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 699f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 700f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 701f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 702ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 703ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size()) 704ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + " ---> currentId=" + Integer.toString(currentNetId)); 705ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 706f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 707453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (currentConfiguration != null) { 708453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (currentNetId != currentConfiguration.networkId) { 709453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("attemptAutoJoin() ERROR wpa_supplicant out of sync nid=" 710453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + Integer.toString(currentNetId) + " WifiStateMachine=" 711453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + Integer.toString(currentConfiguration.networkId)); 712453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //I think this can happen due do race conditions, now what to do?? 713453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle // -> throw an exception, or, 714453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle // -> dont use the current configuration at all for autojoin 715453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //and hope that autojoining will kick us out of this state. 716453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle currentConfiguration = null; 717453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } else { 718453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle mCurrentConfigurationKey = currentConfiguration.configKey(); 719453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 720453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle } 721453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 722f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* select Best Network candidate from known WifiConfigurations */ 723f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (WifiConfiguration config : list) { 724f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((config.status == WifiConfiguration.Status.DISABLED) 725f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (config.disableReason == WifiConfiguration.DISABLED_AUTH_FAILURE)) { 726ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 727ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin skip candidate due to auth failure key " 728ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + config.configKey(true)); 729ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 730f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 731f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 732453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle 733e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle if (config.SSID == null) { 734e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle return; 735e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle } 736e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle 737453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle if (config.autoJoinStatus >= WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED) { 738453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //avoid temporarily disabled networks altogether 739453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle //TODO: implement a better logic which will reenable the network after some time 740ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 741ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin skip candidate due to auto join status " 742ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + Integer.toString(config.autoJoinStatus) + " key " 743ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + config.configKey(true)); 744ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 745f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 746f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 747f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 748f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.networkId == currentNetId) { 749ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 750ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin skip current candidate " + Integer.toString(currentNetId) 751ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + " key " + config.configKey(true)); 752ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 753f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 754f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 755f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 7568c9088d11880553458f09377cc60d6eb7e66747bvandwalle if (lastSelectedConfiguration == null || !config.configKey().equals(lastSelectedConfiguration)) { 7578c9088d11880553458f09377cc60d6eb7e66747bvandwalle //don't try to autojoin a network that is too far 7588c9088d11880553458f09377cc60d6eb7e66747bvandwalle if (config.visibility == null) { 7598c9088d11880553458f09377cc60d6eb7e66747bvandwalle continue; 7608c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 7618c9088d11880553458f09377cc60d6eb7e66747bvandwalle if (config.visibility.rssi5 < -70 && config.visibility.rssi24 < -80) { 7628c9088d11880553458f09377cc60d6eb7e66747bvandwalle continue; 7638c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 7648c9088d11880553458f09377cc60d6eb7e66747bvandwalle } 7658c9088d11880553458f09377cc60d6eb7e66747bvandwalle 766ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG) { 767ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle logDbg("attemptAutoJoin trying candidate id=" + config.networkId + " " 768ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle + config.SSID + " key " + config.configKey(true)); 769ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 770f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 771f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate == null) { 772f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 773f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 774f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 775453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("attemptAutoJoin will compare candidate " + candidate.configKey() 776453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " with " + config.configKey() + " key " + config.configKey(true)); 777f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 778f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 779f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = compareWifiConfigurations(candidate, config); 780f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) { 781f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //ascending : candidate < config 782f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 783f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 784f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 785f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 786f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 787f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* now, go thru scan result to try finding a better Herrevad network */ 788f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mNetworkScoreCache != null) { 789f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi5 = WifiConfiguration.INVALID_RSSI; 790f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi24 = WifiConfiguration.INVALID_RSSI; 791f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility visibility; 792f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate != null) { 793f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi5 = candidate.visibility.rssi5; 794f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi24 = candidate.visibility.rssi24; 795f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 796f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 797f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //get current date 798f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Date now = new Date(); 799f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long now_ms = now.getTime(); 800f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 801f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (rssi5 < -60 && rssi24 < -70) { 802f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (ScanResult result : scanResultCache.values()) { 803f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((now_ms - result.seen) < 3000) { 804f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int score = mNetworkScoreCache.getNetworkScore(result); 805f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (score > 0) { 806f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // try any arbitrary formula for now, adding apple and oranges, 807f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // i.e. adding network score and "dBm over noise" 808f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (result.frequency < 4000) { 809f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.level + score) > (rssi24 -40)) { 810f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // force it as open, TBD should we otherwise verify that this 811f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // BSSID only supports open?? 812f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.capabilities = ""; 813f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 814f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //switch to this scan result 815f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = 816f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.wifiConfigurationFromScanResult(result); 817f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.ephemeral = true; 818f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 819f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 820f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.level + score) > (rssi5 -30)) { 821f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // force it as open, TBD should we otherwise verify that this 822f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // BSSID only supports open?? 823f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.capabilities = ""; 824f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 825f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //switch to this scan result 826f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = 827f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.wifiConfigurationFromScanResult(result); 828f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.ephemeral = true; 829f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 830f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 831f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 832f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 833f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 834f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 835f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 836f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate != null) { 837453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle /* if candidate is found, check the state of the connection so as 838453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle to decide if we should be acting on this candidate and switching over */ 839f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int networkDelta = compareNetwork(candidate); 840ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle if (DBG && (networkDelta > 0)) { 841453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("attemptAutoJoin did find candidate " + candidate.configKey() 842f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " for delta " + Integer.toString(networkDelta)); 843ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle } 844f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* ASK traffic poller permission to switch: 845f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for instance, 846f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if user is currently streaming voice traffic, 847f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle then don’t switch regardless of the delta */ 848f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 849f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mWifiTrafficPoller.shouldSwitchNetwork(networkDelta)) { 850f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mStaStaSupported) { 851453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("mStaStaSupported --> error do nothing now "); 852f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 853f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) { 854453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle logDbg("AutoJoin auto connect with netId " 855f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(candidate.networkId) 856453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle + " to " + candidate.configKey()); 857f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 858f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 859f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT, 860f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.networkId); 861f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //mWifiConfigStore.enableNetworkWithoutBroadcast(candidate.networkId, true); 862f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 863f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we would do the below only if we want to persist the new choice 864f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //mWifiConfigStore.selectNetwork(candidate.networkId); 865f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 866f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 867f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 868f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 869f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) logDbg("Done attemptAutoJoin"); 870f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 871f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle} 872f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 873