WifiNative.java revision 1814928371a30b11af31e2bbe5210c4337ed16f3
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;
22aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalleimport android.net.wifi.WifiLinkLayerStats;
23e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
24155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
25155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
26155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
27f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
28155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
29155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
30155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
31155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
32155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
33155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
35155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
39155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
40155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
41155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
46155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
47155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
48ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean DBG = false;
49155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final String mTAG;
50155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
51155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
52155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
53155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
54155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
55155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
56155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int SCAN_WITHOUT_CONNECTION_SETUP          = 1;
57155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int SCAN_WITH_CONNECTION_SETUP             = 2;
58155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
59155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // Hold this lock before calling supplicant - it is required to
60155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // mutually exclude access from Wifi and P2p state machines
61155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final Object mLock = new Object();
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
63155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public final String mInterfaceName;
64155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public final String mInterfacePrefix;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean mSuspendOptEnabled = false;
67155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
71155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
72155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
73155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
74155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
76155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean loadDriver();
79155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean isDriverLoaded();
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean unloadDriver();
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean startSupplicant(boolean p2pSupported);
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Sends a kill signal to supplicant. To be used when we have lost connection
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       or when the supplicant is hung */
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean killSupplicant(boolean p2pSupported);
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
90155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native boolean connectToSupplicantNative();
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native void closeSupplicantConnectionNative();
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Wait for the supplicant to send an event, returning the event string.
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the event string sent by the supplicant.
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native String waitForEventNative();
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native boolean doBooleanCommandNative(String command);
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native int doIntCommandNative(String command);
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native String doStringCommandNative(String command);
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiNative(String interfaceName) {
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!interfaceName.equals("p2p0")) {
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // commands for p2p0 interface don't need prefix
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
117ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
118ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
119ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = true;
120ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
121ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            DBG = false;
122ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
123ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
124ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final LocalLog mLocalLog = new LocalLog(1024);
126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // hold mLock before accessing mCmdIdLock
128b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sCmdId;
129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public LocalLog getLocalLog() {
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mLocalLog;
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
134b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int getNewCmdIdLocked() {
135b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        return sCmdId++;
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void localLog(String s) {
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mLocalLog != null)
140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog.log(mInterfaceName + ": " + s);
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        localLog(mInterfacePrefix + "connectToSupplicant");
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return connectToSupplicantNative();
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        localLog(mInterfacePrefix + "closeSupplicantConnection");
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        closeSupplicantConnectionNative();
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "->" + mInterfacePrefix + command);
164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "<-" + result);
1660888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (DBG) Log.d(mTAG, command + ": returned " + result);
167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "->" + mInterfacePrefix + command);
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "<-" + result);
178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
1840888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
1850888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
1860888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
1870888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
1880888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
1890888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "->" + mInterfacePrefix + command);
193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
19440ff222cec1bd05879edb53abc75c6deead734cavandwalle            if (result == null) {
19540ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
19640ff222cec1bd05879edb53abc75c6deead734cavandwalle            } else {
19740ff222cec1bd05879edb53abc75c6deead734cavandwalle                localLog(cmdId + "<-" + result);
19840ff222cec1bd05879edb53abc75c6deead734cavandwalle                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
19940ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
2050888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) {
2060888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            //GET_NETWORK commands flood the logs
2070888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (!command.startsWith("GET_NETWORK")) {
2080888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                Log.d(mTAG, "doString: [" + command + "]");
2090888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            }
21027355a942653264388e909a4276196ee63e57811vandwalle        }
21127355a942653264388e909a4276196ee63e57811vandwalle        synchronized (mLock) {
212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
221ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    public void setSupplicantLogLevel(String level) {
222ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle        doStringCommand("LOG_LEVEL " + level);
223ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    }
224ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
225a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
226a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
227a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
228a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
229a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public boolean scan(int type, String freqList) {
230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (type == SCAN_WITHOUT_CONNECTION_SETUP) {
231a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY");
232a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (type == SCAN_WITH_CONNECTION_SETUP) {
234a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN");
235a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN freq=" + freqList);
236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            throw new IllegalArgumentException("Invalid scan type");
238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
279ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
280ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
281ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
282ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
283ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
284ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
285f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
286f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
288ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
289ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + " disableOthers=" + disableOthers);
290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("SELECT_NETWORK " + netId);
292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("ENABLE_NETWORK " + netId);
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
298f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
303f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
308f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
31321bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        if (DBG) logDbg("DISCONNECT ");
314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("STATUS");
319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * bssid=68:7f:74:d7:1b:6e
336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * level=-43
338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * tsf=1344621975160944
339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * age=2623
340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ssid=zubyb
342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String scanResults(int sid) {
349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("BSS RANGE=" + sid + "- MASK=0x21987");
350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
353446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
354446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
355446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
356446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
357446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
358446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
359446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
360446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
361446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
362446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
363446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
364446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
365446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
366446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
367446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
368446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_config_methods=0x0
369446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
370446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
371446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
372446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
373446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
374446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
375446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
376446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
377446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
378446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
379446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
380446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
381446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
382446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
383446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
384446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
385446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    /**
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of command
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER WLS_BATCHING SET SCANFREQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * where x is an ascii representation of an integer number of seconds between scans
389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       r is an ascii representation of an integer number of scans per batch
390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       y is an ascii representation of an integer number of the max AP to remember per scan
391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       z, w, t represent a 1..n size list of channel numbers and/or 'A', 'B' values
392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *           indicating entire ranges of channels
393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       s is an ascii representation of an integer number of highest-strength AP
394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *           for which we'd like approximate distance reported
395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The return value is an ascii integer representing a guess of the number of scans
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * the firmware can remember before it runs out of buffer space or -1 on error
398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String setBatchedScanSettings(BatchedScanSettings settings) {
400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings == null) {
401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("DRIVER WLS_BATCHING STOP");
402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String cmd = "DRIVER WLS_BATCHING SET SCANFREQ=" + settings.scanIntervalSec;
404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        cmd += " MSCAN=" + settings.maxScansPerBatch;
405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.maxApPerScan != BatchedScanSettings.UNSPECIFIED) {
406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " BESTN=" + settings.maxApPerScan;
407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.channelSet != null && !settings.channelSet.isEmpty()) {
409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " CHANNEL=<";
410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int i = 0;
411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String channel : settings.channelSet) {
412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                cmd += (i > 0 ? "," : "") + channel;
413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++i;
414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += ">";
416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.maxApForDistance != BatchedScanSettings.UNSPECIFIED) {
418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " RTT=" + settings.maxApForDistance;
419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(cmd);
421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getBatchedScanResults() {
424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("DRIVER WLS_BATCHING GET");
425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getBand() {
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       String ret = doStringCommand("DRIVER GETBAND");
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //reply is "BAND X" where X is the band
500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" ");
501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (tokens.length == 2) return Integer.parseInt(tokens[1]);
503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (NumberFormatException e) {
504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return -1;
505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return -1;
508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER SETBAND " + band);
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5147ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
5157ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
5167ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
5177ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
5187ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
5197ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
5207ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
5217ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5420a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
5430a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
5440a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
5450a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle       // if (mSuspendOptEnabled == enabled) return true;
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSuspendOptEnabled = enabled;
562f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
563f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        Log.e("native", "do suspend " + enabled);
564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableBackgroundScan(boolean enable) {
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET pno 1");
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET pno 0");
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
583f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
584f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
585f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
586f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
587f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
588f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
589f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
590f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_DISCOVER " + macAddr);
598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_SETUP " + macAddr);
599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
707155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (lc >=1 && lc <= 11) {
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (lc != 0) {
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (oc >= 1 && oc <= 165 ) {
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_SET disallow_freq 1000-"
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (freq - 5) + "," + (freq + 5) + "-6000");
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (oc == 0) {
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_SET disallow_freq \"\"");
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
851155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
852155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
914155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
915155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
918155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
919155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
920155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
921155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
924155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
925155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
926155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
927155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
928155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
932155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
933155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
939155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
94436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress");
94536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
94636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
94736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
94836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        String status = doStringCommandNative("STATUS");
94936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        String result = "";
95036286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
95136286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        if (status != null) {
95236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            String[] tokens = status.split("\n");
95336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande            for (String token : tokens) {
95436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                if (token.startsWith("p2p_device_address=")) {
95536286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    String[] nameValue = token.split("=");
95636286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    if (nameValue.length != 2)
95736286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                        break;
95836286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                    result = nameValue[1];
95936286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande                }
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
96236286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande
96336286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
96436286b23c4e30f042ed753a670c2b462ebf13a48Vinit Deshpande        return result;
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
973155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
974155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
975155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
976155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
977155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
979155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1020155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1029155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
1030155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : servInfo.getSupplicantQueryList()) {
1032155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String command = "P2P_SERVICE_ADD";
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            command += (" " + s);
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand(command)) {
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : servInfo.getSupplicantQueryList()) {
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String command = "P2P_SERVICE_DEL ";
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] data = s.split(" ");
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (data.length < 2) {
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if ("upnp".equals(data[0])) {
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += s;
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if ("bonjour".equals(data[0])) {
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += data[0];
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += (" " + data[1]);
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand(command)) {
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
10933f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
1094446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1095446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1096446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1097446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
10987f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
10997f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1100b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static final String TAG = "WifiNative-HAL";
1101aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static long sWifiHalHandle = 0;  /* used by JNI to save wifi_handle */
1102aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static long[] sWifiIfaceHandles = null;  /* used by JNI to save interface handles */
1103aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sWlan0Index = -1;
1104aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static int sP2p0Index = -1;
1105aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle
1106aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    private static boolean sHalIsStarted = false;
11077f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1108b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startHalNative();
1109b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void stopHalNative();
1110b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native void waitForHalEventNative();
11117f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1112b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static class MonitorThread extends Thread {
11137ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1114b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
11157ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
11167ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
11177ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
11187ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
1119b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean startHal() {
1120b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        Log.i(TAG, "startHal");
1121aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
1122aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            if (sHalIsStarted)
1123aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1124aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            if (startHalNative()) {
112502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                getInterfaces();
1126aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                new MonitorThread().start();
1127aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                sHalIsStarted = true;
1128aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return true;
1129aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            } else {
1130b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                Log.i(TAG, "Could not start hal");
1131aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                sHalIsStarted = false;
1132aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return false;
1133aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            }
11347ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
11357ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
11367ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
1137b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void stopHal() {
11387ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        stopHalNative();
11397ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
11407f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1141b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native int getInterfacesNative();
11427f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1143b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static int getInterfaces() {
1144aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
114502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            if (sWifiIfaceHandles == null) {
114602a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                int num = getInterfacesNative();
114702a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                int wifi_num = 0;
114802a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                for (int i = 0; i < num; i++) {
114902a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    String name = getInterfaceNameNative(i);
115002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    Log.i(TAG, "interface[" + i + "] = " + name);
115102a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    if (name.equals("wlan0")) {
115202a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        sWlan0Index = i;
115302a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        wifi_num++;
115402a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    } else if (name.equals("p2p0")) {
115502a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        sP2p0Index = i;
115602a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                        wifi_num++;
115702a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                    }
1158aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                }
115902a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                return wifi_num;
116002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            } else {
116102a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande                return sWifiIfaceHandles.length;
1162e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1163e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
11647f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
11657f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1166b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native String getInterfaceNameNative(int index);
1167a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    synchronized public static String getInterfaceName(int index) {
1168a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        return getInterfaceNameNative(index);
11697f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
11707f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1171e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1172e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_cache_size;                 // in number of scan results??
1173e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1174e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1175e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1176e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_reporting_threshold;        // in number of scan results??
1177e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_hotlist_aps;
1178e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1179e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1180e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1181b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static boolean getScanCapabilities(ScanCapabilities capabilities) {
1182aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        return getScanCapabilitiesNative(sWlan0Index, capabilities);
1183e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1184e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1185b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean getScanCapabilitiesNative(
1186b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            int iface, ScanCapabilities capabilities);
1187e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1188b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1189b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean stopScanNative(int iface, int id);
1190b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native ScanResult[] getScanResultsNative(int iface, boolean flush);
1191b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
11927f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1193e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1194e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int frequency;
1195e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int dwell_time_ms;
1196e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        boolean passive;
11977f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
11987f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1199e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1200e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int bucket;
1201e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int band;
1202e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int period_ms;
1203e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int report_events;
1204e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_channels;
1205daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        ChannelSettings channels[];
1206e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
12077f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1208e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1209e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int base_period_ms;
1210e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int max_ap_per_scan;
1211e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int report_threshold;
1212e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_buckets;
1213daac2ad767f6047409987bb22812ab5f295e54dfVinit Deshpande        BucketSettings buckets[];
1214e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
12157f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1216b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface ScanEventHandler {
1217e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onScanResultsAvailable();
1218476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        void onFullScanResult(ScanResult fullScanResult);
1219b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        void onSingleScanComplete();
1220b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanPaused();
1221b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        void onScanRestarted();
1222e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1223e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1224b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized static void onScanResultsAvailable(int id) {
1225b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        if (sScanEventHandler  != null) {
1226b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            sScanEventHandler.onScanResultsAvailable();
1227b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
1228b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    }
1229b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
1230b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    /* scan status, keep these values in sync with gscan.h */
1231b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    private static int WIFI_SCAN_BUFFER_FULL = 0;
1232b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    private static int WIFI_SCAN_COMPLETE = 1;
1233b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
1234b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande    synchronized static void onScanStatus(int status) {
1235b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        Log.i(TAG, "Got a scan status changed event, status = " + status);
1236b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande
1237b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        if (status == WIFI_SCAN_BUFFER_FULL) {
1238b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            /* we have a separate event to take care of this */
1239b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        } else if (status == WIFI_SCAN_COMPLETE) {
1240b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            if (sScanEventHandler  != null) {
1241b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande                sScanEventHandler.onSingleScanComplete();
1242b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            }
1243b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
1244e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1245e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1246b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized static void onFullScanResult(int id, ScanResult result, byte bytes[]) {
1247b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " +
1248f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde                "num = " + bytes.length);
1249f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1250f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        int num = 0;
1251f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        for (int i = 0; i < bytes.length; ) {
1252f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            num++;
1253f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            int type  = (int) bytes[i] & 0xFF;
1254f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            int len = (int) bytes[i + 1] & 0xFF;
1255f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            if (len < 0) {
1256b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                Log.e(TAG, "bad length; returning");
1257f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde                return;
1258f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            }
1259f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            i += len + 2;
1260b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            if (DBG) Log.i(TAG, "bytes[" + i + "] = [" + type + ", " + len + "]" + ", " +
1261b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande                    "next = " + i);
1262f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1263f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1264476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        ScanResult.InformationElement elements[] = new ScanResult.InformationElement[num];
1265f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        for (int i = 0, index = 0; i < num; i++) {
1266f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            int type  = (int) bytes[index] & 0xFF;
1267f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            int len = (int) bytes[index + 1] & 0xFF;
1268b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            if (DBG) Log.i(TAG, "index = " + index + ", type = " + type + ", len = " + len);
1269476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande            ScanResult.InformationElement elem = new ScanResult.InformationElement();
1270f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            elem.id = type;
1271f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            elem.bytes = new byte[len];
1272f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            for (int j = 0; j < len; j++) {
1273f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde                elem.bytes[j] = bytes[index + j + 2];
1274f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            }
1275f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            elements[i] = elem;
1276f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde            index += (len + 2);
1277f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde        }
1278f1daf9342b66bf134d13fa0a42e929a008f1ca62Vinit Deshapnde
1279476bee2fef10d060c25c35858b1f7f60803d9f49Vinit Deshpande        result.informationElements = elements;
1280b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        if (sScanEventHandler  != null) {
1281b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande            sScanEventHandler.onFullScanResult(result);
1282b7cc309f06da8a18224057c21ba086f8550367d6Vinit Deshpande        }
12837f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
12847f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1285b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sScanCmdId = 0;
1286b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanEventHandler sScanEventHandler;
1287b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static ScanSettings sScanSettings;
12887f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1289b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean startScan(
1290b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            ScanSettings settings, ScanEventHandler eventHandler) {
12917f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
1292b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1293b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sScanCmdId != 0) {
1294741953368eafa247f2820496aaa521bc0e86e9e1Navtej Singh Mann                stopScan();
1295b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            } else if (sScanSettings != null || sScanEventHandler != null) {
1296b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                /* current scan is paused; no need to stop it */
1297b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
12987f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1299b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanCmdId = getNewCmdIdLocked();
1300e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1301b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanSettings = settings;
1302b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanEventHandler = eventHandler;
1303b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1304b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1305b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanEventHandler = null;
1306b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanSettings = null;
1307e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1308e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1309e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1310e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1311e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
13127f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
13137f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1314b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void stopScan() {
1315b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        synchronized (mLock) {
1316b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            stopScanNative(sWlan0Index, sScanCmdId);
1317b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanSettings = null;
1318b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanEventHandler = null;
1319b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sScanCmdId = 0;
1320b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1321b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1322b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1323b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void pauseScan() {
13247f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
1325b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1326b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                Log.d(TAG, "Pausing scan");
1327b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                stopScanNative(sWlan0Index, sScanCmdId);
1328b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanCmdId = 0;
1329b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanEventHandler.onScanPaused();
1330b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1331b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        }
1332b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    }
1333b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande
1334b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void restartScan() {
1335b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande        synchronized (mLock) {
1336b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
1337b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                Log.d(TAG, "Restarting scan");
1338b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                startScan(sScanSettings, sScanEventHandler);
1339b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sScanEventHandler.onScanRestarted();
1340b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            }
1341e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1342e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1343e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1344b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static ScanResult[] getScanResults() {
1345aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
1346aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            return getScanResultsNative(sWlan0Index, /* flush = */ false);
1347aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1348e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1349e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1350b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface HotlistEventHandler {
1351aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        void onHotlistApFound (ScanResult[]result);
1352e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1353e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1354b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sHotlistCmdId = 0;
1355b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static HotlistEventHandler sHotlistEventHandler;
1356e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1357b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean setHotlistNative(int iface, int id,
1358e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
1359b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private native static boolean resetHotlistNative(int iface, int id);
1360e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1361b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean setHotlist(WifiScanner.HotlistSettings settings,
1362aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                                    HotlistEventHandler eventHandler) {
1363e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1364b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sHotlistCmdId != 0) {
1365e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1366e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            } else {
1367b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistCmdId = getNewCmdIdLocked();
1368e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1369e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1370b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sHotlistEventHandler = eventHandler;
1371b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (setHotlistNative(sWlan0Index, sScanCmdId, settings) == false) {
1372b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistEventHandler = null;
1373e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1374e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1375e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1376e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1377e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1378e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1379e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1380b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void resetHotlist() {
1381e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1382b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sHotlistCmdId != 0) {
1383b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                resetHotlistNative(sWlan0Index, sHotlistCmdId);
1384b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistCmdId = 0;
1385b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sHotlistEventHandler = null;
1386e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
13877f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
13887f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
1389e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1390b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static void onHotlistApFound(int id, ScanResult[] results) {
1391aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
13921814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            if (sHotlistCmdId != 0) {
13931814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                sHotlistEventHandler.onHotlistApFound(results);
13941814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            } else {
13951814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                /* this can happen because of race conditions */
13961814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                Log.d(TAG, "Ignoring hotlist AP found change");
13971814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            }
1398aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1399e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1400e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1401b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    public static interface SignificantWifiChangeEventHandler {
1402e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
1403e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1404e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1405b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
1406b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static int sSignificantWifiChangeCmdId;
1407e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1408b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean trackSignificantWifiChangeNative(
1409e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
1410b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
1411e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1412b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static boolean trackSignificantWifiChange(
1413b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
1414e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1415b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sSignificantWifiChangeCmdId != 0) {
1416e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1417e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            } else {
1418b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sSignificantWifiChangeCmdId = getNewCmdIdLocked();
1419e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1420e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1421b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            sSignificantWifiChangeHandler = handler;
1422b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (trackSignificantWifiChangeNative(sWlan0Index, sScanCmdId, settings) == false) {
14231814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                sSignificantWifiChangeHandler = null;
1424e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1425e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1426e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1427e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1428e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1429e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1430e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1431b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized static void untrackSignificantWifiChange() {
1432e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1433b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande            if (sSignificantWifiChangeCmdId != 0) {
1434b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
1435b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sSignificantWifiChangeCmdId = 0;
1436b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande                sSignificantWifiChangeHandler = null;
1437e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1438e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1439e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1440e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1441b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized static void onSignificantWifiChange(int id, ScanResult[] results) {
1442aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
14431814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            if (sSignificantWifiChangeCmdId != 0) {
14441814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                sSignificantWifiChangeHandler.onChangesFound(results);
14451814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            } else {
14461814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                /* this can happen because of race conditions */
14471814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande                Log.d(TAG, "Ignoring significant wifi change");
14481814928371a30b11af31e2bbe5210c4337ed16f3Vinit Deshpande            }
1449aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1450e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1451e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1452b0b1d59786de7ff1fc98e31a16a7d710458bf549Vinit Deshpande    synchronized public static WifiLinkLayerStats getWifiLinkLayerStats() {
1453aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        synchronized (mLock) {
1454aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            if (!sHalIsStarted)
1455aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                startHal();
1456aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle            if (sHalIsStarted)
1457aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle                return getWifiLinkLayerStatsNative(sWlan0Index);
1458aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        }
1459aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle        return null;
1460aabe7a9f2f32915fd1f25416a6d2034a844005d6vandwalle    }
14615c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
14625c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    /*
14635c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales     * NFC-related calls
14645c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales     */
14655c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public String getNfcWpsConfigurationToken(int netId) {
14665c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
14675c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
14685c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
14695c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public String getNfcHandoverRequest() {
14705c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
14715c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
14725c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
14735c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public String getNfcHandoverSelect() {
14745c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
14755c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
14765c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
14775c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public boolean initiatorReportNfcHandover(String selectMessage) {
14785c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
14795c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
14805c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
14815c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    public boolean responderReportNfcHandover(String requestMessage) {
14825c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
14835c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales    }
14845c08cc119b92af69997af194cc8b6d0111e37d31Andres Morales
1485a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    public static native int getSupportedFeatureSetNative();
1486a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    synchronized public static int getSupportedFeatureSet() {
1487a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        return getSupportedFeatureSetNative();
1488a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
1489143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1490143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    /* Rtt related commands/events */
1491143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    public static interface RttEventHandler {
1492143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        void onRttResults(RttManager.RttResult[] result);
1493143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1494143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1495143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static RttEventHandler sRttEventHandler;
1496143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static int sRttCmdId;
1497143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1498143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    synchronized private static void onRttResults(int id, RttManager.RttResult[] results) {
1499143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        if (id == sRttCmdId) {
150002a1f98f2cecb8ae2d466d6f9fab06b473f970ddVinit Deshpande            Log.d(TAG, "Received " + results.length + " rtt results");
1501143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttEventHandler.onRttResults(results);
1502143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttCmdId = 0;
1503143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        } else {
1504143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            Log.d(TAG, "Received event for unknown cmd = " + id + ", current id = " + sRttCmdId);
1505143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
1506143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1507143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1508143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean requestRangeNative(
1509143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
1510143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    private static native boolean cancelRangeRequestNative(
1511143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            int iface, int id, RttManager.RttParams[] params);
1512143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1513143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    synchronized public static boolean requestRtt(
1514143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            RttManager.RttParams[] params, RttEventHandler handler) {
1515143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        synchronized (mLock) {
1516143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            if (sRttCmdId != 0) {
1517143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
1518143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
1519143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                sRttCmdId = getNewCmdIdLocked();
1520143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
1521143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            sRttEventHandler = handler;
1522143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            return requestRangeNative(sWlan0Index, sRttCmdId, params);
1523143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
1524143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1525143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1526143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    synchronized public static boolean cancelRtt(RttManager.RttParams[] params) {
1527143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        synchronized(mLock) {
1528143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            if (sRttCmdId == 0) {
1529143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
1530143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
1531143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande
1532143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
1533143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                sRttEventHandler = null;
1534143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return true;
1535143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            } else {
1536143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande                return false;
1537143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande            }
1538143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande        }
1539143657392bf0702a155fe688171a5abbf4c86570Vinit Deshpande    }
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
1541