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 com.android.server.wifi.nan;
18 
19 import android.net.wifi.nan.ConfigRequest;
20 import android.net.wifi.nan.IWifiNanEventListener;
21 import android.net.wifi.nan.IWifiNanSessionListener;
22 import android.net.wifi.nan.WifiNanEventListener;
23 import android.os.RemoteException;
24 import android.util.Log;
25 import android.util.SparseArray;
26 
27 import java.io.FileDescriptor;
28 import java.io.PrintWriter;
29 
30 public class WifiNanClientState {
31     private static final String TAG = "WifiNanClientState";
32     private static final boolean DBG = false;
33     private static final boolean VDBG = false; // STOPSHIP if true
34 
35     /* package */ static final int CLUSTER_CHANGE_EVENT_STARTED = 0;
36     /* package */ static final int CLUSTER_CHANGE_EVENT_JOINED = 1;
37 
38     private IWifiNanEventListener mListener;
39     private int mEvents;
40     private final SparseArray<WifiNanSessionState> mSessions = new SparseArray<>();
41 
42     private int mUid;
43     private ConfigRequest mConfigRequest;
44 
WifiNanClientState(int uid, IWifiNanEventListener listener, int events)45     public WifiNanClientState(int uid, IWifiNanEventListener listener, int events) {
46         mUid = uid;
47         mListener = listener;
48         mEvents = events;
49     }
50 
destroy()51     public void destroy() {
52         mListener = null;
53         for (int i = 0; i < mSessions.size(); ++i) {
54             mSessions.valueAt(i).destroy();
55         }
56         mSessions.clear();
57         mConfigRequest = null;
58     }
59 
setConfigRequest(ConfigRequest configRequest)60     public void setConfigRequest(ConfigRequest configRequest) {
61         mConfigRequest = configRequest;
62     }
63 
getConfigRequest()64     public ConfigRequest getConfigRequest() {
65         return mConfigRequest;
66     }
67 
getUid()68     public int getUid() {
69         return mUid;
70     }
71 
getNanSessionStateForPubSubId(int pubSubId)72     public WifiNanSessionState getNanSessionStateForPubSubId(int pubSubId) {
73         for (int i = 0; i < mSessions.size(); ++i) {
74             WifiNanSessionState session = mSessions.valueAt(i);
75             if (session.isPubSubIdSession(pubSubId)) {
76                 return session;
77             }
78         }
79 
80         return null;
81     }
82 
createSession(int sessionId, IWifiNanSessionListener listener, int events)83     public void createSession(int sessionId, IWifiNanSessionListener listener, int events) {
84         WifiNanSessionState session = mSessions.get(sessionId);
85         if (session != null) {
86             Log.e(TAG, "createSession: sessionId already exists (replaced) - " + sessionId);
87         }
88 
89         mSessions.put(sessionId, new WifiNanSessionState(sessionId, listener, events));
90     }
91 
destroySession(int sessionId)92     public void destroySession(int sessionId) {
93         WifiNanSessionState session = mSessions.get(sessionId);
94         if (session == null) {
95             Log.e(TAG, "destroySession: sessionId doesn't exist - " + sessionId);
96             return;
97         }
98 
99         mSessions.delete(sessionId);
100         session.destroy();
101     }
102 
getSession(int sessionId)103     public WifiNanSessionState getSession(int sessionId) {
104         return mSessions.get(sessionId);
105     }
106 
onConfigCompleted(ConfigRequest completedConfig)107     public void onConfigCompleted(ConfigRequest completedConfig) {
108         if (mListener != null && (mEvents & WifiNanEventListener.LISTEN_CONFIG_COMPLETED) != 0) {
109             try {
110                 mListener.onConfigCompleted(completedConfig);
111             } catch (RemoteException e) {
112                 Log.w(TAG, "onConfigCompleted: RemoteException - ignored: " + e);
113             }
114         }
115     }
116 
onConfigFailed(ConfigRequest failedConfig, int reason)117     public void onConfigFailed(ConfigRequest failedConfig, int reason) {
118         if (mListener != null && (mEvents & WifiNanEventListener.LISTEN_CONFIG_FAILED) != 0) {
119             try {
120                 mListener.onConfigFailed(failedConfig, reason);
121             } catch (RemoteException e) {
122                 Log.w(TAG, "onConfigFailed: RemoteException - ignored: " + e);
123             }
124         }
125     }
126 
onNanDown(int reason)127     public int onNanDown(int reason) {
128         if (mListener != null && (mEvents & WifiNanEventListener.LISTEN_NAN_DOWN) != 0) {
129             try {
130                 mListener.onNanDown(reason);
131             } catch (RemoteException e) {
132                 Log.w(TAG, "onNanDown: RemoteException - ignored: " + e);
133             }
134 
135             return 1;
136         }
137 
138         return 0;
139     }
140 
onInterfaceAddressChange(byte[] mac)141     public int onInterfaceAddressChange(byte[] mac) {
142         if (mListener != null && (mEvents & WifiNanEventListener.LISTEN_IDENTITY_CHANGED) != 0) {
143             try {
144                 mListener.onIdentityChanged();
145             } catch (RemoteException e) {
146                 Log.w(TAG, "onIdentityChanged: RemoteException - ignored: " + e);
147             }
148 
149             return 1;
150         }
151 
152         return 0;
153     }
154 
onClusterChange(int flag, byte[] mac)155     public int onClusterChange(int flag, byte[] mac) {
156         if (mListener != null && (mEvents & WifiNanEventListener.LISTEN_IDENTITY_CHANGED) != 0) {
157             try {
158                 mListener.onIdentityChanged();
159             } catch (RemoteException e) {
160                 Log.w(TAG, "onIdentityChanged: RemoteException - ignored: " + e);
161             }
162 
163             return 1;
164         }
165 
166         return 0;
167     }
168 
dump(FileDescriptor fd, PrintWriter pw, String[] args)169     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
170         pw.println("NanClientState:");
171         pw.println("  mUid: " + mUid);
172         pw.println("  mConfigRequest: " + mConfigRequest);
173         pw.println("  mListener: " + mListener);
174         pw.println("  mEvents: " + mEvents);
175         pw.println("  mSessions: [" + mSessions + "]");
176         for (int i = 0; i < mSessions.size(); ++i) {
177             mSessions.valueAt(i).dump(fd, pw, args);
178         }
179     }
180 }
181