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 
19 import android.annotation.IntRange;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Build;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.os.PersistableBundle;
25 
26 import com.android.telephony.Rlog;
27 
28 import java.util.Objects;
29 
30 /**
31  * GSM signal strength related information.
32  */
33 public final class CellSignalStrengthGsm extends CellSignalStrength implements Parcelable {
34 
35     private static final String LOG_TAG = "CellSignalStrengthGsm";
36     private static final boolean DBG = false;
37 
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;
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};
47 
48     private int mRssi; // in dBm [-113, -51] or UNAVAILABLE
49     @UnsupportedAppUsage
50     private int mBitErrorRate; // bit error rate (0-7, 99) TS 27.007 8.5 or UNAVAILABLE
51     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
52     private int mTimingAdvance; // range from 0-219 or CellInfo.UNAVAILABLE if unknown
53     private int mLevel;
54 
55     /** @hide */
56     @UnsupportedAppUsage
CellSignalStrengthGsm()57     public CellSignalStrengthGsm() {
58         setDefaultValues();
59     }
60 
61     /** @hide */
CellSignalStrengthGsm(int rssi, int ber, int ta)62     public CellSignalStrengthGsm(int rssi, int ber, int ta) {
63         mRssi = inRangeOrUnavailable(rssi, GSM_RSSI_MIN, GSM_RSSI_MAX);
64         mBitErrorRate = inRangeOrUnavailable(ber, 0, 7, 99);
65         mTimingAdvance = inRangeOrUnavailable(ta, 0, 219);
66         updateLevel(null, null);
67     }
68 
69     /** @hide */
CellSignalStrengthGsm(android.hardware.radio.V1_0.GsmSignalStrength gsm)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);
73 
74         if (mRssi == CellInfo.UNAVAILABLE) {
75             setDefaultValues();
76         }
77     }
78 
79     /** @hide */
CellSignalStrengthGsm(CellSignalStrengthGsm s)80     public CellSignalStrengthGsm(CellSignalStrengthGsm s) {
81         copyFrom(s);
82     }
83 
84     /** @hide */
copyFrom(CellSignalStrengthGsm s)85     protected void copyFrom(CellSignalStrengthGsm s) {
86         mRssi = s.mRssi;
87         mBitErrorRate = s.mBitErrorRate;
88         mTimingAdvance = s.mTimingAdvance;
89         mLevel = s.mLevel;
90     }
91 
92     /** @hide */
93     @Override
copy()94     public CellSignalStrengthGsm copy() {
95         return new CellSignalStrengthGsm(this);
96     }
97 
98     /** @hide */
99     @Override
setDefaultValues()100     public void setDefaultValues() {
101         mRssi = CellInfo.UNAVAILABLE;
102         mBitErrorRate = CellInfo.UNAVAILABLE;
103         mTimingAdvance = CellInfo.UNAVAILABLE;
104         mLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     @IntRange(from = SIGNAL_STRENGTH_NONE_OR_UNKNOWN, to = SIGNAL_STRENGTH_GREAT)
getLevel()110     public int getLevel() {
111         return mLevel;
112     }
113 
114     /** @hide */
115     @Override
updateLevel(PersistableBundle cc, ServiceState ss)116     public void updateLevel(PersistableBundle cc, ServiceState ss) {
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;
133     }
134 
135     /**
136      * Get the GSM timing advance between 0..219 symbols (normally 0..63).
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      *
140      * @return the current GSM timing advance, if available.
141      */
getTimingAdvance()142     public int getTimingAdvance() {
143         return mTimingAdvance;
144     }
145 
146     /**
147      * Get the signal strength as dBm.
148      *
149      * @return the RSSI of the measured cell.
150      */
151     @Override
getDbm()152     public int getDbm() {
153         return mRssi;
154     }
155 
156     /**
157      * Get the RSSI in ASU.
158      *
159      * Asu is calculated based on 3GPP RSSI. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69
160      *
161      * @return RSSI in ASU 0..31, 99, or UNAVAILABLE
162      */
163     @Override
getAsuLevel()164     public int getAsuLevel() {
165         return getAsuFromRssiDbm(mRssi);
166     }
167 
168     /**
169      * Return the Received Signal Strength Indicator.
170      *
171      * @return the RSSI in dBm (-113, -51) or
172      *         {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE}.
173      */
getRssi()174     public int getRssi() {
175         return mRssi;
176     }
177 
178     /**
179      * Return the Bit Error Rate.
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}.
183      */
getBitErrorRate()184     public int getBitErrorRate() {
185         return mBitErrorRate;
186     }
187 
188     @Override
hashCode()189     public int hashCode() {
190         return Objects.hash(mRssi, mBitErrorRate, mTimingAdvance);
191     }
192 
193     private static final CellSignalStrengthGsm sInvalid = new CellSignalStrengthGsm();
194 
195     /** @hide */
196     @Override
isValid()197     public boolean isValid() {
198         return !this.equals(sInvalid);
199     }
200 
201     @Override
equals(Object o)202     public boolean equals(Object o) {
203         if (!(o instanceof CellSignalStrengthGsm)) return false;
204         CellSignalStrengthGsm s = (CellSignalStrengthGsm) o;
205 
206         return mRssi == s.mRssi
207                 && mBitErrorRate == s.mBitErrorRate
208                 && mTimingAdvance == s.mTimingAdvance
209                 && mLevel == s.mLevel;
210     }
211 
212     /**
213      * @return string representation.
214      */
215     @Override
toString()216     public String toString() {
217         return "CellSignalStrengthGsm:"
218                 + " rssi=" + mRssi
219                 + " ber=" + mBitErrorRate
220                 + " mTa=" + mTimingAdvance
221                 + " mLevel=" + mLevel;
222     }
223 
224     /** Implement the Parcelable interface */
225     @Override
writeToParcel(Parcel dest, int flags)226     public void writeToParcel(Parcel dest, int flags) {
227         if (DBG) log("writeToParcel(Parcel, int): " + toString());
228         dest.writeInt(mRssi);
229         dest.writeInt(mBitErrorRate);
230         dest.writeInt(mTimingAdvance);
231         dest.writeInt(mLevel);
232     }
233 
234     /**
235      * Construct a SignalStrength object from the given parcel
236      * where the token is already been processed.
237      */
CellSignalStrengthGsm(Parcel in)238     private CellSignalStrengthGsm(Parcel in) {
239         mRssi = in.readInt();
240         mBitErrorRate = in.readInt();
241         mTimingAdvance = in.readInt();
242         mLevel = in.readInt();
243         if (DBG) log("CellSignalStrengthGsm(Parcel): " + toString());
244     }
245 
246     /** Implement the Parcelable interface */
247     @Override
describeContents()248     public int describeContents() {
249         return 0;
250     }
251 
252     /** Implement the Parcelable interface */
253     @SuppressWarnings("hiding")
254     public static final @android.annotation.NonNull Parcelable.Creator<CellSignalStrengthGsm> CREATOR =
255             new Parcelable.Creator<CellSignalStrengthGsm>() {
256         @Override
257         public CellSignalStrengthGsm createFromParcel(Parcel in) {
258             return new CellSignalStrengthGsm(in);
259         }
260 
261         @Override
262         public CellSignalStrengthGsm[] newArray(int size) {
263             return new CellSignalStrengthGsm[size];
264         }
265     };
266 
267     /**
268      * log
269      */
log(String s)270     private static void log(String s) {
271         Rlog.w(LOG_TAG, s);
272     }
273 }
274