1 /*
2  * Copyright 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wifi;
18 
19 import android.net.wifi.ScanResult;
20 
21 import java.util.Collection;
22 
23 /**
24  * Evaluates ScanResults for Wifi Wake.
25  */
26 public class WakeupEvaluator {
27 
28     private final ScoringParams mScoringParams;
29 
WakeupEvaluator(ScoringParams scoringParams)30     WakeupEvaluator(ScoringParams scoringParams) {
31         mScoringParams = scoringParams;
32     }
33 
34     /**
35      * Searches ScanResults to find a connectable network.
36      *
37      * <p>This method searches the given ScanResults for one that is present in the given
38      * ScanResultMatchInfos and has a sufficiently high RSSI. If there is no such ScanResult, it
39      * returns null. If there are multiple, it returns the one with the highest RSSI.
40      *
41      * @param scanResults ScanResults to search
42      * @param networks Network list to compare against
43      * @return The {@link ScanResult} representing an in-range connectable network, or {@code null}
44      *         signifying there is no viable network
45      */
findViableNetwork(Collection<ScanResult> scanResults, Collection<ScanResultMatchInfo> networks)46     public ScanResult findViableNetwork(Collection<ScanResult> scanResults,
47                                         Collection<ScanResultMatchInfo> networks) {
48         ScanResult selectedScanResult = null;
49 
50         for (ScanResult scanResult : scanResults) {
51             if (isBelowThreshold(scanResult)) {
52                 continue;
53             }
54             if (networks.contains(ScanResultMatchInfo.fromScanResult(scanResult))) {
55                 if (selectedScanResult == null || selectedScanResult.level < scanResult.level) {
56                     selectedScanResult = scanResult;
57                 }
58             }
59         }
60 
61         return selectedScanResult;
62     }
63 
64     /**
65      * Returns whether the given ScanResult's signal strength is below the selection threshold.
66      */
isBelowThreshold(ScanResult scanResult)67     public boolean isBelowThreshold(ScanResult scanResult) {
68         return scanResult.level < mScoringParams.getEntryRssi(scanResult.frequency);
69     }
70 }
71