WifiNative.java revision ed6a985c7b63e295248fa7e8292c99b48b7a4283
1155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/*
2155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Copyright (C) 2008 The Android Open Source Project
3155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
4155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Licensed under the Apache License, Version 2.0 (the "License");
5155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * you may not use this file except in compliance with the License.
6155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * You may obtain a copy of the License at
7155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
8155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *      http://www.apache.org/licenses/LICENSE-2.0
9155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
10155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Unless required by applicable law or agreed to in writing, software
11155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * distributed under the License is distributed on an "AS IS" BASIS,
12155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * See the License for the specific language governing permissions and
14155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * limitations under the License.
15155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
16155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepackage com.android.server.wifi;
18155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.annotation.Nullable;
20fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.app.AlarmManager;
21fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.app.PendingIntent;
22fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport android.content.BroadcastReceiver;
232a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.Context;
242a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.Intent;
252a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.content.IntentFilter;
26143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpandeimport android.net.wifi.RttManager;
2768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wangimport android.net.wifi.RttManager.ResponderConfig;
28e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
29dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiConfiguration;
3018786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.net.wifi.WifiEnterpriseConfig;
31aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
3203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.WifiManager;
33e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
34dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiSsid;
352a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Willsimport android.net.wifi.WifiWakeReasonAndCounts;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
3903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
40f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
4118786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport android.os.SystemProperties;
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
45fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
46c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadhamimport com.android.server.connectivity.KeepalivePacketData;
472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.NetworkDetail;
482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.SupplicantBridge;
492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Willsimport com.android.server.wifi.hotspot2.Utils;
505d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Willsimport com.android.server.wifi.util.InformationElementUtil;
51c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
5218786eca942042388748b0d98979f21c9dff4a89Mitchell Willsimport libcore.util.HexEncoding;
5318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
54fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONException;
55fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport org.json.JSONObject;
56fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
57fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.io.UnsupportedEncodingException;
58fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLDecoder;
59fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.net.URLEncoder;
605cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.ByteBuffer;
615cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.CharBuffer;
625cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharacterCodingException;
635cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.CharsetDecoder;
645cfd8d8b9f241dcad874125a1b5538ee0d6860fexinheimport java.nio.charset.StandardCharsets;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
663571366ac36c70746b9f013ec2b54482861c9292Randy Panimport java.util.BitSet;
67fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.HashMap;
68fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Iterator;
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
71fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowskiimport java.util.Map;
729ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport java.util.Set;
7318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
74fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
76155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
79155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
85b66b29a00da970ee75052e24f592c8d6c16bd0edxinhe    private static boolean DBG = false;
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
8818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Hold this lock before calling supplicant or HAL methods
8918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * it is required to mutually exclude access to the driver
9018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
91956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static final Object sLock = new Object();
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
93956f54b391677d78379729dd14518edddf3c7660Etan Cohen    private static final LocalLog sLocalLog = new LocalLog(16384);
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static LocalLog getLocalLog() {
96956f54b391677d78379729dd14518edddf3c7660Etan Cohen        return sLocalLog;
9718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
10918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Singleton WifiNative instances
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
11118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative wlanNativeInterface =
11218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            new WifiNative(SystemProperties.get("wifi.interface", "wlan0"));
11318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getWlanNativeInterface() {
11418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return wlanNativeInterface;
11518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
11718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    //STOPSHIP: get interface name from native side
11818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static WifiNative p2pNativeInterface = new WifiNative("p2p0");
11918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static WifiNative getP2pNativeInterface() {
12018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return p2pNativeInterface;
12118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mTAG;
12518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfaceName;
12618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private final String mInterfacePrefix;
12718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
12818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private Context mContext = null;
12918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private PnoMonitor mPnoMonitor = null;
13018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void initContext(Context context) {
13118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (mContext == null && context != null) {
13218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mContext = context;
13318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            mPnoMonitor = new PnoMonitor();
13418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
13518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private WifiNative(String interfaceName) {
138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
140e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!interfaceName.equals("p2p0")) {
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // commands for p2p0 interface don't need prefix
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
14918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName() {
15018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return mInterfaceName;
151e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
152e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
15318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Note this affects logging on for all interfaces
154ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
155ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
156ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
157ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
158ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
159ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
160ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
161ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
16218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private void localLog(String s) {
163956f54b391677d78379729dd14518edddf3c7660Etan Cohen        if (sLocalLog != null) sLocalLog.log(mInterfaceName + ": " + s);
16418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
16718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
16818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
16918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Driver and Supplicant management
17018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
17118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean loadDriverNative();
17218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean loadDriver() {
173956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
17418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return loadDriverNative();
17518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean isDriverLoadedNative();
17918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isDriverLoaded() {
180956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return isDriverLoadedNative();
18218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
18518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean unloadDriverNative();
18618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean unloadDriver() {
187956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
18818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return unloadDriverNative();
18918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
19018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
19118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
19218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean startSupplicantNative(boolean p2pSupported);
19318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startSupplicant(boolean p2pSupported) {
194956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return startSupplicantNative(p2pSupported);
19618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
19718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
19818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
19918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* Sends a kill signal to supplicant. To be used when we have lost connection
20018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills       or when the supplicant is hung */
20118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean killSupplicantNative(boolean p2pSupported);
20218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean killSupplicant(boolean p2pSupported) {
203956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
20418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return killSupplicantNative(p2pSupported);
20518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
20818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static boolean connectToSupplicantNative();
209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
210956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
211b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "connectToSupplicant");
212b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            return connectToSupplicantNative();
213b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
21618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static void closeSupplicantConnectionNative();
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
218956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
219b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            localLog(mInterfacePrefix + "closeSupplicantConnection");
220b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande            closeSupplicantConnectionNative();
221b577f391af2c484e443c19b3df1d62cc0924692aVinit Deshpande        }
222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
22418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /**
22518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wait for the supplicant to send an event, returning the event string.
22618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * @return the event string sent by the supplicant.
22718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
22818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native static String waitForEventNative();
229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
23418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
23518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
23618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Supplicant Command Primitives
23718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
23818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native boolean doBooleanCommandNative(String command);
23918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native int doIntCommandNative(String command);
24118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
24218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private native String doStringCommandNative(String command);
24318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
246956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
24718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
2497b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
2500888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
255a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    private boolean doBooleanCommandWithoutLogging(String command) {
256a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (DBG) Log.d(mTAG, "doBooleanCommandWithoutLogging: " + command);
257956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
258a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
259a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            if (DBG) Log.d(mTAG, command + ": returned " + result);
260a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return result;
261a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
262a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham    }
263a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
266956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
26718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
2697b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
2760888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2770888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2780888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2790888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2800888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
2810888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
282956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
28318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            String toLog = mInterfacePrefix + command;
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
28540ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
28640ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
28740ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
2887b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (!command.startsWith("STATUS-")) {
2897b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog(toLog + " -> " + result);
2907b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
29140ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
29240ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
2980888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2990888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
3000888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
3010888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
3020888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
30327355a942653264388e909a4276196ee63e57811vandwalle        }
304956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
30918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String doCustomSupplicantCommand(String command) {
31018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doStringCommand(command);
31118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
31218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
31318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /*
31418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     * Wrappers for supplicant commands
31518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills     */
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
321ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
322ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
323ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
324ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
325a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
326a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
327a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
328a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
32918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
330ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    /**
331ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * Start a scan using wpa_supplicant for the given frequencies.
332ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     * If freqs is null then all supported channels are scanned.
333ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills     */
334ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    public boolean scan(Set<Integer> freqs) {
335ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        if (freqs == null) {
336ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return scanFrequencyList(null);
337ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        } else if (freqs.size() != 0) {
3389ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            StringBuilder freqList = new StringBuilder();
3399ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            boolean first = true;
3409ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            for (Integer freq : freqs) {
341ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills                if (!first) {
3429ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                    freqList.append(",");
343ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills                }
3449ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                freqList.append(freq.toString());
3459ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                first = false;
3469ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            }
347ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return scanFrequencyList(freqList.toString());
348ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        } else {
3499ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            return false;
3509ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills        }
3519ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
3529ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
353ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills    private boolean scanFrequencyList(String freqList) {
354ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills        if (freqList == null) {
355ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return doBooleanCommand("SCAN TYPE=ONLY");
356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
357ed6a985c7b63e295248fa7e8292c99b48b7a4283Mitchell Wills            return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
375e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
376e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
377e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
378e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
383fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public boolean setNetworkExtra(int netId, String name, Map<String, String> values) {
384fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String encoded;
385fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
386fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            encoded = URLEncoder.encode(new JSONObject(values).toString(), "UTF-8");
387fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (NullPointerException e) {
388fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
389fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
390fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
391fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
392fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return false;
393fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
394fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        return setNetworkVariable(netId, name, "\"" + encoded + "\"");
395fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
396fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
399a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        if (name.equals(WifiConfiguration.pskVarName)
400a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham                || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
401a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value);
402a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        } else {
403a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham            return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
404a6777abfc90496801e9942f57fbfa091ba85ae82Prerepa Viswanadham        }
405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
407fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    public Map<String, String> getNetworkExtra(int netId, String name) {
408fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        final String wrapped = getNetworkVariable(netId, name);
409fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        if (wrapped == null || !wrapped.startsWith("\"") || !wrapped.endsWith("\"")) {
410fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
411fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
412fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        try {
413fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final String encoded = wrapped.substring(1, wrapped.length() - 1);
414fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This method reads a JSON dictionary that was written by setNetworkExtra(). However,
415fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // on devices that upgraded from Marshmallow, it may encounter a legacy value instead -
416fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // an FQDN stored as a plain string. If such a value is encountered, the JSONObject
417fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // constructor will thrown a JSONException and the method will return null.
418fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final JSONObject json = new JSONObject(URLDecoder.decode(encoded, "UTF-8"));
419fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Map<String, String> values = new HashMap<String, String>();
420fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            final Iterator<?> it = json.keys();
421fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            while (it.hasNext()) {
422fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final String key = (String) it.next();
423fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                final Object value = json.get(key);
424fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                if (value instanceof String) {
425fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                    values.put(key, (String) value);
426fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski                }
427fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            }
428fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return values;
429fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (UnsupportedEncodingException e) {
430fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            Log.e(TAG, "Unable to serialize networkExtra: " + e.toString());
431fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
432fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        } catch (JSONException e) {
433fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // This is not necessarily an error. This exception will also occur if we encounter a
434fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // legacy FQDN stored as a plain string. We want to return null in this case as no JSON
435fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            // dictionary of extras was found.
436fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski            return null;
437fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski        }
438fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski    }
439fbb081b7c28c18f0644701061a1ab38a4627db27Bartosz Fabianowski
440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
451f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
452f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
453f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
454f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
455ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
456ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
457ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
458ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
459ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
460ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
464ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
465ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + " disableOthers=" + disableOthers);
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("SELECT_NETWORK " + netId);
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("ENABLE_NETWORK " + netId);
470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
474f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4780047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
4790047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
4800047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
4810047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
4820047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
484f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
489f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
49421bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
49999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
50299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
50399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
50499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
50599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
50699d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
50799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
50899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
50999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
520a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
521a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
5262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:7f:76:d7:1a:6e
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
5282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-44
5292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700342
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zfdy
5322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ====
5332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * id=2
5342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * bssid=68:5f:74:d7:1a:6f
5352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * freq=5180
5362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * level=-73
5372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * tsf=1344626243700373
5382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * flags=[WPA2-PSK-CCMP][WPS][ESS]
5392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills     * ssid=zuby
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
54577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        1                       0     2
54677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
54777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        0                       1     1   -> 9
54877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
54977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       1     9   -> d
55077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
55177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       0     8
55277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
55377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         1                        1                       1     7
55477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
55577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
55677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
55777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
55877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
55977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  wpa_supplicant_ctrl_iface_bss:4315
56077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  print_bss_info
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
5622afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private String getRawScanResults(String range) {
5632afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87");
5642afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    }
5652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_IE_STR = "ie=";
5672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_ID_STR = "id=";
5682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_BSSID_STR = "bssid=";
5692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FREQ_STR = "freq=";
5702afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_LEVEL_STR = "level=";
5712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_TSF_STR = "tsf=";
5722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_FLAGS_STR = "flags=";
5732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_SSID_STR = "ssid=";
5742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_DELIMITER_STR = "====";
5752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    private static final String BSS_END_STR = "####";
5762afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5772afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills    public ArrayList<ScanDetail> getScanResults() {
5782afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        int next_sid = 0;
5792afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        ArrayList<ScanDetail> results = new ArrayList<>();
5802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        while(next_sid >= 0) {
5812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String rawResult = getRawScanResults(next_sid+"-");
5822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            next_sid = -1;
5832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            if (TextUtils.isEmpty(rawResult))
5852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                break;
5862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String[] lines = rawResult.split("\n");
5882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5902afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // note that all these splits and substrings keep references to the original
5912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // huge string buffer while the amount we really want is generally pretty small
5922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            // so make copies instead (one example b/11087956 wasted 400k of heap here).
5932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int bssidStrLen = BSS_BSSID_STR.length();
5942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            final int flagLen = BSS_FLAGS_STR.length();
5952afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
5962afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String bssid = "";
5972afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int level = 0;
5982afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            int freq = 0;
5992afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            long tsf = 0;
6002afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            String flags = "";
6012afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            WifiSsid wifiSsid = null;
6025d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            String infoElementsStr = null;
6032afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            List<String> anqpLines = null;
6042afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6052afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            for (String line : lines) {
6062afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                if (line.startsWith(BSS_ID_STR)) { // Will find the last id line
6072afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6082afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1;
6092afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6102afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        // Nothing to do
6112afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6122afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_BSSID_STR)) {
6132afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen);
6142afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FREQ_STR)) {
6152afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6162afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length()));
6172afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6182afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        freq = 0;
6192afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6202afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_LEVEL_STR)) {
6212afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6222afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length()));
6232afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        /* some implementations avoid negative values by adding 256
6242afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         * so we need to adjust for that here.
6252afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                         */
6262afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        if (level > 0) level -= 256;
6272afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6282afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        level = 0;
6292afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6302afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_TSF_STR)) {
6312afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    try {
6322afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = Long.parseLong(line.substring(BSS_TSF_STR.length()));
6332afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    } catch (NumberFormatException e) {
6342afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        tsf = 0;
6352afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6362afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_FLAGS_STR)) {
6372afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = new String(line.getBytes(), flagLen, line.length() - flagLen);
6382afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_SSID_STR)) {
6392afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = WifiSsid.createFromAsciiEncoded(
6402afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            line.substring(BSS_SSID_STR.length()));
6412afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_IE_STR)) {
6425d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = line;
6432afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (SupplicantBridge.isAnqpAttribute(line)) {
6442afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (anqpLines == null) {
6452afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        anqpLines = new ArrayList<>();
6462afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6472afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines.add(line);
6482afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) {
6492afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    if (bssid != null) {
6502afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        try {
6515d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (infoElementsStr == null) {
6525d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("Null information element data");
6535d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6545d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            int seperator = infoElementsStr.indexOf('=');
6555d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            if (seperator < 0) {
6565d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                throw new IllegalArgumentException("No element separator");
6575d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            }
6585d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6595d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            ScanResult.InformationElement[] infoElements =
6605d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        InformationElementUtil.parseInformationElements(
6615d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        Utils.hexToBytes(infoElementsStr.substring(seperator + 1)));
6625d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
6635d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                            NetworkDetail networkDetail = new NetworkDetail(bssid,
6645d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                    infoElements, anqpLines, freq);
6652afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6662afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE;
6672afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (!xssid.equals(networkDetail.getTrimmedSSID())) {
6682afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, String.format(
6692afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                        "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s",
6705d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                                        bssid, xssid, networkDetail.getSSID(), infoElementsStr));
6712afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6722afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills
6732afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            if (networkDetail.hasInterworking()) {
6742afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                                Log.d(TAG, "HSNwk: '" + networkDetail);
6752afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            }
6769ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags,
6779ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                                    level, freq, tsf);
6789ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            scan.getScanResult().informationElements = infoElements;
6799ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills                            results.add(scan);
6802afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        } catch (IllegalArgumentException iae) {
6812afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                            Log.d(TAG, "Failed to parse information elements: " + iae);
6822afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                        }
6832afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    }
6842afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    bssid = null;
6852afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    level = 0;
6862afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    freq = 0;
6872afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    tsf = 0;
6882afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    flags = "";
6892afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    wifiSsid = null;
6905d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                    infoElementsStr = null;
6912afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                    anqpLines = null;
6922afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills                }
6932afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills            }
6942afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        }
6952afa54e3c8fa1153302a0d57b0e9b7bee35406ffMitchell Wills        return results;
69677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
69777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
699446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
700446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
701446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
702446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
703446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
704446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
705446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
706446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
707446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
708446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
709446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
710446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
711446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
712446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
713446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
71456d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
715446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
716446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
717446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
718446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
719446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
720446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
721446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
722446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
723446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
724446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
725446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
726446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
727446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
728446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
729446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
730446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
80003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
80103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
80203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
80303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
80403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
80503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
80603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
80703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
80903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
81003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
81103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
81203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
81303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
81403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
81503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
81603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
81703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
82018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
82118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
82218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
8237ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
8247ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
8257ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
8267ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
8277ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
8287ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
8297ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
8307ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
8510a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
8520a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
8530a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
8540a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
8770465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
8780465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
8790465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
8800465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
883e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    //PNO Monitor
884e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    private class PnoMonitor {
885e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final int MINIMUM_PNO_GAP = 5 * 1000;
886e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private static final String ACTION_TOGGLE_PNO =
887e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            "com.android.server.Wifi.action.TOGGLE_PNO";
888e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        long mLastPnoChangeTimeStamp = -1L;
889e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mExpectedPnoState = false;
890e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mCurrentPnoState = false;;
891e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        boolean mWaitForTimer = false;
892e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        final Object mPnoLock = new Object();
893e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final AlarmManager mAlarmManager =
894e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
895e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private final PendingIntent mPnoIntent;
896e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
897e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public PnoMonitor() {
898e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            Intent intent = new Intent(ACTION_TOGGLE_PNO, null);
899e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            intent.setPackage("android");
900e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
901e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
902e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mContext.registerReceiver(
903e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new BroadcastReceiver() {
904e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    @Override
905e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    public void onReceive(Context context, Intent intent) {
906e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        synchronized(mPnoLock) {
907e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (DBG) Log.d(mTAG, "PNO timer expire, PNO should change to " +
908e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    mExpectedPnoState);
909e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            if (mCurrentPnoState != mExpectedPnoState) {
910e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to "
911e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                        + mExpectedPnoState);
912e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                boolean ret = setPno(mExpectedPnoState);
913e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (!ret) {
914e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                    Log.e(mTAG, "set PNO failure");
915e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                }
916e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            } else {
917e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                if (DBG) Log.d(mTAG, "Do not change PNO since current is expected");
918e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            }
919e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                            mWaitForTimer = false;
920e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        }
921e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
922e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                },
923e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                new IntentFilter(ACTION_TOGGLE_PNO));
924e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
925e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
926e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        private boolean setPno(boolean enable) {
927e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            String cmd = enable ? "SET pno 1" : "SET pno 0";
928e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            boolean ret = doBooleanCommand(cmd);
929e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            mLastPnoChangeTimeStamp = System.currentTimeMillis();
930e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            if (ret) {
931e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                mCurrentPnoState = enable;
932e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
933e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return ret;
934e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
935e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
936e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        public boolean enableBackgroundScan(boolean enable) {
937e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            synchronized(mPnoLock) {
938e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                if (mWaitForTimer) {
939e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    //already has a timer
940e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    mExpectedPnoState = enable;
941e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (DBG) Log.d(mTAG, "update expected PNO to " +  mExpectedPnoState);
942e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                } else {
943e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (mCurrentPnoState == enable) {
944e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        return true;
945e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
946e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp;
947e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    if (timeDifference >= MINIMUM_PNO_GAP) {
948e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        return setPno(enable);
949e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    } else {
950e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mExpectedPnoState = enable;
951e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mWaitForTimer = true;
952e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference);
953e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
954e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                                System.currentTimeMillis() + timeDifference, mPnoIntent);
955e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                    }
956e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                }
957e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe                return true;
958e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            }
959e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        }
960e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe    }
961e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe
962c97b98d6877f6603a6a0eee820576f59e7da5e52Pierre Vandwalle    public boolean enableBackgroundScan(boolean enable) {
963e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe        if (mPnoMonitor != null) {
964e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return mPnoMonitor.enableBackgroundScan(enable);
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
966e26ad459b63271548abbdeba4f8d77fcca9f88bdxinhe            return false;
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
970f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
971f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
972f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
973f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
974f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
975f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
976f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
977f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
979155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
98256845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    public void setHs20(boolean hs20) {
98356845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        if (hs20) {
98456845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 1");
98556845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        } else {
98656845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande            doBooleanCommand("SET HS20 0");
98756845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande        }
98856845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande    }
98956845c30ef2bc7ccfd92764e4c7352aee03ec3a8Vinit Deshpande
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
992956f54b391677d78379729dd14518edddf3c7660Etan Cohen            synchronized (sLock) {
99318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_DISCOVER " + macAddr);
99418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                doBooleanCommand("TDLS_SETUP " + macAddr);
99518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
1020155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1029155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1030155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
1032956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
1048956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
1063956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
107233b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
107318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        String value = external ? "1" : "0";
107418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        Log.d(TAG, "Setting external_sim to " + value);
107518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("SET external_sim " + value);
107633b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
107733b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
1078f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
1079f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
108018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
108133b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
108233b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
108326eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean simAuthFailedResponse(int id) {
108426eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type GSM-AUTH
108526eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL");
108626eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
108726eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
108826eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    public boolean umtsAuthFailedResponse(int id) {
108926eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        // should be used with type UMTS-AUTH
109026eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande        return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL");
109126eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande    }
109226eebecc04b55129a902d1e7b670fe05668c14faVinit Deshpande
1093ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
109418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
1095ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
1096ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
1145956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
1159956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1221956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
122218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (lc >=1 && lc <= 11) {
122318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
122418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
122518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
122618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (lc != 0) {
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
123018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (oc >= 1 && oc <= 165 ) {
123118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
123218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq 1000-"
123318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + (freq - 5) + "," + (freq + 5) + "-6000");
123418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else if (oc == 0) {
123518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
123618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return doBooleanCommand("P2P_SET disallow_freq \"\"");
123718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            }
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
124618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
1335956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
136836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
136936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
137027f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
137127f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
137236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
137336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
137436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
1375956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
137627f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
137727f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
137827f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
137927f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
138036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
138136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
138236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
138336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
138436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
138536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
138636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
138736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
138836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
139136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
139236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
139336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1460956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
146118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
146218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_ADD";
146318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                command += (" " + s);
146418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
146518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
146618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1477956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
147818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (String s : servInfo.getSupplicantQueryList()) {
147918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String command = "P2P_SERVICE_DEL ";
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
148118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                String[] data = s.split(" ");
148218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (data.length < 2) {
148318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
148418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
148518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if ("upnp".equals(data[0])) {
148618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += s;
148718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else if ("bonjour".equals(data[0])) {
148818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += data[0];
148918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    command += (" " + data[1]);
149018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                } else {
149118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
149218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
149318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                if (!doBooleanCommand(command)) {
149418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    return false;
149518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                }
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
15263f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1527446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1528446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1529446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1530446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
1531f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    /*
1532f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     * NFC-related calls
1533f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande     */
1534f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcWpsConfigurationToken(int netId) {
1535f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1536f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1537f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1538f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverRequest() {
1539f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1540f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1541f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1542f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public String getNfcHandoverSelect() {
1543f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1544f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1545f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1546f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean initiatorReportNfcHandover(String selectMessage) {
1547f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1548f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1549f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1550f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    public boolean responderReportNfcHandover(String requestMessage) {
1551f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1552f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    }
1553f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
15547e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15557e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    /* kernel logging support */
15567e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    private static native byte[] readKernelLogNative();
15577e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15587e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    synchronized public String readKernelLog() {
15597e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        byte[] bytes = readKernelLogNative();
15607e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        if (bytes != null) {
15617e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
15627e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            try {
15637e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
15647e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return decoded.toString();
15657e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            } catch (CharacterCodingException cce) {
15667e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande                return new String(bytes, StandardCharsets.ISO_8859_1);
15677e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            }
15687e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        } else {
15697e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande            return "*** failed to read kernel log ***";
15707e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande        }
15717e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande    }
15727e0315140767e6f12200f7dbbe7aff43b4f75089Vinit Deshpande
15737f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
15747f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
157518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // HAL command ids
157618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int sCmdId = 1;
157718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int getNewCmdIdLocked() {
157818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        return sCmdId++;
157918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    }
158018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
1581b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1582f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long sWifiHalHandle = 0;             /* used by JNI to save wifi_handle */
1583f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static long[] sWifiIfaceHandles = null;     /* used by JNI to save interface handles */
1584956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public static int sWlan0Index = -1;
1585aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sP2p0Index = -1;
1586f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static MonitorThread sThread;
1587f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande    private static final int STOP_HAL_TIMEOUT_MS = 1000;
15887f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1589b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1590b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1591b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
15927f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1593b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
15947ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1595b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
15967ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
15977ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
15987ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
15997ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
160018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startHal() {
1601d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1602d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1603d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1604d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1605d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1606d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1607956f54b391677d78379729dd14518edddf3c7660Etan Cohen        sLocalLog.log(debugLog);
1608d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1609956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1610cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1611f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = new MonitorThread();
1612f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread.start();
1613aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1614aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1615956f54b391677d78379729dd14518edddf3c7660Etan Cohen                if (DBG) sLocalLog.log("Could not start hal");
1616f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.e(TAG, "Could not start hal");
1617aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1618aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
16197ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
16207ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16217ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
162218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopHal() {
1623956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
162471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1625f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                stopHalNative();
1626f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                try {
1627f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sThread.join(STOP_HAL_TIMEOUT_MS);
1628f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "HAL event thread stopped successfully");
1629f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } catch (InterruptedException e) {
1630f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.e(TAG, "Could not stop HAL cleanly");
1631f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1632f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sThread = null;
1633f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiHalHandle = 0;
1634f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiIfaceHandles = null;
1635f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWlan0Index = -1;
1636f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sP2p0Index = -1;
1637f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1638f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
16397ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
16407f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
164118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isHalStarted() {
164271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe        return (sWifiHalHandle != 0);
164371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe    }
1644b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
16457f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
164618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getInterfaces() {
1647956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
164871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1649f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sWifiIfaceHandles == null) {
1650f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int num = getInterfacesNative();
1651f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    int wifi_num = 0;
1652f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    for (int i = 0; i < num; i++) {
1653f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        String name = getInterfaceNameNative(i);
1654f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        Log.i(TAG, "interface[" + i + "] = " + name);
1655f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        if (name.equals("wlan0")) {
1656f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sWlan0Index = i;
1657f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1658f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        } else if (name.equals("p2p0")) {
1659f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            sP2p0Index = i;
1660f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                            wifi_num++;
1661f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        }
166202a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1663f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return wifi_num;
1664f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1665f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return sWifiIfaceHandles.length;
1666aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
166702a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            } else {
1668f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
1669e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1670e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
16717f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
16727f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1673b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
167418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getInterfaceName(int index) {
1675956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
167618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return getInterfaceNameNative(index);
167718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
16787f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
16797f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1680062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan    // TODO: Change variable names to camel style.
1681e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1682297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_cache_size;
1683e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1684e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1685e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1686297c3acabe7a85eb87240fe3ccf772e57ce6aef7Mitchell Wills        public int  max_scan_reporting_threshold;
16877d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1688e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1689e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1690e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
169118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1692956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
1693af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande            return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities);
1694af5b49884f189bb171c9dc6c6a4405d97e7912acVinit Deshpande        }
1695e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1696e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1697b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1698b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1699e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1700b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1701b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
170283a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1703b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1704d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    private static native void setWifiLinkLayerStatsNative(int iface, int enable);
17057f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1706e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1707e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int frequency;
1708e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int dwell_time_ms;
1709e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        boolean passive;
17107f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
17117f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1712e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1713e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int bucket;
1714e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int band;
1715e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int period_ms;
1716062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan        int max_period_ms;
1717062e3f39e37874fedc01f267de5f4cf7dbebe2b4Randy Pan        int step_count;
1718e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int report_events;
1719e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_channels;
1720daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        ChannelSettings channels[];
1721e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17227f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1723e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1724e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int base_period_ms;
1725e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int max_ap_per_scan;
172683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        int report_threshold_percent;
172783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        int report_threshold_num_scans;
1728e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_buckets;
1729daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        BucketSettings buckets[];
1730e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
17317f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
173268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
173368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Wi-Fi channel information.
173468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
173568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public static class WifiChannelInfo {
173668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mPrimaryFrequency;
173768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency0;
173868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mCenterFrequency1;
173968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        int mChannelWidth;
174068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        // TODO: add preamble once available in HAL.
174168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
174268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
1743b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
17443fdceeaa74e21c0ca4da52deeeef6571e7e2a6cdMitchell Wills        void onScanResultsAvailable();
1745476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        void onFullScanResult(ScanResult fullScanResult);
17463fdceeaa74e21c0ca4da52deeeef6571e7e2a6cdMitchell Wills        void onScanStatus();
174783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
1748b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1749e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1750e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1751b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
175271af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_RESULTS_AVAILABLE = 0;
175371af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1;
175471af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_THRESHOLD_PERCENT = 2;
175571af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills    public static final int WIFI_SCAN_FAILED = 3;
1756b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
175718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
17582a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onScanStatus(int id, int event) {
175918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
17602a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        if (event == WIFI_SCAN_RESULTS_AVAILABLE || event == WIFI_SCAN_THRESHOLD_NUM_SCANS
17612a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills                || event == WIFI_SCAN_THRESHOLD_PERCENT) {
176218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            if (handler != null) {
17632a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills                // TODO pass event back to framework
17643fdceeaa74e21c0ca4da52deeeef6571e7e2a6cdMitchell Wills                handler.onScanStatus();
1765b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            }
1766b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
176771af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills        else if (event == WIFI_SCAN_FAILED) {
176871af8bb37fee5852ae458edabdef3b487d62a5b8Mitchell Wills            // TODO signal that scan has failed
17692a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        }
1770e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1771e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
177218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static  WifiSsid createWifiSsid(byte[] rawSsid) {
17735cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid));
17745cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17755cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssidHexString == null) {
17765cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return null;
17775cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
17785cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17795cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString);
17805cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17815cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return wifiSsid;
17825cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
17835cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17845cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static String ssidConvert(byte[] rawSsid) {
17855cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        String ssid;
17865cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17875cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
17885cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        try {
17895cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid));
17905cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = decoded.toString();
17915cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        } catch (CharacterCodingException cce) {
17925cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = null;
17935cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
17945cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17955cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (ssid == null) {
17965cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            ssid = new String(rawSsid, StandardCharsets.ISO_8859_1);
17975cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
17985cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
17995cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return ssid;
18005cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18015cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
180218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Called from native
18035cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    public static boolean setSsid(byte[] rawSsid, ScanResult result) {
18045cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        if (rawSsid == null || rawSsid.length == 0 || result == null) {
18055cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe            return false;
18065cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        }
18075cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18085cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.SSID = ssidConvert(rawSsid);
18095cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        result.wifiSsid = createWifiSsid(rawSsid);
18105cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe        return true;
18115cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe    }
18125cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe
18133571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void populateScanResult(ScanResult result, byte[] bytes, int beaconCap,
18143571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                            String dbg) {
1815dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (bytes == null) return;
1816dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
18175d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18185d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation();
18195d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.VhtOperation vhtOperation =
18205d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.VhtOperation();
18215d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        InformationElementUtil.ExtendedCapabilities extendedCaps =
18225d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                new InformationElementUtil.ExtendedCapabilities();
18235d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills
18245d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        ScanResult.InformationElement elements[] =
18255d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                InformationElementUtil.parseInformationElements(bytes);
18265d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        for (ScanResult.InformationElement ie : elements) {
18275d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) {
18285d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                htOperation.from(ie);
18295d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) {
18305d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                vhtOperation.from(ie);
18315d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) {
18325d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills                extendedCaps.from(ie);
1833243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1834243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1835d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
18365d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (extendedCaps.is80211McRTTResponder) {
1837d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
1838d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        } else {
1839d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande            result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER);
1840d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande        }
1841d9f37b2f3df18e96246db93ec4c2a5159b5d3915Vinit Deshpande
1842243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
18435d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills        if (vhtOperation.isValid()) {
18445d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = vhtOperation.getChannelWidth();
18455d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = vhtOperation.getCenterFreq0();
18465d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq1 = vhtOperation.getCenterFreq1();
1847243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
18485d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.channelWidth = htOperation.getChannelWidth();
18495d31cedf4024e0f038b4dfc2081016c8631ee8feMitchell Wills            result.centerFreq0 = htOperation.getCenterFreq0(result.frequency);
1850243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
1851243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
18523571366ac36c70746b9f013ec2b54482861c9292Randy Pan
18533571366ac36c70746b9f013ec2b54482861c9292Randy Pan        // build capabilities string
18543571366ac36c70746b9f013ec2b54482861c9292Randy Pan        BitSet beaconCapBits = new BitSet(16);
18553571366ac36c70746b9f013ec2b54482861c9292Randy Pan        for (int i = 0; i < 16; i++) {
18563571366ac36c70746b9f013ec2b54482861c9292Randy Pan            if ((beaconCap & (1 << i)) != 0) {
18573571366ac36c70746b9f013ec2b54482861c9292Randy Pan                beaconCapBits.set(i);
18583571366ac36c70746b9f013ec2b54482861c9292Randy Pan            }
18593571366ac36c70746b9f013ec2b54482861c9292Randy Pan        }
18603571366ac36c70746b9f013ec2b54482861c9292Randy Pan        result.capabilities = InformationElementUtil.Capabilities.buildCapabilities(elements,
18613571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                               beaconCapBits);
18623571366ac36c70746b9f013ec2b54482861c9292Randy Pan
1863243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
18643571366ac36c70746b9f013ec2b54482861c9292Randy Pan            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth
18653571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " PrimaryFreq: " + result.frequency + " mCenterfreq0: " + result.centerFreq0
18663571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder
18673571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    ? "Support RTT reponder: " : "Do not support RTT responder")
18683571366ac36c70746b9f013ec2b54482861c9292Randy Pan                    + " Capabilities: " + result.capabilities);
1869f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1870f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1871476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1872dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1873dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
187418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
18752a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    private static void onFullScanResult(int id, ScanResult result, byte bytes[],
18763571366ac36c70746b9f013ec2b54482861c9292Randy Pan            int bucketsScanned, int beaconCap) {
1877dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " +
1878dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                "num = " + bytes.length);
1879dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
188018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        ScanEventHandler handler = sScanEventHandler;
188118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
18823571366ac36c70746b9f013ec2b54482861c9292Randy Pan            populateScanResult(result, bytes, beaconCap, " onFullScanResult ");
188318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onFullScanResult(result);
1884dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
18857f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
18867f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1887b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1888b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1889b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
18907f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
189118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
1892956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
189371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1894f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0) {
1895f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScan();
1896f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else if (sScanSettings != null || sScanEventHandler != null) {
1897b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1898f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
18997f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1900f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = getNewCmdIdLocked();
1901e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1902f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = settings;
1903f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = eventHandler;
1904b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1905f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1906f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler = null;
1907f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanSettings = null;
1908f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1909f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1910f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1911f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
1912f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
1913f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
1914e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1915e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1916e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
19177f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
19187f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
191918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void stopScan() {
1920956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
192171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1922ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sScanCmdId != 0) {
1923ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopScanNative(sWlan0Index, sScanCmdId);
1924ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
1925f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanSettings = null;
1926f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanEventHandler = null;
1927f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sScanCmdId = 0;
1928f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1929b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1930b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1931b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
193218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void pauseScan() {
1933956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
193471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1935f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1936f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.d(TAG, "Pausing scan");
1937f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1938f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    stopScanNative(sWlan0Index, sScanCmdId);
1939f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanCmdId = 0;
1940f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sScanEventHandler.onScanPaused(scanData);
1941f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
1942b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1943b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1944b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1945b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
194618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void restartScan() {
1947956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
194871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1949f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
19506f3626faf50499dd95aa299bb1011c27ab05776dPierre Vandwalle                    Log.d(TAG, "Restarting scan");
1951f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanEventHandler handler = sScanEventHandler;
1952f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    ScanSettings settings = sScanSettings;
1953f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    if (startScan(sScanSettings, sScanEventHandler)) {
1954f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler.onScanRestarted();
1955f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    } else {
195683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
1957f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanEventHandler = handler;
1958f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                        sScanSettings = settings;
1959f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    }
196083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
1961b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1962e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1963e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1964e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
196518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiScanner.ScanData[] getScanResults(boolean flush) {
1966956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
19673ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            WifiScanner.ScanData[] sd = null;
196871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
19693ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                sd = getScanResultsNative(sWlan0Index, flush);
19703ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            }
19713ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande
19723ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande            if (sd != null) {
19733ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return sd;
1974f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
19753ff269ca67e73f66ac22049fc318b2f86eafb253Vinit Deshpande                return new WifiScanner.ScanData[0];
1976f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
1977aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1978e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1979e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1980b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
1981d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
1982d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
1983e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1984e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1985b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
1986b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
1987e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1988b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
1989e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
1990b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
1991e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
199218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setHotlist(WifiScanner.HotlistSettings settings,
199318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            HotlistEventHandler eventHandler) {
1994956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
199571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
1996f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
1997f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
1998f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
1999f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = getNewCmdIdLocked();
2000f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2001f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2002f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sHotlistEventHandler = eventHandler;
2003f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) {
2004f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2005f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2006f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2007e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2008f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2009f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2010e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2011e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2012e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2013e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2014e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
201518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void resetHotlist() {
2016956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
201771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2018f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sHotlistCmdId != 0) {
2019f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    resetHotlistNative(sWlan0Index, sHotlistCmdId);
2020f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistCmdId = 0;
2021f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sHotlistEventHandler = null;
2022f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2023e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
20247f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
20257f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
2026e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
202718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
202818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApFound(int id, ScanResult[] results) {
202918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
203018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
203118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApFound(results);
203218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
203318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
203418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP found event");
2035d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
2036d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
2037d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
203818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
203918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onHotlistApLost(int id, ScanResult[] results) {
204018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        HotlistEventHandler handler = sHotlistEventHandler;
204118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
204218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onHotlistApLost(results);
204318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
204418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
204518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring hotlist AP lost event");
2046aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2047e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2048e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2049b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
2050e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
2051e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2052e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2053b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
2054b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
2055e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2056b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
2057e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
2058b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
2059e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
206018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean trackSignificantWifiChange(
2061b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
2062956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
206371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2064f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2065f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2066f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2067f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = getNewCmdIdLocked();
2068f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2069f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande
2070f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sSignificantWifiChangeHandler = handler;
2071851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId,
2072851075f4f345d98de885bd2f6b833944b282097eMitchell Wills                        settings) == false) {
2073f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2074f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2075f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2076e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2077f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return true;
2078f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2079e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
2080e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2081e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
2082e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2083e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2084e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
208518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void untrackSignificantWifiChange() {
2086956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
208771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2088f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sSignificantWifiChangeCmdId != 0) {
2089f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
2090f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeCmdId = 0;
2091f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sSignificantWifiChangeHandler = null;
2092f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2093e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
2094e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
2095e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2096e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
209718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
209818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onSignificantWifiChange(int id, ScanResult[] results) {
209918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler;
210018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
210118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onChangesFound(results);
210218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
2103f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            /* this can happen because of race conditions */
210418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring significant wifi change");
2105aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
2106e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
2107e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
210818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
2109200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
2110200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
2111956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
211271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2113aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
2114f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2115f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return null;
2116f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2117aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
21185c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
21195c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
212018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public void setWifiLinkLayerStats(String iface, int enable) {
2121d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        if (iface == null) return;
2122956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
212371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2124d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle                setWifiLinkLayerStatsNative(sWlan0Index, enable);
2125d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle            }
2126d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle        }
2127d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle    }
2128d745a52dc4f929d4a4030f205ed173bdf60eaf10Pierre Vandwalle
2129c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
213018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedFeatureSet() {
2131956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
213271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2133f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return getSupportedFeatureSetNative(sWlan0Index);
2134f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
2135f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started");
2136f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
2137f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            }
2138f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande        }
2139a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
2140143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2141143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
2142143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
2143143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
2144143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2145143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2146143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
2147143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
2148143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
214918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
215018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRttResults(int id, RttManager.RttResult[] results) {
215118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        RttEventHandler handler = sRttEventHandler;
215218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null && id == sRttCmdId) {
215302a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
215418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRttResults(results);
2155143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
2156143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
215718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "RTT Received event for unknown cmd = " + id +
215818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                    ", current id = " + sRttCmdId);
2159143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2160143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2161143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2162143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
2163143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2164143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
2165143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
2166143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
216718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean requestRtt(
2168143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
2169956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
217071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2171f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId != 0) {
2172f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    Log.v("TAG", "Last one is still under measurement!");
2173f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2174f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
2175f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttCmdId = getNewCmdIdLocked();
2176f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2177f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttEventHandler = handler;
2178f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                Log.v(TAG, "native issue RTT request");
2179f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return requestRangeNative(sWlan0Index, sRttCmdId, params);
2180143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2181f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return false;
2182143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2183143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2184143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2185143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
218618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean cancelRtt(RttManager.RttParams[] params) {
2187956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
218871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2189f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (sRttCmdId == 0) {
2190f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2191f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2192143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
2193f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sRttCmdId = 0;
2194f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
2195f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
2196f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    sRttEventHandler = null;
21975cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.v(TAG, "RTT cancel Request Successfully");
2198f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2199f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                } else {
22005cfd8d8b9f241dcad874125a1b5538ee0d6860fexinhe                    Log.e(TAG, "RTT cancel Request failed");
2201f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return false;
2202f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2203143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
2204143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
2205143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
2206143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
2207143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
2208042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
220968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static int sRttResponderCmdId = 0;
221068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
221168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native ResponderConfig enableRttResponderNative(int iface, int commandId,
221268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int timeoutSeconds, WifiChannelInfo channelHint);
221368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
221468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Enable RTT responder role on the device. Returns {@link ResponderConfig} if the responder
221568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * role is successfully enabled, {@code null} otherwise.
221668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
221768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    @Nullable
221868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public ResponderConfig enableRttResponder(int timeoutSeconds) {
221968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
222068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return null;
222168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId != 0) {
222268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                if (DBG) Log.e(mTAG, "responder mode already enabled - this shouldn't happen");
222368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return null;
222468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
222568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            int id = getNewCmdIdLocked();
222668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            ResponderConfig config = enableRttResponderNative(
222768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                    sWlan0Index, id, timeoutSeconds, null);
222868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (config != null) sRttResponderCmdId = id;
222968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (DBG) Log.d(TAG, "enabling rtt " + (config != null));
223068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return config;
223168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
223268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
223368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
223468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    private static native boolean disableRttResponderNative(int iface, int commandId);
223568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    /**
223668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * Disable RTT responder role. Returns {@code true} if responder role is successfully disabled,
223768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     * {@code false} otherwise.
223868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang     */
223968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    public boolean disableRttResponder() {
224068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        synchronized (sLock) {
224168cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (!isHalStarted()) return false;
224268cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            if (sRttResponderCmdId == 0) {
224368cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                Log.e(mTAG, "responder role not enabled yet");
224468cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang                return true;
224568cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            }
224668cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            sRttResponderCmdId = 0;
224768cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang            return disableRttResponderNative(sWlan0Index, sRttResponderCmdId);
224868cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang        }
224968cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang    }
225068cb8c02b4ad079c54a2ffd4407da921d18c7af9Wei Wang
2251042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
2252042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
225318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setScanningMacOui(byte[] oui) {
2254956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
225571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2256042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
2257042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
2258042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
2259042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
2260042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
2261042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
2262efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
2263efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
2264efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
2265efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
226618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int [] getChannelsForBand(int band) {
2267956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2268ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2269ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return getChannelsForBandNative(sWlan0Index, band);
227018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2271ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return null;
2272ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2273efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
2274efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
22750465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
2276ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    private static native boolean isGetChannelsForBandSupportedNative();
227718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean isGetChannelsForBandSupported(){
2278956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2279ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            if (isHalStarted()) {
2280ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return isGetChannelsForBandSupportedNative();
228118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
2282ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe                return false;
2283ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe            }
2284ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe        }
2285ee0a0132ebb0d7e9baf42e778ea9c094966ffb14xinhe    }
22860465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
22870465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
228818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setDfsFlag(boolean dfsOn) {
2289956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
229071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
22910465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
22920465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
22930465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
22940465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
22950465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
22960465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
2297b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
2298b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    private static native boolean toggleInterfaceNative(int on);
229918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean toggleInterface(int on) {
2300956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
230171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
230218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                return toggleInterfaceNative(on);
2303b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
2304b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
2305b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
2306b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
2307b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
230812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
230912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
231018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RttManager.RttCapabilities getRttCapabilities() {
2311956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
231271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
231312cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
231418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            } else {
231512cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
231612cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
231712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
231812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
2319939177ff615062ec826601d536466875d8457375xinhe
2320939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
232118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setCountryCodeHal(String CountryCode) {
2322956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
232371d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2324939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
2325939177ff615062ec826601d536466875d8457375xinhe            } else {
2326939177ff615062ec826601d536466875d8457375xinhe                return false;
2327939177ff615062ec826601d536466875d8457375xinhe            }
2328939177ff615062ec826601d536466875d8457375xinhe        }
2329939177ff615062ec826601d536466875d8457375xinhe    }
2330939177ff615062ec826601d536466875d8457375xinhe
2331d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
2332d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
2333d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
2334d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2335d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2336d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
2337d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2338d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
2339d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
234018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) {
2341956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2342f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sTdlsEventHandler = tdlsCallBack;
2343f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
2344d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2345d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2346d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2347d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
2348d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
2349d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
2350d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
2351d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
2352d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
2353d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
2354d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2355d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
235618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsStatus getTdlsStatus(String macAdd) {
2357956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
235871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2359d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
2360d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2361d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2362d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2363d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2364d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2365d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2366d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
2367d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
2368d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
2369d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
2370d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
2371d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
2372d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
2373d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
2374d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2375d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2376d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2377d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2378d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
237918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public TdlsCapabilities getTdlsCapabilities () {
2380956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
238171d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2382d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
2383d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
2384d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
2385d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
2386d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
2387d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2388d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
238918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static boolean onTdlsStatus(String macAddr, int status, int reason) {
239018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        TdlsEventHandler handler = sTdlsEventHandler;
239118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler == null) {
239218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return false;
239318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
239418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onTdlsStatus(macAddr, status, reason);
239518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            return true;
239618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2397d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
2398d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
2399a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
2400a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2401a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
24027d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
2403a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
24040bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onRingBufferData(RingBufferStatus status, byte[] buffer);
24050bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        void onWifiAlert(int errorCode, byte[] buffer);
2406a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2407a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
2408a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
2409a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
241018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24110bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onRingBufferData(RingBufferStatus status, byte[] buffer) {
241218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
241318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
241418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRingBufferData(status, buffer);
241503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
241603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
241718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
24180bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static void onWifiAlert(byte[] buffer, int errorCode) {
241918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiLoggerEventHandler handler = sWifiLoggerEventHandler;
242018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null)
242118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onWifiAlert(errorCode, buffer);
242203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
242303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2424b797893fc1966803d0c013faac42e6396a37a384xinhe    private static int sLogCmdId = -1;
2425b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean setLoggingEventHandlerNative(int iface, int id);
242618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) {
2427956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2428b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2429b797893fc1966803d0c013faac42e6396a37a384xinhe                int oldId =  sLogCmdId;
2430b797893fc1966803d0c013faac42e6396a37a384xinhe                sLogCmdId = getNewCmdIdLocked();
2431b797893fc1966803d0c013faac42e6396a37a384xinhe                if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) {
2432b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = oldId;
2433b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2434b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2435b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = handler;
2436b797893fc1966803d0c013faac42e6396a37a384xinhe                return true;
2437b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2438b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2439b797893fc1966803d0c013faac42e6396a37a384xinhe            }
244003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
244103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
244203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
244303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
24440bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            int flags, int minIntervalSec ,int minDataSize, String ringName);
244518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
244603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
2447956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
244871d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
244903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
245003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
245103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
245203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
245303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
245403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
245503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
245603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
245703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
245818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int getSupportedLoggerFeatureSet() {
2459956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
246071d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
246103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
246203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2463f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return 0;
246403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
246503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
246603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
246703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
2468b797893fc1966803d0c013faac42e6396a37a384xinhe    private static native boolean resetLogHandlerNative(int iface, int id);
246918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean resetLogHandler() {
2470956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2471b797893fc1966803d0c013faac42e6396a37a384xinhe            if (isHalStarted()) {
2472b797893fc1966803d0c013faac42e6396a37a384xinhe                if (sLogCmdId == -1) {
2473b797893fc1966803d0c013faac42e6396a37a384xinhe                    Log.e(TAG,"Can not reset handler Before set any handler");
2474b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2475b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2476b797893fc1966803d0c013faac42e6396a37a384xinhe                sWifiLoggerEventHandler = null;
2477b797893fc1966803d0c013faac42e6396a37a384xinhe                if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) {
2478b797893fc1966803d0c013faac42e6396a37a384xinhe                    sLogCmdId = -1;
2479b797893fc1966803d0c013faac42e6396a37a384xinhe                    return true;
2480b797893fc1966803d0c013faac42e6396a37a384xinhe                } else {
2481b797893fc1966803d0c013faac42e6396a37a384xinhe                    return false;
2482b797893fc1966803d0c013faac42e6396a37a384xinhe                }
2483b797893fc1966803d0c013faac42e6396a37a384xinhe            } else {
2484b797893fc1966803d0c013faac42e6396a37a384xinhe                return false;
2485b797893fc1966803d0c013faac42e6396a37a384xinhe            }
2486b797893fc1966803d0c013faac42e6396a37a384xinhe        }
2487b797893fc1966803d0c013faac42e6396a37a384xinhe    }
2488b797893fc1966803d0c013faac42e6396a37a384xinhe
248903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
249018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getDriverVersion() {
2491956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
249271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
249303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
249403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2495f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
249603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
249703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
249803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
249903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
250003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
250103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
250218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public String getFirmwareVersion() {
2503956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
250471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
250503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
250603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
2507f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return "";
250803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
250903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
251003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
251103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
25120bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    public static class RingBufferStatus{
25130bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        String name;
25140bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int flag;
25150bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferId;
25160bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int ringBufferByteSize;
25170bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int verboseLevel;
25180bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenBytes;
25190bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int readBytes;
25200bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        int writtenRecords;
25210bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25220bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        @Override
25230bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        public String toString() {
25240bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande            return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId +
25250bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel +
25260bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenBytes: " + writtenBytes + " readBytes: " + readBytes +
25270bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    " writtenRecords: " + writtenRecords;
25280bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande        }
25290bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    }
25300bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande
25310bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande    private static native RingBufferStatus[] getRingBufferStatusNative(int iface);
253218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public RingBufferStatus[] getRingBufferStatus() {
2533956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
253471d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
253503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
253603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
253703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
253803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
253903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
254003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
254103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
254203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
254318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean getRingBufferData(String ringName) {
2544956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
254571d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
254603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
254703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
254803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
254903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
255003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
255103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
255203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
255318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static byte[] mFwMemoryDump;
255418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
255503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
255698dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        mFwMemoryDump = buffer;
255798dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        if (DBG) {
255898dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe            Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " +
255998dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe                    (buffer == null ? 0 :  buffer.length));
256098dca3397c0049d3cfb9c97e2b021c5ee3a2e1bdxinhe        }
256103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
2562127f7244183786e6ccae09e81eeccdac31973e69xinhe
256303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
256418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public byte[] getFwMemoryDump() {
2565956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
256671d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
25670bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                if(getFwMemoryDumpNative(sWlan0Index)) {
25680bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    byte[] fwMemoryDump = mFwMemoryDump;
256903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
25700bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return fwMemoryDump;
25710bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                } else {
25720bf150b2d69ec62df905bc6f8603437dc4caee1fVinit Deshpande                    return null;
257303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
257403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2575f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return null;
2576a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2577a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2578dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2579dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
2580dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    /* Configure ePNO */
2581dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
258218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    /* pno flags, keep these values in sync with gscan.h */
258318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_OPEN  = 1; // open
258418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_PSK   = 2; // WPA_PSK or WPA2PSK
258518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_AUTH_CODE_EAPOL = 4; // any EAPOL
258618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
258718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether directed scan needs to be performed (for hidden SSIDs)
258818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_DIRECTED_SCAN = 1;
258918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on A band
259018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_A_BAND = 2;
259118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether PNO event shall be triggered if the network is found on G band
259218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_G_BAND = 4;
259318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Whether strict matching is required (i.e. firmware shall not match on the entire SSID)
259418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static int WIFI_PNO_FLAG_STRICT_MATCH = 8;
259518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
259618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiPnoNetwork {
2597dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        String SSID;
25982a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        int rssi_threshold; // TODO remove
2599dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int flags;
2600dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int auth;
2601dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        String configKey; // kept for reference
2602dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2603dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        WifiPnoNetwork(WifiConfiguration config, int threshold) {
2604dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.SSID == null) {
2605dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                this.SSID = "";
260618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                this.flags = WIFI_PNO_FLAG_DIRECTED_SCAN;
2607dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
2608dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                this.SSID = config.SSID;
2609dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2610dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            this.rssi_threshold = threshold;
2611dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
261218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_PSK;
2613dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) ||
2614dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
261518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_EAPOL;
2616dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.wepKeys[0] != null) {
261718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_OPEN;
2618dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
261918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                auth |= WIFI_PNO_AUTH_CODE_OPEN;
2620dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
262118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
262218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            flags |= WIFI_PNO_FLAG_A_BAND | WIFI_PNO_FLAG_G_BAND;
2623dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            configKey = config.configKey();
2624dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2625dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2626dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        @Override
2627dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        public String toString() {
2628dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2629dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(this.SSID);
2630dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" flags=").append(this.flags);
2631dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" rssi=").append(this.rssi_threshold);
2632dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" auth=").append(this.auth);
2633dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return sbuf.toString();
2634dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2635dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2636dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2637dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    public static interface WifiPnoEventHandler {
2638dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        void onPnoNetworkFound(ScanResult results[]);
2639dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2640dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2641dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static WifiPnoEventHandler sWifiPnoEventHandler;
2642dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2643dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static int sPnoCmdId = 0;
2644dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2645dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]);
2646dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
264718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setPnoList(WifiPnoNetwork list[],
2648dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                                  WifiPnoEventHandler eventHandler) {
2649dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2650dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2651956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
265271d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2653dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2654f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2655dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2656f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sWifiPnoEventHandler = eventHandler;
2657f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                if (setPnoListNative(sWlan0Index, sPnoCmdId, list)) {
2658f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                    return true;
2659f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                }
2660dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2661dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2662f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            sWifiPnoEventHandler = null;
2663f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            return false;
2664dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2665dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2666dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
266718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
26683571366ac36c70746b9f013ec2b54482861c9292Randy Pan    private static void onPnoNetworkFound(int id, ScanResult[] results, int[] beaconCaps) {
2669dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2670dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2671dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2672dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2673dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2674dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2675dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
267618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiPnoEventHandler handler = sWifiPnoEventHandler;
267718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (sPnoCmdId != 0 && handler != null) {
267818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            for (int i=0; i<results.length; i++) {
267918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
268018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        + " " + results[i].level + " " + results[i].frequency);
2681dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
26823571366ac36c70746b9f013ec2b54482861c9292Randy Pan                populateScanResult(results[i], results[i].bytes, beaconCaps[i],
26833571366ac36c70746b9f013ec2b54482861c9292Randy Pan                                    "onPnoNetworkFound ");
268418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2685dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
268618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills
268718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onPnoNetworkFound(results);
268818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        } else {
268918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            /* this can happen because of race conditions */
269018786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            Log.d(TAG, "Ignoring Pno Network found event");
2691dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2692d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2693d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
26942a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
269518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public static class WifiLazyRoamParams {
2696d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_threshold;
2697d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_threshold;
2698d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_factor;
2699d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_factor;
2700d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_max_boost;
2701d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int lazy_roam_hysteresis;
2702d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int alert_roam_rssi_trigger;
2703dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2704d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        WifiLazyRoamParams() {
2705d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2706d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
2707d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        @Override
2708d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        public String toString() {
2709d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2710d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_threshold=").append(this.A_band_boost_threshold);
2711d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_threshold=").append(this.A_band_penalty_threshold);
2712d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_factor=").append(this.A_band_boost_factor);
2713d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_factor=").append(this.A_band_penalty_factor);
2714d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_max_boost=").append(this.A_band_max_boost);
2715d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" lazy_roam_hysteresis=").append(this.lazy_roam_hysteresis);
2716d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" alert_roam_rssi_trigger=").append(this.alert_roam_rssi_trigger);
2717d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            return sbuf.toString();
2718d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2719d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2720d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
27212a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
272218786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setLazyRoam(boolean enabled, WifiLazyRoamParams params) {
27232a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        return false;
2724d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
27259ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
27269ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
27279ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
27289ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
272918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setBssidBlacklist(String list[]) {
27309ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
27319ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
27329ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
27339ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
27349ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
27359ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2736956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
273771d0be16c79791a005d60f9d5fa7d2d81e1f8c80xinhe            if (isHalStarted()) {
2738f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                sPnoCmdId = getNewCmdIdLocked();
2739f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande                return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list);
2740f49a59bda006b13e0118d144e0a4a5f569b2251eVinit Deshpande            } else {
27419ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
27429ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
27439ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
27449ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
27459ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
27462a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills    // TODO remove
274718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public boolean setSsidWhitelist(String list[]) {
27482a6d76f0899289cd3b96e3428f02076fdbc0363eMitchell Wills        return false;
27495caa43b34e0445ff08bff5931b84ffbc850b2a66Pierre Vandwalle    }
2750c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2751c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int startSendingOffloadedPacketNative(int iface, int idx,
2752c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                                    byte[] srcMac, byte[] dstMac, byte[] pktData, int period);
2753c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
275418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2755c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) {
2756c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period);
27573bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham
27583bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        String[] macAddrStr = getMacAddress().split(":");
27593bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        byte[] srcMac = new byte[6];
27603bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        for(int i = 0; i < 6; i++) {
27613bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            Integer hexVal = Integer.parseInt(macAddrStr[i], 16);
27623bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham            srcMac[i] = hexVal.byteValue();
27633bd22cebc41ed0786d0e7bd2970a634a8bb0093ePrerepa Viswanadham        }
2764956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2765c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2766c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac,
276718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills                        keepAlivePacket.dstMac, keepAlivePacket.data, period);
2768c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2769c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2770c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2771c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2772c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2773c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
2774c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    private native static int stopSendingOffloadedPacketNative(int iface, int idx);
2775c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham
277618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int
2777c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    stopSendingOffloadedPacket(int slot) {
2778c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        Log.d(TAG, "stopSendingOffloadedPacket " + slot);
2779956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2780c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            if (isHalStarted()) {
2781c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return stopSendingOffloadedPacketNative(sWlan0Index, slot);
2782c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            } else {
2783c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham                return -1;
2784c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham            }
2785c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham        }
2786c55e88163b223db0ca6a99ed6ffe91845c30a576Prerepa Viswanadham    }
2787aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2788aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    public static interface WifiRssiEventHandler {
2789aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        void onRssiThresholdBreached(byte curRssi);
2790aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2791aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2792aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static WifiRssiEventHandler sWifiRssiEventHandler;
2793aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
279418786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    // Callback from native
279518786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    private static void onRssiThresholdBreached(int id, byte curRssi) {
279618786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        WifiRssiEventHandler handler = sWifiRssiEventHandler;
279718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        if (handler != null) {
279818786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            handler.onRssiThresholdBreached(curRssi);
279918786eca942042388748b0d98979f21c9dff4a89Mitchell Wills        }
2800aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2801aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2802aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int startRssiMonitoringNative(int iface, int id,
2803aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                        byte maxRssi, byte minRssi);
2804aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2805aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private static int sRssiMonitorCmdId = 0;
2806aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
280718786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int startRssiMonitoring(byte maxRssi, byte minRssi,
2808aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                                                WifiRssiEventHandler rssiEventHandler) {
2809aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi);
2810956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
281118786eca942042388748b0d98979f21c9dff4a89Mitchell Wills            sWifiRssiEventHandler = rssiEventHandler;
2812aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2813ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2814ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    stopRssiMonitoring();
2815ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2816ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills
2817aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                sRssiMonitorCmdId = getNewCmdIdLocked();
2818aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId);
2819ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId,
2820ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                        maxRssi, minRssi);
2821ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (ret != 0) { // if not success
2822ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    sRssiMonitorCmdId = 0;
2823ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2824ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2825aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2826aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2827aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2828aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2829aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
2830aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
2831aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    private native static int stopRssiMonitoringNative(int iface, int idx);
2832aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham
283318786eca942042388748b0d98979f21c9dff4a89Mitchell Wills    public int stopRssiMonitoring() {
2834aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId);
2835956f54b391677d78379729dd14518edddf3c7660Etan Cohen        synchronized (sLock) {
2836aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            if (isHalStarted()) {
2837ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                int ret = 0;
2838ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                if (sRssiMonitorCmdId != 0) {
2839ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                    ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId);
2840ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                }
2841ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                sRssiMonitorCmdId = 0;
2842ed34692d4c4e202748d41f90db397f1cc3dc8320Mitchell Wills                return ret;
2843aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            } else {
2844aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham                return -1;
2845aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham            }
2846aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham        }
2847aed5cb65d8eefb2eee56a29e33ac3cd8b03312e7Prerepa Viswanadham    }
28485ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham
28495ea42964ba17901a8d724736b450ace6ed48880fPrerepa Viswanadham    private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface);
2850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
2851