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 /** 24 * LTE signal strength related information. 25 */ 26 public final class CellSignalStrengthCdma extends CellSignalStrength implements Parcelable { 27 28 private static final String LOG_TAG = "CellSignalStrengthCdma"; 29 private static final boolean DBG = false; 30 31 private int mCdmaDbm; // This value is the RSSI value 32 private int mCdmaEcio; // This value is the Ec/Io 33 private int mEvdoDbm; // This value is the EVDO RSSI value 34 private int mEvdoEcio; // This value is the EVDO Ec/Io 35 private int mEvdoSnr; // Valid values are 0-8. 8 is the highest signal to noise ratio 36 37 /** 38 * Empty constructor 39 * 40 * @hide 41 */ 42 public CellSignalStrengthCdma() { 43 setDefaultValues(); 44 } 45 46 /** 47 * Constructor 48 * 49 * @hide 50 */ 51 public CellSignalStrengthCdma(int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio, 52 int evdoSnr) { 53 initialize(cdmaDbm, cdmaEcio, evdoDbm, evdoEcio, evdoSnr); 54 } 55 56 /** 57 * Copy constructors 58 * 59 * @param s Source SignalStrength 60 * 61 * @hide 62 */ 63 public CellSignalStrengthCdma(CellSignalStrengthCdma s) { 64 copyFrom(s); 65 } 66 67 /** 68 * Initialize all the values 69 * 70 * @param cdmaDbm 71 * @param cdmaEcio 72 * @param evdoDbm 73 * @param evdoEcio 74 * @param evdoSnr 75 * 76 * @hide 77 */ 78 public void initialize(int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio, int evdoSnr) { 79 mCdmaDbm = cdmaDbm; 80 mCdmaEcio = cdmaEcio; 81 mEvdoDbm = evdoDbm; 82 mEvdoEcio = evdoEcio; 83 mEvdoSnr = evdoSnr; 84 } 85 86 /** 87 * @hide 88 */ 89 protected void copyFrom(CellSignalStrengthCdma s) { 90 mCdmaDbm = s.mCdmaDbm; 91 mCdmaEcio = s.mCdmaEcio; 92 mEvdoDbm = s.mEvdoDbm; 93 mEvdoEcio = s.mEvdoEcio; 94 mEvdoSnr = s.mEvdoSnr; 95 } 96 97 /** 98 * @hide 99 */ 100 @Override 101 public CellSignalStrengthCdma copy() { 102 return new CellSignalStrengthCdma(this); 103 } 104 105 /** @hide */ 106 @Override 107 public void setDefaultValues() { 108 mCdmaDbm = Integer.MAX_VALUE; 109 mCdmaEcio = Integer.MAX_VALUE; 110 mEvdoDbm = Integer.MAX_VALUE; 111 mEvdoEcio = Integer.MAX_VALUE; 112 mEvdoSnr = Integer.MAX_VALUE; 113 } 114 115 /** 116 * Get signal level as an int from 0..4 117 */ 118 @Override 119 public int getLevel() { 120 int level; 121 122 int cdmaLevel = getCdmaLevel(); 123 int evdoLevel = getEvdoLevel(); 124 if (evdoLevel == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) { 125 /* We don't know evdo, use cdma */ 126 level = getCdmaLevel(); 127 } else if (cdmaLevel == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) { 128 /* We don't know cdma, use evdo */ 129 level = getEvdoLevel(); 130 } else { 131 /* We know both, use the lowest level */ 132 level = cdmaLevel < evdoLevel ? cdmaLevel : evdoLevel; 133 } 134 if (DBG) log("getLevel=" + level); 135 return level; 136 } 137 138 /** 139 * Get the LTE signal level as an asu value between 0..97, 99 is unknown 140 * Asu is calculated based on 3GPP RSRP. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69 141 */ 142 @Override 143 public int getAsuLevel() { 144 final int cdmaDbm = getCdmaDbm(); 145 final int cdmaEcio = getCdmaEcio(); 146 int cdmaAsuLevel; 147 int ecioAsuLevel; 148 149 if (cdmaDbm >= -75) cdmaAsuLevel = 16; 150 else if (cdmaDbm >= -82) cdmaAsuLevel = 8; 151 else if (cdmaDbm >= -90) cdmaAsuLevel = 4; 152 else if (cdmaDbm >= -95) cdmaAsuLevel = 2; 153 else if (cdmaDbm >= -100) cdmaAsuLevel = 1; 154 else cdmaAsuLevel = 99; 155 156 // Ec/Io are in dB*10 157 if (cdmaEcio >= -90) ecioAsuLevel = 16; 158 else if (cdmaEcio >= -100) ecioAsuLevel = 8; 159 else if (cdmaEcio >= -115) ecioAsuLevel = 4; 160 else if (cdmaEcio >= -130) ecioAsuLevel = 2; 161 else if (cdmaEcio >= -150) ecioAsuLevel = 1; 162 else ecioAsuLevel = 99; 163 164 int level = (cdmaAsuLevel < ecioAsuLevel) ? cdmaAsuLevel : ecioAsuLevel; 165 if (DBG) log("getAsuLevel=" + level); 166 return level; 167 } 168 169 /** 170 * Get cdma as level 0..4 171 */ 172 public int getCdmaLevel() { 173 final int cdmaDbm = getCdmaDbm(); 174 final int cdmaEcio = getCdmaEcio(); 175 int levelDbm; 176 int levelEcio; 177 178 if (cdmaDbm >= -75) levelDbm = SIGNAL_STRENGTH_GREAT; 179 else if (cdmaDbm >= -85) levelDbm = SIGNAL_STRENGTH_GOOD; 180 else if (cdmaDbm >= -95) levelDbm = SIGNAL_STRENGTH_MODERATE; 181 else if (cdmaDbm >= -100) levelDbm = SIGNAL_STRENGTH_POOR; 182 else levelDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN; 183 184 // Ec/Io are in dB*10 185 if (cdmaEcio >= -90) levelEcio = SIGNAL_STRENGTH_GREAT; 186 else if (cdmaEcio >= -110) levelEcio = SIGNAL_STRENGTH_GOOD; 187 else if (cdmaEcio >= -130) levelEcio = SIGNAL_STRENGTH_MODERATE; 188 else if (cdmaEcio >= -150) levelEcio = SIGNAL_STRENGTH_POOR; 189 else levelEcio = SIGNAL_STRENGTH_NONE_OR_UNKNOWN; 190 191 int level = (levelDbm < levelEcio) ? levelDbm : levelEcio; 192 if (DBG) log("getCdmaLevel=" + level); 193 return level; 194 } 195 196 /** 197 * Get Evdo as level 0..4 198 */ 199 public int getEvdoLevel() { 200 int evdoDbm = getEvdoDbm(); 201 int evdoSnr = getEvdoSnr(); 202 int levelEvdoDbm; 203 int levelEvdoSnr; 204 205 if (evdoDbm >= -65) levelEvdoDbm = SIGNAL_STRENGTH_GREAT; 206 else if (evdoDbm >= -75) levelEvdoDbm = SIGNAL_STRENGTH_GOOD; 207 else if (evdoDbm >= -90) levelEvdoDbm = SIGNAL_STRENGTH_MODERATE; 208 else if (evdoDbm >= -105) levelEvdoDbm = SIGNAL_STRENGTH_POOR; 209 else levelEvdoDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN; 210 211 if (evdoSnr >= 7) levelEvdoSnr = SIGNAL_STRENGTH_GREAT; 212 else if (evdoSnr >= 5) levelEvdoSnr = SIGNAL_STRENGTH_GOOD; 213 else if (evdoSnr >= 3) levelEvdoSnr = SIGNAL_STRENGTH_MODERATE; 214 else if (evdoSnr >= 1) levelEvdoSnr = SIGNAL_STRENGTH_POOR; 215 else levelEvdoSnr = SIGNAL_STRENGTH_NONE_OR_UNKNOWN; 216 217 int level = (levelEvdoDbm < levelEvdoSnr) ? levelEvdoDbm : levelEvdoSnr; 218 if (DBG) log("getEvdoLevel=" + level); 219 return level; 220 } 221 222 /** 223 * Get the signal strength as dBm 224 */ 225 @Override 226 public int getDbm() { 227 int cdmaDbm = getCdmaDbm(); 228 int evdoDbm = getEvdoDbm(); 229 230 // Use the lower value to be conservative 231 return (cdmaDbm < evdoDbm) ? cdmaDbm : evdoDbm; 232 } 233 234 /** 235 * Get the CDMA RSSI value in dBm 236 */ 237 public int getCdmaDbm() { 238 return mCdmaDbm; 239 } 240 /** @hide */ 241 public void setCdmaDbm(int cdmaDbm) { 242 mCdmaDbm = cdmaDbm; 243 } 244 245 /** 246 * Get the CDMA Ec/Io value in dB*10 247 */ 248 public int getCdmaEcio() { 249 return mCdmaEcio; 250 } 251 /** @hide */ 252 public void setCdmaEcio(int cdmaEcio) { 253 mCdmaEcio = cdmaEcio; 254 } 255 256 /** 257 * Get the EVDO RSSI value in dBm 258 */ 259 public int getEvdoDbm() { 260 return mEvdoDbm; 261 } 262 /** @hide */ 263 public void setEvdoDbm(int evdoDbm) { 264 mEvdoDbm = evdoDbm; 265 } 266 267 /** 268 * Get the EVDO Ec/Io value in dB*10 269 */ 270 public int getEvdoEcio() { 271 return mEvdoEcio; 272 } 273 /** @hide */ 274 public void setEvdoEcio(int evdoEcio) { 275 mEvdoEcio = evdoEcio; 276 } 277 278 /** 279 * Get the signal to noise ratio. Valid values are 0-8. 8 is the highest. 280 */ 281 public int getEvdoSnr() { 282 return mEvdoSnr; 283 } 284 /** @hide */ 285 public void setEvdoSnr(int evdoSnr) { 286 mEvdoSnr = evdoSnr; 287 } 288 289 @Override 290 public int hashCode() { 291 int primeNum = 31; 292 return ((mCdmaDbm * primeNum) + (mCdmaEcio * primeNum) 293 + (mEvdoDbm * primeNum) + (mEvdoEcio * primeNum) + (mEvdoSnr * primeNum)); 294 } 295 296 @Override 297 public boolean equals (Object o) { 298 CellSignalStrengthCdma s; 299 300 try { 301 s = (CellSignalStrengthCdma) o; 302 } catch (ClassCastException ex) { 303 return false; 304 } 305 306 if (o == null) { 307 return false; 308 } 309 310 return mCdmaDbm == s.mCdmaDbm 311 && mCdmaEcio == s.mCdmaEcio 312 && mEvdoDbm == s.mEvdoDbm 313 && mEvdoEcio == s.mEvdoEcio 314 && mEvdoSnr == s.mEvdoSnr; 315 } 316 317 /** 318 * @return string representation. 319 */ 320 @Override 321 public String toString() { 322 return "CellSignalStrengthCdma:" 323 + " cdmaDbm=" + mCdmaDbm 324 + " cdmaEcio=" + mCdmaEcio 325 + " evdoDbm=" + mEvdoDbm 326 + " evdoEcio=" + mEvdoEcio 327 + " evdoSnr=" + mEvdoSnr; 328 } 329 330 /** Implement the Parcelable interface */ 331 @Override 332 public void writeToParcel(Parcel dest, int flags) { 333 if (DBG) log("writeToParcel(Parcel, int): " + toString()); 334 // Need to multiply CdmaDbm, CdmaEcio, EvdoDbm and EvdoEcio by -1 335 // to ensure consistency when reading values written here 336 dest.writeInt(mCdmaDbm * -1); 337 dest.writeInt(mCdmaEcio * -1); 338 dest.writeInt(mEvdoDbm * -1); 339 dest.writeInt(mEvdoEcio * -1); 340 dest.writeInt(mEvdoSnr); 341 } 342 343 /** 344 * Construct a SignalStrength object from the given parcel 345 * where the TYPE_LTE token is already been processed. 346 */ 347 private CellSignalStrengthCdma(Parcel in) { 348 // CdmaDbm, CdmaEcio, EvdoDbm and EvdoEcio are written into 349 // the parcel as positive values. 350 // Need to convert into negative values 351 mCdmaDbm = in.readInt() * -1; 352 mCdmaEcio = in.readInt() * -1; 353 mEvdoDbm = in.readInt() * -1; 354 mEvdoEcio = in.readInt() * -1; 355 mEvdoSnr = in.readInt(); 356 if (DBG) log("CellSignalStrengthCdma(Parcel): " + toString()); 357 } 358 359 /** Implement the Parcelable interface */ 360 @Override 361 public int describeContents() { 362 return 0; 363 } 364 365 /** Implement the Parcelable interface */ 366 @SuppressWarnings("hiding") 367 public static final Parcelable.Creator<CellSignalStrengthCdma> CREATOR = 368 new Parcelable.Creator<CellSignalStrengthCdma>() { 369 @Override 370 public CellSignalStrengthCdma createFromParcel(Parcel in) { 371 return new CellSignalStrengthCdma(in); 372 } 373 374 @Override 375 public CellSignalStrengthCdma[] newArray(int size) { 376 return new CellSignalStrengthCdma[size]; 377 } 378 }; 379 380 /** 381 * log 382 */ 383 private static void log(String s) { 384 Rlog.w(LOG_TAG, s); 385 } 386 } 387