summaryrefslogtreecommitdiff
path: root/service/java/com/android/server/wifi/ConnectedScore.java
blob: b02db5852563c04182b356db7edfedeb2f4e79f4 (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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi;

import android.net.wifi.WifiInfo;

/**
 * Base class for connection scoring
 */
public abstract class ConnectedScore {

    /** Maximum NetworkAgent score that should be generated by wifi */
    public static final int WIFI_MAX_SCORE = 60;

    /** Score at which wifi is considered poor enough to give up ant try something else */
    public static final int WIFI_TRANSITION_SCORE = WIFI_MAX_SCORE - 10;

    public static final int WIFI_MIN_SCORE = 0;

    final Clock mClock;

    /** This is a typical STD for the connected RSSI for a phone sitting still */
    public double mDefaultRssiStandardDeviation = 2.0;

    /**
     *
     * @param clock is the time source for getMillis()
     */
    public ConnectedScore(Clock clock) {
        mClock = clock;
    }

    /**
     * Returns the current time in milliseconds
     *
     * This time is to be passed into the update methods.
     * The scoring methods generally don't need a particular epoch, depending
     * only on deltas. So a different time source may be used, as long as it is consistent.
     *
     * Note that when there are long intervals between updates, it is unlikely to matter much
     * how large the interval is, so a time source that does not update while the processor is
     * asleep could be just fine.
     *
     * @return millisecond-resolution time.
     */
    public long getMillis() {
        return mClock.getWallClockMillis();
    }

    /**
     * Updates scoring state using RSSI alone
     *
     * @param rssi signal strength (dB).
     * @param millis millisecond-resolution time.
     */
    public void updateUsingRssi(int rssi, long millis) {
        updateUsingRssi(rssi, millis, mDefaultRssiStandardDeviation);
    }

    /**
     * Updates scoring state using RSSI and noise estimate
     *
     * This is useful if an RSSI comes from another source (e.g. scan results) and the
     * expected noise varies by source.
     *
     * @param rssi signal strength (dB).
     * @param millis millisecond-resolution time.
     * @param standardDeviation of the RSSI.
     */
    public abstract void updateUsingRssi(int rssi, long millis, double standardDeviation);

    /**
     * Updates the score using relevant parts of WifiInfo
     *
     * @param wifiInfo object holding relevant values.
     * @param millis millisecond-resolution time.
     */
    public void updateUsingWifiInfo(WifiInfo wifiInfo, long millis) {
        updateUsingRssi(wifiInfo.getRssi(), millis);
    }

    /**
     * Generates a score based on the current state
     *
     * @return network score - on NetworkAgent scale.
     */
    public abstract int generateScore();

    /**
     * Clears out state associated with the connection
     */
    public abstract void reset();
}