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