WifiAutoJoinController.java revision 62f1d0ca8ea4466628f6ff179b1f20e1279fa7e0
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; 23f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.WifiConfiguration; 24f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.ScanResult; 25f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.WifiManager; 26f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.WifiParser; 27f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 28f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock; 29f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.util.Log; 30f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 31f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.Iterator; 32f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.HashMap; 33f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.List; 34f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.Date; 35f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 36f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/** 37f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * AutoJoin controller is responsible for WiFi Connect decision 38f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 39f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * It runs in the thread context of WifiStateMachine 40f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 41f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 42f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepublic class WifiAutoJoinController { 43f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 44f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private Context mContext; 45f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiStateMachine mWifiStateMachine; 46f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiConfigStore mWifiConfigStore; 47f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiTrafficPoller mWifiTrafficPoller; 48f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiNative mWifiNative; 49f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 50f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private NetworkScoreManager scoreManager; 51f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private WifiNetworkScoreCache mNetworkScoreCache; 52f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 53f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 54f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final String TAG = "WifiAutoJoinController "; 55f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final boolean DBG = true; 56f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final boolean VDBG = false; 57f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final boolean mStaStaSupported = false; 58f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private static final int SCAN_RESULT_CACHE_SIZE = 80; 59f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 60f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 61f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private HashMap<String, ScanResult> scanResultCache = 62f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle new HashMap<String, ScanResult>(); 63f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 64f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiAutoJoinController(Context c, WifiStateMachine w, WifiConfigStore s, 65f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiTrafficPoller t, WifiNative n) { 66f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mContext = c; 67f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine = w; 68f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore = s; 69f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiTrafficPoller = t; 70f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiNative = n; 71f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mNetworkScoreCache = null; 72f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scoreManager = (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE); 73f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager == null) 74f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("Registered scoreManager NULL " + " service " + Context.NETWORK_SCORE_SERVICE); 75f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle else 76f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("Registered scoreManager NOT NULL" + " service " + Context.NETWORK_SCORE_SERVICE); 77f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 78f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (scoreManager != null) { 79f13817203179f41620514718c8668ae7e418f8afJeff Davidson mNetworkScoreCache = new WifiNetworkScoreCache(mContext); 80f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache); 81f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 82f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("No network score service: Couldnt register as a WiFi score Manager, type=" 83f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(NetworkKey.TYPE_WIFI) 84f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " service " + Context.NETWORK_SCORE_SERVICE); 85f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mNetworkScoreCache = null; 86f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 87f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 88f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 89f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int mScanResultMaximumAge = 30000; /* milliseconds unit */ 90f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 91f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* flush out scan results older than mScanResultMaximumAge */ 92f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private void ageScanResultsOut(int delay) { 93f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (delay <= 0) { 94f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle delay = mScanResultMaximumAge; //something sane 95f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 96f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Date now = new Date(); 97f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long milli = now.getTime(); 98f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 99f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("ageScanResultsOut delay " + Integer.valueOf(delay) + " size " 100f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.valueOf(scanResultCache.size()) + " now " + Long.valueOf(milli)); 101f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 102f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 103f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Iterator<HashMap.Entry<String,ScanResult>> iter = scanResultCache.entrySet().iterator(); 104f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle while (iter.hasNext()) { 105f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle HashMap.Entry<String,ScanResult> entry = iter.next(); 106f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ScanResult result = entry.getValue(); 107f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 108f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.seen + delay) < milli) { 109f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle iter.remove(); 110f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 111f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 112f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 113f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 114f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* Check if this network is known to kepler and return its score */ 115f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private int isScoredNetwork(ScanResult result) { 116f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mNetworkScoreCache == null) 117f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 118f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return mNetworkScoreCache.getNetworkScore(result); 119f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 120f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 121f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 122f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void addToScanCache(List<ScanResult> scanList) { 123f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration associatedConfig; 124f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 125f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for(ScanResult result: scanList) { 126f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.seen = System.currentTimeMillis(); 127f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 128f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ScanResult sr = scanResultCache.get(result.BSSID); 129f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (sr != null) { 130f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // if there was a previous cache result for this BSSID, average the RSSI values 131f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 132f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int previous_rssi = sr.level; 133f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long previously_seen_milli = sr.seen; 134f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 135f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* average RSSI with previously seen instances of this scan result */ 136f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int avg_rssi = result.level; 137f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 138f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((previously_seen_milli > 0) 139f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (previously_seen_milli < mScanResultMaximumAge/2)) { 140f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 141f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* 142f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 143f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * previously_seen_milli = 0 => RSSI = 0.5 * previous_seen_rssi + 0.5 * new_rssi 144f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 145f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * If previously_seen_milli is 15+ seconds old: 146f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * previously_seen_milli = 15000 => RSSI = new_rssi 147f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * 148f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 149f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 150f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle double alpha = 0.5 - (double)previously_seen_milli 151f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle / (double)mScanResultMaximumAge; 152f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 153f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle avg_rssi = (int)((double)avg_rssi * (1-alpha) + (double)previous_rssi * alpha); 154f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 155f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 156f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.level = avg_rssi; 157f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 158f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //remove the previous Scan Result 159f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scanResultCache.remove(result.BSSID); 160f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 161f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 162f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle scanResultCache.put(result.BSSID, new ScanResult(result)); 163f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 164f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ScanResult srn = scanResultCache.get(result.BSSID); 165f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 166f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //add this BSSID to the scanResultCache of the relevant WifiConfiguration 167f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle associatedConfig = mWifiConfigStore.updateSavedNetworkHistory(result); 168f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 169f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //try to associate this BSSID to an existing Saved Wificonfiguration 170f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (associatedConfig == null) { 171f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle associatedConfig = mWifiConfigStore.associateWithConfiguration(result); 172f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (associatedConfig != null) { 173f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 174f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("addToScanCache save associated config " 175f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + associatedConfig.SSID + " with " + associatedConfig.SSID); 176f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 177f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine.sendMessage(WifiManager.SAVE_NETWORK, associatedConfig); 178f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 179f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 180f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 181f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 182f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 183f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void logDbg(String message) { 184f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long now = SystemClock.elapsedRealtimeNanos(); 185f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String ts = String.format("[%,d us] ", now/1000); 186f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Log.e(TAG, ts + message + " stack:" 187f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Thread.currentThread().getStackTrace()[2].getMethodName() +" - " 188f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Thread.currentThread().getStackTrace()[3].getMethodName() +" - " 189f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Thread.currentThread().getStackTrace()[4].getMethodName() +" - " 190f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Thread.currentThread().getStackTrace()[5].getMethodName()); 191f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 192f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 193f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 194f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* called directly from WifiStateMachine */ 195f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void newSupplicantResults() { 196f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<ScanResult> scanList = mWifiStateMachine.syncGetScanResultsList(); 197f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle addToScanCache(scanList); 198f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(mScanResultMaximumAge); 199f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) 200f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size()) ); 201f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 202f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 203f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 204f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 205f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 206f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 207f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 208f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* not used at the moment 209f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * should be a call back from WifiScanner HAL ?? 210f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * this function is not hooked and working yet, it will receive scan results from WifiScanners 211f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan 212f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * results as normal. 213f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */ 214f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void newHalScanResults() { 215f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<ScanResult> scanList = null;//mWifiScanner.syncGetScanResultsList(); 216f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String akm = WifiParser.parse_akm(null, null); 217f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg(akm); 218f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle addToScanCache(scanList); 219f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ageScanResultsOut(0); 220f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 221f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 222f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 223f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 224f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* network link quality changed, called directly from WifiTrafficPoller, 225f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle or by listening to Link Quality intent */ 226f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void linkQualitySignificantChange() { 227f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle attemptAutoJoin(); 228f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 229f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 230f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* 231f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * compare a WifiConfiguration against the current network, return a delta score 232f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * If not associated, and the candidate will always be better 233f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * For instance if the candidate is a home network versus an unknown public wifi, 234f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * the delta will be infinite, else compare Kepler scores etc… 235f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle ***/ 236f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle private int compareNetwork(WifiConfiguration candidate) { 237f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration(); 238f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (currentNetwork == null) 239f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 1000; 240f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 241f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate.configKey(true).equals(currentNetwork.configKey(true))) { 242f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return -1; 243f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 244f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 245f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = compareWifiConfigurations(currentNetwork, candidate); 246f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 247f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) { 248f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //ascending: currentNetwork < candidate 249f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 10; //will try switch over to the candidate 250f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 251f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 252f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 253f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 254f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 255f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 25662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle /* 25762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * user made a netork selection, hence remember that selection so as to use the 25862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * infomration as part of our network selection logic. 25962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * 26062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle */ 26162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) { 262f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 263f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId); 264f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected == null) { 265f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return; 266f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 267f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 26862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (userTriggered) { 26962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle // re-enable autojoin for this network, 27062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle // since the user want to connect to this configuration 27162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.autoJoinStatus = WifiConfiguration.AUTO_JOIN_ENABLED; 27262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle // teh configuration doesn't belong to autojoin anymore if the user modified it 27362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.selfAdded = false; 27462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) { 277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (selected.connectChoices != null) { 278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("updateSavedConfigurationsPriorities will update " 279f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(netId) + " now: " 280f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(selected.connectChoices.size())); 281f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 282f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("updateSavedConfigurationsPriorities will update " 283f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(netId)); 284f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 285f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 286f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 28762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (connect) { 28862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle List<WifiConfiguration> networks = 28962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle mWifiConfigStore.getRecentConfiguredNetworks(12000, false); 29062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (networks != null) { 29162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle for (WifiConfiguration config : networks) { 29262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (DBG) 29362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle logDbg("updateSavedConfigurationsPriorities got " + config.SSID); 294f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 29562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.configKey(true).equals(config.configKey(true))) { 29662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle continue; 29762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 298f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 29962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle //we were preferred over a recently seen config 30062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.connectChoices == null) { 30162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.connectChoices = new HashMap<String, Integer>(); 30262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 303f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 30462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle int rssi = WifiConfiguration.INVALID_RSSI; 30562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.visibility != null) { 30662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle rssi = config.visibility.rssi5; 30762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.visibility.rssi24 > rssi) 30862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle rssi = config.visibility.rssi24; 30962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 31062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (rssi < -80) { 31162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle continue; 31262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 313f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 31462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle //remember the user's choice: 31562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle //add the recently seen config to the selected's choice 31662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle logDbg("updateSavedConfigurationsPriorities add a choice " 31762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + selected.configKey(true) 31862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + " over " + config.configKey(true) + " RSSI " + Integer.toString(rssi)); 31962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle selected.connectChoices.put(config.configKey(true), rssi); 320f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 32162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (config.connectChoices != null) { 32262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (VDBG) 32362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle logDbg("updateSavedConfigurationsPriorities try to remove " 32462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + selected.configKey(true) + " from " + config.configKey(true)); 325f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 32662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle //remove the selected from the recently seen config's array 32762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle config.connectChoices.remove(selected.configKey(true)); 32862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 32962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle printChoices(config); 33062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 331f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 33262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (selected.connectChoices != null) { 33362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle if (VDBG) 33462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle logDbg("updateSavedConfigurationsPriorities " + Integer.toString(netId) 33562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle + " now: " + Integer.toString(selected.connectChoices.size())); 33662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle } 337f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 338f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 339f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.writeKnownNetworkHistory(); 340f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 341f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 342f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void printChoices(WifiConfiguration config) { 343f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int num = 0; 344f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices!= null) { 345f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle num = config.connectChoices.size(); 346f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 347f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 348f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("printChoices " + config.SSID + " num choices: " + Integer.toString(num)); 349f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices!= null) { 350f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : config.connectChoices.keySet()) { 351f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg(" " + key); 352f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 353f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 354f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 355f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 356f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 357f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean hasConnectChoice(WifiConfiguration source, WifiConfiguration target) { 358f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean found = false; 359f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source == null) 360f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return false; 361f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (target == null) 362f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return false; 363f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 364f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source.connectChoices != null) { 365f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ( source.connectChoices.get(target.configKey(true)) != null) { 366f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle found = true; 367f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 368f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 369f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 370f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (source.linkedConfigurations != null) { 371f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : source.linkedConfigurations.keySet()) { 372f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key); 373f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config != null) { 374f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices != null) { 375f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.connectChoices.get(target.configKey(true)) != null) { 376f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle found = true; 377f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 378f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 379f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 380f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 381f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 382f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return found; 383f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 384f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 385f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b) { 386f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 387f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int boost5 = 25; 388f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 389f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility astatus = a.visibility; 390f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility bstatus = b.visibility; 391f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (astatus == null || bstatus == null) { 392f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //error 393f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations NULL band status!"); 394f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 395f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 396f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((astatus.rssi5 > -70) && (bstatus.rssi5 == -127) 397f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((astatus.rssi5+boost5) > (bstatus.rssi24))) { 398f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is seen on 5GHz with good RSSI, greater rssi than b 399f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 400f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -1; 401f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 == -127) 402f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((bstatus.rssi5+boost5) > (bstatus.rssi24))) { 403f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //b is seen on 5GHz with good RSSI, greater rssi than a 404f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 405f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 1; 406f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 407f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 408f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 409f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 410f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) { 411f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = 0; 412f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 413f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle boolean linked = false; 414f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 415f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null)) { 416f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((a.linkedConfigurations.get(b.configKey(true))!= null) 417f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (b.linkedConfigurations.get(a.configKey(true))!= null)) { 418f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle linked = true; 419f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 420f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 421f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 422f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.ephemeral && b.ephemeral == false) { 423f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 424f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations ephemeral and prefers " + b.SSID 425f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over " + a.SSID); 426f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 427f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 1; //b is of higher priority - ascending 428f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 429f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (b.ephemeral && a.ephemeral == false) { 430f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 431f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations ephemeral and prefers " +a.SSID 432f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over " + b.SSID); 433f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 434f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return -1; //a is of higher priority - descending 435f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 436f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 437f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int boost5 = 25; 438f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (linked) { 439f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // then we try prefer 5GHz, and try to ignore user's choice 440f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility astatus = a.visibility; 441f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility bstatus = b.visibility; 442f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (astatus == null || bstatus == null) { 443f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //error 444f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations NULL band status!"); 445f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return 0; 446f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 447f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 448f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 449f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations linked: " + Integer.toString(astatus.rssi5) 450f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + "," + Integer.toString(astatus.rssi24) + " " 451f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5) + "," 452f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi24)); 453f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 454f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 455f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((astatus.rssi5 > -70) && (bstatus.rssi5 == -127) 456f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((astatus.rssi5+boost5) > (bstatus.rssi24))) { 457f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is seen on 5GHz with good RSSI, greater rssi than b 458f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 459f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -10; 460f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations linked and prefers " + a.SSID 463f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over " + b.SSID 464f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " due to 5GHz RSSI " + Integer.toString(astatus.rssi5) 465f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over: 5=" + Integer.toString(bstatus.rssi5) 466f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + ", 2.4=" + Integer.toString(bstatus.rssi5)); 467f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 468f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 == -127) 469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && ((bstatus.rssi5+boost5) > (bstatus.rssi24))) { 470f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //b is seen on 5GHz with good RSSI, greater rssi than a 471f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 472f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 473f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations linked and prefers " + b.SSID 474f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over " + a.SSID + " due to 5GHz RSSI " 475f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(astatus.rssi5) + " over: 5=" 476f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5) + ", 2.4=" 477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(bstatus.rssi5)); 478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 10; 480f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 481f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 482f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //assuming that the WifiConfiguration aren't part of the same "extended roam domain", 483f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //then compare by user's choice. 484f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (hasConnectChoice(a, b)) { 485f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 486f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = order -2; 487f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 488f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations prefers -2 " + a.SSID 489f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " over " + b.SSID + " due to user choice order -> " + Integer.toString(order)); 490f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 491f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 492f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 493f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (hasConnectChoice(b, a)) { 494f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = order + 2; 496f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 497f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations prefers +2 " + b.SSID + " over " 498f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + a.SSID + " due to user choice order ->" + Integer.toString(order)); 499f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 500f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 501f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 502f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order == 0) { 503f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we don't know anything - pick the last seen i.e. K behavior 504f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we should do this only for recently picked configurations 505f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (a.priority > b.priority) { 506f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of higher priority - descending 507f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 508f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations prefers -1 " + a.SSID + " over " 509f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + b.SSID + " due to priority"); 510f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 511f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 512f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = -1; 513f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else if (a.priority < b.priority) { 514f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //a is of lower priority - ascending 515f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 516f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations prefers +1 " + b.SSID + " over " 517f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + a.SSID + " due to priority"); 518f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 519f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 520f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = 1; 521f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 522f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //maybe just look at RSSI or band 523f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 524f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations prefers +1 " + b.SSID + " over " 525f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + a.SSID + " due to nothing"); 526f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 527f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 528f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle order = compareWifiConfigurationsRSSI(a, b); //compare RSSI 529f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 530f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 531f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 532f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String sorder = " == "; 533f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) 534f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " < "; 535f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order < 0) 536f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle sorder = " > "; 537f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 538f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 539f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("compareWifiConfigurations Done: " + a.SSID + sorder 540f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + b.SSID + " order " + Integer.toString(order)); 541f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 542f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 543f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return order; 544f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 545f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 546f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* attemptAutoJoin function implement the core of the a network switching algorithm */ 547f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle void attemptAutoJoin() { 548f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration candidate = null; 549f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 550f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* obtain the subset of recently seen networks */ 551f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle List<WifiConfiguration> list = mWifiConfigStore.getRecentConfiguredNetworks(3000, true); 552f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (list == null) { 553f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) logDbg("attemptAutoJoin nothing"); 554f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle return; 555f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 556f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 557f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* find the currently connected network: ask the supplicant directly */ 558f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String val = mWifiNative.status(); 559f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle String status[] = val.split("\\r?\\n"); 560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin() status=" + val + " split=" 562f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(status.length)); 563f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 564f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 565f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int currentNetId = -1; 566f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (String key : status) { 567f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (key.regionMatches(0, "id=", 0, 3)) { 568f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int idx = 3; 569f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId = 0; 570f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle while (idx < key.length()) { 571f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle char c = key.charAt(idx); 572f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 573f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((c >= 0x30) && (c <= 0x39)) { 574f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId *= 10; 575f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle currentNetId += c - 0x30; 576f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle idx++; 577f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 578f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle break; 579f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 580f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 581f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 582f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 583f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size()) 584f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " ---> currentId=" + Integer.toString(currentNetId)); 585f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 586f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* select Best Network candidate from known WifiConfigurations */ 587f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (WifiConfiguration config : list) { 588f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((config.status == WifiConfiguration.Status.DISABLED) 589f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle && (config.disableReason == WifiConfiguration.DISABLED_AUTH_FAILURE)) { 590f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin skip candidate due to auth failure " 591f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + config.SSID + " key " + config.configKey(true)); 592f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 593f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 594f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.autoJoinStatus != WifiConfiguration.AUTO_JOIN_ENABLED) { 595f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin skip candidate due to auto join status " 596f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(config.autoJoinStatus) + " " + config.SSID + " key " 597f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + config.configKey(true)); 598f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 599f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 600f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 601f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (config.networkId == currentNetId) { 602f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin skip current candidate " + Integer.toString(currentNetId) 603f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " key " + config.configKey(true)); 604f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle continue; 605f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 606f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 607f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) logDbg("attemptAutoJoin trying candidate id=" + config.networkId + " " 608f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + config.SSID + " key " + config.configKey(true)); 609f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 610f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate == null) { 611f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 612f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 613f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 614f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin will compare candidate " + candidate.SSID 615f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " with " + config.SSID + " key " + config.configKey(true)); 616f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 617f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 618f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int order = compareWifiConfigurations(candidate, config); 619f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 620f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 621f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin did compare candidate " + Integer.toString(order)); 622f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 623f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 624f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (order > 0) { 625f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //ascending : candidate < config 626f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = config; 627f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 628f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 629f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 630f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 631f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* now, go thru scan result to try finding a better Herrevad network */ 632f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mNetworkScoreCache != null) { 633f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi5 = WifiConfiguration.INVALID_RSSI; 634f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int rssi24 = WifiConfiguration.INVALID_RSSI; 635f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle WifiConfiguration.Visibility visibility; 636f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate != null) { 637f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi5 = candidate.visibility.rssi5; 638f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle rssi24 = candidate.visibility.rssi24; 639f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 640f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 641f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //get current date 642f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle Date now = new Date(); 643f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle long now_ms = now.getTime(); 644f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 645f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (rssi5 < -60 && rssi24 < -70) { 646f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for (ScanResult result : scanResultCache.values()) { 647f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((now_ms - result.seen) < 3000) { 648f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int score = mNetworkScoreCache.getNetworkScore(result); 649f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (score > 0) { 650f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // try any arbitrary formula for now, adding apple and oranges, 651f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // i.e. adding network score and "dBm over noise" 652f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (result.frequency < 4000) { 653f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.level + score) > (rssi24 -40)) { 654f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // force it as open, TBD should we otherwise verify that this 655f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // BSSID only supports open?? 656f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.capabilities = ""; 657f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 658f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //switch to this scan result 659f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = 660f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.wifiConfigurationFromScanResult(result); 661f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.ephemeral = true; 662f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 663f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 664f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if ((result.level + score) > (rssi5 -30)) { 665f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // force it as open, TBD should we otherwise verify that this 666f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle // BSSID only supports open?? 667f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle result.capabilities = ""; 668f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 669f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //switch to this scan result 670f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate = 671f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiConfigStore.wifiConfigurationFromScanResult(result); 672f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.ephemeral = true; 673f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 674f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 675f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 676f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 677f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 678f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 679f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 680f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 681f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (candidate != null) { 682f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* if candidate is found, check the state of the connection so as 683f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle to decide if we should be acting on this candidate and switching over */ 684f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) { 685f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin did find candidate " + candidate.SSID 686f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " key " + candidate.configKey(true)); 687f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 689f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle int networkDelta = compareNetwork(candidate); 690f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (networkDelta > 0) 691f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("attemptAutoJoin did find candidate " + candidate.SSID 692f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " for delta " + Integer.toString(networkDelta)); 693f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 694f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle /* ASK traffic poller permission to switch: 695f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle for instance, 696f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if user is currently streaming voice traffic, 697f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle then don’t switch regardless of the delta */ 698f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 699f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mWifiTrafficPoller.shouldSwitchNetwork(networkDelta)) { 700f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (mStaStaSupported) { 701f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 702f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } else { 703f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (DBG) { 704f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle logDbg("AutoJoin auto connect to netId " 705f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + Integer.toString(candidate.networkId) 706f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle + " SSID " + candidate.SSID); 707f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 708f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 709f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT, 710f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle candidate.networkId); 711f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //mWifiConfigStore.enableNetworkWithoutBroadcast(candidate.networkId, true); 712f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 713f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //we would do the below only if we want to persist the new choice 714f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle //mWifiConfigStore.selectNetwork(candidate.networkId); 715f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 716f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 717f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 718f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 719f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle if (VDBG) logDbg("Done attemptAutoJoin"); 720f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle } 721f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle} 722f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle 723