Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.telephony; |
| 18 | |
Nathan Harold | 78cf8ac | 2019-04-08 19:21:02 -0700 | [diff] [blame] | 19 | import android.annotation.IntRange; |
Artur Satayev | 74cb719 | 2019-12-10 17:47:56 +0000 | [diff] [blame] | 20 | import android.compat.annotation.UnsupportedAppUsage; |
David Brazdil | 2959b5c | 2019-05-15 10:49:22 +0100 | [diff] [blame] | 21 | import android.os.Build; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 24 | import android.os.PersistableBundle; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 25 | |
Meng Wang | 1dbea2f | 2020-01-30 12:25:08 -0800 | [diff] [blame] | 26 | import com.android.telephony.Rlog; |
| 27 | |
Nathan Harold | 017e7f9 | 2018-01-29 17:17:10 -0800 | [diff] [blame] | 28 | import java.util.Objects; |
| 29 | |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 30 | /** |
Wink Saville | e3a9cbc | 2013-04-17 16:40:17 -0700 | [diff] [blame] | 31 | * GSM signal strength related information. |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 32 | */ |
Wink Saville | c6e4917a | 2012-09-21 13:54:05 -0700 | [diff] [blame] | 33 | public final class CellSignalStrengthGsm extends CellSignalStrength implements Parcelable { |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 34 | |
| 35 | private static final String LOG_TAG = "CellSignalStrengthGsm"; |
| 36 | private static final boolean DBG = false; |
| 37 | |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 38 | private static final int GSM_RSSI_MAX = -51; |
| 39 | private static final int GSM_RSSI_GREAT = -89; |
| 40 | private static final int GSM_RSSI_GOOD = -97; |
| 41 | private static final int GSM_RSSI_MODERATE = -103; |
| 42 | private static final int GSM_RSSI_POOR = -107; |
andychou | 186a87b | 2019-04-09 14:50:31 +0800 | [diff] [blame] | 43 | private static final int GSM_RSSI_MIN = -113; |
| 44 | |
| 45 | private static final int[] sRssiThresholds = new int[] { |
| 46 | GSM_RSSI_POOR, GSM_RSSI_MODERATE, GSM_RSSI_GOOD, GSM_RSSI_GREAT}; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 47 | |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 48 | private int mRssi; // in dBm [-113, -51] or UNAVAILABLE |
Mathew Inwood | a838206 | 2018-08-16 17:01:12 +0100 | [diff] [blame] | 49 | @UnsupportedAppUsage |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 50 | private int mBitErrorRate; // bit error rate (0-7, 99) TS 27.007 8.5 or UNAVAILABLE |
David Brazdil | 2959b5c | 2019-05-15 10:49:22 +0100 | [diff] [blame] | 51 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Nathan Harold | a629ea3 | 2018-10-24 11:35:53 -0700 | [diff] [blame] | 52 | private int mTimingAdvance; // range from 0-219 or CellInfo.UNAVAILABLE if unknown |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 53 | private int mLevel; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 54 | |
Nathan Harold | 9d729c5 | 2018-01-29 19:05:29 -0800 | [diff] [blame] | 55 | /** @hide */ |
Mathew Inwood | a838206 | 2018-08-16 17:01:12 +0100 | [diff] [blame] | 56 | @UnsupportedAppUsage |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 57 | public CellSignalStrengthGsm() { |
| 58 | setDefaultValues(); |
| 59 | } |
| 60 | |
Nathan Harold | 9d729c5 | 2018-01-29 19:05:29 -0800 | [diff] [blame] | 61 | /** @hide */ |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 62 | public CellSignalStrengthGsm(int rssi, int ber, int ta) { |
andychou | 186a87b | 2019-04-09 14:50:31 +0800 | [diff] [blame] | 63 | mRssi = inRangeOrUnavailable(rssi, GSM_RSSI_MIN, GSM_RSSI_MAX); |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 64 | mBitErrorRate = inRangeOrUnavailable(ber, 0, 7, 99); |
| 65 | mTimingAdvance = inRangeOrUnavailable(ta, 0, 219); |
| 66 | updateLevel(null, null); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Nathan Harold | 9d729c5 | 2018-01-29 19:05:29 -0800 | [diff] [blame] | 69 | /** @hide */ |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 70 | public CellSignalStrengthGsm(android.hardware.radio.V1_0.GsmSignalStrength gsm) { |
| 71 | // Convert from HAL values as part of construction. |
| 72 | this(getRssiDbmFromAsu(gsm.signalStrength), gsm.bitErrorRate, gsm.timingAdvance); |
Nathan Harold | 18f18aa | 2019-01-25 11:12:18 -0800 | [diff] [blame] | 73 | |
| 74 | if (mRssi == CellInfo.UNAVAILABLE) { |
| 75 | setDefaultValues(); |
| 76 | } |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Nathan Harold | 9d729c5 | 2018-01-29 19:05:29 -0800 | [diff] [blame] | 79 | /** @hide */ |
| 80 | public CellSignalStrengthGsm(CellSignalStrengthGsm s) { |
| 81 | copyFrom(s); |
| 82 | } |
| 83 | |
| 84 | /** @hide */ |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 85 | protected void copyFrom(CellSignalStrengthGsm s) { |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 86 | mRssi = s.mRssi; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 87 | mBitErrorRate = s.mBitErrorRate; |
Sanket Padawe | 0c86efd | 2016-01-26 18:42:47 -0800 | [diff] [blame] | 88 | mTimingAdvance = s.mTimingAdvance; |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 89 | mLevel = s.mLevel; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Nathan Harold | 9d729c5 | 2018-01-29 19:05:29 -0800 | [diff] [blame] | 92 | /** @hide */ |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public CellSignalStrengthGsm copy() { |
| 95 | return new CellSignalStrengthGsm(this); |
| 96 | } |
| 97 | |
| 98 | /** @hide */ |
| 99 | @Override |
| 100 | public void setDefaultValues() { |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 101 | mRssi = CellInfo.UNAVAILABLE; |
Nathan Harold | a629ea3 | 2018-10-24 11:35:53 -0700 | [diff] [blame] | 102 | mBitErrorRate = CellInfo.UNAVAILABLE; |
| 103 | mTimingAdvance = CellInfo.UNAVAILABLE; |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 104 | mLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Nathan Harold | 78cf8ac | 2019-04-08 19:21:02 -0700 | [diff] [blame] | 107 | /** {@inheritDoc} */ |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 108 | @Override |
Nathan Harold | 78cf8ac | 2019-04-08 19:21:02 -0700 | [diff] [blame] | 109 | @IntRange(from = SIGNAL_STRENGTH_NONE_OR_UNKNOWN, to = SIGNAL_STRENGTH_GREAT) |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 110 | public int getLevel() { |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 111 | return mLevel; |
| 112 | } |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 113 | |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 114 | /** @hide */ |
| 115 | @Override |
| 116 | public void updateLevel(PersistableBundle cc, ServiceState ss) { |
andychou | 186a87b | 2019-04-09 14:50:31 +0800 | [diff] [blame] | 117 | int[] rssiThresholds; |
| 118 | if (cc == null) { |
| 119 | rssiThresholds = sRssiThresholds; |
| 120 | } else { |
| 121 | rssiThresholds = cc.getIntArray(CarrierConfigManager.KEY_GSM_RSSI_THRESHOLDS_INT_ARRAY); |
| 122 | if (rssiThresholds == null || rssiThresholds.length != NUM_SIGNAL_STRENGTH_THRESHOLDS) { |
| 123 | rssiThresholds = sRssiThresholds; |
| 124 | } |
| 125 | } |
| 126 | int level = NUM_SIGNAL_STRENGTH_THRESHOLDS; |
| 127 | if (mRssi < GSM_RSSI_MIN || mRssi > GSM_RSSI_MAX) { |
| 128 | mLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN; |
| 129 | return; |
| 130 | } |
| 131 | while (level > 0 && mRssi < rssiThresholds[level - 1]) level--; |
| 132 | mLevel = level; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Nathan Harold | 90bc8e3 | 2016-11-10 09:15:44 -0800 | [diff] [blame] | 136 | * Get the GSM timing advance between 0..219 symbols (normally 0..63). |
Nathan Harold | a629ea3 | 2018-10-24 11:35:53 -0700 | [diff] [blame] | 137 | * <p>{@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} is reported when there is no RR |
| 138 | * connection. Refer to 3GPP 45.010 Sec 5.8. |
| 139 | * |
Nathan Harold | 90bc8e3 | 2016-11-10 09:15:44 -0800 | [diff] [blame] | 140 | * @return the current GSM timing advance, if available. |
| 141 | */ |
| 142 | public int getTimingAdvance() { |
| 143 | return mTimingAdvance; |
| 144 | } |
| 145 | |
| 146 | /** |
Nathan Harold | 9cbb0b7 | 2019-07-09 11:01:04 -0700 | [diff] [blame] | 147 | * Get the signal strength as dBm. |
Mingming Cai | e5befcb | 2020-03-24 14:24:27 -0700 | [diff] [blame] | 148 | * |
| 149 | * @return the RSSI of the measured cell. |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 150 | */ |
| 151 | @Override |
| 152 | public int getDbm() { |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 153 | return mRssi; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 157 | * Get the RSSI in ASU. |
| 158 | * |
Nathan Harold | 78cf8ac | 2019-04-08 19:21:02 -0700 | [diff] [blame] | 159 | * Asu is calculated based on 3GPP RSSI. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69 |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 160 | * |
| 161 | * @return RSSI in ASU 0..31, 99, or UNAVAILABLE |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 162 | */ |
| 163 | @Override |
| 164 | public int getAsuLevel() { |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 165 | return getAsuFromRssiDbm(mRssi); |
| 166 | } |
| 167 | |
| 168 | /** |
Nathan Harold | 9cbb0b7 | 2019-07-09 11:01:04 -0700 | [diff] [blame] | 169 | * Return the Received Signal Strength Indicator. |
Nathan Harold | 6ff0c79 | 2019-03-07 17:37:37 -0800 | [diff] [blame] | 170 | * |
| 171 | * @return the RSSI in dBm (-113, -51) or |
| 172 | * {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE}. |
Nathan Harold | 6ff0c79 | 2019-03-07 17:37:37 -0800 | [diff] [blame] | 173 | */ |
| 174 | public int getRssi() { |
| 175 | return mRssi; |
| 176 | } |
| 177 | |
| 178 | /** |
Nathan Harold | 9cbb0b7 | 2019-07-09 11:01:04 -0700 | [diff] [blame] | 179 | * Return the Bit Error Rate. |
Nathan Harold | d261a31 | 2019-01-14 14:30:08 -0800 | [diff] [blame] | 180 | * |
| 181 | * @return the bit error rate (0-7, 99) as defined in TS 27.007 8.5 or |
| 182 | * {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE}. |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 183 | */ |
| 184 | public int getBitErrorRate() { |
| 185 | return mBitErrorRate; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public int hashCode() { |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 190 | return Objects.hash(mRssi, mBitErrorRate, mTimingAdvance); |
| 191 | } |
| 192 | |
| 193 | private static final CellSignalStrengthGsm sInvalid = new CellSignalStrengthGsm(); |
| 194 | |
| 195 | /** @hide */ |
| 196 | @Override |
| 197 | public boolean isValid() { |
| 198 | return !this.equals(sInvalid); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 202 | public boolean equals(Object o) { |
| 203 | if (!(o instanceof CellSignalStrengthGsm)) return false; |
| 204 | CellSignalStrengthGsm s = (CellSignalStrengthGsm) o; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 205 | |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 206 | return mRssi == s.mRssi |
| 207 | && mBitErrorRate == s.mBitErrorRate |
| 208 | && mTimingAdvance == s.mTimingAdvance |
| 209 | && mLevel == s.mLevel; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @return string representation. |
| 214 | */ |
| 215 | @Override |
| 216 | public String toString() { |
| 217 | return "CellSignalStrengthGsm:" |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 218 | + " rssi=" + mRssi |
Sanket Padawe | 0c86efd | 2016-01-26 18:42:47 -0800 | [diff] [blame] | 219 | + " ber=" + mBitErrorRate |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 220 | + " mTa=" + mTimingAdvance |
| 221 | + " mLevel=" + mLevel; |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** Implement the Parcelable interface */ |
| 225 | @Override |
| 226 | public void writeToParcel(Parcel dest, int flags) { |
| 227 | if (DBG) log("writeToParcel(Parcel, int): " + toString()); |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 228 | dest.writeInt(mRssi); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 229 | dest.writeInt(mBitErrorRate); |
Sanket Padawe | 0c86efd | 2016-01-26 18:42:47 -0800 | [diff] [blame] | 230 | dest.writeInt(mTimingAdvance); |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 231 | dest.writeInt(mLevel); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Construct a SignalStrength object from the given parcel |
| 236 | * where the token is already been processed. |
| 237 | */ |
| 238 | private CellSignalStrengthGsm(Parcel in) { |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 239 | mRssi = in.readInt(); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 240 | mBitErrorRate = in.readInt(); |
Sanket Padawe | 0c86efd | 2016-01-26 18:42:47 -0800 | [diff] [blame] | 241 | mTimingAdvance = in.readInt(); |
Nathan Harold | f23153f | 2018-11-19 18:09:40 -0800 | [diff] [blame] | 242 | mLevel = in.readInt(); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 243 | if (DBG) log("CellSignalStrengthGsm(Parcel): " + toString()); |
| 244 | } |
| 245 | |
| 246 | /** Implement the Parcelable interface */ |
| 247 | @Override |
| 248 | public int describeContents() { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /** Implement the Parcelable interface */ |
| 253 | @SuppressWarnings("hiding") |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 254 | public static final @android.annotation.NonNull Parcelable.Creator<CellSignalStrengthGsm> CREATOR = |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 255 | new Parcelable.Creator<CellSignalStrengthGsm>() { |
| 256 | @Override |
| 257 | public CellSignalStrengthGsm createFromParcel(Parcel in) { |
Wink Saville | c6e4917a | 2012-09-21 13:54:05 -0700 | [diff] [blame] | 258 | return new CellSignalStrengthGsm(in); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public CellSignalStrengthGsm[] newArray(int size) { |
| 263 | return new CellSignalStrengthGsm[size]; |
| 264 | } |
| 265 | }; |
| 266 | |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 267 | /** |
| 268 | * log |
| 269 | */ |
| 270 | private static void log(String s) { |
Wink Saville | 599a90c | 2012-11-27 12:29:13 -0800 | [diff] [blame] | 271 | Rlog.w(LOG_TAG, s); |
Wink Saville | b208a24 | 2012-07-25 14:08:09 -0700 | [diff] [blame] | 272 | } |
| 273 | } |