1 /*
2  * Copyright (C) 2016 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.wifi.nan;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 /**
23  * Defines the settings (configuration) for a NAN subscribe session. Built using
24  * {@link SubscribeSettings.Builder}. Subscribe is done using
25  * {@link WifiNanManager#subscribe(SubscribeData, SubscribeSettings, WifiNanSessionListener, int)}
26  * or {@link WifiNanSubscribeSession#subscribe(SubscribeData, SubscribeSettings)}.
27  *
28  * @hide PROPOSED_NAN_API
29  */
30 public class SubscribeSettings implements Parcelable {
31 
32     /**
33      * Defines a passive subscribe session - i.e. a subscribe session where
34      * subscribe packets are not transmitted over-the-air and the device listens
35      * and matches to transmitted publish packets. Configuration is done using
36      * {@link SubscribeSettings.Builder#setSubscribeType(int)}.
37      */
38     public static final int SUBSCRIBE_TYPE_PASSIVE = 0;
39 
40     /**
41      * Defines an active subscribe session - i.e. a subscribe session where
42      * subscribe packets are transmitted over-the-air. Configuration is done
43      * using {@link SubscribeSettings.Builder#setSubscribeType(int)}.
44      */
45     public static final int SUBSCRIBE_TYPE_ACTIVE = 1;
46 
47     /**
48      * @hide
49      */
50     public final int mSubscribeType;
51 
52     /**
53      * @hide
54      */
55     public final int mSubscribeCount;
56 
57     /**
58      * @hide
59      */
60     public final int mTtlSec;
61 
SubscribeSettings(int subscribeType, int publichCount, int ttlSec)62     private SubscribeSettings(int subscribeType, int publichCount, int ttlSec) {
63         mSubscribeType = subscribeType;
64         mSubscribeCount = publichCount;
65         mTtlSec = ttlSec;
66     }
67 
68     @Override
toString()69     public String toString() {
70         return "SubscribeSettings [mSubscribeType=" + mSubscribeType + ", mSubscribeCount="
71                 + mSubscribeCount + ", mTtlSec=" + mTtlSec + "]";
72     }
73 
74     @Override
describeContents()75     public int describeContents() {
76         return 0;
77     }
78 
79     @Override
writeToParcel(Parcel dest, int flags)80     public void writeToParcel(Parcel dest, int flags) {
81         dest.writeInt(mSubscribeType);
82         dest.writeInt(mSubscribeCount);
83         dest.writeInt(mTtlSec);
84     }
85 
86     public static final Creator<SubscribeSettings> CREATOR = new Creator<SubscribeSettings>() {
87         @Override
88         public SubscribeSettings[] newArray(int size) {
89             return new SubscribeSettings[size];
90         }
91 
92         @Override
93         public SubscribeSettings createFromParcel(Parcel in) {
94             int subscribeType = in.readInt();
95             int subscribeCount = in.readInt();
96             int ttlSec = in.readInt();
97             return new SubscribeSettings(subscribeType, subscribeCount, ttlSec);
98         }
99     };
100 
101     @Override
equals(Object o)102     public boolean equals(Object o) {
103         if (this == o) {
104             return true;
105         }
106 
107         if (!(o instanceof SubscribeSettings)) {
108             return false;
109         }
110 
111         SubscribeSettings lhs = (SubscribeSettings) o;
112 
113         return mSubscribeType == lhs.mSubscribeType && mSubscribeCount == lhs.mSubscribeCount
114                 && mTtlSec == lhs.mTtlSec;
115     }
116 
117     @Override
hashCode()118     public int hashCode() {
119         int result = 17;
120 
121         result = 31 * result + mSubscribeType;
122         result = 31 * result + mSubscribeCount;
123         result = 31 * result + mTtlSec;
124 
125         return result;
126     }
127 
128     /**
129      * Builder used to build {@link SubscribeSettings} objects.
130      */
131     public static final class Builder {
132         int mSubscribeType;
133         int mSubscribeCount;
134         int mTtlSec;
135 
136         /**
137          * Sets the type of the subscribe session: active (subscribe packets are
138          * transmitted over-the-air), or passive (no subscribe packets are
139          * transmitted, a match is made against a solicited/active publish
140          * session whose packets are transmitted over-the-air).
141          *
142          * @param subscribeType Subscribe session type: active (
143          *            {@link SubscribeSettings#SUBSCRIBE_TYPE_ACTIVE}) or
144          *            passive ( {@link SubscribeSettings#SUBSCRIBE_TYPE_PASSIVE}
145          *            ).
146          * @return The builder to facilitate chaining
147          *         {@code builder.setXXX(..).setXXX(..)}.
148          */
setSubscribeType(int subscribeType)149         public Builder setSubscribeType(int subscribeType) {
150             if (subscribeType < SUBSCRIBE_TYPE_PASSIVE || subscribeType > SUBSCRIBE_TYPE_ACTIVE) {
151                 throw new IllegalArgumentException("Invalid subscribeType - " + subscribeType);
152             }
153             mSubscribeType = subscribeType;
154             return this;
155         }
156 
157         /**
158          * Sets the number of times an active (
159          * {@link SubscribeSettings.Builder#setSubscribeType(int)}) subscribe
160          * session will transmit a packet. When the count is reached an event
161          * will be generated for
162          * {@link WifiNanSessionListener#onSubscribeTerminated(int)} with reason=
163          * {@link WifiNanSessionListener#TERMINATE_REASON_DONE}.
164          *
165          * @param subscribeCount Number of subscribe packets to transmit.
166          * @return The builder to facilitate chaining
167          *         {@code builder.setXXX(..).setXXX(..)}.
168          */
setSubscribeCount(int subscribeCount)169         public Builder setSubscribeCount(int subscribeCount) {
170             if (subscribeCount < 0) {
171                 throw new IllegalArgumentException("Invalid subscribeCount - must be non-negative");
172             }
173             mSubscribeCount = subscribeCount;
174             return this;
175         }
176 
177         /**
178          * Sets the time interval (in seconds) an active (
179          * {@link SubscribeSettings.Builder#setSubscribeType(int)}) subscribe
180          * session will be alive - i.e. transmitting a packet. When the TTL is
181          * reached an event will be generated for
182          * {@link WifiNanSessionListener#onSubscribeTerminated(int)} with reason=
183          * {@link WifiNanSessionListener#TERMINATE_REASON_DONE}.
184          *
185          * @param ttlSec Lifetime of a subscribe session in seconds.
186          * @return The builder to facilitate chaining
187          *         {@code builder.setXXX(..).setXXX(..)}.
188          */
setTtlSec(int ttlSec)189         public Builder setTtlSec(int ttlSec) {
190             if (ttlSec < 0) {
191                 throw new IllegalArgumentException("Invalid ttlSec - must be non-negative");
192             }
193             mTtlSec = ttlSec;
194             return this;
195         }
196 
197         /**
198          * Build {@link SubscribeSettings} given the current requests made on
199          * the builder.
200          */
build()201         public SubscribeSettings build() {
202             return new SubscribeSettings(mSubscribeType, mSubscribeCount, mTtlSec);
203         }
204     }
205 }
206