WifiNative.java revision 5ea42964ba17901a8d724736b450ace6ed48880f
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
19143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpandeimport android.net.wifi.RttManager;
20e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
21dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiConfiguration;
2218786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.net.wifi.WifiEnterpriseConfig;
23aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
245ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadhamimport android.net.wifi.WifiWakeReasonAndCounts;
2503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.WifiManager;
26e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
27dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiSsid;
28155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
29155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
30155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
3103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
32f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
3318786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.os.SystemProperties;
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
35155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
37e26ad459b63271548abbdeba4f8d77fcca9f88bdxinheimport android.content.Context;
38e26ad459b63271548abbdeba4f8d77fcca9f88bdxinheimport android.content.Intent;
3918786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.app.AlarmManager;
40e26ad459b63271548abbdeba4f8d77fcca9f88bdxinheimport android.app.PendingIntent;
41e26ad459b63271548abbdeba4f8d77fcca9f88bdxinheimport android.content.IntentFilter;
42e26ad459b63271548abbdeba4f8d77fcca9f88bdxinheimport android.content.BroadcastReceiver;
43c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadhamimport com.android.server.connectivity.KeepalivePacketData;
442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.NetworkDetail;
452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.SupplicantBridge;
462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.Utils;
475d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Willsimport com.android.server.wifi.util.InformationElementUtil;
48c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
4918786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport libcore.util.HexEncoding;
5018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
515cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.ByteBuffer;
525cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.CharBuffer;
535cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharacterCodingException;
545cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharsetDecoder;
555cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.StandardCharsets;
56155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
57155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
58155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
599ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport java.util.Set;
6018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
61155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
63155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
64155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
67155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
71b66b29a00da970ee75052e24f592c8d6c16bd0edxinhe    private static boolean DBG = false;
72155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
7318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
7418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Hold this lock before calling supplicant or HAL methods
7518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * it is required to mutually exclude access to the driver
7618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
7718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final Object mLock = new Object();
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
7918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final LocalLog mLocalLog = new LocalLog(16384);
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static LocalLog getLocalLog() {
8218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return mLocalLog;
8318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
90155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
9518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Singleton WifiNative instances
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
9718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative wlanNativeInterface =
9818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            new WifiNative(SystemProperties.get("wifi.interface", "wlan0"));
9918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getWlanNativeInterface() {
10018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return wlanNativeInterface;
10118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    //STOPSHIP: get interface name from native side
10418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative p2pNativeInterface = new WifiNative("p2p0");
10518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getP2pNativeInterface() {
10618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return p2pNativeInterface;
10718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
11018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mTAG;
11118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfaceName;
11218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfacePrefix;
11318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
11418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private Context mContext = null;
11518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private PnoMonitor mPnoMonitor = null;
11618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void initContext(Context context) {
11718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (mContext == null && context != null) {
11818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mContext = context;
11918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mPnoMonitor = new PnoMonitor();
12018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
12118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private WifiNative(String interfaceName) {
124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
126e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!interfaceName.equals("p2p0")) {
128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // commands for p2p0 interface don't need prefix
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName() {
13618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return mInterfaceName;
137e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
138e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
13918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Note this affects logging on for all interfaces
140ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
141ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
142ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
143ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
144ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
145ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
146ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
147ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
14818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private void localLog(String s) {
14918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (mLocalLog != null)
15018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mLocalLog.log(mInterfaceName + ": " + s);
15118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
15518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
15618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Driver and Supplicant management
15718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
15818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean loadDriverNative();
15918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean loadDriver() {
16018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
16118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return loadDriverNative();
16218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
16518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean isDriverLoadedNative();
16618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isDriverLoaded() {
16718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
16818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return isDriverLoadedNative();
16918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean unloadDriverNative();
17318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean unloadDriver() {
17418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
17518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return unloadDriverNative();
17618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
17718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
17818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
17918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean startSupplicantNative(boolean p2pSupported);
18018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startSupplicant(boolean p2pSupported) {
18118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
18218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return startSupplicantNative(p2pSupported);
18318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
18418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
18518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
18618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* Sends a kill signal to supplicant. To be used when we have lost connection
18718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills       or when the supplicant is hung */
18818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean killSupplicantNative(boolean p2pSupported);
18918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean killSupplicant(boolean p2pSupported) {
19018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
19118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return killSupplicantNative(p2pSupported);
19218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
19518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean connectToSupplicantNative();
196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
19718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
198b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "connectToSupplicant");
199b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            return connectToSupplicantNative();
200b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
20318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static void closeSupplicantConnectionNative();
204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
20518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
206b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "closeSupplicantConnection");
207b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            closeSupplicantConnectionNative();
208b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
21118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
21218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wait for the supplicant to send an event, returning the event string.
21318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * @return the event string sent by the supplicant.
21418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
21518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static String waitForEventNative();
216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
22118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
22218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
22318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Supplicant Command Primitives
22418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
22518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native boolean doBooleanCommandNative(String command);
22618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
22718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native int doIntCommandNative(String command);
22818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
22918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native String doStringCommandNative(String command);
23018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
23418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
2367b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
2370888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
242a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    private boolean doBooleanCommandWithoutLogging(String command) {
243a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (DBG) Log.d(mTAG, "doBooleanCommandWithoutLogging: " + command);
244a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        synchronized (mLock) {
245a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
246a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            if (DBG) Log.d(mTAG, command + ": returned " + result);
247a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return result;
248a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
249a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    }
250a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
25418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
2567b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
2630888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2640888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2650888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2660888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2670888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
2680888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
27018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
27240ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
27340ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
27440ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
2757b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (!command.startsWith("STATUS-")) {
2767b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog(toLog + " -> " + result);
2777b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
27840ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
27940ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
2850888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2860888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2870888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2880888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2890888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
29027355a942653264388e909a4276196ee63e57811vandwalle        }
29127355a942653264388e909a4276196ee63e57811vandwalle        synchronized (mLock) {
292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
29618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String doCustomSupplicantCommand(String command) {
29718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doStringCommand(command);
29818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
29918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
30018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
30118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wrappers for supplicant commands
30218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
308ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
309ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
310ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
311ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
312a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
313a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
314a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
315a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
31618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
31718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int SCAN_WITHOUT_CONNECTION_SETUP          = 1;
31818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int SCAN_WITH_CONNECTION_SETUP             = 2;
31918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
3209ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public boolean scan(int type, Set<Integer> freqs) {
3219ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        if(freqs == null) {
3229ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            return scan(type, (String)null);
3239ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3249ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        else if (freqs.size() != 0) {
3259ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            StringBuilder freqList = new StringBuilder();
3269ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            boolean first = true;
3279ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            for (Integer freq : freqs) {
3289ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                if (!first)
3299ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                    freqList.append(",");
3309ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                freqList.append(freq.toString());
3319ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                first = false;
3329ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            }
3339ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            return scan(type, freqList.toString());
3349ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3359ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        else {
3369ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            return false;
3379ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3389ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
3399ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
3409ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    private boolean scan(int type, String freqList) {
341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (type == SCAN_WITHOUT_CONNECTION_SETUP) {
342a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY");
343a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (type == SCAN_WITH_CONNECTION_SETUP) {
345a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN");
346a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN freq=" + freqList);
347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            throw new IllegalArgumentException("Invalid scan type");
349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
366e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
367e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
368e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
369e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
376a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (name.equals(WifiConfiguration.pskVarName)
377a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham                || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
378a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value);
379a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        } else {
380a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
381a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
395f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
396f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
397f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
398f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
399ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
400ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
401ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
402ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
403ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
404ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
405f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
406f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
408ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
409ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + " disableOthers=" + disableOthers);
410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("SELECT_NETWORK " + netId);
412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("ENABLE_NETWORK " + netId);
414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
418f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4220047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
4230047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
4240047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
4250047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
4260047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
428f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
433f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
43821bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
44399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
44699d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
44799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
44899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
44999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
45099d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
45199d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
45299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
45399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
464a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
465a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
4702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
4722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
4732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
4752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
4762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
4772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
4782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
4792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
4802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
4812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
4822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
4832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
48977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        1                       0     2
49077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
49177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        0                       1     1   -> 9
49277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
49377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       1     9   -> d
49477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
49577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       0     8
49677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
49777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         1                        1                       1     7
49877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
49977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
50077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
50177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
50277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
50377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  wpa_supplicant_ctrl_iface_bss:4315
50477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  print_bss_info
505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
5062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
5072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
5082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
5092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
5112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
5122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
5132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
5142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
5152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
5162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
5172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
5182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
5192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
5202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
5222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
5232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
5242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
5252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
5262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
5272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
5292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
5302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
5322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
5352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
5362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
5372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
5382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
5392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
5412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
5422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
5432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
5442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
5452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
5465d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
5472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
5482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
5502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
5512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
5522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
5532afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
5542afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
5552afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
5562afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
5572afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
5582afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
5592afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
5602afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
5612afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
5622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
5632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
5642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
5652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
5662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
5672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
5682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
5692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
5702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
5712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
5722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
5732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
5742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
5752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
5762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
5772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
5782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
5792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
5802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
5812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
5822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
5832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
5842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
5852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
5865d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
5872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
5882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
5892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
5902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
5912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
5922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
5932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
5942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
5955d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
5965d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
5975d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
5985d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
5995d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
6005d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
6015d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6025d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6035d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
6045d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
6055d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
6065d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6075d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
6085d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
6132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
6145d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, "HSNwk: '" + networkDetail);
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6209ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
6219ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                                    level, freq, tsf);
6229ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            scan.getScanResult().informationElements = infoElements;
6239ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
6345d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
6392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
64077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
64177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
643446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
644446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
645446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
646446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
647446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
648446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
649446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
650446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
651446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
652446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
653446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
654446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
655446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
656446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
657446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
65856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
659446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
660446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
661446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
662446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
663446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
664446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
665446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
666446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
667446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
668446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
669446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
670446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
671446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
672446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
673446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
674446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
707155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
74403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
74503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
74603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
74703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
74803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
74903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
75003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
75103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
75303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
75403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
75503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
75603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
75703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
75803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
75903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
76003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
76103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
76418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
76518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
76618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
7677ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
7687ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
7697ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
7707ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
7717ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
7727ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
7737ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
7747ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
7950a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
7960a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
7970a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
7980a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
8210465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
8220465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
8230465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
8240465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
827e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    //PNO Monitor
828e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    private class PnoMonitor {
829e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final int MINIMUM_PNO_GAP = 5 * 1000;
830e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final String ACTION_TOGGLE_PNO =
831e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            "com.android.server.Wifi.action.TOGGLE_PNO";
832e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        long mLastPnoChangeTimeStamp = -1L;
833e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mExpectedPnoState = false;
834e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mCurrentPnoState = false;;
835e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mWaitForTimer = false;
836e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        final Object mPnoLock = new Object();
837e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final AlarmManager mAlarmManager =
838e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
839e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final PendingIntent mPnoIntent;
840e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
841e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public PnoMonitor() {
842e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            Intent intent = new Intent(ACTION_TOGGLE_PNO, null);
843e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            intent.setPackage("android");
844e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
845e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
846e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mContext.registerReceiver(
847e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new BroadcastReceiver() {
848e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    @Override
849e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    public void onReceive(Context context, Intent intent) {
850e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        synchronized(mPnoLock) {
851e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (DBG) Log.d(mTAG, "PNO timer expire, PNO should change to " +
852e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    mExpectedPnoState);
853e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (mCurrentPnoState != mExpectedPnoState) {
854e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to "
855e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                        + mExpectedPnoState);
856e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                boolean ret = setPno(mExpectedPnoState);
857e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (!ret) {
858e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    Log.e(mTAG, "set PNO failure");
859e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                }
860e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            } else {
861e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "Do not change PNO since current is expected");
862e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            }
863e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            mWaitForTimer = false;
864e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        }
865e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
866e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                },
867e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new IntentFilter(ACTION_TOGGLE_PNO));
868e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
869e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
870e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private boolean setPno(boolean enable) {
871e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            String cmd = enable ? "SET pno 1" : "SET pno 0";
872e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            boolean ret = doBooleanCommand(cmd);
873e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mLastPnoChangeTimeStamp = System.currentTimeMillis();
874e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            if (ret) {
875e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                mCurrentPnoState = enable;
876e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
877e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return ret;
878e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
879e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
880e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public boolean enableBackgroundScan(boolean enable) {
881e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            synchronized(mPnoLock) {
882e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                if (mWaitForTimer) {
883e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    //already has a timer
884e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    mExpectedPnoState = enable;
885e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (DBG) Log.d(mTAG, "update expected PNO to " +  mExpectedPnoState);
886e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                } else {
887e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (mCurrentPnoState == enable) {
888e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        return true;
889e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
890e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp;
891e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (timeDifference >= MINIMUM_PNO_GAP) {
892e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        return setPno(enable);
893e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    } else {
894e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mExpectedPnoState = enable;
895e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mWaitForTimer = true;
896e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference);
897e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
898e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                System.currentTimeMillis() + timeDifference, mPnoIntent);
899e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
900e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                }
901e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                return true;
902e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
903e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
904e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
905e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
906c97b98d6877f6603a6a0eee820576f59e7da5e52Pierre Vandwalle    public boolean enableBackgroundScan(boolean enable) {
907e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        if (mPnoMonitor != null) {
908e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return mPnoMonitor.enableBackgroundScan(enable);
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
910e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return false;
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
914f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
915f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
916f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
917f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
918f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
919f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
920f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
921f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
924155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
925155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
92656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
92756845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
92856845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
92956845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
93056845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
93156845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
93256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
93356845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
93618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            synchronized (mLock) {
93718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
93818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
93918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
949155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
950155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
951155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
957155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
958155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
959155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
963155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
964155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
973155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
974155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
975155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
976155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
977155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
979155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
101633b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
101718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
101818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
101918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
102033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
102133b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1022f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1023f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
102418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
102533b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
102633b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1027ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
102818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1029ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1030ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1032155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
115518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
115618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
115718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
115818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
115918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
116018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
116418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
116518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
116618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
116718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
116818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
116918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
117018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
117118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
118018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
130236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
130336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
130427f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
130527f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
130636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
130736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
130836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
130927f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        synchronized (mLock) {
131027f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
131127f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
131227f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
131327f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
131436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
131536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
131636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
131736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
131836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
131936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
132036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
132136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
132236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
132536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
132636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
132736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
139418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
139518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
139618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
139718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
139818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
139918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
140018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
141118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
141218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
141318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
141518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
141618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
141718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
141818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
141918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
142018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
142118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
142218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
142318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
142418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
142518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
142618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
142718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
142818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
142918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
14603f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1461446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1462446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1463446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1464446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1465f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1466f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1467f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1468f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1469f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1470f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1471f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1472f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1473f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1474f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1475f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1476f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1477f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1478f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1479f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1480f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1481f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1482f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1483f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1484f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1485f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1486f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1487f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
14887f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
14897f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
149018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
149118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
149218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
149318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
149418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
149518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1496b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1497f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1498f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1499aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sWlan0Index = -1;
1500aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sP2p0Index = -1;
1501f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1502f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
15037f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1504b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1505b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1506b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
15077f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1508b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
15097ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1510b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
15117ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
15127ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
15137ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
15147ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
151518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1516d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1517d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1518d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1519d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1520d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1521d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1522f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        mLocalLog.log(debugLog);
1523d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1524aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
1525cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1526f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1527f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1528aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1529aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1530f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (DBG) mLocalLog.log("Could not start hal");
1531f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1532aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1533aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
15347ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
15357ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
15367ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
153718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1538f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        synchronized (mLock) {
153971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1540f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1541f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1542f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1543f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1544f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1545f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1546f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1547f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1548f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1549f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1550f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1551f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sP2p0Index = -1;
1552f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1553f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
15547ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
15557f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
155618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
155771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
155871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1559b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
15607f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
156118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getInterfaces() {
1562aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
156371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1564f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sWifiIfaceHandles == null) {
1565f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int num = getInterfacesNative();
1566f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int wifi_num = 0;
1567f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    for (int i = 0; i < num; i++) {
1568f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        String name = getInterfaceNameNative(i);
1569f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        Log.i(TAG, "interface[" + i + "] = " + name);
1570f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        if (name.equals("wlan0")) {
1571f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sWlan0Index = i;
1572f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1573f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        } else if (name.equals("p2p0")) {
1574f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sP2p0Index = i;
1575f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1576f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        }
157702a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1578f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return wifi_num;
1579f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1580f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return sWifiIfaceHandles.length;
1581aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
158202a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            } else {
1583f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
1584e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1585e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
15867f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
15877f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1588b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
158918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
159018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
159118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
159218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
15937f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
15947f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1595e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1596297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1597e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1598e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1599e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1600297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
16017d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1602e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1603e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1604e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
160518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1606af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        synchronized (mLock) {
1607af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1608af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1609e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1610e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1611b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1612b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1613e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1614b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1615b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
161683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1617b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1618d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
16197f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1620e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1621e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int frequency;
1622e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int dwell_time_ms;
1623e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        boolean passive;
16247f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
16257f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1626e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1627e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int bucket;
1628e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int band;
1629e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int period_ms;
1630e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int report_events;
1631e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_channels;
1632daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        ChannelSettings channels[];
1633e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
16347f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1635e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1636e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int base_period_ms;
1637e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int max_ap_per_scan;
163883a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        int report_threshold_percent;
163983a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        int report_threshold_num_scans;
1640e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_buckets;
1641daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        BucketSettings buckets[];
1642e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
16437f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1644b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
1645e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onScanResultsAvailable();
1646476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        void onFullScanResult(ScanResult fullScanResult);
164783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanStatus();
164883a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
1649b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1650e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1651e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
165218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
165318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onScanResultsAvailable(int id) {
165418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
165518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
165618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onScanResultsAvailable();
1657b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
1658b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    }
1659b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
1660b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
1661b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    private static int WIFI_SCAN_BUFFER_FULL = 0;
1662b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    private static int WIFI_SCAN_COMPLETE = 1;
1663b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
166418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
166518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onScanStatus(int status) {
166618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
1667b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        if (status == WIFI_SCAN_BUFFER_FULL) {
1668b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            /* we have a separate event to take care of this */
1669b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        } else if (status == WIFI_SCAN_COMPLETE) {
167018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (handler != null) {
167118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                handler.onScanStatus();
1672b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            }
1673b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
1674e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1675e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
167618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
16775cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
16785cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
16795cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
16805cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
16815cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
16825cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
16835cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
16845cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
16855cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
16865cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
16875cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
16885cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
16895cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
16905cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
16915cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
16925cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
16935cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
16945cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
16955cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
16965cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
16975cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
16985cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
16995cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
17005cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
17015cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
17025cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17035cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
17045cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
17055cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
170618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
17075cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
17085cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
17095cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
17105cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
17115cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17125cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
17135cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
17145cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
17155cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
17165cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
171718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void populateScanResult(ScanResult result, byte bytes[], String dbg) {
1718dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (bytes == null) return;
1719dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
17205d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
17215d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
17225d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
17235d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
17245d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
17255d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
17265d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
17275d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
17285d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                InformationElementUtil.parseInformationElements(bytes);
17295d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
17305d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
17315d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
17325d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
17335d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
17345d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
17355d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
1736243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1737243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1738d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
17395d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
1740d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
1741d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
1742d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
1743d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
1744d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
1745243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
17465d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
17475d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
17485d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
17495d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
1750243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
17515d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
17525d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
1753243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
1754243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1755243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
1756dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth +
1757243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    " PrimaryFreq: " + result.frequency +" mCenterfreq0: " + result.centerFreq0 +
17585d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder ?
1759243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    "Support RTT reponder: " : "Do not support RTT responder"));
1760f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1761f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1762476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1763dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1764dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
176518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
176618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onFullScanResult(int id, ScanResult result, byte bytes[]) {
1767dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " +
1768dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                "num = " + bytes.length);
1769dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
177018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
177118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
177218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            populateScanResult(result, bytes, " onFullScanResult ");
177318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onFullScanResult(result);
1774dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
17757f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17767f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1777b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1778b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1779b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
17807f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
178118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
17827f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
178371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1784f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
1785f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
1786f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
1787b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1788f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
17897f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1790f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
1791e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1792f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
1793f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
1794b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1795f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1796f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
1797f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
1798f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1799f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1800f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1801f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1802f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1803f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1804e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1805e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1806e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
18077f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18087f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
180918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
1810b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        synchronized (mLock) {
181171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1812ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
1813ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
1814ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
1815f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
1816f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
1817f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
1818f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1819b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1820b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1821b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
182218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
18237f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
182471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1825f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1826f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
1827f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1828f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
1829f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1830f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
1831f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1832b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1833b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1834b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1835b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
183618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
1837b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        synchronized (mLock) {
183871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1839f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
18406f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
1841f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
1842f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
1843f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
1844f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
1845f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
184683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
1847f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
1848f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
1849f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
185083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
1851b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1852e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1853e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1854e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
185518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
1856aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
185771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1858f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getScanResultsNative(sWlan0Index, flush);
1859f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1860f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
1861f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1862aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1863e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1864e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1865b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
1866d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
1867d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
1868e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1869e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1870b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
1871b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
1872e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1873b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
1874e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
1875b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
1876e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
187718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
187818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
1879e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
188071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1881f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
1882f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1883f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1884f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
1885f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1886f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1887f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
1888f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
1889f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
1890f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1891f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1892e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1893f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1894f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1895e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1896e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1897e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1898e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1899e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
190018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
1901e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
190271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1903f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
1904f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
1905f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
1906f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
1907f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1908e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
19097f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
19107f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
1911e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
191218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
191318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
191418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
191518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
191618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
191718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
191818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
191918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
1920d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
1921d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
1922d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
192318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
192418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
192518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
192618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
192718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
192818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
192918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
193018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
1931aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1932e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1933e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1934b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
1935e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
1936e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1937e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1938b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
1939b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
1940e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1941b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
1942e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
1943b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
1944e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
194518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
1946b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
1947e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
194871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1949f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
1950f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1951f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1952f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
1953f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1954f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1955f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
1956851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
1957851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
1958f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
1959f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1960f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1961e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1962f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1963f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1964e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1965e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1966e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1967e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1968e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1969e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
197018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
1971e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
197271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1973f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
1974f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
1975f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
1976f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
1977f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1978e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1979e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1980e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1981e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
198218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
198318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
198418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
198518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
198618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
198718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
1988f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
198918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
1990aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1991e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1992e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
199318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
1994200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
1995200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
1996aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
199771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1998aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
1999f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2000f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2001f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2002aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
20035c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
20045c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
200518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2006d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2007d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        synchronized (mLock) {
200871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2009d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2010d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2011d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2012d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2013d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2014c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
201518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2016f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        synchronized (mLock) {
201771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2018f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2019f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2020f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2021f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2022f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2023f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2024a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2025143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2026143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2027143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2028143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2029143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2030143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2031143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2032143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2033143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
203418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
203518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
203618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
203718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
203802a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
203918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2040143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2041143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
204218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
204318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2044143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2045143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2046143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2047143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2048143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2049143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2050143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2051143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
205218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2053143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2054143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        synchronized (mLock) {
205571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2056f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2057f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2058f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2059f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2060f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2061f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2062f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2063f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2064f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2065143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2066f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2067143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2068143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2069143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2070143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
207118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
207218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        synchronized (mLock) {
207371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2074f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2075f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2076f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2077143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2078f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2079f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2080f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2081f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
20825cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2083f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2084f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
20855cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2086f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2087f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2088143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2089143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2090143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2091143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2092143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2093042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
2094042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2095042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
209618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2097042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        synchronized (mLock) {
209871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2099042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2100042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2101042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2102042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2103042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2104042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2105efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2106efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2107efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2108efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
210918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2110efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        synchronized (mLock) {
2111ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2112ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
211318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2114ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2115ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2116efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2117efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
21180465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2119ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
212018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2121ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        synchronized (mLock) {
2122ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2123ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
212418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2125ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2126ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2127ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2128ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
21290465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
21300465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
213118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
21320465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        synchronized (mLock) {
213371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
21340465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
21350465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
21360465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
21370465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
21380465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
21390465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2140b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
2141b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    private static native boolean toggleInterfaceNative(int on);
214218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean toggleInterface(int on) {
2143b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        synchronized (mLock) {
214471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
214518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return toggleInterfaceNative(on);
2146b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2147b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2148b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2149b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2150b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
215112cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
215212cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
215318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
215412cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        synchronized (mLock) {
215571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
215612cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
215718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
215812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
215912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
216012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
216112cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2162939177ff615062ec826601d536466875d8457375xinhe
2163939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
216418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2165939177ff615062ec826601d536466875d8457375xinhe        synchronized (mLock) {
216671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2167939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2168939177ff615062ec826601d536466875d8457375xinhe            } else {
2169939177ff615062ec826601d536466875d8457375xinhe                return false;
2170939177ff615062ec826601d536466875d8457375xinhe            }
2171939177ff615062ec826601d536466875d8457375xinhe        }
2172939177ff615062ec826601d536466875d8457375xinhe    }
2173939177ff615062ec826601d536466875d8457375xinhe
2174d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2175d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2176d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2177d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2178d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2179d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2180d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2181d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2182d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
218318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2184d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        synchronized (mLock) {
2185f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2186f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2187d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2188d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2189d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2190d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2191d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2192d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2193d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2194d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2195d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2196d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2197d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2198d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
219918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2200d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        synchronized (mLock) {
220171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2202d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2203d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2204d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2205d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2206d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2207d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2208d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2209d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2210d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2211d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2212d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2213d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2214d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2215d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2216d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2217d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2218d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2219d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2220d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2221d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
222218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2223d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        synchronized (mLock) {
222471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2225d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2226d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2227d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2228d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2229d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2230d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2231d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
223218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
223318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
223418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
223518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
223618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
223718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
223818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
223918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2240d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2241d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2242a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2243a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2244a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
22457d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2246a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
22470bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
22480bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2249a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2250a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2251a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2252a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
225318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
22540bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
225518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
225618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
225718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
225803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
225903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
226018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
22610bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
226218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
226318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
226418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
226503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
226603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2267b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2268b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
226918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
22700bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        synchronized (mLock) {
2271b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2272b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2273b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2274b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2275b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2276b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2277b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2278b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2279b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2280b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2281b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2282b797893fc1966803d0c013faac42e6396a37a384xinhe            }
228303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
228403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
228503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
228603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
22870bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
228818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
228903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
229003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
229171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
229203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
229303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
229403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
229503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
229603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
229703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
229803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
229903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
230003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
230118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
230203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
230371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
230403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
230503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2306f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
230703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
230803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
230903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
231003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2311b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
231218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2313b797893fc1966803d0c013faac42e6396a37a384xinhe        synchronized (mLock) {
2314b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2315b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2316b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2317b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2318b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2319b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2320b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2321b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2322b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2323b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2324b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2325b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2326b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2327b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2328b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2329b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2330b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2331b797893fc1966803d0c013faac42e6396a37a384xinhe
233203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
233318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
233403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
233571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
233603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
233703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2338f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
233903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
234003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
234103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
234203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
234303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
234403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
234518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
234603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
234771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
234803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
234903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2350f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
235103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
235203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
235303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
235403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
23550bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
23560bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
23570bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
23580bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
23590bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
23600bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
23610bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
23620bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
23630bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
23640bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
23650bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
23660bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
23670bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
23680bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
23690bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
23700bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
23710bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
23720bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
23730bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
23740bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
237518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
237603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
237771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
237803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
237903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
238003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
238103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
238203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
238303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
238403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
238503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
238618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
238703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
238871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
238903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
239003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
239103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
239203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
239303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
239403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
239503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
239618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
239718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
239803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
239998dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
240098dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
240198dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
240298dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
240398dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
240403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2405127f7244183786e6ccae09e81eeccdac31973e69xinhe
240603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
240718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
240803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
240971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
24100bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
24110bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
241203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
24130bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
24140bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
24150bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
241603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
241703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2418f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2419a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2420a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2421dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2422dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
2423dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    /* Configure ePNO */
2424dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
242518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* pno flags, keep these values in sync with gscan.h */
242618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_OPEN  = 1; // open
242718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_PSK   = 2; // WPA_PSK or WPA2PSK
242818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_EAPOL = 4; // any EAPOL
242918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
243018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether directed scan needs to be performed (for hidden SSIDs)
243118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_DIRECTED_SCAN = 1;
243218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on A band
243318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_A_BAND = 2;
243418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on G band
243518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_G_BAND = 4;
243618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether strict matching is required (i.e. firmware shall not match on the entire SSID)
243718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_STRICT_MATCH = 8;
243818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
243918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiPnoNetwork {
2440dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        String SSID;
2441dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int rssi_threshold;
2442dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int flags;
2443dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int auth;
2444dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        String configKey; // kept for reference
2445dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2446dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        WifiPnoNetwork(WifiConfiguration config, int threshold) {
2447dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.SSID == null) {
2448dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                this.SSID = "";
244918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                this.flags = WIFI_PNO_FLAG_DIRECTED_SCAN;
2450dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
2451dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                this.SSID = config.SSID;
2452dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2453dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            this.rssi_threshold = threshold;
2454dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
245518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_PSK;
2456dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) ||
2457dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
245818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_EAPOL;
2459dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.wepKeys[0] != null) {
246018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_OPEN;
2461dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
246218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_OPEN;
2463dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
246418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
246518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            flags |= WIFI_PNO_FLAG_A_BAND | WIFI_PNO_FLAG_G_BAND;
2466dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            configKey = config.configKey();
2467dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2468dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2469dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        @Override
2470dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        public String toString() {
2471dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2472dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(this.SSID);
2473dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" flags=").append(this.flags);
2474dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" rssi=").append(this.rssi_threshold);
2475dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" auth=").append(this.auth);
2476dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return sbuf.toString();
2477dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2478dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2479dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2480dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    public static interface WifiPnoEventHandler {
2481dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        void onPnoNetworkFound(ScanResult results[]);
2482dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2483dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2484dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static WifiPnoEventHandler sWifiPnoEventHandler;
2485dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2486dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static int sPnoCmdId = 0;
2487dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2488dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]);
2489dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
249018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setPnoList(WifiPnoNetwork list[],
2491dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                                  WifiPnoEventHandler eventHandler) {
2492dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2493dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2494dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        synchronized (mLock) {
249571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2496dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2497f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2498dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2499f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiPnoEventHandler = eventHandler;
2500f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setPnoListNative(sWlan0Index, sPnoCmdId, list)) {
2501f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2502f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2503dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2504dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2505f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sWifiPnoEventHandler = null;
2506f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
2507dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2508dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2509dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
251018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
251118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onPnoNetworkFound(int id, ScanResult[] results) {
2512dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2513dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2514dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2515dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2516dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2517dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2518dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
251918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiPnoEventHandler handler = sWifiPnoEventHandler;
252018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
252118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
252218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
252318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
2524dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
252518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                populateScanResult(results[i], results[i].bytes, "onPnoNetworkFound ");
252618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2527dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
252818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
252918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
253018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
253118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
253218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
2533dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2534d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2535d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
253618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiLazyRoamParams {
2537d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_threshold;
2538d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_threshold;
2539d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_factor;
2540d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_factor;
2541d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_max_boost;
2542d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int lazy_roam_hysteresis;
2543d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int alert_roam_rssi_trigger;
2544dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2545d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        WifiLazyRoamParams() {
2546d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2547d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
2548d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        @Override
2549d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        public String toString() {
2550d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2551d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_threshold=").append(this.A_band_boost_threshold);
2552d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_threshold=").append(this.A_band_penalty_threshold);
2553d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_factor=").append(this.A_band_boost_factor);
2554d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_factor=").append(this.A_band_penalty_factor);
2555d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_max_boost=").append(this.A_band_max_boost);
2556d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" lazy_roam_hysteresis=").append(this.lazy_roam_hysteresis);
2557d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" alert_roam_rssi_trigger=").append(this.alert_roam_rssi_trigger);
2558d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            return sbuf.toString();
2559d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2560d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2561d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
25629ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setLazyRoamNative(int iface, int id,
2563d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle                                              boolean enabled, WifiLazyRoamParams param);
2564d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
256518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLazyRoam(boolean enabled, WifiLazyRoamParams params) {
2566d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        synchronized (mLock) {
256771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2568d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle                sPnoCmdId = getNewCmdIdLocked();
25699ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return setLazyRoamNative(sWlan0Index, sPnoCmdId, enabled, params);
2570d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            } else {
2571d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle                return false;
2572d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            }
2573d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2574d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
25759ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
25769ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
25779ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
25789ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
257918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
25809ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
25819ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
25829ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
25839ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
25849ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
25859ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
25869ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        synchronized (mLock) {
258771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2588f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2589f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
2590f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
25919ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
25929ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
25939ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
25949ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
25959ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
25965caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle    private native static boolean setSsidWhitelistNative(int iface, int id, String list[]);
25975caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle
259818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setSsidWhitelist(String list[]) {
25995caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle        int size = 0;
26005caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle        if (list != null) {
26015caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle            size = list.length;
26025caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle        }
26035caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle        Log.e(TAG, "setSsidWhitelist cmd " + sPnoCmdId + " size " + size);
26045caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle
26055caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle        synchronized (mLock) {
260671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2607f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
26085caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle
2609f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setSsidWhitelistNative(sWlan0Index, sPnoCmdId, list);
2610f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
26115caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle                return false;
26125caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle            }
26135caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle        }
26145caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle    }
2615c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2616c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
2617c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
2618c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
261918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2620c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
2621c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
26223bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
26233bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
26243bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
26253bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
26263bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
26273bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
26283bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
2629c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        synchronized (mLock) {
2630c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2631c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
263218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
2633c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2634c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2635c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2636c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2637c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2638c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2639c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
2640c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
264118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2642c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
2643c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
2644c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        synchronized (mLock) {
2645c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2646c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
2647c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2648c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2649c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2650c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2651c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2652aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2653aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
2654aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
2655aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2656aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2657aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
2658aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
265918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
266018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
266118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
266218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
266318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
266418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2665aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2666aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2667aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
2668aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
2669aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2670aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
2671aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
267218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
2673aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
2674aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
2675aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        synchronized (mLock) {
267618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
2677aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2678ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2679ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
2680ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2681ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
2682aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
2683aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
2684ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
2685ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
2686ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
2687ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
2688ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2689ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2690aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2691aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2692aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2693aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2694aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2695aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2696aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
2697aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
269818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
2699aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
2700aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        synchronized (mLock) {
2701aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2702ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
2703ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2704ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
2705ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2706ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
2707ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2708aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2709aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2710aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2711aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2712aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
27135ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
27145ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
2715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
2716