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.os.Parcel;
20 import android.os.Parcelable;
21 import android.telephony.Rlog;
22 
23 import java.util.Objects;
24 
25 /**
26  * Wcdma signal strength related information.
27  */
28 public final class CellSignalStrengthWcdma extends CellSignalStrength implements Parcelable {
29 
30     private static final String LOG_TAG = "CellSignalStrengthWcdma";
31     private static final boolean DBG = false;
32 
33     private static final int WCDMA_SIGNAL_STRENGTH_GREAT = 12;
34     private static final int WCDMA_SIGNAL_STRENGTH_GOOD = 8;
35     private static final int WCDMA_SIGNAL_STRENGTH_MODERATE = 5;
36 
37     private int mSignalStrength; // in ASU; Valid values are (0-31, 99) as defined in TS 27.007 8.5
38     private int mBitErrorRate; // bit error rate (0-7, 99) as defined in TS 27.007 8.5
39 
40     /** @hide */
CellSignalStrengthWcdma()41     public CellSignalStrengthWcdma() {
42         setDefaultValues();
43     }
44 
45     /** @hide */
CellSignalStrengthWcdma(int ss, int ber)46     public CellSignalStrengthWcdma(int ss, int ber) {
47         mSignalStrength = ss;
48         mBitErrorRate = ber;
49     }
50 
51     /** @hide */
CellSignalStrengthWcdma(CellSignalStrengthWcdma s)52     public CellSignalStrengthWcdma(CellSignalStrengthWcdma s) {
53         copyFrom(s);
54     }
55 
56     /** @hide */
copyFrom(CellSignalStrengthWcdma s)57     protected void copyFrom(CellSignalStrengthWcdma s) {
58         mSignalStrength = s.mSignalStrength;
59         mBitErrorRate = s.mBitErrorRate;
60     }
61 
62     /** @hide */
63     @Override
copy()64     public CellSignalStrengthWcdma copy() {
65         return new CellSignalStrengthWcdma(this);
66     }
67 
68     /** @hide */
69     @Override
setDefaultValues()70     public void setDefaultValues() {
71         mSignalStrength = Integer.MAX_VALUE;
72         mBitErrorRate = Integer.MAX_VALUE;
73     }
74 
75     /**
76      * Get signal level as an int from 0..4
77      */
78     @Override
getLevel()79     public int getLevel() {
80         int level;
81 
82         // ASU ranges from 0 to 31 - TS 27.007 Sec 8.5
83         // asu = 0 (-113dB or less) is very weak
84         // signal, its better to show 0 bars to the user in such cases.
85         // asu = 99 is a special case, where the signal strength is unknown.
86         int asu = mSignalStrength;
87         if (asu <= 2 || asu == 99) level = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
88         else if (asu >= WCDMA_SIGNAL_STRENGTH_GREAT) level = SIGNAL_STRENGTH_GREAT;
89         else if (asu >= WCDMA_SIGNAL_STRENGTH_GOOD)  level = SIGNAL_STRENGTH_GOOD;
90         else if (asu >= WCDMA_SIGNAL_STRENGTH_MODERATE)  level = SIGNAL_STRENGTH_MODERATE;
91         else level = SIGNAL_STRENGTH_POOR;
92         if (DBG) log("getLevel=" + level);
93         return level;
94     }
95 
96     /**
97      * Get the signal strength as dBm
98      */
99     @Override
getDbm()100     public int getDbm() {
101         int dBm;
102 
103         int level = mSignalStrength;
104         int asu = (level == 99 ? Integer.MAX_VALUE : level);
105         if (asu != Integer.MAX_VALUE) {
106             dBm = -113 + (2 * asu);
107         } else {
108             dBm = Integer.MAX_VALUE;
109         }
110         if (DBG) log("getDbm=" + dBm);
111         return dBm;
112     }
113 
114     /**
115      * Get the signal level as an asu value between 0..31, 99 is unknown
116      * Asu is calculated based on 3GPP RSRP. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69
117      */
118     @Override
getAsuLevel()119     public int getAsuLevel() {
120         // ASU ranges from 0 to 31 - TS 27.007 Sec 8.5
121         // asu = 0 (-113dB or less) is very weak
122         // signal, its better to show 0 bars to the user in such cases.
123         // asu = 99 is a special case, where the signal strength is unknown.
124         int level = mSignalStrength;
125         if (DBG) log("getAsuLevel=" + level);
126         return level;
127     }
128 
129     @Override
hashCode()130     public int hashCode() {
131         return Objects.hash(mSignalStrength, mBitErrorRate);
132     }
133 
134     @Override
equals(Object o)135     public boolean equals (Object o) {
136         CellSignalStrengthWcdma s;
137 
138         try {
139             s = (CellSignalStrengthWcdma) o;
140         } catch (ClassCastException ex) {
141             return false;
142         }
143 
144         if (o == null) {
145             return false;
146         }
147 
148         return mSignalStrength == s.mSignalStrength && mBitErrorRate == s.mBitErrorRate;
149     }
150 
151     /**
152      * @return string representation.
153      */
154     @Override
toString()155     public String toString() {
156         return "CellSignalStrengthWcdma:"
157                 + " ss=" + mSignalStrength
158                 + " ber=" + mBitErrorRate;
159     }
160 
161     /** Implement the Parcelable interface */
162     @Override
writeToParcel(Parcel dest, int flags)163     public void writeToParcel(Parcel dest, int flags) {
164         if (DBG) log("writeToParcel(Parcel, int): " + toString());
165         dest.writeInt(mSignalStrength);
166         dest.writeInt(mBitErrorRate);
167     }
168 
169     /**
170      * Construct a SignalStrength object from the given parcel
171      * where the token is already been processed.
172      */
CellSignalStrengthWcdma(Parcel in)173     private CellSignalStrengthWcdma(Parcel in) {
174         mSignalStrength = in.readInt();
175         mBitErrorRate = in.readInt();
176         if (DBG) log("CellSignalStrengthWcdma(Parcel): " + toString());
177     }
178 
179     /** Implement the Parcelable interface */
180     @Override
describeContents()181     public int describeContents() {
182         return 0;
183     }
184 
185     /** Implement the Parcelable interface */
186     @SuppressWarnings("hiding")
187     public static final Parcelable.Creator<CellSignalStrengthWcdma> CREATOR =
188             new Parcelable.Creator<CellSignalStrengthWcdma>() {
189         @Override
190         public CellSignalStrengthWcdma createFromParcel(Parcel in) {
191             return new CellSignalStrengthWcdma(in);
192         }
193 
194         @Override
195         public CellSignalStrengthWcdma[] newArray(int size) {
196             return new CellSignalStrengthWcdma[size];
197         }
198     };
199 
200     /**
201      * log
202      */
log(String s)203     private static void log(String s) {
204         Rlog.w(LOG_TAG, s);
205     }
206 }
207