blob: 9d55f109f751b2384f390e1894839c747fbaa9b0 [file] [log] [blame]
Wink Savilleb208a242012-07-25 14:08:09 -07001/*
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
17package android.telephony;
18
Nathan Harold78cf8ac2019-04-08 19:21:02 -070019import android.annotation.IntRange;
Artur Satayev74cb7192019-12-10 17:47:56 +000020import android.compat.annotation.UnsupportedAppUsage;
David Brazdil2959b5c2019-05-15 10:49:22 +010021import android.os.Build;
Wink Savilleb208a242012-07-25 14:08:09 -070022import android.os.Parcel;
23import android.os.Parcelable;
Nathan Haroldf23153f2018-11-19 18:09:40 -080024import android.os.PersistableBundle;
Wink Savilleb208a242012-07-25 14:08:09 -070025
Meng Wang1dbea2f2020-01-30 12:25:08 -080026import com.android.telephony.Rlog;
27
Nathan Harold017e7f92018-01-29 17:17:10 -080028import java.util.Objects;
29
Wink Savilleb208a242012-07-25 14:08:09 -070030/**
Wink Savillee3a9cbc2013-04-17 16:40:17 -070031 * GSM signal strength related information.
Wink Savilleb208a242012-07-25 14:08:09 -070032 */
Wink Savillec6e4917a2012-09-21 13:54:05 -070033public final class CellSignalStrengthGsm extends CellSignalStrength implements Parcelable {
Wink Savilleb208a242012-07-25 14:08:09 -070034
35 private static final String LOG_TAG = "CellSignalStrengthGsm";
36 private static final boolean DBG = false;
37
Nathan Haroldf23153f2018-11-19 18:09:40 -080038 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;
andychou186a87b2019-04-09 14:50:31 +080043 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 Savilleb208a242012-07-25 14:08:09 -070047
Nathan Haroldf23153f2018-11-19 18:09:40 -080048 private int mRssi; // in dBm [-113, -51] or UNAVAILABLE
Mathew Inwooda8382062018-08-16 17:01:12 +010049 @UnsupportedAppUsage
Nathan Haroldf23153f2018-11-19 18:09:40 -080050 private int mBitErrorRate; // bit error rate (0-7, 99) TS 27.007 8.5 or UNAVAILABLE
David Brazdil2959b5c2019-05-15 10:49:22 +010051 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Nathan Harolda629ea32018-10-24 11:35:53 -070052 private int mTimingAdvance; // range from 0-219 or CellInfo.UNAVAILABLE if unknown
Nathan Haroldf23153f2018-11-19 18:09:40 -080053 private int mLevel;
Wink Savilleb208a242012-07-25 14:08:09 -070054
Nathan Harold9d729c52018-01-29 19:05:29 -080055 /** @hide */
Mathew Inwooda8382062018-08-16 17:01:12 +010056 @UnsupportedAppUsage
Wink Savilleb208a242012-07-25 14:08:09 -070057 public CellSignalStrengthGsm() {
58 setDefaultValues();
59 }
60
Nathan Harold9d729c52018-01-29 19:05:29 -080061 /** @hide */
Nathan Haroldf23153f2018-11-19 18:09:40 -080062 public CellSignalStrengthGsm(int rssi, int ber, int ta) {
andychou186a87b2019-04-09 14:50:31 +080063 mRssi = inRangeOrUnavailable(rssi, GSM_RSSI_MIN, GSM_RSSI_MAX);
Nathan Haroldf23153f2018-11-19 18:09:40 -080064 mBitErrorRate = inRangeOrUnavailable(ber, 0, 7, 99);
65 mTimingAdvance = inRangeOrUnavailable(ta, 0, 219);
66 updateLevel(null, null);
Wink Savilleb208a242012-07-25 14:08:09 -070067 }
68
Nathan Harold9d729c52018-01-29 19:05:29 -080069 /** @hide */
Nathan Haroldf23153f2018-11-19 18:09:40 -080070 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 Harold18f18aa2019-01-25 11:12:18 -080073
74 if (mRssi == CellInfo.UNAVAILABLE) {
75 setDefaultValues();
76 }
Wink Savilleb208a242012-07-25 14:08:09 -070077 }
78
Nathan Harold9d729c52018-01-29 19:05:29 -080079 /** @hide */
80 public CellSignalStrengthGsm(CellSignalStrengthGsm s) {
81 copyFrom(s);
82 }
83
84 /** @hide */
Wink Savilleb208a242012-07-25 14:08:09 -070085 protected void copyFrom(CellSignalStrengthGsm s) {
Nathan Haroldf23153f2018-11-19 18:09:40 -080086 mRssi = s.mRssi;
Wink Savilleb208a242012-07-25 14:08:09 -070087 mBitErrorRate = s.mBitErrorRate;
Sanket Padawe0c86efd2016-01-26 18:42:47 -080088 mTimingAdvance = s.mTimingAdvance;
Nathan Haroldf23153f2018-11-19 18:09:40 -080089 mLevel = s.mLevel;
Wink Savilleb208a242012-07-25 14:08:09 -070090 }
91
Nathan Harold9d729c52018-01-29 19:05:29 -080092 /** @hide */
Wink Savilleb208a242012-07-25 14:08:09 -070093 @Override
94 public CellSignalStrengthGsm copy() {
95 return new CellSignalStrengthGsm(this);
96 }
97
98 /** @hide */
99 @Override
100 public void setDefaultValues() {
Nathan Haroldf23153f2018-11-19 18:09:40 -0800101 mRssi = CellInfo.UNAVAILABLE;
Nathan Harolda629ea32018-10-24 11:35:53 -0700102 mBitErrorRate = CellInfo.UNAVAILABLE;
103 mTimingAdvance = CellInfo.UNAVAILABLE;
Nathan Haroldf23153f2018-11-19 18:09:40 -0800104 mLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Wink Savilleb208a242012-07-25 14:08:09 -0700105 }
106
Nathan Harold78cf8ac2019-04-08 19:21:02 -0700107 /** {@inheritDoc} */
Wink Savilleb208a242012-07-25 14:08:09 -0700108 @Override
Nathan Harold78cf8ac2019-04-08 19:21:02 -0700109 @IntRange(from = SIGNAL_STRENGTH_NONE_OR_UNKNOWN, to = SIGNAL_STRENGTH_GREAT)
Wink Savilleb208a242012-07-25 14:08:09 -0700110 public int getLevel() {
Nathan Haroldf23153f2018-11-19 18:09:40 -0800111 return mLevel;
112 }
Wink Savilleb208a242012-07-25 14:08:09 -0700113
Nathan Haroldf23153f2018-11-19 18:09:40 -0800114 /** @hide */
115 @Override
116 public void updateLevel(PersistableBundle cc, ServiceState ss) {
andychou186a87b2019-04-09 14:50:31 +0800117 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 Savilleb208a242012-07-25 14:08:09 -0700133 }
134
135 /**
Nathan Harold90bc8e32016-11-10 09:15:44 -0800136 * Get the GSM timing advance between 0..219 symbols (normally 0..63).
Nathan Harolda629ea32018-10-24 11:35:53 -0700137 * <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 Harold90bc8e32016-11-10 09:15:44 -0800140 * @return the current GSM timing advance, if available.
141 */
142 public int getTimingAdvance() {
143 return mTimingAdvance;
144 }
145
146 /**
Nathan Harold9cbb0b72019-07-09 11:01:04 -0700147 * Get the signal strength as dBm.
Mingming Caie5befcb2020-03-24 14:24:27 -0700148 *
149 * @return the RSSI of the measured cell.
Wink Savilleb208a242012-07-25 14:08:09 -0700150 */
151 @Override
152 public int getDbm() {
Nathan Haroldf23153f2018-11-19 18:09:40 -0800153 return mRssi;
Wink Savilleb208a242012-07-25 14:08:09 -0700154 }
155
156 /**
Nathan Haroldf23153f2018-11-19 18:09:40 -0800157 * Get the RSSI in ASU.
158 *
Nathan Harold78cf8ac2019-04-08 19:21:02 -0700159 * Asu is calculated based on 3GPP RSSI. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69
Nathan Haroldf23153f2018-11-19 18:09:40 -0800160 *
161 * @return RSSI in ASU 0..31, 99, or UNAVAILABLE
Wink Savilleb208a242012-07-25 14:08:09 -0700162 */
163 @Override
164 public int getAsuLevel() {
Nathan Haroldf23153f2018-11-19 18:09:40 -0800165 return getAsuFromRssiDbm(mRssi);
166 }
167
168 /**
Nathan Harold9cbb0b72019-07-09 11:01:04 -0700169 * Return the Received Signal Strength Indicator.
Nathan Harold6ff0c792019-03-07 17:37:37 -0800170 *
171 * @return the RSSI in dBm (-113, -51) or
172 * {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE}.
Nathan Harold6ff0c792019-03-07 17:37:37 -0800173 */
174 public int getRssi() {
175 return mRssi;
176 }
177
178 /**
Nathan Harold9cbb0b72019-07-09 11:01:04 -0700179 * Return the Bit Error Rate.
Nathan Haroldd261a312019-01-14 14:30:08 -0800180 *
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 Haroldf23153f2018-11-19 18:09:40 -0800183 */
184 public int getBitErrorRate() {
185 return mBitErrorRate;
Wink Savilleb208a242012-07-25 14:08:09 -0700186 }
187
188 @Override
189 public int hashCode() {
Nathan Haroldf23153f2018-11-19 18:09:40 -0800190 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 Savilleb208a242012-07-25 14:08:09 -0700199 }
200
201 @Override
Nathan Haroldf23153f2018-11-19 18:09:40 -0800202 public boolean equals(Object o) {
203 if (!(o instanceof CellSignalStrengthGsm)) return false;
204 CellSignalStrengthGsm s = (CellSignalStrengthGsm) o;
Wink Savilleb208a242012-07-25 14:08:09 -0700205
Nathan Haroldf23153f2018-11-19 18:09:40 -0800206 return mRssi == s.mRssi
207 && mBitErrorRate == s.mBitErrorRate
208 && mTimingAdvance == s.mTimingAdvance
209 && mLevel == s.mLevel;
Wink Savilleb208a242012-07-25 14:08:09 -0700210 }
211
212 /**
213 * @return string representation.
214 */
215 @Override
216 public String toString() {
217 return "CellSignalStrengthGsm:"
Nathan Haroldf23153f2018-11-19 18:09:40 -0800218 + " rssi=" + mRssi
Sanket Padawe0c86efd2016-01-26 18:42:47 -0800219 + " ber=" + mBitErrorRate
Nathan Haroldf23153f2018-11-19 18:09:40 -0800220 + " mTa=" + mTimingAdvance
221 + " mLevel=" + mLevel;
Wink Savilleb208a242012-07-25 14:08:09 -0700222 }
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 Haroldf23153f2018-11-19 18:09:40 -0800228 dest.writeInt(mRssi);
Wink Savilleb208a242012-07-25 14:08:09 -0700229 dest.writeInt(mBitErrorRate);
Sanket Padawe0c86efd2016-01-26 18:42:47 -0800230 dest.writeInt(mTimingAdvance);
Nathan Haroldf23153f2018-11-19 18:09:40 -0800231 dest.writeInt(mLevel);
Wink Savilleb208a242012-07-25 14:08:09 -0700232 }
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 Haroldf23153f2018-11-19 18:09:40 -0800239 mRssi = in.readInt();
Wink Savilleb208a242012-07-25 14:08:09 -0700240 mBitErrorRate = in.readInt();
Sanket Padawe0c86efd2016-01-26 18:42:47 -0800241 mTimingAdvance = in.readInt();
Nathan Haroldf23153f2018-11-19 18:09:40 -0800242 mLevel = in.readInt();
Wink Savilleb208a242012-07-25 14:08:09 -0700243 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 Sharkey9e8f83d2019-02-28 12:06:45 -0700254 public static final @android.annotation.NonNull Parcelable.Creator<CellSignalStrengthGsm> CREATOR =
Wink Savilleb208a242012-07-25 14:08:09 -0700255 new Parcelable.Creator<CellSignalStrengthGsm>() {
256 @Override
257 public CellSignalStrengthGsm createFromParcel(Parcel in) {
Wink Savillec6e4917a2012-09-21 13:54:05 -0700258 return new CellSignalStrengthGsm(in);
Wink Savilleb208a242012-07-25 14:08:09 -0700259 }
260
261 @Override
262 public CellSignalStrengthGsm[] newArray(int size) {
263 return new CellSignalStrengthGsm[size];
264 }
265 };
266
Wink Savilleb208a242012-07-25 14:08:09 -0700267 /**
268 * log
269 */
270 private static void log(String s) {
Wink Saville599a90c2012-11-27 12:29:13 -0800271 Rlog.w(LOG_TAG, s);
Wink Savilleb208a242012-07-25 14:08:09 -0700272 }
273}