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