WifiNative.java revision 3d995b8a3a508e957e3dac2e508956020cd9abfe
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;
26143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpandeimport android.net.wifi.RttManager;
2768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.net.wifi.RttManager.ResponderConfig;
28e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
29dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiConfiguration;
3018786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.net.wifi.WifiEnterpriseConfig;
31aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
3203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.WifiManager;
33e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
34dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiSsid;
352a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.net.wifi.WifiWakeReasonAndCounts;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
3903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
40f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
4118786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.os.SystemProperties;
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
45fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
46c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadhamimport com.android.server.connectivity.KeepalivePacketData;
472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.NetworkDetail;
482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.SupplicantBridge;
492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.Utils;
505d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Willsimport com.android.server.wifi.util.InformationElementUtil;
51c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
5218786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport libcore.util.HexEncoding;
5318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
54fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONException;
55fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONObject;
56fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
57fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.io.UnsupportedEncodingException;
58fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLDecoder;
59fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLEncoder;
605cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.ByteBuffer;
615cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.CharBuffer;
625cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharacterCodingException;
635cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharsetDecoder;
645cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.StandardCharsets;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
663571366ac36c70746b9f013ec2b54482861c9292Randy Panimport java.util.BitSet;
67fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.HashMap;
68fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Iterator;
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
71fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Map;
729ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport java.util.Set;
7318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
74fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
76155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
79155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
85b66b29a00da970ee75052e24f592c8d6c16bd0edxinhe    private static boolean DBG = false;
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
8818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Hold this lock before calling supplicant or HAL methods
8918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * it is required to mutually exclude access to the driver
9018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
91956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static final Object sLock = new Object();
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
93956f54b391677d78379729dd14518edddf3c7660Etan Cohen    private static final LocalLog sLocalLog = new LocalLog(16384);
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static LocalLog getLocalLog() {
96956f54b391677d78379729dd14518edddf3c7660Etan Cohen        return sLocalLog;
9718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
10918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Singleton WifiNative instances
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
11118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative wlanNativeInterface =
11218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            new WifiNative(SystemProperties.get("wifi.interface", "wlan0"));
11318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getWlanNativeInterface() {
11418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return wlanNativeInterface;
11518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
11718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    //STOPSHIP: get interface name from native side
11818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative p2pNativeInterface = new WifiNative("p2p0");
11918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getP2pNativeInterface() {
12018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return p2pNativeInterface;
12118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mTAG;
12518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfaceName;
12618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfacePrefix;
12718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
12818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private Context mContext = null;
12918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private PnoMonitor mPnoMonitor = null;
13018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void initContext(Context context) {
13118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (mContext == null && context != null) {
13218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mContext = context;
13318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mPnoMonitor = new PnoMonitor();
13418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
13518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private WifiNative(String interfaceName) {
138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
140e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!interfaceName.equals("p2p0")) {
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // commands for p2p0 interface don't need prefix
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
14918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName() {
15018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return mInterfaceName;
151e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
152e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
15318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Note this affects logging on for all interfaces
154ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
155ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
156ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
157ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
158ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
159ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
160ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
161ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
16218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private void localLog(String s) {
163956f54b391677d78379729dd14518edddf3c7660Etan Cohen        if (sLocalLog != null) sLocalLog.log(mInterfaceName + ": " + s);
16418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
16718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
16818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
16918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Driver and Supplicant management
17018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
17118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean loadDriverNative();
17218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean loadDriver() {
173956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
17418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return loadDriverNative();
17518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean isDriverLoadedNative();
17918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isDriverLoaded() {
180956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return isDriverLoadedNative();
18218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
18518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean unloadDriverNative();
18618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean unloadDriver() {
187956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return unloadDriverNative();
18918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
19018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
19118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
19218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean startSupplicantNative(boolean p2pSupported);
19318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startSupplicant(boolean p2pSupported) {
194956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return startSupplicantNative(p2pSupported);
19618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
19718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
19818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
19918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* Sends a kill signal to supplicant. To be used when we have lost connection
20018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills       or when the supplicant is hung */
20118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean killSupplicantNative(boolean p2pSupported);
20218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean killSupplicant(boolean p2pSupported) {
203956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
20418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return killSupplicantNative(p2pSupported);
20518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
20818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean connectToSupplicantNative();
209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
210956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
211b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "connectToSupplicant");
212b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            return connectToSupplicantNative();
213b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
21618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static void closeSupplicantConnectionNative();
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
218956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
219b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "closeSupplicantConnection");
220b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            closeSupplicantConnectionNative();
221b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
22418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
22518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wait for the supplicant to send an event, returning the event string.
22618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * @return the event string sent by the supplicant.
22718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
22818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static String waitForEventNative();
229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
23418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
23518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
23618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Supplicant Command Primitives
23718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
23818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native boolean doBooleanCommandNative(String command);
23918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native int doIntCommandNative(String command);
24118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native String doStringCommandNative(String command);
24318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
246956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
24718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
2497b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
2500888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
255a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    private boolean doBooleanCommandWithoutLogging(String command) {
256a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (DBG) Log.d(mTAG, "doBooleanCommandWithoutLogging: " + command);
257956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
258a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
259a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            if (DBG) Log.d(mTAG, command + ": returned " + result);
260a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return result;
261a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
262a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    }
263a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
266956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
26718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
2697b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
2760888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2770888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2780888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2790888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2800888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
2810888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
282956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
28318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
28540ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
28640ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
28740ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
2887b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (!command.startsWith("STATUS-")) {
2897b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog(toLog + " -> " + result);
2907b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
29140ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
29240ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
2980888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2990888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
3000888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
3010888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
3020888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
30327355a942653264388e909a4276196ee63e57811vandwalle        }
304956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
30918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String doCustomSupplicantCommand(String command) {
31018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doStringCommand(command);
31118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
31218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
31318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
31418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wrappers for supplicant commands
31518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
321ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
322ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
323ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
324ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
325a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
326a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
327a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
328a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
3293d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    /**
3303d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * Create a comma separate string from integer set.
3313d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param values List of integers.
3323d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @return comma separated string.
3333d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     */
3343d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    private static String createCSVStringFromIntegerSet(Set<Integer> values) {
3353d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        StringBuilder list = new StringBuilder();
3363d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        boolean first = true;
3373d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        for (Integer value : values) {
3383d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            if (!first) {
3393d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius                list.append(",");
3403d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            }
3413d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            list.append(value);
3423d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            first = false;
3433d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3443d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return list.toString();
3453d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    }
34618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
347ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    /**
348ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * Start a scan using wpa_supplicant for the given frequencies.
3493d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param freqs list of frequencies to scan for, if null scan all supported channels.
3503d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param hiddenNetworkIds List of hidden networks to be scanned for.
351ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     */
3523d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    public boolean scan(Set<Integer> freqs, Set<Integer> hiddenNetworkIds) {
3533d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String freqList = null;
3543d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String hiddenNetworkIdList = null;
3553d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqs != null && freqs.size() != 0) {
3563d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            freqList = createCSVStringFromIntegerSet(freqs);
3573d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3583d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIds != null && hiddenNetworkIds.size() != 0) {
3593d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            hiddenNetworkIdList = createCSVStringFromIntegerSet(hiddenNetworkIds);
3609ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3613d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return scanWithParams(freqList, hiddenNetworkIdList);
3629ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
3639ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
3643d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    private boolean scanWithParams(String freqList, String hiddenNetworkIdList) {
3653d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        StringBuilder scanCommand = new StringBuilder();
3663d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        scanCommand.append("SCAN TYPE=ONLY");
3673d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqList != null) {
3683d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" freq=" + freqList);
3693d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3703d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIdList != null) {
3713d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" scan_id=" + hiddenNetworkIdList);
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3733d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return doBooleanCommand(scanCommand.toString());
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
390e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
391e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
392e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
393e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
398fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public boolean setNetworkExtra(int netId, String name, Map<String, String> values) {
399fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String encoded;
400fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
401fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            encoded = URLEncoder.encode(new JSONObject(values).toString(), "UTF-8");
402fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (NullPointerException e) {
403fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
404fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
405fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
406fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
407fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
408fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
409fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        return setNetworkVariable(netId, name, "\"" + encoded + "\"");
410fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
411fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
414a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (name.equals(WifiConfiguration.pskVarName)
415a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham                || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
416a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value);
417a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        } else {
418a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
419a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
422fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public Map<String, String> getNetworkExtra(int netId, String name) {
423fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String wrapped = getNetworkVariable(netId, name);
424fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        if (wrapped == null || !wrapped.startsWith("\"") || !wrapped.endsWith("\"")) {
425fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
426fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
427fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
428fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final String encoded = wrapped.substring(1, wrapped.length() - 1);
429fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This method reads a JSON dictionary that was written by setNetworkExtra(). However,
430fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // on devices that upgraded from Marshmallow, it may encounter a legacy value instead -
431fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // an FQDN stored as a plain string. If such a value is encountered, the JSONObject
432fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // constructor will thrown a JSONException and the method will return null.
433fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final JSONObject json = new JSONObject(URLDecoder.decode(encoded, "UTF-8"));
434fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Map<String, String> values = new HashMap<String, String>();
435fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Iterator<?> it = json.keys();
436fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            while (it.hasNext()) {
437fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final String key = (String) it.next();
438fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final Object value = json.get(key);
439fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                if (value instanceof String) {
440fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                    values.put(key, (String) value);
441fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                }
442fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            }
443fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return values;
444fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
445fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
446fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
447fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (JSONException e) {
448fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This is not necessarily an error. This exception will also occur if we encounter a
449fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // legacy FQDN stored as a plain string. We want to return null in this case as no JSON
450fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // dictionary of extras was found.
451fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
452fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
453fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
454fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
466f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
467f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
468f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
470ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
471ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
472ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
473ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
474ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
475ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
476f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
4789d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius
4799d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4809d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Enables a network in wpa_supplicant.
4819d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be enabled.
4829d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4839d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
4849d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    public boolean enableNetwork(int netId) {
4859d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId));
4869d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        return doBooleanCommand("ENABLE_NETWORK " + netId);
487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4899d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4909d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Disables a network in wpa_supplicant.
4919d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be disabled.
4929d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4939d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4999d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
5009d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Select a network in wpa_supplicant (Disables all others).
5019d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be selected.
5029d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
5039d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
5040047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
5050047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
5060047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
5070047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
5080047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
510f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
515f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
52021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
52599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
52899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
52999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
53099d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
53199d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
53299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
53399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
53499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
53599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
546a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
547a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
5522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
5542afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
5552afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5572afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
5582afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
5592afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
5602afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
5612afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
5622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
5632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
5642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
57177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        1                       0     2
57277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
57377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        0                       1     1   -> 9
57477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
57577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       1     9   -> d
57677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
57777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       0     8
57877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
57977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         1                        1                       1     7
58077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
58177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
58277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
58377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
58477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
58577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  wpa_supplicant_ctrl_iface_bss:4315
58677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  print_bss_info
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
5882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
5892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
5902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
5912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
5932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
5942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
5952afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
5962afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
5972afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
5982afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
5992afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
6002afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
6012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
6022afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
6042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
6052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
6062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
6072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
6082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
6142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
6202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
6212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
6232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
6285d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
6392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
6402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
6412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
6432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
6452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
6472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
6492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
6502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
6512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
6522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
6532afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6542afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
6552afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6562afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
6572afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6582afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
6592afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6602afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
6612afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
6632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
6642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
6652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
6662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
6672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
6685d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
6692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
6702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
6712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
6722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
6742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
6752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
6762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
6775d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
6785d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
6795d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6805d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
6815d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
6825d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
6835d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6845d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6855d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
6865d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
6875d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
6885d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6895d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
6905d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
691947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            if (DBG) {
692947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                Log.v(TAG + ":DTIM", "SSID" + networkDetail.getSSID()
693947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + ", DTIM=" + networkDetail.getDtimInterval() + ", "
694947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + " IEstr:" + infoElementsStr);
695947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            }
6962afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
6972afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
6982afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
6992afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
7005d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
7012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7022afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
7032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
7042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, "HSNwk: '" + networkDetail);
7052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7069ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
70763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills                                    level, freq, tsf, infoElements, anqpLines);
7089ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
7092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
7102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
7112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
7122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
7132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
7142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
7152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
7162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
7172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
7182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
7195d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
7202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
7212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
7222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
7232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
7242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
72577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
72677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
728446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
729446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
730446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
731446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
732446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
733446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
734446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
735446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
736446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
737446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
738446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
739446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
740446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
741446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
742446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
74356d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
744446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
745446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
746446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
747446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
748446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
749446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
750446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
751446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
752446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
753446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
754446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
755446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
756446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
757446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
758446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
759446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
82903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
83003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
83103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
83203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
83303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
83403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
83503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
83603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
83803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
83903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
84003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
84103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
84203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
84303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
84403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
84503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
84603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
84918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
85018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
85118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
8527ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
8537ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
8547ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
8557ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
8567ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
8577ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
8587ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
8597ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8800a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
8810a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
8820a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
8830a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
9060465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
9070465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
9080465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
9090465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
912ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius
913e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    //PNO Monitor
914e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    private class PnoMonitor {
915e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final int MINIMUM_PNO_GAP = 5 * 1000;
916e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final String ACTION_TOGGLE_PNO =
917e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            "com.android.server.Wifi.action.TOGGLE_PNO";
918e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        long mLastPnoChangeTimeStamp = -1L;
919e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mExpectedPnoState = false;
9205c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        List<WifiPnoNetwork> mExpectedWifiPnoNetworkList = null;
921e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mCurrentPnoState = false;;
922e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mWaitForTimer = false;
923e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        final Object mPnoLock = new Object();
924e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final AlarmManager mAlarmManager =
925e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
926e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final PendingIntent mPnoIntent;
927e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
928e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public PnoMonitor() {
929e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            Intent intent = new Intent(ACTION_TOGGLE_PNO, null);
930e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            intent.setPackage("android");
931e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
932e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
933e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mContext.registerReceiver(
934e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new BroadcastReceiver() {
935e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    @Override
936e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    public void onReceive(Context context, Intent intent) {
937e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        synchronized(mPnoLock) {
938e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (DBG) Log.d(mTAG, "PNO timer expire, PNO should change to " +
939e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    mExpectedPnoState);
940e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (mCurrentPnoState != mExpectedPnoState) {
941e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to "
942e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                        + mExpectedPnoState);
943ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                                boolean ret = setPno(
9445c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                                        mExpectedPnoState, mExpectedWifiPnoNetworkList);
945e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (!ret) {
946e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    Log.e(mTAG, "set PNO failure");
947e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                }
948e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            } else {
949e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "Do not change PNO since current is expected");
950e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            }
951e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            mWaitForTimer = false;
952e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        }
953e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
954e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                },
955e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new IntentFilter(ACTION_TOGGLE_PNO));
956e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
957e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
958ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        /*  Enable/Disable PNO with updated network priorities.
959ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         *
960ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param enable boolean indicating whether PNO is being enabled or disabled.
961ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param pnoNetworkList list of networks with priorities to be set before PNO setting.
962ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         */
9635c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        private boolean setPno(boolean enable, List<WifiPnoNetwork> pnoNetworkList) {
964ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // TODO: Couple of cases yet to be handled:
965ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 1. What if the network priority update fails, should we bail out of PNO setting?
966ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 2. If PNO setting fails below, should we go back and revert this priority change?
967ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            if (pnoNetworkList != null) {
968ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                if (DBG) Log.i(mTAG, "Update priorities for PNO. Enable: " + enable);
9695c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                for (WifiPnoNetwork pnoNetwork : pnoNetworkList) {
970ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    // What if this fails? Should we bail out?
971ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    boolean isSuccess = setNetworkVariable(pnoNetwork.networkId,
972ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            WifiConfiguration.priorityVarName,
973ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            Integer.toString(pnoNetwork.priority));
9745c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    if (!isSuccess) {
975ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        Log.e(mTAG, "Update priority failed for :" + pnoNetwork.networkId);
976ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    }
977ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                }
978ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            }
979e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            String cmd = enable ? "SET pno 1" : "SET pno 0";
980e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            boolean ret = doBooleanCommand(cmd);
981e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mLastPnoChangeTimeStamp = System.currentTimeMillis();
982e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            if (ret) {
983e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                mCurrentPnoState = enable;
984e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
985e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return ret;
986e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
987e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
988ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        public boolean enableBackgroundScan(
989ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                boolean enable,
9905c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                List<WifiPnoNetwork> pnoNetworkList) {
991e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            synchronized(mPnoLock) {
992e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                if (mWaitForTimer) {
993e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    //already has a timer
994e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    mExpectedPnoState = enable;
9955c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    mExpectedWifiPnoNetworkList = pnoNetworkList;
996e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (DBG) Log.d(mTAG, "update expected PNO to " +  mExpectedPnoState);
997e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                } else {
998e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (mCurrentPnoState == enable) {
999e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        return true;
1000e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
1001e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp;
1002e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (timeDifference >= MINIMUM_PNO_GAP) {
1003ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        return setPno(enable, pnoNetworkList);
1004e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    } else {
1005e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mExpectedPnoState = enable;
10065c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                        mExpectedWifiPnoNetworkList = pnoNetworkList;
1007e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mWaitForTimer = true;
1008e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference);
1009e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
1010e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                System.currentTimeMillis() + timeDifference, mPnoIntent);
1011e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
1012e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                }
1013e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                return true;
1014e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
1015e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
1016e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
1017e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
1018ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius    public boolean enableBackgroundScan(
1019ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            boolean enable,
10205c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            List<WifiPnoNetwork> pnoNetworkList) {
1021e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        if (mPnoMonitor != null) {
1022ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            return mPnoMonitor.enableBackgroundScan(enable, pnoNetworkList);
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1024e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return false;
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1028f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
1029f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
1030f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
1031f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
1032f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
1033f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1034f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1035f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
104056845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
104156845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
104256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
104356845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
104456845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
104556845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
104656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
104756845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
1050956f54b391677d78379729dd14518edddf3c7660Etan Cohen            synchronized (sLock) {
105118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
105218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
105318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
1090956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1106956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1121956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
113033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
113118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
113218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
113318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
113433b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
113533b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1136f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1137f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
113818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
113933b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
114033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
114126eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean simAuthFailedResponse(int id) {
114226eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type GSM-AUTH
114326eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL");
114426eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
114526eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
114626eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean umtsAuthFailedResponse(int id) {
114726eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type UMTS-AUTH
114826eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL");
114926eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
115026eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
1151ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
115218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1153ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1154ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1203956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1217956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1279956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
128018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
128118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
128218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
128318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
128418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
128818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
128918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
129018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
129118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
129218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
129318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
129418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
129518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
130418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1393956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
142636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
142736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
142827f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
142927f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
143036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
143136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
143236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
1433956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
143427f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
143527f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
143627f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
143727f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
143836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
143936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
144036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
144136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
144236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
144336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
144436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
144536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
144636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
144936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
145036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
145136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1518956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
151918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
152018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
152118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
152218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
152318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
152418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1535956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
153618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
153718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
153918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
154018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
154118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
154218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
154318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
154418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
154518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
154618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
154718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
154818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
154918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
155018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
155118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
155218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
155318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
15843f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1585446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1586446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1587446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1588446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1589f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1590f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1591f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1592f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1593f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1594f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1595f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1596f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1597f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1598f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1599f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1600f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1601f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1602f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1603f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1604f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1605f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1606f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1607f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1608f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1609f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1610f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1611f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
16127e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16137e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    /* kernel logging support */
16147e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    private static native byte[] readKernelLogNative();
16157e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16167e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    synchronized public String readKernelLog() {
16177e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        byte[] bytes = readKernelLogNative();
16187e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        if (bytes != null) {
16197e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
16207e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            try {
16217e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
16227e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return decoded.toString();
16237e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            } catch (CharacterCodingException cce) {
16247e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return new String(bytes, StandardCharsets.ISO_8859_1);
16257e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            }
16267e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        } else {
16277e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            return "*** failed to read kernel log ***";
16287e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        }
16297e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    }
16307e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16317f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
16327f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
163318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
163418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
163518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
163618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
163718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
163818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1639b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1640f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1641f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1642956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static int sWlan0Index = -1;
1643aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sP2p0Index = -1;
1644f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1645f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
16467f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1647b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1648b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1649b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
16507f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1651b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
16527ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1653b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
16547ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
16557ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
16567ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16577ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
165818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1659d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1660d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1661d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1662d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1663d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1664d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1665956f54b391677d78379729dd14518edddf3c7660Etan Cohen        sLocalLog.log(debugLog);
1666d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1667956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1668cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1669f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1670f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1671aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1672aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1673956f54b391677d78379729dd14518edddf3c7660Etan Cohen                if (DBG) sLocalLog.log("Could not start hal");
1674f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1675aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1676aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
16777ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
16787ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16797ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
168018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1681956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
168271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1683f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1684f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1685f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1686f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1687f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1688f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1689f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1690f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1691f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1692f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1693f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1694f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sP2p0Index = -1;
1695f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1696f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
16977ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16987f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
169918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
170071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
170171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1702b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
17037f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
170418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getInterfaces() {
1705956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
170671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1707f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sWifiIfaceHandles == null) {
1708f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int num = getInterfacesNative();
1709f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int wifi_num = 0;
1710f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    for (int i = 0; i < num; i++) {
1711f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        String name = getInterfaceNameNative(i);
1712f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        Log.i(TAG, "interface[" + i + "] = " + name);
1713f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        if (name.equals("wlan0")) {
1714f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sWlan0Index = i;
1715f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1716f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        } else if (name.equals("p2p0")) {
1717f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sP2p0Index = i;
1718f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1719f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        }
172002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1721f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return wifi_num;
1722f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1723f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return sWifiIfaceHandles.length;
1724aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
172502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            } else {
1726f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
1727e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1728e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
17297f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17307f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1731b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
173218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
1733956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
173418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
173518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
17367f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17377f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1738062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan    // TODO: Change variable names to camel style.
1739e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1740297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1741e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1742e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1743e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1744297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
17457d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1746e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1747e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1748e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
174918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1750956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1751af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1752af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1753e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1754e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1755b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1756b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1757e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1758b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1759b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
176083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1761b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1762d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
17637f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1764e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1765712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int frequency;
1766712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int dwell_time_ms;
1767712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public boolean passive;
17687f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17697f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1770e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1771712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int bucket;
1772712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int band;
1773712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int period_ms;
1774712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_period_ms;
1775712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int step_count;
1776712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_events;
1777712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_channels;
1778712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public ChannelSettings[] channels;
1779e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17807f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1781e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1782712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int base_period_ms;
1783712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_ap_per_scan;
1784712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_percent;
1785712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_num_scans;
1786712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_buckets;
1787712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public BucketSettings[] buckets;
1788e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17897f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
179068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
179168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Wi-Fi channel information.
179268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
179368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public static class WifiChannelInfo {
179468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mPrimaryFrequency;
179568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency0;
179668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency1;
179768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mChannelWidth;
179868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        // TODO: add preamble once available in HAL.
179968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
180068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
1801b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
180263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
180363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called for each AP as it is found with the entire contents of the beacon/probe response.
180463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Only called when WifiScanner.REPORT_EVENT_FULL_SCAN_RESULT is specified.
180563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1806476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        void onFullScanResult(ScanResult fullScanResult);
180763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
180863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Callback on an event during a gscan scan.
180963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * See WifiNative.WIFI_SCAN_* for possible values.
181063539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
181163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        void onScanStatus(int event);
181263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
181363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is paused.
181463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
181583a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
181663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
181763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is resumed.
181863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1819b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1820e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1821e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1822b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
182371af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_RESULTS_AVAILABLE = 0;
182471af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1;
182571af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_PERCENT = 2;
182671af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_FAILED = 3;
1827b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
182818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
18292a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onScanStatus(int id, int event) {
183018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
183163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        if (handler != null) {
183263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills            handler.onScanStatus(event);
18332a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        }
1834e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1835e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
183618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
18375cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
18385cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18395cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
18405cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
18415cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18425cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18435cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
18445cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18455cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
18465cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18475cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18485cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
18495cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
18505cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18515cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
18525cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
18535cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
18545cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
18555cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
18565cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
18575cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18585cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18595cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
18605cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
18615cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18625cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18635cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
18645cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18655cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
186618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
18675cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
18685cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
18695cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
18705cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18715cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18725cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
18735cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
18745cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
18755cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18765cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18771ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void populateScanResult(ScanResult result, int beaconCap, String dbg) {
1878dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
18795d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18805d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
18815d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
18825d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
18835d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
18845d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
18855d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18865d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
18871ab129e587d334a144a0bca5323c27985397a403Randy Pan                InformationElementUtil.parseInformationElements(result.bytes);
18885d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
18895d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
18905d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
18915d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
18925d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
18935d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
18945d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
1895243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1896243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1897d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
18985d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
1899d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
1900d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
1901d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
1902d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
1903d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
1904243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
19055d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
19065d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
19075d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
19085d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
1909243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
19105d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
19115d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
1912243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
1913243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
19143571366ac36c70746b9f013ec2b54482861c9292Randy Pan
19153571366ac36c70746b9f013ec2b54482861c9292Randy Pan        // build capabilities string
19163571366ac36c70746b9f013ec2b54482861c9292Randy Pan        BitSet beaconCapBits = new BitSet(16);
19173571366ac36c70746b9f013ec2b54482861c9292Randy Pan        for (int i = 0; i < 16; i++) {
19183571366ac36c70746b9f013ec2b54482861c9292Randy Pan            if ((beaconCap & (1 << i)) != 0) {
19193571366ac36c70746b9f013ec2b54482861c9292Randy Pan                beaconCapBits.set(i);
19203571366ac36c70746b9f013ec2b54482861c9292Randy Pan            }
19213571366ac36c70746b9f013ec2b54482861c9292Randy Pan        }
19223571366ac36c70746b9f013ec2b54482861c9292Randy Pan        result.capabilities = InformationElementUtil.Capabilities.buildCapabilities(elements,
19233571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                               beaconCapBits);
19243571366ac36c70746b9f013ec2b54482861c9292Randy Pan
1925243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
19263571366ac36c70746b9f013ec2b54482861c9292Randy Pan            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth
19273571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " PrimaryFreq: " + result.frequency + " mCenterfreq0: " + result.centerFreq0
19283571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder
19293571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    ? "Support RTT reponder: " : "Do not support RTT responder")
19303571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " Capabilities: " + result.capabilities);
1931f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1932f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1933476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1934dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1935dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
193618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
19371ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void onFullScanResult(int id, ScanResult result,
19383571366ac36c70746b9f013ec2b54482861c9292Randy Pan            int bucketsScanned, int beaconCap) {
19391ab129e587d334a144a0bca5323c27985397a403Randy Pan        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID);
1940dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
194118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
194218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
19431ab129e587d334a144a0bca5323c27985397a403Randy Pan            populateScanResult(result, beaconCap, " onFullScanResult ");
194418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onFullScanResult(result);
1945dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
19467f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19477f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1948b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1949b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1950b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
19517f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
195218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
1953956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
195471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1955f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
1956f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
1957f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
1958b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1959f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
19607f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1961f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
1962e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1963f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
1964f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
1965b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1966f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1967f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
1968f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
1969f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1970f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1971f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1972f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1973f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1974f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1975e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1976e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1977e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
19787f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19797f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
198018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
1981956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
198271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1983ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
1984ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
1985ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
1986f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
1987f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
1988f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
1989f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1990b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1991b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1992b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
199318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
1994956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
199571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1996f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1997f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
1998f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1999f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
2000f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
2001f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
2002f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2003b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
2004b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
2005b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
2006b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
200718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
2008956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
200971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2010f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
20116f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
2012f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
2013f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
2014f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
2015f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
2016f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
201783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
2018f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
2019f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
2020f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
202183a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
2022b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
2023e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2024e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2025e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
202618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
2027956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
20283ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            WifiScanner.ScanData[] sd = null;
202971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
20303ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                sd = getScanResultsNative(sWlan0Index, flush);
20313ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            }
20323ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande
20333ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            if (sd != null) {
20343ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return sd;
2035f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
20363ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return new WifiScanner.ScanData[0];
2037f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2038aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2039e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2040e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2041b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
2042d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
2043d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
2044e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2045e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2046b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
2047b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
2048e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2049b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
2050e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
2051b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
2052e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
205318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
205418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
2055956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
205671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2057f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2058f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2059f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2060f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
2061f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2062f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2063f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
2064f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
2065f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2066f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2067f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2068e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2069f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2070f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2071e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2072e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2073e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2074e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2075e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
207618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
2077956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
207871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2079f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2080f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
2081f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
2082f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2083f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2084e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
20857f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
20867f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
2087e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
208818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
208918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
209018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
209118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
209218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
209318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
209418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
209518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
2096d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
2097d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
2098d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
209918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
210018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
210118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
210218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
210318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
210418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
210518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
210618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
2107aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2108e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2109e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2110b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
2111e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
2112e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2113e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2114b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
2115b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
2116e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2117b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
2118e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
2119b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
2120e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
212118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
2122b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
2123956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
212471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2125f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2126f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2127f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2128f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
2129f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2130f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2131f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
2132851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
2133851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
2134f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2135f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2136f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2137e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2138f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2139f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2140e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2141e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2142e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2143e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2144e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2145e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
214618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
2147956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
214871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2149f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2150f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
2151f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
2152f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2153f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2154e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2155e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2156e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2157e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
215818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
215918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
216018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
216118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
216218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
216318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
2164f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
216518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
2166aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2167e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2168e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
216918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
2170200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
2171200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
2172956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
217371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2174aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
2175f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2176f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2177f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2178aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
21795c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
21805c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
218118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2182d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2183956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
218471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2185d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2186d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2187d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2188d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2189d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2190c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
219118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2192956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
219371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2194f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2195f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2196f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2197f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2198f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2199f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2200a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2201143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2202143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2203143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2204143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2205143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2206143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2207143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2208143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2209143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
221018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
221118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
221218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
221318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
221402a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
221518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2216143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2217143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
221818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
221918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2220143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2221143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2222143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2223143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2224143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2225143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2226143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2227143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
222818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2229143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2230956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
223171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2232f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2233f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2234f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2235f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2236f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2237f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2238f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2239f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2240f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2241143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2242f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2243143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2244143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2245143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2246143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
224718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
2248956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
224971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2250f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2251f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2252f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2253143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2254f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2255f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2256f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2257f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
22585cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2259f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2260f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
22615cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2262f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2263f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2264143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2265143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2266143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2267143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2268143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2269042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
227068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static int sRttResponderCmdId = 0;
227168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
227268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native ResponderConfig enableRttResponderNative(int iface, int commandId,
227368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int timeoutSeconds, WifiChannelInfo channelHint);
227468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
227568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Enable RTT responder role on the device. Returns {@link ResponderConfig} if the responder
227668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * role is successfully enabled, {@code null} otherwise.
227768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
227868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    @Nullable
227968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public ResponderConfig enableRttResponder(int timeoutSeconds) {
228068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
228168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return null;
228268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId != 0) {
228368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                if (DBG) Log.e(mTAG, "responder mode already enabled - this shouldn't happen");
228468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return null;
228568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
228668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int id = getNewCmdIdLocked();
228768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            ResponderConfig config = enableRttResponderNative(
228868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                    sWlan0Index, id, timeoutSeconds, null);
228968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (config != null) sRttResponderCmdId = id;
229068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (DBG) Log.d(TAG, "enabling rtt " + (config != null));
229168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return config;
229268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
229368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
229468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
229568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native boolean disableRttResponderNative(int iface, int commandId);
229668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
229768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Disable RTT responder role. Returns {@code true} if responder role is successfully disabled,
229868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * {@code false} otherwise.
229968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
230068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public boolean disableRttResponder() {
230168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
230268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return false;
230368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId == 0) {
230468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                Log.e(mTAG, "responder role not enabled yet");
230568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return true;
230668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
230768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            sRttResponderCmdId = 0;
230868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return disableRttResponderNative(sWlan0Index, sRttResponderCmdId);
230968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
231068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
231168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
2312042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2313042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
231418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2315956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
231671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2317042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2318042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2319042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2320042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2321042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2322042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2323efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2324efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2325efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2326efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
232718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2328956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2329ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2330ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
233118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2332ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2333ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2334efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2335efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
23360465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2337ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
233818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2339956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2340ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2341ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
234218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2343ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2344ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2345ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2346ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
23470465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
23480465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
234918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
2350956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
235171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
23520465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
23530465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
23540465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
23550465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
23560465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
23570465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2358b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
2359b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    private static native boolean toggleInterfaceNative(int on);
236018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean toggleInterface(int on) {
2361956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
236271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
236318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return toggleInterfaceNative(on);
2364b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2365b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2366b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2367b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2368b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
236912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
237012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
237118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
2372956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
237371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
237412cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
237518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
237612cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
237712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
237812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
237912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2380939177ff615062ec826601d536466875d8457375xinhe
23816609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    public static final class PacketFilterCapabilities {
23826609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        /**
23836609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen         * Version of APF instruction set supported for packet filtering.  0 indicates no support for
23846609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen         * packet filtering using APF programs.
23856609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen         */
23866609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        public int apfVersionSupported;
23876609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
23886609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        /**
23896609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen         * Maximum size of APF program allowed.
23906609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen         */
23916609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        public int maximumApfProgramSize;
23926609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
23936609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    private static native PacketFilterCapabilities getPacketFilterCapabilitiesNative(int iface);
23946609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    public PacketFilterCapabilities getPacketFilterCapabilities() {
23956609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
23966609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
23976609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return getPacketFilterCapabilitiesNative(sWlan0Index);
23986609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
23996609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return null;
24006609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
24016609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
24026609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
24036609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
24046609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    private static native boolean installPacketFilterNative(int iface, byte[] filter);
24056609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    public boolean installPacketFilter(byte[] filter) {
24066609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
24076609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
24086609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return installPacketFilterNative(sWlan0Index, filter);
24096609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
24106609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return false;
24116609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
24126609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
24136609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
24146609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
2415939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
241618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2417956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
241871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2419939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2420939177ff615062ec826601d536466875d8457375xinhe            } else {
2421939177ff615062ec826601d536466875d8457375xinhe                return false;
2422939177ff615062ec826601d536466875d8457375xinhe            }
2423939177ff615062ec826601d536466875d8457375xinhe        }
2424939177ff615062ec826601d536466875d8457375xinhe    }
2425939177ff615062ec826601d536466875d8457375xinhe
2426d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2427d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2428d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2429d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2430d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2431d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2432d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2433d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2434d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
243518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2436956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2437f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2438f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2439d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2440d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2441d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2442d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2443d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2444d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2445d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2446d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2447d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2448d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2449d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2450d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
245118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2452956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
245371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2454d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2455d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2456d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2457d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2458d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2459d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2460d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2461d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2462d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2463d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2464d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2465d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2466d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2467d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2468d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2469d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2470d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2471d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2472d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2473d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
247418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2475956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
247671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2477d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2478d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2479d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2480d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2481d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2482d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2483d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
248418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
248518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
248618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
248718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
248818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
248918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
249018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
249118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2492d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2493d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2494a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2495a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2496a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
24977d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2498a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
24990bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
25000bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2501a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2502a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2503a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2504a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
250518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
25060bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
250718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
250818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
250918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
251003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
251103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
251218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
25130bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
251418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
251518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
251618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
251703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
251803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2519b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2520b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
252118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
2522956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2523b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2524b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2525b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2526b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2527b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2528b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2529b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2530b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2531b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2532b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2533b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2534b797893fc1966803d0c013faac42e6396a37a384xinhe            }
253503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
253603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
253703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
253803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
25390bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
254018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
254103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
2542956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
254371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
254403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
254503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
254603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
254703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
254803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
254903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
255003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
255103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
255203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
255318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
2554956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
255571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
255603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
255703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2558f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
255903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
256003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
256103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
256203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2563b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
256418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2565956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2566b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2567b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2568b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2569b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2570b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2571b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2572b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2573b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2574b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2575b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2576b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2577b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2578b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2579b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2580b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2581b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2582b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2583b797893fc1966803d0c013faac42e6396a37a384xinhe
258403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
258518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
2586956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
258771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
258803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
258903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2590f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
259103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
259203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
259303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
259403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
259503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
259603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
259718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
2598956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
259971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
260003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
260103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2602f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
260303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
260403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
260503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
260603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
26070bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
26080bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
26090bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
26100bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
26110bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
26120bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
26130bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
26140bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
26150bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
26160bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
26170bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
26180bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
26190bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
26200bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
26210bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
26220bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
26230bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
26240bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
26250bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
26260bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
262718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
2628956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
262971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
263003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
263103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
263203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
263303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
263403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
263503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
263603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
263703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
263818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
2639956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
264071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
264103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
264203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
264303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
264403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
264503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
264603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
264703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
264818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
264918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
265003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
265198dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
265298dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
265398dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
265498dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
265598dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
265603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2657127f7244183786e6ccae09e81eeccdac31973e69xinhe
265803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
265918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
2660956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
266171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
26620bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
26630bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
266403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
26650bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
26660bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
26670bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
266803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
266903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2670f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2671a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2672a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2673dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2674dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
26755c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /* Configure ePNO/PNO */
2676dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
267718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* pno flags, keep these values in sync with gscan.h */
267818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_OPEN  = 1; // open
267918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_PSK   = 2; // WPA_PSK or WPA2PSK
268018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_EAPOL = 4; // any EAPOL
268118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
268218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether directed scan needs to be performed (for hidden SSIDs)
268318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_DIRECTED_SCAN = 1;
268418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on A band
268518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_A_BAND = 2;
268618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on G band
268718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_G_BAND = 4;
268818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether strict matching is required (i.e. firmware shall not match on the entire SSID)
268918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_STRICT_MATCH = 8;
269018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
26915c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /**
26925c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     * Object holding the network ID and the corresponding priority to be set before enabling/
26935c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     * disabling PNO.
26945c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     */
269518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiPnoNetwork {
26965c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public String SSID;
26975c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int rssi_threshold; // TODO remove
26985c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int flags;
26995c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int auth;
27005c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public String configKey; // kept for reference
27015c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int networkId;
27025c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int priority;
2703dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
27045c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        WifiPnoNetwork(WifiConfiguration config, int threshold, int newPriority) {
2705dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.SSID == null) {
27065c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                SSID = "";
27075c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                flags = WIFI_PNO_FLAG_DIRECTED_SCAN;
2708dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
27095c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                SSID = config.SSID;
2710dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
27115c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            rssi_threshold = threshold;
2712dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
271318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_PSK;
2714dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) ||
2715dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
271618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_EAPOL;
2717dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
271818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_OPEN;
2719dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
272018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            flags |= WIFI_PNO_FLAG_A_BAND | WIFI_PNO_FLAG_G_BAND;
2721dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            configKey = config.configKey();
27225c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            networkId = config.networkId;
27235c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            priority = newPriority;
2724dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2725dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2726dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        @Override
2727dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        public String toString() {
2728dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2729dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(this.SSID);
2730dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" flags=").append(this.flags);
2731dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" rssi=").append(this.rssi_threshold);
2732dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" auth=").append(this.auth);
27335c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            sbuf.append(" Network ID=").append(this.networkId);
27345c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            sbuf.append(" Priority=").append(this.priority);
2735dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return sbuf.toString();
2736dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2737dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2738dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2739dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    public static interface WifiPnoEventHandler {
2740dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        void onPnoNetworkFound(ScanResult results[]);
2741dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2742dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2743dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static WifiPnoEventHandler sWifiPnoEventHandler;
2744dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2745dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static int sPnoCmdId = 0;
2746dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2747dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]);
2748dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
274918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setPnoList(WifiPnoNetwork list[],
2750dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                                  WifiPnoEventHandler eventHandler) {
2751dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2752dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2753956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
275471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2755dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2756f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2757dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2758f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiPnoEventHandler = eventHandler;
2759f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setPnoListNative(sWlan0Index, sPnoCmdId, list)) {
2760f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2761f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2762dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2763dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2764f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sWifiPnoEventHandler = null;
2765f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
2766dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2767dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2768dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
276918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
27703571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void onPnoNetworkFound(int id, ScanResult[] results, int[] beaconCaps) {
2771dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2772dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2773dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2774dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2775dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2776dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2777dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
277818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiPnoEventHandler handler = sWifiPnoEventHandler;
277918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
278018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
278118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
278218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
2783dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
27841ab129e587d334a144a0bca5323c27985397a403Randy Pan                populateScanResult(results[i], beaconCaps[i], "onPnoNetworkFound ");
278518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2786dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
278718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
278818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
278918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
279018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
279118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
2792dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2793d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2794d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
27959ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
27969ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
27979ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
279818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
27999ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
28009ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
28019ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
28029ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
28039ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
28049ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2805956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
280671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2807f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2808f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
2809f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
28109ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
28119ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
28129ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
28139ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
28149ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2815c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
2816c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
2817c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
281818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2819c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
2820c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
28213bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
28223bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
28233bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
28243bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
28253bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
28263bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
28273bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
2828956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2829c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2830c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
283118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
2832c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2833c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2834c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2835c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2836c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2837c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2838c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
2839c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
284018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2841c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
2842c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
2843956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2844c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2845c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
2846c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2847c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2848c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2849c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2850c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2851aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2852aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
2853aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
2854aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2855aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2856aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
2857aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
285818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
285918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
286018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
286118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
286218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
286318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2864aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2865aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2866aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
2867aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
2868aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2869aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
2870aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
287118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
2872aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
2873aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
2874956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
287518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
2876aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2877ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2878ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
2879ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2880ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
2881aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
2882aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
2883ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
2884ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
2885ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
2886ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
2887ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2888ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2889aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2890aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2891aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2892aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2893aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2894aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2895aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
2896aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
289718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
2898aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
2899956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2900aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2901ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
2902ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2903ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
2904ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2905ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
2906ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2907aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2908aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2909aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2910aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2911aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
29125ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
29135ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
29146bf6986d359556010638dfae332b585162f06520Roshan Pius
29156bf6986d359556010638dfae332b585162f06520Roshan Pius    /**
29166bf6986d359556010638dfae332b585162f06520Roshan Pius     * Fetch the host wakeup reasons stats from wlan driver.
29176bf6986d359556010638dfae332b585162f06520Roshan Pius     * @return the |WifiWakeReasonAndCounts| object retrieved from the wlan driver.
29186bf6986d359556010638dfae332b585162f06520Roshan Pius     */
29196bf6986d359556010638dfae332b585162f06520Roshan Pius    public WifiWakeReasonAndCounts getWlanWakeReasonCount() {
29206bf6986d359556010638dfae332b585162f06520Roshan Pius        Log.d(TAG, "getWlanWakeReasonCount " + sWlan0Index);
29216bf6986d359556010638dfae332b585162f06520Roshan Pius        synchronized (sLock) {
29226bf6986d359556010638dfae332b585162f06520Roshan Pius            if (isHalStarted()) {
29236bf6986d359556010638dfae332b585162f06520Roshan Pius                return getWlanWakeReasonCountNative(sWlan0Index);
29246bf6986d359556010638dfae332b585162f06520Roshan Pius            } else {
29256bf6986d359556010638dfae332b585162f06520Roshan Pius                return null;
29266bf6986d359556010638dfae332b585162f06520Roshan Pius            }
29276bf6986d359556010638dfae332b585162f06520Roshan Pius        }
29286bf6986d359556010638dfae332b585162f06520Roshan Pius    }
29293dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
29303dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    private static native int configureNeighborDiscoveryOffload(int iface, boolean enabled);
29313dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
29323dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    public boolean configureNeighborDiscoveryOffload(boolean enabled) {
29333dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        final String logMsg =  "configureNeighborDiscoveryOffload(" + enabled + ")";
29343dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        Log.d(mTAG, logMsg);
29353dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        synchronized (sLock) {
29363dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            if (isHalStarted()) {
29373dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                final int ret = configureNeighborDiscoveryOffload(sWlan0Index, enabled);
29383dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                if (ret != 0) {
29393dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                    Log.d(mTAG, logMsg + " returned: " + ret);
29403dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                }
29413dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                return (ret == 0);
29423dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            }
29433dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        }
29443dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        return false;
29453dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    }
2946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
2947