1 /*
2  * Copyright (C) 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.annotation.Nullable;
20 import android.net.wifi.WifiInfo;
21 
22 import com.android.server.wifi.ActiveModeManager.ClientRole;
23 /**
24  * Base class for connection scoring
25  */
26 public abstract class ConnectedScore {
27 
28     /** Maximum NetworkAgent score that should be generated by wifi */
29     public static final int WIFI_MAX_SCORE = 60;
30 
31     /** Initial Wifi score when starting up NetworkAgent. */
32     public static final int WIFI_INITIAL_SCORE = WIFI_MAX_SCORE;
33 
34     /** Wifi score of secondary network compared to primary network. */
35     public static final int WIFI_SECONDARY_DELTA_SCORE = 8;
36     /** Maximum NetworkAgent score for secondary network that should be generated by wifi */
37     public static final int WIFI_SECONDARY_MAX_SCORE = WIFI_MAX_SCORE - WIFI_SECONDARY_DELTA_SCORE;
38     /** Initial Wifi score of secondary network when starting up NetworkAgent. */
39     public static final int WIFI_SECONDARY_INITIAL_SCORE = WIFI_INITIAL_SCORE
40             - WIFI_SECONDARY_DELTA_SCORE;
41 
42     /** Score at which wifi is considered poor enough to give up ant try something else */
43     public static final int WIFI_TRANSITION_SCORE = WIFI_MAX_SCORE - 10;
44 
45     public static final int WIFI_SECONDARY_TRANSITION_SCORE = WIFI_TRANSITION_SCORE
46             - WIFI_SECONDARY_DELTA_SCORE;
47 
48     public static final int WIFI_MIN_SCORE = 0;
49 
50     final Clock mClock;
51 
52     /** This is a typical STD for the connected RSSI for a phone sitting still */
53     public double mDefaultRssiStandardDeviation = 2.0;
54 
55     /** The current role of the WifiScoreReport owns this scorer. */
56     @Nullable
57     private ClientRole mCurrentRole = null;
58 
59     /**
60      *
61      * @param clock is the time source for getMillis()
62      */
ConnectedScore(Clock clock)63     public ConnectedScore(Clock clock) {
64         mClock = clock;
65     }
66 
67     /**
68      * Returns the current time in milliseconds
69      *
70      * This time is to be passed into the update methods.
71      * The scoring methods generally don't need a particular epoch, depending
72      * only on deltas. So a different time source may be used, as long as it is consistent.
73      *
74      * Note that when there are long intervals between updates, it is unlikely to matter much
75      * how large the interval is, so a time source that does not update while the processor is
76      * asleep could be just fine.
77      *
78      * @return millisecond-resolution time.
79      */
getMillis()80     public long getMillis() {
81         return mClock.getWallClockMillis();
82     }
83 
84     /**
85      * Updates scoring state using RSSI alone
86      *
87      * @param rssi signal strength (dB).
88      * @param millis millisecond-resolution time.
89      */
updateUsingRssi(int rssi, long millis)90     public void updateUsingRssi(int rssi, long millis) {
91         updateUsingRssi(rssi, millis, mDefaultRssiStandardDeviation);
92     }
93 
94     /**
95      * Updates scoring state using RSSI and noise estimate
96      *
97      * This is useful if an RSSI comes from another source (e.g. scan results) and the
98      * expected noise varies by source.
99      *
100      * @param rssi signal strength (dB).
101      * @param millis millisecond-resolution time.
102      * @param standardDeviation of the RSSI.
103      */
updateUsingRssi(int rssi, long millis, double standardDeviation)104     public abstract void updateUsingRssi(int rssi, long millis, double standardDeviation);
105 
106     /**
107      * Updates the score using relevant parts of WifiInfo
108      *
109      * @param wifiInfo object holding relevant values.
110      * @param millis millisecond-resolution time.
111      */
updateUsingWifiInfo(WifiInfo wifiInfo, long millis)112     public void updateUsingWifiInfo(WifiInfo wifiInfo, long millis) {
113         updateUsingRssi(wifiInfo.getRssi(), millis);
114     }
115 
116     /**
117      * Generates a score based on the current state
118      *
119      * @return network score - on NetworkAgent scale.
120      */
generateScore()121     public abstract int generateScore();
122 
123     /**
124      * Clears out state associated with the connection
125      */
reset()126     public abstract void reset();
127 
128     /** Called when the owner {@link ConcreteClientModeManager}'s role changes. */
onRoleChanged(@ullable ClientRole role)129     public void onRoleChanged(@Nullable ClientRole role) {
130         mCurrentRole = role;
131     }
132 
133     /** Returns whether this scores primary network based on the role */
isPrimary()134     protected boolean isPrimary() {
135         return mCurrentRole != null && mCurrentRole == ActiveModeManager.ROLE_CLIENT_PRIMARY;
136     }
137 }
138