1/*
2 * Copyright (C) 2009 Qualcomm Innovation Center, Inc.  All Rights Reserved.
3 * Copyright (C) 2009 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package android.telephony.data;
19
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.net.LinkAddress;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26import java.net.InetAddress;
27import java.util.ArrayList;
28import java.util.List;
29import java.util.Objects;
30
31/**
32 * Description of the response of a setup data call connection request.
33 *
34 * @hide
35 */
36public final class DataCallResponse implements Parcelable {
37    private final int mStatus;
38    private final int mSuggestedRetryTime;
39    private final int mCid;
40    private final int mActive;
41    private final String mType;
42    private final String mIfname;
43    private final List<LinkAddress> mAddresses;
44    private final List<InetAddress> mDnses;
45    private final List<InetAddress> mGateways;
46    private final List<String> mPcscfs;
47    private final int mMtu;
48
49    /**
50     * @param status Data call fail cause. 0 indicates no error.
51     * @param suggestedRetryTime The suggested data retry time in milliseconds.
52     * @param cid The unique id of the data connection.
53     * @param active Data connection active status. 0 = inactive, 1 = active/physical link down,
54     *               2 = active/physical link up.
55     * @param type The connection protocol, should be one of the PDP_type values in TS 27.007
56     *             section 10.1.1. For example, "IP", "IPV6", "IPV4V6", or "PPP".
57     * @param ifname The network interface name.
58     * @param addresses A list of addresses with optional "/" prefix length, e.g.,
59     *                  "192.0.1.3" or "192.0.1.11/16 2001:db8::1/64". Typically 1 IPv4 or 1 IPv6 or
60     *                  one of each. If the prefix length is absent the addresses are assumed to be
61     *                  point to point with IPv4 having a prefix length of 32 and IPv6 128.
62     * @param dnses A list of DNS server addresses, e.g., "192.0.1.3" or
63     *              "192.0.1.11 2001:db8::1". Null if no dns server addresses returned.
64     * @param gateways A list of default gateway addresses, e.g., "192.0.1.3" or
65     *                 "192.0.1.11 2001:db8::1". When null, the addresses represent point to point
66     *                 connections.
67     * @param pcscfs A list of Proxy Call State Control Function address via PCO(Protocol
68     *               Configuration Option) for IMS client.
69     * @param mtu MTU (Maximum transmission unit) received from network Value <= 0 means network has
70     *            either not sent a value or sent an invalid value.
71     */
72    public DataCallResponse(int status, int suggestedRetryTime, int cid, int active,
73                            @Nullable String type, @Nullable String ifname,
74                            @Nullable List<LinkAddress> addresses,
75                            @Nullable List<InetAddress> dnses,
76                            @Nullable List<InetAddress> gateways,
77                            @Nullable List<String> pcscfs, int mtu) {
78        mStatus = status;
79        mSuggestedRetryTime = suggestedRetryTime;
80        mCid = cid;
81        mActive = active;
82        mType = (type == null) ? "" : type;
83        mIfname = (ifname == null) ? "" : ifname;
84        mAddresses = (addresses == null) ? new ArrayList<>() : addresses;
85        mDnses = (dnses == null) ? new ArrayList<>() : dnses;
86        mGateways = (gateways == null) ? new ArrayList<>() : gateways;
87        mPcscfs = (pcscfs == null) ? new ArrayList<>() : pcscfs;
88        mMtu = mtu;
89    }
90
91    public DataCallResponse(Parcel source) {
92        mStatus = source.readInt();
93        mSuggestedRetryTime = source.readInt();
94        mCid = source.readInt();
95        mActive = source.readInt();
96        mType = source.readString();
97        mIfname = source.readString();
98        mAddresses = new ArrayList<>();
99        source.readList(mAddresses, LinkAddress.class.getClassLoader());
100        mDnses = new ArrayList<>();
101        source.readList(mDnses, InetAddress.class.getClassLoader());
102        mGateways = new ArrayList<>();
103        source.readList(mGateways, InetAddress.class.getClassLoader());
104        mPcscfs = new ArrayList<>();
105        source.readList(mPcscfs, InetAddress.class.getClassLoader());
106        mMtu = source.readInt();
107    }
108
109    /**
110     * @return Data call fail cause. 0 indicates no error.
111     */
112    public int getStatus() { return mStatus; }
113
114    /**
115     * @return The suggested data retry time in milliseconds.
116     */
117    public int getSuggestedRetryTime() { return mSuggestedRetryTime; }
118
119    /**
120     * @return The unique id of the data connection.
121     */
122    public int getCallId() { return mCid; }
123
124    /**
125     * @return 0 = inactive, 1 = active/physical link down, 2 = active/physical link up.
126     */
127    public int getActive() { return mActive; }
128
129    /**
130     * @return The connection protocol, should be one of the PDP_type values in TS 27.007 section
131     * 10.1.1. For example, "IP", "IPV6", "IPV4V6", or "PPP".
132     */
133    @NonNull
134    public String getType() { return mType; }
135
136    /**
137     * @return The network interface name.
138     */
139    @NonNull
140    public String getIfname() { return mIfname; }
141
142    /**
143     * @return A list of {@link LinkAddress}
144     */
145    @NonNull
146    public List<LinkAddress> getAddresses() { return mAddresses; }
147
148    /**
149     * @return A list of DNS server addresses, e.g., "192.0.1.3" or
150     * "192.0.1.11 2001:db8::1". Empty list if no dns server addresses returned.
151     */
152    @NonNull
153    public List<InetAddress> getDnses() { return mDnses; }
154
155    /**
156     * @return A list of default gateway addresses, e.g., "192.0.1.3" or
157     * "192.0.1.11 2001:db8::1". Empty list if the addresses represent point to point connections.
158     */
159    @NonNull
160    public List<InetAddress> getGateways() { return mGateways; }
161
162    /**
163     * @return A list of Proxy Call State Control Function address via PCO(Protocol Configuration
164     * Option) for IMS client.
165     */
166    @NonNull
167    public List<String> getPcscfs() { return mPcscfs; }
168
169    /**
170     * @return MTU received from network Value <= 0 means network has either not sent a value or
171     * sent an invalid value
172     */
173    public int getMtu() { return mMtu; }
174
175    @Override
176    public String toString() {
177        StringBuffer sb = new StringBuffer();
178        sb.append("DataCallResponse: {")
179           .append(" status=").append(mStatus)
180           .append(" retry=").append(mSuggestedRetryTime)
181           .append(" cid=").append(mCid)
182           .append(" active=").append(mActive)
183           .append(" type=").append(mType)
184           .append(" ifname=").append(mIfname)
185           .append(" addresses=").append(mAddresses)
186           .append(" dnses=").append(mDnses)
187           .append(" gateways=").append(mGateways)
188           .append(" pcscf=").append(mPcscfs)
189           .append(" mtu=").append(mMtu)
190           .append("}");
191        return sb.toString();
192    }
193
194    @Override
195    public boolean equals (Object o) {
196        if (this == o) return true;
197
198        if (o == null || !(o instanceof DataCallResponse)) {
199            return false;
200        }
201
202        DataCallResponse other = (DataCallResponse) o;
203        return this.mStatus == other.mStatus
204                && this.mSuggestedRetryTime == other.mSuggestedRetryTime
205                && this.mCid == other.mCid
206                && this.mActive == other.mActive
207                && this.mType.equals(other.mType)
208                && this.mIfname.equals(other.mIfname)
209                && mAddresses.size() == other.mAddresses.size()
210                && mAddresses.containsAll(other.mAddresses)
211                && mDnses.size() == other.mDnses.size()
212                && mDnses.containsAll(other.mDnses)
213                && mGateways.size() == other.mGateways.size()
214                && mGateways.containsAll(other.mGateways)
215                && mPcscfs.size() == other.mPcscfs.size()
216                && mPcscfs.containsAll(other.mPcscfs)
217                && mMtu == other.mMtu;
218    }
219
220    @Override
221    public int hashCode() {
222        return Objects.hash(mStatus, mSuggestedRetryTime, mCid, mActive, mType, mIfname, mAddresses,
223                mDnses, mGateways, mPcscfs, mMtu);
224    }
225
226    @Override
227    public int describeContents() {
228        return 0;
229    }
230
231    @Override
232    public void writeToParcel(Parcel dest, int flags) {
233        dest.writeInt(mStatus);
234        dest.writeInt(mSuggestedRetryTime);
235        dest.writeInt(mCid);
236        dest.writeInt(mActive);
237        dest.writeString(mType);
238        dest.writeString(mIfname);
239        dest.writeList(mAddresses);
240        dest.writeList(mDnses);
241        dest.writeList(mGateways);
242        dest.writeList(mPcscfs);
243        dest.writeInt(mMtu);
244    }
245
246    public static final Parcelable.Creator<DataCallResponse> CREATOR =
247            new Parcelable.Creator<DataCallResponse>() {
248                @Override
249                public DataCallResponse createFromParcel(Parcel source) {
250                    return new DataCallResponse(source);
251                }
252
253                @Override
254                public DataCallResponse[] newArray(int size) {
255                    return new DataCallResponse[size];
256                }
257            };
258}