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 
18 package android.telephony.data;
19 
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.SystemApi;
24 import android.net.LinkAddress;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 import android.telephony.DataFailCause;
28 import android.telephony.DataFailCause.FailCause;
29 import android.telephony.data.ApnSetting.ProtocolType;
30 
31 import com.android.internal.annotations.VisibleForTesting;
32 
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 import java.net.InetAddress;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Objects;
39 
40 /**
41  * Description of the response of a setup data call connection request.
42  *
43  * @hide
44  */
45 @SystemApi
46 public final class DataCallResponse implements Parcelable {
47 
48     /** {@hide} */
49     @IntDef(prefix = "LINK_STATUS_", value = {
50             LINK_STATUS_UNKNOWN,
51             LINK_STATUS_INACTIVE,
52             LINK_STATUS_DORMANT,
53             LINK_STATUS_ACTIVE
54     })
55     @Retention(RetentionPolicy.SOURCE)
56     public @interface LinkStatus {}
57 
58     /** Unknown status */
59     public static final int LINK_STATUS_UNKNOWN = -1;
60 
61     /** Indicates the data connection is inactive. */
62     public static final int LINK_STATUS_INACTIVE = 0;
63 
64     /** Indicates the data connection is active with physical link dormant. */
65     public static final int LINK_STATUS_DORMANT = 1;
66 
67     /** Indicates the data connection is active with physical link up. */
68     public static final int LINK_STATUS_ACTIVE = 2;
69 
70     private final @FailCause int mCause;
71     private final int mSuggestedRetryTime;
72     private final int mId;
73     private final @LinkStatus int mLinkStatus;
74     private final @ProtocolType int mProtocolType;
75     private final String mInterfaceName;
76     private final List<LinkAddress> mAddresses;
77     private final List<InetAddress> mDnsAddresses;
78     private final List<InetAddress> mGatewayAddresses;
79     private final List<InetAddress> mPcscfAddresses;
80     private final int mMtu;
81 
82     /**
83      * @param cause Data call fail cause. {@link DataFailCause#NONE} indicates no error.
84      * @param suggestedRetryTime The suggested data retry time in milliseconds.
85      * @param id The unique id of the data connection.
86      * @param linkStatus Data connection link status.
87      * @param protocolType The connection protocol, should be one of the PDP_type values in 3GPP
88      * TS 27.007 section 10.1.1. For example, "IP", "IPV6", "IPV4V6", or "PPP".
89      * @param interfaceName The network interface name.
90      * @param addresses A list of addresses with optional "/" prefix length, e.g.,
91      * "192.0.1.3" or "192.0.1.11/16 2001:db8::1/64". Typically 1 IPv4 or 1 IPv6 or
92      * one of each. If the prefix length is absent the addresses are assumed to be
93      * point to point with IPv4 having a prefix length of 32 and IPv6 128.
94      * @param dnsAddresses A list of DNS server addresses, e.g., "192.0.1.3" or
95      * "192.0.1.11 2001:db8::1". Null if no dns server addresses returned.
96      * @param gatewayAddresses A list of default gateway addresses, e.g., "192.0.1.3" or
97      * "192.0.1.11 2001:db8::1". When null, the addresses represent point to point connections.
98      * @param pcscfAddresses A list of Proxy Call State Control Function address via PCO (Protocol
99      * Configuration Option) for IMS client.
100      * @param mtu MTU (maximum transmission unit) in bytes received from network. Zero or negative
101      * values means network has either not sent a value or sent an invalid value.
102      * either not sent a value or sent an invalid value.
103      *
104      * @removed Use the {@link Builder()} instead.
105      */
DataCallResponse(@ailCause int cause, int suggestedRetryTime, int id, @LinkStatus int linkStatus, @ProtocolType int protocolType, @Nullable String interfaceName, @Nullable List<LinkAddress> addresses, @Nullable List<InetAddress> dnsAddresses, @Nullable List<InetAddress> gatewayAddresses, @Nullable List<InetAddress> pcscfAddresses, int mtu)106     public DataCallResponse(@FailCause int cause, int suggestedRetryTime, int id,
107                             @LinkStatus int linkStatus,
108                             @ProtocolType int protocolType, @Nullable String interfaceName,
109                             @Nullable List<LinkAddress> addresses,
110                             @Nullable List<InetAddress> dnsAddresses,
111                             @Nullable List<InetAddress> gatewayAddresses,
112                             @Nullable List<InetAddress> pcscfAddresses, int mtu) {
113         mCause = cause;
114         mSuggestedRetryTime = suggestedRetryTime;
115         mId = id;
116         mLinkStatus = linkStatus;
117         mProtocolType = protocolType;
118         mInterfaceName = (interfaceName == null) ? "" : interfaceName;
119         mAddresses = (addresses == null)
120                 ? new ArrayList<>() : new ArrayList<>(addresses);
121         mDnsAddresses = (dnsAddresses == null)
122                 ? new ArrayList<>() : new ArrayList<>(dnsAddresses);
123         mGatewayAddresses = (gatewayAddresses == null)
124                 ? new ArrayList<>() : new ArrayList<>(gatewayAddresses);
125         mPcscfAddresses = (pcscfAddresses == null)
126                 ? new ArrayList<>() : new ArrayList<>(pcscfAddresses);
127         mMtu = mtu;
128     }
129 
130     /** @hide */
131     @VisibleForTesting
DataCallResponse(Parcel source)132     public DataCallResponse(Parcel source) {
133         mCause = source.readInt();
134         mSuggestedRetryTime = source.readInt();
135         mId = source.readInt();
136         mLinkStatus = source.readInt();
137         mProtocolType = source.readInt();
138         mInterfaceName = source.readString();
139         mAddresses = new ArrayList<>();
140         source.readList(mAddresses, LinkAddress.class.getClassLoader());
141         mDnsAddresses = new ArrayList<>();
142         source.readList(mDnsAddresses, InetAddress.class.getClassLoader());
143         mGatewayAddresses = new ArrayList<>();
144         source.readList(mGatewayAddresses, InetAddress.class.getClassLoader());
145         mPcscfAddresses = new ArrayList<>();
146         source.readList(mPcscfAddresses, InetAddress.class.getClassLoader());
147         mMtu = source.readInt();
148     }
149 
150     /**
151      * @return Data call fail cause. {@link DataFailCause#NONE} indicates no error.
152      */
153     @FailCause
getCause()154     public int getCause() { return mCause; }
155 
156     /**
157      * @return The suggested data retry time in milliseconds.
158      */
getSuggestedRetryTime()159     public int getSuggestedRetryTime() { return mSuggestedRetryTime; }
160 
161     /**
162      * @return The unique id of the data connection.
163      */
getId()164     public int getId() { return mId; }
165 
166     /**
167      * @return The link status
168      */
getLinkStatus()169     @LinkStatus public int getLinkStatus() { return mLinkStatus; }
170 
171     /**
172      * @return The connection protocol type.
173      */
174     @ProtocolType
getProtocolType()175     public int getProtocolType() { return mProtocolType; }
176 
177     /**
178      * @return The network interface name (e.g. "rmnet_data1").
179      */
180     @NonNull
getInterfaceName()181     public String getInterfaceName() { return mInterfaceName; }
182 
183     /**
184      * @return A list of addresses of this data connection.
185      */
186     @NonNull
getAddresses()187     public List<LinkAddress> getAddresses() { return mAddresses; }
188 
189     /**
190      * @return A list of DNS server addresses, e.g., "192.0.1.3" or
191      * "192.0.1.11 2001:db8::1". Empty list if no dns server addresses returned.
192      */
193     @NonNull
getDnsAddresses()194     public List<InetAddress> getDnsAddresses() { return mDnsAddresses; }
195 
196     /**
197      * @return A list of default gateway addresses, e.g., "192.0.1.3" or
198      * "192.0.1.11 2001:db8::1". Empty list if the addresses represent point to point connections.
199      */
200     @NonNull
getGatewayAddresses()201     public List<InetAddress> getGatewayAddresses() { return mGatewayAddresses; }
202 
203     /**
204      * @return A list of Proxy Call State Control Function address via PCO (Protocol Configuration
205      * Option) for IMS client.
206      */
207     @NonNull
getPcscfAddresses()208     public List<InetAddress> getPcscfAddresses() { return mPcscfAddresses; }
209 
210     /**
211      * @return MTU (maximum transmission unit) in bytes received from network. Zero or negative
212      * values means network has either not sent a value or sent an invalid value.
213      */
getMtu()214     public int getMtu() { return mMtu; }
215 
216     @Override
toString()217     public String toString() {
218         StringBuffer sb = new StringBuffer();
219         sb.append("DataCallResponse: {")
220            .append(" cause=").append(mCause)
221            .append(" retry=").append(mSuggestedRetryTime)
222            .append(" cid=").append(mId)
223            .append(" linkStatus=").append(mLinkStatus)
224            .append(" protocolType=").append(mProtocolType)
225            .append(" ifname=").append(mInterfaceName)
226            .append(" addresses=").append(mAddresses)
227            .append(" dnses=").append(mDnsAddresses)
228            .append(" gateways=").append(mGatewayAddresses)
229            .append(" pcscf=").append(mPcscfAddresses)
230            .append(" mtu=").append(mMtu)
231            .append("}");
232         return sb.toString();
233     }
234 
235     @Override
equals(Object o)236     public boolean equals (Object o) {
237         if (this == o) return true;
238 
239         if (!(o instanceof DataCallResponse)) {
240             return false;
241         }
242 
243         DataCallResponse other = (DataCallResponse) o;
244         return this.mCause == other.mCause
245                 && this.mSuggestedRetryTime == other.mSuggestedRetryTime
246                 && this.mId == other.mId
247                 && this.mLinkStatus == other.mLinkStatus
248                 && this.mProtocolType == other.mProtocolType
249                 && this.mInterfaceName.equals(other.mInterfaceName)
250                 && mAddresses.size() == other.mAddresses.size()
251                 && mAddresses.containsAll(other.mAddresses)
252                 && mDnsAddresses.size() == other.mDnsAddresses.size()
253                 && mDnsAddresses.containsAll(other.mDnsAddresses)
254                 && mGatewayAddresses.size() == other.mGatewayAddresses.size()
255                 && mGatewayAddresses.containsAll(other.mGatewayAddresses)
256                 && mPcscfAddresses.size() == other.mPcscfAddresses.size()
257                 && mPcscfAddresses.containsAll(other.mPcscfAddresses)
258                 && mMtu == other.mMtu;
259     }
260 
261     @Override
hashCode()262     public int hashCode() {
263         return Objects.hash(mCause, mSuggestedRetryTime, mId, mLinkStatus, mProtocolType,
264                 mInterfaceName, mAddresses, mDnsAddresses, mGatewayAddresses, mPcscfAddresses,
265                 mMtu);
266     }
267 
268     @Override
describeContents()269     public int describeContents() {
270         return 0;
271     }
272 
273     @Override
writeToParcel(Parcel dest, int flags)274     public void writeToParcel(Parcel dest, int flags) {
275         dest.writeInt(mCause);
276         dest.writeInt(mSuggestedRetryTime);
277         dest.writeInt(mId);
278         dest.writeInt(mLinkStatus);
279         dest.writeInt(mProtocolType);
280         dest.writeString(mInterfaceName);
281         dest.writeList(mAddresses);
282         dest.writeList(mDnsAddresses);
283         dest.writeList(mGatewayAddresses);
284         dest.writeList(mPcscfAddresses);
285         dest.writeInt(mMtu);
286     }
287 
288     public static final @android.annotation.NonNull Parcelable.Creator<DataCallResponse> CREATOR =
289             new Parcelable.Creator<DataCallResponse>() {
290                 @Override
291                 public DataCallResponse createFromParcel(Parcel source) {
292                     return new DataCallResponse(source);
293                 }
294 
295                 @Override
296                 public DataCallResponse[] newArray(int size) {
297                     return new DataCallResponse[size];
298                 }
299             };
300 
301     /**
302      * Provides a convenient way to set the fields of a {@link DataCallResponse} when creating a new
303      * instance.
304      *
305      * <p>The example below shows how you might create a new {@code DataCallResponse}:
306      *
307      * <pre><code>
308      *
309      * DataCallResponse response = new DataCallResponse.Builder()
310      *     .setAddresses(Arrays.asList("192.168.1.2"))
311      *     .setProtocolType(ApnSetting.PROTOCOL_IPV4V6)
312      *     .build();
313      * </code></pre>
314      */
315     public static final class Builder {
316         private @FailCause int mCause;
317 
318         private int mSuggestedRetryTime;
319 
320         private int mId;
321 
322         private @LinkStatus int mLinkStatus;
323 
324         private @ProtocolType int mProtocolType;
325 
326         private String mInterfaceName;
327 
328         private List<LinkAddress> mAddresses;
329 
330         private List<InetAddress> mDnsAddresses;
331 
332         private List<InetAddress> mGatewayAddresses;
333 
334         private List<InetAddress> mPcscfAddresses;
335 
336         private int mMtu;
337 
338         /**
339          * Default constructor for Builder.
340          */
Builder()341         public Builder() {
342         }
343 
344         /**
345          * Set data call fail cause.
346          *
347          * @param cause Data call fail cause. {@link DataFailCause#NONE} indicates no error.
348          * @return The same instance of the builder.
349          */
setCause(@ailCause int cause)350         public @NonNull Builder setCause(@FailCause int cause) {
351             mCause = cause;
352             return this;
353         }
354 
355         /**
356          * Set the suggested data retry time.
357          *
358          * @param suggestedRetryTime The suggested data retry time in milliseconds.
359          * @return The same instance of the builder.
360          */
setSuggestedRetryTime(int suggestedRetryTime)361         public @NonNull Builder setSuggestedRetryTime(int suggestedRetryTime) {
362             mSuggestedRetryTime = suggestedRetryTime;
363             return this;
364         }
365 
366         /**
367          * Set the unique id of the data connection.
368          *
369          * @param id The unique id of the data connection.
370          * @return The same instance of the builder.
371          */
setId(int id)372         public @NonNull Builder setId(int id) {
373             mId = id;
374             return this;
375         }
376 
377         /**
378          * Set the link status
379          *
380          * @param linkStatus The link status
381          * @return The same instance of the builder.
382          */
setLinkStatus(@inkStatus int linkStatus)383         public @NonNull Builder setLinkStatus(@LinkStatus int linkStatus) {
384             mLinkStatus = linkStatus;
385             return this;
386         }
387 
388         /**
389          * Set the connection protocol type.
390          *
391          * @param protocolType The connection protocol type.
392          * @return The same instance of the builder.
393          */
setProtocolType(@rotocolType int protocolType)394         public @NonNull Builder setProtocolType(@ProtocolType int protocolType) {
395             mProtocolType = protocolType;
396             return this;
397         }
398 
399         /**
400          * Set the network interface name.
401          *
402          * @param interfaceName The network interface name (e.g. "rmnet_data1").
403          * @return The same instance of the builder.
404          */
setInterfaceName(@onNull String interfaceName)405         public @NonNull Builder setInterfaceName(@NonNull String interfaceName) {
406             mInterfaceName = interfaceName;
407             return this;
408         }
409 
410         /**
411          * Set the addresses of this data connection.
412          *
413          * @param addresses The list of address of the data connection.
414          * @return The same instance of the builder.
415          */
setAddresses(@onNull List<LinkAddress> addresses)416         public @NonNull Builder setAddresses(@NonNull List<LinkAddress> addresses) {
417             mAddresses = addresses;
418             return this;
419         }
420 
421         /**
422          * Set the DNS addresses of this data connection
423          *
424          * @param dnsAddresses The list of DNS address of the data connection.
425          * @return The same instance of the builder.
426          */
setDnsAddresses(@onNull List<InetAddress> dnsAddresses)427         public @NonNull Builder setDnsAddresses(@NonNull List<InetAddress> dnsAddresses) {
428             mDnsAddresses = dnsAddresses;
429             return this;
430         }
431 
432         /**
433          * Set the gateway addresses of this data connection
434          *
435          * @param gatewayAddresses The list of gateway address of the data connection.
436          * @return The same instance of the builder.
437          */
setGatewayAddresses(@onNull List<InetAddress> gatewayAddresses)438         public @NonNull Builder setGatewayAddresses(@NonNull List<InetAddress> gatewayAddresses) {
439             mGatewayAddresses = gatewayAddresses;
440             return this;
441         }
442 
443         /**
444          * Set the Proxy Call State Control Function address via PCO(Protocol Configuration
445          * Option) for IMS client.
446          *
447          * @param pcscfAddresses The list of pcscf address of the data connection.
448          * @return The same instance of the builder.
449          */
setPcscfAddresses(@onNull List<InetAddress> pcscfAddresses)450         public @NonNull Builder setPcscfAddresses(@NonNull List<InetAddress> pcscfAddresses) {
451             mPcscfAddresses = pcscfAddresses;
452             return this;
453         }
454 
455         /**
456          * Set maximum transmission unit of the data connection.
457          *
458          * @param mtu MTU (maximum transmission unit) in bytes received from network. Zero or
459          * negative values means network has either not sent a value or sent an invalid value.
460          *
461          * @return The same instance of the builder.
462          */
setMtu(int mtu)463         public @NonNull Builder setMtu(int mtu) {
464             mMtu = mtu;
465             return this;
466         }
467 
468         /**
469          * Build the DataCallResponse.
470          *
471          * @return the DataCallResponse object.
472          */
build()473         public @NonNull DataCallResponse build() {
474             return new DataCallResponse(mCause, mSuggestedRetryTime, mId, mLinkStatus,
475                     mProtocolType, mInterfaceName, mAddresses, mDnsAddresses, mGatewayAddresses,
476                     mPcscfAddresses, mMtu);
477         }
478     }
479 }
480