WifiNative.java revision 0fcb06473cbe2824e401a80c0520bb1c14ed8f41
1155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/*
2155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Copyright (C) 2008 The Android Open Source Project
3155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
4155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Licensed under the Apache License, Version 2.0 (the "License");
5155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * you may not use this file except in compliance with the License.
6155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * You may obtain a copy of the License at
7155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
8155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *      http://www.apache.org/licenses/LICENSE-2.0
9155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
10155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Unless required by applicable law or agreed to in writing, software
11155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * distributed under the License is distributed on an "AS IS" BASIS,
12155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * See the License for the specific language governing permissions and
14155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * limitations under the License.
15155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
16155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepackage com.android.server.wifi;
18155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.annotation.Nullable;
20fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.app.AlarmManager;
21fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.app.PendingIntent;
22fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.content.BroadcastReceiver;
232a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.Context;
242a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.Intent;
252a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.IntentFilter;
26e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensenimport android.net.apf.ApfCapabilities;
27143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpandeimport android.net.wifi.RttManager;
2868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.net.wifi.RttManager.ResponderConfig;
29e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
30dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiConfiguration;
3118786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.net.wifi.WifiEnterpriseConfig;
32aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
3303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.WifiManager;
34e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
35dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiSsid;
362a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.net.wifi.WifiWakeReasonAndCounts;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
39155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
4003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
41f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
4218786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.os.SystemProperties;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
46fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
4709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawalimport com.android.internal.annotations.Immutable;
480fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawalimport com.android.internal.util.HexDump;
49c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadhamimport com.android.server.connectivity.KeepalivePacketData;
502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.NetworkDetail;
512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.SupplicantBridge;
522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.Utils;
535d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Willsimport com.android.server.wifi.util.InformationElementUtil;
54c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
5518786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport libcore.util.HexEncoding;
5618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
57fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONException;
58fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONObject;
59fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
600fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawalimport java.io.PrintWriter;
610fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawalimport java.io.StringWriter;
62fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.io.UnsupportedEncodingException;
63fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLDecoder;
64fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLEncoder;
655cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.ByteBuffer;
665cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.CharBuffer;
675cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharacterCodingException;
685cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharsetDecoder;
695cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.StandardCharsets;
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
713571366ac36c70746b9f013ec2b54482861c9292Randy Panimport java.util.BitSet;
72fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.HashMap;
73fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Iterator;
74155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
76fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Map;
779ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport java.util.Set;
7818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
79fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
90b66b29a00da970ee75052e24f592c8d6c16bd0edxinhe    private static boolean DBG = false;
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
920fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    // Must match wifi_hal.h
930fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public static final int WIFI_SUCCESS = 0;
940fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public static final int WIFI_ERROR_UNINITIALIZED = -2;
950fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
9618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
9718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Hold this lock before calling supplicant or HAL methods
9818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * it is required to mutually exclude access to the driver
9918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
100956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static final Object sLock = new Object();
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
102956f54b391677d78379729dd14518edddf3c7660Etan Cohen    private static final LocalLog sLocalLog = new LocalLog(16384);
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static LocalLog getLocalLog() {
105956f54b391677d78379729dd14518edddf3c7660Etan Cohen        return sLocalLog;
10618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
11718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
11818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Singleton WifiNative instances
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
12018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative wlanNativeInterface =
121c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            new WifiNative(SystemProperties.get("wifi.interface", "wlan0"), true);
12218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getWlanNativeInterface() {
12318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return wlanNativeInterface;
12418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
126c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang    private static WifiNative p2pNativeInterface =
127c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            // commands for p2p0 interface don't need prefix
128c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            new WifiNative(SystemProperties.get("wifi.direct.interface", "p2p0"), false);
12918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getP2pNativeInterface() {
13018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return p2pNativeInterface;
13118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mTAG;
13518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfaceName;
13618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfacePrefix;
13718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
13818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private Context mContext = null;
13918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private PnoMonitor mPnoMonitor = null;
14018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void initContext(Context context) {
14118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (mContext == null && context != null) {
14218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mContext = context;
14318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mPnoMonitor = new PnoMonitor();
14418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
14518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
147c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang    private WifiNative(String interfaceName,
148c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang                       boolean requiresPrefix) {
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
151e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
152c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang        if (requiresPrefix) {
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName() {
16018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return mInterfaceName;
161e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
162e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
16318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Note this affects logging on for all interfaces
164ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
165ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
166ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
167ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
168ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
169ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
170ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
171ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
17218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private void localLog(String s) {
173956f54b391677d78379729dd14518edddf3c7660Etan Cohen        if (sLocalLog != null) sLocalLog.log(mInterfaceName + ": " + s);
17418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
17818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
17918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Driver and Supplicant management
18018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
18118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean loadDriverNative();
18218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean loadDriver() {
183956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return loadDriverNative();
18518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
18818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean isDriverLoadedNative();
18918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isDriverLoaded() {
190956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return isDriverLoadedNative();
19218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
19518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean unloadDriverNative();
19618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean unloadDriver() {
197956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return unloadDriverNative();
19918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
20018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
20118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
20218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean startSupplicantNative(boolean p2pSupported);
20318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startSupplicant(boolean p2pSupported) {
204956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
20518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return startSupplicantNative(p2pSupported);
20618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
20718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
20818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
20918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* Sends a kill signal to supplicant. To be used when we have lost connection
21018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills       or when the supplicant is hung */
21118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean killSupplicantNative(boolean p2pSupported);
21218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean killSupplicant(boolean p2pSupported) {
213956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
21418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return killSupplicantNative(p2pSupported);
21518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
21818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean connectToSupplicantNative();
219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
220956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
221b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "connectToSupplicant");
222b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            return connectToSupplicantNative();
223b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
22618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static void closeSupplicantConnectionNative();
227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
228956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
229b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "closeSupplicantConnection");
230b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            closeSupplicantConnectionNative();
231b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
23418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
23518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wait for the supplicant to send an event, returning the event string.
23618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * @return the event string sent by the supplicant.
23718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
23818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static String waitForEventNative();
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
24418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
24618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Supplicant Command Primitives
24718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
24818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native boolean doBooleanCommandNative(String command);
24918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
25018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native int doIntCommandNative(String command);
25118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
25218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native String doStringCommandNative(String command);
25318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
256956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
25718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
2597b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
2600888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
265a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    private boolean doBooleanCommandWithoutLogging(String command) {
266a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (DBG) Log.d(mTAG, "doBooleanCommandWithoutLogging: " + command);
267956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
268a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
269a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            if (DBG) Log.d(mTAG, command + ": returned " + result);
270a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return result;
271a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
272a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    }
273a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
276956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
27718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
2797b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
2860888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2870888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2880888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2890888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2900888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
2910888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
292956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
29318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
29540ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
29640ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
29740ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
2987b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (!command.startsWith("STATUS-")) {
2997b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog(toLog + " -> " + result);
3007b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
30140ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
30240ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
3080888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
3090888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
3100888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
3110888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
3120888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
31327355a942653264388e909a4276196ee63e57811vandwalle        }
314956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
31918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String doCustomSupplicantCommand(String command) {
32018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doStringCommand(command);
32118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
32218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
32318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
32418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wrappers for supplicant commands
32518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
331ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
332ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
333ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
334ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
335a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
336a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
337a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
338a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
3393d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    /**
3403d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * Create a comma separate string from integer set.
3413d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param values List of integers.
3423d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @return comma separated string.
3433d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     */
3443d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    private static String createCSVStringFromIntegerSet(Set<Integer> values) {
3453d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        StringBuilder list = new StringBuilder();
3463d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        boolean first = true;
3473d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        for (Integer value : values) {
3483d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            if (!first) {
3493d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius                list.append(",");
3503d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            }
3513d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            list.append(value);
3523d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            first = false;
3533d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3543d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return list.toString();
3553d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    }
35618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
357ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    /**
358ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * Start a scan using wpa_supplicant for the given frequencies.
3593d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param freqs list of frequencies to scan for, if null scan all supported channels.
3603d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param hiddenNetworkIds List of hidden networks to be scanned for.
361ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     */
3623d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    public boolean scan(Set<Integer> freqs, Set<Integer> hiddenNetworkIds) {
3633d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String freqList = null;
3643d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String hiddenNetworkIdList = null;
3653d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqs != null && freqs.size() != 0) {
3663d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            freqList = createCSVStringFromIntegerSet(freqs);
3673d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3683d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIds != null && hiddenNetworkIds.size() != 0) {
3693d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            hiddenNetworkIdList = createCSVStringFromIntegerSet(hiddenNetworkIds);
3709ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3713d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return scanWithParams(freqList, hiddenNetworkIdList);
3729ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
3739ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
3743d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    private boolean scanWithParams(String freqList, String hiddenNetworkIdList) {
3753d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        StringBuilder scanCommand = new StringBuilder();
3763d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        scanCommand.append("SCAN TYPE=ONLY");
3773d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqList != null) {
3783d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" freq=" + freqList);
3793d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3803d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIdList != null) {
3813d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" scan_id=" + hiddenNetworkIdList);
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3833d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return doBooleanCommand(scanCommand.toString());
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
400e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
401e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
402e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
403e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
408fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public boolean setNetworkExtra(int netId, String name, Map<String, String> values) {
409fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String encoded;
410fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
411fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            encoded = URLEncoder.encode(new JSONObject(values).toString(), "UTF-8");
412fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (NullPointerException e) {
413fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
414fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
415fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
416fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
417fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
418fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
419fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        return setNetworkVariable(netId, name, "\"" + encoded + "\"");
420fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
421fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
424a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (name.equals(WifiConfiguration.pskVarName)
425a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham                || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
426a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value);
427a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        } else {
428a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
429a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
432fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public Map<String, String> getNetworkExtra(int netId, String name) {
433fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String wrapped = getNetworkVariable(netId, name);
434fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        if (wrapped == null || !wrapped.startsWith("\"") || !wrapped.endsWith("\"")) {
435fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
436fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
437fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
438fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final String encoded = wrapped.substring(1, wrapped.length() - 1);
439fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This method reads a JSON dictionary that was written by setNetworkExtra(). However,
440fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // on devices that upgraded from Marshmallow, it may encounter a legacy value instead -
441fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // an FQDN stored as a plain string. If such a value is encountered, the JSONObject
442fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // constructor will thrown a JSONException and the method will return null.
443fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final JSONObject json = new JSONObject(URLDecoder.decode(encoded, "UTF-8"));
444fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Map<String, String> values = new HashMap<String, String>();
445fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Iterator<?> it = json.keys();
446fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            while (it.hasNext()) {
447fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final String key = (String) it.next();
448fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final Object value = json.get(key);
449fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                if (value instanceof String) {
450fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                    values.put(key, (String) value);
451fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                }
452fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            }
453fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return values;
454fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
45564697f7a5b9c1c39f8c9d9c225b8ca4c798422c2Samuel Tan            Log.e(TAG, "Unable to deserialize networkExtra: " + e.toString());
456fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
457fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (JSONException e) {
458fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This is not necessarily an error. This exception will also occur if we encounter a
459fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // legacy FQDN stored as a plain string. We want to return null in this case as no JSON
460fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // dictionary of extras was found.
461fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
462fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
463fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
464fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
476f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
480ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
481ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
482ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
483ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
484ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
485ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
486f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
487f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
4889d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius
4899d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4909d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Enables a network in wpa_supplicant.
4919d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be enabled.
4929d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4939d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
4949d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    public boolean enableNetwork(int netId) {
4959d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId));
4969d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        return doBooleanCommand("ENABLE_NETWORK " + netId);
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4999d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
500163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * Enable a network in wpa_supplicant, do not connect.
501163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * @param netId - Network ID of the network to be enabled.
502163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * @return true if command succeeded, false otherwise.
503163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     */
504163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    public boolean enableNetworkWithoutConnect(int netId) {
505163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt        if (DBG) logDbg("enableNetworkWithoutConnect nid=" + Integer.toString(netId));
506163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt        return doBooleanCommand("ENABLE_NETWORK " + netId + " " + "no-connect");
507163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    }
508163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt
509163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    /**
5109d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Disables a network in wpa_supplicant.
5119d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be disabled.
5129d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
5139d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
515f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5199d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
5209d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Select a network in wpa_supplicant (Disables all others).
5219d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be selected.
5229d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
5239d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
5240047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
5250047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
5260047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
5270047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
5280047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
530f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
535f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
54021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
54599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
54899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
54999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
55099d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
55199d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
55299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
55399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
55499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
55599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
566a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
567a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
5722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
5742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
5752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
5782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
5792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
5802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
5812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
5822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
5832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
5842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
590eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * MASK=<N> BSS command information mask.
59177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
592eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * The mask used in this method, 0x29d87, gets the following fields:
59377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
594eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_ID         (Bit 0)
595eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_BSSID      (Bit 1)
596eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_FREQ       (Bit 2)
597eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_LEVEL      (Bit 7)
598eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_TSF        (Bit 8)
599eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_IE         (Bit 10)
600eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_FLAGS      (Bit 11)
601eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_SSID       (Bit 12)
602eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_INTERNETW  (Bit 15) (adds ANQP info)
603eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_DELIM      (Bit 17)
604eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *
605eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * See wpa_supplicant/src/common/wpa_ctrl.h for details.
606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
6072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
6082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
6132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
6142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
6202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
6212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
6232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
6282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
6392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
6402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
6422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
6432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
6442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
6452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
6462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
6475d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
6482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
6492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
6512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
6522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6532afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
6542afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6552afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
6562afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6572afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
6582afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
6592afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
6602afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6612afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
6622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
6642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
6662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
6682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
6692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
6702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
6712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
6722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
6742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
6762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
6782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
6802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
6822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
6832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
6842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
6852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
6862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
6875d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
6882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
6892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
6902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
6912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
6932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
6942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
6952afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
6965d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
6975d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
6985d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6995d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
7005d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
7015d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
7025d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
7035d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
7045d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
7055d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
7065d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
7075d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
7085d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
7095d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
710947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            if (DBG) {
711947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                Log.v(TAG + ":DTIM", "SSID" + networkDetail.getSSID()
712947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + ", DTIM=" + networkDetail.getDtimInterval() + ", "
713947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + " IEstr:" + infoElementsStr);
714947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            }
7152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
7162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
7172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
7182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
7195d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
7202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
7222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
723af2b79e4cfc1da793b3d8fb4a96c144deefc7d58Joe Onorato                                if (DBG) Log.d(TAG, "HSNwk: '" + networkDetail);
7242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7259ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
72663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills                                    level, freq, tsf, infoElements, anqpLines);
7279ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
7282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
7292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
7302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
7312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
7322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
7332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
7342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
7352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
7362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
7372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
7385d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
7392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
7402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
7412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
7422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
7432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
74477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
74577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
747446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
748446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
749446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
750446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
751446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
752446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
753446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
754446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
755446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
756446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
757446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
758446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
759446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
760446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
761446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
76256d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
763446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
764446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
765446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
766446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
767446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
768446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
769446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
770446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
771446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
772446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
773446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
774446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
775446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
776446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
777446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
778446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
84803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
84903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
85003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
85103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
85203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
85303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
85403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
85503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
85703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
85803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
85903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
86003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
86103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
86203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
86303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
86403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
86503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
86818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
86918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
87018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
8717ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
8727ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
8737ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
8747ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
8757ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
8767ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
8777ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
8787ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8990a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
9000a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
9010a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
9020a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
914155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
915155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
918155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
919155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
920155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
921155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
924155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
9250465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
9260465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
9270465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
9280465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9319153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    /**
9329153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * Start/Stop PNO scan.
9339153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @param enable boolean indicating whether PNO is being enabled or disabled.
9349153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     */
9359153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    public boolean setPnoScan(boolean enable) {
9369153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        String cmd = enable ? "SET pno 1" : "SET pno 0";
9379153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        return doBooleanCommand(cmd);
9389153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    }
939ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius
9409153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    // TODO(rpius): Move PnoMonitor to SupplicantWifiScannerImpl.
941e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    //PNO Monitor
942e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    private class PnoMonitor {
943e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final int MINIMUM_PNO_GAP = 5 * 1000;
944e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final String ACTION_TOGGLE_PNO =
945e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            "com.android.server.Wifi.action.TOGGLE_PNO";
946e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        long mLastPnoChangeTimeStamp = -1L;
947e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mExpectedPnoState = false;
9489bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        List<PnoNetwork> mExpectedPnoNetworkList = null;
949e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mCurrentPnoState = false;;
950e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mWaitForTimer = false;
951d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        private boolean mIsPaused = false;
952e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        final Object mPnoLock = new Object();
953e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final AlarmManager mAlarmManager =
954e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
955e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final PendingIntent mPnoIntent;
956e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
957e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public PnoMonitor() {
958e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            Intent intent = new Intent(ACTION_TOGGLE_PNO, null);
959e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            intent.setPackage("android");
960e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
961e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
962e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mContext.registerReceiver(
963e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new BroadcastReceiver() {
964e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    @Override
965e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    public void onReceive(Context context, Intent intent) {
966e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        synchronized(mPnoLock) {
967d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            if (DBG) {
968d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                                Log.d(mTAG, "PNO timer expire, PNO should change to "
969e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                        + mExpectedPnoState);
970d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            }
971d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            boolean ret = setPno(mExpectedPnoState, mExpectedPnoNetworkList);
972d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            if (!ret) {
973d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                                Log.e(mTAG, "set PNO failure");
974e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            }
975e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            mWaitForTimer = false;
976e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        }
977e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
978e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                },
979e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new IntentFilter(ACTION_TOGGLE_PNO));
980e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
981e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
9829153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        /**
9839153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius         * Enable/Disable PNO with updated network priorities.
984ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param enable boolean indicating whether PNO is being enabled or disabled.
985ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param pnoNetworkList list of networks with priorities to be set before PNO setting.
986ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         */
9879bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        private boolean setPno(boolean enable, List<PnoNetwork> pnoNetworkList) {
988d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            if (mIsPaused) {
989d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (DBG) Log.d(mTAG, "Do not change PNO state since it is paused");
990d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                return true;
991d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
992d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            if (mCurrentPnoState == enable) {
993d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (DBG) Log.d(mTAG, "Do not change PNO since current is expected");
994d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                return true;
995d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
996d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            if (DBG) {
997d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to " + enable);
998d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
999ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // TODO: Couple of cases yet to be handled:
1000ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 1. What if the network priority update fails, should we bail out of PNO setting?
1001ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 2. If PNO setting fails below, should we go back and revert this priority change?
1002ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            if (pnoNetworkList != null) {
1003d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (DBG) Log.i(mTAG, "update priorities for PNO. Enable: " + enable);
10049bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                for (PnoNetwork pnoNetwork : pnoNetworkList) {
1005ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    // What if this fails? Should we bail out?
1006ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    boolean isSuccess = setNetworkVariable(pnoNetwork.networkId,
1007ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            WifiConfiguration.priorityVarName,
1008ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            Integer.toString(pnoNetwork.priority));
10095c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    if (!isSuccess) {
1010d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                        Log.e(mTAG, "update priority failed for :" + pnoNetwork.networkId);
1011ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    }
1012ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                }
1013ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            }
10149153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius            boolean ret = WifiNative.this.setPnoScan(enable);
1015e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mLastPnoChangeTimeStamp = System.currentTimeMillis();
1016e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            if (ret) {
1017e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                mCurrentPnoState = enable;
1018e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
1019e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return ret;
1020e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
1021e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
1022ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        public boolean enableBackgroundScan(
1023ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                boolean enable,
10249bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                List<PnoNetwork> pnoNetworkList) {
1025e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            synchronized(mPnoLock) {
1026d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                Log.i(mTAG, "set expected PNO to " +  enable);
1027d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                mExpectedPnoState = enable;
1028d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                mExpectedPnoNetworkList = pnoNetworkList;
1029e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                if (mWaitForTimer) {
1030e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    //already has a timer
1031e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (DBG) Log.d(mTAG, "update expected PNO to " +  mExpectedPnoState);
1032e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                } else {
1033e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp;
1034e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (timeDifference >= MINIMUM_PNO_GAP) {
1035ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        return setPno(enable, pnoNetworkList);
1036e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    } else {
1037e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mWaitForTimer = true;
1038e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference);
1039e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
1040e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                System.currentTimeMillis() + timeDifference, mPnoIntent);
1041e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
1042e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                }
1043e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                return true;
1044e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
1045e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
1046d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1047d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        /**
1048d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         * Pause PNO scanning.
1049d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         */
1050d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        public void pauseBackgroundScan() {
1051d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            synchronized (mPnoLock) {
1052d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (!mIsPaused) {
1053d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    Log.i(mTAG, "Pausing Pno scan. Current state: " + mCurrentPnoState);
1054d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    if (mCurrentPnoState) {
1055d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                        setPno(false, null);
1056d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    }
1057d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    mIsPaused = true;
1058d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                }
1059d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
1060d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1061d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1062d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        /**
1063d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         * Resume PNO scanning.
1064d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         */
1065d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        public void resumeBackgroundScan() {
1066d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            synchronized (mPnoLock) {
1067d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (mIsPaused) {
1068d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    Log.i(mTAG, "Resuming Pno scan. Expected state: " + mExpectedPnoState);
1069d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    mIsPaused = false;
1070d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    if (mExpectedPnoState) {
1071d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                        setPno(true, null);
1072d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    }
1073d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                }
1074d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
1075d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1076e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
1077e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
1078ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius    public boolean enableBackgroundScan(
1079ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            boolean enable,
10809bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            List<PnoNetwork> pnoNetworkList) {
1081e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        if (mPnoMonitor != null) {
1082ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            return mPnoMonitor.enableBackgroundScan(enable, pnoNetworkList);
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1084e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return false;
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1088d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    /**
1089d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     * Pause PNO scanning.
1090d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     * Note: If Pno is currently running, the stop might take a little while to take effect!
1091d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     */
1092d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    public void pauseBackgroundScan() {
1093d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        if (mPnoMonitor != null) {
1094d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            mPnoMonitor.pauseBackgroundScan();
1095d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1096d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    }
1097d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1098d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    /**
1099d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     * Resume PNO scanning.
1100d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     */
1101d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    public void resumeBackgroundScan() {
1102d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        if (mPnoMonitor != null) {
1103d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            mPnoMonitor.resumeBackgroundScan();
1104d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1105d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    }
1106d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1107f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
1108f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
1109f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
1110f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
1111f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
1112f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1113f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1114f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
111956845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
112056845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
112156845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
112256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
112356845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
112456845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
112556845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
112656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
1129956f54b391677d78379729dd14518edddf3c7660Etan Cohen            synchronized (sLock) {
113018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
113118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
113218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
1169956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1185956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1200956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
120933b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
121018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
121118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
121218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
121333b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
121433b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1215f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1216f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
121718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
121833b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
121933b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
122026eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean simAuthFailedResponse(int id) {
122126eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type GSM-AUTH
122226eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL");
122326eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
122426eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
122526eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean umtsAuthFailedResponse(int id) {
122626eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type UMTS-AUTH
122726eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL");
122826eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
122926eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
1230ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
123118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1232ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1233ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1282956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1296956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1358956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
135918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
136018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
136118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
136218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
136318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
136718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
136818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
136918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
137018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
137118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
137218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
137318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
137418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
138318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1472956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
150536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
150636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
150727f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
150827f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
150936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
151036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
151136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
1512956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
151327f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
151427f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
151527f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
151627f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
151736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
151836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
151936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
152036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
152136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
152236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
152336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
152436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
152536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
152836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
152936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
153036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1597956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
159818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
159918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
160018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
160118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
160218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
160318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1614956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
161518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
161618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
161818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
161918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
162018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
162118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
162218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
162318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
162418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
162518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
162618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
162718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
162818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
162918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
163018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
163118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
163218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
16633f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1664446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1665446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1666446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1667446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1668f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1669f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1670f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1671f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1672f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1673f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1674f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1675f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1676f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1677f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1678f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1679f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1680f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1681f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1682f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1683f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1684f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1685f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1686f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1687f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1688f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1689f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1690f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
16917e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16927e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    /* kernel logging support */
16937e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    private static native byte[] readKernelLogNative();
16947e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16957e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    synchronized public String readKernelLog() {
16967e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        byte[] bytes = readKernelLogNative();
16977e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        if (bytes != null) {
16987e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
16997e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            try {
17007e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
17017e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return decoded.toString();
17027e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            } catch (CharacterCodingException cce) {
17037e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return new String(bytes, StandardCharsets.ISO_8859_1);
17047e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            }
17057e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        } else {
17067e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            return "*** failed to read kernel log ***";
17077e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        }
17087e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    }
17097e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
17107f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
17117f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
171218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
171318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
171418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
171518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
171618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
171718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1718b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1719f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1720f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1721956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static int sWlan0Index = -1;
1722f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1723f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
17247f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1725b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1726b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1727b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
17287f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1729b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
17307ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1731b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
17327ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
17337ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
17347ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
17357ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
173618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1737d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1738d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1739d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1740d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1741d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1742d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1743956f54b391677d78379729dd14518edddf3c7660Etan Cohen        sLocalLog.log(debugLog);
1744d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1745956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1746918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang            if (startHalNative()) {
1747918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                int wlan0Index = queryInterfaceIndex(mInterfaceName);
1748918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                if (wlan0Index == -1) {
1749918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    if (DBG) sLocalLog.log("Could not find interface with name: " + mInterfaceName);
1750918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    return false;
1751918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                }
1752918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                sWlan0Index = wlan0Index;
1753f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1754f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1755aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1756aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1757956f54b391677d78379729dd14518edddf3c7660Etan Cohen                if (DBG) sLocalLog.log("Could not start hal");
1758f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1759aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1760aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
17617ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
17627ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
17637ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
176418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1765956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
176671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1767f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1768f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1769f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1770f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1771f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1772f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1773f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1774f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1775f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1776f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1777f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1778f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1779f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
17807ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
17817f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
178218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
178371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
178471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1785b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
17867f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1787918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang    public int queryInterfaceIndex(String interfaceName) {
1788956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
178971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1790918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                int num = getInterfacesNative();
1791918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                for (int i = 0; i < num; i++) {
1792918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    String name = getInterfaceNameNative(i);
1793918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    if (name.equals(interfaceName)) {
1794918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                        return i;
179502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1796aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
1797e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1798e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1799918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang        return -1;
18007f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18017f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1802b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
180318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
1804956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
180518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
180618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
18077f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18087f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1809062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan    // TODO: Change variable names to camel style.
1810e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1811297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1812e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1813e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1814e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1815297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
18167d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1817e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1818d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_bssid_history_entries;
1819d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_epno_networks;
1820d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_epno_networks_by_ssid;
1821d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_of_white_listed_ssid;
1822e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1823e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
182418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1825956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1826af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1827af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1828e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1829e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1830b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1831b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1832e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1833b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1834b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
183583a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1836b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1837d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
18387f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1839e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1840712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int frequency;
1841712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int dwell_time_ms;
1842712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public boolean passive;
18437f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18447f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1845e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1846712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int bucket;
1847712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int band;
1848712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int period_ms;
1849712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_period_ms;
1850712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int step_count;
1851712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_events;
1852712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_channels;
1853712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public ChannelSettings[] channels;
1854e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
18557f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1856e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1857712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int base_period_ms;
1858712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_ap_per_scan;
1859712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_percent;
1860712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_num_scans;
1861712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_buckets;
1862d74ff28cdb01d3f711e0b0f0e99e51fdb221eaf2Roshan Pius        /* Not part of gscan HAL API. Used only for wpa_supplicant scanning */
1863d74ff28cdb01d3f711e0b0f0e99e51fdb221eaf2Roshan Pius        public int[] hiddenNetworkIds;
1864712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public BucketSettings[] buckets;
1865e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
18667f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
186768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
18689bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Network parameters to start PNO scan.
18699bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
18709bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public static class PnoNetwork {
18719bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public String ssid;
18729bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int networkId;
18739bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int priority;
18749bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public byte flags;
1875ef3ea1092bc17673c0a85a845b053151b7c10e07Roshan Pius        public byte auth_bit_field;
18769bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
18779bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
18789bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
18799bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Parameters to start PNO scan. This holds the list of networks which are going to used for
18809bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * PNO scan.
18819bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
18829bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public static class PnoSettings {
18839bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int min5GHzRssi;
18849bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int min24GHzRssi;
18859bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int initialScoreMax;
18869bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int currentConnectionBonus;
18879bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int sameNetworkBonus;
18889bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int secureBonus;
18899bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int band5GHzBonus;
1890dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        public boolean isConnected;
18919bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public PnoNetwork[] networkList;
18929bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
18939bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
18949bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
189568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Wi-Fi channel information.
189668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
189768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public static class WifiChannelInfo {
189868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mPrimaryFrequency;
189968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency0;
190068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency1;
190168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mChannelWidth;
190268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        // TODO: add preamble once available in HAL.
190368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
190468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
1905b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
190663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
190763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called for each AP as it is found with the entire contents of the beacon/probe response.
190863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Only called when WifiScanner.REPORT_EVENT_FULL_SCAN_RESULT is specified.
190963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1910c9e6069eb941d282af213dc20b171877db6b567bMitchell Wills        void onFullScanResult(ScanResult fullScanResult, int bucketsScanned);
191163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
191263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Callback on an event during a gscan scan.
191363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * See WifiNative.WIFI_SCAN_* for possible values.
191463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
191563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        void onScanStatus(int event);
191663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
191763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is paused.
191863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
191983a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
192063539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
192163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is resumed.
192263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1923b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1924e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1925e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
19269bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
19279bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Handler to notify the occurrence of various events during PNO scan.
19289bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
19299bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public interface PnoEventHandler {
19309bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        /**
19319bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         * Callback to notify when one of the shortlisted networks is found during PNO scan.
19329bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         * @param results List of Scan results received.
19339bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         */
19349bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        void onPnoNetworkFound(ScanResult[] results);
1935063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius
1936063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius        /**
1937063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius         * Callback to notify when the PNO scan schedule fails.
1938063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius         */
1939063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius        void onPnoScanFailed();
19409bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
19419bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
1942b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
194371af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_RESULTS_AVAILABLE = 0;
194471af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1;
194571af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_PERCENT = 2;
194671af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_FAILED = 3;
1947b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
194818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
19492a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onScanStatus(int id, int event) {
195018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
195163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        if (handler != null) {
195263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills            handler.onScanStatus(event);
19532a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        }
1954e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1955e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
195618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
19575cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
19585cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19595cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
19605cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
19615cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19625cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19635cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
19645cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19655cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
19665cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
19675cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19685cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
19695cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
19705cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19715cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
19725cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
19735cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
19745cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
19755cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
19765cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
19775cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19785cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19795cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
19805cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
19815cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19825cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19835cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
19845cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
19855cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
198618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
19875cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
19885cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
19895cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
19905cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19915cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19925cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
19935cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
19945cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
19955cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
19965cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19971ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void populateScanResult(ScanResult result, int beaconCap, String dbg) {
1998dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
19995d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
20005d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
20015d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
20025d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
20035d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
20045d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
20055d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
20065d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
20071ab129e587d334a144a0bca5323c27985397a403Randy Pan                InformationElementUtil.parseInformationElements(result.bytes);
20085d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
20095d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
20105d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
20115d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
20125d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
20135d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
20145d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
2015243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
2016243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
2017d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
20185d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
2019d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
2020d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
2021d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
2022d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
2023d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
2024243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
20255d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
20265d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
20275d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
20285d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
2029243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
20305d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
20315d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
2032243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
2033243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
20343571366ac36c70746b9f013ec2b54482861c9292Randy Pan
20353571366ac36c70746b9f013ec2b54482861c9292Randy Pan        // build capabilities string
20363571366ac36c70746b9f013ec2b54482861c9292Randy Pan        BitSet beaconCapBits = new BitSet(16);
20373571366ac36c70746b9f013ec2b54482861c9292Randy Pan        for (int i = 0; i < 16; i++) {
20383571366ac36c70746b9f013ec2b54482861c9292Randy Pan            if ((beaconCap & (1 << i)) != 0) {
20393571366ac36c70746b9f013ec2b54482861c9292Randy Pan                beaconCapBits.set(i);
20403571366ac36c70746b9f013ec2b54482861c9292Randy Pan            }
20413571366ac36c70746b9f013ec2b54482861c9292Randy Pan        }
20423571366ac36c70746b9f013ec2b54482861c9292Randy Pan        result.capabilities = InformationElementUtil.Capabilities.buildCapabilities(elements,
20433571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                               beaconCapBits);
20443571366ac36c70746b9f013ec2b54482861c9292Randy Pan
2045243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
20463571366ac36c70746b9f013ec2b54482861c9292Randy Pan            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth
20473571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " PrimaryFreq: " + result.frequency + " mCenterfreq0: " + result.centerFreq0
20483571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder
20493571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    ? "Support RTT reponder: " : "Do not support RTT responder")
20503571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " Capabilities: " + result.capabilities);
2051f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
2052f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
2053476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
2054dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2055dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
205618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
20571ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void onFullScanResult(int id, ScanResult result,
20583571366ac36c70746b9f013ec2b54482861c9292Randy Pan            int bucketsScanned, int beaconCap) {
20591ab129e587d334a144a0bca5323c27985397a403Randy Pan        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID);
2060dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
206118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
206218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
20631ab129e587d334a144a0bca5323c27985397a403Randy Pan            populateScanResult(result, beaconCap, " onFullScanResult ");
2064c9e6069eb941d282af213dc20b171877db6b567bMitchell Wills            handler.onFullScanResult(result, bucketsScanned);
2065dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
20667f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
20677f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
2068b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
2069b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
2070b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
20717f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
207218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
2073956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
207471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2075f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
2076f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
2077f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
2078b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
2079f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
20807f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
2081f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
2082e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2083f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
2084f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
2085b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
2086f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
2087f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
2088f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
2089f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
2090f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2091f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2092f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2093f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2094f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2095e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2096e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2097e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
20987f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
20997f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
210018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
2101956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
210271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2103ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
2104ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
2105ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2106f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
2107f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
2108f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
2109f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2110b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
2111b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
2112b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
211318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
2114956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
211571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2116f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
2117f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
2118f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
2119f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
2120f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
2121f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
2122f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2123b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
2124b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
2125b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
2126b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
212718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
2128956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
212971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2130f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
21316f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
2132f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
2133f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
2134f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
2135f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
2136f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
213783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
2138f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
2139f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
2140f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
214183a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
2142b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
2143e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2144e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2145e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
214618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
2147956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
21483ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            WifiScanner.ScanData[] sd = null;
214971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
21503ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                sd = getScanResultsNative(sWlan0Index, flush);
21513ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            }
21523ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande
21533ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            if (sd != null) {
21543ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return sd;
2155f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
21563ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return new WifiScanner.ScanData[0];
2157f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2158aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2159e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2160e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2161b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
2162d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
2163d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
2164e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2165e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2166b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
2167b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
2168e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2169b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
2170e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
2171b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
2172e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
217318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
217418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
2175956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
217671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2177f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2178f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2179f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2180f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
2181f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2182f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2183f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
2184f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
2185f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2186f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2187f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2188e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2189f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2190f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2191e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2192e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2193e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2194e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2195e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
219618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
2197956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
219871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2199f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2200f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
2201f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
2202f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2203f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2204e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
22057f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
22067f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
2207e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
220818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
220918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
221018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
221118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
221218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
221318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
221418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
221518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
2216d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
2217d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
2218d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
221918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
222018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
222118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
222218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
222318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
222418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
222518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
222618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
2227aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2228e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2229e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2230b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
2231e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
2232e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2233e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2234b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
2235b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
2236e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2237b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
2238e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
2239b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
2240e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
224118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
2242b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
2243956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
224471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2245f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2246f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2247f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2248f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
2249f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2250f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2251f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
2252851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
2253851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
2254f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2255f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2256f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2257e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2258f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2259f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2260e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2261e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2262e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2263e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2264e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2265e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
226618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
2267956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
226871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2269f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2270f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
2271f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
2272f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2273f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2274e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2275e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2276e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2277e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
227818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
227918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
228018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
228118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
228218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
228318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
2284f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
228518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
2286aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2287e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2288e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
228918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
2290200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
2291200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
2292956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
229371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2294aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
2295f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2296f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2297f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2298aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
22995c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
23005c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
230118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2302d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2303956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
230471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2305d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2306d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2307d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2308d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2309d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2310c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
231118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2312956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
231371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2314f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2315f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2316f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2317f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2318f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2319f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2320a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2321143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2322143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2323143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2324143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2325143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2326143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2327143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2328143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2329143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
233018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
233118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
233218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
233318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
233402a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
233518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2336143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2337143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
233818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
233918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2340143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2341143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2342143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2343143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2344143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2345143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2346143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2347143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
234818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2349143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2350956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
235171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2352f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2353f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2354f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2355f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2356f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2357f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2358f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2359f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2360f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2361143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2362f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2363143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2364143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2365143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2366143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
236718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
2368956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
236971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2370f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2371f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2372f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2373143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2374f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2375f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2376f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2377f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
23785cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2379f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2380f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
23815cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2382f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2383f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2384143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2385143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2386143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2387143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2388143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2389042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
239068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static int sRttResponderCmdId = 0;
239168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
239268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native ResponderConfig enableRttResponderNative(int iface, int commandId,
239368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int timeoutSeconds, WifiChannelInfo channelHint);
239468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
239568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Enable RTT responder role on the device. Returns {@link ResponderConfig} if the responder
239668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * role is successfully enabled, {@code null} otherwise.
239768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
239868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    @Nullable
239968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public ResponderConfig enableRttResponder(int timeoutSeconds) {
240068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
240168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return null;
240268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId != 0) {
240368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                if (DBG) Log.e(mTAG, "responder mode already enabled - this shouldn't happen");
240468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return null;
240568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
240668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int id = getNewCmdIdLocked();
240768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            ResponderConfig config = enableRttResponderNative(
240868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                    sWlan0Index, id, timeoutSeconds, null);
240968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (config != null) sRttResponderCmdId = id;
241068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (DBG) Log.d(TAG, "enabling rtt " + (config != null));
241168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return config;
241268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
241368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
241468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
241568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native boolean disableRttResponderNative(int iface, int commandId);
241668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
241768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Disable RTT responder role. Returns {@code true} if responder role is successfully disabled,
241868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * {@code false} otherwise.
241968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
242068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public boolean disableRttResponder() {
242168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
242268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return false;
242368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId == 0) {
242468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                Log.e(mTAG, "responder role not enabled yet");
242568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return true;
242668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
242768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            sRttResponderCmdId = 0;
242868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return disableRttResponderNative(sWlan0Index, sRttResponderCmdId);
242968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
243068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
243168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
2432042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2433042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
243418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2435956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
243671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2437042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2438042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2439042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2440042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2441042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2442042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2443efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2444efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2445efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2446efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
244718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2448956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2449ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2450ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
245118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2452ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2453ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2454efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2455efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
24560465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2457ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
245818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2459956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2460ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2461ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
246218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2463ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2464ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2465ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2466ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
24670465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
24680465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
246918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
2470956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
247171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
24720465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
24730465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
24740465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
24750465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
24760465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
24770465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2478b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
24793b51fd1bb8356b284822f4f677ad941524e616ebNingyuan Wang    private static native boolean setInterfaceUpNative(boolean up);
248064e816ff5483030636e1815f69a6cbaf8cef289eNingyuan Wang    public boolean setInterfaceUp(boolean up) {
2481956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
248271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
24833b51fd1bb8356b284822f4f677ad941524e616ebNingyuan Wang                return setInterfaceUpNative(up);
2484b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2485b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2486b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2487b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2488b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
248912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
249012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
249118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
2492956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
249371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
249412cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
249518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
249612cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
249712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
249812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
249912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2500939177ff615062ec826601d536466875d8457375xinhe
2501e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen    private static native ApfCapabilities getApfCapabilitiesNative(int iface);
2502e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen    public ApfCapabilities getApfCapabilities() {
25036609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
25046609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
2505e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen                return getApfCapabilitiesNative(sWlan0Index);
25066609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
25076609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return null;
25086609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
25096609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
25106609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
25116609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
25126609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    private static native boolean installPacketFilterNative(int iface, byte[] filter);
25136609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    public boolean installPacketFilter(byte[] filter) {
25146609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
25156609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
25166609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return installPacketFilterNative(sWlan0Index, filter);
25176609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
25186609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return false;
25196609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
25206609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
25216609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
25226609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
2523939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
252418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2525956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
252671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2527939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2528939177ff615062ec826601d536466875d8457375xinhe            } else {
2529939177ff615062ec826601d536466875d8457375xinhe                return false;
2530939177ff615062ec826601d536466875d8457375xinhe            }
2531939177ff615062ec826601d536466875d8457375xinhe        }
2532939177ff615062ec826601d536466875d8457375xinhe    }
2533939177ff615062ec826601d536466875d8457375xinhe
2534d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2535d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2536d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2537d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2538d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2539d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2540d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2541d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2542d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
254318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2544956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2545f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2546f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2547d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2548d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2549d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2550d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2551d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2552d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2553d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2554d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2555d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2556d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2557d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2558d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
255918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2560956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
256171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2562d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2563d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2564d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2565d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2566d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2567d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2568d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2569d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2570d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2571d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2572d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2573d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2574d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2575d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2576d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2577d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2578d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2579d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2580d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2581d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
258218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2583956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
258471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2585d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2586d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2587d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2588d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2589d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2590d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2591d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
259218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
259318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
259418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
259518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
259618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
259718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
259818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
259918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2600d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2601d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2602a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2603a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2604a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
26057d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2606a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
26070bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
26080bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2609a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2610a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2611a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2612a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
261318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
26140bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
261518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
261618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
261718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
261803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
261903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
262018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
26210bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
262218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
262318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
262418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
262503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
262603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2627b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2628b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
262918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
2630956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2631b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2632b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2633b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2634b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2635b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2636b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2637b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2638b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2639b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2640b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2641b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2642b797893fc1966803d0c013faac42e6396a37a384xinhe            }
264303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
264403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
264503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
264603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
26470bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
264818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
264903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
2650956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
265171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
265203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
265303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
265403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
265503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
265603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
265703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
265803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
265903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
266003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
266118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
2662956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
266371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
266403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
266503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2666f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
266703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
266803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
266903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
267003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2671b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
267218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2673956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2674b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2675b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2676b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2677b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2678b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2679b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2680b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2681b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2682b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2683b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2684b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2685b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2686b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2687b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2688b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2689b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2690b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2691b797893fc1966803d0c013faac42e6396a37a384xinhe
269203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
269318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
2694956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
269571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
269603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
269703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2698f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
269903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
270003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
270103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
270203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
270303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
270403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
270518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
2706956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
270771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
270803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
270903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2710f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
271103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
271203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
271303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
271403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
27150bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
27160bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
27170bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
27180bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
27190bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
27200bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
27210bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
27220bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
27230bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
27240bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
27250bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
27260bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
27270bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
27280bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
27290bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
27300bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
27310bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
27320bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
27330bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
27340bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
273518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
2736956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
273771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
273803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
273903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
274003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
274103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
274203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
274303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
274403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
274503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
274618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
2747956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
274871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
274903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
275003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
275103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
275203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
275303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
275403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
275503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
275618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
275718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
275803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
275998dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
276098dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
276198dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
276298dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
276398dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
276403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2765127f7244183786e6ccae09e81eeccdac31973e69xinhe
276603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
276718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
2768956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
276971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
27700bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
27710bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
277203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
27730bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
27740bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
27750bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
277603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
277703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2778f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2779a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2780a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2781dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2782dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
278309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /* Packet fate API */
278409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
278509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
278609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    abstract static class FateReport {
278709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte mFate;
278809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final long mDriverTimestampUSec;
278909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte mFrameType;
279009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte[] mFrameBytes;
279109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
279209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        FateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
279309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFate = fate;
279409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mDriverTimestampUSec = driverTimestampUSec;
279509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFrameType = frameType;
279609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFrameBytes = frameBytes;
279709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
27980fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
27990fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
28000fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        public String toString() {
28010fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            StringWriter sw = new StringWriter();
28020fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            PrintWriter pw = new PrintWriter(sw);
28030fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame direction: %s\n", directionToString());
28040fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame timestamp: %d\n", mDriverTimestampUSec);
28050fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame fate: %s\n", fateToString());
28060fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame type: %s\n", frameTypeToString(mFrameType));
28070fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame length: %d\n", mFrameBytes.length);
28080fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.append("Frame bytes");
28090fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.append(HexDump.dumpHexString(mFrameBytes));
28100fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.append("\n");
28110fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            return sw.toString();
28120fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
28130fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
28140fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected abstract String directionToString();
28150fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
28160fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected abstract String fateToString();
28170fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
28180fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        private static String frameTypeToString(byte frameType) {
28190fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            switch (frameType) {
28200fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.FRAME_TYPE_UNKNOWN:
28210fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "unknown";
28220fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.FRAME_TYPE_ETHERNET_II:
28230fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "data";
28240fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.FRAME_TYPE_80211_MGMT:
28250fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "802.11 management";
28260fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                default:
28270fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return Byte.toString(frameType);
28280fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
28290fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
283009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
283109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
283209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /**
283309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     * Represents the fate information for one outbound packet.
283409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     */
283509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
283609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    public static final class TxFateReport extends FateReport {
283709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        TxFateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
283809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            super(fate, driverTimestampUSec, frameType, frameBytes);
283909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
28400fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
28410fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
28420fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String directionToString() {
28430fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            return "TX";
28440fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
28450fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
28460fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
28470fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String fateToString() {
28480fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            switch (mFate) {
28490fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_ACKED:
28500fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "acked";
28510fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_SENT:
28520fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "sent";
28530fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_QUEUED:
28540fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware queued";
28550fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_DROP_INVALID:
28560fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (invalid frame)";
28570fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_DROP_NOBUFS:
28580fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (no bufs)";
28590fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_DROP_OTHER:
28600fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (other)";
28610fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_QUEUED:
28620fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver queued";
28630fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_DROP_INVALID:
28640fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (invalid frame)";
28650fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_DROP_NOBUFS:
28660fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (no bufs)";
28670fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_DROP_OTHER:
28680fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (other)";
28690fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                default:
28700fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return Byte.toString(mFate);
28710fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
28720fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
287309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
287409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
287509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /**
287609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     * Represents the fate information for one inbound packet.
287709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     */
287809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
287909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    public static final class RxFateReport extends FateReport {
288009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        RxFateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
288109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            super(fate, driverTimestampUSec, frameType, frameBytes);
288209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
28830fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
28840fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
28850fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String directionToString() {
28860fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            return "RX";
28870fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
28880fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
28890fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
28900fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String fateToString() {
28910fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            switch (mFate) {
28920fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_SUCCESS:
28930fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "success";
28940fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_QUEUED:
28950fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware queued";
28960fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_FILTER:
28970fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (filter)";
28980fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_INVALID:
28990fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (invalid frame)";
29000fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_NOBUFS:
29010fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (no bufs)";
29020fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_OTHER:
29030fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (other)";
29040fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_QUEUED:
29050fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver queued";
29060fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_FILTER:
29070fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (filter)";
29080fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_INVALID:
29090fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (invalid frame)";
29100fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_NOBUFS:
29110fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (no bufs)";
29120fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_OTHER:
29130fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (other)";
29140fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                default:
29150fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return Byte.toString(mFate);
29160fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
29170fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
291809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
291909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
292009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int startPktFateMonitoringNative(int iface);
29210fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    /**
29220fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     * Ask the HAL to enable packet fate monitoring. Fails unless HAL is started.
29230fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     */
29240fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public boolean startPktFateMonitoring() {
29250fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        synchronized (sLock) {
29260fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            if (isHalStarted()) {
29270fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return startPktFateMonitoringNative(sWlan0Index) == WIFI_SUCCESS;
29280fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            } else {
29290fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return false;
29300fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
29310fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
29320fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    }
29330fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
293409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int getTxPktFatesNative(int iface, TxFateReport[] reportBufs);
29350fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    /**
29360fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     * Fetch the most recent TX packet fates from the HAL. Fails unless HAL is started.
29370fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     */
29380fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public boolean getTxPktFates(TxFateReport[] reportBufs) {
29390fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        synchronized (sLock) {
29400fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            if (isHalStarted()) {
29410fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                int res = getTxPktFatesNative(sWlan0Index, reportBufs);
29420fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                if (res != WIFI_SUCCESS) {
29430fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    Log.e(TAG, "getTxPktFatesNative returned " + res);
29440fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return false;
29450fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                } else {
29460fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return true;
29470fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                }
29480fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            } else {
29490fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return false;
29500fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
29510fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
29520fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    }
29530fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
295409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int getRxPktFatesNative(int iface, RxFateReport[] reportBufs);
29550fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    /**
29560fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     * Fetch the most recent RX packet fates from the HAL. Fails unless HAL is started.
29570fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     */
29580fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public boolean getRxPktFates(RxFateReport[] reportBufs) {
29590fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        synchronized (sLock) {
29600fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            if (isHalStarted()) {
29610fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                int res = getRxPktFatesNative(sWlan0Index, reportBufs);
29620fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                if (res != WIFI_SUCCESS) {
29630fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    Log.e(TAG, "getRxPktFatesNative returned " + res);
29640fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return false;
29650fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                } else {
29660fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return true;
29670fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                }
29680fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            } else {
29690fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return false;
29700fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
29710fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
29720fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    }
297309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
297409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    //---------------------------------------------------------------------------------
29755c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /* Configure ePNO/PNO */
29769bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static PnoEventHandler sPnoEventHandler;
29779bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static int sPnoCmdId = 0;
2978dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
29799bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static native boolean setPnoListNative(int iface, int id, PnoSettings settings);
298018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
29815c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /**
29829bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Set the PNO settings & the network list in HAL to start PNO.
29839bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param settings PNO settings and network list.
29849bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param eventHandler Handler to receive notifications back during PNO scan.
29859bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
29865c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     */
29879bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean setPnoList(PnoSettings settings, PnoEventHandler eventHandler) {
29889bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2989dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
29909bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        synchronized (sLock) {
29919bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            if (isHalStarted()) {
29929bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoCmdId = getNewCmdIdLocked();
29939bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoEventHandler = eventHandler;
29949bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                if (setPnoListNative(sWlan0Index, sPnoCmdId, settings)) {
29959bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                    return true;
29969bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                }
2997dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
29989bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            sPnoEventHandler = null;
29999bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            return false;
3000dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
3001dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
3002dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
30039bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
30049bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Set the PNO network list in HAL to start PNO.
30059bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param list PNO network list.
30069bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param eventHandler Handler to receive notifications back during PNO scan.
30079bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
30089bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
30099bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean setPnoList(PnoNetwork[] list, PnoEventHandler eventHandler) {
30109bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        PnoSettings settings = new PnoSettings();
30119bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        settings.networkList = list;
30129bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        return setPnoList(settings, eventHandler);
3013dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
3014dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
30159bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static native boolean resetPnoListNative(int iface, int id);
3016dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
30179bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
30189bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Reset the PNO settings in HAL to stop PNO.
30199bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
30209bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
30219bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean resetPnoList() {
30229bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        Log.e(TAG, "resetPnoList cmd " + sPnoCmdId);
3023dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
3024956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
302571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
3026f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
30279bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoEventHandler = null;
30289bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                if (resetPnoListNative(sWlan0Index, sPnoCmdId)) {
3029f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
3030f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
3031dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
3032f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
3033dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
3034dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
3035dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
303618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
30373571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void onPnoNetworkFound(int id, ScanResult[] results, int[] beaconCaps) {
3038dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
3039dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
3040dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
3041dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
3042dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
3043dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
3044dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
30459bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        PnoEventHandler handler = sPnoEventHandler;
304618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
304718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
304818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
304918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
3050dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
30511ab129e587d334a144a0bca5323c27985397a403Randy Pan                populateScanResult(results[i], beaconCaps[i], "onPnoNetworkFound ");
305218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
3053dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
305418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
305518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
305618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
305718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
305818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
3059dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
3060d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
3061d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
30629ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
30639ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
30649ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
306518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
30669ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
30679ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
30689ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
30699ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
30709ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
30719ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
3072956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
307371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
3074f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
3075f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
3076f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
30779ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
30789ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
30799ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
30809ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
30819ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
3082c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
3083c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
3084c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
308518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
3086c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
3087c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
30883bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
30893bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
30903bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
30913bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
30923bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
30933bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
30943bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
3095956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
3096c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
3097c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
309818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
3099c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
3100c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
3101c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
3102c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
3103c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
3104c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
3105c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
3106c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
310718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
3108c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
3109c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
3110956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
3111c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
3112c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
3113c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
3114c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
3115c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
3116c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
3117c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
3118aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3119aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
3120aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
3121aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
3122aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3123aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
3124aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
312518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
312618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
312718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
312818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
312918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
313018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
3131aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
3132aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3133aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
3134aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
3135aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3136aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
3137aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
313818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
3139aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
3140aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
3141956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
314218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
3143aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
3144ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
3145ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
3146ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
3147ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
3148aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
3149aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
3150ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
3151ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
3152ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
3153ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
3154ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
3155ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
3156aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
3157aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
3158aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
3159aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
3160aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
3161aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3162aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
3163aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
316418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
3165aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
3166956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
3167aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
3168ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
3169ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
3170ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
3171ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
3172ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
3173ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
3174aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
3175aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
3176aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
3177aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
3178aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
31795ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
31805ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
31816bf6986d359556010638dfae332b585162f06520Roshan Pius
31826bf6986d359556010638dfae332b585162f06520Roshan Pius    /**
31836bf6986d359556010638dfae332b585162f06520Roshan Pius     * Fetch the host wakeup reasons stats from wlan driver.
31846bf6986d359556010638dfae332b585162f06520Roshan Pius     * @return the |WifiWakeReasonAndCounts| object retrieved from the wlan driver.
31856bf6986d359556010638dfae332b585162f06520Roshan Pius     */
31866bf6986d359556010638dfae332b585162f06520Roshan Pius    public WifiWakeReasonAndCounts getWlanWakeReasonCount() {
31876bf6986d359556010638dfae332b585162f06520Roshan Pius        Log.d(TAG, "getWlanWakeReasonCount " + sWlan0Index);
31886bf6986d359556010638dfae332b585162f06520Roshan Pius        synchronized (sLock) {
31896bf6986d359556010638dfae332b585162f06520Roshan Pius            if (isHalStarted()) {
31906bf6986d359556010638dfae332b585162f06520Roshan Pius                return getWlanWakeReasonCountNative(sWlan0Index);
31916bf6986d359556010638dfae332b585162f06520Roshan Pius            } else {
31926bf6986d359556010638dfae332b585162f06520Roshan Pius                return null;
31936bf6986d359556010638dfae332b585162f06520Roshan Pius            }
31946bf6986d359556010638dfae332b585162f06520Roshan Pius        }
31956bf6986d359556010638dfae332b585162f06520Roshan Pius    }
31963dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
31973dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    private static native int configureNeighborDiscoveryOffload(int iface, boolean enabled);
31983dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
31993dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    public boolean configureNeighborDiscoveryOffload(boolean enabled) {
32003dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        final String logMsg =  "configureNeighborDiscoveryOffload(" + enabled + ")";
32013dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        Log.d(mTAG, logMsg);
32023dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        synchronized (sLock) {
32033dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            if (isHalStarted()) {
32043dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                final int ret = configureNeighborDiscoveryOffload(sWlan0Index, enabled);
32053dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                if (ret != 0) {
32063dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                    Log.d(mTAG, logMsg + " returned: " + ret);
32073dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                }
32083dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                return (ret == 0);
32093dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            }
32103dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        }
32113dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        return false;
32123dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    }
3213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
3214