WifiAwareNativeManager.java revision 3145bb4d3359647cccd9b4aba1734066f36bb2e6
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 com.android.server.wifi.aware;
18
19import android.hardware.wifi.V1_0.IWifiNanIface;
20import android.util.Log;
21
22import com.android.server.wifi.HalDeviceManager;
23
24/**
25 * Manages the interface to Wi-Fi Aware HIDL (HAL).
26 */
27class WifiAwareNativeManager {
28    private static final String TAG = "WifiAwareNativeManager";
29    private static final boolean DBG = false;
30
31    // to be used for synchronizing access to any of the WifiAwareNative objects
32    private final Object mLock = new Object();
33
34    private WifiAwareStateManager mWifiAwareStateManager;
35    private HalDeviceManager mHalDeviceManager;
36    private IWifiNanIface mWifiNanIface = null;
37    private InterfaceDestroyedListener mInterfaceDestroyedListener =
38            new InterfaceDestroyedListener();
39    private InterfaceAvailableForRequestListener mInterfaceAvailableForRequestListener =
40            new InterfaceAvailableForRequestListener();
41
42    WifiAwareNativeManager(WifiAwareStateManager awareStateManager,
43            HalDeviceManager halDeviceManager) {
44        mWifiAwareStateManager = awareStateManager;
45        mHalDeviceManager = halDeviceManager;
46        mHalDeviceManager.registerStatusListener(
47                new HalDeviceManager.ManagerStatusListener() {
48                    @Override
49                    public void onStatusChanged() {
50                        if (DBG) Log.d(TAG, "onStatusChanged");
51                        // only care about isStarted (Wi-Fi started) not isReady - since if not
52                        // ready then Wi-Fi will also be down.
53                        if (mHalDeviceManager.isStarted()) {
54                            tryToGetAware();
55                        } else {
56                            awareIsDown();
57                        }
58                    }
59                }, null);
60        if (mHalDeviceManager.isStarted()) {
61            tryToGetAware();
62        }
63    }
64
65    /* package */ IWifiNanIface getWifiNanIface() {
66        synchronized (mLock) {
67            return mWifiNanIface;
68        }
69    }
70
71    private void tryToGetAware() {
72        synchronized (mLock) {
73            if (mWifiNanIface != null) {
74                if (DBG) Log.d(TAG, "Already have a NAN interface");
75                return;
76            }
77            IWifiNanIface iface = mHalDeviceManager.createNanIface(
78                    mInterfaceDestroyedListener, mInterfaceAvailableForRequestListener, null);
79            if (iface == null) {
80                if (DBG) Log.d(TAG, "Was not able to obtain an IWifiNanIface");
81            } else {
82                if (DBG) Log.d(TAG, "Obtained an IWifiNanIface");
83
84                mWifiNanIface = iface;
85                mWifiAwareStateManager.enableUsage();
86            }
87        }
88    }
89
90    private void awareIsDown() {
91        if (mWifiNanIface != null) {
92            synchronized (mLock) {
93                mWifiNanIface = null;
94                mWifiAwareStateManager.disableUsage();
95            }
96        }
97    }
98
99    private class InterfaceDestroyedListener implements
100            HalDeviceManager.InterfaceDestroyedListener {
101        @Override
102        public void onDestroyed() {
103            if (DBG) Log.d(TAG, "Interface was destroyed");
104            awareIsDown();
105        }
106    }
107
108    private class InterfaceAvailableForRequestListener implements
109            HalDeviceManager.InterfaceAvailableForRequestListener {
110        @Override
111        public void onAvailableForRequest() {
112            if (DBG) Log.d(TAG, "Interface is possibly available");
113            tryToGetAware();
114        }
115    }
116}
117