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(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
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(CellSignalStrengthGsm s)70     public CellSignalStrengthGsm(CellSignalStrengthGsm s) {
71         copyFrom(s);
72     }
73 
74     /** @hide */
copyFrom(CellSignalStrengthGsm s)75     protected void copyFrom(CellSignalStrengthGsm s) {
76         mRssi = s.mRssi;
77         mBitErrorRate = s.mBitErrorRate;
78         mTimingAdvance = s.mTimingAdvance;
79         mLevel = s.mLevel;
80     }
81 
82     /** @hide */
83     @Override
copy()84     public CellSignalStrengthGsm copy() {
85         return new CellSignalStrengthGsm(this);
86     }
87 
88     /** @hide */
89     @Override
setDefaultValues()90     public void setDefaultValues() {
91         mRssi = CellInfo.UNAVAILABLE;
92         mBitErrorRate = CellInfo.UNAVAILABLE;
93         mTimingAdvance = CellInfo.UNAVAILABLE;
94         mLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
95     }
96 
97     /** {@inheritDoc} */
98     @Override
99     @IntRange(from = SIGNAL_STRENGTH_NONE_OR_UNKNOWN, to = SIGNAL_STRENGTH_GREAT)
getLevel()100     public int getLevel() {
101         return mLevel;
102     }
103 
104     /** @hide */
105     @Override
updateLevel(PersistableBundle cc, ServiceState ss)106     public void updateLevel(PersistableBundle cc, ServiceState ss) {
107         int[] rssiThresholds;
108         if (cc == null) {
109             rssiThresholds = sRssiThresholds;
110         } else {
111             rssiThresholds = cc.getIntArray(CarrierConfigManager.KEY_GSM_RSSI_THRESHOLDS_INT_ARRAY);
112             if (rssiThresholds == null || rssiThresholds.length != NUM_SIGNAL_STRENGTH_THRESHOLDS) {
113                 rssiThresholds = sRssiThresholds;
114             }
115         }
116         int level = NUM_SIGNAL_STRENGTH_THRESHOLDS;
117         if (mRssi < GSM_RSSI_MIN || mRssi > GSM_RSSI_MAX) {
118             mLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
119             return;
120         }
121         while (level > 0 && mRssi < rssiThresholds[level - 1]) level--;
122         mLevel = level;
123     }
124 
125     /**
126      * Get the GSM timing advance between 0..219 symbols (normally 0..63).
127      * <p>{@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} is reported when there is no RR
128      * connection. Refer to 3GPP 45.010 Sec 5.8.
129      *
130      * @return the current GSM timing advance, if available.
131      */
getTimingAdvance()132     public int getTimingAdvance() {
133         return mTimingAdvance;
134     }
135 
136     /**
137      * Get the signal strength as dBm.
138      *
139      * @return the RSSI of the measured cell.
140      */
141     @Override
getDbm()142     public int getDbm() {
143         return mRssi;
144     }
145 
146     /**
147      * Get the RSSI in ASU.
148      *
149      * Asu is calculated based on 3GPP RSSI. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69
150      *
151      * @return RSSI in ASU 0..31, 99, or
152      *         {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE}.
153      */
154     @Override
getAsuLevel()155     public int getAsuLevel() {
156         return getAsuFromRssiDbm(mRssi);
157     }
158 
159     /**
160      * Return the Received Signal Strength Indicator.
161      *
162      * @return the RSSI in dBm (-113, -51) or
163      *         {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE}.
164      */
getRssi()165     public int getRssi() {
166         return mRssi;
167     }
168 
169     /**
170      * Return the Bit Error Rate.
171      *
172      * @return the bit error rate (0-7, 99) as defined in TS 27.007 8.5 or
173      *         {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE}.
174      */
getBitErrorRate()175     public int getBitErrorRate() {
176         return mBitErrorRate;
177     }
178 
179     @Override
hashCode()180     public int hashCode() {
181         return Objects.hash(mRssi, mBitErrorRate, mTimingAdvance);
182     }
183 
184     private static final CellSignalStrengthGsm sInvalid = new CellSignalStrengthGsm();
185 
186     /** @hide */
187     @Override
isValid()188     public boolean isValid() {
189         return !this.equals(sInvalid);
190     }
191 
192     @Override
equals(Object o)193     public boolean equals(Object o) {
194         if (!(o instanceof CellSignalStrengthGsm)) return false;
195         CellSignalStrengthGsm s = (CellSignalStrengthGsm) o;
196 
197         return mRssi == s.mRssi
198                 && mBitErrorRate == s.mBitErrorRate
199                 && mTimingAdvance == s.mTimingAdvance
200                 && mLevel == s.mLevel;
201     }
202 
203     /**
204      * @return string representation.
205      */
206     @Override
toString()207     public String toString() {
208         return "CellSignalStrengthGsm:"
209                 + " rssi=" + mRssi
210                 + " ber=" + mBitErrorRate
211                 + " mTa=" + mTimingAdvance
212                 + " mLevel=" + mLevel;
213     }
214 
215     /** Implement the Parcelable interface */
216     @Override
writeToParcel(Parcel dest, int flags)217     public void writeToParcel(Parcel dest, int flags) {
218         if (DBG) log("writeToParcel(Parcel, int): " + toString());
219         dest.writeInt(mRssi);
220         dest.writeInt(mBitErrorRate);
221         dest.writeInt(mTimingAdvance);
222         dest.writeInt(mLevel);
223     }
224 
225     /**
226      * Construct a SignalStrength object from the given parcel
227      * where the token is already been processed.
228      */
CellSignalStrengthGsm(Parcel in)229     private CellSignalStrengthGsm(Parcel in) {
230         mRssi = in.readInt();
231         mBitErrorRate = in.readInt();
232         mTimingAdvance = in.readInt();
233         mLevel = in.readInt();
234         if (DBG) log("CellSignalStrengthGsm(Parcel): " + toString());
235     }
236 
237     /** Implement the Parcelable interface */
238     @Override
describeContents()239     public int describeContents() {
240         return 0;
241     }
242 
243     /** Implement the Parcelable interface */
244     @SuppressWarnings("hiding")
245     public static final @android.annotation.NonNull Parcelable.Creator<CellSignalStrengthGsm> CREATOR =
246             new Parcelable.Creator<CellSignalStrengthGsm>() {
247         @Override
248         public CellSignalStrengthGsm createFromParcel(Parcel in) {
249             return new CellSignalStrengthGsm(in);
250         }
251 
252         @Override
253         public CellSignalStrengthGsm[] newArray(int size) {
254             return new CellSignalStrengthGsm[size];
255         }
256     };
257 
258     /**
259      * log
260      */
log(String s)261     private static void log(String s) {
262         Rlog.w(LOG_TAG, s);
263     }
264 }
265