summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/ScanDetailCache.java
blob: 5030ef936986317fa408be9174bc146c6bea85ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

package com.android.server.wifi;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.os.SystemClock;

import com.android.server.wifi.ScanDetail;
import com.android.server.wifi.hotspot2.PasspointMatch;
import com.android.server.wifi.hotspot2.PasspointMatchInfo;
import com.android.server.wifi.hotspot2.pps.HomeSP;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

class ScanDetailCache {

    private static final String TAG = "ScanDetailCache";

    private WifiConfiguration mConfig;
    private HashMap<String, ScanDetail> mMap;
    private HashMap<String, PasspointMatchInfo> mPasspointMatches;
    private static boolean DBG=false;
    ScanDetailCache(WifiConfiguration config) {
        mConfig = config;
        mMap = new HashMap();
        mPasspointMatches = new HashMap();
    }

    void put(ScanDetail scanDetail) {
        put(scanDetail, null, null);
    }

    void put(ScanDetail scanDetail, PasspointMatch match, HomeSP homeSp) {

        mMap.put(scanDetail.getBSSIDString(), scanDetail);

        if (match != null && homeSp != null) {
            mPasspointMatches.put(scanDetail.getBSSIDString(),
                    new PasspointMatchInfo(match, scanDetail, homeSp));
        }
    }

    ScanResult get(String bssid) {
        ScanDetail scanDetail = getScanDetail(bssid);
        return scanDetail == null ? null : scanDetail.getScanResult();
    }

    ScanDetail getScanDetail(String bssid) {
        return mMap.get(bssid);
    }

    void remove(String bssid) {
        mMap.remove(bssid);
    }

    int size() {
        return mMap.size();
    }

    boolean isEmpty() {
        return size() == 0;
    }

    ScanDetail getFirst() {
        Iterator<ScanDetail> it = mMap.values().iterator();
        return it.hasNext() ? it.next() : null;
    }

    Collection<String> keySet() {
        return mMap.keySet();
    }

    Collection<ScanDetail> values() {
        return mMap.values();
    }

    public void trim(int num) {
        int currentSize = mMap.size();
        if (currentSize <= num) {
            return; // Nothing to trim
        }
        ArrayList<ScanDetail> list = new ArrayList<ScanDetail>(mMap.values());
        if (list.size() != 0) {
            // Sort by descending timestamp
            Collections.sort(list, new Comparator() {
                public int compare(Object o1, Object o2) {
                    ScanDetail a = (ScanDetail) o1;
                    ScanDetail b = (ScanDetail) o2;
                    if (a.getSeen() > b.getSeen()) {
                        return 1;
                    }
                    if (a.getSeen() < b.getSeen()) {
                        return -1;
                    }
                    return a.getBSSIDString().compareTo(b.getBSSIDString());
                }
            });
        }
        for (int i = 0; i < currentSize - num ; i++) {
            // Remove oldest results from scan cache
            ScanDetail result = list.get(i);
            mMap.remove(result.getBSSIDString());
            mPasspointMatches.remove(result.getBSSIDString());
        }
    }

    /* @hide */
    private ArrayList<ScanDetail> sort() {
        ArrayList<ScanDetail> list = new ArrayList<ScanDetail>(mMap.values());
        if (list.size() != 0) {
            Collections.sort(list, new Comparator() {
                public int compare(Object o1, Object o2) {
                    ScanResult a = ((ScanDetail)o1).getScanResult();
                    ScanResult b = ((ScanDetail)o2).getScanResult();
                    if (a.numIpConfigFailures > b.numIpConfigFailures) {
                        return 1;
                    }
                    if (a.numIpConfigFailures < b.numIpConfigFailures) {
                        return -1;
                    }
                    if (a.seen > b.seen) {
                        return -1;
                    }
                    if (a.seen < b.seen) {
                        return 1;
                    }
                    if (a.level > b.level) {
                        return -1;
                    }
                    if (a.level < b.level) {
                        return 1;
                    }
                    return a.BSSID.compareTo(b.BSSID);
                }
            });
        }
        return list;
    }

    public WifiConfiguration.Visibility getVisibilityByRssi(long age) {
        return getVisibilityByRssi(age, WifiManager.WIFI_FREQUENCY_BAND_AUTO);
    }

