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.net.wifi.WifiInfo;
20 
21 /**
22  * Base class for connection scoring
23  */
24 public abstract class ConnectedScore {
25 
26     /** Maximum NetworkAgent score that should be generated by wifi */
27     public static final int WIFI_MAX_SCORE = 60;
28 
29     /** Score at which wifi is considered poor enough to give up ant try something else */
30     public static final int WIFI_TRANSITION_SCORE = WIFI_MAX_SCORE - 10;
31 
32     public static final int WIFI_MIN_SCORE = 0;
33 
34     final Clock mClock;
35 
36     /** This is a typical STD for the connected RSSI for a phone sitting still */
37     public double mDefaultRssiStandardDeviation = 2.0;
38 
39     /**
40      *
41      * @param clock is the time source for getMillis()
42      */
ConnectedScore(Clock clock)43     public ConnectedScore(Clock clock) {
44         mClock = clock;
45     }
46 
47     /**
48      * Returns the current time in milliseconds
49      *
50      * This time is to be passed into the update methods.
51      * The scoring methods generally don't need a particular epoch, depending
52      * only on deltas. So a different time source may be used, as long as it is consistent.
53      *
54      * Note that when there are long intervals between updates, it is unlikely to matter much
55      * how large the interval is, so a time source that does not update while the processor is
56      * asleep could be just fine.
57      *
58      * @return millisecond-resolution time.
59      */
getMillis()60     public long getMillis() {
61         return mClock.getWallClockMillis();
62     }
63 
64     /**
65      * Updates scoring state using RSSI alone
66      *
67      * @param rssi signal strength (dB).
68      * @param millis millisecond-resolution time.
69      */
updateUsingRssi(int rssi, long millis)70     public void updateUsingRssi(int rssi, long millis) {
71         updateUsingRssi(rssi, millis, mDefaultRssiStandardDeviation);
72     }
73 
74     /**
75      * Updates scoring state using RSSI and noise estimate
76      *
77      * This is useful if an RSSI comes from another source (e.g. scan results) and the
78      * expected noise varies by source.
79      *
80      * @param rssi signal strength (dB).
81      * @param millis millisecond-resolution time.
82      * @param standardDeviation of the RSSI.
83      */
updateUsingRssi(int rssi, long millis, double standardDeviation)84     public abstract void updateUsingRssi(int rssi, long millis, double standardDeviation);
85 
86     /**
87      * Updates the score using relevant parts of WifiInfo
88      *
89      * @param wifiInfo object holding relevant values.
90      * @param millis millisecond-resolution time.
91      */
updateUsingWifiInfo(WifiInfo wifiInfo, long millis)92     public void updateUsingWifiInfo(WifiInfo wifiInfo, long millis) {
93         updateUsingRssi(wifiInfo.getRssi(), millis);
94     }
95 
96     /**
97      * Generates a score based on the current state
98      *
99      * @return network score - on NetworkAgent scale.
100      */
generateScore()101     public abstract int generateScore();
102 
103     /**
104      * Clears out state associated with the connection
105      */
reset()106     public abstract void reset();
107 }
108