1 /* 2 * Copyright (C) 2014 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 android.net; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.util.Arrays; 26 import java.util.Objects; 27 28 /** 29 * A curve defining the network score over a range of RSSI values. 30 * 31 * <p>For each RSSI bucket, the score may be any byte. Scores have no absolute meaning and are only 32 * considered relative to other scores assigned by the same scorer. Networks with no score are 33 * treated equivalently to a network with score {@link Byte#MIN_VALUE}, and will not be used. 34 * 35 * <p>For example, consider a curve starting at -110 dBm with a bucket width of 10 and the 36 * following buckets: {@code [-20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]}. 37 * This represents a linear curve between -110 dBm and 30 dBm. It scores progressively higher at 38 * stronger signal strengths. 39 * 40 * <p>A network can be assigned a fixed score independent of RSSI by setting 41 * {@link #rssiBuckets} to a one-byte array whose element is the fixed score. {@link #start} 42 * should be set to the lowest RSSI value at which this fixed score should apply, and 43 * {@link #bucketWidth} should be set such that {@code start + bucketWidth} is equal to the 44 * highest RSSI value at which this fixed score should apply. 45 * 46 * <p>Note that RSSI values below -110 dBm or above 30 dBm are unlikely to cause any difference 47 * in connectivity behavior from those endpoints. That is, the connectivity framework will treat 48 * a network with a -120 dBm signal exactly as it would treat one with a -110 dBm signal. 49 * Therefore, graphs which specify scores outside this range may be truncated to this range by 50 * the system. 51 * 52 * @see ScoredNetwork 53 * @hide 54 */ 55 @SystemApi 56 public class RssiCurve implements Parcelable { 57 private static final int DEFAULT_ACTIVE_NETWORK_RSSI_BOOST = 25; 58 59 /** The starting dBm of the curve. */ 60 public final int start; 61 62 /** The width of each RSSI bucket, in dBm. */ 63 public final int bucketWidth; 64 65 /** The score for each RSSI bucket. */ 66 public final byte[] rssiBuckets; 67 68 /** 69 * The RSSI boost to give this network when active, in dBm. 70 * 71 * <p>When the system is connected to this network, it will pretend that the network has this 72 * much higher of an RSSI. This is to avoid switching networks when another network has only a 73 * slightly higher score. 74 */ 75 public final int activeNetworkRssiBoost; 76 77 /** 78 * Construct a new {@link RssiCurve}. 79 * 80 * @param start the starting dBm of the curve. 81 * @param bucketWidth the width of each RSSI bucket, in dBm. 82 * @param rssiBuckets the score for each RSSI bucket. 83 */ RssiCurve(int start, int bucketWidth, byte[] rssiBuckets)84 public RssiCurve(int start, int bucketWidth, byte[] rssiBuckets) { 85 this(start, bucketWidth, rssiBuckets, DEFAULT_ACTIVE_NETWORK_RSSI_BOOST); 86 } 87 88 /** 89 * Construct a new {@link RssiCurve}. 90 * 91 * @param start the starting dBm of the curve. 92 * @param bucketWidth the width of each RSSI bucket, in dBm. 93 * @param rssiBuckets the score for each RSSI bucket. 94 * @param activeNetworkRssiBoost the RSSI boost to apply when this network is active, in dBm. 95 */ RssiCurve(int start, int bucketWidth, byte[] rssiBuckets, int activeNetworkRssiBoost)96 public RssiCurve(int start, int bucketWidth, byte[] rssiBuckets, int activeNetworkRssiBoost) { 97 this.start = start; 98 this.bucketWidth = bucketWidth; 99 if (rssiBuckets == null || rssiBuckets.length == 0) { 100 throw new IllegalArgumentException("rssiBuckets must be at least one element large."); 101 } 102 this.rssiBuckets = rssiBuckets; 103 this.activeNetworkRssiBoost = activeNetworkRssiBoost; 104 } 105 RssiCurve(Parcel in)106 private RssiCurve(Parcel in) { 107 start = in.readInt(); 108 bucketWidth = in.readInt(); 109 int bucketCount = in.readInt(); 110 rssiBuckets = new byte[bucketCount]; 111 in.readByteArray(rssiBuckets); 112 activeNetworkRssiBoost = in.readInt(); 113 } 114 115 @Override describeContents()116 public int describeContents() { 117 return 0; 118 } 119 120 @Override writeToParcel(Parcel out, int flags)121 public void writeToParcel(Parcel out, int flags) { 122 out.writeInt(start); 123 out.writeInt(bucketWidth); 124 out.writeInt(rssiBuckets.length); 125 out.writeByteArray(rssiBuckets); 126 out.writeInt(activeNetworkRssiBoost); 127 } 128 129 /** 130 * Lookup the score for a given RSSI value. 131 * 132 * @param rssi The RSSI to lookup. If the RSSI falls below the start of the curve, the score at 133 * the start of the curve will be returned. If it falls after the end of the curve, the 134 * score at the end of the curve will be returned. 135 * @return the score for the given RSSI. 136 */ lookupScore(int rssi)137 public byte lookupScore(int rssi) { 138 return lookupScore(rssi, false /* isActiveNetwork */); 139 } 140 141 /** 142 * Lookup the score for a given RSSI value. 143 * 144 * @param rssi The RSSI to lookup. If the RSSI falls below the start of the curve, the score at 145 * the start of the curve will be returned. If it falls after the end of the curve, the 146 * score at the end of the curve will be returned. 147 * @param isActiveNetwork Whether this network is currently active. 148 * @return the score for the given RSSI. 149 */ lookupScore(int rssi, boolean isActiveNetwork)150 public byte lookupScore(int rssi, boolean isActiveNetwork) { 151 if (isActiveNetwork) { 152 rssi += activeNetworkRssiBoost; 153 } 154 155 int index = (rssi - start) / bucketWidth; 156 157 // Snap the index to the closest bucket if it falls outside the curve. 158 if (index < 0) { 159 index = 0; 160 } else if (index > rssiBuckets.length - 1) { 161 index = rssiBuckets.length - 1; 162 } 163 164 return rssiBuckets[index]; 165 } 166 167 /** 168 * Determine if two RSSI curves are defined in the same way. 169 * 170 * <p>Note that two curves can be equivalent but defined differently, e.g. if one bucket in one 171 * curve is split into two buckets in another. For the purpose of this method, these curves are 172 * not considered equal to each other. 173 */ 174 @Override equals(@ullable Object o)175 public boolean equals(@Nullable Object o) { 176 if (this == o) return true; 177 if (o == null || getClass() != o.getClass()) return false; 178 179 RssiCurve rssiCurve = (RssiCurve) o; 180 181 return start == rssiCurve.start && 182 bucketWidth == rssiCurve.bucketWidth && 183 Arrays.equals(rssiBuckets, rssiCurve.rssiBuckets) && 184 activeNetworkRssiBoost == rssiCurve.activeNetworkRssiBoost; 185 } 186 187 @Override hashCode()188 public int hashCode() { 189 return Objects.hash(start, bucketWidth, activeNetworkRssiBoost) ^ Arrays.hashCode(rssiBuckets); 190 } 191 192 @NonNull 193 @Override toString()194 public String toString() { 195 StringBuilder sb = new StringBuilder(); 196 sb.append("RssiCurve[start=") 197 .append(start) 198 .append(",bucketWidth=") 199 .append(bucketWidth) 200 .append(",activeNetworkRssiBoost=") 201 .append(activeNetworkRssiBoost); 202 203 sb.append(",buckets="); 204 for (int i = 0; i < rssiBuckets.length; i++) { 205 sb.append(rssiBuckets[i]); 206 if (i < rssiBuckets.length - 1) { 207 sb.append(","); 208 } 209 } 210 sb.append("]"); 211 212 return sb.toString(); 213 } 214 215 public static final @android.annotation.NonNull Creator<RssiCurve> CREATOR = 216 new Creator<RssiCurve>() { 217 @Override 218 public RssiCurve createFromParcel(Parcel in) { 219 return new RssiCurve(in); 220 } 221 222 @Override 223 public RssiCurve[] newArray(int size) { 224 return new RssiCurve[size]; 225 } 226 }; 227 } 228