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