WifiNative.java revision 5c3c06082b24f9ff0d479e82a63b52220c86598b
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    }
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
464ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
465ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + " disableOthers=" + disableOthers);
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("SELECT_NETWORK " + netId);
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("ENABLE_NETWORK " + netId);
470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
474f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4780047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
4790047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
4800047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
4810047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
4820047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
484f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
489f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
49421bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
49999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
50299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
50399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
50499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
50599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
50699d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
50799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
50899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
50999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
520a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
521a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
5262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
5282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
5292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
5322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
5332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
5342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
5352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
5362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
5372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
5382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
54577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        1                       0     2
54677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
54777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        0                       1     1   -> 9
54877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
54977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       1     9   -> d
55077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
55177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       0     8
55277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
55377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         1                        1                       1     7
55477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
55577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
55677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
55777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
55877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
55977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  wpa_supplicant_ctrl_iface_bss:4315
56077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  print_bss_info
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
5622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
5632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
5642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
5652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
5672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
5682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
5692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
5702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
5712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
5722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
5732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
5742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
5752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
5762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
5782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
5792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
5802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
5812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
5822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
5832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
5852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
5862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
5882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
5912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
5922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
5932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
5942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
5952afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5962afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
5972afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
5982afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
5992afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
6002afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
6012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
6025d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
6032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
6042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
6062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
6072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
6132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
6142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
6212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
6232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
6392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
6402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
6412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
6425d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
6432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
6442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
6452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
6462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
6482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
6492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
6502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
6515d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
6525d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
6535d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6545d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
6555d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
6565d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
6575d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6585d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6595d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
6605d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
6615d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
6625d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6635d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
6645d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
6652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
6672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
6682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
6692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
6705d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
6712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
6742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, "HSNwk: '" + networkDetail);
6752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6769ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
67763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills                                    level, freq, tsf, infoElements, anqpLines);
6789ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
6792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
6802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
6812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
6822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
6842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
6852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
6862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
6872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
6882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
6895d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
6902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
6912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
6922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
6932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
6942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
69577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
69677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
698446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
699446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
700446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
701446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
702446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
703446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
704446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
705446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
706446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
707446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
708446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
709446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
710446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
711446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
712446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
71356d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
714446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
715446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
716446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
717446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
718446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
719446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
720446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
721446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
722446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
723446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
724446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
725446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
726446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
727446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
728446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
729446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
79903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
80003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
80103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
80203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
80303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
80403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
80503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
80603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
80803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
80903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
81003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
81103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
81203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
81303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
81403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
81503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
81603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
81918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
82018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
82118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
8227ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
8237ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
8247ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
8257ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
8267ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
8277ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
8287ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
8297ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8500a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
8510a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
8520a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
8530a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
8760465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
8770465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
8780465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
8790465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
882ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius
883e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    //PNO Monitor
884e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    private class PnoMonitor {
885e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final int MINIMUM_PNO_GAP = 5 * 1000;
886e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final String ACTION_TOGGLE_PNO =
887e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            "com.android.server.Wifi.action.TOGGLE_PNO";
888e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        long mLastPnoChangeTimeStamp = -1L;
889e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mExpectedPnoState = false;
8905c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        List<WifiPnoNetwork> mExpectedWifiPnoNetworkList = null;
891e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mCurrentPnoState = false;;
892e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mWaitForTimer = false;
893e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        final Object mPnoLock = new Object();
894e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final AlarmManager mAlarmManager =
895e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
896e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final PendingIntent mPnoIntent;
897e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
898e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public PnoMonitor() {
899e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            Intent intent = new Intent(ACTION_TOGGLE_PNO, null);
900e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            intent.setPackage("android");
901e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
902e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
903e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mContext.registerReceiver(
904e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new BroadcastReceiver() {
905e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    @Override
906e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    public void onReceive(Context context, Intent intent) {
907e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        synchronized(mPnoLock) {
908e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (DBG) Log.d(mTAG, "PNO timer expire, PNO should change to " +
909e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    mExpectedPnoState);
910e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (mCurrentPnoState != mExpectedPnoState) {
911e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to "
912e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                        + mExpectedPnoState);
913ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                                boolean ret = setPno(
9145c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                                        mExpectedPnoState, mExpectedWifiPnoNetworkList);
915e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (!ret) {
916e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    Log.e(mTAG, "set PNO failure");
917e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                }
918e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            } else {
919e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "Do not change PNO since current is expected");
920e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            }
921e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            mWaitForTimer = false;
922e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        }
923e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
924e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                },
925e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new IntentFilter(ACTION_TOGGLE_PNO));
926e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
927e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
928ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        /*  Enable/Disable PNO with updated network priorities.
929ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         *
930ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param enable boolean indicating whether PNO is being enabled or disabled.
931ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         * @param pnoNetworkList list of networks with priorities to be set before PNO setting.
932ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius         */
9335c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        private boolean setPno(boolean enable, List<WifiPnoNetwork> pnoNetworkList) {
934ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // TODO: Couple of cases yet to be handled:
935ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 1. What if the network priority update fails, should we bail out of PNO setting?
936ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            // 2. If PNO setting fails below, should we go back and revert this priority change?
937ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            if (pnoNetworkList != null) {
938ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                if (DBG) Log.i(mTAG, "Update priorities for PNO. Enable: " + enable);
9395c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                for (WifiPnoNetwork pnoNetwork : pnoNetworkList) {
940ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    // What if this fails? Should we bail out?
941ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    boolean isSuccess = setNetworkVariable(pnoNetwork.networkId,
942ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            WifiConfiguration.priorityVarName,
943ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                            Integer.toString(pnoNetwork.priority));
9445c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    if (!isSuccess) {
945ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        Log.e(mTAG, "Update priority failed for :" + pnoNetwork.networkId);
946ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                    }
947ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                }
948ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            }
949e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            String cmd = enable ? "SET pno 1" : "SET pno 0";
950e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            boolean ret = doBooleanCommand(cmd);
951e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mLastPnoChangeTimeStamp = System.currentTimeMillis();
952e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            if (ret) {
953e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                mCurrentPnoState = enable;
954e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
955e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return ret;
956e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
957e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
958ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius        public boolean enableBackgroundScan(
959ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                boolean enable,
9605c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                List<WifiPnoNetwork> pnoNetworkList) {
961e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            synchronized(mPnoLock) {
962e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                if (mWaitForTimer) {
963e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    //already has a timer
964e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    mExpectedPnoState = enable;
9655c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                    mExpectedWifiPnoNetworkList = pnoNetworkList;
966e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (DBG) Log.d(mTAG, "update expected PNO to " +  mExpectedPnoState);
967e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                } else {
968e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (mCurrentPnoState == enable) {
969e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        return true;
970e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
971e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp;
972e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (timeDifference >= MINIMUM_PNO_GAP) {
973ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius                        return setPno(enable, pnoNetworkList);
974e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    } else {
975e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mExpectedPnoState = enable;
9765c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                        mExpectedWifiPnoNetworkList = pnoNetworkList;
977e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mWaitForTimer = true;
978e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference);
979e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
980e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                System.currentTimeMillis() + timeDifference, mPnoIntent);
981e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
982e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                }
983e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                return true;
984e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
985e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
986e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
987e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
988ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius    public boolean enableBackgroundScan(
989ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            boolean enable,
9905c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            List<WifiPnoNetwork> pnoNetworkList) {
991e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        if (mPnoMonitor != null) {
992ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius            return mPnoMonitor.enableBackgroundScan(enable, pnoNetworkList);
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
994e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return false;
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
998f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
999f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
1000f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
1001f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
1002f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
1003f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1004f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1005f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
101056845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
101156845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
101256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
101356845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
101456845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
101556845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
101656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
101756845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
1020956f54b391677d78379729dd14518edddf3c7660Etan Cohen            synchronized (sLock) {
102118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
102218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
102318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1029155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
1030155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
1031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
1032155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
1060956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1076956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1091956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
110033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
110118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
110218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
110318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
110433b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
110533b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1106f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1107f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
110818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
110933b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
111033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
111126eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean simAuthFailedResponse(int id) {
111226eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type GSM-AUTH
111326eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL");
111426eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
111526eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
111626eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean umtsAuthFailedResponse(int id) {
111726eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type UMTS-AUTH
111826eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL");
111926eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
112026eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
1121ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
112218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1123ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1124ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1173956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1187956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1249956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
125018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
125118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
125218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
125318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
125418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
125818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
125918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
126018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
126118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
126218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
126318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
126418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
126518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
127418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1363956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
139636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
139736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
139827f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
139927f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
140036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
140136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
140236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
1403956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
140427f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
140527f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
140627f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
140727f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
140836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
140936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
141036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
141136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
141236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
141336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
141436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
141536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
141636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
141936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
142036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
142136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1488956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
148918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
149018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
149118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
149218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
149318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
149418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1505956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
150618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
150718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
150918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
151018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
151118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
151218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
151318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
151418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
151518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
151618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
151718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
151818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
151918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
152018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
152118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
152218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
152318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
15543f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1555446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1556446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1557446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1558446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1559f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1560f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1561f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1562f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1563f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1564f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1565f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1566f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1567f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1568f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1569f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1570f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1571f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1572f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1573f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1574f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1575f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1576f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1577f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1578f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1579f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1580f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1581f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
15827e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15837e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    /* kernel logging support */
15847e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    private static native byte[] readKernelLogNative();
15857e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15867e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    synchronized public String readKernelLog() {
15877e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        byte[] bytes = readKernelLogNative();
15887e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        if (bytes != null) {
15897e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
15907e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            try {
15917e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
15927e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return decoded.toString();
15937e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            } catch (CharacterCodingException cce) {
15947e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return new String(bytes, StandardCharsets.ISO_8859_1);
15957e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            }
15967e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        } else {
15977e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            return "*** failed to read kernel log ***";
15987e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        }
15997e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    }
16007e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
16017f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
16027f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
160318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
160418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
160518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
160618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
160718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
160818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1609b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1610f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1611f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1612956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static int sWlan0Index = -1;
1613aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sP2p0Index = -1;
1614f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1615f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
16167f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1617b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1618b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1619b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
16207f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1621b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
16227ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1623b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
16247ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
16257ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
16267ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16277ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
162818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1629d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1630d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1631d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1632d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1633d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1634d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1635956f54b391677d78379729dd14518edddf3c7660Etan Cohen        sLocalLog.log(debugLog);
1636d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1637956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1638cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1639f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1640f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1641aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1642aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1643956f54b391677d78379729dd14518edddf3c7660Etan Cohen                if (DBG) sLocalLog.log("Could not start hal");
1644f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1645aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1646aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
16477ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
16487ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16497ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
165018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1651956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
165271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1653f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1654f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1655f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1656f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1657f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1658f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1659f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1660f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1661f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1662f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1663f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1664f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sP2p0Index = -1;
1665f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1666f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
16677ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16687f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
166918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
167071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
167171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1672b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
16737f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
167418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getInterfaces() {
1675956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
167671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1677f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sWifiIfaceHandles == null) {
1678f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int num = getInterfacesNative();
1679f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int wifi_num = 0;
1680f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    for (int i = 0; i < num; i++) {
1681f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        String name = getInterfaceNameNative(i);
1682f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        Log.i(TAG, "interface[" + i + "] = " + name);
1683f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        if (name.equals("wlan0")) {
1684f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sWlan0Index = i;
1685f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1686f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        } else if (name.equals("p2p0")) {
1687f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sP2p0Index = i;
1688f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1689f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        }
169002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1691f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return wifi_num;
1692f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1693f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return sWifiIfaceHandles.length;
1694aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
169502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            } else {
1696f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
1697e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1698e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
16997f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17007f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1701b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
170218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
1703956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
170418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
170518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
17067f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17077f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1708062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan    // TODO: Change variable names to camel style.
1709e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1710297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1711e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1712e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1713e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1714297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
17157d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1716e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1717e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1718e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
171918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1720956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1721af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1722af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1723e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1724e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1725b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1726b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1727e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1728b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1729b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
173083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1731b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1732d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
17337f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1734e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1735712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int frequency;
1736712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int dwell_time_ms;
1737712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public boolean passive;
17387f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17397f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1740e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1741712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int bucket;
1742712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int band;
1743712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int period_ms;
1744712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_period_ms;
1745712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int step_count;
1746712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_events;
1747712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_channels;
1748712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public ChannelSettings[] channels;
1749e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17507f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1751e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1752712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int base_period_ms;
1753712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_ap_per_scan;
1754712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_percent;
1755712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_num_scans;
1756712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_buckets;
1757712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public BucketSettings[] buckets;
1758e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17597f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
176068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
176168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Wi-Fi channel information.
176268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
176368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public static class WifiChannelInfo {
176468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mPrimaryFrequency;
176568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency0;
176668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency1;
176768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mChannelWidth;
176868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        // TODO: add preamble once available in HAL.
176968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
177068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
1771b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
177263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
177363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called for each AP as it is found with the entire contents of the beacon/probe response.
177463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Only called when WifiScanner.REPORT_EVENT_FULL_SCAN_RESULT is specified.
177563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1776476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        void onFullScanResult(ScanResult fullScanResult);
177763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
177863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Callback on an event during a gscan scan.
177963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * See WifiNative.WIFI_SCAN_* for possible values.
178063539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
178163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        void onScanStatus(int event);
178263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
178363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is paused.
178463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
178583a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
178663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
178763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is resumed.
178863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1789b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1790e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1791e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1792b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
179371af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_RESULTS_AVAILABLE = 0;
179471af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1;
179571af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_PERCENT = 2;
179671af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_FAILED = 3;
1797b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
179818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
17992a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onScanStatus(int id, int event) {
180018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
180163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        if (handler != null) {
180263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills            handler.onScanStatus(event);
18032a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        }
1804e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1805e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
180618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
18075cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
18085cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18095cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
18105cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
18115cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18125cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18135cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
18145cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18155cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
18165cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18175cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18185cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
18195cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
18205cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18215cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
18225cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
18235cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
18245cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
18255cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
18265cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
18275cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18285cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18295cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
18305cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
18315cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18325cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18335cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
18345cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18355cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
183618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
18375cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
18385cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
18395cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
18405cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18415cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18425cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
18435cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
18445cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
18455cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18465cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18471ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void populateScanResult(ScanResult result, int beaconCap, String dbg) {
1848dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
18495d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18505d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
18515d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
18525d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
18535d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
18545d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
18555d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18565d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
18571ab129e587d334a144a0bca5323c27985397a403Randy Pan                InformationElementUtil.parseInformationElements(result.bytes);
18585d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
18595d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
18605d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
18615d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
18625d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
18635d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
18645d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
1865243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1866243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1867d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
18685d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
1869d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
1870d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
1871d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
1872d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
1873d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
1874243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
18755d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
18765d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
18775d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
18785d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
1879243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
18805d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
18815d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
1882243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
1883243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
18843571366ac36c70746b9f013ec2b54482861c9292Randy Pan
18853571366ac36c70746b9f013ec2b54482861c9292Randy Pan        // build capabilities string
18863571366ac36c70746b9f013ec2b54482861c9292Randy Pan        BitSet beaconCapBits = new BitSet(16);
18873571366ac36c70746b9f013ec2b54482861c9292Randy Pan        for (int i = 0; i < 16; i++) {
18883571366ac36c70746b9f013ec2b54482861c9292Randy Pan            if ((beaconCap & (1 << i)) != 0) {
18893571366ac36c70746b9f013ec2b54482861c9292Randy Pan                beaconCapBits.set(i);
18903571366ac36c70746b9f013ec2b54482861c9292Randy Pan            }
18913571366ac36c70746b9f013ec2b54482861c9292Randy Pan        }
18923571366ac36c70746b9f013ec2b54482861c9292Randy Pan        result.capabilities = InformationElementUtil.Capabilities.buildCapabilities(elements,
18933571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                               beaconCapBits);
18943571366ac36c70746b9f013ec2b54482861c9292Randy Pan
1895243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
18963571366ac36c70746b9f013ec2b54482861c9292Randy Pan            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth
18973571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " PrimaryFreq: " + result.frequency + " mCenterfreq0: " + result.centerFreq0
18983571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder
18993571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    ? "Support RTT reponder: " : "Do not support RTT responder")
19003571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " Capabilities: " + result.capabilities);
1901f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1902f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1903476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1904dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1905dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
190618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
19071ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void onFullScanResult(int id, ScanResult result,
19083571366ac36c70746b9f013ec2b54482861c9292Randy Pan            int bucketsScanned, int beaconCap) {
19091ab129e587d334a144a0bca5323c27985397a403Randy Pan        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID);
1910dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
191118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
191218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
19131ab129e587d334a144a0bca5323c27985397a403Randy Pan            populateScanResult(result, beaconCap, " onFullScanResult ");
191418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onFullScanResult(result);
1915dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
19167f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19177f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1918b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1919b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1920b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
19217f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
192218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
1923956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
192471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1925f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
1926f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
1927f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
1928b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1929f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
19307f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1931f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
1932e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1933f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
1934f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
1935b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1936f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1937f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
1938f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
1939f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1940f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1941f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1942f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1943f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1944f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1945e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1946e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1947e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
19487f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19497f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
195018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
1951956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
195271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1953ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
1954ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
1955ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
1956f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
1957f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
1958f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
1959f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1960b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1961b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1962b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
196318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
1964956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
196571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1966f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1967f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
1968f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1969f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
1970f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1971f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
1972f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1973b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1974b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1975b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1976b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
197718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
1978956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
197971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1980f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
19816f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
1982f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
1983f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
1984f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
1985f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
1986f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
198783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
1988f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
1989f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
1990f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
199183a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
1992b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1993e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1994e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1995e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
199618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
1997956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19983ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            WifiScanner.ScanData[] sd = null;
199971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
20003ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                sd = getScanResultsNative(sWlan0Index, flush);
20013ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            }
20023ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande
20033ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            if (sd != null) {
20043ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return sd;
2005f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
20063ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return new WifiScanner.ScanData[0];
2007f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2008aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2009e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2010e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2011b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
2012d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
2013d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
2014e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2015e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2016b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
2017b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
2018e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2019b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
2020e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
2021b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
2022e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
202318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
202418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
2025956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
202671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2027f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2028f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2029f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2030f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
2031f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2032f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2033f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
2034f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
2035f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2036f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2037f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2038e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2039f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2040f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2041e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2042e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2043e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2044e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2045e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
204618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
2047956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
204871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2049f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2050f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
2051f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
2052f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2053f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2054e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
20557f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
20567f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
2057e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
205818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
205918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
206018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
206118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
206218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
206318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
206418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
206518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
2066d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
2067d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
2068d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
206918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
207018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
207118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
207218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
207318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
207418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
207518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
207618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
2077aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2078e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2079e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2080b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
2081e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
2082e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2083e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2084b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
2085b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
2086e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2087b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
2088e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
2089b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
2090e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
209118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
2092b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
2093956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
209471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2095f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2096f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2097f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2098f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
2099f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2100f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2101f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
2102851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
2103851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
2104f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2105f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2106f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2107e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2108f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2109f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2110e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2111e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2112e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2113e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2114e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2115e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
211618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
2117956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
211871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2119f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2120f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
2121f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
2122f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2123f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2124e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2125e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2126e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2127e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
212818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
212918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
213018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
213118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
213218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
213318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
2134f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
213518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
2136aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2137e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2138e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
213918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
2140200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
2141200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
2142956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
214371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2144aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
2145f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2146f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2147f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2148aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
21495c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
21505c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
215118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2152d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2153956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
215471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2155d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2156d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2157d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2158d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2159d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2160c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
216118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2162956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
216371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2164f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2165f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2166f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2167f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2168f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2169f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2170a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2171143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2172143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2173143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2174143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2175143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2176143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2177143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2178143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2179143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
218018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
218118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
218218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
218318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
218402a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
218518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2186143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2187143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
218818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
218918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2190143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2191143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2192143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2193143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2194143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2195143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2196143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2197143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
219818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2199143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2200956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
220171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2202f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2203f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2204f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2205f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2206f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2207f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2208f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2209f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2210f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2211143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2212f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2213143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2214143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2215143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2216143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
221718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
2218956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
221971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2220f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2221f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2222f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2223143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2224f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2225f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2226f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2227f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
22285cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2229f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2230f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
22315cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2232f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2233f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2234143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2235143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2236143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2237143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2238143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2239042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
224068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static int sRttResponderCmdId = 0;
224168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
224268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native ResponderConfig enableRttResponderNative(int iface, int commandId,
224368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int timeoutSeconds, WifiChannelInfo channelHint);
224468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
224568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Enable RTT responder role on the device. Returns {@link ResponderConfig} if the responder
224668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * role is successfully enabled, {@code null} otherwise.
224768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
224868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    @Nullable
224968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public ResponderConfig enableRttResponder(int timeoutSeconds) {
225068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
225168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return null;
225268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId != 0) {
225368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                if (DBG) Log.e(mTAG, "responder mode already enabled - this shouldn't happen");
225468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return null;
225568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
225668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int id = getNewCmdIdLocked();
225768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            ResponderConfig config = enableRttResponderNative(
225868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                    sWlan0Index, id, timeoutSeconds, null);
225968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (config != null) sRttResponderCmdId = id;
226068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (DBG) Log.d(TAG, "enabling rtt " + (config != null));
226168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return config;
226268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
226368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
226468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
226568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native boolean disableRttResponderNative(int iface, int commandId);
226668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
226768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Disable RTT responder role. Returns {@code true} if responder role is successfully disabled,
226868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * {@code false} otherwise.
226968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
227068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public boolean disableRttResponder() {
227168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
227268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return false;
227368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId == 0) {
227468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                Log.e(mTAG, "responder role not enabled yet");
227568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return true;
227668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
227768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            sRttResponderCmdId = 0;
227868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return disableRttResponderNative(sWlan0Index, sRttResponderCmdId);
227968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
228068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
228168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
2282042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2283042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
228418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2285956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
228671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2287042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2288042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2289042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2290042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2291042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2292042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2293efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2294efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2295efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2296efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
229718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2298956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2299ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2300ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
230118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2302ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2303ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2304efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2305efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
23060465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2307ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
230818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2309956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2310ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2311ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
231218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2313ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2314ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2315ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2316ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
23170465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
23180465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
231918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
2320956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
232171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
23220465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
23230465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
23240465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
23250465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
23260465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
23270465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2328b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
2329b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    private static native boolean toggleInterfaceNative(int on);
233018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean toggleInterface(int on) {
2331956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
233271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
233318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return toggleInterfaceNative(on);
2334b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2335b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2336b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2337b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2338b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
233912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
234012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
234118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
2342956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
234371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
234412cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
234518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
234612cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
234712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
234812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
234912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2350939177ff615062ec826601d536466875d8457375xinhe
2351939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
235218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2353956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
235471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2355939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2356939177ff615062ec826601d536466875d8457375xinhe            } else {
2357939177ff615062ec826601d536466875d8457375xinhe                return false;
2358939177ff615062ec826601d536466875d8457375xinhe            }
2359939177ff615062ec826601d536466875d8457375xinhe        }
2360939177ff615062ec826601d536466875d8457375xinhe    }
2361939177ff615062ec826601d536466875d8457375xinhe
2362d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2363d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2364d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2365d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2366d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2367d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2368d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2369d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2370d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
237118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2372956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2373f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2374f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2375d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2376d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2377d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2378d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2379d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2380d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2381d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2382d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2383d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2384d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2385d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2386d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
238718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2388956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
238971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2390d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2391d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2392d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2393d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2394d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2395d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2396d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2397d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2398d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2399d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2400d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2401d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2402d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2403d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2404d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2405d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2406d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2407d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2408d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2409d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
241018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2411956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
241271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2413d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2414d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2415d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2416d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2417d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2418d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2419d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
242018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
242118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
242218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
242318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
242418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
242518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
242618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
242718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2428d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2429d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2430a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2431a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2432a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
24337d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2434a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
24350bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
24360bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2437a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2438a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2439a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2440a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
244118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24420bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
244318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
244418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
244518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
244603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
244703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
244818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24490bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
245018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
245118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
245218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
245303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
245403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2455b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2456b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
245718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
2458956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2459b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2460b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2461b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2462b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2463b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2464b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2465b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2466b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2467b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2468b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2469b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2470b797893fc1966803d0c013faac42e6396a37a384xinhe            }
247103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
247203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
247303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
247403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
24750bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
247618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
247703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
2478956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
247971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
248003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
248103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
248203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
248303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
248403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
248503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
248603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
248703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
248803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
248918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
2490956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
249171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
249203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
249303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2494f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
249503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
249603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
249703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
249803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2499b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
250018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2501956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2502b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2503b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2504b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2505b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2506b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2507b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2508b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2509b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2510b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2511b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2512b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2513b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2514b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2515b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2516b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2517b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2518b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2519b797893fc1966803d0c013faac42e6396a37a384xinhe
252003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
252118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
2522956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
252371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
252403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
252503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2526f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
252703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
252803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
252903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
253003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
253103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
253203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
253318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
2534956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
253571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
253603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
253703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2538f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
253903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
254003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
254103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
254203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
25430bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
25440bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
25450bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
25460bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
25470bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
25480bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
25490bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
25500bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
25510bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
25520bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25530bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
25540bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
25550bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
25560bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
25570bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
25580bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
25590bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
25600bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
25610bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25620bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
256318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
2564956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
256571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
256603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
256703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
256803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
256903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
257003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
257103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
257203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
257303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
257418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
2575956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
257671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
257703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
257803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
257903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
258003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
258103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
258203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
258303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
258418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
258518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
258603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
258798dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
258898dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
258998dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
259098dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
259198dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
259203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2593127f7244183786e6ccae09e81eeccdac31973e69xinhe
259403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
259518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
2596956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
259771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
25980bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
25990bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
260003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
26010bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
26020bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
26030bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
260403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
260503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2606f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2607a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2608a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2609dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2610dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
26115c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /* Configure ePNO/PNO */
2612dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
261318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* pno flags, keep these values in sync with gscan.h */
261418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_OPEN  = 1; // open
261518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_PSK   = 2; // WPA_PSK or WPA2PSK
261618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_EAPOL = 4; // any EAPOL
261718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
261818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether directed scan needs to be performed (for hidden SSIDs)
261918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_DIRECTED_SCAN = 1;
262018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on A band
262118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_A_BAND = 2;
262218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on G band
262318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_G_BAND = 4;
262418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether strict matching is required (i.e. firmware shall not match on the entire SSID)
262518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_STRICT_MATCH = 8;
262618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
26275c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /**
26285c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     * Object holding the network ID and the corresponding priority to be set before enabling/
26295c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     * disabling PNO.
26305c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     */
263118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiPnoNetwork {
26325c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public String SSID;
26335c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int rssi_threshold; // TODO remove
26345c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int flags;
26355c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int auth;
26365c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public String configKey; // kept for reference
26375c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int networkId;
26385c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        public int priority;
2639dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
26405c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius        WifiPnoNetwork(WifiConfiguration config, int threshold, int newPriority) {
2641dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.SSID == null) {
26425c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                SSID = "";
26435c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                flags = WIFI_PNO_FLAG_DIRECTED_SCAN;
2644dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
26455c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius                SSID = config.SSID;
2646dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
26475c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            rssi_threshold = threshold;
2648dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
264918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_PSK;
2650dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) ||
2651dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
265218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_EAPOL;
2653dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
265418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_OPEN;
2655dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
265618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            flags |= WIFI_PNO_FLAG_A_BAND | WIFI_PNO_FLAG_G_BAND;
2657dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            configKey = config.configKey();
26585c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            networkId = config.networkId;
26595c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            priority = newPriority;
2660dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2661dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2662dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        @Override
2663dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        public String toString() {
2664dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2665dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(this.SSID);
2666dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" flags=").append(this.flags);
2667dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" rssi=").append(this.rssi_threshold);
2668dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" auth=").append(this.auth);
26695c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            sbuf.append(" Network ID=").append(this.networkId);
26705c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius            sbuf.append(" Priority=").append(this.priority);
2671dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return sbuf.toString();
2672dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2673dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2674dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2675dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    public static interface WifiPnoEventHandler {
2676dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        void onPnoNetworkFound(ScanResult results[]);
2677dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2678dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2679dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static WifiPnoEventHandler sWifiPnoEventHandler;
2680dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2681dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static int sPnoCmdId = 0;
2682dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2683dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]);
2684dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
268518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setPnoList(WifiPnoNetwork list[],
2686dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                                  WifiPnoEventHandler eventHandler) {
2687dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2688dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2689956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
269071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2691dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2692f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2693dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2694f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiPnoEventHandler = eventHandler;
2695f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setPnoListNative(sWlan0Index, sPnoCmdId, list)) {
2696f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2697f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2698dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2699dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2700f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sWifiPnoEventHandler = null;
2701f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
2702dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2703dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2704dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
270518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
27063571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void onPnoNetworkFound(int id, ScanResult[] results, int[] beaconCaps) {
2707dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2708dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2709dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2710dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2711dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2712dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2713dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
271418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiPnoEventHandler handler = sWifiPnoEventHandler;
271518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
271618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
271718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
271818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
2719dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
27201ab129e587d334a144a0bca5323c27985397a403Randy Pan                populateScanResult(results[i], beaconCaps[i], "onPnoNetworkFound ");
272118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2722dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
272318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
272418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
272518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
272618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
272718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
2728dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2729d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2730d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
27312a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
273218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiLazyRoamParams {
2733d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_threshold;
2734d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_threshold;
2735d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_factor;
2736d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_factor;
2737d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_max_boost;
2738d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int lazy_roam_hysteresis;
2739d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int alert_roam_rssi_trigger;
2740dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2741d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        WifiLazyRoamParams() {
2742d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2743d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
2744d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        @Override
2745d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        public String toString() {
2746d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2747d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_threshold=").append(this.A_band_boost_threshold);
2748d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_threshold=").append(this.A_band_penalty_threshold);
2749d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_factor=").append(this.A_band_boost_factor);
2750d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_factor=").append(this.A_band_penalty_factor);
2751d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_max_boost=").append(this.A_band_max_boost);
2752d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" lazy_roam_hysteresis=").append(this.lazy_roam_hysteresis);
2753d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" alert_roam_rssi_trigger=").append(this.alert_roam_rssi_trigger);
2754d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            return sbuf.toString();
2755d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2756d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2757d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
27582a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
275918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLazyRoam(boolean enabled, WifiLazyRoamParams params) {
27602a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        return false;
2761d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
27629ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
27639ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
27649ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
27659ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
276618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
27679ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
27689ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
27699ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
27709ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
27719ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
27729ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2773956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
277471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2775f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2776f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
2777f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
27789ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
27799ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
27809ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
27819ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
27829ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
27832a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
278418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setSsidWhitelist(String list[]) {
27852a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        return false;
27865caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle    }
2787c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2788c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
2789c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
2790c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
279118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2792c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
2793c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
27943bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
27953bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
27963bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
27973bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
27983bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
27993bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
28003bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
2801956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2802c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2803c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
280418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
2805c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2806c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2807c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2808c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2809c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2810c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2811c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
2812c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
281318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2814c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
2815c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
2816956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2817c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2818c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
2819c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2820c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2821c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2822c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2823c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2824aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2825aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
2826aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
2827aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2828aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2829aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
2830aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
283118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
283218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
283318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
283418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
283518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
283618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2837aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2838aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2839aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
2840aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
2841aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2842aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
2843aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
284418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
2845aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
2846aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
2847956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
284818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
2849aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2850ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2851ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
2852ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2853ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
2854aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
2855aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
2856ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
2857ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
2858ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
2859ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
2860ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2861ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2862aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2863aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2864aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2865aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2866aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2867aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2868aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
2869aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
287018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
2871aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
2872956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2873aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2874ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
2875ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2876ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
2877ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2878ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
2879ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2880aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2881aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2882aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2883aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2884aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
28855ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
28865ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
2887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
2888