WifiNative.java revision cc180872c51908b15ce5cbf834634ff323e036bc
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;
202a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.Context;
21e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensenimport android.net.apf.ApfCapabilities;
22143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpandeimport android.net.wifi.RttManager;
2368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.net.wifi.RttManager.ResponderConfig;
24e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
25dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiConfiguration;
2618786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.net.wifi.WifiEnterpriseConfig;
27aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
2803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.WifiManager;
29e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
30dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiSsid;
312a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.net.wifi.WifiWakeReasonAndCounts;
32155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
33155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
3503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
36f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
3718786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.os.SystemProperties;
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
39155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
40155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
41fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
4209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawalimport com.android.internal.annotations.Immutable;
43e3831b70d4a8a967fe8df5496d542a432692c434Roshan Piusimport com.android.internal.annotations.VisibleForTesting;
440fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawalimport com.android.internal.util.HexDump;
45c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadhamimport com.android.server.connectivity.KeepalivePacketData;
462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.NetworkDetail;
472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.SupplicantBridge;
482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.Utils;
49590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tanimport com.android.server.wifi.util.FrameParser;
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
570fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawalimport java.io.PrintWriter;
580fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawalimport java.io.StringWriter;
59fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.io.UnsupportedEncodingException;
60fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLDecoder;
61fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLEncoder;
625cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.ByteBuffer;
635cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.CharBuffer;
645cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharacterCodingException;
655cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharsetDecoder;
665cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.StandardCharsets;
67eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawalimport java.text.SimpleDateFormat;
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
693571366ac36c70746b9f013ec2b54482861c9292Randy Panimport java.util.BitSet;
70eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawalimport java.util.Date;
71fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.HashMap;
72fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Iterator;
73155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
74155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
75fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Map;
769ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport java.util.Set;
77eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawalimport java.util.TimeZone;
7818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
79fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
90b66b29a00da970ee75052e24f592c8d6c16bd0edxinhe    private static boolean DBG = false;
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
920fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    // Must match wifi_hal.h
930fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public static final int WIFI_SUCCESS = 0;
940fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
9518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
9618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Hold this lock before calling supplicant or HAL methods
9718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * it is required to mutually exclude access to the driver
9818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
99956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static final Object sLock = new Object();
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10115941215e85f924765f00779e9b5daff9ed6f118Randy Pan    private static final LocalLog sLocalLog = new LocalLog(8192);
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static LocalLog getLocalLog() {
104956f54b391677d78379729dd14518edddf3c7660Etan Cohen        return sLocalLog;
10518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
11618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
11718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Singleton WifiNative instances
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
11918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative wlanNativeInterface =
120c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            new WifiNative(SystemProperties.get("wifi.interface", "wlan0"), true);
12118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getWlanNativeInterface() {
12218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return wlanNativeInterface;
12318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
125c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang    private static WifiNative p2pNativeInterface =
126c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            // commands for p2p0 interface don't need prefix
127c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang            new WifiNative(SystemProperties.get("wifi.direct.interface", "p2p0"), false);
12818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getP2pNativeInterface() {
12918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return p2pNativeInterface;
13018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mTAG;
13418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfaceName;
13518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfacePrefix;
13618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
13718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private Context mContext = null;
13818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void initContext(Context context) {
13918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (mContext == null && context != null) {
14018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mContext = context;
14118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
14218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
144c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang    private WifiNative(String interfaceName,
145c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang                       boolean requiresPrefix) {
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
148e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
149c6ccad1ec19b0a53266962237774422b156ea726Ningyuan Wang        if (requiresPrefix) {
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName() {
15718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return mInterfaceName;
158e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
159e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
16018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Note this affects logging on for all interfaces
161ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
162ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
163ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
164ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
165ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
166ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
167ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
168ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
16918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private void localLog(String s) {
170956f54b391677d78379729dd14518edddf3c7660Etan Cohen        if (sLocalLog != null) sLocalLog.log(mInterfaceName + ": " + s);
17118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
17518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
17618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Driver and Supplicant management
17718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
17818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean loadDriverNative();
17918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean loadDriver() {
180956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return loadDriverNative();
18218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
18518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean isDriverLoadedNative();
18618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isDriverLoaded() {
187956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return isDriverLoadedNative();
18918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
19218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean unloadDriverNative();
19318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean unloadDriver() {
194956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return unloadDriverNative();
19618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
19718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
19818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
19918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean startSupplicantNative(boolean p2pSupported);
20018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startSupplicant(boolean p2pSupported) {
201956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
20218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return startSupplicantNative(p2pSupported);
20318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
20418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
20518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
20618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* Sends a kill signal to supplicant. To be used when we have lost connection
20718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills       or when the supplicant is hung */
20818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean killSupplicantNative(boolean p2pSupported);
20918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean killSupplicant(boolean p2pSupported) {
210956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
21118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return killSupplicantNative(p2pSupported);
21218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
21518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean connectToSupplicantNative();
216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
217956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
218b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "connectToSupplicant");
219b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            return connectToSupplicantNative();
220b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
22318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static void closeSupplicantConnectionNative();
224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
225956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
226b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "closeSupplicantConnection");
227b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            closeSupplicantConnectionNative();
228b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
23118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
23218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wait for the supplicant to send an event, returning the event string.
23318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * @return the event string sent by the supplicant.
23418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
23518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static String waitForEventNative();
236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
24118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
24318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Supplicant Command Primitives
24418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
24518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native boolean doBooleanCommandNative(String command);
24618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native int doIntCommandNative(String command);
24818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native String doStringCommandNative(String command);
25018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
253956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
25418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
2567b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
2570888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
262a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    private boolean doBooleanCommandWithoutLogging(String command) {
263a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (DBG) Log.d(mTAG, "doBooleanCommandWithoutLogging: " + command);
264956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
265a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
266a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            if (DBG) Log.d(mTAG, command + ": returned " + result);
267a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return result;
268a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
269a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    }
270a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
273956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
27418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
2767b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
2830888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2840888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2850888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2860888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2870888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
2880888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
289956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
29018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
29240ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
29340ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
29440ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
2957b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (!command.startsWith("STATUS-")) {
2967b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog(toLog + " -> " + result);
2977b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
29840ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
29940ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
3050888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
3060888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
3070888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
3080888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
3090888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
31027355a942653264388e909a4276196ee63e57811vandwalle        }
311956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
31618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String doCustomSupplicantCommand(String command) {
31718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doStringCommand(command);
31818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
31918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
32018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
32118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wrappers for supplicant commands
32218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
328ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
329ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
330ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
331ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
332a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
333a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
334a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
335a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
3363d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    /**
337ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * Start a scan using wpa_supplicant for the given frequencies.
3383d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param freqs list of frequencies to scan for, if null scan all supported channels.
3393d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius     * @param hiddenNetworkIds List of hidden networks to be scanned for.
340ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     */
3413d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    public boolean scan(Set<Integer> freqs, Set<Integer> hiddenNetworkIds) {
3423d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String freqList = null;
3433d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        String hiddenNetworkIdList = null;
3443d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqs != null && freqs.size() != 0) {
3457e1082bec4d42fdb4fc92061c7041826218eff1cNingyuan Wang            freqList = TextUtils.join(",", freqs);
3463d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3473d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIds != null && hiddenNetworkIds.size() != 0) {
3487e1082bec4d42fdb4fc92061c7041826218eff1cNingyuan Wang            hiddenNetworkIdList = TextUtils.join(",", hiddenNetworkIds);
3499ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3503d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return scanWithParams(freqList, hiddenNetworkIdList);
3519ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
3529ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
3533d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius    private boolean scanWithParams(String freqList, String hiddenNetworkIdList) {
3543d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        StringBuilder scanCommand = new StringBuilder();
3553d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        scanCommand.append("SCAN TYPE=ONLY");
3563d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (freqList != null) {
3573d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" freq=" + freqList);
3583d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        }
3593d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        if (hiddenNetworkIdList != null) {
3603d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius            scanCommand.append(" scan_id=" + hiddenNetworkIdList);
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3623d995b8a3a508e957e3dac2e508956020cd9abfeRoshan Pius        return doBooleanCommand(scanCommand.toString());
363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
379e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
380e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
381e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
382e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
387fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public boolean setNetworkExtra(int netId, String name, Map<String, String> values) {
388e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        String encoded = createNetworkExtra(values);
389e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        if (encoded == null) {
390e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius            return false;
391e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        }
392e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        return setNetworkVariable(netId, name, "\"" + encoded + "\"");
393e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    }
394e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius
395e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    @VisibleForTesting
396e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    public static String createNetworkExtra(Map<String, String> values) {
397fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String encoded;
398fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
399fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            encoded = URLEncoder.encode(new JSONObject(values).toString(), "UTF-8");
400fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (NullPointerException e) {
401fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
402e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius            return null;
403fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
404fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
405e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius            return null;
406fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
407e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        return encoded;
408fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
409fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
412a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (name.equals(WifiConfiguration.pskVarName)
413a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham                || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
414a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value);
415a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        } else {
416a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
417a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
420fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public Map<String, String> getNetworkExtra(int netId, String name) {
421e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        final String extraString = getNetworkVariable(netId, name);
422e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        return parseNetworkExtra(extraString);
423e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    }
424e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius
425e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    public static Map<String, String> parseNetworkExtra(String extraSting) {
426e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        if (extraSting == null || !extraSting.startsWith("\"") || !extraSting.endsWith("\"")) {
427fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
428fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
429fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
430e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius            final String encoded = extraSting.substring(1, extraSting.length() - 1);
431fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This method reads a JSON dictionary that was written by setNetworkExtra(). However,
432fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // on devices that upgraded from Marshmallow, it may encounter a legacy value instead -
433fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // an FQDN stored as a plain string. If such a value is encountered, the JSONObject
434fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // constructor will thrown a JSONException and the method will return null.
435fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final JSONObject json = new JSONObject(URLDecoder.decode(encoded, "UTF-8"));
436fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Map<String, String> values = new HashMap<String, String>();
437fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Iterator<?> it = json.keys();
438fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            while (it.hasNext()) {
439fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final String key = (String) it.next();
440fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final Object value = json.get(key);
441fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                if (value instanceof String) {
442fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                    values.put(key, (String) value);
443fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                }
444fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            }
445fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return values;
446fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
44764697f7a5b9c1c39f8c9d9c225b8ca4c798422c2Samuel Tan            Log.e(TAG, "Unable to deserialize networkExtra: " + e.toString());
448fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
449fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (JSONException e) {
450fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This is not necessarily an error. This exception will also occur if we encounter a
451fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // legacy FQDN stored as a plain string. We want to return null in this case as no JSON
452fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // dictionary of extras was found.
453fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
454fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
455fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
456fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
468f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
470f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
471f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
472ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
473ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
474ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
475ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
476ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
477ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
4809d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius
4819d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
4829d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Enables a network in wpa_supplicant.
4839d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be enabled.
4849d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
4859d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
4869d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    public boolean enableNetwork(int netId) {
4879d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId));
4889d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius        return doBooleanCommand("ENABLE_NETWORK " + netId);
489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4919d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
492163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * Enable a network in wpa_supplicant, do not connect.
493163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * @param netId - Network ID of the network to be enabled.
494163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     * @return true if command succeeded, false otherwise.
495163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt     */
496163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    public boolean enableNetworkWithoutConnect(int netId) {
497163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt        if (DBG) logDbg("enableNetworkWithoutConnect nid=" + Integer.toString(netId));
498163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt        return doBooleanCommand("ENABLE_NETWORK " + netId + " " + "no-connect");
499163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    }
500163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt
501163d510aac068682c390664e51e83bbf870fe5f5Sunil Dutt    /**
5029d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Disables a network in wpa_supplicant.
5039d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be disabled.
5049d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
5059d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
507f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5119d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius    /**
5129d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * Select a network in wpa_supplicant (Disables all others).
5139d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @param netId - Network ID of the network to be selected.
5149d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     * @return true if command succeeded, false otherwise.
5159d7489491984e86915b2cf4fac38a882de1c8cdbRoshan Pius     */
5160047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
5170047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
5180047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
5190047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
5200047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
522f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
527f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
53221bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
53799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
54099d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
54199d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
54299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
54399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
54499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
54599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
54699d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
54799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
558a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
559a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
5642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
5662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
5672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
5702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
5712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
5722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
5732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
5742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
5752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
5762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
582eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * MASK=<N> BSS command information mask.
58377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
584eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * The mask used in this method, 0x29d87, gets the following fields:
58577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
586eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_ID         (Bit 0)
587eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_BSSID      (Bit 1)
588eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_FREQ       (Bit 2)
589eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_LEVEL      (Bit 7)
590eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_TSF        (Bit 8)
591eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_IE         (Bit 10)
592eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_FLAGS      (Bit 11)
593eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_SSID       (Bit 12)
594eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_INTERNETW  (Bit 15) (adds ANQP info)
595eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *     WPA_BSS_MASK_DELIM      (Bit 17)
596eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     *
597eb5731beb47704699dd8866b1a1dc59f3764c981Samuel Tan     * See wpa_supplicant/src/common/wpa_ctrl.h for details.
598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
5992afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
6002afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
6012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
6022afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
6042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
6052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
6062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
6072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
6082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
6132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
6202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
6222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
6232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
6282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
6342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
6395d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
6402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
6412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6422afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
6432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
6442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
6462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
6482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
6502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
6512afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
6522afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6532afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
6542afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6552afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
6562afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6572afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
6582afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6592afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
6602afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
6612afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
6622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
6632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
6642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
6662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
6682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
6702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
6722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
6742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
6752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
6762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
6772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
6782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
6795d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
6802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
6812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
6822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
6832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
6852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
6862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
6872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
6885d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
6895d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
6905d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6915d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
6925d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
6935d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
6945d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6955d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6965d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
6975d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
6985d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
6995d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
7005d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
7015d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
7022afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
7032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
7042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
7052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
7065d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
7072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
7092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
710af2b79e4cfc1da793b3d8fb4a96c144deefc7d58Joe Onorato                                if (DBG) Log.d(TAG, "HSNwk: '" + networkDetail);
7112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
7129ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
71363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills                                    level, freq, tsf, infoElements, anqpLines);
7149ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
7152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
7162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
7172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
7182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
7192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
7202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
7212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
7222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
7232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
7242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
7255d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
7262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
7272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
7282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
7292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
7302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
73177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
73277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
734446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
735446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
736446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
737446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
738446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
739446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
740446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
741446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
742446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
743446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
744446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
745446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
746446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
747446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
748446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
74956d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
750446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
751446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
752446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
753446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
754446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
755446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
756446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
757446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
758446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
759446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
760446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
761446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
762446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
763446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
764446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
765446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
83503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
83603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
83703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
83803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
83903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
84003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
84103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
84203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
84403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
84503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
84603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
84703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
84803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
84903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
85003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
85103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
85203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
85518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
85618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
85718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
8587ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
8597ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
8607ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
8617ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
8627ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
8637ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
8647ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
8657ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
875cc180872c51908b15ce5cbf834634ff323e036bcChristopher Wiley     * @param setCoexScanMode whether to enable or disable this mode
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8860a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
8870a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
8880a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
8890a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
9120465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
9130465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
9140465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
9150465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9189153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    /**
9199153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * Start/Stop PNO scan.
9209153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @param enable boolean indicating whether PNO is being enabled or disabled.
9219153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     */
9229153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    public boolean setPnoScan(boolean enable) {
9239153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        String cmd = enable ? "SET pno 1" : "SET pno 0";
9249153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius        return doBooleanCommand(cmd);
9259153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    }
926ac9ad3283508db15b65b1cbb89b841278973276bRoshan Pius
927f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
928f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
929f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
930f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
931f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
932f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
933f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
934f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
93956845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
94056845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
94156845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
94256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
94356845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
94456845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
94556845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
94656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
949956f54b391677d78379729dd14518edddf3c7660Etan Cohen            synchronized (sLock) {
95018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
95118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
95218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
957155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
958155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
959155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
963155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
964155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
973155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
974155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
975155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
976155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
977155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
979155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
989956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1005956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1020956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
102933b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
103018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
103118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
103218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
103333b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
103433b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1035f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1036f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
103718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
103833b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
103933b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
104026eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean simAuthFailedResponse(int id) {
104126eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type GSM-AUTH
104226eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL");
104326eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
104426eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
104526eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean umtsAuthFailedResponse(int id) {
104626eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type UMTS-AUTH
104726eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL");
104826eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
104926eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
1050ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
105118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1052ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1053ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1102956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1116956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1178956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
117918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
118018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
118118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
118218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
118318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
118718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
118818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
118918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
119018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
119118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
119218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
119318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
119418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
120318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1292956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
132536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
132636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
132727f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
132827f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
132936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
133036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
133136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
1332956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
133327f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
133427f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
133527f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
133627f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
133736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
133836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
133936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
134036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
134136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
134236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
134336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
134436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
134536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
134836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
134936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
135036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1417956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
141818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
141918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
142018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
142118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
142218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
142318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1434956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
143518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
143618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
143818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
143918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
144018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
144118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
144218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
144318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
144418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
144518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
144618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
144718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
144818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
144918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
145018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
145118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
145218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
14833f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1484446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1485446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1486446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1487446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1488f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1489f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1490f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1491f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1492f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1493f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1494f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1495f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1496f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1497f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1498f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1499f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1500f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1501f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1502f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1503f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1504f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1505f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1506f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1507f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1508f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1509f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1510f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
15117e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15127e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    /* kernel logging support */
15137e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    private static native byte[] readKernelLogNative();
15147e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15157e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    synchronized public String readKernelLog() {
15167e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        byte[] bytes = readKernelLogNative();
15177e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        if (bytes != null) {
15187e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
15197e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            try {
15207e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
15217e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return decoded.toString();
15227e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            } catch (CharacterCodingException cce) {
15237e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return new String(bytes, StandardCharsets.ISO_8859_1);
15247e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            }
15257e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        } else {
15267e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            return "*** failed to read kernel log ***";
15277e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        }
15287e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    }
15297e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15307f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
15317f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
153218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
153318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
153418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
153518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
153618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
153718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1538b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1539f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1540f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1541956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static int sWlan0Index = -1;
1542f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1543f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
15447f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1545b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1546b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1547b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
15487f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1549b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
1550cc180872c51908b15ce5cbf834634ff323e036bcChristopher Wiley        @Override
15517ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1552b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
15537ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
15547ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
15557ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
15567ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
155718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1558d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1559d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1560d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1561d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1562d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1563d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1564956f54b391677d78379729dd14518edddf3c7660Etan Cohen        sLocalLog.log(debugLog);
1565d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1566956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1567918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang            if (startHalNative()) {
1568918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                int wlan0Index = queryInterfaceIndex(mInterfaceName);
1569918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                if (wlan0Index == -1) {
1570918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    if (DBG) sLocalLog.log("Could not find interface with name: " + mInterfaceName);
1571918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    return false;
1572918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                }
1573918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                sWlan0Index = wlan0Index;
1574f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1575f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1576aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1577aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1578956f54b391677d78379729dd14518edddf3c7660Etan Cohen                if (DBG) sLocalLog.log("Could not start hal");
1579f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1580aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1581aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
15827ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
15837ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
15847ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
158518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1586956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
158771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1588f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1589f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1590f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1591f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1592f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1593f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1594f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1595f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1596f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1597f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1598f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1599f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1600f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
16017ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16027f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
160318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
160471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
160571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1606b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
16077f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1608918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang    public int queryInterfaceIndex(String interfaceName) {
1609956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
161071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1611918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                int num = getInterfacesNative();
1612918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                for (int i = 0; i < num; i++) {
1613918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    String name = getInterfaceNameNative(i);
1614918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                    if (name.equals(interfaceName)) {
1615918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang                        return i;
161602a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1617aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
1618e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1619e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1620918b4bdc2ae16438c5213320daeff1d551f4aed8Ningyuan Wang        return -1;
16217f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
16227f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1623b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
162418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
1625956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
162618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
162718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
16287f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
16297f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1630062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan    // TODO: Change variable names to camel style.
1631e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1632297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1633e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1634e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1635e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1636297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
16377d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1638e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1639d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_bssid_history_entries;
1640d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_epno_networks;
1641d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_epno_networks_by_ssid;
1642d34e3a7b98d51f684100483151fceb233fd95215Roshan Pius        public int  max_number_of_white_listed_ssid;
1643e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1644e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
164518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1646956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1647af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1648af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1649e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1650e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1651b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1652b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1653e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1654b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1655b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
165683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1657b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1658d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
16597f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1660e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1661712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int frequency;
1662712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int dwell_time_ms;
1663712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public boolean passive;
16647f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
16657f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1666e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1667712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int bucket;
1668712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int band;
1669712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int period_ms;
1670712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_period_ms;
1671712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int step_count;
1672712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_events;
1673712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_channels;
1674712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public ChannelSettings[] channels;
1675e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
16767f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1677e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1678712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int base_period_ms;
1679712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int max_ap_per_scan;
1680712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_percent;
1681712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int report_threshold_num_scans;
1682712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public int num_buckets;
1683d74ff28cdb01d3f711e0b0f0e99e51fdb221eaf2Roshan Pius        /* Not part of gscan HAL API. Used only for wpa_supplicant scanning */
1684d74ff28cdb01d3f711e0b0f0e99e51fdb221eaf2Roshan Pius        public int[] hiddenNetworkIds;
1685712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public BucketSettings[] buckets;
1686e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
16877f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
168868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
16899bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Network parameters to start PNO scan.
16909bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
16919bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public static class PnoNetwork {
16929bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public String ssid;
16939bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int networkId;
16949bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int priority;
16959bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public byte flags;
1696ef3ea1092bc17673c0a85a845b053151b7c10e07Roshan Pius        public byte auth_bit_field;
16979bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
16989bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
16999bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
17009bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Parameters to start PNO scan. This holds the list of networks which are going to used for
17019bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * PNO scan.
17029bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
17039bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public static class PnoSettings {
17049bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int min5GHzRssi;
17059bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int min24GHzRssi;
17069bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int initialScoreMax;
17079bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int currentConnectionBonus;
17089bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int sameNetworkBonus;
17099bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int secureBonus;
17109bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public int band5GHzBonus;
1711dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        public boolean isConnected;
17129bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        public PnoNetwork[] networkList;
17139bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
17149bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
17159bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
171668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Wi-Fi channel information.
171768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
171868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public static class WifiChannelInfo {
171968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mPrimaryFrequency;
172068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency0;
172168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency1;
172268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mChannelWidth;
172368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        // TODO: add preamble once available in HAL.
172468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
172568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
1726b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
172763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
172863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called for each AP as it is found with the entire contents of the beacon/probe response.
172963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Only called when WifiScanner.REPORT_EVENT_FULL_SCAN_RESULT is specified.
173063539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1731c9e6069eb941d282af213dc20b171877db6b567bMitchell Wills        void onFullScanResult(ScanResult fullScanResult, int bucketsScanned);
173263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
173363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Callback on an event during a gscan scan.
173463539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * See WifiNative.WIFI_SCAN_* for possible values.
173563539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
173663539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        void onScanStatus(int event);
173763539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
173863539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is paused.
173963539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
174083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
174163539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        /**
174263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         * Called with the current cached scan results when gscan is resumed.
174363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills         */
1744b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1745e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1746e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
17479bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
17489bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Handler to notify the occurrence of various events during PNO scan.
17499bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
17509bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public interface PnoEventHandler {
17519bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        /**
17529bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         * Callback to notify when one of the shortlisted networks is found during PNO scan.
17539bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         * @param results List of Scan results received.
17549bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius         */
17559bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        void onPnoNetworkFound(ScanResult[] results);
1756063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius
1757063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius        /**
1758063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius         * Callback to notify when the PNO scan schedule fails.
1759063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius         */
1760063cfc7e3eef78fcbda24a66f0c473828b39c854Roshan Pius        void onPnoScanFailed();
17619bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    }
17629bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius
1763b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
176471af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_RESULTS_AVAILABLE = 0;
176571af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1;
176671af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_PERCENT = 2;
176771af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_FAILED = 3;
1768b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
176918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
17702a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onScanStatus(int id, int event) {
177118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
177263539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills        if (handler != null) {
177363539f1283899fbbf83ab90757961b4be51d5034Mitchell Wills            handler.onScanStatus(event);
17742a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        }
1775e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1776e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
177718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
17785cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
17795cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17805cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
17815cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
17825cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
17835cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17845cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
17855cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17865cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
17875cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
17885cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17895cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
17905cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
17915cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17925cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
17935cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
17945cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
17955cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
17965cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
17975cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
17985cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
17995cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18005cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
18015cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
18025cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18035cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18045cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
18055cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18065cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
180718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
18085cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
18095cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
18105cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
18115cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18125cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18135cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
18145cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
18155cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
18165cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18175cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18181ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void populateScanResult(ScanResult result, int beaconCap, String dbg) {
1819dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
18205d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18215d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
18225d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
18235d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
18245d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
18255d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
18265d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18275d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
18281ab129e587d334a144a0bca5323c27985397a403Randy Pan                InformationElementUtil.parseInformationElements(result.bytes);
18295d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
18305d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
18315d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
18325d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
18335d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
18345d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
18355d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
1836243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1837243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1838d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
18395d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
1840d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
1841d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
1842d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
1843d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
1844d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
1845243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
18465d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
18475d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
18485d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
18495d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
1850243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
18515d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
18525d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
1853243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
1854243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
18553571366ac36c70746b9f013ec2b54482861c9292Randy Pan
18563571366ac36c70746b9f013ec2b54482861c9292Randy Pan        // build capabilities string
18573571366ac36c70746b9f013ec2b54482861c9292Randy Pan        BitSet beaconCapBits = new BitSet(16);
18583571366ac36c70746b9f013ec2b54482861c9292Randy Pan        for (int i = 0; i < 16; i++) {
18593571366ac36c70746b9f013ec2b54482861c9292Randy Pan            if ((beaconCap & (1 << i)) != 0) {
18603571366ac36c70746b9f013ec2b54482861c9292Randy Pan                beaconCapBits.set(i);
18613571366ac36c70746b9f013ec2b54482861c9292Randy Pan            }
18623571366ac36c70746b9f013ec2b54482861c9292Randy Pan        }
18633571366ac36c70746b9f013ec2b54482861c9292Randy Pan        result.capabilities = InformationElementUtil.Capabilities.buildCapabilities(elements,
18643571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                               beaconCapBits);
18653571366ac36c70746b9f013ec2b54482861c9292Randy Pan
1866243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
18673571366ac36c70746b9f013ec2b54482861c9292Randy Pan            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth
18683571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " PrimaryFreq: " + result.frequency + " mCenterfreq0: " + result.centerFreq0
18693571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder
18703571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    ? "Support RTT reponder: " : "Do not support RTT responder")
18713571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " Capabilities: " + result.capabilities);
1872f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1873f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1874476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1875dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1876dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
187718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
18781ab129e587d334a144a0bca5323c27985397a403Randy Pan    private static void onFullScanResult(int id, ScanResult result,
18793571366ac36c70746b9f013ec2b54482861c9292Randy Pan            int bucketsScanned, int beaconCap) {
18801ab129e587d334a144a0bca5323c27985397a403Randy Pan        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID);
1881dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
188218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
188318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
18841ab129e587d334a144a0bca5323c27985397a403Randy Pan            populateScanResult(result, beaconCap, " onFullScanResult ");
1885c9e6069eb941d282af213dc20b171877db6b567bMitchell Wills            handler.onFullScanResult(result, bucketsScanned);
1886dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
18877f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18887f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1889b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1890b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1891b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
18927f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
189318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
1894956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
189571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1896f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
1897f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
1898f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
1899b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1900f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
19017f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1902f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
1903e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1904f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
1905f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
1906b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1907f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1908f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
1909f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
1910f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1911f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1912f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1913f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1914f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1915f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1916e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1917e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1918e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
19197f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19207f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
192118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
1922956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
192371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1924ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
1925ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
1926ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
1927f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
1928f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
1929f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
1930f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1931b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1932b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1933b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
193418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
1935956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
193671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1937f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1938f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
1939f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1940f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
1941f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1942f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
1943f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1944b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1945b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1946b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1947b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
194818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
1949956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
195071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1951f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
19526f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
1953f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
1954f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
1955f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
1956f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
1957f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
195883a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
1959f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
1960f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
1961f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
196283a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
1963b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1964e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1965e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1966e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
196718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
1968956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19693ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            WifiScanner.ScanData[] sd = null;
197071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
19713ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                sd = getScanResultsNative(sWlan0Index, flush);
19723ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            }
19733ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande
19743ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            if (sd != null) {
19753ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return sd;
1976f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
19773ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return new WifiScanner.ScanData[0];
1978f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1979aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1980e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1981e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1982b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
1983d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
1984d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
1985e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1986e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1987b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
1988b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
1989e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1990b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
1991e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
1992b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
1993e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
199418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
199518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
1996956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
199771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1998f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
1999f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2000f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2001f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
2002f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2003f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2004f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
2005f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
2006f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2007f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2008f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2009e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2010f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2011f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2012e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2013e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2014e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2015e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2016e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
201718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
2018956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
201971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2020f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2021f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
2022f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
2023f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2024f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2025e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
20267f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
20277f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
2028e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
202918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
203018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
203118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
203218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
203318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
203418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
203518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
203618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
2037d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
2038d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
2039d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
204018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
204118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
204218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
204318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
204418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
204518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
204618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
204718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
2048aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2049e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2050e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2051b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
2052e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
2053e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2054e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2055b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
2056b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
2057e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2058b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
2059e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
2060b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
2061e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
206218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
2063b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
2064956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
206571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2066f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2067f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2068f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2069f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
2070f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2071f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2072f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
2073851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
2074851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
2075f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2076f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2077f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2078e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2079f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2080f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2081e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2082e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2083e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2084e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2085e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2086e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
208718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
2088956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
208971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2090f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2091f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
2092f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
2093f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2094f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2095e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2096e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2097e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2098e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
209918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
210018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
210118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
210218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
210318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
210418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
2105f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
210618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
2107aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2108e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2109e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
211018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
2111200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
2112200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
2113956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
211471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2115aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
2116f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2117f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2118f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2119aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
21205c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
21215c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
212218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2123d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2124956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
212571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2126d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2127d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2128d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2129d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2130d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2131c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
213218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2133956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
213471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2135f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2136f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2137f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2138f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2139f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2140f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2141a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2142143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2143143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2144143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2145143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2146143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2147143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2148143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2149143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2150143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
215118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
215218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
215318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
215418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
215502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
215618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2157143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2158143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
215918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
216018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2161143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2162143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2163143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2164143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2165143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2166143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2167143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2168143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
216918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2170143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2171956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
217271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2173f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2174f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2175f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2176f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2177f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2178f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2179f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2180f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2181f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2182143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2183f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2184143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2185143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2186143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2187143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
218818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
2189956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
219071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2191f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2192f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2193f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2194143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2195f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2196f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2197f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2198f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
21995cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2200f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2201f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
22025cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2203f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2204f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2205143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2206143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2207143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2208143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2209143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2210042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
221168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static int sRttResponderCmdId = 0;
221268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
221368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native ResponderConfig enableRttResponderNative(int iface, int commandId,
221468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int timeoutSeconds, WifiChannelInfo channelHint);
221568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
221668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Enable RTT responder role on the device. Returns {@link ResponderConfig} if the responder
221768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * role is successfully enabled, {@code null} otherwise.
221868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
221968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    @Nullable
222068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public ResponderConfig enableRttResponder(int timeoutSeconds) {
222168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
222268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return null;
222368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId != 0) {
222468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                if (DBG) Log.e(mTAG, "responder mode already enabled - this shouldn't happen");
222568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return null;
222668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
222768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int id = getNewCmdIdLocked();
222868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            ResponderConfig config = enableRttResponderNative(
222968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                    sWlan0Index, id, timeoutSeconds, null);
223068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (config != null) sRttResponderCmdId = id;
223168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (DBG) Log.d(TAG, "enabling rtt " + (config != null));
223268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return config;
223368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
223468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
223568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
223668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native boolean disableRttResponderNative(int iface, int commandId);
223768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
223868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Disable RTT responder role. Returns {@code true} if responder role is successfully disabled,
223968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * {@code false} otherwise.
224068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
224168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public boolean disableRttResponder() {
224268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
224368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return false;
224468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId == 0) {
224568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                Log.e(mTAG, "responder role not enabled yet");
224668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return true;
224768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
224868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            sRttResponderCmdId = 0;
224968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return disableRttResponderNative(sWlan0Index, sRttResponderCmdId);
225068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
225168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
225268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
2253042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2254042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
225518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2256956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
225771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2258042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2259042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2260042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2261042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2262042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2263042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2264efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2265efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2266efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2267efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
226818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2269956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2270ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2271ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
227218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2273ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2274ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2275efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2276efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
22770465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2278ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
227918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2280956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2281ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2282ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
228318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2284ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2285ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2286ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2287ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
22880465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
22890465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
229018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
2291956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
229271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
22930465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
22940465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
22950465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
22960465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
22970465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
22980465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2299b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
23003b51fd1bb8356b284822f4f677ad941524e616ebNingyuan Wang    private static native boolean setInterfaceUpNative(boolean up);
230164e816ff5483030636e1815f69a6cbaf8cef289eNingyuan Wang    public boolean setInterfaceUp(boolean up) {
2302956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
230371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
23043b51fd1bb8356b284822f4f677ad941524e616ebNingyuan Wang                return setInterfaceUpNative(up);
2305b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2306b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2307b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2308b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2309b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
231012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
231112cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
231218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
2313956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
231471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
231512cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
231618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
231712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
231812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
231912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
232012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2321939177ff615062ec826601d536466875d8457375xinhe
2322e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen    private static native ApfCapabilities getApfCapabilitiesNative(int iface);
2323e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen    public ApfCapabilities getApfCapabilities() {
23246609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
23256609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
2326e1dab7a2e3ab5911f812a302b4beed1f6eb5aba7Paul Jensen                return getApfCapabilitiesNative(sWlan0Index);
23276609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
23286609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return null;
23296609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
23306609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
23316609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
23326609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
23336609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    private static native boolean installPacketFilterNative(int iface, byte[] filter);
23346609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    public boolean installPacketFilter(byte[] filter) {
23356609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        synchronized (sLock) {
23366609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            if (isHalStarted()) {
23376609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return installPacketFilterNative(sWlan0Index, filter);
23386609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            } else {
23396609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen                return false;
23406609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen            }
23416609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen        }
23426609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen    }
23436609df5a9c14e4440c085567a27437a8cfc50f88Paul Jensen
2344939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
234518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2346956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
234771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2348939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2349939177ff615062ec826601d536466875d8457375xinhe            } else {
2350939177ff615062ec826601d536466875d8457375xinhe                return false;
2351939177ff615062ec826601d536466875d8457375xinhe            }
2352939177ff615062ec826601d536466875d8457375xinhe        }
2353939177ff615062ec826601d536466875d8457375xinhe    }
2354939177ff615062ec826601d536466875d8457375xinhe
2355d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2356d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2357d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2358d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2359d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2360d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2361d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2362d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2363d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
236418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2365956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2366f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2367f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2368d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2369d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2370d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2371d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2372d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2373d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2374d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2375d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2376d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2377d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2378d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2379d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
238018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2381956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
238271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2383d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2384d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2385d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2386d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2387d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2388d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2389d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2390d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2391d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2392d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2393d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2394d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2395d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2396d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2397d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2398d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2399d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2400d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2401d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2402d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
240318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2404956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
240571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2406d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2407d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2408d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2409d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2410d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2411d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2412d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
241318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
241418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
241518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
241618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
241718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
241818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
241918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
242018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2421d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2422d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2423a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2424a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2425a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
24267d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2427a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
24280bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
24290bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2430a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2431a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2432a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2433a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
243418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24350bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
243618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
243718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
243818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
243903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
244003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
244118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24420bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
244318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
244418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
244518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
244603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
244703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2448b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2449b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
245018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
2451956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2452b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2453b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2454b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2455b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2456b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2457b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2458b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2459b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2460b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2461b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2462b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2463b797893fc1966803d0c013faac42e6396a37a384xinhe            }
246403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
246503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
246603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
246703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
24680bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
246918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
247003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
2471956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
247271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
247303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
247403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
247503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
247603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
247703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
247803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
247903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
248003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
248103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
248218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
2483956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
248471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
248503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
248603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2487f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
248803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
248903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
249003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
249103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2492b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
249318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2494956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2495b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2496b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2497b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2498b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2499b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2500b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2501b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2502b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2503b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2504b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2505b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2506b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2507b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2508b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2509b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2510b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2511b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2512b797893fc1966803d0c013faac42e6396a37a384xinhe
251303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
251418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
2515956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
251671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
251703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
251803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2519f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
252003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
252103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
252203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
252303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
252403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
252503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
252618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
2527956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
252871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
252903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
253003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2531f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
253203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
253303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
253403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
253503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
25360bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
25370bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
25380bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
25390bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
25400bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
25410bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
25420bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
25430bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
25440bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
25450bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25460bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
25470bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
25480bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
25490bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
25500bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
25510bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
25520bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
25530bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
25540bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25550bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
255618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
2557956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
255871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
255903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
256003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
256103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
256203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
256303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
256403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
256503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
256603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
256718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
2568956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
256971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
257003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
257103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
257203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
257303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
257403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
257503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
257603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
257718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
257818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
257903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
258098dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
258198dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
258298dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
258398dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
258498dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
258503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2586127f7244183786e6ccae09e81eeccdac31973e69xinhe
258703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
258818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
2589956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
259071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
25910bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
25920bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
259303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
25940bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
25950bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
25960bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
259703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
259803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2599f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2600a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2601a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2602dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2603d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal    private static native byte[] getDriverStateDumpNative(int iface);
2604d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal    /** Fetch the driver state, for driver debugging. */
2605d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal    public byte[] getDriverStateDump() {
2606d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal        synchronized (sLock) {
2607d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal            if (isHalStarted()) {
2608d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal                return getDriverStateDumpNative(sWlan0Index);
2609d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal            } else {
2610d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal                return null;
2611d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal            }
2612d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal        }
2613d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal    }
2614d0cb2fae195ebb9f658095667f3c7b7b8d69a204mukesh agrawal
2615dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
261609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /* Packet fate API */
261709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
261809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
261909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    abstract static class FateReport {
2620eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        final static int USEC_PER_MSEC = 1000;
2621eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        // The driver timestamp is a 32-bit counter, in microseconds. This field holds the
2622eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        // maximal value of a driver timestamp in milliseconds.
2623eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        final static int MAX_DRIVER_TIMESTAMP_MSEC = (int) (0xffffffffL / 1000);
2624eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        final static SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss.SSS");
2625eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal
262609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte mFate;
262709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final long mDriverTimestampUSec;
262809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte mFrameType;
262909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        final byte[] mFrameBytes;
2630eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        final long mEstimatedWallclockMSec;
263109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
263209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        FateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
263309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFate = fate;
263409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mDriverTimestampUSec = driverTimestampUSec;
2635eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            mEstimatedWallclockMSec =
2636eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                    convertDriverTimestampUSecToWallclockMSec(mDriverTimestampUSec);
263709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFrameType = frameType;
263809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            mFrameBytes = frameBytes;
263909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
26400fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
2641590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan        public String toTableRowString() {
2642590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            StringWriter sw = new StringWriter();
2643590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            PrintWriter pw = new PrintWriter(sw);
2644590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            FrameParser parser = new FrameParser(mFrameType, mFrameBytes);
2645eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            dateFormatter.setTimeZone(TimeZone.getDefault());
2646eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            pw.format("%-15s  %12s  %-9s  %-32s  %-12s  %-23s  %s\n",
2647eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                    mDriverTimestampUSec,
2648eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                    dateFormatter.format(new Date(mEstimatedWallclockMSec)),
2649eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                    directionToString(), fateToString(), parser.mMostSpecificProtocolString,
2650eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                    parser.mTypeString, parser.mResultString);
2651590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            return sw.toString();
2652590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan        }
2653590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan
2654590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan        public String toVerboseStringWithPiiAllowed() {
26550fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            StringWriter sw = new StringWriter();
26560fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            PrintWriter pw = new PrintWriter(sw);
2657590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            FrameParser parser = new FrameParser(mFrameType, mFrameBytes);
26580fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame direction: %s\n", directionToString());
26590fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame timestamp: %d\n", mDriverTimestampUSec);
26600fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame fate: %s\n", fateToString());
26610fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame type: %s\n", frameTypeToString(mFrameType));
2662590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            pw.format("Frame protocol: %s\n", parser.mMostSpecificProtocolString);
2663590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            pw.format("Frame protocol type: %s\n", parser.mTypeString);
26640fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.format("Frame length: %d\n", mFrameBytes.length);
26650fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.append("Frame bytes");
2666590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            pw.append(HexDump.dumpHexString(mFrameBytes));  // potentially contains PII
26670fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            pw.append("\n");
26680fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            return sw.toString();
26690fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
26700fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
2671590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan        /* Returns a header to match the output of toTableRowString(). */
2672590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan        public static String getTableHeader() {
2673590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            StringWriter sw = new StringWriter();
2674590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            PrintWriter pw = new PrintWriter(sw);
2675eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            pw.format("\n%-15s  %-12s  %-9s  %-32s  %-12s  %-23s  %s\n",
2676eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                    "Time usec", "Walltime", "Direction", "Fate", "Protocol", "Type", "Result");
2677eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            pw.format("%-15s  %-12s  %-9s  %-32s  %-12s  %-23s  %s\n",
2678eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                    "---------", "--------", "---------", "----", "--------", "----", "------");
2679590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan            return sw.toString();
2680590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan        }
2681590f3fc2045389d5ef274c4b3bd6162d93b1a0acSamuel Tan
26820fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected abstract String directionToString();
26830fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
26840fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected abstract String fateToString();
26850fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
26860fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        private static String frameTypeToString(byte frameType) {
26870fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            switch (frameType) {
26880fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.FRAME_TYPE_UNKNOWN:
26890fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "unknown";
26900fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.FRAME_TYPE_ETHERNET_II:
26910fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "data";
26920fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.FRAME_TYPE_80211_MGMT:
26930fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "802.11 management";
26940fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                default:
26950fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return Byte.toString(frameType);
26960fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
26970fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
2698eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal
2699eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        /**
2700eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal         * Converts a driver timestamp to a wallclock time, based on the current
2701eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal         * BOOTTIME to wallclock mapping. The driver timestamp is a 32-bit counter of
2702eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal         * microseconds, with the same base as BOOTTIME.
2703eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal         */
2704eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        private static long convertDriverTimestampUSecToWallclockMSec(long driverTimestampUSec) {
2705eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            final long wallclockMillisNow = System.currentTimeMillis();
2706eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            final long boottimeMillisNow = SystemClock.elapsedRealtime();
2707eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            final long driverTimestampMillis = driverTimestampUSec / USEC_PER_MSEC;
2708eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal
2709eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            long boottimeTimestampMillis = boottimeMillisNow % MAX_DRIVER_TIMESTAMP_MSEC;
2710eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            if (boottimeTimestampMillis < driverTimestampMillis) {
2711eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // The 32-bit microsecond count has wrapped between the time that the driver
2712eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // recorded the packet, and the call to this function. Adjust the BOOTTIME
2713eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // timestamp, to compensate.
2714eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                //
2715eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // Note that overflow is not a concern here, since the result is less than
2716eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // 2 * MAX_DRIVER_TIMESTAMP_MSEC. (Given the modulus operation above,
2717eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // boottimeTimestampMillis must be less than MAX_DRIVER_TIMESTAMP_MSEC.) And, since
2718eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // MAX_DRIVER_TIMESTAMP_MSEC is an int, 2 * MAX_DRIVER_TIMESTAMP_MSEC must fit
2719eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                // within a long.
2720eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal                boottimeTimestampMillis += MAX_DRIVER_TIMESTAMP_MSEC;
2721eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            }
2722eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal
2723eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            final long millisSincePacketTimestamp = boottimeTimestampMillis - driverTimestampMillis;
2724eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal            return wallclockMillisNow - millisSincePacketTimestamp;
2725eaf6303a081b4a7f2231b5053099da7a1c16b7bdmukesh agrawal        }
272609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
272709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
272809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /**
272909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     * Represents the fate information for one outbound packet.
273009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     */
273109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
273209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    public static final class TxFateReport extends FateReport {
273309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        TxFateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
273409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            super(fate, driverTimestampUSec, frameType, frameBytes);
273509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
27360fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
27370fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
27380fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String directionToString() {
27390fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            return "TX";
27400fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
27410fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
27420fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
27430fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String fateToString() {
27440fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            switch (mFate) {
27450fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_ACKED:
27460fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "acked";
27470fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_SENT:
27480fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "sent";
27490fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_QUEUED:
27500fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware queued";
27510fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_DROP_INVALID:
27520fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (invalid frame)";
27530fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_DROP_NOBUFS:
27540fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (no bufs)";
27550fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_FW_DROP_OTHER:
27560fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (other)";
27570fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_QUEUED:
27580fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver queued";
27590fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_DROP_INVALID:
27600fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (invalid frame)";
27610fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_DROP_NOBUFS:
27620fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (no bufs)";
27630fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.TX_PKT_FATE_DRV_DROP_OTHER:
27640fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (other)";
27650fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                default:
27660fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return Byte.toString(mFate);
27670fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
27680fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
276909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
277009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
277109b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    /**
277209b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     * Represents the fate information for one inbound packet.
277309b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal     */
277409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    @Immutable
277509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    public static final class RxFateReport extends FateReport {
277609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        RxFateReport(byte fate, long driverTimestampUSec, byte frameType, byte[] frameBytes) {
277709b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal            super(fate, driverTimestampUSec, frameType, frameBytes);
277809b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal        }
27790fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
27800fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
27810fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String directionToString() {
27820fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            return "RX";
27830fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
27840fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
27850fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        @Override
27860fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        protected String fateToString() {
27870fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            switch (mFate) {
27880fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_SUCCESS:
27890fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "success";
27900fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_QUEUED:
27910fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware queued";
27920fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_FILTER:
27930fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (filter)";
27940fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_INVALID:
27950fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (invalid frame)";
27960fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_NOBUFS:
27970fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (no bufs)";
27980fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_FW_DROP_OTHER:
27990fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "firmware dropped (other)";
28000fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_QUEUED:
28010fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver queued";
28020fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_FILTER:
28030fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (filter)";
28040fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_INVALID:
28050fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (invalid frame)";
28060fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_NOBUFS:
28070fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (no bufs)";
28080fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                case WifiLoggerHal.RX_PKT_FATE_DRV_DROP_OTHER:
28090fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return "driver dropped (other)";
28100fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                default:
28110fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return Byte.toString(mFate);
28120fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
28130fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
281409b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    }
281509b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
281609b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int startPktFateMonitoringNative(int iface);
28170fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    /**
28180fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     * Ask the HAL to enable packet fate monitoring. Fails unless HAL is started.
28190fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     */
28200fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public boolean startPktFateMonitoring() {
28210fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        synchronized (sLock) {
28220fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            if (isHalStarted()) {
28230fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return startPktFateMonitoringNative(sWlan0Index) == WIFI_SUCCESS;
28240fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            } else {
28250fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return false;
28260fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
28270fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
28280fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    }
28290fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
283009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int getTxPktFatesNative(int iface, TxFateReport[] reportBufs);
28310fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    /**
28320fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     * Fetch the most recent TX packet fates from the HAL. Fails unless HAL is started.
28330fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     */
28340fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public boolean getTxPktFates(TxFateReport[] reportBufs) {
28350fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        synchronized (sLock) {
28360fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            if (isHalStarted()) {
28370fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                int res = getTxPktFatesNative(sWlan0Index, reportBufs);
28380fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                if (res != WIFI_SUCCESS) {
28390fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    Log.e(TAG, "getTxPktFatesNative returned " + res);
28400fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return false;
28410fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                } else {
28420fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return true;
28430fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                }
28440fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            } else {
28450fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return false;
28460fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
28470fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
28480fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    }
28490fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal
285009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    private static native int getRxPktFatesNative(int iface, RxFateReport[] reportBufs);
28510fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    /**
28520fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     * Fetch the most recent RX packet fates from the HAL. Fails unless HAL is started.
28530fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal     */
28540fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    public boolean getRxPktFates(RxFateReport[] reportBufs) {
28550fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        synchronized (sLock) {
28560fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            if (isHalStarted()) {
28570fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                int res = getRxPktFatesNative(sWlan0Index, reportBufs);
28580fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                if (res != WIFI_SUCCESS) {
28590fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    Log.e(TAG, "getRxPktFatesNative returned " + res);
28600fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return false;
28610fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                } else {
28620fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                    return true;
28630fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                }
28640fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            } else {
28650fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal                return false;
28660fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal            }
28670fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal        }
28680fcb06473cbe2824e401a80c0520bb1c14ed8f41mukesh agrawal    }
286909b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal
287009b1d0786d05436d524d7556c269e665a0962ee6mukesh agrawal    //---------------------------------------------------------------------------------
28715c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /* Configure ePNO/PNO */
28729bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static PnoEventHandler sPnoEventHandler;
28739bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static int sPnoCmdId = 0;
2874dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28759bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static native boolean setPnoListNative(int iface, int id, PnoSettings settings);
287618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
28775c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius    /**
28789bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Set the PNO settings & the network list in HAL to start PNO.
28799bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param settings PNO settings and network list.
28809bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param eventHandler Handler to receive notifications back during PNO scan.
28819bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
28825c3c06082b24f9ff0d479e82a63b52220c86598bRoshan Pius     */
28839bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean setPnoList(PnoSettings settings, PnoEventHandler eventHandler) {
28849bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2885dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28869bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        synchronized (sLock) {
28879bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            if (isHalStarted()) {
28889bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoCmdId = getNewCmdIdLocked();
28899bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoEventHandler = eventHandler;
28909bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                if (setPnoListNative(sWlan0Index, sPnoCmdId, settings)) {
28919bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                    return true;
28929bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                }
2893dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
28949bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            sPnoEventHandler = null;
28959bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius            return false;
2896dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2897dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2898dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
28999bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
29009bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Set the PNO network list in HAL to start PNO.
29019bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param list PNO network list.
29029bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @param eventHandler Handler to receive notifications back during PNO scan.
29039bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
29049bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
29059bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean setPnoList(PnoNetwork[] list, PnoEventHandler eventHandler) {
29069bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        PnoSettings settings = new PnoSettings();
29079bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        settings.networkList = list;
29089bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        return setPnoList(settings, eventHandler);
2909dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2910dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
29119bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    private static native boolean resetPnoListNative(int iface, int id);
2912dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
29139bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    /**
29149bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * Reset the PNO settings in HAL to stop PNO.
29159bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     * @return true if success, false otherwise
29169bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius     */
29179bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius    public boolean resetPnoList() {
29189bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        Log.e(TAG, "resetPnoList cmd " + sPnoCmdId);
2919dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2920956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
292171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2922f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
29239bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                sPnoEventHandler = null;
29249bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius                if (resetPnoListNative(sWlan0Index, sPnoCmdId)) {
2925f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2926f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2927dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2928f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
2929dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2930dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2931dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
293218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
29333571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void onPnoNetworkFound(int id, ScanResult[] results, int[] beaconCaps) {
2934dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2935dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2936dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2937dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2938dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2939dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2940dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
29419bd01d6cb65ea96d6729d35a1fc43bb2bb40e3bdRoshan Pius        PnoEventHandler handler = sPnoEventHandler;
294218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
294318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
294418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
294518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
2946dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
29471ab129e587d334a144a0bca5323c27985397a403Randy Pan                populateScanResult(results[i], beaconCaps[i], "onPnoNetworkFound ");
294818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2949dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
295018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
295118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
295218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
295318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
295418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
2955dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2956d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2957d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
29589ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
29599ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
29609ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
296118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
29629ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
29639ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
29649ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
29659ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
29669ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
29679ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2968956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
296971d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2970f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2971f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
2972f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
29739ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
29749ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
29759ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
29769ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
29779ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2978c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
2979c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
2980c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
298118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2982c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
2983c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
29843bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
29853bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
29863bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
29873bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
29883bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
29893bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
29903bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
2991956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2992c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2993c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
299418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
2995c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2996c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2997c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2998c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2999c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
3000c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
3001c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
3002c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
300318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
3004c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
3005c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
3006956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
3007c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
3008c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
3009c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
3010c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
3011c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
3012c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
3013c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
3014aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3015aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
3016aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
3017aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
3018aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3019aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
3020aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
302118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
302218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
302318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
302418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
302518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
302618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
3027aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
3028aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3029aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
3030aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
3031aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3032aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
3033aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
303418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
3035aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
3036aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
3037956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
303818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
3039aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
3040ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
3041ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
3042ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
3043ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
3044aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
3045aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
3046ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
3047ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
3048ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
3049ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
3050ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
3051ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
3052aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
3053aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
3054aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
3055aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
3056aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
3057aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
3058aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
3059aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
306018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
3061aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
3062956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
3063aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
3064ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
3065ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
3066ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
3067ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
3068ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
3069ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
3070aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
3071aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
3072aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
3073aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
3074aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
30755ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
30765ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
30776bf6986d359556010638dfae332b585162f06520Roshan Pius
30786bf6986d359556010638dfae332b585162f06520Roshan Pius    /**
30796bf6986d359556010638dfae332b585162f06520Roshan Pius     * Fetch the host wakeup reasons stats from wlan driver.
30806bf6986d359556010638dfae332b585162f06520Roshan Pius     * @return the |WifiWakeReasonAndCounts| object retrieved from the wlan driver.
30816bf6986d359556010638dfae332b585162f06520Roshan Pius     */
30826bf6986d359556010638dfae332b585162f06520Roshan Pius    public WifiWakeReasonAndCounts getWlanWakeReasonCount() {
30836bf6986d359556010638dfae332b585162f06520Roshan Pius        Log.d(TAG, "getWlanWakeReasonCount " + sWlan0Index);
30846bf6986d359556010638dfae332b585162f06520Roshan Pius        synchronized (sLock) {
30856bf6986d359556010638dfae332b585162f06520Roshan Pius            if (isHalStarted()) {
30866bf6986d359556010638dfae332b585162f06520Roshan Pius                return getWlanWakeReasonCountNative(sWlan0Index);
30876bf6986d359556010638dfae332b585162f06520Roshan Pius            } else {
30886bf6986d359556010638dfae332b585162f06520Roshan Pius                return null;
30896bf6986d359556010638dfae332b585162f06520Roshan Pius            }
30906bf6986d359556010638dfae332b585162f06520Roshan Pius        }
30916bf6986d359556010638dfae332b585162f06520Roshan Pius    }
30923dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
30933dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    private static native int configureNeighborDiscoveryOffload(int iface, boolean enabled);
30943dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline
30953dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    public boolean configureNeighborDiscoveryOffload(boolean enabled) {
30963dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        final String logMsg =  "configureNeighborDiscoveryOffload(" + enabled + ")";
30973dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        Log.d(mTAG, logMsg);
30983dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        synchronized (sLock) {
30993dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            if (isHalStarted()) {
31003dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                final int ret = configureNeighborDiscoveryOffload(sWlan0Index, enabled);
31013dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                if (ret != 0) {
31023dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                    Log.d(mTAG, logMsg + " returned: " + ret);
31033dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                }
31043dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline                return (ret == 0);
31053dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline            }
31063dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        }
31073dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline        return false;
31083dd6f75ff2ab823bd0c14581f2e047c74916f16fErik Kline    }
3109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
3110