WifiNative.java revision 77f2b82a2e80af8da52c22d69a76def6d4209757
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wifi;
18
19import android.net.wifi.BatchedScanSettings;
20import android.net.wifi.RttManager;
21import android.net.wifi.ScanResult;
22import android.net.wifi.WifiLinkLayerStats;
23import android.net.wifi.WifiManager;
24import android.net.wifi.WifiScanner;
25import android.net.wifi.WpsInfo;
26import android.net.wifi.p2p.WifiP2pConfig;
27import android.net.wifi.p2p.WifiP2pGroup;
28import android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
29import android.os.SystemClock;
30import android.text.TextUtils;
31import android.util.LocalLog;
32import android.util.Log;
33
34import java.util.ArrayList;
35import java.util.List;
36import java.util.Locale;
37
38/**
39 * Native calls for bring up/shut down of the supplicant daemon and for
40 * sending requests to the supplicant daemon
41 *
42 * waitForEvent() is called on the monitor thread for events. All other methods
43 * must be serialized from the framework.
44 *
45 * {@hide}
46 */
47public class WifiNative {
48
49    private static boolean DBG = false;
50    private final String mTAG;
51    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
52
53    static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
54    static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
55    static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
56
57    static final int SCAN_WITHOUT_CONNECTION_SETUP          = 1;
58    static final int SCAN_WITH_CONNECTION_SETUP             = 2;
59
60    // Hold this lock before calling supplicant - it is required to
61    // mutually exclude access from Wifi and P2p state machines
62    static final Object mLock = new Object();
63
64    public final String mInterfaceName;
65    public final String mInterfacePrefix;
66
67    private boolean mSuspendOptEnabled = false;
68
69    /* Register native functions */
70
71    static {
72        /* Native functions are defined in libwifi-service.so */
73        System.loadLibrary("wifi-service");
74        registerNatives();
75    }
76
77    private static native int registerNatives();
78
79    public native static boolean loadDriver();
80
81    public native static boolean isDriverLoaded();
82
83    public native static boolean unloadDriver();
84
85    public native static boolean startSupplicant(boolean p2pSupported);
86
87    /* Sends a kill signal to supplicant. To be used when we have lost connection
88       or when the supplicant is hung */
89    public native static boolean killSupplicant(boolean p2pSupported);
90
91    private native boolean connectToSupplicantNative();
92
93    private native void closeSupplicantConnectionNative();
94
95    /**
96     * Wait for the supplicant to send an event, returning the event string.
97     * @return the event string sent by the supplicant.
98     */
99    private native String waitForEventNative();
100
101    private native boolean doBooleanCommandNative(String command);
102
103    private native int doIntCommandNative(String command);
104
105    private native String doStringCommandNative(String command);
106
107    public WifiNative(String interfaceName) {
108        mInterfaceName = interfaceName;
109        mTAG = "WifiNative-" + interfaceName;
110        if (!interfaceName.equals("p2p0")) {
111            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
112        } else {
113            // commands for p2p0 interface don't need prefix
114            mInterfacePrefix = "";
115        }
116    }
117
118    void enableVerboseLogging(int verbose) {
119        if (verbose > 0) {
120            DBG = true;
121        } else {
122            DBG = false;
123        }
124    }
125
126    private static final LocalLog mLocalLog = new LocalLog(1024);
127
128    // hold mLock before accessing mCmdIdLock
129    private static int sCmdId;
130
131    public LocalLog getLocalLog() {
132        return mLocalLog;
133    }
134
135    private static int getNewCmdIdLocked() {
136        return sCmdId++;
137    }
138
139    private void localLog(String s) {
140        if (mLocalLog != null)
141            mLocalLog.log(mInterfaceName + ": " + s);
142    }
143
144    public boolean connectToSupplicant() {
145        // No synchronization necessary .. it is implemented in WifiMonitor
146        localLog(mInterfacePrefix + "connectToSupplicant");
147        return connectToSupplicantNative();
148    }
149
150    public void closeSupplicantConnection() {
151        localLog(mInterfacePrefix + "closeSupplicantConnection");
152        closeSupplicantConnectionNative();
153    }
154
155    public String waitForEvent() {
156        // No synchronization necessary .. it is implemented in WifiMonitor
157        return waitForEventNative();
158    }
159
160    private boolean doBooleanCommand(String command) {
161        if (DBG) Log.d(mTAG, "doBoolean: " + command);
162        synchronized (mLock) {
163            int cmdId = getNewCmdIdLocked();
164            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
165            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
166            localLog(toLog + " -> " + result);
167            if (DBG) Log.d(mTAG, command + ": returned " + result);
168            return result;
169        }
170    }
171
172    private int doIntCommand(String command) {
173        if (DBG) Log.d(mTAG, "doInt: " + command);
174        synchronized (mLock) {
175            int cmdId = getNewCmdIdLocked();
176            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
177            int result = doIntCommandNative(mInterfacePrefix + command);
178            localLog(toLog + " -> " + result);
179            if (DBG) Log.d(mTAG, "   returned " + result);
180            return result;
181        }
182    }
183
184    private String doStringCommand(String command) {
185        if (DBG) {
186            //GET_NETWORK commands flood the logs
187            if (!command.startsWith("GET_NETWORK")) {
188                Log.d(mTAG, "doString: [" + command + "]");
189            }
190        }
191        synchronized (mLock) {
192            int cmdId = getNewCmdIdLocked();
193            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
194            String result = doStringCommandNative(mInterfacePrefix + command);
195            if (result == null) {
196                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
197            } else {
198                if (!command.startsWith("STATUS-")) {
199                    localLog(toLog + " -> " + result);
200                }
201                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
202            }
203            return result;
204        }
205    }
206
207    private String doStringCommandWithoutLogging(String command) {
208        if (DBG) {
209            //GET_NETWORK commands flood the logs
210            if (!command.startsWith("GET_NETWORK")) {
211                Log.d(mTAG, "doString: [" + command + "]");
212            }
213        }
214        synchronized (mLock) {
215            return doStringCommandNative(mInterfacePrefix + command);
216        }
217    }
218
219    public boolean ping() {
220        String pong = doStringCommand("PING");
221        return (pong != null && pong.equals("PONG"));
222    }
223
224    public void setSupplicantLogLevel(String level) {
225        doStringCommand("LOG_LEVEL " + level);
226    }
227
228    public String getFreqCapability() {
229        return doStringCommand("GET_CAPABILITY freq");
230    }
231
232    public boolean scan(int type, String freqList) {
233        if (type == SCAN_WITHOUT_CONNECTION_SETUP) {
234            if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY");
235            else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
236        } else if (type == SCAN_WITH_CONNECTION_SETUP) {
237            if (freqList == null) return doBooleanCommand("SCAN");
238            else return doBooleanCommand("SCAN freq=" + freqList);
239        } else {
240            throw new IllegalArgumentException("Invalid scan type");
241        }
242    }
243
244    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
245     *
246     * Note that underneath we use a harsh-sounding "terminate" supplicant command
247     * for a graceful stop and a mild-sounding "stop" interface
248     * to kill the process
249     */
250    public boolean stopSupplicant() {
251        return doBooleanCommand("TERMINATE");
252    }
253
254    public String listNetworks() {
255        return doStringCommand("LIST_NETWORKS");
256    }
257
258    public String listNetworks(int last_id) {
259        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
260    }
261
262    public int addNetwork() {
263        return doIntCommand("ADD_NETWORK");
264    }
265
266    public boolean setNetworkVariable(int netId, String name, String value) {
267        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
268        return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
269    }
270
271    public String getNetworkVariable(int netId, String name) {
272        if (TextUtils.isEmpty(name)) return null;
273
274        // GET_NETWORK will likely flood the logs ...
275        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
276    }
277
278    public boolean removeNetwork(int netId) {
279        return doBooleanCommand("REMOVE_NETWORK " + netId);
280    }
281
282
283    private void logDbg(String debug) {
284        long now = SystemClock.elapsedRealtimeNanos();
285        String ts = String.format("[%,d us] ", now/1000);
286        Log.e("WifiNative: ", ts+debug+ " stack:"
287                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
288                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
289                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
290                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
291                + Thread.currentThread().getStackTrace()[6].getMethodName());
292
293    }
294    public boolean enableNetwork(int netId, boolean disableOthers) {
295        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
296                + " disableOthers=" + disableOthers);
297        if (disableOthers) {
298            return doBooleanCommand("SELECT_NETWORK " + netId);
299        } else {
300            return doBooleanCommand("ENABLE_NETWORK " + netId);
301        }
302    }
303
304    public boolean disableNetwork(int netId) {
305        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
306        return doBooleanCommand("DISABLE_NETWORK " + netId);
307    }
308
309    public boolean reconnect() {
310        if (DBG) logDbg("RECONNECT ");
311        return doBooleanCommand("RECONNECT");
312    }
313
314    public boolean reassociate() {
315        if (DBG) logDbg("REASSOCIATE ");
316        return doBooleanCommand("REASSOCIATE");
317    }
318
319    public boolean disconnect() {
320        if (DBG) logDbg("DISCONNECT ");
321        return doBooleanCommand("DISCONNECT");
322    }
323
324    public String status() {
325        return status(false);
326    }
327
328    public String status(boolean noEvents) {
329        if (noEvents) {
330            return doStringCommand("STATUS-NO_EVENTS");
331        } else {
332            return doStringCommand("STATUS");
333        }
334    }
335
336    public String getMacAddress() {
337        //Macaddr = XX.XX.XX.XX.XX.XX
338        String ret = doStringCommand("DRIVER MACADDR");
339        if (!TextUtils.isEmpty(ret)) {
340            String[] tokens = ret.split(" = ");
341            if (tokens.length == 2) return tokens[1];
342        }
343        return null;
344    }
345
346    /**
347     * Format of results:
348     * =================
349     * id=1
350     * bssid=68:7f:74:d7:1b:6e
351     * freq=2412
352     * level=-43
353     * tsf=1344621975160944
354     * age=2623
355     * flags=[WPA2-PSK-CCMP][WPS][ESS]
356     * ssid=zubyb
357     * ====
358     *
359     * RANGE=ALL gets all scan results
360     * RANGE=ID- gets results from ID
361     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
362     * 0                         0                        1                       0     2
363     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
364     * 0                         0                        0                       1     1   -> 9
365     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
366     * 1                         0                        0                       1     9   -> d
367     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
368     * 1                         0                        0                       0     8
369     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
370     * 0                         1                        1                       1     7
371     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
372     *
373     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
374     *
375     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
376     *  wpa_supplicant_ctrl_iface_bss:4315
377     *  print_bss_info
378     */
379    public String scanResults(int sid) {
380        return doStringCommandWithoutLogging("BSS RANGE=" + sid + "- MASK=0x29d87");
381    }
382
383    public String doCustomCommand(String command) {
384        return doStringCommand(command);
385    }
386
387    /**
388     * Format of result:
389     * id=1016
390     * bssid=00:03:7f:40:84:10
391     * freq=2462
392     * beacon_int=200
393     * capabilities=0x0431
394     * qual=0
395     * noise=0
396     * level=-46
397     * tsf=0000002669008476
398     * age=5
399     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
400     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
401     * ssid=QCA-HS20-R2-TEST
402     * p2p_device_name=
403     * p2p_config_methods=0x0SET_NE
404     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
405     * anqp_network_auth_type=010000
406     * anqp_roaming_consortium=03506f9a05001bc504bd
407     * anqp_ip_addr_type_availability=0c
408     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
409     * anqp_3gpp=000600040132f465
410     * anqp_domain_name=0b65786d61706c652e636f6d
411     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
412     * hs20_wan_metrics=01c40900008001000000000a00
413     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
414     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
415     */
416    public String scanResult(String bssid) {
417        return doStringCommand("BSS " + bssid);
418    }
419
420    /**
421     * Format of command
422     * DRIVER WLS_BATCHING SET SCANFREQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s
423     * where x is an ascii representation of an integer number of seconds between scans
424     *       r is an ascii representation of an integer number of scans per batch
425     *       y is an ascii representation of an integer number of the max AP to remember per scan
426     *       z, w, t represent a 1..n size list of channel numbers and/or 'A', 'B' values
427     *           indicating entire ranges of channels
428     *       s is an ascii representation of an integer number of highest-strength AP
429     *           for which we'd like approximate distance reported
430     *
431     * The return value is an ascii integer representing a guess of the number of scans
432     * the firmware can remember before it runs out of buffer space or -1 on error
433     */
434    public String setBatchedScanSettings(BatchedScanSettings settings) {
435        if (settings == null) {
436            return doStringCommand("DRIVER WLS_BATCHING STOP");
437        }
438        String cmd = "DRIVER WLS_BATCHING SET SCANFREQ=" + settings.scanIntervalSec;
439        cmd += " MSCAN=" + settings.maxScansPerBatch;
440        if (settings.maxApPerScan != BatchedScanSettings.UNSPECIFIED) {
441            cmd += " BESTN=" + settings.maxApPerScan;
442        }
443        if (settings.channelSet != null && !settings.channelSet.isEmpty()) {
444            cmd += " CHANNEL=<";
445            int i = 0;
446            for (String channel : settings.channelSet) {
447                cmd += (i > 0 ? "," : "") + channel;
448                ++i;
449            }
450            cmd += ">";
451        }
452        if (settings.maxApForDistance != BatchedScanSettings.UNSPECIFIED) {
453            cmd += " RTT=" + settings.maxApForDistance;
454        }
455        return doStringCommand(cmd);
456    }
457
458    public String getBatchedScanResults() {
459        return doStringCommand("DRIVER WLS_BATCHING GET");
460    }
461
462    public boolean startDriver() {
463        return doBooleanCommand("DRIVER START");
464    }
465
466    public boolean stopDriver() {
467        return doBooleanCommand("DRIVER STOP");
468    }
469
470
471    /**
472     * Start filtering out Multicast V4 packets
473     * @return {@code true} if the operation succeeded, {@code false} otherwise
474     *
475     * Multicast filtering rules work as follows:
476     *
477     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
478     * a power optimized mode (typically when screen goes off).
479     *
480     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
481     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
482     *
483     * DRIVER RXFILTER-ADD Num
484     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
485     *
486     * and DRIVER RXFILTER-START
487     * In order to stop the usage of these rules, we do
488     *
489     * DRIVER RXFILTER-STOP
490     * DRIVER RXFILTER-REMOVE Num
491     *   where Num is as described for RXFILTER-ADD
492     *
493     * The  SETSUSPENDOPT driver command overrides the filtering rules
494     */
495    public boolean startFilteringMulticastV4Packets() {
496        return doBooleanCommand("DRIVER RXFILTER-STOP")
497            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
498            && doBooleanCommand("DRIVER RXFILTER-START");
499    }
500
501    /**
502     * Stop filtering out Multicast V4 packets.
503     * @return {@code true} if the operation succeeded, {@code false} otherwise
504     */
505    public boolean stopFilteringMulticastV4Packets() {
506        return doBooleanCommand("DRIVER RXFILTER-STOP")
507            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
508            && doBooleanCommand("DRIVER RXFILTER-START");
509    }
510
511    /**
512     * Start filtering out Multicast V6 packets
513     * @return {@code true} if the operation succeeded, {@code false} otherwise
514     */
515    public boolean startFilteringMulticastV6Packets() {
516        return doBooleanCommand("DRIVER RXFILTER-STOP")
517            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
518            && doBooleanCommand("DRIVER RXFILTER-START");
519    }
520
521    /**
522     * Stop filtering out Multicast V6 packets.
523     * @return {@code true} if the operation succeeded, {@code false} otherwise
524     */
525    public boolean stopFilteringMulticastV6Packets() {
526        return doBooleanCommand("DRIVER RXFILTER-STOP")
527            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
528            && doBooleanCommand("DRIVER RXFILTER-START");
529    }
530
531    /**
532     * Set the operational frequency band
533     * @param band One of
534     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
535     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
536     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
537     * @return {@code true} if the operation succeeded, {@code false} otherwise
538     */
539    public boolean setBand(int band) {
540        String bandstr;
541
542        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
543            bandstr = "5G";
544        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
545            bandstr = "2G";
546        else
547            bandstr = "AUTO";
548        return doBooleanCommand("SET SETBAND " + bandstr);
549    }
550
551    /**
552      * Sets the bluetooth coexistence mode.
553      *
554      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
555      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
556      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
557      * @return Whether the mode was successfully set.
558      */
559    public boolean setBluetoothCoexistenceMode(int mode) {
560        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
561    }
562
563    /**
564     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
565     * some of the low-level scan parameters used by the driver are changed to
566     * reduce interference with A2DP streaming.
567     *
568     * @param isSet whether to enable or disable this mode
569     * @return {@code true} if the command succeeded, {@code false} otherwise.
570     */
571    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
572        if (setCoexScanMode) {
573            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
574        } else {
575            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
576        }
577    }
578
579    public void enableSaveConfig() {
580        doBooleanCommand("SET update_config 1");
581    }
582
583    public boolean saveConfig() {
584        return doBooleanCommand("SAVE_CONFIG");
585    }
586
587    public boolean addToBlacklist(String bssid) {
588        if (TextUtils.isEmpty(bssid)) return false;
589        return doBooleanCommand("BLACKLIST " + bssid);
590    }
591
592    public boolean clearBlacklist() {
593        return doBooleanCommand("BLACKLIST clear");
594    }
595
596    public boolean setSuspendOptimizations(boolean enabled) {
597       // if (mSuspendOptEnabled == enabled) return true;
598        mSuspendOptEnabled = enabled;
599
600        Log.e("native", "do suspend " + enabled);
601        if (enabled) {
602            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
603        } else {
604            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
605        }
606    }
607
608    public boolean setCountryCode(String countryCode) {
609        if (countryCode != null)
610            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
611        else
612            return doBooleanCommand("DRIVER COUNTRY");
613    }
614
615    public void enableBackgroundScan(boolean enable) {
616        if (enable) {
617            doBooleanCommand("SET pno 1");
618        } else {
619            doBooleanCommand("SET pno 0");
620        }
621    }
622
623    public void enableAutoConnect(boolean enable) {
624        if (enable) {
625            doBooleanCommand("STA_AUTOCONNECT 1");
626        } else {
627            doBooleanCommand("STA_AUTOCONNECT 0");
628        }
629    }
630
631    public void setScanInterval(int scanInterval) {
632        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
633    }
634
635    public void startTdls(String macAddr, boolean enable) {
636        if (enable) {
637            doBooleanCommand("TDLS_DISCOVER " + macAddr);
638            doBooleanCommand("TDLS_SETUP " + macAddr);
639        } else {
640            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
641        }
642    }
643
644    /** Example output:
645     * RSSI=-65
646     * LINKSPEED=48
647     * NOISE=9999
648     * FREQUENCY=0
649     */
650    public String signalPoll() {
651        return doStringCommandWithoutLogging("SIGNAL_POLL");
652    }
653
654    /** Example outout:
655     * TXGOOD=396
656     * TXBAD=1
657     */
658    public String pktcntPoll() {
659        return doStringCommand("PKTCNT_POLL");
660    }
661
662    public void bssFlush() {
663        doBooleanCommand("BSS_FLUSH 0");
664    }
665
666    public boolean startWpsPbc(String bssid) {
667        if (TextUtils.isEmpty(bssid)) {
668            return doBooleanCommand("WPS_PBC");
669        } else {
670            return doBooleanCommand("WPS_PBC " + bssid);
671        }
672    }
673
674    public boolean startWpsPbc(String iface, String bssid) {
675        synchronized (mLock) {
676            if (TextUtils.isEmpty(bssid)) {
677                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
678            } else {
679                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
680            }
681        }
682    }
683
684    public boolean startWpsPinKeypad(String pin) {
685        if (TextUtils.isEmpty(pin)) return false;
686        return doBooleanCommand("WPS_PIN any " + pin);
687    }
688
689    public boolean startWpsPinKeypad(String iface, String pin) {
690        if (TextUtils.isEmpty(pin)) return false;
691        synchronized (mLock) {
692            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
693        }
694    }
695
696
697    public String startWpsPinDisplay(String bssid) {
698        if (TextUtils.isEmpty(bssid)) {
699            return doStringCommand("WPS_PIN any");
700        } else {
701            return doStringCommand("WPS_PIN " + bssid);
702        }
703    }
704
705    public String startWpsPinDisplay(String iface, String bssid) {
706        synchronized (mLock) {
707            if (TextUtils.isEmpty(bssid)) {
708                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
709            } else {
710                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
711            }
712        }
713    }
714
715    public boolean setExternalSim(boolean external) {
716        synchronized (mLock) {
717            String value = external ? "1" : "0";
718            Log.d(TAG, "Setting external_sim to " + value);
719            return doBooleanCommand("SET external_sim " + value);
720        }
721    }
722
723    public boolean simAuthResponse(int id, String response) {
724        synchronized (mLock) {
725            return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-AUTH" + response);
726        }
727    }
728
729    /* Configures an access point connection */
730    public boolean startWpsRegistrar(String bssid, String pin) {
731        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
732        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
733    }
734
735    public boolean cancelWps() {
736        return doBooleanCommand("WPS_CANCEL");
737    }
738
739    public boolean setPersistentReconnect(boolean enabled) {
740        int value = (enabled == true) ? 1 : 0;
741        return doBooleanCommand("SET persistent_reconnect " + value);
742    }
743
744    public boolean setDeviceName(String name) {
745        return doBooleanCommand("SET device_name " + name);
746    }
747
748    public boolean setDeviceType(String type) {
749        return doBooleanCommand("SET device_type " + type);
750    }
751
752    public boolean setConfigMethods(String cfg) {
753        return doBooleanCommand("SET config_methods " + cfg);
754    }
755
756    public boolean setManufacturer(String value) {
757        return doBooleanCommand("SET manufacturer " + value);
758    }
759
760    public boolean setModelName(String value) {
761        return doBooleanCommand("SET model_name " + value);
762    }
763
764    public boolean setModelNumber(String value) {
765        return doBooleanCommand("SET model_number " + value);
766    }
767
768    public boolean setSerialNumber(String value) {
769        return doBooleanCommand("SET serial_number " + value);
770    }
771
772    public boolean setP2pSsidPostfix(String postfix) {
773        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
774    }
775
776    public boolean setP2pGroupIdle(String iface, int time) {
777        synchronized (mLock) {
778            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
779        }
780    }
781
782    public void setPowerSave(boolean enabled) {
783        if (enabled) {
784            doBooleanCommand("SET ps 1");
785        } else {
786            doBooleanCommand("SET ps 0");
787        }
788    }
789
790    public boolean setP2pPowerSave(String iface, boolean enabled) {
791        synchronized (mLock) {
792            if (enabled) {
793                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
794            } else {
795                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
796            }
797        }
798    }
799
800    public boolean setWfdEnable(boolean enable) {
801        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
802    }
803
804    public boolean setWfdDeviceInfo(String hex) {
805        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
806    }
807
808    /**
809     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
810     * P2P connection over STA
811     */
812    public boolean setConcurrencyPriority(String s) {
813        return doBooleanCommand("P2P_SET conc_pref " + s);
814    }
815
816    public boolean p2pFind() {
817        return doBooleanCommand("P2P_FIND");
818    }
819
820    public boolean p2pFind(int timeout) {
821        if (timeout <= 0) {
822            return p2pFind();
823        }
824        return doBooleanCommand("P2P_FIND " + timeout);
825    }
826
827    public boolean p2pStopFind() {
828       return doBooleanCommand("P2P_STOP_FIND");
829    }
830
831    public boolean p2pListen() {
832        return doBooleanCommand("P2P_LISTEN");
833    }
834
835    public boolean p2pListen(int timeout) {
836        if (timeout <= 0) {
837            return p2pListen();
838        }
839        return doBooleanCommand("P2P_LISTEN " + timeout);
840    }
841
842    public boolean p2pExtListen(boolean enable, int period, int interval) {
843        if (enable && interval < period) {
844            return false;
845        }
846        return doBooleanCommand("P2P_EXT_LISTEN"
847                    + (enable ? (" " + period + " " + interval) : ""));
848    }
849
850    public boolean p2pSetChannel(int lc, int oc) {
851        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
852
853        if (lc >=1 && lc <= 11) {
854            if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
855                return false;
856            }
857        } else if (lc != 0) {
858            return false;
859        }
860
861        if (oc >= 1 && oc <= 165 ) {
862            int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
863            return doBooleanCommand("P2P_SET disallow_freq 1000-"
864                    + (freq - 5) + "," + (freq + 5) + "-6000");
865        } else if (oc == 0) {
866            /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
867            return doBooleanCommand("P2P_SET disallow_freq \"\"");
868        }
869
870        return false;
871    }
872
873    public boolean p2pFlush() {
874        return doBooleanCommand("P2P_FLUSH");
875    }
876
877    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
878        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
879    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
880        if (config == null) return null;
881        List<String> args = new ArrayList<String>();
882        WpsInfo wps = config.wps;
883        args.add(config.deviceAddress);
884
885        switch (wps.setup) {
886            case WpsInfo.PBC:
887                args.add("pbc");
888                break;
889            case WpsInfo.DISPLAY:
890                if (TextUtils.isEmpty(wps.pin)) {
891                    args.add("pin");
892                } else {
893                    args.add(wps.pin);
894                }
895                args.add("display");
896                break;
897            case WpsInfo.KEYPAD:
898                args.add(wps.pin);
899                args.add("keypad");
900                break;
901            case WpsInfo.LABEL:
902                args.add(wps.pin);
903                args.add("label");
904            default:
905                break;
906        }
907
908        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
909            args.add("persistent");
910        }
911
912        if (joinExistingGroup) {
913            args.add("join");
914        } else {
915            //TODO: This can be adapted based on device plugged in state and
916            //device battery state
917            int groupOwnerIntent = config.groupOwnerIntent;
918            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
919                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
920            }
921            args.add("go_intent=" + groupOwnerIntent);
922        }
923
924        String command = "P2P_CONNECT ";
925        for (String s : args) command += s + " ";
926
927        return doStringCommand(command);
928    }
929
930    public boolean p2pCancelConnect() {
931        return doBooleanCommand("P2P_CANCEL");
932    }
933
934    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
935        if (config == null) return false;
936
937        switch (config.wps.setup) {
938            case WpsInfo.PBC:
939                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
940            case WpsInfo.DISPLAY:
941                //We are doing display, so provision discovery is keypad
942                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
943            case WpsInfo.KEYPAD:
944                //We are doing keypad, so provision discovery is display
945                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
946            default:
947                break;
948        }
949        return false;
950    }
951
952    public boolean p2pGroupAdd(boolean persistent) {
953        if (persistent) {
954            return doBooleanCommand("P2P_GROUP_ADD persistent");
955        }
956        return doBooleanCommand("P2P_GROUP_ADD");
957    }
958
959    public boolean p2pGroupAdd(int netId) {
960        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
961    }
962
963    public boolean p2pGroupRemove(String iface) {
964        if (TextUtils.isEmpty(iface)) return false;
965        synchronized (mLock) {
966            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
967        }
968    }
969
970    public boolean p2pReject(String deviceAddress) {
971        return doBooleanCommand("P2P_REJECT " + deviceAddress);
972    }
973
974    /* Invite a peer to a group */
975    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
976        if (TextUtils.isEmpty(deviceAddress)) return false;
977
978        if (group == null) {
979            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
980        } else {
981            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
982                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
983        }
984    }
985
986    /* Reinvoke a persistent connection */
987    public boolean p2pReinvoke(int netId, String deviceAddress) {
988        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
989
990        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
991    }
992
993    public String p2pGetSsid(String deviceAddress) {
994        return p2pGetParam(deviceAddress, "oper_ssid");
995    }
996
997    public String p2pGetDeviceAddress() {
998
999        Log.d(TAG, "p2pGetDeviceAddress");
1000
1001        String status = null;
1002
1003        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
1004        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
1005
1006        synchronized (mLock) {
1007            status = doStringCommandNative("STATUS");
1008        }
1009
1010        String result = "";
1011        if (status != null) {
1012            String[] tokens = status.split("\n");
1013            for (String token : tokens) {
1014                if (token.startsWith("p2p_device_address=")) {
1015                    String[] nameValue = token.split("=");
1016                    if (nameValue.length != 2)
1017                        break;
1018                    result = nameValue[1];
1019                }
1020            }
1021        }
1022
1023        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
1024        return result;
1025    }
1026
1027    public int getGroupCapability(String deviceAddress) {
1028        int gc = 0;
1029        if (TextUtils.isEmpty(deviceAddress)) return gc;
1030        String peerInfo = p2pPeer(deviceAddress);
1031        if (TextUtils.isEmpty(peerInfo)) return gc;
1032
1033        String[] tokens = peerInfo.split("\n");
1034        for (String token : tokens) {
1035            if (token.startsWith("group_capab=")) {
1036                String[] nameValue = token.split("=");
1037                if (nameValue.length != 2) break;
1038                try {
1039                    return Integer.decode(nameValue[1]);
1040                } catch(NumberFormatException e) {
1041                    return gc;
1042                }
1043            }
1044        }
1045        return gc;
1046    }
1047
1048    public String p2pPeer(String deviceAddress) {
1049        return doStringCommand("P2P_PEER " + deviceAddress);
1050    }
1051
1052    private String p2pGetParam(String deviceAddress, String key) {
1053        if (deviceAddress == null) return null;
1054
1055        String peerInfo = p2pPeer(deviceAddress);
1056        if (peerInfo == null) return null;
1057        String[] tokens= peerInfo.split("\n");
1058
1059        key += "=";
1060        for (String token : tokens) {
1061            if (token.startsWith(key)) {
1062                String[] nameValue = token.split("=");
1063                if (nameValue.length != 2) break;
1064                return nameValue[1];
1065            }
1066        }
1067        return null;
1068    }
1069
1070    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1071        /*
1072         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1073         * P2P_SERVICE_ADD upnp <version hex> <service>
1074         *
1075         * e.g)
1076         * [Bonjour]
1077         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1078         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1079         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1080         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1081         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1082         *
1083         * [UPnP]
1084         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1085         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1086         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1087         * -org:device:InternetGatewayDevice:1
1088         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1089         * -org:service:ContentDirectory:2
1090         */
1091        for (String s : servInfo.getSupplicantQueryList()) {
1092            String command = "P2P_SERVICE_ADD";
1093            command += (" " + s);
1094            if (!doBooleanCommand(command)) {
1095                return false;
1096            }
1097        }
1098        return true;
1099    }
1100
1101    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1102        /*
1103         * P2P_SERVICE_DEL bonjour <query hexdump>
1104         * P2P_SERVICE_DEL upnp <version hex> <service>
1105         */
1106        for (String s : servInfo.getSupplicantQueryList()) {
1107            String command = "P2P_SERVICE_DEL ";
1108
1109            String[] data = s.split(" ");
1110            if (data.length < 2) {
1111                return false;
1112            }
1113            if ("upnp".equals(data[0])) {
1114                command += s;
1115            } else if ("bonjour".equals(data[0])) {
1116                command += data[0];
1117                command += (" " + data[1]);
1118            } else {
1119                return false;
1120            }
1121            if (!doBooleanCommand(command)) {
1122                return false;
1123            }
1124        }
1125        return true;
1126    }
1127
1128    public boolean p2pServiceFlush() {
1129        return doBooleanCommand("P2P_SERVICE_FLUSH");
1130    }
1131
1132    public String p2pServDiscReq(String addr, String query) {
1133        String command = "P2P_SERV_DISC_REQ";
1134        command += (" " + addr);
1135        command += (" " + query);
1136
1137        return doStringCommand(command);
1138    }
1139
1140    public boolean p2pServDiscCancelReq(String id) {
1141        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1142    }
1143
1144    /* Set the current mode of miracast operation.
1145     *  0 = disabled
1146     *  1 = operating as source
1147     *  2 = operating as sink
1148     */
1149    public void setMiracastMode(int mode) {
1150        // Note: optional feature on the driver. It is ok for this to fail.
1151        doBooleanCommand("DRIVER MIRACAST " + mode);
1152    }
1153
1154    public boolean fetchAnqp(String bssid, String subtypes) {
1155        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1156    }
1157
1158    /* WIFI HAL support */
1159
1160    private static final String TAG = "WifiNative-HAL";
1161    private static long sWifiHalHandle = 0;  /* used by JNI to save wifi_handle */
1162    private static long[] sWifiIfaceHandles = null;  /* used by JNI to save interface handles */
1163    private static int sWlan0Index = -1;
1164    private static int sP2p0Index = -1;
1165
1166    private static boolean sHalIsStarted = false;
1167    private static boolean sHalFailed = false;
1168
1169    private static native boolean startHalNative();
1170    private static native void stopHalNative();
1171    private static native void waitForHalEventNative();
1172
1173    private static class MonitorThread extends Thread {
1174        public void run() {
1175            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
1176            waitForHalEventNative();
1177        }
1178    }
1179
1180    synchronized public static boolean startHal() {
1181        Log.i(TAG, "startHal");
1182        synchronized (mLock) {
1183            if (sHalFailed)
1184                return false;
1185            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1186                new MonitorThread().start();
1187                sHalIsStarted = true;
1188                return true;
1189            } else {
1190                Log.i(TAG, "Could not start hal");
1191                sHalIsStarted = false;
1192                sHalFailed = true;
1193                return false;
1194            }
1195        }
1196    }
1197
1198    synchronized public static void stopHal() {
1199        stopHalNative();
1200    }
1201
1202    private static native int getInterfacesNative();
1203
1204    synchronized public static int getInterfaces() {
1205        synchronized (mLock) {
1206            if (sWifiIfaceHandles == null) {
1207                int num = getInterfacesNative();
1208                int wifi_num = 0;
1209                for (int i = 0; i < num; i++) {
1210                    String name = getInterfaceNameNative(i);
1211                    Log.i(TAG, "interface[" + i + "] = " + name);
1212                    if (name.equals("wlan0")) {
1213                        sWlan0Index = i;
1214                        wifi_num++;
1215                    } else if (name.equals("p2p0")) {
1216                        sP2p0Index = i;
1217                        wifi_num++;
1218                    }
1219                }
1220                return wifi_num;
1221            } else {
1222                return sWifiIfaceHandles.length;
1223            }
1224        }
1225    }
1226
1227    private static native String getInterfaceNameNative(int index);
1228    synchronized public static String getInterfaceName(int index) {
1229        return getInterfaceNameNative(index);
1230    }
1231
1232    public static class ScanCapabilities {
1233        public int  max_scan_cache_size;                 // in number of scan results??
1234        public int  max_scan_buckets;
1235        public int  max_ap_cache_per_scan;
1236        public int  max_rssi_sample_size;
1237        public int  max_scan_reporting_threshold;        // in number of scan results??
1238        public int  max_hotlist_aps;
1239        public int  max_significant_wifi_change_aps;
1240    }
1241
1242    public static boolean getScanCapabilities(ScanCapabilities capabilities) {
1243        return getScanCapabilitiesNative(sWlan0Index, capabilities);
1244    }
1245
1246    private static native boolean getScanCapabilitiesNative(
1247            int iface, ScanCapabilities capabilities);
1248
1249    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1250    private static native boolean stopScanNative(int iface, int id);
1251    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1252    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1253
1254    public static class ChannelSettings {
1255        int frequency;
1256        int dwell_time_ms;
1257        boolean passive;
1258    }
1259
1260    public static class BucketSettings {
1261        int bucket;
1262        int band;
1263        int period_ms;
1264        int report_events;
1265        int num_channels;
1266        ChannelSettings channels[];
1267    }
1268
1269    public static class ScanSettings {
1270        int base_period_ms;
1271        int max_ap_per_scan;
1272        int report_threshold_percent;
1273        int report_threshold_num_scans;
1274        int num_buckets;
1275        BucketSettings buckets[];
1276    }
1277
1278    public static interface ScanEventHandler {
1279        void onScanResultsAvailable();
1280        void onFullScanResult(ScanResult fullScanResult);
1281        void onScanStatus();
1282        void onScanPaused(WifiScanner.ScanData[] data);
1283        void onScanRestarted();
1284    }
1285
1286    synchronized static void onScanResultsAvailable(int id) {
1287        if (sScanEventHandler  != null) {
1288            sScanEventHandler.onScanResultsAvailable();
1289        }
1290    }
1291
1292    /* scan status, keep these values in sync with gscan.h */
1293    private static int WIFI_SCAN_BUFFER_FULL = 0;
1294    private static int WIFI_SCAN_COMPLETE = 1;
1295
1296    synchronized static void onScanStatus(int status) {
1297        Log.i(TAG, "Got a scan status changed event, status = " + status);
1298
1299        if (status == WIFI_SCAN_BUFFER_FULL) {
1300            /* we have a separate event to take care of this */
1301        } else if (status == WIFI_SCAN_COMPLETE) {
1302            if (sScanEventHandler  != null) {
1303                sScanEventHandler.onScanStatus();
1304            }
1305        }
1306    }
1307
1308    synchronized static void onFullScanResult(int id, ScanResult result, byte bytes[]) {
1309        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " +
1310                "num = " + bytes.length);
1311
1312        if (sScanEventHandler == null) {
1313            return;
1314        }
1315
1316        int num = 0;
1317        for (int i = 0; i < bytes.length; ) {
1318            int type  = bytes[i] & 0xFF;
1319            int len = bytes[i + 1] & 0xFF;
1320
1321            if (i + len + 2 > bytes.length) {
1322                Log.w(TAG, "bad length " + len + " of IE " + type + " from " + result.BSSID);
1323                Log.w(TAG, "ignoring the rest of the IEs");
1324                break;
1325            }
1326            num++;
1327            i += len + 2;
1328            if (DBG) Log.i(TAG, "bytes[" + i + "] = [" + type + ", " + len + "]" + ", " +
1329                    "next = " + i);
1330        }
1331
1332        ScanResult.InformationElement elements[] = new ScanResult.InformationElement[num];
1333        for (int i = 0, index = 0; i < num; i++) {
1334            int type  = bytes[index] & 0xFF;
1335            int len = bytes[index + 1] & 0xFF;
1336            if (DBG) Log.i(TAG, "index = " + index + ", type = " + type + ", len = " + len);
1337            ScanResult.InformationElement elem = new ScanResult.InformationElement();
1338            elem.id = type;
1339            elem.bytes = new byte[len];
1340            for (int j = 0; j < len; j++) {
1341                elem.bytes[j] = bytes[index + j + 2];
1342            }
1343            elements[i] = elem;
1344            index += (len + 2);
1345        }
1346
1347        result.informationElements = elements;
1348        sScanEventHandler.onFullScanResult(result);
1349    }
1350
1351    private static int sScanCmdId = 0;
1352    private static ScanEventHandler sScanEventHandler;
1353    private static ScanSettings sScanSettings;
1354
1355    synchronized public static boolean startScan(
1356            ScanSettings settings, ScanEventHandler eventHandler) {
1357        synchronized (mLock) {
1358
1359            if (sScanCmdId != 0) {
1360                stopScan();
1361            } else if (sScanSettings != null || sScanEventHandler != null) {
1362                /* current scan is paused; no need to stop it */
1363            }
1364
1365            sScanCmdId = getNewCmdIdLocked();
1366
1367            sScanSettings = settings;
1368            sScanEventHandler = eventHandler;
1369
1370            if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1371                sScanEventHandler = null;
1372                sScanSettings = null;
1373                sScanCmdId = 0;
1374                return false;
1375            }
1376
1377            return true;
1378        }
1379    }
1380
1381    synchronized public static void stopScan() {
1382        synchronized (mLock) {
1383            stopScanNative(sWlan0Index, sScanCmdId);
1384            sScanSettings = null;
1385            sScanEventHandler = null;
1386            sScanCmdId = 0;
1387        }
1388    }
1389
1390    synchronized public static void pauseScan() {
1391        synchronized (mLock) {
1392            if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1393                Log.d(TAG, "Pausing scan");
1394                WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1395                stopScanNative(sWlan0Index, sScanCmdId);
1396                sScanCmdId = 0;
1397                sScanEventHandler.onScanPaused(scanData);
1398            }
1399        }
1400    }
1401
1402    synchronized public static void restartScan() {
1403        synchronized (mLock) {
1404            if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
1405                Log.d(TAG, "Restarting scan");
1406                ScanEventHandler handler = sScanEventHandler;
1407                ScanSettings settings = sScanSettings;
1408                if (startScan(sScanSettings, sScanEventHandler)) {
1409                    sScanEventHandler.onScanRestarted();
1410                } else {
1411                    /* we are still paused; don't change state */
1412                    sScanEventHandler = handler;
1413                    sScanSettings = settings;
1414                }
1415            }
1416        }
1417    }
1418
1419    synchronized public static WifiScanner.ScanData[] getScanResults(boolean flush) {
1420        synchronized (mLock) {
1421            return getScanResultsNative(sWlan0Index, flush);
1422        }
1423    }
1424
1425    public static interface HotlistEventHandler {
1426        void onHotlistApFound (ScanResult[] result);
1427        void onHotlistApLost  (ScanResult[] result);
1428    }
1429
1430    private static int sHotlistCmdId = 0;
1431    private static HotlistEventHandler sHotlistEventHandler;
1432
1433    private native static boolean setHotlistNative(int iface, int id,
1434            WifiScanner.HotlistSettings settings);
1435    private native static boolean resetHotlistNative(int iface, int id);
1436
1437    synchronized public static boolean setHotlist(WifiScanner.HotlistSettings settings,
1438                                    HotlistEventHandler eventHandler) {
1439        synchronized (mLock) {
1440            if (sHotlistCmdId != 0) {
1441                return false;
1442            } else {
1443                sHotlistCmdId = getNewCmdIdLocked();
1444            }
1445
1446            sHotlistEventHandler = eventHandler;
1447            if (setHotlistNative(sWlan0Index, sScanCmdId, settings) == false) {
1448                sHotlistEventHandler = null;
1449                return false;
1450            }
1451
1452            return true;
1453        }
1454    }
1455
1456    synchronized public static void resetHotlist() {
1457        synchronized (mLock) {
1458            if (sHotlistCmdId != 0) {
1459                resetHotlistNative(sWlan0Index, sHotlistCmdId);
1460                sHotlistCmdId = 0;
1461                sHotlistEventHandler = null;
1462            }
1463        }
1464    }
1465
1466    synchronized public static void onHotlistApFound(int id, ScanResult[] results) {
1467        synchronized (mLock) {
1468            if (sHotlistCmdId != 0) {
1469                sHotlistEventHandler.onHotlistApFound(results);
1470            } else {
1471                /* this can happen because of race conditions */
1472                Log.d(TAG, "Ignoring hotlist AP found event");
1473            }
1474        }
1475    }
1476
1477    synchronized public static void onHotlistApLost(int id, ScanResult[] results) {
1478        synchronized (mLock) {
1479            if (sHotlistCmdId != 0) {
1480                sHotlistEventHandler.onHotlistApLost(results);
1481            } else {
1482                /* this can happen because of race conditions */
1483                Log.d(TAG, "Ignoring hotlist AP lost event");
1484            }
1485        }
1486    }
1487
1488    public static interface SignificantWifiChangeEventHandler {
1489        void onChangesFound(ScanResult[] result);
1490    }
1491
1492    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
1493    private static int sSignificantWifiChangeCmdId;
1494
1495    private static native boolean trackSignificantWifiChangeNative(
1496            int iface, int id, WifiScanner.WifiChangeSettings settings);
1497    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
1498
1499    synchronized public static boolean trackSignificantWifiChange(
1500            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
1501        synchronized (mLock) {
1502            if (sSignificantWifiChangeCmdId != 0) {
1503                return false;
1504            } else {
1505                sSignificantWifiChangeCmdId = getNewCmdIdLocked();
1506            }
1507
1508            sSignificantWifiChangeHandler = handler;
1509            if (trackSignificantWifiChangeNative(sWlan0Index, sScanCmdId, settings) == false) {
1510                sSignificantWifiChangeHandler = null;
1511                return false;
1512            }
1513
1514            return true;
1515        }
1516    }
1517
1518    synchronized static void untrackSignificantWifiChange() {
1519        synchronized (mLock) {
1520            if (sSignificantWifiChangeCmdId != 0) {
1521                untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
1522                sSignificantWifiChangeCmdId = 0;
1523                sSignificantWifiChangeHandler = null;
1524            }
1525        }
1526    }
1527
1528    synchronized static void onSignificantWifiChange(int id, ScanResult[] results) {
1529        synchronized (mLock) {
1530            if (sSignificantWifiChangeCmdId != 0) {
1531                sSignificantWifiChangeHandler.onChangesFound(results);
1532            } else {
1533                /* this can happen because of race conditions */
1534                Log.d(TAG, "Ignoring significant wifi change");
1535            }
1536        }
1537    }
1538
1539    synchronized public static WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
1540        // TODO: use correct iface name to Index translation
1541        if (iface == null) return null;
1542        synchronized (mLock) {
1543            if (!sHalIsStarted)
1544                startHal();
1545            if (sHalIsStarted)
1546                return getWifiLinkLayerStatsNative(sWlan0Index);
1547        }
1548        return null;
1549    }
1550
1551    /*
1552     * NFC-related calls
1553     */
1554    public String getNfcWpsConfigurationToken(int netId) {
1555        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1556    }
1557
1558    public String getNfcHandoverRequest() {
1559        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1560    }
1561
1562    public String getNfcHandoverSelect() {
1563        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1564    }
1565
1566    public boolean initiatorReportNfcHandover(String selectMessage) {
1567        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1568    }
1569
1570    public boolean responderReportNfcHandover(String requestMessage) {
1571        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1572    }
1573
1574    public static native int getSupportedFeatureSetNative(int iface);
1575    synchronized public static int getSupportedFeatureSet() {
1576        return getSupportedFeatureSetNative(sWlan0Index);
1577    }
1578
1579    /* Rtt related commands/events */
1580    public static interface RttEventHandler {
1581        void onRttResults(RttManager.RttResult[] result);
1582    }
1583
1584    private static RttEventHandler sRttEventHandler;
1585    private static int sRttCmdId;
1586
1587    synchronized private static void onRttResults(int id, RttManager.RttResult[] results) {
1588        if (id == sRttCmdId) {
1589            Log.d(TAG, "Received " + results.length + " rtt results");
1590            sRttEventHandler.onRttResults(results);
1591            sRttCmdId = 0;
1592        } else {
1593            Log.d(TAG, "Received event for unknown cmd = " + id + ", current id = " + sRttCmdId);
1594        }
1595    }
1596
1597    private static native boolean requestRangeNative(
1598            int iface, int id, RttManager.RttParams[] params);
1599    private static native boolean cancelRangeRequestNative(
1600            int iface, int id, RttManager.RttParams[] params);
1601
1602    synchronized public static boolean requestRtt(
1603            RttManager.RttParams[] params, RttEventHandler handler) {
1604        synchronized (mLock) {
1605            if (sRttCmdId != 0) {
1606                return false;
1607            } else {
1608                sRttCmdId = getNewCmdIdLocked();
1609            }
1610            sRttEventHandler = handler;
1611            return requestRangeNative(sWlan0Index, sRttCmdId, params);
1612        }
1613    }
1614
1615    synchronized public static boolean cancelRtt(RttManager.RttParams[] params) {
1616        synchronized(mLock) {
1617            if (sRttCmdId == 0) {
1618                return false;
1619            }
1620
1621            if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
1622                sRttEventHandler = null;
1623                return true;
1624            } else {
1625                return false;
1626            }
1627        }
1628    }
1629
1630    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
1631
1632    synchronized public static boolean setScanningMacOui(byte[] oui) {
1633        synchronized (mLock) {
1634            if (startHal()) {
1635                return setScanningMacOuiNative(sWlan0Index, oui);
1636            } else {
1637                return false;
1638            }
1639        }
1640    }
1641
1642    private static native int[] getChannelsForBandNative(
1643            int iface, int band);
1644
1645    synchronized public static int [] getChannelsForBand(int band) {
1646        synchronized (mLock) {
1647            if (startHal()) {
1648                return getChannelsForBandNative(sWlan0Index, band);
1649            } else {
1650                return null;
1651            }
1652        }
1653    }
1654
1655
1656    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
1657    synchronized public static boolean setDfsFlag(boolean dfsOn) {
1658        synchronized (mLock) {
1659            if (startHal()) {
1660                return setDfsFlagNative(sWlan0Index, dfsOn);
1661            } else {
1662                return false;
1663            }
1664        }
1665    }
1666
1667    private static native boolean toggleInterfaceNative(int on);
1668    synchronized public static boolean toggleInterface(int on) {
1669        synchronized (mLock) {
1670            if (startHal()) {
1671                return toggleInterfaceNative(0);
1672            } else {
1673
1674                return false;
1675            }
1676        }
1677    }
1678}
1679