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