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.util.Log;
20
21/**
22 * A representation of a single publish or subscribe NAN session. This object
23 * will not be created directly - only its child classes are available:
24 * {@link WifiNanPublishSession} and {@link WifiNanSubscribeSession}.
25 *
26 * @hide PROPOSED_NAN_API
27 */
28public class WifiNanSession {
29    private static final String TAG = "WifiNanSession";
30    private static final boolean DBG = false;
31    private static final boolean VDBG = false; // STOPSHIP if true
32
33    /**
34     * {@hide}
35     */
36    protected WifiNanManager mManager;
37
38    /**
39     * {@hide}
40     */
41    protected int mSessionId;
42
43    /**
44     * {@hide}
45     */
46    private boolean mDestroyed;
47
48    /**
49     * {@hide}
50     */
51    public WifiNanSession(WifiNanManager manager, int sessionId) {
52        if (VDBG) Log.v(TAG, "New client created: manager=" + manager + ", sessionId=" + sessionId);
53
54        mManager = manager;
55        mSessionId = sessionId;
56        mDestroyed = false;
57    }
58
59    /**
60     * Terminate the current publish or subscribe session - i.e. stop
61     * transmitting packet on-air (for an active session) or listening for
62     * matches (for a passive session). Note that the session may still receive
63     * incoming messages and may be re-configured/re-started at a later time.
64     */
65    public void stop() {
66        mManager.stopSession(mSessionId);
67    }
68
69    /**
70     * Destroy the current publish or subscribe session. Performs a
71     * {@link WifiNanSession#stop()} function but in addition destroys the session -
72     * it will not be able to receive any messages or to be restarted at a later
73     * time.
74     */
75    public void destroy() {
76        mManager.destroySession(mSessionId);
77        mDestroyed = true;
78    }
79
80    /**
81     * {@hide}
82     */
83    @Override
84    protected void finalize() throws Throwable {
85        if (!mDestroyed) {
86            Log.w(TAG, "WifiNanSession mSessionId=" + mSessionId
87                            + " was not explicitly destroyed. The session may use resources until "
88                            + "destroyed so step should be done explicitly");
89        }
90        destroy();
91    }
92
93    /**
94     * Sends a message to the specified destination. Message transmission is
95     * part of the current discovery session - i.e. executed subsequent to a
96     * publish/subscribe
97     * {@link WifiNanSessionListener#onMatch(int, byte[], int, byte[], int)}
98     * event.
99     *
100     * @param peerId The peer's ID for the message. Must be a result of an
101     *            {@link WifiNanSessionListener#onMatch(int, byte[], int, byte[], int)}
102     *            event.
103     * @param message The message to be transmitted.
104     * @param messageLength The number of bytes from the {@code message} to be
105     *            transmitted.
106     * @param messageId An arbitrary integer used by the caller to identify the
107     *            message. The same integer ID will be returned in the callbacks
108     *            indicated message send success or failure.
109     */
110    public void sendMessage(int peerId, byte[] message, int messageLength, int messageId) {
111        mManager.sendMessage(mSessionId, peerId, message, messageLength, messageId);
112    }
113}
114