Home | History | Annotate | Download | only in uicc
      1 /*
      2  * Copyright (C) 2006 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 com.android.internal.telephony.uicc;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 import android.telephony.Rlog;
     22 
     23 import java.util.Arrays;
     24 
     25 /**
     26  * {@hide}
     27  */
     28 public class PlmnActRecord implements Parcelable {
     29     private static final String LOG_TAG = "PlmnActRecord";
     30 
     31     // Values specified in 3GPP 31.102 sec. 4.2.5
     32     public static final int ACCESS_TECH_UTRAN = 0x8000;
     33     public static final int ACCESS_TECH_EUTRAN = 0x4000;
     34     public static final int ACCESS_TECH_GSM = 0x0080;
     35     public static final int ACCESS_TECH_GSM_COMPACT = 0x0040;
     36     public static final int ACCESS_TECH_CDMA2000_HRPD = 0x0020;
     37     public static final int ACCESS_TECH_CDMA2000_1XRTT = 0x0010;
     38     public static final int ACCESS_TECH_RESERVED = 0x3F0F;
     39 
     40     public static final int ENCODED_LENGTH = 5;
     41 
     42     public final String plmn;
     43     public final int accessTechs;
     44 
     45     private static final boolean VDBG = false;
     46 
     47     public static final Parcelable.Creator<PlmnActRecord> CREATOR =
     48             new Parcelable.Creator<PlmnActRecord>() {
     49         @Override
     50         public PlmnActRecord createFromParcel(Parcel source) {
     51             return new PlmnActRecord(source.readString(), source.readInt());
     52         }
     53 
     54         @Override
     55         public PlmnActRecord[] newArray(int size) {
     56             return new PlmnActRecord[size];
     57         }
     58     };
     59 
     60     /* From 3gpp 31.102 section 4.2.5
     61      * Bytes 0-2 bcd-encoded PLMN-ID
     62      * Bytes 3-4 bitfield of access technologies
     63      */
     64     public PlmnActRecord(byte[] bytes, int offset) {
     65         if (VDBG) Rlog.v(LOG_TAG, "Creating PlmnActRecord " + offset);
     66         this.plmn = IccUtils.bcdPlmnToString(bytes, offset);
     67         this.accessTechs = (Byte.toUnsignedInt(bytes[offset + 3]) << 8)
     68                         | Byte.toUnsignedInt(bytes[offset + 4]);
     69     }
     70 
     71     private PlmnActRecord(String plmn, int accessTechs) {
     72         this.plmn = plmn;
     73         this.accessTechs = accessTechs;
     74     }
     75 
     76     private String accessTechString() {
     77         if (accessTechs == 0) {
     78             return "NONE";
     79         }
     80 
     81         StringBuilder sb = new StringBuilder();
     82         if ((accessTechs & ACCESS_TECH_UTRAN) != 0) {
     83             sb.append("UTRAN|");
     84         }
     85         if ((accessTechs & ACCESS_TECH_EUTRAN) != 0) {
     86             sb.append("EUTRAN|");
     87         }
     88         if ((accessTechs & ACCESS_TECH_GSM) != 0) {
     89             sb.append("GSM|");
     90         }
     91         if ((accessTechs & ACCESS_TECH_GSM_COMPACT) != 0) {
     92             sb.append("GSM_COMPACT|");
     93         }
     94         if ((accessTechs & ACCESS_TECH_CDMA2000_HRPD) != 0) {
     95             sb.append("CDMA2000_HRPD|");
     96         }
     97         if ((accessTechs & ACCESS_TECH_CDMA2000_1XRTT) != 0) {
     98             sb.append("CDMA2000_1XRTT|");
     99         }
    100         if ((accessTechs & ACCESS_TECH_RESERVED) != 0) {
    101             sb.append(String.format("UNKNOWN:%x|", accessTechs & ACCESS_TECH_RESERVED));
    102         }
    103         // Trim the tailing pipe character
    104         return sb.substring(0, sb.length() - 1);
    105     }
    106 
    107     @Override
    108     public String toString() {
    109         return String.format("{PLMN=%s,AccessTechs=%s}", plmn, accessTechString());
    110     }
    111 
    112     /**
    113      * Convenience method for extracting all records from encoded bytes
    114      */
    115     public static PlmnActRecord[] getRecords(byte[] recordBytes) {
    116         if (recordBytes == null || recordBytes.length == 0
    117                 || recordBytes.length % ENCODED_LENGTH != 0) {
    118             Rlog.e(LOG_TAG, "Malformed PlmnActRecord, bytes: "
    119                     + ((recordBytes != null) ? Arrays.toString(recordBytes) : null));
    120             return null;
    121         }
    122         int numRecords = recordBytes.length / ENCODED_LENGTH;
    123         if (VDBG) Rlog.v(LOG_TAG, "Extracting Logs, count=" + numRecords);
    124 
    125         PlmnActRecord[] records = new PlmnActRecord[numRecords];
    126 
    127         for(int i = 0; i < numRecords; i++) {
    128             records[i] = new PlmnActRecord(recordBytes, i * ENCODED_LENGTH);
    129         }
    130         return records;
    131     }
    132 
    133     // Parcelable Implementation
    134     @Override
    135     public int describeContents() {
    136         return 0;
    137     }
    138 
    139     @Override
    140     public void writeToParcel(Parcel dest, int flags) {
    141         dest.writeString(plmn);
    142         dest.writeInt(accessTechs);
    143     }
    144 
    145 }
    146