CellSignalStrengthLte.java revision 017e7f90eea67b0ecd002d1ab193f60238ad0555
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
17package android.telephony;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.telephony.Rlog;
22
23import java.util.Objects;
24
25/**
26 * LTE signal strength related information.
27 */
28public final class CellSignalStrengthLte extends CellSignalStrength implements Parcelable {
29
30    private static final String LOG_TAG = "CellSignalStrengthLte";
31    private static final boolean DBG = false;
32
33    private int mSignalStrength;
34    private int mRsrp;
35    private int mRsrq;
36    private int mRssnr;
37    private int mCqi;
38    private int mTimingAdvance;
39
40    /**
41     * Empty constructor
42     *
43     * @hide
44     */
45    public CellSignalStrengthLte() {
46        setDefaultValues();
47    }
48
49    /**
50     * Constructor
51     *
52     * @hide
53     */
54    public CellSignalStrengthLte(int signalStrength, int rsrp, int rsrq, int rssnr, int cqi,
55            int timingAdvance) {
56        initialize(signalStrength, rsrp, rsrq, rssnr, cqi, timingAdvance);
57    }
58
59    /**
60     * Copy constructors
61     *
62     * @param s Source SignalStrength
63     *
64     * @hide
65     */
66    public CellSignalStrengthLte(CellSignalStrengthLte s) {
67        copyFrom(s);
68    }
69
70    /**
71     * Initialize all the values
72     *
73     * @param lteSignalStrength
74     * @param rsrp
75     * @param rsrq
76     * @param rssnr
77     * @param cqi
78     *
79     * @hide
80     */
81    public void initialize(int lteSignalStrength, int rsrp, int rsrq, int rssnr, int cqi,
82            int timingAdvance) {
83        mSignalStrength = lteSignalStrength;
84        mRsrp = rsrp;
85        mRsrq = rsrq;
86        mRssnr = rssnr;
87        mCqi = cqi;
88        mTimingAdvance = timingAdvance;
89    }
90
91    /**
92     * Initialize from the SignalStrength structure.
93     *
94     * @param ss
95     *
96     * @hide
97     */
98    public void initialize(SignalStrength ss, int timingAdvance) {
99        mSignalStrength = ss.getLteSignalStrength();
100        mRsrp = ss.getLteRsrp();
101        mRsrq = ss.getLteRsrq();
102        mRssnr = ss.getLteRssnr();
103        mCqi = ss.getLteCqi();
104        mTimingAdvance = timingAdvance;
105    }
106
107    /**
108     * @hide
109     */
110    protected void copyFrom(CellSignalStrengthLte s) {
111        mSignalStrength = s.mSignalStrength;
112        mRsrp = s.mRsrp;
113        mRsrq = s.mRsrq;
114        mRssnr = s.mRssnr;
115        mCqi = s.mCqi;
116        mTimingAdvance = s.mTimingAdvance;
117    }
118
119    /**
120     * @hide
121     */
122    @Override
123    public CellSignalStrengthLte copy() {
124        return new CellSignalStrengthLte(this);
125    }
126
127    /** @hide */
128    @Override
129    public void setDefaultValues() {
130        mSignalStrength = Integer.MAX_VALUE;
131        mRsrp = Integer.MAX_VALUE;
132        mRsrq = Integer.MAX_VALUE;
133        mRssnr = Integer.MAX_VALUE;
134        mCqi = Integer.MAX_VALUE;
135        mTimingAdvance = Integer.MAX_VALUE;
136    }
137
138    /**
139     * Get signal level as an int from 0..4
140     */
141    @Override
142    public int getLevel() {
143        int levelRsrp = 0;
144        int levelRssnr = 0;
145
146        if (mRsrp == Integer.MAX_VALUE) levelRsrp = 0;
147        else if (mRsrp >= -95) levelRsrp = SIGNAL_STRENGTH_GREAT;
148        else if (mRsrp >= -105) levelRsrp = SIGNAL_STRENGTH_GOOD;
149        else if (mRsrp >= -115) levelRsrp = SIGNAL_STRENGTH_MODERATE;
150        else levelRsrp = SIGNAL_STRENGTH_POOR;
151
152        // See RIL_LTE_SignalStrength in ril.h
153        if (mRssnr == Integer.MAX_VALUE) levelRssnr = 0;
154        else if (mRssnr >= 45) levelRssnr = SIGNAL_STRENGTH_GREAT;
155        else if (mRssnr >= 10) levelRssnr = SIGNAL_STRENGTH_GOOD;
156        else if (mRssnr >= -30) levelRssnr = SIGNAL_STRENGTH_MODERATE;
157        else levelRssnr = SIGNAL_STRENGTH_POOR;
158
159        int level;
160        if (mRsrp == Integer.MAX_VALUE)
161            level = levelRssnr;
162        else if (mRssnr == Integer.MAX_VALUE)
163            level = levelRsrp;
164        else
165            level = (levelRssnr < levelRsrp) ? levelRssnr : levelRsrp;
166
167        if (DBG) log("Lte rsrp level: " + levelRsrp
168                + " snr level: " + levelRssnr + " level: " + level);
169        return level;
170    }
171
172    /**
173     * Get reference signal received quality
174     */
175    public int getRsrq() {
176        return mRsrq;
177    }
178
179    /**
180     * Get reference signal signal-to-noise ratio
181     */
182    public int getRssnr() {
183        return mRssnr;
184    }
185
186    /**
187     * Get reference signal received power
188     */
189    public int getRsrp() {
190        return mRsrp;
191    }
192
193    /**
194     * Get channel quality indicator
195     */
196    public int getCqi() {
197        return mCqi;
198    }
199
200    /**
201     * Get signal strength as dBm
202     */
203    @Override
204    public int getDbm() {
205        return mRsrp;
206    }
207
208    /**
209     * Get the LTE signal level as an asu value between 0..97, 99 is unknown
210     * Asu is calculated based on 3GPP RSRP. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69
211     */
212    @Override
213    public int getAsuLevel() {
214        int lteAsuLevel = 99;
215        int lteDbm = getDbm();
216        if (lteDbm == Integer.MAX_VALUE) lteAsuLevel = 99;
217        else if (lteDbm <= -140) lteAsuLevel = 0;
218        else if (lteDbm >= -43) lteAsuLevel = 97;
219        else lteAsuLevel = lteDbm + 140;
220        if (DBG) log("Lte Asu level: "+lteAsuLevel);
221        return lteAsuLevel;
222    }
223
224    /**
225     * Get the timing advance value for LTE, as a value between 0..63.
226     * Integer.MAX_VALUE is reported when there is no active RRC
227     * connection. Refer to 3GPP 36.213 Sec 4.2.3
228     * @return the LTE timing advance, if available.
229     */
230    public int getTimingAdvance() {
231        return mTimingAdvance;
232    }
233
234    @Override
235    public int hashCode() {
236        return Objects.hash(mSignalStrength, mRsrp, mRsrq, mRssnr, mCqi, mTimingAdvance);
237    }
238
239    @Override
240    public boolean equals (Object o) {
241        CellSignalStrengthLte s;
242
243        try {
244            s = (CellSignalStrengthLte) o;
245        } catch (ClassCastException ex) {
246            return false;
247        }
248
249        if (o == null) {
250            return false;
251        }
252
253        return mSignalStrength == s.mSignalStrength
254                && mRsrp == s.mRsrp
255                && mRsrq == s.mRsrq
256                && mRssnr == s.mRssnr
257                && mCqi == s.mCqi
258                && mTimingAdvance == s.mTimingAdvance;
259    }
260
261    /**
262     * @return string representation.
263     */
264    @Override
265    public String toString() {
266        return "CellSignalStrengthLte:"
267                + " ss=" + mSignalStrength
268                + " rsrp=" + mRsrp
269                + " rsrq=" + mRsrq
270                + " rssnr=" + mRssnr
271                + " cqi=" + mCqi
272                + " ta=" + mTimingAdvance;
273    }
274
275    /** Implement the Parcelable interface */
276    @Override
277    public void writeToParcel(Parcel dest, int flags) {
278        if (DBG) log("writeToParcel(Parcel, int): " + toString());
279        dest.writeInt(mSignalStrength);
280        // Need to multiply rsrp and rsrq by -1
281        // to ensure consistency when reading values written here
282        // unless the values are invalid
283        dest.writeInt(mRsrp * (mRsrp != Integer.MAX_VALUE ? -1 : 1));
284        dest.writeInt(mRsrq * (mRsrq != Integer.MAX_VALUE ? -1 : 1));
285        dest.writeInt(mRssnr);
286        dest.writeInt(mCqi);
287        dest.writeInt(mTimingAdvance);
288    }
289
290    /**
291     * Construct a SignalStrength object from the given parcel
292     * where the token is already been processed.
293     */
294    private CellSignalStrengthLte(Parcel in) {
295        mSignalStrength = in.readInt();
296        // rsrp and rsrq are written into the parcel as positive values.
297        // Need to convert into negative values unless the values are invalid
298        mRsrp = in.readInt();
299        if (mRsrp != Integer.MAX_VALUE) mRsrp *= -1;
300        mRsrq = in.readInt();
301        if (mRsrq != Integer.MAX_VALUE) mRsrq *= -1;
302        mRssnr = in.readInt();
303        mCqi = in.readInt();
304        mTimingAdvance = in.readInt();
305        if (DBG) log("CellSignalStrengthLte(Parcel): " + toString());
306    }
307
308    /** Implement the Parcelable interface */
309    @Override
310    public int describeContents() {
311        return 0;
312    }
313
314    /** Implement the Parcelable interface */
315    @SuppressWarnings("hiding")
316    public static final Parcelable.Creator<CellSignalStrengthLte> CREATOR =
317            new Parcelable.Creator<CellSignalStrengthLte>() {
318        @Override
319        public CellSignalStrengthLte createFromParcel(Parcel in) {
320            return new CellSignalStrengthLte(in);
321        }
322
323        @Override
324        public CellSignalStrengthLte[] newArray(int size) {
325            return new CellSignalStrengthLte[size];
326        }
327    };
328
329    /**
330     * log
331     */
332    private static void log(String s) {
333        Rlog.w(LOG_TAG, s);
334    }
335}
336