WifiNative.java revision 9ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7
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
19155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.BatchedScanSettings;
20143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpandeimport android.net.wifi.RttManager;
21e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
22dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiConfiguration;
23aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
2403cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.WifiManager;
25e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
2612cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinheimport android.net.wifi.RttManager;
27dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport android.net.wifi.WifiSsid;
28155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
29155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
30155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
3103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidtimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
32f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
33155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
3403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinheimport android.util.Base64;
35155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
39155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
40155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
41155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
46155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
47155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
48155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
49155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
50155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
51155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
52155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
53ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean DBG = false;
54155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final String mTAG;
55155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
56155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
57155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
58155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
59155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
60155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
61155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int SCAN_WITHOUT_CONNECTION_SETUP          = 1;
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int SCAN_WITH_CONNECTION_SETUP             = 2;
63155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
64155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // Hold this lock before calling supplicant - it is required to
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // mutually exclude access from Wifi and P2p state machines
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final Object mLock = new Object();
67155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public final String mInterfaceName;
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public final String mInterfacePrefix;
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
71155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean mSuspendOptEnabled = false;
72155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
73243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe    private static final int EID_HT_OPERATION = 61;
74243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe    private static final int EID_VHT_OPERATION = 192;
75243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe    private static final int EID_EXTENDED_CAPS = 127;
76243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe    private static final int RTT_RESP_ENABLE_BIT = 70;
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
79155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean loadDriver();
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean isDriverLoaded();
90155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean unloadDriver();
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean startSupplicant(boolean p2pSupported);
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Sends a kill signal to supplicant. To be used when we have lost connection
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       or when the supplicant is hung */
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean killSupplicant(boolean p2pSupported);
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native boolean connectToSupplicantNative();
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native void closeSupplicantConnectionNative();
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Wait for the supplicant to send an event, returning the event string.
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the event string sent by the supplicant.
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native String waitForEventNative();
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native boolean doBooleanCommandNative(String command);
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native int doIntCommandNative(String command);
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native String doStringCommandNative(String command);
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiNative(String interfaceName) {
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!interfaceName.equals("p2p0")) {
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // commands for p2p0 interface don't need prefix
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
126ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
127ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
128ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
129ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
130ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
131ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
132ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
133ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final LocalLog mLocalLog = new LocalLog(1024);
135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // hold mLock before accessing mCmdIdLock
137b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sCmdId;
138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public LocalLog getLocalLog() {
140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mLocalLog;
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
143b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int getNewCmdIdLocked() {
144b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        return sCmdId++;
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void localLog(String s) {
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mLocalLog != null)
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog.log(mInterfaceName + ": " + s);
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        localLog(mInterfacePrefix + "connectToSupplicant");
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return connectToSupplicantNative();
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        localLog(mInterfacePrefix + "closeSupplicantConnection");
160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        closeSupplicantConnectionNative();
161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
1727b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
1747b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
1750888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
1847b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
1867b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog(toLog + " -> " + result);
187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
1930888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
1940888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
1950888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
1960888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
1970888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
1980888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
2017b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
20340ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
20440ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
20540ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
2067b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (!command.startsWith("STATUS-")) {
2077b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog(toLog + " -> " + result);
2087b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
20940ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
21040ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
2160888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2170888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2180888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2190888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2200888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
22127355a942653264388e909a4276196ee63e57811vandwalle        }
22227355a942653264388e909a4276196ee63e57811vandwalle        synchronized (mLock) {
223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
232ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
233ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
234ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
235ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
236a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
237a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
238a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
239a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
240a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public boolean scan(int type, String freqList) {
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (type == SCAN_WITHOUT_CONNECTION_SETUP) {
242a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY");
243a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (type == SCAN_WITH_CONNECTION_SETUP) {
245a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN");
246a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN freq=" + freqList);
247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            throw new IllegalArgumentException("Invalid scan type");
249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
266e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    public String listNetworks(int last_id) {
267e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
268e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande    }
269e3939cb40d9ba3842be105a6e85172dc06e14758Vinit Deshpande
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
290f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
291f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
292f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
293f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
294ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
295ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
296ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
297ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
298ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
299ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
300f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
301f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
303ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
304ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + " disableOthers=" + disableOthers);
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("SELECT_NETWORK " + netId);
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("ENABLE_NETWORK " + netId);
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
313f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3170047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    public boolean selectNetwork(int netId) {
3180047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
3190047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande        return doBooleanCommand("SELECT_NETWORK " + netId);
3200047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande    }
3210047ccf563baa288777e06c6fe95d3681fcf5ccdVinit Deshpande
322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
323f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
328f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
33321bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
33899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        return status(false);
339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
34199d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    public String status(boolean noEvents) {
34299d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        if (noEvents) {
34399d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS-NO_EVENTS");
34499d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        } else {
34599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle            return doStringCommand("STATUS");
34699d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        }
34799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle    }
34899d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle
349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
359a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
360a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * bssid=68:7f:74:d7:1b:6e
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * level=-43
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * tsf=1344621975160944
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * age=2623
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ssid=zubyb
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
37777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        1                       0     2
37877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
37977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         0                        0                       1     1   -> 9
38077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
38177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       1     9   -> d
38277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
38377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 1                         0                        0                       0     8
38477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
38577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * 0                         1                        1                       1     7
38677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
38777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
38877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
38977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *
39077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
39177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  wpa_supplicant_ctrl_iface_bss:4315
39277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist     *  print_bss_info
393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String scanResults(int sid) {
39577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        return doStringCommandWithoutLogging("BSS RANGE=" + sid + "- MASK=0x29d87");
39677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
39777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
39877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    public String doCustomCommand(String command) {
39977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        return doStringCommand(command);
400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
403446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
404446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
405446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
406446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
407446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
408446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
409446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
410446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
411446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
412446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
413446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
414446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
415446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
416446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
417446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
41856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle     * p2p_config_methods=0x0SET_NE
419446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
420446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
421446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
422446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
423446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
424446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
425446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
426446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
427446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
428446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
429446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
430446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
431446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
432446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
433446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
434446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
435446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    /**
436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of command
437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER WLS_BATCHING SET SCANFREQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s
438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * where x is an ascii representation of an integer number of seconds between scans
439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       r is an ascii representation of an integer number of scans per batch
440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       y is an ascii representation of an integer number of the max AP to remember per scan
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       z, w, t represent a 1..n size list of channel numbers and/or 'A', 'B' values
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *           indicating entire ranges of channels
443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       s is an ascii representation of an integer number of highest-strength AP
444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *           for which we'd like approximate distance reported
445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The return value is an ascii integer representing a guess of the number of scans
447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * the firmware can remember before it runs out of buffer space or -1 on error
448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String setBatchedScanSettings(BatchedScanSettings settings) {
450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings == null) {
451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("DRIVER WLS_BATCHING STOP");
452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String cmd = "DRIVER WLS_BATCHING SET SCANFREQ=" + settings.scanIntervalSec;
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        cmd += " MSCAN=" + settings.maxScansPerBatch;
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.maxApPerScan != BatchedScanSettings.UNSPECIFIED) {
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " BESTN=" + settings.maxApPerScan;
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.channelSet != null && !settings.channelSet.isEmpty()) {
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " CHANNEL=<";
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int i = 0;
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String channel : settings.channelSet) {
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                cmd += (i > 0 ? "," : "") + channel;
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++i;
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += ">";
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.maxApForDistance != BatchedScanSettings.UNSPECIFIED) {
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " RTT=" + settings.maxApForDistance;
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(cmd);
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getBatchedScanResults() {
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("DRIVER WLS_BATCHING GET");
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
54603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt    /**
54703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * Set the operational frequency band
54803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @param band One of
54903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
55003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
55103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
55203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     * @return {@code true} if the operation succeeded, {@code false} otherwise
55303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt     */
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
55503cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        String bandstr;
55603cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt
55703cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
55803cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "5G";
55903cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
56003cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "2G";
56103cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        else
56203cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt            bandstr = "AUTO";
56303cdd624f5da6d62cb731304aa7505921566f69dDmitry Shmidt        return doBooleanCommand("SET SETBAND " + bandstr);
564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5667ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
5677ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
5687ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
5697ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
5707ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
5717ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
5727ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
5737ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5940a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
5950a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
5960a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
5970a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
612f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle       // if (mSuspendOptEnabled == enabled) return true;
613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSuspendOptEnabled = enabled;
614f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
615f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        Log.e("native", "do suspend " + enabled);
616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
6240465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        if (countryCode != null)
6250465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
6260465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        else
6270465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            return doBooleanCommand("DRIVER COUNTRY");
628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableBackgroundScan(boolean enable) {
631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET pno 1");
633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET pno 0");
635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
638f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
639f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
640f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
641f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
642f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
643f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
644f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
645f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_DISCOVER " + macAddr);
653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_SETUP " + macAddr);
654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
707155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
73033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    public boolean setExternalSim(boolean external) {
73133b575ca6bee66183929f9474b5a161432918604Vinit Deshpande        synchronized (mLock) {
73233b575ca6bee66183929f9474b5a161432918604Vinit Deshpande            String value = external ? "1" : "0";
7334d701eca56d62586b0ab8af6ad864bac74a1dcd0Vinit Deshpande            Log.d(TAG, "Setting external_sim to " + value);
7344d701eca56d62586b0ab8af6ad864bac74a1dcd0Vinit Deshpande            return doBooleanCommand("SET external_sim " + value);
73533b575ca6bee66183929f9474b5a161432918604Vinit Deshpande        }
73633b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
73733b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
738f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot    public boolean simAuthResponse(int id, String type, String response) {
739f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot        // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS
74033b575ca6bee66183929f9474b5a161432918604Vinit Deshpande        synchronized (mLock) {
741f97140d51d14ce0659d381f443c08dbd94dfea28Honore Tricot            return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response);
74233b575ca6bee66183929f9474b5a161432918604Vinit Deshpande        }
74333b575ca6bee66183929f9474b5a161432918604Vinit Deshpande    }
74433b575ca6bee66183929f9474b5a161432918604Vinit Deshpande
745ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    public boolean simIdentityResponse(int id, String response) {
746ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot        synchronized (mLock) {
747ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot            return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
748ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot        }
749ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot    }
750ad607d99c372160c7d4b38e755e1b47d6419856eHonore Tricot
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
851155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
852155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (lc >=1 && lc <= 11) {
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (lc != 0) {
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (oc >= 1 && oc <= 165 ) {
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_SET disallow_freq 1000-"
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (freq - 5) + "," + (freq + 5) + "-6000");
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (oc == 0) {
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_SET disallow_freq \"\"");
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
914155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
915155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
918155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
919155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
920155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
921155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
924155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
925155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
926155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
927155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
928155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
932155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
933155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
939155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
949155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
950155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
951155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
957155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
958155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
959155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
963155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
964155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
973155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
974155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
975155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
976155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
977155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
979155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
102027f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
102136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
102236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
102327f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String status = null;
102427f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
102536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
102636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
102736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
102827f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        synchronized (mLock) {
102927f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande            status = doStringCommandNative("STATUS");
103027f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        }
103127f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande
103227f4b0c6ea9533e91863da48cefc80f8b5a88d1eVinit Deshpande        String result = "";
103336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
103436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
103536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
103636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
103736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
103836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
103936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
104036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
104136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
104436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
104536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
104636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : servInfo.getSupplicantQueryList()) {
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String command = "P2P_SERVICE_ADD";
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            command += (" " + s);
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand(command)) {
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : servInfo.getSupplicantQueryList()) {
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String command = "P2P_SERVICE_DEL ";
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] data = s.split(" ");
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (data.length < 2) {
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if ("upnp".equals(data[0])) {
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += s;
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if ("bonjour".equals(data[0])) {
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += data[0];
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += (" " + data[1]);
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand(command)) {
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
11753f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1176446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1177446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1178446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1179446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
11807f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
11817f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1182b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1183aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static long sWifiHalHandle = 0;  /* used by JNI to save wifi_handle */
1184aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static long[] sWifiIfaceHandles = null;  /* used by JNI to save interface handles */
1185aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sWlan0Index = -1;
1186aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sP2p0Index = -1;
1187aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle
1188aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static boolean sHalIsStarted = false;
1189cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande    private static boolean sHalFailed = false;
11907f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1191b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1192b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1193b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
11947f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1195b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
11967ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1197b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
11987ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
11997ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
12007ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
12017ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
1202b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean startHal() {
1203d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1204d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        String debugLog = "startHal stack: ";
1205d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace();
1206d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        for (int i = 2; i < elements.length && i <= 7; i++ ) {
1207d6307b404302949f6dadd14fa0860ff1aec432dcxinhe            debugLog = debugLog + " - " + elements[i].getMethodName();
1208d6307b404302949f6dadd14fa0860ff1aec432dcxinhe        }
1209d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1210d7cbebf7fa9081e294ecab38cfe82709f381e1c9Vinit Deshpande        if (DBG) mLocalLog.log(debugLog);
1211d6307b404302949f6dadd14fa0860ff1aec432dcxinhe
1212aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
1213cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande            if (sHalFailed)
1214cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande                return false;
1215cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1216f75cae779514830d18a3e69e03e5e4c6aa14d0b9xinhe                if(!sHalIsStarted){
1217f75cae779514830d18a3e69e03e5e4c6aa14d0b9xinhe                    new MonitorThread().start();
1218f75cae779514830d18a3e69e03e5e4c6aa14d0b9xinhe                }
1219aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                sHalIsStarted = true;
1220aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1221aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1222b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                Log.i(TAG, "Could not start hal");
1223aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                sHalIsStarted = false;
1224cce360ad854cabb238ba0d9290785c26e837749cVinit Deshpande                sHalFailed = true;
1225aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1226aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
12277ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
12287ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
12297ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
1230b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void stopHal() {
12317ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        stopHalNative();
12327ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
12337f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1234b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
12357f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1236b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static int getInterfaces() {
1237aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
123802a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            if (sWifiIfaceHandles == null) {
123902a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                int num = getInterfacesNative();
124002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                int wifi_num = 0;
124102a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                for (int i = 0; i < num; i++) {
124202a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    String name = getInterfaceNameNative(i);
124302a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    Log.i(TAG, "interface[" + i + "] = " + name);
124402a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    if (name.equals("wlan0")) {
124502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        sWlan0Index = i;
124602a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        wifi_num++;
124702a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    } else if (name.equals("p2p0")) {
124802a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        sP2p0Index = i;
124902a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        wifi_num++;
125002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1251aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
125202a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                return wifi_num;
125302a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            } else {
125402a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                return sWifiIfaceHandles.length;
1255e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1256e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
12577f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
12587f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1259b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
1260a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    synchronized public static String getInterfaceName(int index) {
1261a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        return getInterfaceNameNative(index);
12627f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
12637f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1264e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1265e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_cache_size;                 // in number of scan results??
1266e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1267e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1268e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1269e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_reporting_threshold;        // in number of scan results??
12707d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande        public int  max_hotlist_bssids;
1271e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1272e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1273e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1274b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static boolean getScanCapabilities(ScanCapabilities capabilities) {
1275aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        return getScanCapabilitiesNative(sWlan0Index, capabilities);
1276e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1277e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1278b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1279b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1280e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1281b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1282b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
128383a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1284b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
12857f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1286e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1287e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int frequency;
1288e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int dwell_time_ms;
1289e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        boolean passive;
12907f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
12917f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1292e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1293e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int bucket;
1294e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int band;
1295e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int period_ms;
1296e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int report_events;
1297e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_channels;
1298daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        ChannelSettings channels[];
1299e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
13007f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1301e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1302e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int base_period_ms;
1303e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int max_ap_per_scan;
130483a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        int report_threshold_percent;
130583a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        int report_threshold_num_scans;
1306e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_buckets;
1307daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        BucketSettings buckets[];
1308e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
13097f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1310b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
1311e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onScanResultsAvailable();
1312476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        void onFullScanResult(ScanResult fullScanResult);
131383a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanStatus();
131483a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande        void onScanPaused(WifiScanner.ScanData[] data);
1315b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1316e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1317e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1318b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized static void onScanResultsAvailable(int id) {
1319b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        if (sScanEventHandler  != null) {
1320b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            sScanEventHandler.onScanResultsAvailable();
1321b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
1322b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    }
1323b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
1324b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
1325b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    private static int WIFI_SCAN_BUFFER_FULL = 0;
1326b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    private static int WIFI_SCAN_COMPLETE = 1;
1327b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
1328b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    synchronized static void onScanStatus(int status) {
1329b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        Log.i(TAG, "Got a scan status changed event, status = " + status);
1330b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
1331b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        if (status == WIFI_SCAN_BUFFER_FULL) {
1332b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            /* we have a separate event to take care of this */
1333b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        } else if (status == WIFI_SCAN_COMPLETE) {
1334b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            if (sScanEventHandler  != null) {
133583a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                sScanEventHandler.onScanStatus();
1336b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            }
1337b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
1338e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1339e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1340dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    static void populateScanResult(ScanResult result, byte bytes[], String dbg) {
1341f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        int num = 0;
1342dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (bytes == null) return;
1343dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (dbg == null) dbg = "";
1344f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        for (int i = 0; i < bytes.length; ) {
1345e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande            int type  = bytes[i] & 0xFF;
1346e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande            int len = bytes[i + 1] & 0xFF;
1347e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande
1348e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande            if (i + len + 2 > bytes.length) {
1349dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                Log.w(TAG, dbg + "bad length " + len + " of IE " + type + " from " + result.BSSID);
1350dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                Log.w(TAG, dbg + "ignoring the rest of the IEs");
1351e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande                break;
1352f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            }
1353e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande            num++;
1354f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            i += len + 2;
1355dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (DBG) Log.i(TAG, dbg + "bytes[" + i + "] = [" + type + ", " + len + "]" + ", " +
1356b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande                    "next = " + i);
1357f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1358f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1359243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        int secondChanelOffset = 0;
1360243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        byte channelMode = 0;
1361243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        byte centerFreqIndex1 = 0;
1362243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        byte centerFreqIndex2 = 0;
1363243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        result.is80211McRTTResponder = false;
1364243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe
1365476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        ScanResult.InformationElement elements[] = new ScanResult.InformationElement[num];
1366f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        for (int i = 0, index = 0; i < num; i++) {
1367e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande            int type  = bytes[index] & 0xFF;
1368e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande            int len = bytes[index + 1] & 0xFF;
1369dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (DBG) Log.i(TAG, dbg + "index = " + index + ", type = " + type + ", len = " + len);
1370476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande            ScanResult.InformationElement elem = new ScanResult.InformationElement();
1371f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            elem.id = type;
1372f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            elem.bytes = new byte[len];
1373f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            for (int j = 0; j < len; j++) {
1374f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde                elem.bytes[j] = bytes[index + j + 2];
1375f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            }
1376f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            elements[i] = elem;
1377243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            int inforStart = index + 2;
1378f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            index += (len + 2);
1379243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe
1380243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            if(type == EID_HT_OPERATION) {
1381243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                secondChanelOffset = bytes[inforStart + 1] & 0x3;
1382243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            } else if(type == EID_VHT_OPERATION) {
1383243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                channelMode = bytes[inforStart];
1384243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                centerFreqIndex1 = bytes[inforStart + 1];
1385243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                centerFreqIndex2 = bytes[inforStart + 2];
1386243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            } else if (type == EID_EXTENDED_CAPS) {
1387dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                int tempIndex = RTT_RESP_ENABLE_BIT / 8;
1388dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                byte offset = RTT_RESP_ENABLE_BIT % 8;
1389dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1390dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                if(len < tempIndex + 1) {
1391dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    result.is80211McRTTResponder = false;
1392dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                } else {
1393dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    if ((bytes[inforStart + tempIndex] & ((byte)0x1 << offset)) != 0) {
1394dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        result.is80211McRTTResponder = true;
1395dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    } else {
1396dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        result.is80211McRTTResponder = false;
1397dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    }
1398dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                }
1399243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1400243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1401243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        //handle RTT related information
1402243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if (channelMode != 0) {
1403243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            // 80 or 160 MHz
1404243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.channelWidth = channelMode + 1;
1405243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe
1406243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            //convert channel index to frequency in MHz, channel 36 is 5180MHz
1407243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq0 = (centerFreqIndex1 - 36) * 5 + 5180;
1408243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe
1409243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            if(channelMode > 1) { //160MHz
1410243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                result.centerFreq1 = (centerFreqIndex2 - 36) * 5 + 5180;
1411243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            } else {
1412243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                result.centerFreq1 = 0;
1413243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1414243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        } else {
1415243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            //20 or 40 MHz
1416243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            if (secondChanelOffset != 0) {//40MHz
1417243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                result.channelWidth = 1;
1418243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                if (secondChanelOffset == 1) {
1419243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    result.centerFreq0 = result.frequency + 20;
1420243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                } else if (secondChanelOffset == 3) {
1421243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    result.centerFreq0 = result.frequency - 20;
1422243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                } else {
1423243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    result.centerFreq0 = 0;
1424dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    Log.e(TAG, dbg + ": Error on secondChanelOffset");
1425243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                }
1426243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            } else {
1427243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                result.centerFreq0  = 0;
1428243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                result.centerFreq1  = 0;
1429243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            }
1430243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe            result.centerFreq1  = 0;
1431243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        }
1432243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe        if(DBG) {
1433dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth +
1434243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    " PrimaryFreq: " + result.frequency +" mCenterfreq0: " + result.centerFreq0 +
1435243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    " mCenterfreq1: " + result.centerFreq1 + (result.is80211McRTTResponder ?
1436243931f3474f6235cfcf5c1a55fa2f192ee264aexinhe                    "Support RTT reponder: " : "Do not support RTT responder"));
1437f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1438f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1439476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1440dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1441dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1442dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    synchronized static void onFullScanResult(int id, ScanResult result, byte bytes[]) {
1443dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " +
1444dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                "num = " + bytes.length);
1445dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1446dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (sScanEventHandler == null) {
1447dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
1448dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
1449dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        populateScanResult(result, bytes, " onFullScanResult ");
1450dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1451e7b9e74a9a699e74881e5c98c684567763dfaa35Vinit Deshpande        sScanEventHandler.onFullScanResult(result);
14527f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
14537f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1454b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1455b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1456b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
14577f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1458b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean startScan(
1459b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            ScanSettings settings, ScanEventHandler eventHandler) {
14607f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
1461b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1462b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sScanCmdId != 0) {
1463741953368eafa247f2820496aaa521bc0e86e9e1Navtej Singh Mann                stopScan();
1464b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            } else if (sScanSettings != null || sScanEventHandler != null) {
1465b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1466b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
14677f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1468b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanCmdId = getNewCmdIdLocked();
1469e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1470b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanSettings = settings;
1471b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanEventHandler = eventHandler;
1472b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1473b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1474b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanEventHandler = null;
1475b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanSettings = null;
147683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                sScanCmdId = 0;
1477e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1478e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1479e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1480e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1481e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
14827f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
14837f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1484b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void stopScan() {
1485b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        synchronized (mLock) {
1486b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            stopScanNative(sWlan0Index, sScanCmdId);
1487b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanSettings = null;
1488b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanEventHandler = null;
1489b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanCmdId = 0;
1490b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1491b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1492b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1493b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void pauseScan() {
14947f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
1495b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1496b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                Log.d(TAG, "Pausing scan");
149783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1498b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                stopScanNative(sWlan0Index, sScanCmdId);
1499b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanCmdId = 0;
150083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                sScanEventHandler.onScanPaused(scanData);
1501b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1502b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1503b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1504b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1505b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void restartScan() {
1506b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        synchronized (mLock) {
1507b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
1508b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                Log.d(TAG, "Restarting scan");
150983a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                ScanEventHandler handler = sScanEventHandler;
151083a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                ScanSettings settings = sScanSettings;
151183a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                if (startScan(sScanSettings, sScanEventHandler)) {
151283a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    sScanEventHandler.onScanRestarted();
151383a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                } else {
151483a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    /* we are still paused; don't change state */
151583a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    sScanEventHandler = handler;
151683a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                    sScanSettings = settings;
151783a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande                }
1518b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1519e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1520e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1521e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
152283a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande    synchronized public static WifiScanner.ScanData[] getScanResults(boolean flush) {
1523aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
152483a674a18c84ff4f01377bbfd8988699dec93bc2Vinit Deshpande            return getScanResultsNative(sWlan0Index, flush);
1525aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1526e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1527e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1528b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
1529d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApFound (ScanResult[] result);
1530d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        void onHotlistApLost  (ScanResult[] result);
1531e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1532e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1533b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
1534b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
1535e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1536b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
1537e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
1538b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
1539e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1540b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean setHotlist(WifiScanner.HotlistSettings settings,
1541aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                                    HotlistEventHandler eventHandler) {
1542e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1543b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sHotlistCmdId != 0) {
1544e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1545e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            } else {
1546b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistCmdId = getNewCmdIdLocked();
1547e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1548e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1549b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sHotlistEventHandler = eventHandler;
1550b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (setHotlistNative(sWlan0Index, sScanCmdId, settings) == false) {
1551b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistEventHandler = null;
1552e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1553e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1554e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1555e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1556e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1557e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1558e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1559b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void resetHotlist() {
1560e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1561b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sHotlistCmdId != 0) {
1562b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                resetHotlistNative(sWlan0Index, sHotlistCmdId);
1563b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistCmdId = 0;
1564b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistEventHandler = null;
1565e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
15667f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
15677f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
1568e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1569b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void onHotlistApFound(int id, ScanResult[] results) {
1570aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
15711814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            if (sHotlistCmdId != 0) {
15721814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                sHotlistEventHandler.onHotlistApFound(results);
15731814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            } else {
15741814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                /* this can happen because of race conditions */
1575d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande                Log.d(TAG, "Ignoring hotlist AP found event");
1576d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande            }
1577d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        }
1578d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    }
1579d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande
1580d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande    synchronized public static void onHotlistApLost(int id, ScanResult[] results) {
1581d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande        synchronized (mLock) {
1582d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande            if (sHotlistCmdId != 0) {
1583d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande                sHotlistEventHandler.onHotlistApLost(results);
1584d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande            } else {
1585d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande                /* this can happen because of race conditions */
1586d4762401ec14be6bdd2d27aff2478ddbf8d6ce2aVinit Deshpande                Log.d(TAG, "Ignoring hotlist AP lost event");
15871814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            }
1588aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1589e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1590e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1591b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
1592e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
1593e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1594e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1595b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
1596b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
1597e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1598b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
1599e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
1600b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
1601e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1602b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean trackSignificantWifiChange(
1603b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
1604e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1605b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sSignificantWifiChangeCmdId != 0) {
1606e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1607e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            } else {
1608b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sSignificantWifiChangeCmdId = getNewCmdIdLocked();
1609e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1610e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1611b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sSignificantWifiChangeHandler = handler;
1612b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (trackSignificantWifiChangeNative(sWlan0Index, sScanCmdId, settings) == false) {
16131814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                sSignificantWifiChangeHandler = null;
1614e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1615e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1616e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1617e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1618e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1619e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1620e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1621b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized static void untrackSignificantWifiChange() {
1622e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1623b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sSignificantWifiChangeCmdId != 0) {
1624b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
1625b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sSignificantWifiChangeCmdId = 0;
1626b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sSignificantWifiChangeHandler = null;
1627e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1628e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1629e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1630e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1631b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized static void onSignificantWifiChange(int id, ScanResult[] results) {
1632aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
16331814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            if (sSignificantWifiChangeCmdId != 0) {
16341814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                sSignificantWifiChangeHandler.onChangesFound(results);
16351814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            } else {
16361814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                /* this can happen because of race conditions */
16371814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                Log.d(TAG, "Ignoring significant wifi change");
16381814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            }
1639aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1640e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1641e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1642200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    synchronized public static WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
1643200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        // TODO: use correct iface name to Index translation
1644200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (iface == null) return null;
1645aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
1646aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            if (!sHalIsStarted)
1647aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                startHal();
1648aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            if (sHalIsStarted)
1649aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
1650aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1651aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        return null;
1652aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    }
16535c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
16545c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    /*
16555c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales     * NFC-related calls
16565c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales     */
16575c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public String getNfcWpsConfigurationToken(int netId) {
16585c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
16595c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
16605c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
16615c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public String getNfcHandoverRequest() {
16625c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
16635c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
16645c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
16655c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public String getNfcHandoverSelect() {
16665c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
16675c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
16685c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
16695c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public boolean initiatorReportNfcHandover(String selectMessage) {
16705c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
16715c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
16725c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
16735c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public boolean responderReportNfcHandover(String requestMessage) {
16745c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
16755c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
16765c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
1677c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande    public static native int getSupportedFeatureSetNative(int iface);
1678a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    synchronized public static int getSupportedFeatureSet() {
1679c35361d54d4885c3174499e4ad46d3324387a9bbVinit Deshpande        return getSupportedFeatureSetNative(sWlan0Index);
1680a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
1681143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1682143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
1683143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
1684143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
1685143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1686143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1687143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
1688143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
1689143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1690143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    synchronized private static void onRttResults(int id, RttManager.RttResult[] results) {
1691143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        if (id == sRttCmdId) {
169202a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
1693143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttEventHandler.onRttResults(results);
1694143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
1695143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
1696f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe            Log.d(TAG, "RTT Received event for unknown cmd = " + id + ", current id = " + sRttCmdId);
1697143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
1698143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1699143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1700143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
1701143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
1702143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
1703143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
1704143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1705143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    synchronized public static boolean requestRtt(
1706143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
1707143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        synchronized (mLock) {
1708143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            if (sRttCmdId != 0) {
1709f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe                Log.v("TAG", "Last one is still under measurement!");
1710143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
1711143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
1712143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                sRttCmdId = getNewCmdIdLocked();
1713143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
1714143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttEventHandler = handler;
1715f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe            Log.v(TAG, "native issue RTT request");
1716143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            return requestRangeNative(sWlan0Index, sRttCmdId, params);
1717143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
1718143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1719143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1720143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    synchronized public static boolean cancelRtt(RttManager.RttParams[] params) {
1721143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        synchronized(mLock) {
1722143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            if (sRttCmdId == 0) {
1723143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
1724143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
1725143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1726f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe            sRttCmdId = 0;
1727f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe
1728143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
1729143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                sRttEventHandler = null;
1730f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe                Log.v(TAG, "Xin: RTT cancel Request Successfully");
1731143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return true;
1732143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
1733f95649f33db0a328cb4c0bb5e10c7075e6c828f8xinhe                Log.e(TAG, "Xin:RTT cancel Request failed");
1734143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
1735143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
1736143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
1737143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1738042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
1739042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
1740042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande
1741042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    synchronized public static boolean setScanningMacOui(byte[] oui) {
1742042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        synchronized (mLock) {
1743042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            if (startHal()) {
1744042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return setScanningMacOuiNative(sWlan0Index, oui);
1745042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            } else {
1746042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande                return false;
1747042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande            }
1748042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande        }
1749042c54bfd5144ce8e720585b2093796e7e28de5eVinit Deshpande    }
1750efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
1751efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    private static native int[] getChannelsForBandNative(
1752efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            int iface, int band);
1753efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande
1754efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    synchronized public static int [] getChannelsForBand(int band) {
1755efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        synchronized (mLock) {
1756efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            if (startHal()) {
1757efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande                return getChannelsForBandNative(sWlan0Index, band);
1758efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            } else {
1759efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande                return null;
1760efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande            }
1761efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande        }
1762efa77c1826499b0a3e57998bd6b3073b107e45d7Vinit Deshpande    }
17630465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
17640465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande
17650465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
17660465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    synchronized public static boolean setDfsFlag(boolean dfsOn) {
17670465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        synchronized (mLock) {
17680465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            if (startHal()) {
17690465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return setDfsFlagNative(sWlan0Index, dfsOn);
17700465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            } else {
17710465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande                return false;
17720465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande            }
17730465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande        }
17740465ff513cdccf7e883a505e14c5b78758e7e458Vinit Deshpande    }
1775b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
1776b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    private static native boolean toggleInterfaceNative(int on);
1777b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    synchronized public static boolean toggleInterface(int on) {
1778b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        synchronized (mLock) {
1779b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            if (startHal()) {
1780b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return toggleInterfaceNative(0);
1781b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            } else {
1782b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe
1783b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe                return false;
1784b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe            }
1785b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe        }
1786b830d76fbf5fa4077531b516066faa2fdbb92e81xinhe    }
178712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe
178812cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
178912cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    synchronized public static RttManager.RttCapabilities getRttCapabilities() {
179012cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        synchronized (mLock) {
179112cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            if (startHal()) {
179212cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return getRttCapabilitiesNative(sWlan0Index);
179312cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            } else {
179412cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe                return null;
179512cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe            }
179612cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe        }
179712cf388ecf3b2aa5ba66ed680b931fa356ab75b5xinhe    }
1798939177ff615062ec826601d536466875d8457375xinhe
1799939177ff615062ec826601d536466875d8457375xinhe    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
1800939177ff615062ec826601d536466875d8457375xinhe    synchronized public static boolean setCountryCodeHal( String CountryCode) {
1801939177ff615062ec826601d536466875d8457375xinhe        synchronized (mLock) {
1802939177ff615062ec826601d536466875d8457375xinhe            if (startHal()) {
1803939177ff615062ec826601d536466875d8457375xinhe                return setCountryCodeHalNative(sWlan0Index, CountryCode);
1804939177ff615062ec826601d536466875d8457375xinhe            } else {
1805939177ff615062ec826601d536466875d8457375xinhe                return false;
1806939177ff615062ec826601d536466875d8457375xinhe            }
1807939177ff615062ec826601d536466875d8457375xinhe        }
1808939177ff615062ec826601d536466875d8457375xinhe    }
1809939177ff615062ec826601d536466875d8457375xinhe
1810d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    /* Rtt related commands/events */
1811d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public abstract class TdlsEventHandler {
1812d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        abstract public void onTdlsStatus(String macAddr, int status, int reason);
1813d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
1814d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1815d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static TdlsEventHandler sTdlsEventHandler;
1816d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1817d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1818d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native boolean enableDisableTdlsNative(int iface, boolean enable,
1819d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            String macAddr);
1820d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    synchronized public static boolean enableDisableTdls(boolean enable, String macAdd,
1821d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            TdlsEventHandler tdlsCallBack) {
1822d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        synchronized (mLock) {
1823d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            if (startHal()) {
1824d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                sTdlsEventHandler = tdlsCallBack;
1825d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return enableDisableTdlsNative(sWlan0Index, enable, macAdd);
1826d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
1827d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return false;
1828d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
1829d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
1830d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
1831d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1832d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // Once TDLS per mac and event feature is implemented, this class definition should be
1833d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiManager etc
1834d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsStatus {
1835d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int channel;
1836d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int global_operating_class;
1837d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int state;
1838d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int reason;
1839d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
1840d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr);
1841d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    synchronized public static TdlsStatus getTdlsStatus (String macAdd) {
1842d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        synchronized (mLock) {
1843d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            if (startHal()) {
1844d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsStatusNative(sWlan0Index, macAdd);
1845d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
1846d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
1847d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
1848d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
1849d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
1850d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1851d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be
1852d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    // moved to the right place, like WifiStateMachine etc
1853d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    public static class TdlsCapabilities {
1854d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        /* Maximum TDLS session number can be supported by the Firmware and hardware */
1855d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        int maxConcurrentTdlsSessionNumber;
1856d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isGlobalTdlsSupported;
1857d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isPerMacTdlsSupported;
1858d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        boolean isOffChannelTdlsSupported;
1859d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
1860d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1861d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1862d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1863d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface);
1864d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    synchronized public static TdlsCapabilities getTdlsCapabilities () {
1865d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        synchronized (mLock) {
1866d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            if (startHal()) {
1867d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return getTdlsCapabilitiesNative(sWlan0Index);
1868d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            } else {
1869d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe                return null;
1870d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe            }
1871d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe        }
1872d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
1873d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1874d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    synchronized private static boolean onTdlsStatus(String macAddr, int status, int reason) {
1875d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe         if (sTdlsEventHandler == null) {
1876d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe             return false;
1877d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe         } else {
1878d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe             sTdlsEventHandler.onTdlsStatus(macAddr, status, reason);
1879d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe             return true;
1880d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe         }
1881d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe    }
1882d57f630f6a7a5fca872d2b96fc4cce1905daee5dxinhe
1883a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    //---------------------------------------------------------------------------------
1884a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
1885a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    /* Wifi Logger commands/events */
18867d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
188703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    public static native boolean startLogging(int iface);
18887d6301ead19afdf3de37455e9ed133c25b4938cdVinit Deshpande
1889a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    public static interface WifiLoggerEventHandler {
1890a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        void onDataAvailable(char data[], int len);
1891a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
1892a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
1893a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
1894a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle
189503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    public static class WifiLoggerEvent {
189603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        WifiLogger.RingBufferStatus status;
189703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        int entrySize;
189803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        int flags;
189903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        int type;
190003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        long timestamp;
190103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        byte[] entry;
190203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
190303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        public String toString() {
190403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            StringBuilder str =  new StringBuilder();
190503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            str.append(status + "\n entry size: " + entrySize + " flags: " + flags);
190603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            str.append(" type: " + type + " timestamp: " + timestamp +"\n");
190703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (entry != null) {
190803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                str.append(android.util.Base64.encodeToString(entry, Base64.DEFAULT));
190903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
191003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                str.append(" empty bytes[]");
191103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
191203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            str.append("\n\n");
191303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            return str.toString();
191403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
191503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
191603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
191703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiLoggerEvent(WifiLoggerEvent event) {
191803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        if (event != null) {
191903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            Log.d(TAG, "Logger Event:" + event);
192003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
192103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
192203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
192303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiAlert(byte[] buffer, int errorCode) {
192403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        Log.d(TAG, "Logger Alert error code:" + errorCode);
192503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        if (buffer != null) {
192603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            StringBuilder str =  new StringBuilder();
192703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            str.append(android.util.Base64.encodeToString(buffer, android.util.Base64.NO_WRAP));
192803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            Log.e(TAG,"Logger Alert event:");
192903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            Log.e(TAG, str.toString());
193003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        } else {
193103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            Log.e(TAG," empty Alert");
193203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
193303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
193403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
193503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel,
193603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int flags, int maxInterval,int minDataSize, String ringName);
193703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    synchronized public static boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval,
193803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            int minDataSize, String ringName){
193903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
194003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (startHal()) {
194103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval,
194203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                        minDataSize, ringName);
194303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
194403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
194503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
194603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
194703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
194803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
194903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native int getSupportedLoggerFeatureSetNative(int iface);
195003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    synchronized public static int getSupportedLoggerFeatureSet() {
195103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
195203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (startHal()) {
195303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getSupportedLoggerFeatureSetNative(sWlan0Index);
195403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
195503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return -1;
195603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
195703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
195803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
195903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
196003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getDriverVersionNative(int iface);
196103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    synchronized public static String getDriverVersion() {
196203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
196303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (startHal()) {
196403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getDriverVersionNative(sWlan0Index);
196503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
196603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
196703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
196803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
196903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
197003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
197103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
197203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native String getFirmwareVersionNative(int iface);
197303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    synchronized public static String getFirmwareVersion() {
197403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
197503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (startHal()) {
197603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getFirmwareVersionNative(sWlan0Index);
197703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
197803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
197903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
198003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
198103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
198203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
198303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native WifiLogger.RingBufferStatus[] getRingBufferStatusNative(int iface);
198403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    synchronized public static WifiLogger.RingBufferStatus[] getRingBufferStatus() {
198503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
198603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (startHal()) {
198703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferStatusNative(sWlan0Index);
198803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
198903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
199003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
199103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
199203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
199303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
199403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getRingBufferDataNative(int iface, String ringName);
199503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    synchronized public static boolean getRingBufferData(String ringName) {
199603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
199703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (startHal()) {
199803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return getRingBufferDataNative(sWlan0Index, ringName);
199903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
200003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return false;
200103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
200203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        }
200303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
200403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe
200503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    public static String mFwMemoryDump;
200603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static void onWifiFwMemoryAvailable(byte[] buffer) {
200703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        Log.d(TAG, "onWifiFwMemoryAvailable is called");
200803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        mFwMemoryDump =  android.util.Base64.encodeToString(buffer, Base64.DEFAULT);
200903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    }
201003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    private static native boolean getFwMemoryDumpNative(int iface);
201103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe    synchronized public static String getFwMemoryDump() {
201203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe        synchronized (mLock) {
201303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            if (startHal()) {
201403ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                if(getFwMemoryDumpNative(sWlan0Index)) {
201503ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    String tmp = mFwMemoryDump;
201603ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    mFwMemoryDump = null;
201703ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    return tmp;
201803ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                } else {
201903ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                    return null;
202003ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                }
202103ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            } else {
202203ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe                return null;
202303ba4a5b108ab2bbdb5d0e7be46ac45c00a6d9ffxinhe            }
2024a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle        }
2025a0d34d3626bd9631039a485d2d428b1b7de390e2Pierre Vandwalle    }
2026dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2027dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    //---------------------------------------------------------------------------------
2028dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    /* Configure ePNO */
2029dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2030dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    public class WifiPnoNetwork {
2031dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        String SSID;
2032dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int rssi_threshold;
2033dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int flags;
2034dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int auth;
2035dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        String configKey; // kept for reference
2036dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2037dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        WifiPnoNetwork(WifiConfiguration config, int threshold) {
2038dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.SSID == null) {
2039dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                this.SSID = "";
2040dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                this.flags = 1;
2041dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
2042dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                this.SSID = config.SSID;
2043dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2044dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            this.rssi_threshold = threshold;
2045dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
2046dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                auth |= 2;
2047dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) ||
2048dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
2049dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                auth |= 4;
2050dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else if (config.wepKeys[0] != null) {
2051dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                auth |= 1;
2052dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
2053dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                auth |= 1;
2054dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2055dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle//            auth = 0;
2056dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            flags |= 6; //A and G
2057dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            configKey = config.configKey();
2058dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2059dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2060dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        @Override
2061dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        public String toString() {
2062dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2063dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(this.SSID);
2064dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" flags=").append(this.flags);
2065dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" rssi=").append(this.rssi_threshold);
2066dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sbuf.append(" auth=").append(this.auth);
2067dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return sbuf.toString();
2068dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2069dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2070dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2071dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    public static interface WifiPnoEventHandler {
2072dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        void onPnoNetworkFound(ScanResult results[]);
2073dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2074dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2075dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static WifiPnoEventHandler sWifiPnoEventHandler;
2076dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2077dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private static int sPnoCmdId = 0;
2078dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2079dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]);
2080dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2081dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    synchronized public static boolean setPnoList(WifiPnoNetwork list[],
2082dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                                  WifiPnoEventHandler eventHandler) {
2083dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.e(TAG, "setPnoList cmd " + sPnoCmdId);
2084dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2085dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        synchronized (mLock) {
2086dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2087dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sPnoCmdId = getNewCmdIdLocked();
2088dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2089dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            sWifiPnoEventHandler = eventHandler;
2090dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (setPnoListNative(sWlan0Index, sPnoCmdId, list) == false) {
2091dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                sWifiPnoEventHandler = null;
2092dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                return false;
2093dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2094dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2095dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return true;
2096dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2097dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2098dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2099dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    synchronized public static void onPnoNetworkFound(int id, ScanResult[] results) {
2100dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2101dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (results == null) {
2102dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound null results");
2103dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return;
2104dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2105dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2106dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length);
2107dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2108dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        //Log.e(TAG, "onPnoNetworkFound length " + results.length);
2109dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        //return;
2110dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        for (int i=0; i<results.length; i++) {
2111dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID
2112dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    + " " + results[i].level + " " + results[i].frequency);
2113dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2114dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            populateScanResult(results[i], results[i].bytes, "onPnoNetworkFound ");
2115dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID);
2116dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2117dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        synchronized (mLock) {
2118dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (sPnoCmdId != 0 && sWifiPnoEventHandler != null) {
2119dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                sWifiPnoEventHandler.onPnoNetworkFound(results);
2120dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            } else {
2121dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                /* this can happen because of race conditions */
2122dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                Log.d(TAG, "Ignoring Pno Network found event");
2123dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
2124dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2125d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2126d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
2127d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    public class WifiLazyRoamParams {
2128d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_threshold;
2129d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_threshold;
2130d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_boost_factor;
2131d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_penalty_factor;
2132d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int A_band_max_boost;
2133d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int lazy_roam_hysteresis;
2134d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        int alert_roam_rssi_trigger;
2135dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
2136d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        WifiLazyRoamParams() {
2137d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2138d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
2139d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        @Override
2140d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        public String toString() {
2141d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            StringBuilder sbuf = new StringBuilder();
2142d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_threshold=").append(this.A_band_boost_threshold);
2143d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_threshold=").append(this.A_band_penalty_threshold);
2144d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_boost_factor=").append(this.A_band_boost_factor);
2145d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_penalty_factor=").append(this.A_band_penalty_factor);
2146d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" A_band_max_boost=").append(this.A_band_max_boost);
2147d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" lazy_roam_hysteresis=").append(this.lazy_roam_hysteresis);
2148d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            sbuf.append(" alert_roam_rssi_trigger=").append(this.alert_roam_rssi_trigger);
2149d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            return sbuf.toString();
2150d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2151d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
2152d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
21539ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setLazyRoamNative(int iface, int id,
2154d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle                                              boolean enabled, WifiLazyRoamParams param);
2155d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
2156d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    synchronized public static boolean setLazyRoam(boolean enabled, WifiLazyRoamParams params) {
2157d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        synchronized (mLock) {
2158d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            if (startHal()) {
2159d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle                sPnoCmdId = getNewCmdIdLocked();
2160d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle
21619ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return setLazyRoamNative(sWlan0Index, sPnoCmdId, enabled, params);
2162d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            } else {
2163d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle                return false;
2164d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle            }
2165d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        }
2166d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle    }
21679ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
21689ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    private native static boolean setBssidBlacklistNative(int iface, int id,
21699ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                                              String list[]);
21709ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
21719ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    synchronized public static boolean setBssidBlacklist(String list[]) {
21729ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        int size = 0;
21739ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        if (list != null) {
21749ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            size = list.length;
21759ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
21769ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size);
21779ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
21789ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        synchronized (mLock) {
21799ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            sPnoCmdId = getNewCmdIdLocked();
21809ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
21819ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            if (setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list) == false) {
21829ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                return false;
21839ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
21849ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            return true;
21859ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        }
21869ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle    }
21879ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
2188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
2189