WifiNative.java revision 09b1d0786d05436d524d7556c269e665a0962ee6
1155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/*
2155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Copyright (C) 2008 The Android Open Source Project
3155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
4155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Licensed under the Apache License, Version 2.0 (the "License");
5155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * you may not use this file except in compliance with the License.
6155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * You may obtain a copy of the License at
7155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
8155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *      http://www.apache.org/licenses/LICENSE-2.0
9155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
10155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Unless required by applicable law or agreed to in writing, software
11155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * distributed under the License is distributed on an "AS IS" BASIS,
12155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * See the License for the specific language governing permissions and
14155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * limitations under the License.
15155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
16155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepackage com.android.server.wifi;
18155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.annotation.Nullable;
20fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.app.AlarmManager;
21fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.app.PendingIntent;
22fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.content.BroadcastReceiver;
232a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.Context;
242a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.Intent;
252a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.IntentFilter;
26e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensenimport android.net.apf.ApfCapabilities;
27143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpandeimport android.net.wifi.RttManager;
2868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.net.wifi.RttManager.ResponderConfig;
29e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
30dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiConfiguration;
3118786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.net.wifi.WifiEnterpriseConfig;
32aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
3303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.WifiManager;
34e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
35dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiSsid;
362a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.net.wifi.WifiWakeReasonAndCounts;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
39155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
4003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
41f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
4218786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.os.SystemProperties;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
46fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
4709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawalimport com.android.internal.annotations.Immutable;
48c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadhamimport com.android.server.connectivity.KeepalivePacketData;
492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.NetworkDetail;
502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.SupplicantBridge;
512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.Utils;
525d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Willsimport com.android.server.wifi.util.InformationElementUtil;
53c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
5418786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport libcore.util.HexEncoding;
5518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
56fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONException;
57fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONObject;
58fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
59fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.io.UnsupportedEncodingException;
60fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLDecoder;
61fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLEncoder;
625cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.ByteBuffer;
635cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.CharBuffer;
645cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharacterCodingException;
655cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharsetDecoder;
665cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.StandardCharsets;
67155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
683571366ac36c70746b9f013ec2b54482861c9292Randy Panimport java.util.BitSet;
69fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.HashMap;
70fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Iterator;
71155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
72155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
73fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Map;
749ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport java.util.Set;
7518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
76fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
79155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
87b66b29a00da970ee75052e24f592c8d6c16bd0edxinhe    private static boolean DBG = false;
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
9018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Hold this lock before calling supplicant or HAL methods
9118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * it is required to mutually exclude access to the driver
9218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
93956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static final Object sLock = new Object();
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
95956f54b391677d78379729dd14518edddf3c7660Etan Cohen    private static final LocalLog sLocalLog = new LocalLog(16384);
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static LocalLog getLocalLog() {
98956f54b391677d78379729dd14518edddf3c7660Etan Cohen        return sLocalLog;
9918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
11018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
11118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Singleton WifiNative instances
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
11318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative wlanNativeInterface =
114c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            new WifiNative(SystemProperties.get("wifi.interface", "wlan0"), true);
11518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getWlanNativeInterface() {
11618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return wlanNativeInterface;
11718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
119c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang    private static WifiNative p2pNativeInterface =
120c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            // commands for p2p0 interface don't need prefix
121c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            new WifiNative(SystemProperties.get("wifi.direct.interface", "p2p0"), false);
12218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getP2pNativeInterface() {
12318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return p2pNativeInterface;
12418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mTAG;
12818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfaceName;
12918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfacePrefix;
13018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
13118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private Context mContext = null;
13218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private PnoMonitor mPnoMonitor = null;
13318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void initContext(Context context) {
13418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (mContext == null && context != null) {
13518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mContext = context;
13618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mPnoMonitor = new PnoMonitor();
13718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
13818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
140c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang    private WifiNative(String interfaceName,
141c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang                       boolean requiresPrefix) {
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
144e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
145c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang        if (requiresPrefix) {
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName() {
15318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return mInterfaceName;
154e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
155e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
15618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Note this affects logging on for all interfaces
157ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
158ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
159ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
160ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
161ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
162ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
163ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
164ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
16518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private void localLog(String s) {
166956f54b391677d78379729dd14518edddf3c7660Etan Cohen        if (sLocalLog != null) sLocalLog.log(mInterfaceName + ": " + s);
16718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
17118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
17218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Driver and Supplicant management
17318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
17418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean loadDriverNative();
17518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean loadDriver() {
176956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
17718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return loadDriverNative();
17818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
18118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean isDriverLoadedNative();
18218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isDriverLoaded() {
183956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return isDriverLoadedNative();
18518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
18818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean unloadDriverNative();
18918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean unloadDriver() {
190956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return unloadDriverNative();
19218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
19318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
19418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
19518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean startSupplicantNative(boolean p2pSupported);
19618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startSupplicant(boolean p2pSupported) {
197956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return startSupplicantNative(p2pSupported);
19918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
20018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
20118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
20218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* Sends a kill signal to supplicant. To be used when we have lost connection
20318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills       or when the supplicant is hung */
20418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean killSupplicantNative(boolean p2pSupported);
20518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean killSupplicant(boolean p2pSupported) {
206956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
20718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return killSupplicantNative(p2pSupported);
20818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
21118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean connectToSupplicantNative();
212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
213956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
214b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "connectToSupplicant");
215b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            return connectToSupplicantNative();
216b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
21918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static void closeSupplicantConnectionNative();
220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
221956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
222b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "closeSupplicantConnection");
223b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            closeSupplicantConnectionNative();
224b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
22718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
22818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wait for the supplicant to send an event, returning the event string.
22918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * @return the event string sent by the supplicant.
23018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
23118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static String waitForEventNative();
232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
23718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
23818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
23918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Supplicant Command Primitives
24018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
24118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native boolean doBooleanCommandNative(String command);
24218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native int doIntCommandNative(String command);
24418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native String doStringCommandNative(String command);
24618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
249956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
25018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
2527b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
2530888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
258a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    private boolean doBooleanCommandWithoutLogging(String command) {
259a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (DBG) Log.d(mTAG, "doBooleanCommandWithoutLogging: " + command);
260956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
261a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
262a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            if (DBG) Log.d(mTAG, command + ": returned " + result);
263a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return result;
264a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
265a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    }
266a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham
267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
269956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
27018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
2727b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
2790888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2800888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2810888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2820888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2830888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
2840888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
285956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
28618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
28840ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
28940ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
29040ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
2917b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (!command.startsWith("STATUS-")) {
2927b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog(toLog + " -> " + result);
2937b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
29440ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
29540ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
3010888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
3020888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
3030888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
3040888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
3050888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
30627355a942653264388e909a4276196ee63e57811vandwalle        }
307956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
31218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String doCustomSupplicantCommand(String command) {
31318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doStringCommand(command);
31418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
31518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
31618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
31718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wrappers for supplicant commands
31818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
324ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
325ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
326ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
327ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
328a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
329a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
330a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
331a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
3323d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    /**
3333d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * Create a comma separate string from integer set.
3343d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param values List of integers.
3353d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @return comma separated string.
3363d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     */
3373d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    private static String createCSVStringFromIntegerSet(Set<Integer> values) {
3383d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        StringBuilder list = new StringBuilder();
3393d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        boolean first = true;
3403d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        for (Integer value : values) {
3413d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            if (!first) {
3423d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius                list.append(",");
3433d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            }
3443d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            list.append(value);
3453d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            first = false;
3463d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3473d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return list.toString();
3483d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    }
34918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
350ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    /**
351ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * Start a scan using wpa_supplicant for the given frequencies.
3523d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param freqs list of frequencies to scan for, if null scan all supported channels.
3533d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param hiddenNetworkIds List of hidden networks to be scanned for.
354ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     */
3553d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    public boolean scan(Set<Integer> freqs, Set<Integer> hiddenNetworkIds) {
3563d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String freqList = null;
3573d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String hiddenNetworkIdList = null;
3583d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqs != null && freqs.size() != 0) {
3593d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            freqList = createCSVStringFromIntegerSet(freqs);
3603d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3613d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIds != null && hiddenNetworkIds.size() != 0) {
3623d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            hiddenNetworkIdList = createCSVStringFromIntegerSet(hiddenNetworkIds);
3639ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3643d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return scanWithParams(freqList, hiddenNetworkIdList);
3659ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
3669ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
3673d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    private boolean scanWithParams(String freqList, String hiddenNetworkIdList) {
3683d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        StringBuilder scanCommand = new StringBuilder();
3693d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        scanCommand.append("SCAN TYPE=ONLY");
3703d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqList != null) {
3713d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" freq=" + freqList);
3723d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3733d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIdList != null) {
3743d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" scan_id=" + hiddenNetworkIdList);
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3763d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return doBooleanCommand(scanCommand.toString());
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
393e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
394e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
395e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
396e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
401fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public boolean setNetworkExtra(int netId, String name, Map<String, String> values) {
402fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String encoded;
403fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
404fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            encoded = URLEncoder.encode(new JSONObject(values).toString(), "UTF-8");
405fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (NullPointerException e) {
406fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
407fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
408fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
409fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
410fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
411fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
412fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        return setNetworkVariable(netId, name, "\"" + encoded + "\"");
413fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
414fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
417a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (name.equals(WifiConfiguration.pskVarName)
418a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham                || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
419a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value);
420a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        } else {
421a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
422a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
425fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public Map<String, String> getNetworkExtra(int netId, String name) {
426fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String wrapped = getNetworkVariable(netId, name);
427fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        if (wrapped == null || !wrapped.startsWith("\"") || !wrapped.endsWith("\"")) {
428fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
429fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
430fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
431fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final String encoded = wrapped.substring(1, wrapped.length() - 1);
432fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This method reads a JSON dictionary that was written by setNetworkExtra(). However,
433fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // on devices that upgraded from Marshmallow, it may encounter a legacy value instead -
434fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // an FQDN stored as a plain string. If such a value is encountered, the JSONObject
435fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // constructor will thrown a JSONException and the method will return null.
436fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final JSONObject json = new JSONObject(URLDecoder.decode(encoded, "UTF-8"));
437fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Map<String, String> values = new HashMap<String, String>();
438fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Iterator<?> it = json.keys();
439fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            while (it.hasNext()) {
440fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final String key = (String) it.next();
441fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final Object value = json.get(key);
442fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                if (value instanceof String) {
443fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                    values.put(key, (String) value);
444fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                }
445fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            }
446fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return values;
447fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
44864697f7a5b9c1c39f8c9d9c225b8ca4c798422c2Samuel Tan            Log.e(TAG, "Unable to deserialize networkExtra: " + e.toString());
449fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
450fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (JSONException e) {
451fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This is not necessarily an error. This exception will also occur if we encounter a
452fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // legacy FQDN stored as a plain string. We want to return null in this case as no JSON
453fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // dictionary of extras was found.
454fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
455fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
456fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
457fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
470f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
471f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
472f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
473ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
474ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
475ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
476ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
477ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
478ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
480f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
4819d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius
4829d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4839d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Enables a network in wpa_supplicant.
4849d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be enabled.
4859d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4869d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
4879d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    public boolean enableNetwork(int netId) {
4889d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId));
4899d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        return doBooleanCommand("ENABLE_NETWORK " + netId);
490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4929d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
493163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * Enable a network in wpa_supplicant, do not connect.
494163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * @param netId - Network ID of the network to be enabled.
495163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * @return true if command succeeded, false otherwise.
496163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     */
497163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    public boolean enableNetworkWithoutConnect(int netId) {
498163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt        if (DBG) logDbg("enableNetworkWithoutConnect nid=" + Integer.toString(netId));
499163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt        return doBooleanCommand("ENABLE_NETWORK " + netId + " " + "no-connect");
500163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    }
501163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt
502163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    /**
5039d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Disables a network in wpa_supplicant.
5049d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be disabled.
5059d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
5069d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
508f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5129d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
5139d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Select a network in wpa_supplicant (Disables all others).
5149d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be selected.
5159d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
5169d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
5170047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
5180047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
5190047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
5200047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
5210047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
523f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
528f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
53321bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
53899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
54199d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
54299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
54399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
54499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
54599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
54699d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
54799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
54899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
559a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
560a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
5652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
5672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
5682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
5712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
5722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
5732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
5742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
5752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
5762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
5772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
583eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * MASK=<N> BSS command information mask.
58477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
585eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * The mask used in this method, 0x29d87, gets the following fields:
58677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
587eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_ID         (Bit 0)
588eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_BSSID      (Bit 1)
589eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_FREQ       (Bit 2)
590eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_LEVEL      (Bit 7)
591eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_TSF        (Bit 8)
592eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_IE         (Bit 10)
593eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_FLAGS      (Bit 11)
594eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_SSID       (Bit 12)
595eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_INTERNETW  (Bit 15) (adds ANQP info)
596eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_DELIM      (Bit 17)
597eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *
598eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * See wpa_supplicant/src/common/wpa_ctrl.h for details.
599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
6002afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
6012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
6022afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
6032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
6052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
6062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
6072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
6082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
6132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
6142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
6202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
6212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
6232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
6392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
6405d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
6412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
6422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
6442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
6452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
6472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
6492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
6512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
6522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
6532afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6542afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
6552afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6562afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
6572afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6582afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
6592afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6602afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
6612afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
6622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
6632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
6642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
6652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
6672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
6692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
6712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
6732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
6752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
6762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
6772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
6782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
6792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
6805d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
6812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
6822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
6832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
6842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
6862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
6872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
6882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
6895d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
6905d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
6915d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6925d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
6935d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
6945d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
6955d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6965d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6975d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
6985d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
6995d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
7005d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
7015d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
7025d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
703947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            if (DBG) {
704947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                Log.v(TAG + ":DTIM", "SSID" + networkDetail.getSSID()
705947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + ", DTIM=" + networkDetail.getDtimInterval() + ", "
706947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + " IEstr:" + infoElementsStr);
707947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            }
7082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
7092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
7102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
7112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
7125d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
7132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
7152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
716af2b79e4cfc1da793b3d8fb4a96c144deefc7d58Joe Onorato                                if (DBG) Log.d(TAG, "HSNwk: '" + networkDetail);
7172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7189ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
71963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills                                    level, freq, tsf, infoElements, anqpLines);
7209ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
7212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
7222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
7232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
7242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
7252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
7262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
7272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
7282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
7292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
7302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
7315d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
7322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
7332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
7342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
7352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
7362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
73777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
73877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
740446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
741446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
742446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
743446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
744446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
745446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
746446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
747446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
748446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
749446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
750446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
751446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
752446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
753446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
754446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
75556d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
756446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
757446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
758446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
759446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
760446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
761446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
762446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
763446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
764446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
765446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
766446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
767446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
768446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
769446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
770446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
771446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
84103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
84203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
84303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
84403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
84503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
84603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
84703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
84803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
85003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
85103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
85203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
85303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
85403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
85503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
85603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
85703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
85803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
86118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
86218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
86318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
8647ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
8657ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
8667ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
8677ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
8687ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
8697ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
8707ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
8717ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8920a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
8930a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
8940a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
8950a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
914155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
915155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
9180465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
9190465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
9200465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
9210465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9249153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    /**
9259153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * Start/Stop PNO scan.
9269153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @param enable boolean indicating whether PNO is being enabled or disabled.
9279153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     */
9289153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    public boolean setPnoScan(boolean enable) {
9299153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        String cmd = enable ? "SET pno 1" : "SET pno 0";
9309153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        return doBooleanCommand(cmd);
9319153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    }
932ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius
9339153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    // TODO(rpius): Move PnoMonitor to SupplicantWifiScannerImpl.
934e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    //PNO Monitor
935e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    private class PnoMonitor {
936e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final int MINIMUM_PNO_GAP = 5 * 1000;
937e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final String ACTION_TOGGLE_PNO =
938e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            "com.android.server.Wifi.action.TOGGLE_PNO";
939e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        long mLastPnoChangeTimeStamp = -1L;
940e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mExpectedPnoState = false;
9419bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        List<PnoNetwork> mExpectedPnoNetworkList = null;
942e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mCurrentPnoState = false;;
943e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mWaitForTimer = false;
944d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        private boolean mIsPaused = false;
945e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        final Object mPnoLock = new Object();
946e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final AlarmManager mAlarmManager =
947e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
948e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final PendingIntent mPnoIntent;
949e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
950e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public PnoMonitor() {
951e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            Intent intent = new Intent(ACTION_TOGGLE_PNO, null);
952e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            intent.setPackage("android");
953e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
954e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
955e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mContext.registerReceiver(
956e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new BroadcastReceiver() {
957e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    @Override
958e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    public void onReceive(Context context, Intent intent) {
959e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        synchronized(mPnoLock) {
960d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            if (DBG) {
961d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                                Log.d(mTAG, "PNO timer expire, PNO should change to "
962e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                        + mExpectedPnoState);
963d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            }
964d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            boolean ret = setPno(mExpectedPnoState, mExpectedPnoNetworkList);
965d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                            if (!ret) {
966d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                                Log.e(mTAG, "set PNO failure");
967e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            }
968e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            mWaitForTimer = false;
969e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        }
970e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
971e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                },
972e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new IntentFilter(ACTION_TOGGLE_PNO));
973e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
974e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
9759153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        /**
9769153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius         * Enable/Disable PNO with updated network priorities.
977ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param enable boolean indicating whether PNO is being enabled or disabled.
978ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param pnoNetworkList list of networks with priorities to be set before PNO setting.
979ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         */
9809bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        private boolean setPno(boolean enable, List<PnoNetwork> pnoNetworkList) {
981d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            if (mIsPaused) {
982d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (DBG) Log.d(mTAG, "Do not change PNO state since it is paused");
983d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                return true;
984d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
985d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            if (mCurrentPnoState == enable) {
986d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (DBG) Log.d(mTAG, "Do not change PNO since current is expected");
987d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                return true;
988d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
989d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            if (DBG) {
990d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to " + enable);
991d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
992ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // TODO: Couple of cases yet to be handled:
993ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 1. What if the network priority update fails, should we bail out of PNO setting?
994ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 2. If PNO setting fails below, should we go back and revert this priority change?
995ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            if (pnoNetworkList != null) {
996d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (DBG) Log.i(mTAG, "update priorities for PNO. Enable: " + enable);
9979bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                for (PnoNetwork pnoNetwork : pnoNetworkList) {
998ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    // What if this fails? Should we bail out?
999ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    boolean isSuccess = setNetworkVariable(pnoNetwork.networkId,
1000ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            WifiConfiguration.priorityVarName,
1001ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            Integer.toString(pnoNetwork.priority));
10025c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    if (!isSuccess) {
1003d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                        Log.e(mTAG, "update priority failed for :" + pnoNetwork.networkId);
1004ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    }
1005ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                }
1006ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            }
10079153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius            boolean ret = WifiNative.this.setPnoScan(enable);
1008e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mLastPnoChangeTimeStamp = System.currentTimeMillis();
1009e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            if (ret) {
1010e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                mCurrentPnoState = enable;
1011e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
1012e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return ret;
1013e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
1014e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
1015ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        public boolean enableBackgroundScan(
1016ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                boolean enable,
10179bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                List<PnoNetwork> pnoNetworkList) {
1018e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            synchronized(mPnoLock) {
1019d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                Log.i(mTAG, "set expected PNO to " +  enable);
1020d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                mExpectedPnoState = enable;
1021d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                mExpectedPnoNetworkList = pnoNetworkList;
1022e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                if (mWaitForTimer) {
1023e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    //already has a timer
1024e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (DBG) Log.d(mTAG, "update expected PNO to " +  mExpectedPnoState);
1025e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                } else {
1026e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp;
1027e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (timeDifference >= MINIMUM_PNO_GAP) {
1028ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        return setPno(enable, pnoNetworkList);
1029e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    } else {
1030e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mWaitForTimer = true;
1031e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference);
1032e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
1033e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                System.currentTimeMillis() + timeDifference, mPnoIntent);
1034e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
1035e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                }
1036e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                return true;
1037e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
1038e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
1039d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1040d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        /**
1041d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         * Pause PNO scanning.
1042d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         */
1043d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        public void pauseBackgroundScan() {
1044d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            synchronized (mPnoLock) {
1045d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (!mIsPaused) {
1046d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    Log.i(mTAG, "Pausing Pno scan. Current state: " + mCurrentPnoState);
1047d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    if (mCurrentPnoState) {
1048d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                        setPno(false, null);
1049d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    }
1050d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    mIsPaused = true;
1051d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                }
1052d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
1053d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1054d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1055d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        /**
1056d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         * Resume PNO scanning.
1057d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius         */
1058d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        public void resumeBackgroundScan() {
1059d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            synchronized (mPnoLock) {
1060d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                if (mIsPaused) {
1061d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    Log.i(mTAG, "Resuming Pno scan. Expected state: " + mExpectedPnoState);
1062d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    mIsPaused = false;
1063d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    if (mExpectedPnoState) {
1064d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                        setPno(true, null);
1065d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                    }
1066d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius                }
1067d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            }
1068d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1069e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
1070e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
1071ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius    public boolean enableBackgroundScan(
1072ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            boolean enable,
10739bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            List<PnoNetwork> pnoNetworkList) {
1074e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        if (mPnoMonitor != null) {
1075ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            return mPnoMonitor.enableBackgroundScan(enable, pnoNetworkList);
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1077e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return false;
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1081d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    /**
1082d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     * Pause PNO scanning.
1083d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     * Note: If Pno is currently running, the stop might take a little while to take effect!
1084d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     */
1085d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    public void pauseBackgroundScan() {
1086d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        if (mPnoMonitor != null) {
1087d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            mPnoMonitor.pauseBackgroundScan();
1088d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1089d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    }
1090d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1091d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    /**
1092d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     * Resume PNO scanning.
1093d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius     */
1094d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    public void resumeBackgroundScan() {
1095d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        if (mPnoMonitor != null) {
1096d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius            mPnoMonitor.resumeBackgroundScan();
1097d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius        }
1098d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius    }
1099d0310c7cb097dcb88a042b9308306091b4b8d568Roshan Pius
1100f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
1101f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
1102f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
1103f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
1104f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
1105f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1106f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1107f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
111256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
111356845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
111456845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
111556845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
111656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
111756845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
111856845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
111956845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
1122956f54b391677d78379729dd14518edddf3c7660Etan Cohen            synchronized (sLock) {
112318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
112418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
112518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
1162956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1178956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1193956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
120233b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
120318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
120418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
120518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
120633b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
120733b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1208f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1209f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
121018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
121133b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
121233b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
121326eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean simAuthFailedResponse(int id) {
121426eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type GSM-AUTH
121526eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL");
121626eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
121726eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
121826eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean umtsAuthFailedResponse(int id) {
121926eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type UMTS-AUTH
122026eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL");
122126eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
122226eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
1223ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
122418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1225ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1226ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1275956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1289956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1351956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
135218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
135318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
135418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
135518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
135618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
136018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
136118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
136218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
136318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
136418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
136518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
136618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
136718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
137618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1465956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
149836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
149936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
150027f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
150127f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
150236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
150336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
150436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
1505956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
150627f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
150727f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
150827f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
150927f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
151036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
151136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
151236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
151336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
151436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
151536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
151636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
151736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
151836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
152136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
152236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
152336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1590956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
159118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
159218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
159318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
159418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
159518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
159618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1607956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
160818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
160918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
161118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
161218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
161318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
161418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
161518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
161618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
161718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
161818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
161918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
162018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
162118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
162218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
162318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
162418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
162518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
16563f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1657446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1658446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1659446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1660446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1661f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1662f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1663f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1664f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1665f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1666f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1667f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1668f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1669f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1670f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1671f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1672f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1673f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1674f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1675f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1676f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1677f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1678f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1679f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1680f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1681f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1682f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1683f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
16847e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16857e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    /* kernel logging support */
16867e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    private static native byte[] readKernelLogNative();
16877e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16887e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    synchronized public String readKernelLog() {
16897e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        byte[] bytes = readKernelLogNative();
16907e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        if (bytes != null) {
16917e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
16927e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            try {
16937e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
16947e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return decoded.toString();
16957e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            } catch (CharacterCodingException cce) {
16967e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return new String(bytes, StandardCharsets.ISO_8859_1);
16977e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            }
16987e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        } else {
16997e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            return "*** failed to read kernel log ***";
17007e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        }
17017e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    }
17027e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
17037f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
17047f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
170518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
170618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
170718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
170818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
170918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
171018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1711b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1712f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1713f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1714956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static int sWlan0Index = -1;
1715f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1716f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
17177f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1718b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1719b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1720b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
17217f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1722b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
17237ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1724b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
17257ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
17267ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
17277ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
17287ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
172918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1730d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1731d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1732d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1733d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1734d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1735d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1736956f54b391677d78379729dd14518edddf3c7660Etan Cohen        sLocalLog.log(debugLog);
1737d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1738956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1739918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang            if (startHalNative()) {
1740918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                int wlan0Index = queryInterfaceIndex(mInterfaceName);
1741918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                if (wlan0Index == -1) {
1742918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    if (DBG) sLocalLog.log("Could not find interface with name: " + mInterfaceName);
1743918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    return false;
1744918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                }
1745918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                sWlan0Index = wlan0Index;
1746f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1747f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1748aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1749aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1750956f54b391677d78379729dd14518edddf3c7660Etan Cohen                if (DBG) sLocalLog.log("Could not start hal");
1751f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1752aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1753aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
17547ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
17557ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
17567ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
175718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1758956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
175971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1760f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1761f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1762f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1763f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1764f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1765f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1766f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1767f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1768f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1769f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1770f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1771f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1772f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
17737ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
17747f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
177518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
177671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
177771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1778b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
17797f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1780918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang    public int queryInterfaceIndex(String interfaceName) {
1781956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
178271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1783918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                int num = getInterfacesNative();
1784918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                for (int i = 0; i < num; i++) {
1785918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    String name = getInterfaceNameNative(i);
1786918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    if (name.equals(interfaceName)) {
1787918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                        return i;
178802a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1789aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
1790e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1791e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1792918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang        return -1;
17937f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17947f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1795b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
179618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
1797956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
179818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
179918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
18007f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18017f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1802062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan    // TODO: Change variable names to camel style.
1803e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1804297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1805e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1806e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1807e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1808297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
18097d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1810e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1811d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_bssid_history_entries;
1812d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_epno_networks;
1813d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_epno_networks_by_ssid;
1814d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_of_white_listed_ssid;
1815e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1816e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
181718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1818956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1819af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1820af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1821e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1822e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1823b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1824b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1825e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1826b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1827b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
182883a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1829b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1830d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
18317f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1832e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1833712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int frequency;
1834712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int dwell_time_ms;
1835712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public boolean passive;
18367f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18377f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1838e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1839712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int bucket;
1840712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int band;
1841712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int period_ms;
1842712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_period_ms;
1843712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int step_count;
1844712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_events;
1845712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_channels;
1846712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public ChannelSettings[] channels;
1847e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
18487f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1849e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1850712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int base_period_ms;
1851712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_ap_per_scan;
1852712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_percent;
1853712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_num_scans;
1854712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_buckets;
1855d74ff28cdb01d3f711e0b0f0e99e51fdb221eaf2Roshan Pius        /* Not part of gscan HAL API. Used only for wpa_supplicant scanning */
1856d74ff28cdb01d3f711e0b0f0e99e51fdb221eaf2Roshan Pius        public int[] hiddenNetworkIds;
1857712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public BucketSettings[] buckets;
1858e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
18597f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
186068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
18619bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Network parameters to start PNO scan.
18629bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
18639bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public static class PnoNetwork {
18649bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public String ssid;
18659bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int networkId;
18669bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int priority;
18679bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public byte flags;
1868ef3ea1092bc17673c0a85a845b053151b7c10e07Roshan Pius        public byte auth_bit_field;
18699bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
18709bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
18719bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
18729bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Parameters to start PNO scan. This holds the list of networks which are going to used for
18739bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * PNO scan.
18749bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
18759bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public static class PnoSettings {
18769bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int min5GHzRssi;
18779bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int min24GHzRssi;
18789bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int initialScoreMax;
18799bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int currentConnectionBonus;
18809bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int sameNetworkBonus;
18819bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int secureBonus;
18829bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int band5GHzBonus;
1883dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        public boolean isConnected;
18849bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public PnoNetwork[] networkList;
18859bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
18869bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
18879bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
188868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Wi-Fi channel information.
188968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
189068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public static class WifiChannelInfo {
189168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mPrimaryFrequency;
189268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency0;
189368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency1;
189468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mChannelWidth;
189568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        // TODO: add preamble once available in HAL.
189668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
189768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
1898b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
189963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
190063539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called for each AP as it is found with the entire contents of the beacon/probe response.
190163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Only called when WifiScanner.REPORT_EVENT_FULL_SCAN_RESULT is specified.
190263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1903c9e6069eb941d282af213dc20b171877db6b567bMitchell Wills        void onFullScanResult(ScanResult fullScanResult, int bucketsScanned);
190463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
190563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Callback on an event during a gscan scan.
190663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * See WifiNative.WIFI_SCAN_* for possible values.
190763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
190863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        void onScanStatus(int event);
190963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
191063539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is paused.
191163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
191283a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
191363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
191463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is resumed.
191563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1916b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1917e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1918e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
19199bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
19209bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Handler to notify the occurrence of various events during PNO scan.
19219bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
19229bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public interface PnoEventHandler {
19239bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        /**
19249bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         * Callback to notify when one of the shortlisted networks is found during PNO scan.
19259bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         * @param results List of Scan results received.
19269bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         */
19279bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        void onPnoNetworkFound(ScanResult[] results);
1928063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius
1929063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius        /**
1930063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius         * Callback to notify when the PNO scan schedule fails.
1931063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius         */
1932063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius        void onPnoScanFailed();
19339bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
19349bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
1935b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
193671af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_RESULTS_AVAILABLE = 0;
193771af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1;
193871af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_PERCENT = 2;
193971af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_FAILED = 3;
1940b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
194118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
19422a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onScanStatus(int id, int event) {
194318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
194463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        if (handler != null) {
194563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills            handler.onScanStatus(event);
19462a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        }
1947e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1948e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
194918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
19505cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
19515cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19525cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
19535cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
19545cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19555cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19565cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
19575cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19585cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
19595cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
19605cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19615cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
19625cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
19635cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19645cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
19655cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
19665cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
19675cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
19685cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
19695cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
19705cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19715cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19725cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
19735cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
19745cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19755cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19765cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
19775cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
19785cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
197918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
19805cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
19815cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
19825cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
19835cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
19845cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19855cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
19865cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
19875cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
19885cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
19895cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
19901ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void populateScanResult(ScanResult result, int beaconCap, String dbg) {
1991dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
19925d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
19935d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
19945d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
19955d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
19965d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
19975d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
19985d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
19995d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
20001ab129e587d334a144a0bca5323c27985397a403Randy Pan                InformationElementUtil.parseInformationElements(result.bytes);
20015d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
20025d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
20035d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
20045d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
20055d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
20065d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
20075d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
2008243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
2009243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
2010d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
20115d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
2012d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
2013d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
2014d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
2015d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
2016d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
2017243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
20185d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
20195d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
20205d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
20215d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
2022243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
20235d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
20245d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
2025243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
2026243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
20273571366ac36c70746b9f013ec2b54482861c9292Randy Pan
20283571366ac36c70746b9f013ec2b54482861c9292Randy Pan        // build capabilities string
20293571366ac36c70746b9f013ec2b54482861c9292Randy Pan        BitSet beaconCapBits = new BitSet(16);
20303571366ac36c70746b9f013ec2b54482861c9292Randy Pan        for (int i = 0; i < 16; i++) {
20313571366ac36c70746b9f013ec2b54482861c9292Randy Pan            if ((beaconCap & (1 << i)) != 0) {
20323571366ac36c70746b9f013ec2b54482861c9292Randy Pan                beaconCapBits.set(i);
20333571366ac36c70746b9f013ec2b54482861c9292Randy Pan            }
20343571366ac36c70746b9f013ec2b54482861c9292Randy Pan        }
20353571366ac36c70746b9f013ec2b54482861c9292Randy Pan        result.capabilities = InformationElementUtil.Capabilities.buildCapabilities(elements,
20363571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                               beaconCapBits);
20373571366ac36c70746b9f013ec2b54482861c9292Randy Pan
2038243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
20393571366ac36c70746b9f013ec2b54482861c9292Randy Pan            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth
20403571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " PrimaryFreq: " + result.frequency + " mCenterfreq0: " + result.centerFreq0
20413571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder
20423571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    ? "Support RTT reponder: " : "Do not support RTT responder")
20433571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " Capabilities: " + result.capabilities);
2044f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
2045f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
2046476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
2047dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2048dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
204918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
20501ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void onFullScanResult(int id, ScanResult result,
20513571366ac36c70746b9f013ec2b54482861c9292Randy Pan            int bucketsScanned, int beaconCap) {
20521ab129e587d334a144a0bca5323c27985397a403Randy Pan        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID);
2053dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
205418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
205518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
20561ab129e587d334a144a0bca5323c27985397a403Randy Pan            populateScanResult(result, beaconCap, " onFullScanResult ");
2057c9e6069eb941d282af213dc20b171877db6b567bMitchell Wills            handler.onFullScanResult(result, bucketsScanned);
2058dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
20597f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
20607f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
2061b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
2062b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
2063b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
20647f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
206518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
2066956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
206771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2068f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
2069f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
2070f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
2071b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
2072f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
20737f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
2074f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
2075e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2076f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
2077f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
2078b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
2079f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
2080f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
2081f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
2082f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
2083f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2084f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2085f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2086f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2087f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2088e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2089e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2090e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
20917f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
20927f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
209318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
2094956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
209571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2096ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
2097ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
2098ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2099f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
2100f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
2101f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
2102f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2103b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
2104b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
2105b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
210618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
2107956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
210871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2109f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
2110f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
2111f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
2112f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
2113f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
2114f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
2115f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2116b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
2117b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
2118b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
2119b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
212018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
2121956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
212271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2123f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
21246f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
2125f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
2126f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
2127f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
2128f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
2129f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
213083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
2131f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
2132f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
2133f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
213483a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
2135b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
2136e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2137e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2138e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
213918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
2140956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
21413ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            WifiScanner.ScanData[] sd = null;
214271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
21433ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                sd = getScanResultsNative(sWlan0Index, flush);
21443ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            }
21453ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande
21463ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            if (sd != null) {
21473ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return sd;
2148f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
21493ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return new WifiScanner.ScanData[0];
2150f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2151aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2152e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2153e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2154b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
2155d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
2156d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
2157e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2158e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2159b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
2160b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
2161e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2162b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
2163e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
2164b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
2165e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
216618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
216718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
2168956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
216971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2170f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2171f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2172f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2173f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
2174f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2175f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2176f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
2177f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
2178f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2179f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2180f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2181e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2182f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2183f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2184e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2185e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2186e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2187e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2188e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
218918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
2190956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
219171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2192f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2193f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
2194f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
2195f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2196f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2197e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
21987f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
21997f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
2200e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
220118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
220218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
220318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
220418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
220518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
220618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
220718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
220818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
2209d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
2210d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
2211d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
221218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
221318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
221418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
221518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
221618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
221718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
221818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
221918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
2220aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2221e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2222e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2223b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
2224e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
2225e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2226e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2227b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
2228b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
2229e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2230b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
2231e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
2232b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
2233e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
223418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
2235b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
2236956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
223771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2238f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2239f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2240f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2241f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
2242f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2243f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2244f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
2245851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
2246851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
2247f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2248f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2249f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2250e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2251f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2252f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2253e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2254e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2255e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2256e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2257e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2258e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
225918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
2260956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
226171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2262f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2263f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
2264f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
2265f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2266f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2267e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2268e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2269e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2270e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
227118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
227218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
227318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
227418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
227518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
227618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
2277f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
227818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
2279aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2280e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2281e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
228218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
2283200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
2284200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
2285956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
228671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2287aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
2288f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2289f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2290f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2291aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
22925c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
22935c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
229418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2295d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2296956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
229771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2298d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2299d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2300d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2301d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2302d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2303c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
230418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2305956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
230671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2307f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2308f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2309f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2310f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2311f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2312f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2313a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2314143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2315143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2316143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2317143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2318143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2319143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2320143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2321143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2322143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
232318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
232418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
232518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
232618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
232702a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
232818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2329143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2330143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
233118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
233218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2333143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2334143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2335143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2336143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2337143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2338143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2339143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2340143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
234118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2342143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2343956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
234471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2345f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2346f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2347f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2348f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2349f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2350f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2351f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2352f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2353f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2354143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2355f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2356143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2357143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2358143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2359143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
236018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
2361956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
236271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2363f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2364f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2365f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2366143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2367f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2368f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2369f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2370f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
23715cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2372f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2373f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
23745cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2375f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2376f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2377143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2378143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2379143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2380143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2381143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2382042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
238368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static int sRttResponderCmdId = 0;
238468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
238568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native ResponderConfig enableRttResponderNative(int iface, int commandId,
238668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int timeoutSeconds, WifiChannelInfo channelHint);
238768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
238868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Enable RTT responder role on the device. Returns {@link ResponderConfig} if the responder
238968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * role is successfully enabled, {@code null} otherwise.
239068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
239168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    @Nullable
239268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public ResponderConfig enableRttResponder(int timeoutSeconds) {
239368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
239468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return null;
239568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId != 0) {
239668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                if (DBG) Log.e(mTAG, "responder mode already enabled - this shouldn't happen");
239768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return null;
239868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
239968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int id = getNewCmdIdLocked();
240068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            ResponderConfig config = enableRttResponderNative(
240168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                    sWlan0Index, id, timeoutSeconds, null);
240268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (config != null) sRttResponderCmdId = id;
240368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (DBG) Log.d(TAG, "enabling rtt " + (config != null));
240468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return config;
240568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
240668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
240768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
240868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native boolean disableRttResponderNative(int iface, int commandId);
240968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
241068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Disable RTT responder role. Returns {@code true} if responder role is successfully disabled,
241168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * {@code false} otherwise.
241268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
241368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public boolean disableRttResponder() {
241468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
241568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return false;
241668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId == 0) {
241768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                Log.e(mTAG, "responder role not enabled yet");
241868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return true;
241968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
242068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            sRttResponderCmdId = 0;
242168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return disableRttResponderNative(sWlan0Index, sRttResponderCmdId);
242268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
242368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
242468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
2425042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2426042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
242718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2428956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
242971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2430042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2431042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2432042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2433042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2434042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2435042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2436efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2437efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2438efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2439efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
244018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2441956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2442ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2443ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
244418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2445ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2446ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2447efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2448efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
24490465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2450ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
245118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2452956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2453ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2454ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
245518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2456ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2457ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2458ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2459ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
24600465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
24610465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
246218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
2463956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
246471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
24650465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
24660465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
24670465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
24680465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
24690465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
24700465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2471b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
24723b51fd1bb8356b284822f4f677ad941524e616ebNingyuan Wang    private static native boolean setInterfaceUpNative(boolean up);
247364e816ff5483030636e1815f69a6cbaf8cef289eNingyuan Wang    public boolean setInterfaceUp(boolean up) {
2474956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
247571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
24763b51fd1bb8356b284822f4f677ad941524e616ebNingyuan Wang                return setInterfaceUpNative(up);
2477b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2478b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2479b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2480b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2481b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
248212cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
248312cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
248418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
2485956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
248671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
248712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
248818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
248912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
249012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
249112cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
249212cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2493939177ff615062ec826601d536466875d8457375xinhe
2494e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen    private static native ApfCapabilities getApfCapabilitiesNative(int iface);
2495e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen    public ApfCapabilities getApfCapabilities() {
24966609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
24976609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
2498e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen                return getApfCapabilitiesNative(sWlan0Index);
24996609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
25006609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return null;
25016609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
25026609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
25036609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
25046609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
25056609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    private static native boolean installPacketFilterNative(int iface, byte[] filter);
25066609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    public boolean installPacketFilter(byte[] filter) {
25076609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
25086609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
25096609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return installPacketFilterNative(sWlan0Index, filter);
25106609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
25116609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return false;
25126609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
25136609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
25146609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
25156609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
2516939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
251718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2518956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
251971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2520939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2521939177ff615062ec826601d536466875d8457375xinhe            } else {
2522939177ff615062ec826601d536466875d8457375xinhe                return false;
2523939177ff615062ec826601d536466875d8457375xinhe            }
2524939177ff615062ec826601d536466875d8457375xinhe        }
2525939177ff615062ec826601d536466875d8457375xinhe    }
2526939177ff615062ec826601d536466875d8457375xinhe
2527d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2528d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2529d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2530d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2531d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2532d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2533d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2534d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2535d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
253618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2537956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2538f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2539f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2540d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2541d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2542d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2543d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2544d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2545d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2546d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2547d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2548d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2549d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2550d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2551d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
255218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2553956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
255471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2555d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2556d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2557d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2558d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2559d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2560d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2561d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2562d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2563d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2564d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2565d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2566d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2567d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2568d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2569d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2570d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2571d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2572d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2573d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2574d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
257518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2576956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
257771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2578d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2579d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2580d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2581d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2582d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2583d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2584d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
258518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
258618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
258718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
258818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
258918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
259018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
259118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
259218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2593d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2594d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2595a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2596a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2597a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
25987d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2599a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
26000bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
26010bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2602a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2603a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2604a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2605a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
260618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
26070bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
260818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
260918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
261018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
261103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
261203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
261318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
26140bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
261518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
261618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
261718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
261803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
261903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2620b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2621b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
262218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
2623956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2624b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2625b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2626b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2627b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2628b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2629b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2630b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2631b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2632b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2633b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2634b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2635b797893fc1966803d0c013faac42e6396a37a384xinhe            }
263603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
263703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
263803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
263903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
26400bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
264118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
264203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
2643956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
264471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
264503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
264603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
264703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
264803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
264903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
265003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
265103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
265203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
265303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
265418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
2655956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
265671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
265703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
265803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2659f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
266003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
266103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
266203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
266303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2664b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
266518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2666956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2667b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2668b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2669b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2670b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2671b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2672b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2673b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2674b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2675b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2676b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2677b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2678b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2679b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2680b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2681b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2682b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2683b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2684b797893fc1966803d0c013faac42e6396a37a384xinhe
268503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
268618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
2687956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
268871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
268903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
269003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2691f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
269203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
269303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
269403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
269503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
269603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
269703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
269818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
2699956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
270071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
270103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
270203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2703f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
270403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
270503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
270603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
270703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
27080bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
27090bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
27100bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
27110bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
27120bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
27130bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
27140bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
27150bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
27160bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
27170bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
27180bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
27190bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
27200bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
27210bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
27220bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
27230bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
27240bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
27250bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
27260bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
27270bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
272818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
2729956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
273071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
273103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
273203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
273303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
273403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
273503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
273603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
273703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
273803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
273918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
2740956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
274171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
274203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
274303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
274403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
274503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
274603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
274703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
274803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
274918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
275018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
275103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
275298dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
275398dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
275498dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
275598dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
275698dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
275703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2758127f7244183786e6ccae09e81eeccdac31973e69xinhe
275903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
276018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
2761956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
276271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
27630bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
27640bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
276503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
27660bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
27670bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
27680bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
276903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
277003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2771f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2772a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2773a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2774dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2775dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
277609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /* Packet fate API */
277709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
277809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
277909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    abstract static class FateReport {
278009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte mFate;
278109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final long mDriverTimestampUSec;
278209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte mFrameType;
278309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte[] mFrameBytes;
278409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
278509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        FateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
278609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFate = fate;
278709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mDriverTimestampUSec = driverTimestampUSec;
278809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFrameType = frameType;
278909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFrameBytes = frameBytes;
279009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
279109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
279209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
279309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /**
279409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     * Represents the fate information for one outbound packet.
279509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     */
279609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
279709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    public static final class TxFateReport extends FateReport {
279809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        TxFateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
279909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            super(fate, driverTimestampUSec, frameType, frameBytes);
280009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
280109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
280209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
280309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /**
280409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     * Represents the fate information for one inbound packet.
280509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     */
280609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
280709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    public static final class RxFateReport extends FateReport {
280809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        RxFateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
280909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            super(fate, driverTimestampUSec, frameType, frameBytes);
281009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
281109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
281209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
281309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int startPktFateMonitoringNative(int iface);
281409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int getTxPktFatesNative(int iface, TxFateReport[] reportBufs);
281509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int getRxPktFatesNative(int iface, RxFateReport[] reportBufs);
281609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
281709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    //---------------------------------------------------------------------------------
28185c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /* Configure ePNO/PNO */
28199bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static PnoEventHandler sPnoEventHandler;
28209bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static int sPnoCmdId = 0;
2821dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28229bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static native boolean setPnoListNative(int iface, int id, PnoSettings settings);
282318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
28245c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /**
28259bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Set the PNO settings & the network list in HAL to start PNO.
28269bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param settings PNO settings and network list.
28279bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param eventHandler Handler to receive notifications back during PNO scan.
28289bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
28295c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     */
28309bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean setPnoList(PnoSettings settings, PnoEventHandler eventHandler) {
28319bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2832dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28339bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        synchronized (sLock) {
28349bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            if (isHalStarted()) {
28359bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoCmdId = getNewCmdIdLocked();
28369bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoEventHandler = eventHandler;
28379bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                if (setPnoListNative(sWlan0Index, sPnoCmdId, settings)) {
28389bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                    return true;
28399bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                }
2840dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
28419bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            sPnoEventHandler = null;
28429bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            return false;
2843dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2844dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2845dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28469bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
28479bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Set the PNO network list in HAL to start PNO.
28489bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param list PNO network list.
28499bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param eventHandler Handler to receive notifications back during PNO scan.
28509bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
28519bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
28529bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean setPnoList(PnoNetwork[] list, PnoEventHandler eventHandler) {
28539bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        PnoSettings settings = new PnoSettings();
28549bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        settings.networkList = list;
28559bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        return setPnoList(settings, eventHandler);
2856dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2857dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28589bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static native boolean resetPnoListNative(int iface, int id);
2859dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28609bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
28619bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Reset the PNO settings in HAL to stop PNO.
28629bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
28639bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
28649bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean resetPnoList() {
28659bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        Log.e(TAG, "resetPnoList cmd " + sPnoCmdId);
2866dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2867956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
286871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2869f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
28709bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoEventHandler = null;
28719bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                if (resetPnoListNative(sWlan0Index, sPnoCmdId)) {
2872f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2873f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2874dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2875f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
2876dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2877dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2878dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
287918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
28803571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void onPnoNetworkFound(int id, ScanResult[] results, int[] beaconCaps) {
2881dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2882dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2883dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2884dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2885dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2886dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2887dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28889bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        PnoEventHandler handler = sPnoEventHandler;
288918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
289018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
289118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
289218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
2893dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28941ab129e587d334a144a0bca5323c27985397a403Randy Pan                populateScanResult(results[i], beaconCaps[i], "onPnoNetworkFound ");
289518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2896dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
289718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
289818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
289918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
290018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
290118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
2902dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2903d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2904d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
29059ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
29069ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
29079ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
290818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
29099ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
29109ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
29119ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
29129ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
29139ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
29149ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2915956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
291671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2917f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2918f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
2919f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
29209ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
29219ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
29229ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
29239ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
29249ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2925c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
2926c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
2927c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
292818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2929c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
2930c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
29313bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
29323bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
29333bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
29343bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
29353bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
29363bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
29373bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
2938956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2939c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2940c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
294118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
2942c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2943c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2944c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2945c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2946c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2947c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2948c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
2949c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
295018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2951c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
2952c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
2953956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2954c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2955c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
2956c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2957c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2958c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2959c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2960c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2961aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2962aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
2963aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
2964aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2965aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2966aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
2967aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
296818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
296918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
297018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
297118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
297218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
297318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2974aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2975aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2976aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
2977aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
2978aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2979aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
2980aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
298118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
2982aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
2983aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
2984956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
298518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
2986aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2987ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2988ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
2989ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2990ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
2991aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
2992aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
2993ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
2994ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
2995ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
2996ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
2997ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2998ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2999aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
3000aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
3001aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
3002aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
3003aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
3004aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3005aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
3006aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
300718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
3008aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
3009956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
3010aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
3011ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
3012ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
3013ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
3014ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
3015ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
3016ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
3017aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
3018aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
3019aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
3020aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
3021aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
30225ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
30235ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
30246bf6986d359556010638dfae332b585162f06520Roshan Pius
30256bf6986d359556010638dfae332b585162f06520Roshan Pius    /**
30266bf6986d359556010638dfae332b585162f06520Roshan Pius     * Fetch the host wakeup reasons stats from wlan driver.
30276bf6986d359556010638dfae332b585162f06520Roshan Pius     * @return the |WifiWakeReasonAndCounts| object retrieved from the wlan driver.
30286bf6986d359556010638dfae332b585162f06520Roshan Pius     */
30296bf6986d359556010638dfae332b585162f06520Roshan Pius    public WifiWakeReasonAndCounts getWlanWakeReasonCount() {
30306bf6986d359556010638dfae332b585162f06520Roshan Pius        Log.d(TAG, "getWlanWakeReasonCount " + sWlan0Index);
30316bf6986d359556010638dfae332b585162f06520Roshan Pius        synchronized (sLock) {
30326bf6986d359556010638dfae332b585162f06520Roshan Pius            if (isHalStarted()) {
30336bf6986d359556010638dfae332b585162f06520Roshan Pius                return getWlanWakeReasonCountNative(sWlan0Index);
30346bf6986d359556010638dfae332b585162f06520Roshan Pius            } else {
30356bf6986d359556010638dfae332b585162f06520Roshan Pius                return null;
30366bf6986d359556010638dfae332b585162f06520Roshan Pius            }
30376bf6986d359556010638dfae332b585162f06520Roshan Pius        }
30386bf6986d359556010638dfae332b585162f06520Roshan Pius    }
30393dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
30403dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    private static native int configureNeighborDiscoveryOffload(int iface, boolean enabled);
30413dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
30423dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    public boolean configureNeighborDiscoveryOffload(boolean enabled) {
30433dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        final String logMsg =  "configureNeighborDiscoveryOffload(" + enabled + ")";
30443dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        Log.d(mTAG, logMsg);
30453dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        synchronized (sLock) {
30463dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            if (isHalStarted()) {
30473dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                final int ret = configureNeighborDiscoveryOffload(sWlan0Index, enabled);
30483dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                if (ret != 0) {
30493dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                    Log.d(mTAG, logMsg + " returned: " + ret);
30503dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                }
30513dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                return (ret == 0);
30523dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            }
30533dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        }
30543dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        return false;
30553dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    }
3056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
3057