    public WifiConfiguration.Visibility getVisibilityByRssi(long age, int configBand) {
        WifiConfiguration.Visibility status = new WifiConfiguration.Visibility();

        long now_ms = System.currentTimeMillis();
        long now_elapsed_ms = SystemClock.elapsedRealtime();
        boolean isNetworkFound = false;
        String profileConfigKey = mConfig.configKey();
        for(ScanDetail scanDetail : values()) {
            ScanResult result = scanDetail.getScanResult();
            if (scanDetail.getSeen() == 0)
                continue;

            if (result.is5GHz()) {
                if (configBand == WifiManager.WIFI_FREQUENCY_BAND_2GHZ) {
                    continue;
            }
                //strictly speaking: [4915, 5825]
                //number of known BSSID on 5GHz band
                status.num5 = status.num5 + 1;
            } else if (result.is24GHz()) {
                if (configBand == WifiManager.WIFI_FREQUENCY_BAND_5GHZ) {
                    continue;
            }
                //strictly speaking: [2412, 2482]
                //number of known BSSID on 2.4Ghz band
                status.num24 = status.num24 + 1;
            }

            if (result.timestamp != 0) {
                if (DBG) {
                    Log.e("getVisibilityByRssi", " considering " + result.SSID + " " + result.BSSID
                        + " elapsed=" + now_elapsed_ms + " timestamp=" + result.timestamp
                        + " age = " + age);
                }
                if ((now_elapsed_ms - (result.timestamp/1000)) > age) continue;
            } else {
                // This check the time at which we have received the scan result from supplicant
                if ((now_ms - result.seen) > age) continue;
            }

            if (result.is5GHz()) {
                if (profileConfigKey.equals(WifiConfiguration.configKey(result))) {
                    isNetworkFound = true;
                }
                if (result.level > status.rssi5) {
                    status.rssi5 = result.level;
                    status.age5 = result.seen;
                    status.BSSID5 = result.BSSID;
                }
            } else if (result.is24GHz()) {
                if (profileConfigKey.equals(WifiConfiguration.configKey(result))) {
                    isNetworkFound = true;
                }
                if (result.level > status.rssi24) {
                    status.rssi24 = result.level;
                    status.age24 = result.seen;
                    status.BSSID24 = result.BSSID;
                }
            }
        }
        /*
         * Visibility should be set to null if there is no BSSIDs in
         * both bands,so that auto join will not consider this network
         * for connection attempt.
         */
        if (isNetworkFound) {
            return status;
        } else {
            return null;
        }
    }

    public WifiConfiguration.Visibility getVisibilityByPasspointMatch(long age) {

        long now_ms = System.currentTimeMillis();
        PasspointMatchInfo pmiBest24 = null, pmiBest5 = null;

        for(PasspointMatchInfo pmi : mPasspointMatches.values()) {
            ScanDetail scanDetail = pmi.getScanDetail();
            if (scanDetail == null) continue;
            ScanResult result = scanDetail.getScanResult();
            if (result == null) continue;

            if (scanDetail.getSeen() == 0)
                continue;

            if ((now_ms - result.seen) > age) continue;

            if (result.is5GHz()) {
                if (pmiBest5 == null || pmiBest5.compareTo(pmi) < 0) {
                    pmiBest5 = pmi;
                }
            } else if (result.is24GHz()) {
                if (pmiBest24 == null || pmiBest24.compareTo(pmi) < 0) {
                    pmiBest24 = pmi;
                }
            }
        }

        WifiConfiguration.Visibility status = new WifiConfiguration.Visibility();
        String logMsg = "Visiblity by passpoint match returned ";
        if (pmiBest5 != null) {
            ScanResult result = pmiBest5.getScanDetail().getScanResult();
            status.rssi5 = result.level;
            status.age5 = result.seen;
            status.BSSID5 = result.BSSID;
            logMsg += "5 GHz BSSID of " + result.BSSID;
        }
        if (pmiBest24 != null) {
            ScanResult result = pmiBest24.getScanDetail().getScanResult();
            status.rssi24 = result.level;
            status.age24 = result.seen;
            status.BSSID24 = result.BSSID;
            logMsg += "2.4 GHz BSSID of " + result.BSSID;
        }

        Log.d(TAG, logMsg);

        return status;
    }

    public WifiConfiguration.Visibility getVisibility(long age) {
        return getVisibility(age, WifiManager.WIFI_FREQUENCY_BAND_AUTO);
    }

    public WifiConfiguration.Visibility getVisibility(long age, int configBand) {
        if (mConfig.isPasspoint()) {
            return getVisibilityByPasspointMatch(age);
        } else {
            return getVisibilityByRssi(age, configBand);
        }
    }



    @Override
    public String toString() {
        StringBuilder sbuf = new StringBuilder();
        sbuf.append("Scan Cache:  ").append('\n');

        ArrayList<ScanDetail> list = sort();
        long now_ms = System.currentTimeMillis();
        if (list.size() > 0) {
            for (ScanDetail scanDetail : list) {
                ScanResult result = scanDetail.getScanResult();
                long milli = now_ms - scanDetail.getSeen();
                long ageSec = 0;
                long ageMin = 0;
                long ageHour = 0;
                long ageMilli = 0;
                long ageDay = 0;
                if (now_ms > scanDetail.getSeen() && scanDetail.getSeen() > 0) {
                    ageMilli = milli % 1000;
                    ageSec   = (milli / 1000) % 60;
                    ageMin   = (milli / (60*1000)) % 60;
                    ageHour  = (milli / (60*60*1000)) % 24;
                    ageDay   = (milli / (24*60*60*1000));
                }
                sbuf.append("{").append(result.BSSID).append(",").append(result.frequency);
                sbuf.append(",").append(String.format("%3d", result.level));
                if (result.autoJoinStatus > 0) {
                    sbuf.append(",st=").append(result.autoJoinStatus);
                }
                if (ageSec > 0 || ageMilli > 0) {
                    sbuf.append(String.format(",%4d.%02d.%02d.%02d.%03dms", ageDay,
                            ageHour, ageMin, ageSec, ageMilli));
                }
                if (result.numIpConfigFailures > 0) {
                    sbuf.append(",ipfail=");
                    sbuf.append(result.numIpConfigFailures);
                }
                sbuf.append("} ");
            }
            sbuf.append('\n');
        }

        return sbuf.toString();
    }

}