WifiNative.java revision 446db2d5457456743e4476029e14d7c3bb9f5bcc
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;
20e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.ScanResult;
21e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapndeimport android.net.wifi.WifiScanner;
22155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
23155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pConfig;
24155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.WifiP2pGroup;
25f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
26155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
27155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
28155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
29155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
30155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
31155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
32155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
33155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.Locale;
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
35155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Native calls for bring up/shut down of the supplicant daemon and for
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * sending requests to the supplicant daemon
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
39155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * waitForEvent() is called on the monitor thread for events. All other methods
40155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * must be serialized from the framework.
41155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * {@hide}
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic class WifiNative {
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
46155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final boolean DBG = false;
47155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final String mTAG;
48155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
49155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
50155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
51155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
52155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
53155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
54155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int SCAN_WITHOUT_CONNECTION_SETUP          = 1;
55155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final int SCAN_WITH_CONNECTION_SETUP             = 2;
56155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
57155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // Hold this lock before calling supplicant - it is required to
58155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // mutually exclude access from Wifi and P2p state machines
59155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final Object mLock = new Object();
60155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
61155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public final String mInterfaceName;
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public final String mInterfacePrefix;
63155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
64155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean mSuspendOptEnabled = false;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Register native functions */
67155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static {
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Native functions are defined in libwifi-service.so */
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        System.loadLibrary("wifi-service");
71155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerNatives();
72155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
73155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
74155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static native int registerNatives();
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
76155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean loadDriver();
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean isDriverLoaded();
79155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean unloadDriver();
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean startSupplicant(boolean p2pSupported);
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Sends a kill signal to supplicant. To be used when we have lost connection
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       or when the supplicant is hung */
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public native static boolean killSupplicant(boolean p2pSupported);
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native boolean connectToSupplicantNative();
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
90155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native void closeSupplicantConnectionNative();
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Wait for the supplicant to send an event, returning the event string.
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the event string sent by the supplicant.
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native String waitForEventNative();
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native boolean doBooleanCommandNative(String command);
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native int doIntCommandNative(String command);
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private native String doStringCommandNative(String command);
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiNative(String interfaceName) {
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName = interfaceName;
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTAG = "WifiNative-" + interfaceName;
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!interfaceName.equals("p2p0")) {
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // commands for p2p0 interface don't need prefix
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mInterfacePrefix = "";
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final LocalLog mLocalLog = new LocalLog(1024);
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // hold mLock before accessing mCmdIdLock
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mCmdId;
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public LocalLog getLocalLog() {
121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mLocalLog;
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int getNewCmdIdLocked() {
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mCmdId++;
126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void localLog(String s) {
129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mLocalLog != null)
130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog.log(mInterfaceName + ": " + s);
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean connectToSupplicant() {
134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        localLog(mInterfacePrefix + "connectToSupplicant");
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return connectToSupplicantNative();
137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void closeSupplicantConnection() {
140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        localLog(mInterfacePrefix + "closeSupplicantConnection");
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        closeSupplicantConnectionNative();
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String waitForEvent() {
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // No synchronization necessary .. it is implemented in WifiMonitor
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return waitForEventNative();
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean doBooleanCommand(String command) {
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doBoolean: " + command);
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "->" + mInterfacePrefix + command);
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "<-" + result);
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int doIntCommand(String command) {
162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doInt: " + command);
163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "->" + mInterfacePrefix + command);
166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int result = doIntCommandNative(mInterfacePrefix + command);
167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "<-" + result);
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommand(String command) {
174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doString: " + command);
175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int cmdId = getNewCmdIdLocked();
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "->" + mInterfacePrefix + command);
178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String result = doStringCommandNative(mInterfacePrefix + command);
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            localLog(cmdId + "<-" + result);
180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(mTAG, "   returned " + result);
181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return result;
182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String doStringCommandWithoutLogging(String command) {
186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "doString: " + command);
187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommandNative(mInterfacePrefix + command);
189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean ping() {
193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String pong = doStringCommand("PING");
194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return (pong != null && pong.equals("PONG"));
195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
197a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public String getFreqCapability() {
198a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        return doStringCommand("GET_CAPABILITY freq");
199a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
200a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
201a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public boolean scan(int type, String freqList) {
202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (type == SCAN_WITHOUT_CONNECTION_SETUP) {
203a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY");
204a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (type == SCAN_WITH_CONNECTION_SETUP) {
206a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (freqList == null) return doBooleanCommand("SCAN");
207a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            else return doBooleanCommand("SCAN freq=" + freqList);
208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            throw new IllegalArgumentException("Invalid scan type");
210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Note that underneath we use a harsh-sounding "terminate" supplicant command
216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * for a graceful stop and a mild-sounding "stop" interface
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to kill the process
218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopSupplicant() {
220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("TERMINATE");
221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String listNetworks() {
224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("LIST_NETWORKS");
225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addNetwork() {
228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doIntCommand("ADD_NETWORK");
229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setNetworkVariable(int netId, String name, String value) {
232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getNetworkVariable(int netId, String name) {
237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(name)) return null;
238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // GET_NETWORK will likely flood the logs ...
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REMOVE_NETWORK " + netId);
245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
247f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
248f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void logDbg(String debug) {
249f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        long now = SystemClock.elapsedRealtimeNanos();
250f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String ts = String.format("[%,d us] ", now/1000);
251ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        Log.e("WifiNative: ", ts+debug+ " stack:"
252ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
253ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
254ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
255ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
256ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + Thread.currentThread().getStackTrace()[6].getMethodName());
257f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
258f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
260ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
261ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                + " disableOthers=" + disableOthers);
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("SELECT_NETWORK " + netId);
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("ENABLE_NETWORK " + netId);
266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISABLE_NETWORK " + netId);
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reconnect() {
275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("RECONNECT");
277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean reassociate() {
280f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("REASSOCIATE ");
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("REASSOCIATE");
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disconnect() {
285f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) logDbg("RECONNECT ");
286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DISCONNECT");
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String status() {
290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("STATUS");
291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getMacAddress() {
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //Macaddr = XX.XX.XX.XX.XX.XX
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ret = doStringCommand("DRIVER MACADDR");
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" = ");
298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (tokens.length == 2) return tokens[1];
299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of results:
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * =================
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * id=1
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * bssid=68:7f:74:d7:1b:6e
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * freq=2412
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * level=-43
310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * tsf=1344621975160944
311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * age=2623
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * flags=[WPA2-PSK-CCMP][WPS][ESS]
313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ssid=zubyb
314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * ====
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ALL gets all scan results
317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RANGE=ID- gets results from ID
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String scanResults(int sid) {
321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("BSS RANGE=" + sid + "- MASK=0x21987");
322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
325446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * Format of result:
326446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * id=1016
327446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * bssid=00:03:7f:40:84:10
328446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * freq=2462
329446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * beacon_int=200
330446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * capabilities=0x0431
331446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * qual=0
332446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * noise=0
333446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * level=-46
334446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * tsf=0000002669008476
335446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * age=5
336446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
337446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
338446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * ssid=QCA-HS20-R2-TEST
339446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_device_name=
340446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * p2p_config_methods=0x0
341446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
342446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_network_auth_type=010000
343446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_roaming_consortium=03506f9a05001bc504bd
344446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_ip_addr_type_availability=0c
345446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
346446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_3gpp=000600040132f465
347446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * anqp_domain_name=0b65786d61706c652e636f6d
348446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
349446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_wan_metrics=01c40900008001000000000a00
350446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
351446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
352446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng     */
353446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public String scanResult(String bssid) {
354446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doStringCommand("BSS " + bssid);
355446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
356446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
357446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    /**
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Format of command
359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER WLS_BATCHING SET SCANFREQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s
360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * where x is an ascii representation of an integer number of seconds between scans
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       r is an ascii representation of an integer number of scans per batch
362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       y is an ascii representation of an integer number of the max AP to remember per scan
363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       z, w, t represent a 1..n size list of channel numbers and/or 'A', 'B' values
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *           indicating entire ranges of channels
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *       s is an ascii representation of an integer number of highest-strength AP
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *           for which we'd like approximate distance reported
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The return value is an ascii integer representing a guess of the number of scans
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * the firmware can remember before it runs out of buffer space or -1 on error
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String setBatchedScanSettings(BatchedScanSettings settings) {
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings == null) {
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("DRIVER WLS_BATCHING STOP");
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String cmd = "DRIVER WLS_BATCHING SET SCANFREQ=" + settings.scanIntervalSec;
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        cmd += " MSCAN=" + settings.maxScansPerBatch;
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.maxApPerScan != BatchedScanSettings.UNSPECIFIED) {
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " BESTN=" + settings.maxApPerScan;
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.channelSet != null && !settings.channelSet.isEmpty()) {
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " CHANNEL=<";
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int i = 0;
383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String channel : settings.channelSet) {
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                cmd += (i > 0 ? "," : "") + channel;
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++i;
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += ">";
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (settings.maxApForDistance != BatchedScanSettings.UNSPECIFIED) {
390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            cmd += " RTT=" + settings.maxApForDistance;
391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(cmd);
393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getBatchedScanResults() {
396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("DRIVER WLS_BATCHING GET");
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startDriver() {
400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER START");
401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopDriver() {
404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER STOP");
405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V4 packets
410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Multicast filtering rules work as follows:
413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a power optimized mode (typically when screen goes off).
416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-ADD Num
421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and DRIVER RXFILTER-START
424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In order to stop the usage of these rules, we do
425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-STOP
427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * DRIVER RXFILTER-REMOVE Num
428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *   where Num is as described for RXFILTER-ADD
429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The  SETSUSPENDOPT driver command overrides the filtering rules
431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV4Packets() {
433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V4 packets.
440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV4Packets() {
443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start filtering out Multicast V6 packets
450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startFilteringMulticastV6Packets() {
453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Stop filtering out Multicast V6 packets.
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded, {@code false} otherwise
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean stopFilteringMulticastV6Packets() {
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER RXFILTER-STOP")
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            && doBooleanCommand("DRIVER RXFILTER-START");
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getBand() {
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       String ret = doStringCommand("DRIVER GETBAND");
470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ret)) {
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //reply is "BAND X" where X is the band
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] tokens = ret.split(" ");
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (tokens.length == 2) return Integer.parseInt(tokens[1]);
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (NumberFormatException e) {
476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return -1;
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return -1;
480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBand(int band) {
483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER SETBAND " + band);
484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4867ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    /**
4877ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * Sets the bluetooth coexistence mode.
4887ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *
4897ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
4907ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
4917ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
4927ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      * @return Whether the mode was successfully set.
4937ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde      */
494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceMode(int mode) {
495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * some of the low-level scan parameters used by the driver are changed to
501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * reduce interference with A2DP streaming.
502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param isSet whether to enable or disable this mode
504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the command succeeded, {@code false} otherwise.
505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (setCoexScanMode) {
508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5140a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    public void enableSaveConfig() {
5150a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde        doBooleanCommand("SET update_config 1");
5160a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde    }
5170a696d168d7ad98ab5084d2a16e3d02c545a85aaVinit Deshapnde
518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfig() {
519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SAVE_CONFIG");
520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean addToBlacklist(String bssid) {
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) return false;
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST " + bssid);
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean clearBlacklist() {
528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("BLACKLIST clear");
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSuspendOptimizations(boolean enabled) {
532f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle       // if (mSuspendOptEnabled == enabled) return true;
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSuspendOptEnabled = enabled;
534f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
535f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        Log.e("native", "do suspend " + enabled);
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setCountryCode(String countryCode) {
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableBackgroundScan(boolean enable) {
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET pno 1");
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET pno 0");
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
555f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void enableAutoConnect(boolean enable) {
556f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (enable) {
557f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 1");
558f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
559f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            doBooleanCommand("STA_AUTOCONNECT 0");
560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
562f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setScanInterval(int scanInterval) {
564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startTdls(String macAddr, boolean enable) {
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable) {
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_DISCOVER " + macAddr);
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_SETUP " + macAddr);
571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example output:
577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * RSSI=-65
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * LINKSPEED=48
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NOISE=9999
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * FREQUENCY=0
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String signalPoll() {
583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommandWithoutLogging("SIGNAL_POLL");
584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Example outout:
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXGOOD=396
588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TXBAD=1
589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String pktcntPoll() {
591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("PKTCNT_POLL");
592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void bssFlush() {
595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("BSS_FLUSH 0");
596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String bssid) {
599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC");
601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("WPS_PBC " + bssid);
603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPbc(String iface, String bssid) {
607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String pin) {
617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_PIN any " + pin);
619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsPinKeypad(String iface, String pin) {
622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(pin)) return false;
623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String bssid) {
630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid)) {
631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN any");
632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doStringCommand("WPS_PIN " + bssid);
634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String startWpsPinDisplay(String iface, String bssid) {
638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(bssid)) {
640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Configures an access point connection */
648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean startWpsRegistrar(String bssid, String pin) {
649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean cancelWps() {
654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WPS_CANCEL");
655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setPersistentReconnect(boolean enabled) {
658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int value = (enabled == true) ? 1 : 0;
659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET persistent_reconnect " + value);
660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceName(String name) {
663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_name " + name);
664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setDeviceType(String type) {
667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET device_type " + type);
668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConfigMethods(String cfg) {
671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET config_methods " + cfg);
672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setManufacturer(String value) {
675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET manufacturer " + value);
676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelName(String value) {
679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_name " + value);
680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setModelNumber(String value) {
683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET model_number " + value);
684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setSerialNumber(String value) {
687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET serial_number " + value);
688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pSsidPostfix(String postfix) {
691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pGroupIdle(String iface, int time) {
695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setPowerSave(boolean enabled) {
701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enabled) {
702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 1");
703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            doBooleanCommand("SET ps 0");
705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
707155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setP2pPowerSave(String iface, boolean enabled) {
709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabled) {
711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdEnable(boolean enable) {
719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setWfdDeviceInfo(String hex) {
723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * P2P connection over STA
729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean setConcurrencyPriority(String s) {
731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SET conc_pref " + s);
732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind() {
735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND");
736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFind(int timeout) {
739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pFind();
741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FIND " + timeout);
743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pStopFind() {
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       return doBooleanCommand("P2P_STOP_FIND");
747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen() {
750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN");
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pListen(int timeout) {
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (timeout <= 0) {
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return p2pListen();
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_LISTEN " + timeout);
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pExtListen(boolean enable, int period, int interval) {
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (enable && interval < period) {
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_EXT_LISTEN"
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (enable ? (" " + period + " " + interval) : ""));
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pSetChannel(int lc, int oc) {
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (lc >=1 && lc <= 11) {
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (lc != 0) {
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (oc >= 1 && oc <= 165 ) {
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_SET disallow_freq 1000-"
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + (freq - 5) + "," + (freq + 5) + "-6000");
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else if (oc == 0) {
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_SET disallow_freq \"\"");
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pFlush() {
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_FLUSH");
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return null;
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<String> args = new ArrayList<String>();
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsInfo wps = config.wps;
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        args.add(config.deviceAddress);
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (wps.setup) {
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("pbc");
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (TextUtils.isEmpty(wps.pin)) {
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add("pin");
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    args.add(wps.pin);
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("display");
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("keypad");
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.LABEL:
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add(wps.pin);
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                args.add("label");
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("persistent");
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (joinExistingGroup) {
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("join");
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //TODO: This can be adapted based on device plugged in state and
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            //device battery state
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int groupOwnerIntent = config.groupOwnerIntent;
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            args.add("go_intent=" + groupOwnerIntent);
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_CONNECT ";
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : args) command += s + " ";
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pCancelConnect() {
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_CANCEL");
850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
851155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
852155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null) return false;
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch (config.wps.setup) {
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.PBC:
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.DISPLAY:
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing display, so provision discovery is keypad
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WpsInfo.KEYPAD:
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //We are doing keypad, so provision discovery is display
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(boolean persistent) {
871155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (persistent) {
872155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_GROUP_ADD persistent");
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD");
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupAdd(int netId) {
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pGroupRemove(String iface) {
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(iface)) return false;
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLock) {
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReject(String deviceAddress) {
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_REJECT " + deviceAddress);
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Invite a peer to a group */
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return false;
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (group == null) {
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Reinvoke a persistent connection */
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pReinvoke(int netId, String deviceAddress) {
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetSsid(String deviceAddress) {
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return p2pGetParam(deviceAddress, "oper_ssid");
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
914155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
915155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pGetDeviceAddress() {
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String status = status();
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (status == null) return "";
918155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
919155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = status.split("\n");
920155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
921155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("p2p_device_address=")) {
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
924155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
925155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
926155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
927155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return "";
928155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getGroupCapability(String deviceAddress) {
931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int gc = 0;
932155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(deviceAddress)) return gc;
933155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(peerInfo)) return gc;
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens = peerInfo.split("\n");
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith("group_capab=")) {
939155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return Integer.decode(nameValue[1]);
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch(NumberFormatException e) {
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return gc;
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return gc;
949155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
950155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
951155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pPeer(String deviceAddress) {
952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand("P2P_PEER " + deviceAddress);
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String p2pGetParam(String deviceAddress, String key) {
956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (deviceAddress == null) return null;
957155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
958155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String peerInfo = p2pPeer(deviceAddress);
959155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (peerInfo == null) return null;
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] tokens= peerInfo.split("\n");
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        key += "=";
963155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String token : tokens) {
964155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (token.startsWith(key)) {
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String[] nameValue = token.split("=");
966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (nameValue.length != 2) break;
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return nameValue[1];
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
973155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
974155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
975155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
976155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp <version hex> <service>
977155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * e.g)
979155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [Bonjour]
980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         *
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * [UPnP]
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:device:InternetGatewayDevice:1
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * -org:service:ContentDirectory:2
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : servInfo.getSupplicantQueryList()) {
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String command = "P2P_SERVICE_ADD";
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            command += (" " + s);
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand(command)) {
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL bonjour <query hexdump>
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * P2P_SERVICE_DEL upnp <version hex> <service>
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String s : servInfo.getSupplicantQueryList()) {
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String command = "P2P_SERVICE_DEL ";
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] data = s.split(" ");
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (data.length < 2) {
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if ("upnp".equals(data[0])) {
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += s;
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if ("bonjour".equals(data[0])) {
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += data[0];
1020155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                command += (" " + data[1]);
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!doBooleanCommand(command)) {
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
1029155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1030155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServiceFlush() {
1032155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERVICE_FLUSH");
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String p2pServDiscReq(String addr, String query) {
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String command = "P2P_SERV_DISC_REQ";
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + addr);
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        command += (" " + query);
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doStringCommand(command);
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean p2pServDiscCancelReq(String id) {
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Set the current mode of miracast operation.
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  0 = disabled
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  1 = operating as source
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *  2 = operating as sink
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setMiracastMode(int mode) {
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Note: optional feature on the driver. It is ok for this to fail.
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        doBooleanCommand("DRIVER MIRACAST " + mode);
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
10563f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
10573f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    public String getNfcWpsConfigurationToken(int netId) {
10583f7ef65ab71619040032aee96b5599849881d6fdAndres Morales        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
10593f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    }
10607ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
1061446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    public boolean fetchAnqp(String bssid, String subtypes) {
1062446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1063446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng    }
1064446db2d5457456743e4476029e14d7c3bb9f5bccYuhao Zheng
10657ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
10667f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    /* WIFI HAL support */
10677f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
10687f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    private long mWifiHalHandle;                        /* used by JNI to save wifi_handle */
10697f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    private long[] mWifiIfaceHandles;                   /* used by JNI to save interface handles */
1070e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private int mWlan0Index;
1071e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private int mP2p0Index;
10727f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1073e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native boolean startHalNative();
1074e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native void stopHalNative();
1075e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native void waitForHalEventNative();
10767f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
10777ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    private class MonitorThread extends Thread {
10787ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        public void run() {
1079e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            Log.i(mTAG, "Waiting for HAL events");
10807ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            waitForHalEventNative();
10817ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
10827ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
10837ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
1084e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public boolean startHal() {
10857ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        if (startHalNative()) {
10867ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            new MonitorThread().start();
1087e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
10887ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        } else {
10897ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde            Log.i(mTAG, "Could not start hal");
1090e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return false;
10917ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        }
10927ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
10937ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde
10947ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    public void stopHal() {
10957ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde        stopHalNative();
10967ef73dd1b6e43c72b3841723504cd86dc402a134Vinit Deshapnde    }
10977f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
10987f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    private native int getInterfacesNative();
10997f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
11007f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    public int getInterfaces() {
1101e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num = getInterfacesNative();
1102e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        for (int i = 0; i < num; i++) {
1103e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            String name = getInterfaceNameNative(i);
1104e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            Log.i(mTAG, "interface[" + i + "] = " + name);
1105e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (name.equals("wlan0")) {
1106e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mWlan0Index = i;
1107e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            } else if (name.equals("p2p0")) {
1108e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mP2p0Index = i;
1109e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1110e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1111e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        return num;
11127f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
11137f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
11147f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    private native String getInterfaceNameNative(int index);
11157f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
11167f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    public void printInterfaceNames() {
11177f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        for (int i = 0; i < mWifiIfaceHandles.length; i++) {
11187f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde            String name = getInterfaceNameNative(i);
11197f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde            Log.i(mTAG, "interface[" + i + "] = " + name);
11207f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
11217f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
11227f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1123e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanCapabilities {
1124e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_cache_size;                 // in number of scan results??
1125e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_buckets;
1126e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_ap_cache_per_scan;
1127e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_rssi_sample_size;
1128e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_scan_reporting_threshold;        // in number of scan results??
1129e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_hotlist_aps;
1130e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        public int  max_significant_wifi_change_aps;
1131e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1132e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1133e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public boolean getScanCapabilities(ScanCapabilities capabilities) {
1134e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        return getScanCapabilitiesNative(mWlan0Index, capabilities);
1135e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1136e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1137e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native boolean getScanCapabilitiesNative(int iface, ScanCapabilities capabilities);
1138e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1139e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native boolean startScanNative(int iface, int id, ScanSettings settings);
11407f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    private native boolean stopScanNative(int iface, int id);
1141e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native ScanResult[] getScanResultsNative(int iface, boolean flush);
11427f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1143e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ChannelSettings {
1144e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int frequency;
1145e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int dwell_time_ms;
1146e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        boolean passive;
11477f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
11487f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1149e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class BucketSettings {
1150e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int bucket;
1151e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int band;
1152e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int period_ms;
1153e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int report_events;
1154e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_channels;
1155e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        ChannelSettings channels[] = new ChannelSettings[8];
1156e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
11577f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1158e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public static class ScanSettings {
1159e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int base_period_ms;
1160e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int max_ap_per_scan;
1161e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int report_threshold;
1162e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        int num_buckets;
1163e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        BucketSettings buckets[] = new BucketSettings[8];
1164e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
11657f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1166e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public interface ScanEventHandler {
1167e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onScanResultsAvailable();
1168e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onFullScanResult(ScanResult result, WifiScanner.InformationElement elems[]);
1169e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1170e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1171e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    void onScanResultsAvailable(int id) {
1172e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        mScanEventHandler.onScanResultsAvailable();
1173e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1174e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1175e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    void onFullScanResult(int id, ScanResult result, WifiScanner.InformationElement elems[]) {
1176e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        mScanEventHandler.onFullScanResult(result, elems);
11777f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
11787f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
11797f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    private int mScanCmdId = 0;
1180e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private ScanEventHandler mScanEventHandler;
11817f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1182e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) {
11837f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
11847f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde            if (mScanCmdId != 0) {
11857f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde                return false;
11867f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde            } else {
11877f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde                mScanCmdId = getNewCmdIdLocked();
11887f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde            }
11897f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
1190e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            mScanEventHandler = eventHandler;
1191e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1192e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (startScanNative(mWlan0Index, mScanCmdId, settings) == false) {
1193e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mScanEventHandler = null;
1194e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1195e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1196e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1197e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1198e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
11997f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
12007f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde
12017f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    public void stopScan() {
12027f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        synchronized (mLock) {
1203e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            stopScanNative(mWlan0Index, mScanCmdId);
1204e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            mScanEventHandler = null;
1205e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            mScanCmdId = 0;
1206e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1207e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1208e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1209e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public ScanResult[] getScanResults() {
1210e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        return getScanResultsNative(mWlan0Index, /* flush = */ false);
1211e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1212e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1213e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public interface HotlistEventHandler {
1214e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onHotlistApFound(ScanResult[] result);
1215e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1216e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1217e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private int mHotlistCmdId = 0;
1218e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private HotlistEventHandler mHotlistEventHandler;
1219e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1220e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native boolean setHotlistNative(int iface, int id,
1221e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            WifiScanner.HotlistSettings settings);
1222e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native boolean resetHotlistNative(int iface, int id);
1223e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1224e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    boolean setHotlist(WifiScanner.HotlistSettings settings, HotlistEventHandler eventHandler) {
1225e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1226e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (mHotlistCmdId != 0) {
1227e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1228e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            } else {
1229e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mHotlistCmdId = getNewCmdIdLocked();
1230e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1231e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1232e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            mHotlistEventHandler = eventHandler;
1233e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (setHotlistNative(mWlan0Index, mScanCmdId, settings) == false) {
1234e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mHotlistEventHandler = null;
1235e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1236e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1237e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1238e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1239e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1240e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1241e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1242e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    void resetHotlist() {
1243e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1244e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (mHotlistCmdId != 0) {
1245e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                resetHotlistNative(mWlan0Index, mHotlistCmdId);
1246e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mHotlistCmdId = 0;
1247e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mHotlistEventHandler = null;
1248e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
12497f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde        }
12507f9a15d554f69311a0db43347d6473a7c4c46e2eVinit Deshapnde    }
1251e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1252e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    void onHotlistApFound(int id, ScanResult[] results) {
1253e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        mHotlistEventHandler.onHotlistApFound(results);
1254e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1255e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1256e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    public interface SignificantWifiChangeEventHandler {
1257e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        void onChangesFound(ScanResult[] result);
1258e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1259e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1260e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    SignificantWifiChangeEventHandler mSignificantWifiChangeHandler;
1261e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    int mSignificantWifiChangeCmdId;
1262e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1263e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native boolean trackSignificantWifiChangeNative(
1264e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            int iface, int id, WifiScanner.WifiChangeSettings settings);
1265e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    private native boolean untrackSignificantWifiChangeNative(int iface, int id);
1266e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1267e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    boolean trackSignificantWifiChange(WifiScanner.WifiChangeSettings settings,
1268e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                                       SignificantWifiChangeEventHandler handler) {
1269e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1270e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (mSignificantWifiChangeCmdId != 0) {
1271e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1272e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            } else {
1273e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mSignificantWifiChangeCmdId = getNewCmdIdLocked();
1274e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1275e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1276e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            mSignificantWifiChangeHandler = handler;
1277e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (trackSignificantWifiChangeNative(mWlan0Index, mScanCmdId, settings) == false) {
1278e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mHotlistEventHandler = null;
1279e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                return false;
1280e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1281e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1282e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            return true;
1283e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1284e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1285e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1286e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    void untrackSignificantWifiChange() {
1287e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        synchronized (mLock) {
1288e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            if (mSignificantWifiChangeCmdId != 0) {
1289e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                untrackSignificantWifiChangeNative(mWlan0Index, mSignificantWifiChangeCmdId);
1290e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mSignificantWifiChangeCmdId = 0;
1291e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde                mSignificantWifiChangeHandler = null;
1292e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde            }
1293e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        }
1294e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1295e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1296e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    void onSignificantWifiChange(int id, ScanResult[] results) {
1297e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde        mSignificantWifiChangeHandler.onChangesFound(results);
1298e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde    }
1299e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1300e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1301e4e3750390bec0a849a9153348b7c21b2cc8b843Vinit Deshapnde
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
1303