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