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 * CellIdentity to represent a unique GSM cell
27 */
28public final class CellIdentityGsm implements Parcelable {
29
30    private static final String LOG_TAG = "CellIdentityGsm";
31    private static final boolean DBG = false;
32
33    // 3-digit Mobile Country Code, 0..999
34    private final int mMcc;
35    // 2 or 3-digit Mobile Network Code, 0..999
36    private final int mMnc;
37    // 16-bit Location Area Code, 0..65535
38    private final int mLac;
39    // 16-bit GSM Cell Identity described in TS 27.007, 0..65535
40    private final int mCid;
41    // 16-bit GSM Absolute RF Channel Number
42    private final int mArfcn;
43    // 6-bit Base Station Identity Code
44    private final int mBsic;
45
46    /**
47     * @hide
48     */
49    public CellIdentityGsm() {
50        mMcc = Integer.MAX_VALUE;
51        mMnc = Integer.MAX_VALUE;
52        mLac = Integer.MAX_VALUE;
53        mCid = Integer.MAX_VALUE;
54        mArfcn = Integer.MAX_VALUE;
55        mBsic = Integer.MAX_VALUE;
56    }
57    /**
58     * public constructor
59     * @param mcc 3-digit Mobile Country Code, 0..999
60     * @param mnc 2 or 3-digit Mobile Network Code, 0..999
61     * @param lac 16-bit Location Area Code, 0..65535
62     * @param cid 16-bit GSM Cell Identity or 28-bit UMTS Cell Identity
63     *
64     * @hide
65     */
66    public CellIdentityGsm (int mcc, int mnc, int lac, int cid) {
67        this(mcc, mnc, lac, cid, Integer.MAX_VALUE, Integer.MAX_VALUE);
68    }
69
70    /**
71     * public constructor
72     * @param mcc 3-digit Mobile Country Code, 0..999
73     * @param mnc 2 or 3-digit Mobile Network Code, 0..999
74     * @param lac 16-bit Location Area Code, 0..65535
75     * @param cid 16-bit GSM Cell Identity or 28-bit UMTS Cell Identity
76     * @param arfcn 16-bit GSM Absolute RF Channel Number
77     * @param bsic 6-bit Base Station Identity Code
78     *
79     * @hide
80     */
81    public CellIdentityGsm (int mcc, int mnc, int lac, int cid, int arfcn, int bsic) {
82        mMcc = mcc;
83        mMnc = mnc;
84        mLac = lac;
85        mCid = cid;
86        mArfcn = arfcn;
87        mBsic = bsic;
88    }
89
90    private CellIdentityGsm(CellIdentityGsm cid) {
91        mMcc = cid.mMcc;
92        mMnc = cid.mMnc;
93        mLac = cid.mLac;
94        mCid = cid.mCid;
95        mArfcn = cid.mArfcn;
96        mBsic = cid.mBsic;
97    }
98
99    CellIdentityGsm copy() {
100       return new CellIdentityGsm(this);
101    }
102
103    /**
104     * @return 3-digit Mobile Country Code, 0..999, Integer.MAX_VALUE if unknown
105     */
106    public int getMcc() {
107        return mMcc;
108    }
109
110    /**
111     * @return 2 or 3-digit Mobile Network Code, 0..999, Integer.MAX_VALUE if unknown
112     */
113    public int getMnc() {
114        return mMnc;
115    }
116
117    /**
118     * @return 16-bit Location Area Code, 0..65535, Integer.MAX_VALUE if unknown
119     */
120    public int getLac() {
121        return mLac;
122    }
123
124    /**
125     * @return CID
126     * Either 16-bit GSM Cell Identity described
127     * in TS 27.007, 0..65535, Integer.MAX_VALUE if unknown
128     */
129    public int getCid() {
130        return mCid;
131    }
132
133    /**
134     * @return 16-bit GSM Absolute RF Channel Number, Integer.MAX_VALUE if unknown
135     */
136    public int getArfcn() {
137        return mArfcn;
138    }
139
140    /**
141     * @return 6-bit Base Station Identity Code, Integer.MAX_VALUE if unknown
142     */
143    public int getBsic() {
144        return mBsic;
145    }
146
147
148    /**
149     * @return Integer.MAX_VALUE, undefined for GSM
150     */
151    @Deprecated
152    public int getPsc() {
153        return Integer.MAX_VALUE;
154    }
155
156    @Override
157    public int hashCode() {
158        return Objects.hash(mMcc, mMnc, mLac, mCid);
159    }
160
161    @Override
162    public boolean equals(Object other) {
163        if (this == other) {
164            return true;
165        }
166
167        if (!(other instanceof CellIdentityGsm)) {
168            return false;
169        }
170
171        CellIdentityGsm o = (CellIdentityGsm) other;
172        return mMcc == o.mMcc &&
173                mMnc == o.mMnc &&
174                mLac == o.mLac &&
175                mCid == o.mCid &&
176                mArfcn == o.mArfcn &&
177                mBsic == o.mBsic;
178    }
179
180    @Override
181    public String toString() {
182        StringBuilder sb = new StringBuilder("CellIdentityGsm:{");
183        sb.append(" mMcc=").append(mMcc);
184        sb.append(" mMnc=").append(mMnc);
185        sb.append(" mLac=").append(mLac);
186        sb.append(" mCid=").append(mCid);
187        sb.append(" mArfcn=").append(mArfcn);
188        sb.append(" mBsic=").append("0x").append(Integer.toHexString(mBsic));
189        sb.append("}");
190
191        return sb.toString();
192    }
193
194    /** Implement the Parcelable interface */
195    @Override
196    public int describeContents() {
197        return 0;
198    }
199
200    /** Implement the Parcelable interface */
201    @Override
202    public void writeToParcel(Parcel dest, int flags) {
203        if (DBG) log("writeToParcel(Parcel, int): " + toString());
204        dest.writeInt(mMcc);
205        dest.writeInt(mMnc);
206        dest.writeInt(mLac);
207        dest.writeInt(mCid);
208        dest.writeInt(mArfcn);
209        dest.writeInt(mBsic);
210    }
211
212    /** Construct from Parcel, type has already been processed */
213    private CellIdentityGsm(Parcel in) {
214        mMcc = in.readInt();
215        mMnc = in.readInt();
216        mLac = in.readInt();
217        mCid = in.readInt();
218        mArfcn = in.readInt();
219        int bsic = in.readInt();
220        // In RIL BSIC is a UINT8, so 0xFF is the 'INVALID' designator
221        // for inbound parcels
222        if (bsic == 0xFF) bsic = Integer.MAX_VALUE;
223        mBsic = bsic;
224
225        if (DBG) log("CellIdentityGsm(Parcel): " + toString());
226    }
227
228    /** Implement the Parcelable interface */
229    @SuppressWarnings("hiding")
230    public static final Creator<CellIdentityGsm> CREATOR =
231            new Creator<CellIdentityGsm>() {
232        @Override
233        public CellIdentityGsm createFromParcel(Parcel in) {
234            return new CellIdentityGsm(in);
235        }
236
237        @Override
238        public CellIdentityGsm[] newArray(int size) {
239            return new CellIdentityGsm[size];
240        }
241    };
242
243    /**
244     * log
245     */
246    private static void log(String s) {
247        Rlog.w(LOG_TAG, s);
248    }
249}
250