WifiAutoJoinController.java revision abde872adced15dfb6781fb71959453d963326db
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 ) {
93abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = true;
94ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = true;
95ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
96abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = false;
97ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = false;
98ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
99ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
100ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
101ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
102ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        int mScanResultMaximumAge = 30000; /* milliseconds unit */
103f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
104ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    /*
105ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * flush out scan results older than mScanResultMaximumAge
106ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     *
107ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * */
108f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void ageScanResultsOut(int delay) {
109f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (delay <= 0) {
110f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            delay = mScanResultMaximumAge; //something sane
111f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
112f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        Date now = new Date();
113f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long milli = now.getTime();
114f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
115f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("ageScanResultsOut delay " + Integer.valueOf(delay) + " size "
116f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.valueOf(scanResultCache.size()) + " now " + Long.valueOf(milli));
117f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
118f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
119f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        Iterator<HashMap.Entry<String,ScanResult>> iter = scanResultCache.entrySet().iterator();
120f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        while (iter.hasNext()) {
121f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            HashMap.Entry<String,ScanResult> entry = iter.next();
122f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            ScanResult result = entry.getValue();
123f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
124f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if ((result.seen + delay) < milli) {
125f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                iter.remove();
126f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
127f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
128f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
129f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
130f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void addToScanCache(List<ScanResult> scanList) {
131f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration associatedConfig;
132f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1330c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        ArrayList<NetworkKey> unknownScanResults = new ArrayList<NetworkKey>();
1340c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
135f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for(ScanResult result: scanList) {
136f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            result.seen = System.currentTimeMillis();
137f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
138f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            ScanResult sr = scanResultCache.get(result.BSSID);
139f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (sr != null) {
140f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                // if there was a previous cache result for this BSSID, average the RSSI values
141f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
142f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int previous_rssi = sr.level;
143f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                long previously_seen_milli = sr.seen;
144f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
145f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                /* average RSSI with previously seen instances of this scan result */
146f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int avg_rssi = result.level;
147f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
148f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if ((previously_seen_milli > 0)
149f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        && (previously_seen_milli < mScanResultMaximumAge/2)) {
150f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    /*
151f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    *
152f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    * previously_seen_milli = 0 => RSSI = 0.5 * previous_seen_rssi + 0.5 * new_rssi
153f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    *
154f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    * If previously_seen_milli is 15+ seconds old:
155f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    *      previously_seen_milli = 15000 => RSSI = new_rssi
156f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    *
157f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    */
158f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
159f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    double alpha = 0.5 - (double)previously_seen_milli
160f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            / (double)mScanResultMaximumAge;
161f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
162f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    avg_rssi = (int)((double)avg_rssi * (1-alpha) + (double)previous_rssi * alpha);
163f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
164f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                result.level = avg_rssi;
165f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
166f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                //remove the previous Scan Result
167f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                scanResultCache.remove(result.BSSID);
1680c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            } else {
1690c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                if (!mNetworkScoreCache.isScoredNetwork(result)) {
1700c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    //TODO : properly handle SSID formatting, i.e. among others check for string
1710c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    //TODO : representing hexadecimal SSIDs
1720c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    WifiKey wkey = new WifiKey("\"" + result.SSID + "\"", result.BSSID);
1730c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    NetworkKey nkey = new NetworkKey(wkey);
1740c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    //if we don't know this scan result then request a score to Herrevad
1750c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    unknownScanResults.add(nkey);
1760c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                }
177f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
178f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
179f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            scanResultCache.put(result.BSSID, new ScanResult(result));
180f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
181f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            ScanResult srn = scanResultCache.get(result.BSSID);
182f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
183f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //add this BSSID to the scanResultCache of the relevant WifiConfiguration
184f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            associatedConfig = mWifiConfigStore.updateSavedNetworkHistory(result);
185f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1860c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            //try to associate this BSSID to an existing Saved WifiConfiguration
187f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (associatedConfig == null) {
188f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                associatedConfig = mWifiConfigStore.associateWithConfiguration(result);
189f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (associatedConfig != null) {
190f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (VDBG) {
191f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        logDbg("addToScanCache save associated config "
192f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                + associatedConfig.SSID + " with " + associatedConfig.SSID);
193f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
194f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    mWifiStateMachine.sendMessage(WifiManager.SAVE_NETWORK, associatedConfig);
195f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
196f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
197f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1980c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
1990c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        if (unknownScanResults.size() != 0) {
2000c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            NetworkKey[] newKeys =
2010c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    unknownScanResults.toArray(new NetworkKey[unknownScanResults.size()]);
2020c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                //kick the score manager, we will get updated scores asynchronously
2030c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            scoreManager.requestScores(newKeys);
2040c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        }
205f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
206f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
207f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void logDbg(String message) {
2080888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        logDbg(message, false);
209ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    }
210ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
211ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    void logDbg(String message, boolean stackTrace) {
212f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
2130888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        String ts = String.format("[%,d us] ", now / 1000);
214ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (stackTrace) {
215ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            Log.e(TAG, ts + message + " stack:"
216ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[2].getMethodName() + " - "
217ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[3].getMethodName() + " - "
218ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[4].getMethodName() + " - "
219ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[5].getMethodName());
220ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        } else {
221ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            Log.e(TAG, ts + message);
222ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        }
223f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
224f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
225f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /* called directly from WifiStateMachine  */
226f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void newSupplicantResults() {
227f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        List<ScanResult> scanList = mWifiStateMachine.syncGetScanResultsList();
228f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        addToScanCache(scanList);
229f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(mScanResultMaximumAge);
230f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG)
231f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle           logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size()) );
232f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
233f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
234f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiConfigStore.writeKnownNetworkHistory();
235f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
236f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
237f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
238f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
239f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /* not used at the moment
240f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * should be a call back from WifiScanner HAL ??
241f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * this function is not hooked and working yet, it will receive scan results from WifiScanners
242f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan
243f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * results as normal.
244f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
245f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void newHalScanResults() {
246f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        List<ScanResult> scanList = null;//mWifiScanner.syncGetScanResultsList();
247f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String akm = WifiParser.parse_akm(null, null);
248f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        logDbg(akm);
249f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        addToScanCache(scanList);
250f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(0);
251f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
252f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiConfigStore.writeKnownNetworkHistory();
253f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
254f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
255f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /* network link quality changed, called directly from WifiTrafficPoller,
256f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    or by listening to Link Quality intent */
257f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void linkQualitySignificantChange() {
258f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
259f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
260f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
261f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /*
262f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * compare a WifiConfiguration against the current network, return a delta score
263f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * If not associated, and the candidate will always be better
264f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * For instance if the candidate is a home network versus an unknown public wifi,
265f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * the delta will be infinite, else compare Kepler scores etc…
266f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     ***/
267f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private int compareNetwork(WifiConfiguration candidate) {
268f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration();
269f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (currentNetwork == null)
270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return 1000;
271f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
272f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (candidate.configKey(true).equals(currentNetwork.configKey(true))) {
273f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return -1;
274f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = compareWifiConfigurations(currentNetwork, candidate);
277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (order > 0) {
279f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //ascending: currentNetwork < candidate
280f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return 10; //will try switch over to the candidate
281f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
282f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
283f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return 0;
284f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
285f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
286ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    /**
287ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * update the network history fields fo that configuration
288ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if userTriggered, we mark the configuration as "non selfAdded" since the user has seen it
289ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * and took over management
290ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if it is a "connect", remember which network were there at the point of the connect, so
291ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * as those networks get a relative lower score than the selected configuration
29262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle     *
293ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param netId
294ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param userTriggered : if the update come from WiFiManager
295ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param connect : if the update includes a connect
296117be607246604e875de62aa8cdd99700b77a2b4vandwalle     **/
29762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle    public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) {
298f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId);
299f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (selected == null) {
300f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return;
301f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
302f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
303e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        if (selected.SSID == null) {
304e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            return;
305e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        }
306e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
30762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        if (userTriggered) {
308ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            // reenable autojoin for this network,
30962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            // since the user want to connect to this configuration
31062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            selected.autoJoinStatus = WifiConfiguration.AUTO_JOIN_ENABLED;
31162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            selected.selfAdded = false;
31262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        }
313f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
314f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) {
315f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (selected.connectChoices != null) {
316ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
317f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + Integer.toString(netId) + " now: "
3180888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        + Integer.toString(selected.connectChoices.size()), true);
319f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
320ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
3210888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        + Integer.toString(netId), true);
322f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
323f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
324f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
325ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (connect && userTriggered) {
326ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            boolean found = false;
32762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            List<WifiConfiguration> networks =
32862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    mWifiConfigStore.getRecentConfiguredNetworks(12000, false);
32962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            if (networks != null) {
33062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                for (WifiConfiguration config : networks) {
33162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (DBG)
332ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        logDbg("updateConfigurationHistory got " + config.SSID);
333f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
33462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (selected.configKey(true).equals(config.configKey(true))) {
335ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        found = true;
33662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        continue;
33762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
338f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
33962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    int rssi = WifiConfiguration.INVALID_RSSI;
34062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (config.visibility != null) {
34162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        rssi = config.visibility.rssi5;
34262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        if (config.visibility.rssi24 > rssi)
34362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                            rssi = config.visibility.rssi24;
34462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
34562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (rssi < -80) {
34662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        continue;
34762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
348f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
349ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    //the selected configuration was preferred over a recently seen config
350ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    //hence remember the user's choice:
351ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    //add the recently seen config to the selected's connectChoices array
352ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
353ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    if (selected.connectChoices == null) {
354ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        selected.connectChoices = new HashMap<String, Integer>();
355ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    }
356ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
357ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    logDbg("updateConfigurationHistory add a choice " + selected.configKey(true)
3580888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + " over " + config.configKey(true)
3590888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + " RSSI " + Integer.toString(rssi), true);
3600888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    //add the visible config to the selected's connect choice list
36162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    selected.connectChoices.put(config.configKey(true), rssi);
362f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
36362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (config.connectChoices != null) {
364ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        if (VDBG) {
365ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                            logDbg("updateConfigurationHistory will remove "
36662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                    + selected.configKey(true) + " from " + config.configKey(true));
367ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        }
3680888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        //remove the selected from the recently seen config's connectChoice list
36962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        config.connectChoices.remove(selected.configKey(true));
3700888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
3710888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        if (selected.linkedConfigurations != null) {
3720888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           //remove the selected's linked configuration from the
3730888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           //recently seen config's connectChoice list
3740888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           for (String key : selected.linkedConfigurations.keySet()) {
3750888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                               config.connectChoices.remove(key);
3760888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           }
3770888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        }
37862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
379ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                }
380ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                if (found == false) {
381ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                     // log an error for now but do something stringer later
382ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                     // we will need a new scan before attempting to connect to this
383ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                     // configuration anyhow and thus we can process the scan results then
384ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                     logDbg("updateConfigurationHistory try to connect to an old network!! : "
385ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                             + selected.configKey());
38662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
387f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
38862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                if (selected.connectChoices != null) {
38962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (VDBG)
390ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        logDbg("updateConfigurationHistory " + Integer.toString(netId)
39162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                + " now: " + Integer.toString(selected.connectChoices.size()));
39262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
393ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
394ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                mWifiConfigStore.writeKnownNetworkHistory();
395f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
396f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
397f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
398f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
399f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void printChoices(WifiConfiguration config) {
400f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int num = 0;
401f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (config.connectChoices!= null) {
402f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            num = config.connectChoices.size();
403f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
404f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
405f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        logDbg("printChoices " + config.SSID + " num choices: " + Integer.toString(num));
406f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (config.connectChoices!= null) {
407f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            for (String key : config.connectChoices.keySet()) {
408f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                logDbg("                 " + key);
409f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
410f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
411f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
412f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
413f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    boolean hasConnectChoice(WifiConfiguration source, WifiConfiguration target) {
414f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        boolean found = false;
415f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (source == null)
416f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return false;
417f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (target == null)
418f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return false;
419f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
420f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (source.connectChoices != null) {
421f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if ( source.connectChoices.get(target.configKey(true)) != null) {
422f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                found = true;
423f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
424f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
425f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
426f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (source.linkedConfigurations != null) {
427f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            for (String key : source.linkedConfigurations.keySet()) {
428f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key);
429f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (config != null) {
430f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (config.connectChoices != null) {
431f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (config.connectChoices.get(target.configKey(true)) != null) {
432f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            found = true;
433f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
434f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
435f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
436f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
437f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
438f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return found;
439f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
440f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
441f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b) {
442f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
443f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int boost5 = 25;
444f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
445f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility astatus = a.visibility;
446f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility bstatus = b.visibility;
447f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (astatus == null || bstatus == null) {
448453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            //error -> cant happen, need to throw en exception
449f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("compareWifiConfigurations NULL band status!");
450f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return 0;
451f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
452453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if ((astatus.rssi5 > -70) && (bstatus.rssi5 == WifiConfiguration.INVALID_RSSI)
453f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                && ((astatus.rssi5+boost5) > (bstatus.rssi24))) {
454f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //a is seen on 5GHz with good RSSI, greater rssi than b
455f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //a is of higher priority - descending
456f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            order = -1;
457453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 == WifiConfiguration.INVALID_RSSI)
458f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                && ((bstatus.rssi5+boost5) > (bstatus.rssi24))) {
459f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //b is seen on 5GHz with good RSSI, greater rssi than a
460f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //a is of lower priority - ascending
461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            order = 1;
462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
463f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
464f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
465f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
466f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) {
467f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
468117be607246604e875de62aa8cdd99700b77a2b4vandwalle        String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration();
469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        boolean linked = false;
470f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
471453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null)
472453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (a.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)
473453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (b.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)) {
474f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if ((a.linkedConfigurations.get(b.configKey(true))!= null)
475f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    && (b.linkedConfigurations.get(a.configKey(true))!= null)) {
476f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                linked = true;
477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
480f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (a.ephemeral && b.ephemeral == false) {
481f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
482453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("compareWifiConfigurations ephemeral and prefers " + b.configKey()
483453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + a.configKey());
484f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
485f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return 1; //b is of higher priority - ascending
486f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
487f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (b.ephemeral && a.ephemeral == false) {
488f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
489453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("compareWifiConfigurations ephemeral and prefers " +a.configKey()
490453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + b.configKey());
491f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
492f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return -1; //a is of higher priority - descending
493f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
494f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int boost5 = 25;
496453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        //apply Hysteresis: boost the RSSI value of the currently connected configuration
497453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        int aRssiBoost = 0;
498453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        int bRssiBoost = 0;
499453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if (null != mCurrentConfigurationKey) {
500453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            if (a.configKey().equals(mCurrentConfigurationKey)) {
501453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                aRssiBoost += 10;
502453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            } else if (b.configKey().equals(mCurrentConfigurationKey)) {
503453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                bRssiBoost += 10;
504453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            }
505453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        }
506f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (linked) {
507f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            // then we try prefer 5GHz, and try to ignore user's choice
508f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            WifiConfiguration.Visibility astatus = a.visibility;
509f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            WifiConfiguration.Visibility bstatus = b.visibility;
510f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (astatus == null || bstatus == null) {
511f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                //error
512f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                logDbg("compareWifiConfigurations NULL band status!");
513f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                return 0;
514f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
515f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
516f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG)  {
517f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                logDbg("compareWifiConfigurations linked: " + Integer.toString(astatus.rssi5)
518f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + "," + Integer.toString(astatus.rssi24) + "   "
519f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + Integer.toString(bstatus.rssi5) + ","
520f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + Integer.toString(bstatus.rssi24));
521f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
522f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
523453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            if ((astatus.rssi5 > -70) && (bstatus.rssi5 <= WifiConfiguration.INVALID_RSSI)
524453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    && (astatus.rssi5+boost5+aRssiBoost) > (bstatus.rssi24+bRssiBoost)) {
525453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    //in this case: a has 5GHz and b doesn't have 5GHz
526453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    //compare a's 5GHz RSSI to b's 5GHz RSSI
527453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
528f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //a is seen on 5GHz with good RSSI, greater rssi than b
529f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //a is of higher priority - descending
530f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    order = -10;
531f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
532f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG) {
533453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("compareWifiConfigurations linked and prefers " + a.configKey()
534453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + " over " + b.configKey()
535f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            + " due to 5GHz RSSI " + Integer.toString(astatus.rssi5)
536f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            + " over: 5=" + Integer.toString(bstatus.rssi5)
537f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            + ", 2.4=" + Integer.toString(bstatus.rssi5));
538f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
539453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            } else if ((bstatus.rssi5 > -70) && (astatus.rssi5 <= WifiConfiguration.INVALID_RSSI)
540453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    && ((bstatus.rssi5+boost5+bRssiBoost) > (astatus.rssi24+aRssiBoost))) {
541453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    //in this case: b has 5GHz and a doesn't have 5GHz
542453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
543f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //b is seen on 5GHz with good RSSI, greater rssi than a
544f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //a is of lower priority - ascending
545f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG)   {
546453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("compareWifiConfigurations linked and prefers " + b.configKey()
547453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + " over " + a.configKey() + " due to 5GHz RSSI "
548f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            + Integer.toString(astatus.rssi5) + " over: 5="
549f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            + Integer.toString(bstatus.rssi5) + ", 2.4="
550f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            + Integer.toString(bstatus.rssi5));
551f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
552f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                order = 10;
553453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            } else {
554453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                //TODO: handle cases where configurations are dual band
555f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
556f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
557ede1310be531a84faa08f02c3fd243448dd936ddvandwalle
558ede1310be531a84faa08f02c3fd243448dd936ddvandwalle        //compare by user's choice.
559f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (hasConnectChoice(a, b)) {
560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //a is of higher priority - descending
561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            order = order -2;
562f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG)   {
563453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("compareWifiConfigurations prefers -2 " + a.configKey()
564453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + b.configKey()
565ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        + " due to user choice order -> " + Integer.toString(order));
566f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
567f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
568f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
569f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (hasConnectChoice(b, a)) {
570f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //a is of lower priority - ascending
571f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            order = order + 2;
572f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG)   {
573453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("compareWifiConfigurations prefers +2 " + b.configKey() + " over "
574453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + a.configKey() + " due to user choice order ->" + Integer.toString(order));
575f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
576f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
577f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
578ede1310be531a84faa08f02c3fd243448dd936ddvandwalle        //TODO count the number of association rejection
579ede1310be531a84faa08f02c3fd243448dd936ddvandwalle        // and use this to adjust the order by more than +/- 3
580ede1310be531a84faa08f02c3fd243448dd936ddvandwalle        if ((a.status == WifiConfiguration.Status.DISABLED)
581ede1310be531a84faa08f02c3fd243448dd936ddvandwalle                && (a.disableReason == WifiConfiguration.DISABLED_ASSOCIATION_REJECT)) {
582ede1310be531a84faa08f02c3fd243448dd936ddvandwalle            //a is of lower priority - ascending
583ede1310be531a84faa08f02c3fd243448dd936ddvandwalle            //lower the comparison score a bit
584ede1310be531a84faa08f02c3fd243448dd936ddvandwalle            order = order +3;
585ede1310be531a84faa08f02c3fd243448dd936ddvandwalle        }
586ede1310be531a84faa08f02c3fd243448dd936ddvandwalle        if ((b.status == WifiConfiguration.Status.DISABLED)
587ede1310be531a84faa08f02c3fd243448dd936ddvandwalle                && (b.disableReason == WifiConfiguration.DISABLED_ASSOCIATION_REJECT)) {
588ede1310be531a84faa08f02c3fd243448dd936ddvandwalle            //a is of higher priority - descending
589ede1310be531a84faa08f02c3fd243448dd936ddvandwalle            //lower the comparison score a bit
590ede1310be531a84faa08f02c3fd243448dd936ddvandwalle            order = order -3;
591ede1310be531a84faa08f02c3fd243448dd936ddvandwalle        }
592ede1310be531a84faa08f02c3fd243448dd936ddvandwalle
593ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if ((lastSelectedConfiguration != null)
594ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                && a.configKey().equals(lastSelectedConfiguration)) {
595ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            // a is the last selected configuration, so keep it above connect choices
596ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            //by giving a -4 (whereas connect choice preference gives +2)
597ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            order = order - 4;
598ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            if (VDBG)   {
599453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("compareWifiConfigurations prefers -4 " + a.configKey()
600453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + b.configKey() + " because a is the last selected -> "
601ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        + Integer.toString(order));
602ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            }
603ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        } else if ((lastSelectedConfiguration != null)
604ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                && b.configKey().equals(lastSelectedConfiguration)) {
605ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            // b is the last selected configuration, so keep it above connect choices
606ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            //by giving a +4 (whereas connect choice preference gives -2)
607ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            order = order + 4;
608ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            if (VDBG)   {
609453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("compareWifiConfigurations prefers +4 " + a.configKey()
610453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + b.configKey() + " because b is the last selected -> "
611ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        + Integer.toString(order));
612ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            }
613ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        }
614ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
615f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (order == 0) {
616f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //we don't know anything - pick the last seen i.e. K behavior
617f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //we should do this only for recently picked configurations
618f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (a.priority > b.priority) {
619f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                //a is of higher priority - descending
620f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG)   {
621453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("compareWifiConfigurations prefers -1 " + a.configKey() + " over "
622453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + b.configKey() + " due to priority");
623f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
624f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
625f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                order = -1;
626f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else if (a.priority < b.priority) {
627f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                //a is of lower priority - ascending
628f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG)  {
629453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("compareWifiConfigurations prefers +1 " + b.configKey() + " over "
630453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + a.configKey() + " due to priority");
631f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
632f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
633f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle              order = 1;
634f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
635f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                //maybe just look at RSSI or band
636f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG)  {
637453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("compareWifiConfigurations prefers +1 " + b.configKey() + " over "
638453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + a.configKey() + " due to nothing");
639f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
640f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
641f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                order = compareWifiConfigurationsRSSI(a, b); //compare RSSI
642f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
643f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
644f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
645f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String sorder = " == ";
646f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (order > 0)
647f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " < ";
648f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (order < 0)
649f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " > ";
650f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
651f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG)   {
652453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            logDbg("compareWifiConfigurations Done: " + a.configKey() + sorder
653453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    + b.configKey() + " order " + Integer.toString(order));
654f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
655f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
656f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
657f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
658f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
659f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /* attemptAutoJoin function implement the core of the a network switching algorithm */
660f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void attemptAutoJoin() {
6618c9088d11880553458f09377cc60d6eb7e66747bvandwalle        String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration();
6628c9088d11880553458f09377cc60d6eb7e66747bvandwalle
663453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        //reset the currentConfiguration Key, and set it only if WifiStateMachine and
664453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        // supplicant agree
665453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        mCurrentConfigurationKey = null;
666453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        WifiConfiguration currentConfiguration = mWifiStateMachine.getCurrentWifiConfiguration();
667453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
668f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration candidate = null;
669f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
670f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        /* obtain the subset of recently seen networks */
671f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        List<WifiConfiguration> list = mWifiConfigStore.getRecentConfiguredNetworks(3000, true);
672f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (list == null) {
673f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG)  logDbg("attemptAutoJoin nothing");
674f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return;
675f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
676f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
677f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        /* find the currently connected network: ask the supplicant directly */
678f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String val = mWifiNative.status();
679f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String status[] = val.split("\\r?\\n");
680f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
681f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("attemptAutoJoin() status=" + val + " split="
682f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.toString(status.length));
683f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
684f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
685f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int currentNetId = -1;
686f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (String key : status) {
687f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (key.regionMatches(0, "id=", 0, 3)) {
688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int idx = 3;
689f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                currentNetId = 0;
690f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                while (idx < key.length()) {
691f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    char c = key.charAt(idx);
692f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
693f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if ((c >= 0x30) && (c <= 0x39)) {
694f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        currentNetId *= 10;
695f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        currentNetId += c - 0x30;
696f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        idx++;
697f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    } else {
698f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        break;
699f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
700f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
701f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
702f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
703ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (DBG) {
704ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size())
705ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    + " ---> currentId=" + Integer.toString(currentNetId));
706ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
707f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
708453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if (currentConfiguration != null) {
709453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            if (currentNetId != currentConfiguration.networkId) {
710453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("attemptAutoJoin() ERROR wpa_supplicant out of sync nid="
711453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + Integer.toString(currentNetId) + " WifiStateMachine="
712453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + Integer.toString(currentConfiguration.networkId));
713453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                //I think this can happen due do race conditions, now what to do??
714453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                // -> throw an exception, or,
715453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                // -> dont use the current configuration at all for autojoin
716453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                //and hope that autojoining will kick us out of this state.
717453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                currentConfiguration = null;
718453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            } else {
719453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                mCurrentConfigurationKey = currentConfiguration.configKey();
720453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            }
721453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        }
722453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
723f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        /* select Best Network candidate from known WifiConfigurations */
724f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration config : list) {
725f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if ((config.status == WifiConfiguration.Status.DISABLED)
726f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    && (config.disableReason == WifiConfiguration.DISABLED_AUTH_FAILURE)) {
727ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
728ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    logDbg("attemptAutoJoin skip candidate due to auth failure key "
729ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + config.configKey(true));
730ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
731f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
732f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
733453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
734e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            if (config.SSID == null) {
735e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                return;
736e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            }
737e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
738453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            if (config.autoJoinStatus >= WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED) {
739453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                //avoid temporarily disabled networks altogether
740453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                //TODO: implement a better logic which will reenable the network after some time
741ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
742ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    logDbg("attemptAutoJoin skip candidate due to auto join status "
743ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + Integer.toString(config.autoJoinStatus) + " key "
744ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + config.configKey(true));
745ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
746f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
747f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
748f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
749f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config.networkId == currentNetId) {
750ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
751ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    logDbg("attemptAutoJoin skip current candidate  " + Integer.toString(currentNetId)
752ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + " key " + config.configKey(true));
753ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
754f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
755f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
756f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
7578c9088d11880553458f09377cc60d6eb7e66747bvandwalle            if (lastSelectedConfiguration == null || !config.configKey().equals(lastSelectedConfiguration)) {
7588c9088d11880553458f09377cc60d6eb7e66747bvandwalle                //don't try to autojoin a network that is too far
7598c9088d11880553458f09377cc60d6eb7e66747bvandwalle                if (config.visibility == null) {
7608c9088d11880553458f09377cc60d6eb7e66747bvandwalle                    continue;
7618c9088d11880553458f09377cc60d6eb7e66747bvandwalle                }
7628c9088d11880553458f09377cc60d6eb7e66747bvandwalle                if (config.visibility.rssi5 < -70 && config.visibility.rssi24 < -80) {
7638c9088d11880553458f09377cc60d6eb7e66747bvandwalle                    continue;
7648c9088d11880553458f09377cc60d6eb7e66747bvandwalle                }
7658c9088d11880553458f09377cc60d6eb7e66747bvandwalle            }
7668c9088d11880553458f09377cc60d6eb7e66747bvandwalle
767ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            if (DBG) {
768ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                logDbg("attemptAutoJoin trying candidate id=" + config.networkId + " "
769ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                        + config.SSID + " key " + config.configKey(true));
770ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            }
771f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
772f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate == null) {
773f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                candidate = config;
774f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
775f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG)  {
776453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("attemptAutoJoin will compare candidate  " + candidate.configKey()
777453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + " with " + config.configKey() + " key " + config.configKey(true));
778f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
779f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
780f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int order = compareWifiConfigurations(candidate, config);
781f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (order > 0) {
782f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //ascending : candidate < config
783f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    candidate = config;
784f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
785f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
786f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
787f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
788f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        /* now, go thru scan result to try finding a better Herrevad network */
789f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (mNetworkScoreCache != null) {
790f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi5 = WifiConfiguration.INVALID_RSSI;
791f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi24 = WifiConfiguration.INVALID_RSSI;
792f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            WifiConfiguration.Visibility visibility;
793f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate != null) {
794f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi5 = candidate.visibility.rssi5;
795f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi24 = candidate.visibility.rssi24;
796f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
797f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
798f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //get current date
799f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            Date now = new Date();
800f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            long now_ms = now.getTime();
801f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
802f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (rssi5 < -60 && rssi24 < -70) {
803f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                for (ScanResult result : scanResultCache.values()) {
804f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if ((now_ms - result.seen) < 3000) {
805f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        int score = mNetworkScoreCache.getNetworkScore(result);
806f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (score > 0) {
807f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            // try any arbitrary formula for now, adding apple and oranges,
808f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            // i.e. adding network score and "dBm over noise"
809f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                           if (result.frequency < 4000) {
810f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                if ((result.level + score) > (rssi24 -40)) {
811f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    // force it as open, TBD should we otherwise verify that this
812f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    // BSSID only supports open??
813f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    result.capabilities = "";
814f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
815f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    //switch to this scan result
816f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    candidate =
817f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                            mWifiConfigStore.wifiConfigurationFromScanResult(result);
818f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    candidate.ephemeral = true;
819f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                }
820f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                           } else {
821f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                if ((result.level + score) > (rssi5 -30)) {
822f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    // force it as open, TBD should we otherwise verify that this
823f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    // BSSID only supports open??
824f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    result.capabilities = "";
825f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
826f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    //switch to this scan result
827f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    candidate =
828f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                            mWifiConfigStore.wifiConfigurationFromScanResult(result);
829f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    candidate.ephemeral = true;
830f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                }
831f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                           }
832f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
833f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
834f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
835f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
836f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
837f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (candidate != null) {
838453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        /* if candidate is found, check the state of the connection so as
839453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        to decide if we should be acting on this candidate and switching over */
840f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int networkDelta = compareNetwork(candidate);
841ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            if (DBG && (networkDelta > 0)) {
842453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("attemptAutoJoin did find candidate " + candidate.configKey()
843f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + " for delta " + Integer.toString(networkDelta));
844ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            }
845f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            /* ASK traffic poller permission to switch:
846f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                for instance,
847f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if user is currently streaming voice traffic,
848f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                then don’t switch regardless of the delta */
849f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
850f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (mWifiTrafficPoller.shouldSwitchNetwork(networkDelta)) {
851f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (mStaStaSupported) {
852453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("mStaStaSupported --> error do nothing now ");
853f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                } else {
854f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (DBG) {
855453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        logDbg("AutoJoin auto connect with netId "
856f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                + Integer.toString(candidate.networkId)
857453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                                + " to " + candidate.configKey());
858f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
859f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
860f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT,
861f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            candidate.networkId);
862f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //mWifiConfigStore.enableNetworkWithoutBroadcast(candidate.networkId, true);
863f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
864f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //we would do the below only if we want to persist the new choice
865f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    //mWifiConfigStore.selectNetwork(candidate.networkId);
866f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
867f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
868f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
869f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
870f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) logDbg("Done attemptAutoJoin");
871f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
872f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle}
873f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
874