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.Annotation.DataFailureCause;
28 import android.telephony.DataFailCause;
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 @DataFailureCause 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     private final int mMtuV4;
82     private final int mMtuV6;
83 
84     /**
85      * @param cause Data call fail cause. {@link DataFailCause#NONE} indicates no error.
86      * @param suggestedRetryTime The suggested data retry time in milliseconds.
87      * @param id The unique id of the data connection.
88      * @param linkStatus Data connection link status.
89      * @param protocolType The connection protocol, should be one of the PDP_type values in 3GPP
90      * TS 27.007 section 10.1.1. For example, "IP", "IPV6", "IPV4V6", or "PPP".
91      * @param interfaceName The network interface name.
92      * @param addresses A list of addresses with optional "/" prefix length, e.g.,
93      * "192.0.1.3" or "192.0.1.11/16 2001:db8::1/64". Typically 1 IPv4 or 1 IPv6 or
94      * one of each. If the prefix length is absent the addresses are assumed to be
95      * point to point with IPv4 having a prefix length of 32 and IPv6 128.
96      * @param dnsAddresses A list of DNS server addresses, e.g., "192.0.1.3" or
97      * "192.0.1.11 2001:db8::1". Null if no dns server addresses returned.
98      * @param gatewayAddresses A list of default gateway addresses, e.g., "192.0.1.3" or
99      * "192.0.1.11 2001:db8::1". When null, the addresses represent point to point connections.
100      * @param pcscfAddresses A list of Proxy Call State Control Function address via PCO (Protocol
101      * Configuration Option) for IMS client.
102      * @param mtu MTU (maximum transmission unit) in bytes received from network.
103      * Zero or negative values means network has either not sent a value or sent an invalid value.
104      *
105      * @removed Use the {@link Builder()} instead.
106      */
DataCallResponse(@ataFailureCause 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)107     public DataCallResponse(@DataFailureCause int cause, int suggestedRetryTime, int id,
108                             @LinkStatus int linkStatus,
109                             @ProtocolType int protocolType, @Nullable String interfaceName,
110                             @Nullable List<LinkAddress> addresses,
111                             @Nullable List<InetAddress> dnsAddresses,
112                             @Nullable List<InetAddress> gatewayAddresses,
113                             @Nullable List<InetAddress> pcscfAddresses, int mtu) {
114         mCause = cause;
115         mSuggestedRetryTime = suggestedRetryTime;
116         mId = id;
117         mLinkStatus = linkStatus;
118         mProtocolType = protocolType;
119         mInterfaceName = (interfaceName == null) ? "" : interfaceName;
120         mAddresses = (addresses == null)
121                 ? new ArrayList<>() : new ArrayList<>(addresses);
122         mDnsAddresses = (dnsAddresses == null)
123                 ? new ArrayList<>() : new ArrayList<>(dnsAddresses);
124         mGatewayAddresses = (gatewayAddresses == null)
125                 ? new ArrayList<>() : new ArrayList<>(gatewayAddresses);
126         mPcscfAddresses = (pcscfAddresses == null)
127                 ? new ArrayList<>() : new ArrayList<>(pcscfAddresses);
128         mMtu = mMtuV4 = mMtuV6 = mtu;
129     }
130 
131     /** @hide */
DataCallResponse(@ataFailureCause 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, int mtuV4, int mtuV6)132     private DataCallResponse(@DataFailureCause int cause, int suggestedRetryTime, int id,
133             @LinkStatus int linkStatus, @ProtocolType int protocolType,
134             @Nullable String interfaceName, @Nullable List<LinkAddress> addresses,
135             @Nullable List<InetAddress> dnsAddresses, @Nullable List<InetAddress> gatewayAddresses,
136             @Nullable List<InetAddress> pcscfAddresses, int mtu, int mtuV4, int mtuV6) {
137         mCause = cause;
138         mSuggestedRetryTime = suggestedRetryTime;
139         mId = id;
140         mLinkStatus = linkStatus;
141         mProtocolType = protocolType;
142         mInterfaceName = (interfaceName == null) ? "" : interfaceName;
143         mAddresses = (addresses == null)
144                 ? new ArrayList<>() : new ArrayList<>(addresses);
145         mDnsAddresses = (dnsAddresses == null)
146                 ? new ArrayList<>() : new ArrayList<>(dnsAddresses);
147         mGatewayAddresses = (gatewayAddresses == null)
148                 ? new ArrayList<>() : new ArrayList<>(gatewayAddresses);
149         mPcscfAddresses = (pcscfAddresses == null)
150                 ? new ArrayList<>() : new ArrayList<>(pcscfAddresses);
151         mMtu = mtu;
152         mMtuV4 = mtuV4;
153         mMtuV6 = mtuV6;
154     }
155 
156     /** @hide */
157     @VisibleForTesting
DataCallResponse(Parcel source)158     public DataCallResponse(Parcel source) {
159         mCause = source.readInt();
160         mSuggestedRetryTime = source.readInt();
161         mId = source.readInt();
162         mLinkStatus = source.readInt();
163         mProtocolType = source.readInt();
164         mInterfaceName = source.readString();
165         mAddresses = new ArrayList<>();
166         source.readList(mAddresses, LinkAddress.class.getClassLoader());
167         mDnsAddresses = new ArrayList<>();
168         source.readList(mDnsAddresses, InetAddress.class.getClassLoader());
169         mGatewayAddresses = new ArrayList<>();
170         source.readList(mGatewayAddresses, InetAddress.class.getClassLoader());
171         mPcscfAddresses = new ArrayList<>();
172         source.readList(mPcscfAddresses, InetAddress.class.getClassLoader());
173         mMtu = source.readInt();
174         mMtuV4 = source.readInt();
175         mMtuV6 = source.readInt();
176     }
177 
178     /**
179      * @return Data call fail cause. {@link DataFailCause#NONE} indicates no error.
180      */
181     @DataFailureCause
getCause()182     public int getCause() { return mCause; }
183 
184     /**
185      * @return The suggested data retry time in milliseconds.
186      */
getSuggestedRetryTime()187     public int getSuggestedRetryTime() { return mSuggestedRetryTime; }
188 
189     /**
190      * @return The unique id of the data connection.
191      */
getId()192     public int getId() { return mId; }
193 
194     /**
195      * @return The link status
196      */
getLinkStatus()197     @LinkStatus public int getLinkStatus() { return mLinkStatus; }
198 
199     /**
200      * @return The connection protocol type.
201      */
202     @ProtocolType
getProtocolType()203     public int getProtocolType() { return mProtocolType; }
204 
205     /**
206      * @return The network interface name (e.g. "rmnet_data1").
207      */
208     @NonNull
getInterfaceName()209     public String getInterfaceName() { return mInterfaceName; }
210 
211     /**
212      * @return A list of addresses of this data connection.
213      */
214     @NonNull
getAddresses()215     public List<LinkAddress> getAddresses() { return mAddresses; }
216 
217     /**
218      * @return A list of DNS server addresses, e.g., "192.0.1.3" or
219      * "192.0.1.11 2001:db8::1". Empty list if no dns server addresses returned.
220      */
221     @NonNull
getDnsAddresses()222     public List<InetAddress> getDnsAddresses() { return mDnsAddresses; }
223 
224     /**
225      * @return A list of default gateway addresses, e.g., "192.0.1.3" or
226      * "192.0.1.11 2001:db8::1". Empty list if the addresses represent point to point connections.
227      */
228     @NonNull
getGatewayAddresses()229     public List<InetAddress> getGatewayAddresses() { return mGatewayAddresses; }
230 
231     /**
232      * @return A list of Proxy Call State Control Function address via PCO (Protocol Configuration
233      * Option) for IMS client.
234      */
235     @NonNull
getPcscfAddresses()236     public List<InetAddress> getPcscfAddresses() { return mPcscfAddresses; }
237 
238     /**
239      * @return MTU (maximum transmission unit) in bytes received from network. Zero or negative
240      * values means network has either not sent a value or sent an invalid value.
241      * @deprecated For IRadio 1.5 and up, use {@link #getMtuV4} or {@link #getMtuV6} instead.
242      */
243     @Deprecated
getMtu()244     public int getMtu() {
245         return mMtu;
246     }
247 
248     /**
249      * This replaces the deprecated method getMtu.
250      * @return MTU (maximum transmission unit) in bytes received from network, for IPv4.
251      * Zero or negative values means network has either not sent a value or sent an invalid value.
252      */
getMtuV4()253     public int getMtuV4() {
254         return mMtuV4;
255     }
256 
257     /**
258      * @return MTU (maximum transmission unit) in bytes received from network, for IPv6.
259      * Zero or negative values means network has either not sent a value or sent an invalid value.
260      */
getMtuV6()261     public int getMtuV6() {
262         return mMtuV6;
263     }
264 
265     @NonNull
266     @Override
toString()267     public String toString() {
268         StringBuffer sb = new StringBuffer();
269         sb.append("DataCallResponse: {")
270            .append(" cause=").append(mCause)
271            .append(" retry=").append(mSuggestedRetryTime)
272            .append(" cid=").append(mId)
273            .append(" linkStatus=").append(mLinkStatus)
274            .append(" protocolType=").append(mProtocolType)
275            .append(" ifname=").append(mInterfaceName)
276            .append(" addresses=").append(mAddresses)
277            .append(" dnses=").append(mDnsAddresses)
278            .append(" gateways=").append(mGatewayAddresses)
279            .append(" pcscf=").append(mPcscfAddresses)
280            .append(" mtu=").append(getMtu())
281            .append(" mtuV4=").append(getMtuV4())
282            .append(" mtuV6=").append(getMtuV6())
283            .append("}");
284         return sb.toString();
285     }
286 
287     @Override
equals(@ullable Object o)288     public boolean equals(@Nullable Object o) {
289         if (this == o) return true;
290 
291         if (!(o instanceof DataCallResponse)) {
292             return false;
293         }
294 
295         DataCallResponse other = (DataCallResponse) o;
296         return this.mCause == other.mCause
297                 && this.mSuggestedRetryTime == other.mSuggestedRetryTime
298                 && this.mId == other.mId
299                 && this.mLinkStatus == other.mLinkStatus
300                 && this.mProtocolType == other.mProtocolType
301                 && this.mInterfaceName.equals(other.mInterfaceName)
302                 && mAddresses.size() == other.mAddresses.size()
303                 && mAddresses.containsAll(other.mAddresses)
304                 && mDnsAddresses.size() == other.mDnsAddresses.size()
305                 && mDnsAddresses.containsAll(other.mDnsAddresses)
306                 && mGatewayAddresses.size() == other.mGatewayAddresses.size()
307                 && mGatewayAddresses.containsAll(other.mGatewayAddresses)
308                 && mPcscfAddresses.size() == other.mPcscfAddresses.size()
309                 && mPcscfAddresses.containsAll(other.mPcscfAddresses)
310                 && mMtu == other.mMtu
311                 && mMtuV4 == other.mMtuV4
312                 && mMtuV6 == other.mMtuV6;
313     }
314 
315     @Override
hashCode()316     public int hashCode() {
317         return Objects.hash(mCause, mSuggestedRetryTime, mId, mLinkStatus, mProtocolType,
318                 mInterfaceName, mAddresses, mDnsAddresses, mGatewayAddresses, mPcscfAddresses,
319                 mMtu, mMtuV4, mMtuV6);
320     }
321 
322     @Override
describeContents()323     public int describeContents() {
324         return 0;
325     }
326 
327     @Override
writeToParcel(Parcel dest, int flags)328     public void writeToParcel(Parcel dest, int flags) {
329         dest.writeInt(mCause);
330         dest.writeInt(mSuggestedRetryTime);
331         dest.writeInt(mId);
332         dest.writeInt(mLinkStatus);
333         dest.writeInt(mProtocolType);
334         dest.writeString(mInterfaceName);
335         dest.writeList(mAddresses);
336         dest.writeList(mDnsAddresses);
337         dest.writeList(mGatewayAddresses);
338         dest.writeList(mPcscfAddresses);
339         dest.writeInt(mMtu);
340         dest.writeInt(mMtuV4);
341         dest.writeInt(mMtuV6);
342     }
343 
344     public static final @android.annotation.NonNull Parcelable.Creator<DataCallResponse> CREATOR =
345             new Parcelable.Creator<DataCallResponse>() {
346                 @Override
347                 public DataCallResponse createFromParcel(Parcel source) {
348                     return new DataCallResponse(source);
349                 }
350 
351                 @Override
352                 public DataCallResponse[] newArray(int size) {
353                     return new DataCallResponse[size];
354                 }
355             };
356 
357     /**
358      * Provides a convenient way to set the fields of a {@link DataCallResponse} when creating a new
359      * instance.
360      *
361      * <p>The example below shows how you might create a new {@code DataCallResponse}:
362      *
363      * <pre><code>
364      *
365      * DataCallResponse response = new DataCallResponse.Builder()
366      *     .setAddresses(Arrays.asList("192.168.1.2"))
367      *     .setProtocolType(ApnSetting.PROTOCOL_IPV4V6)
368      *     .build();
369      * </code></pre>
370      */
371     public static final class Builder {
372         private @DataFailureCause int mCause;
373 
374         private int mSuggestedRetryTime;
375 
376         private int mId;
377 
378         private @LinkStatus int mLinkStatus;
379 
380         private @ProtocolType int mProtocolType;
381 
382         private String mInterfaceName;
383 
384         private List<LinkAddress> mAddresses;
385 
386         private List<InetAddress> mDnsAddresses;
387 
388         private List<InetAddress> mGatewayAddresses;
389 
390         private List<InetAddress> mPcscfAddresses;
391 
392         private int mMtu;
393 
394         private int mMtuV4;
395 
396         private int mMtuV6;
397 
398         /**
399          * Default constructor for Builder.
400          */
Builder()401         public Builder() {
402         }
403 
404         /**
405          * Set data call fail cause.
406          *
407          * @param cause Data call fail cause. {@link DataFailCause#NONE} indicates no error.
408          * @return The same instance of the builder.
409          */
setCause(@ataFailureCause int cause)410         public @NonNull Builder setCause(@DataFailureCause int cause) {
411             mCause = cause;
412             return this;
413         }
414 
415         /**
416          * Set the suggested data retry time.
417          *
418          * @param suggestedRetryTime The suggested data retry time in milliseconds.
419          * @return The same instance of the builder.
420          */
setSuggestedRetryTime(int suggestedRetryTime)421         public @NonNull Builder setSuggestedRetryTime(int suggestedRetryTime) {
422             mSuggestedRetryTime = suggestedRetryTime;
423             return this;
424         }
425 
426         /**
427          * Set the unique id of the data connection.
428          *
429          * @param id The unique id of the data connection.
430          * @return The same instance of the builder.
431          */
setId(int id)432         public @NonNull Builder setId(int id) {
433             mId = id;
434             return this;
435         }
436 
437         /**
438          * Set the link status
439          *
440          * @param linkStatus The link status
441          * @return The same instance of the builder.
442          */
setLinkStatus(@inkStatus int linkStatus)443         public @NonNull Builder setLinkStatus(@LinkStatus int linkStatus) {
444             mLinkStatus = linkStatus;
445             return this;
446         }
447 
448         /**
449          * Set the connection protocol type.
450          *
451          * @param protocolType The connection protocol type.
452          * @return The same instance of the builder.
453          */
setProtocolType(@rotocolType int protocolType)454         public @NonNull Builder setProtocolType(@ProtocolType int protocolType) {
455             mProtocolType = protocolType;
456             return this;
457         }
458 
459         /**
460          * Set the network interface name.
461          *
462          * @param interfaceName The network interface name (e.g. "rmnet_data1").
463          * @return The same instance of the builder.
464          */
setInterfaceName(@onNull String interfaceName)465         public @NonNull Builder setInterfaceName(@NonNull String interfaceName) {
466             mInterfaceName = interfaceName;
467             return this;
468         }
469 
470         /**
471          * Set the addresses of this data connection.
472          *
473          * @param addresses The list of address of the data connection.
474          * @return The same instance of the builder.
475          */
setAddresses(@onNull List<LinkAddress> addresses)476         public @NonNull Builder setAddresses(@NonNull List<LinkAddress> addresses) {
477             mAddresses = addresses;
478             return this;
479         }
480 
481         /**
482          * Set the DNS addresses of this data connection
483          *
484          * @param dnsAddresses The list of DNS address of the data connection.
485          * @return The same instance of the builder.
486          */
setDnsAddresses(@onNull List<InetAddress> dnsAddresses)487         public @NonNull Builder setDnsAddresses(@NonNull List<InetAddress> dnsAddresses) {
488             mDnsAddresses = dnsAddresses;
489             return this;
490         }
491 
492         /**
493          * Set the gateway addresses of this data connection
494          *
495          * @param gatewayAddresses The list of gateway address of the data connection.
496          * @return The same instance of the builder.
497          */
setGatewayAddresses(@onNull List<InetAddress> gatewayAddresses)498         public @NonNull Builder setGatewayAddresses(@NonNull List<InetAddress> gatewayAddresses) {
499             mGatewayAddresses = gatewayAddresses;
500             return this;
501         }
502 
503         /**
504          * Set the Proxy Call State Control Function address via PCO(Protocol Configuration
505          * Option) for IMS client.
506          *
507          * @param pcscfAddresses The list of pcscf address of the data connection.
508          * @return The same instance of the builder.
509          */
setPcscfAddresses(@onNull List<InetAddress> pcscfAddresses)510         public @NonNull Builder setPcscfAddresses(@NonNull List<InetAddress> pcscfAddresses) {
511             mPcscfAddresses = pcscfAddresses;
512             return this;
513         }
514 
515         /**
516          * Set maximum transmission unit of the data connection.
517          *
518          * @param mtu MTU (maximum transmission unit) in bytes received from network. Zero or
519          * negative values means network has either not sent a value or sent an invalid value.
520          *
521          * @return The same instance of the builder.
522          * @deprecated For IRadio 1.5 and up, use {@link #setMtuV4} or {@link #setMtuV6} instead.
523          */
setMtu(int mtu)524         public @NonNull Builder setMtu(int mtu) {
525             mMtu = mtu;
526             return this;
527         }
528 
529         /**
530          * Set maximum transmission unit of the data connection, for IPv4.
531          *
532          * @param mtu MTU (maximum transmission unit) in bytes received from network. Zero or
533          * negative values means network has either not sent a value or sent an invalid value.
534          *
535          * @return The same instance of the builder.
536          */
setMtuV4(int mtu)537         public @NonNull Builder setMtuV4(int mtu) {
538             mMtuV4 = mtu;
539             return this;
540         }
541 
542         /**
543          * Set maximum transmission unit of the data connection, for IPv6.
544          *
545          * @param mtu MTU (maximum transmission unit) in bytes received from network. Zero or
546          * negative values means network has either not sent a value or sent an invalid value.
547          *
548          * @return The same instance of the builder.
549          */
setMtuV6(int mtu)550         public @NonNull Builder setMtuV6(int mtu) {
551             mMtuV6 = mtu;
552             return this;
553         }
554 
555         /**
556          * Build the DataCallResponse.
557          *
558          * @return the DataCallResponse object.
559          */
build()560         public @NonNull DataCallResponse build() {
561             return new DataCallResponse(mCause, mSuggestedRetryTime, mId, mLinkStatus,
562                     mProtocolType, mInterfaceName, mAddresses, mDnsAddresses, mGatewayAddresses,
563                     mPcscfAddresses, mMtu, mMtuV4, mMtuV6);
564         }
565     }
566 }
567