WifiNative.java revision 9d7489491984e86915b2cf4fac38a882de1c8cdb
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
32918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
330ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    /**
331ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * Start a scan using wpa_supplicant for the given frequencies.
332ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * If freqs is null then all supported channels are scanned.
333ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     */
334ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    public boolean scan(Set<Integer> freqs) {
335ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        if (freqs == null) {
336ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return scanFrequencyList(null);
337ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        } else if (freqs.size() != 0) {
3389ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            StringBuilder freqList = new StringBuilder();
3399ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            boolean first = true;
3409ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            for (Integer freq : freqs) {
341ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills                if (!first) {
3429ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                    freqList.append(",");
343ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills                }
3449ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                freqList.append(freq.toString());
3459ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                first = false;
3469ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            }
347ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return scanFrequencyList(freqList.toString());
348ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        } else {
3499ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            return false;
3509ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3519ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
3529ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
353ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    private boolean scanFrequencyList(String freqList) {
354ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        if (freqList == null) {
355ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return doBooleanCommand("SCAN TYPE=ONLY");
356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
357ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
375e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
376e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
377e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
378e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
383fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public boolean setNetworkExtra(int netId, String name, Map<String, String> values) {
384fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String encoded;
385fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
386fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            encoded = URLEncoder.encode(new JSONObject(values).toString(), "UTF-8");
387fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (NullPointerException e) {
388fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
389fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
390fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
391fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
392fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
393fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
394fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        return setNetworkVariable(netId, name, "\"" + encoded + "\"");
395fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
396fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
399a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (name.equals(WifiConfiguration.pskVarName)
400a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham                || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
401a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value);
402a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        } else {
403a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
404a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
407fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public Map<String, String> getNetworkExtra(int netId, String name) {
408fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String wrapped = getNetworkVariable(netId, name);
409fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        if (wrapped == null || !wrapped.startsWith("\"") || !wrapped.endsWith("\"")) {
410fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
411fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
412fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
413fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final String encoded = wrapped.substring(1, wrapped.length() - 1);
414fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This method reads a JSON dictionary that was written by setNetworkExtra(). However,
415fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // on devices that upgraded from Marshmallow, it may encounter a legacy value instead -
416fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // an FQDN stored as a plain string. If such a value is encountered, the JSONObject
417fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // constructor will thrown a JSONException and the method will return null.
418fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final JSONObject json = new JSONObject(URLDecoder.decode(encoded, "UTF-8"));
419fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Map<String, String> values = new HashMap<String, String>();
420fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Iterator<?> it = json.keys();
421fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            while (it.hasNext()) {
422fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final String key = (String) it.next();
423fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final Object value = json.get(key);
424fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                if (value instanceof String) {
425fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                    values.put(key, (String) value);
426fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                }
427fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            }
428fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return values;
429fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
430fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
431fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
432fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (JSONException e) {
433fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This is not necessarily an error. This exception will also occur if we encounter a
434fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // legacy FQDN stored as a plain string. We want to return null in this case as no JSON
435fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // dictionary of extras was found.
436fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
437fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
438fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
439fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
451f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
452f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
453f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
454f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
455ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
456ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
457ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
458ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
459ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
460ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
4639d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius
4649d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4659d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Enables a network in wpa_supplicant.
4669d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be enabled.
4679d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4689d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
4699d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    public boolean enableNetwork(int netId) {
4709d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId));
4719d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        return doBooleanCommand("ENABLE_NETWORK " + netId);
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4749d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4759d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Disables a network in wpa_supplicant.
4769d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be disabled.
4779d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4789d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
480f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4849d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4859d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Select a network in wpa_supplicant (Disables all others).
4869d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be selected.
4879d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4889d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
4890047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
4900047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
4910047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
4920047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
4930047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
500f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
50521bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
51099d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
51399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
51499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
51599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
51699d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
51799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
51899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
51999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
52099d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
531a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
532a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
5372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
5392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
5402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
5432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
5442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
5452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
5462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
5472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
5482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
5492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
55677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        1                       0     2
55777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
55877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        0                       1     1   -> 9
55977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
56077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       1     9   -> d
56177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
56277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       0     8
56377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
56477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         1                        1                       1     7
56577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
56677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
56777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
56877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
56977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
57077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  wpa_supplicant_ctrl_iface_bss:4315
57177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  print_bss_info
572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
5732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
5742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
5752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
5762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
5782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
5792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
5802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
5812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
5822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
5832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
5842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
5852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
5862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
5872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
5892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
5902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
5912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
5922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
5932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
5942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5952afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
5962afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
5972afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5982afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
5992afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6002afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
6022afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
6032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
6042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
6052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
6062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
6082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
6135d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
6142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
6202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
6222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
6282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
6342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
6402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
6422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
6442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
6462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
6482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
6492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
6502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
6512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
6522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
6535d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
6542afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
6552afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
6562afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
6572afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6582afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
6592afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
6602afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
6612afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
6625d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
6635d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
6645d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6655d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
6665d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
6675d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
6685d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6695d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6705d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
6715d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
6725d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
6735d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6745d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
6755d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
676947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            if (DBG) {
677947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                Log.v(TAG + ":DTIM", "SSID" + networkDetail.getSSID()
678947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + ", DTIM=" + networkDetail.getDtimInterval() + ", "
679947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                                        + " IEstr:" + infoElementsStr);
680947e55415eab3989f2f5cede0c03745cf9268309Glen Kuhne                            }
6812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
6822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
6832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
6842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
6855d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
6862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
6892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, "HSNwk: '" + networkDetail);
6902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6919ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
69263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills                                    level, freq, tsf, infoElements, anqpLines);
6939ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
6942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
6952afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
6962afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
6972afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6982afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
6992afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
7002afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
7012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
7022afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
7032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
7045d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
7052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
7062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
7072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
7082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
7092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
71077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
71177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
713446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
714446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
715446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
716446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
717446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
718446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
719446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
720446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
721446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
722446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
723446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
724446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
725446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
726446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
727446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
72856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
729446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
730446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
731446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
732446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
733446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
734446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
735446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
736446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
737446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
738446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
739446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
740446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
741446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
742446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
743446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
744446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
81403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
81503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
81603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
81703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
81803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
81903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
82003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
82103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
82303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
82403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
82503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
82603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
82703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
82803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
82903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
83003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
83103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
83418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
83518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
83618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
8377ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
8387ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
8397ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
8407ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
8417ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
8427ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
8437ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
8447ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
851155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
852155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8650a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
8660a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
8670a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
8680a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
8910465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
8920465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
8930465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
8940465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
897ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius
898e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    //PNO Monitor
899e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    private class PnoMonitor {
900e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final int MINIMUM_PNO_GAP = 5 * 1000;
901e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final String ACTION_TOGGLE_PNO =
902e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            "com.android.server.Wifi.action.TOGGLE_PNO";
903e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        long mLastPnoChangeTimeStamp = -1L;
904e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mExpectedPnoState = false;
9055c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        List<WifiPnoNetwork> mExpectedWifiPnoNetworkList = null;
906e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mCurrentPnoState = false;;
907e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mWaitForTimer = false;
908e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        final Object mPnoLock = new Object();
909e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final AlarmManager mAlarmManager =
910e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
911e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final PendingIntent mPnoIntent;
912e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
913e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public PnoMonitor() {
914e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            Intent intent = new Intent(ACTION_TOGGLE_PNO, null);
915e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            intent.setPackage("android");
916e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
917e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
918e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mContext.registerReceiver(
919e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new BroadcastReceiver() {
920e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    @Override
921e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    public void onReceive(Context context, Intent intent) {
922e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        synchronized(mPnoLock) {
923e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (DBG) Log.d(mTAG, "PNO timer expire, PNO should change to " +
924e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    mExpectedPnoState);
925e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (mCurrentPnoState != mExpectedPnoState) {
926e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to "
927e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                        + mExpectedPnoState);
928ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                                boolean ret = setPno(
9295c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                                        mExpectedPnoState, mExpectedWifiPnoNetworkList);
930e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (!ret) {
931e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    Log.e(mTAG, "set PNO failure");
932e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                }
933e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            } else {
934e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "Do not change PNO since current is expected");
935e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            }
936e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            mWaitForTimer = false;
937e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        }
938e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
939e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                },
940e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new IntentFilter(ACTION_TOGGLE_PNO));
941e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
942e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
943ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        /*  Enable/Disable PNO with updated network priorities.
944ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         *
945ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param enable boolean indicating whether PNO is being enabled or disabled.
946ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param pnoNetworkList list of networks with priorities to be set before PNO setting.
947ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         */
9485c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        private boolean setPno(boolean enable, List<WifiPnoNetwork> pnoNetworkList) {
949ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // TODO: Couple of cases yet to be handled:
950ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 1. What if the network priority update fails, should we bail out of PNO setting?
951ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 2. If PNO setting fails below, should we go back and revert this priority change?
952ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            if (pnoNetworkList != null) {
953ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                if (DBG) Log.i(mTAG, "Update priorities for PNO. Enable: " + enable);
9545c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                for (WifiPnoNetwork pnoNetwork : pnoNetworkList) {
955ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    // What if this fails? Should we bail out?
956ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    boolean isSuccess = setNetworkVariable(pnoNetwork.networkId,
957ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            WifiConfiguration.priorityVarName,
958ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            Integer.toString(pnoNetwork.priority));
9595c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    if (!isSuccess) {
960ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        Log.e(mTAG, "Update priority failed for :" + pnoNetwork.networkId);
961ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    }
962ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                }
963ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            }
964e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            String cmd = enable ? "SET pno 1" : "SET pno 0";
965e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            boolean ret = doBooleanCommand(cmd);
966e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mLastPnoChangeTimeStamp = System.currentTimeMillis();
967e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            if (ret) {
968e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                mCurrentPnoState = enable;
969e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
970e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return ret;
971e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
972e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
973ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        public boolean enableBackgroundScan(
974ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                boolean enable,
9755c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                List<WifiPnoNetwork> pnoNetworkList) {
976e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            synchronized(mPnoLock) {
977e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                if (mWaitForTimer) {
978e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    //already has a timer
979e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    mExpectedPnoState = enable;
9805c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    mExpectedWifiPnoNetworkList = pnoNetworkList;
981e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (DBG) Log.d(mTAG, "update expected PNO to " +  mExpectedPnoState);
982e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                } else {
983e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (mCurrentPnoState == enable) {
984e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        return true;
985e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
986e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp;
987e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (timeDifference >= MINIMUM_PNO_GAP) {
988ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        return setPno(enable, pnoNetworkList);
989e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    } else {
990e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mExpectedPnoState = enable;
9915c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                        mExpectedWifiPnoNetworkList = pnoNetworkList;
992e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mWaitForTimer = true;
993e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference);
994e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
995e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                System.currentTimeMillis() + timeDifference, mPnoIntent);
996e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
997e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                }
998e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                return true;
999e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
1000e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
1001e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
1002e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
1003ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius    public boolean enableBackgroundScan(
1004ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            boolean enable,
10055c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            List<WifiPnoNetwork> pnoNetworkList) {
1006e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        if (mPnoMonitor != null) {
1007ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            return mPnoMonitor.enableBackgroundScan(enable, pnoNetworkList);
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1009e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return false;
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1013f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
1014f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
1015f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
1016f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
1017f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
1018f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1019f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1020f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
102556845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
102656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
102756845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
102856845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
102956845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
103056845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
103156845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
103256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
1035956f54b391677d78379729dd14518edddf3c7660Etan Cohen            synchronized (sLock) {
103618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
103718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
103818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
1075956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1091956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1106956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
111533b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
111618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
111718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
111818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
111933b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
112033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1121f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1122f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
112318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
112433b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
112533b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
112626eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean simAuthFailedResponse(int id) {
112726eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type GSM-AUTH
112826eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL");
112926eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
113026eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
113126eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean umtsAuthFailedResponse(int id) {
113226eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type UMTS-AUTH
113326eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL");
113426eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
113526eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
1136ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
113718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1138ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1139ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1188956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1202956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1264956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
126518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
126618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
126718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
126818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
126918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
127318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
127418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
127518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
127618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
127718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
127818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
127918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
128018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
128918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1378956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
141136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
141236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
141327f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
141427f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
141536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
141636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
141736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
1418956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
141927f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
142027f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
142127f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
142227f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
142336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
142436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
142536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
142636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
142736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
142836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
142936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
143036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
143136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
143436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
143536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
143636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1503956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
150418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
150518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
150618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
150718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
150818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
150918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1520956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
152118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
152218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
152418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
152518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
152618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
152718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
152818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
152918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
153018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
153118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
153218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
153318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
153418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
153518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
153618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
153718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
153818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
15693f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1570446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1571446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1572446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1573446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1574f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1575f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1576f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1577f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1578f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1579f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1580f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1581f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1582f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1583f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1584f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1585f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1586f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1587f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1588f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1589f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1590f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1591f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1592f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1593f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1594f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1595f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1596f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
15977e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15987e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    /* kernel logging support */
15997e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    private static native byte[] readKernelLogNative();
16007e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16017e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    synchronized public String readKernelLog() {
16027e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        byte[] bytes = readKernelLogNative();
16037e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        if (bytes != null) {
16047e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
16057e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            try {
16067e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
16077e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return decoded.toString();
16087e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            } catch (CharacterCodingException cce) {
16097e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return new String(bytes, StandardCharsets.ISO_8859_1);
16107e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            }
16117e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        } else {
16127e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            return "*** failed to read kernel log ***";
16137e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        }
16147e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    }
16157e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16167f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
16177f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
161818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
161918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
162018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
162118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
162218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
162318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1624b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1625f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1626f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1627956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static int sWlan0Index = -1;
1628aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sP2p0Index = -1;
1629f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1630f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
16317f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1632b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1633b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1634b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
16357f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1636b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
16377ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1638b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
16397ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
16407ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
16417ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16427ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
164318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1644d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1645d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1646d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1647d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1648d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1649d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1650956f54b391677d78379729dd14518edddf3c7660Etan Cohen        sLocalLog.log(debugLog);
1651d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1652956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1653cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1654f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1655f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1656aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1657aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1658956f54b391677d78379729dd14518edddf3c7660Etan Cohen                if (DBG) sLocalLog.log("Could not start hal");
1659f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1660aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1661aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
16627ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
16637ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16647ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
166518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1666956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
166771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1668f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1669f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1670f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1671f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1672f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1673f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1674f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1675f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1676f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1677f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1678f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1679f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sP2p0Index = -1;
1680f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1681f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
16827ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16837f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
168418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
168571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
168671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1687b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
16887f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
168918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getInterfaces() {
1690956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
169171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1692f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sWifiIfaceHandles == null) {
1693f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int num = getInterfacesNative();
1694f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int wifi_num = 0;
1695f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    for (int i = 0; i < num; i++) {
1696f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        String name = getInterfaceNameNative(i);
1697f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        Log.i(TAG, "interface[" + i + "] = " + name);
1698f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        if (name.equals("wlan0")) {
1699f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sWlan0Index = i;
1700f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1701f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        } else if (name.equals("p2p0")) {
1702f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sP2p0Index = i;
1703f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1704f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        }
170502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1706f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return wifi_num;
1707f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1708f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return sWifiIfaceHandles.length;
1709aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
171002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            } else {
1711f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
1712e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1713e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
17147f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17157f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1716b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
171718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
1718956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
171918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
172018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
17217f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17227f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1723062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan    // TODO: Change variable names to camel style.
1724e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1725297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1726e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1727e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1728e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1729297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
17307d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1731e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1732e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1733e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
173418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1735956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1736af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1737af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1738e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1739e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1740b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1741b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1742e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1743b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1744b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
174583a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1746b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1747d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
17487f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1749e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1750712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int frequency;
1751712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int dwell_time_ms;
1752712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public boolean passive;
17537f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17547f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1755e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1756712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int bucket;
1757712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int band;
1758712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int period_ms;
1759712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_period_ms;
1760712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int step_count;
1761712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_events;
1762712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_channels;
1763712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public ChannelSettings[] channels;
1764e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17657f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1766e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1767712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int base_period_ms;
1768712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_ap_per_scan;
1769712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_percent;
1770712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_num_scans;
1771712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_buckets;
1772712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public BucketSettings[] buckets;
1773e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17747f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
177568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
177668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Wi-Fi channel information.
177768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
177868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public static class WifiChannelInfo {
177968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mPrimaryFrequency;
178068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency0;
178168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency1;
178268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mChannelWidth;
178368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        // TODO: add preamble once available in HAL.
178468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
178568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
1786b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
178763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
178863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called for each AP as it is found with the entire contents of the beacon/probe response.
178963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Only called when WifiScanner.REPORT_EVENT_FULL_SCAN_RESULT is specified.
179063539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1791476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        void onFullScanResult(ScanResult fullScanResult);
179263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
179363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Callback on an event during a gscan scan.
179463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * See WifiNative.WIFI_SCAN_* for possible values.
179563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
179663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        void onScanStatus(int event);
179763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
179863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is paused.
179963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
180083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
180163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
180263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is resumed.
180363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1804b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1805e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1806e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1807b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
180871af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_RESULTS_AVAILABLE = 0;
180971af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1;
181071af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_PERCENT = 2;
181171af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_FAILED = 3;
1812b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
181318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
18142a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onScanStatus(int id, int event) {
181518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
181663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        if (handler != null) {
181763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills            handler.onScanStatus(event);
18182a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        }
1819e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1820e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
182118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
18225cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
18235cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18245cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
18255cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
18265cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18275cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18285cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
18295cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18305cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
18315cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18325cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18335cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
18345cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
18355cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18365cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
18375cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
18385cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
18395cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
18405cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
18415cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
18425cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18435cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18445cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
18455cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
18465cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18475cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18485cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
18495cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18505cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
185118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
18525cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
18535cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
18545cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
18555cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18565cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18575cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
18585cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
18595cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
18605cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18615cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18621ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void populateScanResult(ScanResult result, int beaconCap, String dbg) {
1863dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
18645d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18655d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
18665d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
18675d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
18685d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
18695d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
18705d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18715d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
18721ab129e587d334a144a0bca5323c27985397a403Randy Pan                InformationElementUtil.parseInformationElements(result.bytes);
18735d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
18745d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
18755d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
18765d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
18775d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
18785d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
18795d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
1880243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1881243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1882d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
18835d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
1884d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
1885d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
1886d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
1887d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
1888d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
1889243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
18905d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
18915d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
18925d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
18935d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
1894243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
18955d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
18965d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
1897243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
1898243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
18993571366ac36c70746b9f013ec2b54482861c9292Randy Pan
19003571366ac36c70746b9f013ec2b54482861c9292Randy Pan        // build capabilities string
19013571366ac36c70746b9f013ec2b54482861c9292Randy Pan        BitSet beaconCapBits = new BitSet(16);
19023571366ac36c70746b9f013ec2b54482861c9292Randy Pan        for (int i = 0; i < 16; i++) {
19033571366ac36c70746b9f013ec2b54482861c9292Randy Pan            if ((beaconCap & (1 << i)) != 0) {
19043571366ac36c70746b9f013ec2b54482861c9292Randy Pan                beaconCapBits.set(i);
19053571366ac36c70746b9f013ec2b54482861c9292Randy Pan            }
19063571366ac36c70746b9f013ec2b54482861c9292Randy Pan        }
19073571366ac36c70746b9f013ec2b54482861c9292Randy Pan        result.capabilities = InformationElementUtil.Capabilities.buildCapabilities(elements,
19083571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                               beaconCapBits);
19093571366ac36c70746b9f013ec2b54482861c9292Randy Pan
1910243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
19113571366ac36c70746b9f013ec2b54482861c9292Randy Pan            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth
19123571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " PrimaryFreq: " + result.frequency + " mCenterfreq0: " + result.centerFreq0
19133571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder
19143571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    ? "Support RTT reponder: " : "Do not support RTT responder")
19153571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " Capabilities: " + result.capabilities);
1916f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1917f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1918476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1919dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1920dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
192118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
19221ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void onFullScanResult(int id, ScanResult result,
19233571366ac36c70746b9f013ec2b54482861c9292Randy Pan            int bucketsScanned, int beaconCap) {
19241ab129e587d334a144a0bca5323c27985397a403Randy Pan        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID);
1925dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
192618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
192718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
19281ab129e587d334a144a0bca5323c27985397a403Randy Pan            populateScanResult(result, beaconCap, " onFullScanResult ");
192918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onFullScanResult(result);
1930dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
19317f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19327f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1933b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1934b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1935b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
19367f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
193718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
1938956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
193971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1940f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
1941f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
1942f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
1943b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1944f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
19457f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1946f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
1947e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1948f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
1949f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
1950b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1951f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1952f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
1953f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
1954f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1955f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1956f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1957f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1958f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1959f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1960e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1961e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1962e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
19637f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19647f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
196518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
1966956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
196771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1968ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
1969ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
1970ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
1971f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
1972f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
1973f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
1974f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1975b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1976b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1977b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
197818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
1979956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
198071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1981f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1982f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
1983f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1984f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
1985f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1986f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
1987f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1988b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1989b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1990b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1991b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
199218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
1993956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
199471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1995f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
19966f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
1997f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
1998f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
1999f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
2000f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
2001f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
200283a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
2003f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
2004f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
2005f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
200683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
2007b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
2008e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2009e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2010e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
201118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
2012956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
20133ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            WifiScanner.ScanData[] sd = null;
201471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
20153ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                sd = getScanResultsNative(sWlan0Index, flush);
20163ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            }
20173ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande
20183ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            if (sd != null) {
20193ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return sd;
2020f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
20213ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return new WifiScanner.ScanData[0];
2022f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2023aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2024e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2025e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2026b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
2027d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
2028d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
2029e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2030e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2031b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
2032b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
2033e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2034b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
2035e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
2036b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
2037e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
203818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
203918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
2040956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
204171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2042f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2043f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2044f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2045f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
2046f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2047f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2048f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
2049f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
2050f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2051f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2052f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2053e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2054f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2055f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2056e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2057e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2058e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2059e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2060e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
206118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
2062956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
206371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2064f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2065f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
2066f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
2067f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2068f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2069e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
20707f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
20717f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
2072e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
207318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
207418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
207518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
207618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
207718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
207818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
207918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
208018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
2081d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
2082d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
2083d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
208418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
208518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
208618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
208718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
208818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
208918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
209018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
209118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
2092aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2093e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2094e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2095b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
2096e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
2097e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2098e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2099b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
2100b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
2101e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2102b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
2103e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
2104b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
2105e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
210618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
2107b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
2108956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
210971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2110f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2111f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2112f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2113f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
2114f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2115f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2116f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
2117851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
2118851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
2119f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2120f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2121f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2122e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2123f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2124f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2125e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2126e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2127e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2128e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2129e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2130e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
213118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
2132956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
213371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2134f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2135f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
2136f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
2137f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2138f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2139e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2140e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2141e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2142e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
214318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
214418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
214518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
214618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
214718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
214818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
2149f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
215018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
2151aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2152e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2153e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
215418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
2155200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
2156200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
2157956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
215871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2159aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
2160f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2161f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2162f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2163aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
21645c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
21655c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
216618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2167d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2168956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
216971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2170d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2171d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2172d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2173d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2174d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2175c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
217618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2177956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
217871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2179f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2180f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2181f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2182f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2183f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2184f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2185a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2186143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2187143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2188143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2189143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2190143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2191143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2192143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2193143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2194143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
219518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
219618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
219718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
219818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
219902a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
220018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2201143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2202143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
220318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
220418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2205143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2206143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2207143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2208143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2209143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2210143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2211143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2212143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
221318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2214143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2215956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
221671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2217f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2218f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2219f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2220f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2221f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2222f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2223f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2224f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2225f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2226143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2227f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2228143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2229143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2230143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2231143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
223218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
2233956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
223471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2235f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2236f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2237f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2238143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2239f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2240f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2241f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2242f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
22435cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2244f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2245f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
22465cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2247f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2248f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2249143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2250143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2251143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2252143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2253143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2254042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
225568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static int sRttResponderCmdId = 0;
225668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
225768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native ResponderConfig enableRttResponderNative(int iface, int commandId,
225868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int timeoutSeconds, WifiChannelInfo channelHint);
225968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
226068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Enable RTT responder role on the device. Returns {@link ResponderConfig} if the responder
226168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * role is successfully enabled, {@code null} otherwise.
226268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
226368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    @Nullable
226468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public ResponderConfig enableRttResponder(int timeoutSeconds) {
226568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
226668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return null;
226768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId != 0) {
226868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                if (DBG) Log.e(mTAG, "responder mode already enabled - this shouldn't happen");
226968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return null;
227068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
227168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int id = getNewCmdIdLocked();
227268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            ResponderConfig config = enableRttResponderNative(
227368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                    sWlan0Index, id, timeoutSeconds, null);
227468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (config != null) sRttResponderCmdId = id;
227568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (DBG) Log.d(TAG, "enabling rtt " + (config != null));
227668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return config;
227768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
227868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
227968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
228068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native boolean disableRttResponderNative(int iface, int commandId);
228168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
228268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Disable RTT responder role. Returns {@code true} if responder role is successfully disabled,
228368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * {@code false} otherwise.
228468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
228568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public boolean disableRttResponder() {
228668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
228768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return false;
228868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId == 0) {
228968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                Log.e(mTAG, "responder role not enabled yet");
229068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return true;
229168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
229268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            sRttResponderCmdId = 0;
229368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return disableRttResponderNative(sWlan0Index, sRttResponderCmdId);
229468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
229568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
229668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
2297042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2298042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
229918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2300956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
230171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2302042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2303042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2304042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2305042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2306042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2307042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2308efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2309efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2310efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2311efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
231218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2313956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2314ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2315ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
231618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2317ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2318ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2319efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2320efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
23210465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2322ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
232318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2324956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2325ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2326ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
232718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2328ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2329ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2330ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2331ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
23320465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
23330465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
233418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
2335956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
233671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
23370465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
23380465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
23390465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
23400465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
23410465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
23420465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2343b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
2344b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    private static native boolean toggleInterfaceNative(int on);
234518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean toggleInterface(int on) {
2346956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
234771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
234818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return toggleInterfaceNative(on);
2349b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2350b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2351b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2352b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2353b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
235412cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
235512cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
235618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
2357956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
235871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
235912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
236018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
236112cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
236212cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
236312cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
236412cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2365939177ff615062ec826601d536466875d8457375xinhe
2366939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
236718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2368956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
236971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2370939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2371939177ff615062ec826601d536466875d8457375xinhe            } else {
2372939177ff615062ec826601d536466875d8457375xinhe                return false;
2373939177ff615062ec826601d536466875d8457375xinhe            }
2374939177ff615062ec826601d536466875d8457375xinhe        }
2375939177ff615062ec826601d536466875d8457375xinhe    }
2376939177ff615062ec826601d536466875d8457375xinhe
2377d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2378d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2379d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2380d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2381d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2382d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2383d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2384d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2385d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
238618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2387956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2388f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2389f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2390d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2391d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2392d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2393d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2394d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2395d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2396d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2397d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2398d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2399d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2400d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2401d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
240218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2403956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
240471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2405d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2406d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2407d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2408d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2409d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2410d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2411d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2412d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2413d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2414d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2415d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2416d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2417d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2418d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2419d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2420d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2421d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2422d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2423d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2424d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
242518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2426956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
242771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2428d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2429d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2430d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2431d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2432d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2433d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2434d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
243518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
243618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
243718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
243818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
243918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
244018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
244118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
244218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2443d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2444d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2445a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2446a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2447a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
24487d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2449a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
24500bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
24510bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2452a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2453a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2454a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2455a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
245618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24570bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
245818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
245918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
246018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
246103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
246203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
246318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24640bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
246518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
246618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
246718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
246803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
246903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2470b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2471b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
247218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
2473956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2474b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2475b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2476b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2477b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2478b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2479b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2480b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2481b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2482b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2483b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2484b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2485b797893fc1966803d0c013faac42e6396a37a384xinhe            }
248603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
248703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
248803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
248903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
24900bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
249118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
249203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
2493956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
249471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
249503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
249603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
249703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
249803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
249903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
250003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
250103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
250203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
250303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
250418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
2505956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
250671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
250703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
250803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2509f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
251003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
251103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
251203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
251303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2514b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
251518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2516956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2517b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2518b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2519b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2520b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2521b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2522b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2523b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2524b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2525b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2526b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2527b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2528b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2529b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2530b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2531b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2532b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2533b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2534b797893fc1966803d0c013faac42e6396a37a384xinhe
253503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
253618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
2537956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
253871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
253903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
254003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2541f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
254203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
254303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
254403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
254503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
254603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
254703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
254818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
2549956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
255071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
255103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
255203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2553f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
255403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
255503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
255603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
255703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
25580bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
25590bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
25600bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
25610bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
25620bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
25630bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
25640bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
25650bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
25660bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
25670bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25680bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
25690bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
25700bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
25710bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
25720bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
25730bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
25740bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
25750bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
25760bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25770bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
257818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
2579956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
258071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
258103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
258203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
258303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
258403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
258503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
258603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
258703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
258803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
258918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
2590956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
259171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
259203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
259303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
259403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
259503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
259603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
259703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
259803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
259918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
260018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
260103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
260298dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
260398dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
260498dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
260598dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
260698dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
260703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2608127f7244183786e6ccae09e81eeccdac31973e69xinhe
260903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
261018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
2611956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
261271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
26130bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
26140bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
261503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
26160bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
26170bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
26180bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
261903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
262003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2621f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2622a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2623a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2624dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2625dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
26265c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /* Configure ePNO/PNO */
2627dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
262818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* pno flags, keep these values in sync with gscan.h */
262918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_OPEN  = 1; // open
263018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_PSK   = 2; // WPA_PSK or WPA2PSK
263118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_EAPOL = 4; // any EAPOL
263218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
263318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether directed scan needs to be performed (for hidden SSIDs)
263418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_DIRECTED_SCAN = 1;
263518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on A band
263618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_A_BAND = 2;
263718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on G band
263818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_G_BAND = 4;
263918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether strict matching is required (i.e. firmware shall not match on the entire SSID)
264018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_STRICT_MATCH = 8;
264118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
26425c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /**
26435c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     * Object holding the network ID and the corresponding priority to be set before enabling/
26445c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     * disabling PNO.
26455c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     */
264618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiPnoNetwork {
26475c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public String SSID;
26485c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int rssi_threshold; // TODO remove
26495c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int flags;
26505c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int auth;
26515c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public String configKey; // kept for reference
26525c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int networkId;
26535c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int priority;
2654dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
26555c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        WifiPnoNetwork(WifiConfiguration config, int threshold, int newPriority) {
2656dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.SSID == null) {
26575c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                SSID = "";
26585c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                flags = WIFI_PNO_FLAG_DIRECTED_SCAN;
2659dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
26605c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                SSID = config.SSID;
2661dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
26625c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            rssi_threshold = threshold;
2663dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
266418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_PSK;
2665dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) ||
2666dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
266718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_EAPOL;
2668dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
266918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_OPEN;
2670dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
267118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            flags |= WIFI_PNO_FLAG_A_BAND | WIFI_PNO_FLAG_G_BAND;
2672dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            configKey = config.configKey();
26735c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            networkId = config.networkId;
26745c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            priority = newPriority;
2675dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2676dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2677dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        @Override
2678dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        public String toString() {
2679dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2680dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(this.SSID);
2681dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" flags=").append(this.flags);
2682dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" rssi=").append(this.rssi_threshold);
2683dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" auth=").append(this.auth);
26845c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            sbuf.append(" Network ID=").append(this.networkId);
26855c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            sbuf.append(" Priority=").append(this.priority);
2686dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return sbuf.toString();
2687dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2688dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2689dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2690dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    public static interface WifiPnoEventHandler {
2691dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        void onPnoNetworkFound(ScanResult results[]);
2692dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2693dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2694dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static WifiPnoEventHandler sWifiPnoEventHandler;
2695dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2696dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static int sPnoCmdId = 0;
2697dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2698dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]);
2699dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
270018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setPnoList(WifiPnoNetwork list[],
2701dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                                  WifiPnoEventHandler eventHandler) {
2702dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2703dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2704956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
270571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2706dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2707f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2708dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2709f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiPnoEventHandler = eventHandler;
2710f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setPnoListNative(sWlan0Index, sPnoCmdId, list)) {
2711f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2712f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2713dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2714dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2715f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sWifiPnoEventHandler = null;
2716f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
2717dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2718dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2719dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
272018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
27213571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void onPnoNetworkFound(int id, ScanResult[] results, int[] beaconCaps) {
2722dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2723dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2724dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2725dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2726dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2727dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2728dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
272918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiPnoEventHandler handler = sWifiPnoEventHandler;
273018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
273118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
273218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
273318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
2734dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
27351ab129e587d334a144a0bca5323c27985397a403Randy Pan                populateScanResult(results[i], beaconCaps[i], "onPnoNetworkFound ");
273618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2737dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
273818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
273918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
274018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
274118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
274218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
2743dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2744d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2745d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
27462a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
274718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiLazyRoamParams {
2748d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_threshold;
2749d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_threshold;
2750d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_factor;
2751d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_factor;
2752d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_max_boost;
2753d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int lazy_roam_hysteresis;
2754d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int alert_roam_rssi_trigger;
2755dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2756d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        WifiLazyRoamParams() {
2757d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2758d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
2759d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        @Override
2760d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        public String toString() {
2761d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2762d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_threshold=").append(this.A_band_boost_threshold);
2763d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_threshold=").append(this.A_band_penalty_threshold);
2764d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_factor=").append(this.A_band_boost_factor);
2765d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_factor=").append(this.A_band_penalty_factor);
2766d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_max_boost=").append(this.A_band_max_boost);
2767d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" lazy_roam_hysteresis=").append(this.lazy_roam_hysteresis);
2768d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" alert_roam_rssi_trigger=").append(this.alert_roam_rssi_trigger);
2769d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            return sbuf.toString();
2770d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2771d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2772d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
27732a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
277418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLazyRoam(boolean enabled, WifiLazyRoamParams params) {
27752a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        return false;
2776d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
27779ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
27789ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
27799ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
27809ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
278118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
27829ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
27839ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
27849ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
27859ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
27869ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
27879ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2788956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
278971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2790f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2791f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
2792f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
27939ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
27949ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
27959ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
27969ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
27979ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
27982a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
279918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setSsidWhitelist(String list[]) {
28002a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        return false;
28015caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle    }
2802c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2803c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
2804c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
2805c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
280618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2807c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
2808c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
28093bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
28103bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
28113bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
28123bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
28133bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
28143bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
28153bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
2816956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2817c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2818c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
281918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
2820c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2821c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2822c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2823c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2824c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2825c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2826c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
2827c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
282818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2829c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
2830c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
2831956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2832c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2833c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
2834c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2835c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2836c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2837c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2838c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2839aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2840aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
2841aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
2842aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2843aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2844aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
2845aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
284618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
284718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
284818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
284918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
285018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
285118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2852aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2853aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2854aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
2855aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
2856aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2857aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
2858aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
285918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
2860aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
2861aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
2862956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
286318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
2864aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2865ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2866ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
2867ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2868ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
2869aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
2870aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
2871ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
2872ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
2873ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
2874ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
2875ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2876ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2877aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2878aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2879aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2880aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2881aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2882aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2883aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
2884aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
288518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
2886aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
2887956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2888aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2889ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
2890ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2891ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
2892ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2893ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
2894ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2895aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2896aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2897aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2898aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2899aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
29005ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
29015ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
29026bf6986d359556010638dfae332b585162f06520Roshan Pius
29036bf6986d359556010638dfae332b585162f06520Roshan Pius    /**
29046bf6986d359556010638dfae332b585162f06520Roshan Pius     * Fetch the host wakeup reasons stats from wlan driver.
29056bf6986d359556010638dfae332b585162f06520Roshan Pius     * @return the |WifiWakeReasonAndCounts| object retrieved from the wlan driver.
29066bf6986d359556010638dfae332b585162f06520Roshan Pius     */
29076bf6986d359556010638dfae332b585162f06520Roshan Pius    public WifiWakeReasonAndCounts getWlanWakeReasonCount() {
29086bf6986d359556010638dfae332b585162f06520Roshan Pius        Log.d(TAG, "getWlanWakeReasonCount " + sWlan0Index);
29096bf6986d359556010638dfae332b585162f06520Roshan Pius        synchronized (sLock) {
29106bf6986d359556010638dfae332b585162f06520Roshan Pius            if (isHalStarted()) {
29116bf6986d359556010638dfae332b585162f06520Roshan Pius                return getWlanWakeReasonCountNative(sWlan0Index);
29126bf6986d359556010638dfae332b585162f06520Roshan Pius            } else {
29136bf6986d359556010638dfae332b585162f06520Roshan Pius                return null;
29146bf6986d359556010638dfae332b585162f06520Roshan Pius            }
29156bf6986d359556010638dfae332b585162f06520Roshan Pius        }
29166bf6986d359556010638dfae332b585162f06520Roshan Pius    }
2917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
2918