1 /*
2  * Copyright (C) 2020 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 android.net;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * Provides identifying information of a QoS session.  Sent to an application through
30  * {@link QosCallback}.
31  *
32  * @hide
33  */
34 @SystemApi
35 public final class QosSession implements Parcelable {
36 
37     /**
38      * The {@link QosSession} is a LTE EPS Session.
39      */
40     public static final int TYPE_EPS_BEARER = 1;
41 
42     /**
43      * The {@link QosSession} is a NR Session.
44      */
45     public static final int TYPE_NR_BEARER = 2;
46 
47     private final int mSessionId;
48 
49     private final int mSessionType;
50 
51     /**
52      * Gets the unique id of the session that is used to differentiate sessions across different
53      * types.
54      * <p/>
55      * Note: Different qos sessions can be provided by different actors.
56      *
57      * @return the unique id
58      */
getUniqueId()59     public long getUniqueId() {
60         return (long) mSessionType << 32 | mSessionId;
61     }
62 
63     /**
64      * Gets the {@link QosSession} identifier which is set by the actor providing the QoS.
65      * <p/>
66      * Note: It can be either manufactured by the actor, but also may have a particular meaning
67      * within that type.  For example, using the bearer id as the session id for
68      * {@link android.telephony.data.EpsBearerQosSessionAttributes} is a straight forward way to
69      * keep the sessions unique from one another within that type.
70      *
71      * @return the id of the session
72      */
getSessionId()73     public int getSessionId() {
74         return mSessionId;
75     }
76 
77     /**
78      * Gets the type of session.
79      */
80     @QosSessionType
getSessionType()81     public int getSessionType() {
82         return mSessionType;
83     }
84 
85     /**
86      * Creates a {@link QosSession}.
87      *
88      * @param sessionId uniquely identifies the session across all sessions of the same type
89      * @param sessionType the type of session
90      */
QosSession(final int sessionId, @QosSessionType final int sessionType)91     public QosSession(final int sessionId, @QosSessionType final int sessionType) {
92         //Ensures the session id is unique across types of sessions
93         mSessionId = sessionId;
94         mSessionType = sessionType;
95     }
96 
97 
98     @Override
toString()99     public String toString() {
100         return "QosSession{"
101                 + "mSessionId=" + mSessionId
102                 + ", mSessionType=" + mSessionType
103                 + '}';
104     }
105 
106     /**
107      * Annotations for types of qos sessions.
108      */
109     @IntDef(value = {
110             TYPE_EPS_BEARER,
111             TYPE_NR_BEARER,
112     })
113     @Retention(RetentionPolicy.SOURCE)
114     @interface QosSessionType {}
115 
QosSession(final Parcel in)116     private QosSession(final Parcel in) {
117         mSessionId = in.readInt();
118         mSessionType = in.readInt();
119     }
120 
121     @NonNull
122     public static final Creator<QosSession> CREATOR = new Creator<QosSession>() {
123         @NonNull
124         @Override
125         public QosSession createFromParcel(@NonNull final Parcel in) {
126             return new QosSession(in);
127         }
128 
129         @NonNull
130         @Override
131         public QosSession[] newArray(final int size) {
132             return new QosSession[size];
133         }
134     };
135 
136     @Override
describeContents()137     public int describeContents() {
138         return 0;
139     }
140 
141     @Override
writeToParcel(@onNull final Parcel dest, final int flags)142     public void writeToParcel(@NonNull final Parcel dest, final int flags) {
143         dest.writeInt(mSessionId);
144         dest.writeInt(mSessionType);
145     }
146 }
147